A game about forced loneliness, made by TACStudios
1using System;
2using System.Diagnostics;
3#if !BURST_COMPILER_SHARED
4using Unity.Jobs.LowLevel.Unsafe;
5#endif
6
7namespace Unity.Burst
8{
9 /// <summary>
10 /// Provides helper intrinsics that can be used at runtime.
11 /// </summary>
12#if BURST_COMPILER_SHARED
13 internal static class BurstRuntimeInternal
14#else
15 public static class BurstRuntime
16#endif
17 {
18 /// <summary>
19 /// Gets a 32-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>
20 /// </summary>
21 /// <typeparam name="T">The type to compute the hash from</typeparam>
22 /// <returns>The 32-bit hashcode.</returns>
23 public static int GetHashCode32<T>()
24 {
25 return HashCode32<T>.Value;
26 }
27
28 /// <summary>
29 /// Gets a 32-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>
30 /// This method cannot be used from a burst job.
31 /// </summary>
32 /// <param name="type">The type to compute the hash from</param>
33 /// <returns>The 32-bit hashcode.</returns>
34 public static int GetHashCode32(Type type)
35 {
36 return HashStringWithFNV1A32(type.AssemblyQualifiedName);
37 }
38
39 /// <summary>
40 /// Gets a 64-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>
41 /// </summary>
42 /// <typeparam name="T">The type to compute the hash from</typeparam>
43 /// <returns>The 64-bit hashcode.</returns>
44 public static long GetHashCode64<T>()
45 {
46 return HashCode64<T>.Value;
47 }
48
49 /// <summary>
50 /// Gets a 64-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>.
51 /// This method cannot be used from a burst job.
52 /// </summary>
53 /// <param name="type">Type to calculate a hash for</param>
54 /// <returns>The 64-bit hashcode.</returns>
55 public static long GetHashCode64(Type type)
56 {
57 return HashStringWithFNV1A64(type.AssemblyQualifiedName);
58 }
59
60 // method internal as it is used by the compiler directly
61 internal static int HashStringWithFNV1A32(string text)
62 {
63 // Using http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-1a
64 // with basis and prime:
65 const uint offsetBasis = 2166136261;
66 const uint prime = 16777619;
67
68 uint result = offsetBasis;
69 foreach (var c in text)
70 {
71 result = prime * (result ^ (byte)(c & 255));
72 result = prime * (result ^ (byte)(c >> 8));
73 }
74 return (int)result;
75 }
76
77 // method internal as it is used by the compiler directly
78 // WARNING: This **must** be kept in sync with the definition in ILPostProcessing.cs!
79 internal static long HashStringWithFNV1A64(string text)
80 {
81 // Using http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-1a
82 // with basis and prime:
83 const ulong offsetBasis = 14695981039346656037;
84 const ulong prime = 1099511628211;
85
86 ulong result = offsetBasis;
87
88 foreach (var c in text)
89 {
90 result = prime * (result ^ (byte)(c & 255));
91 result = prime * (result ^ (byte)(c >> 8));
92 }
93
94 return (long)result;
95 }
96
97 private struct HashCode32<T>
98 {
99 public static readonly int Value = HashStringWithFNV1A32(typeof(T).AssemblyQualifiedName);
100 }
101
102 private struct HashCode64<T>
103 {
104 public static readonly long Value = HashStringWithFNV1A64(typeof(T).AssemblyQualifiedName);
105 }
106
107#if !BURST_COMPILER_SHARED
108
109 /// <summary>
110 /// Allows for loading additional Burst native libraries
111 /// Important: Designed for Play mode / Desktop Standalone Players ONLY
112 /// In Editor, any libraries that have been loaded will be unloaded on exit of playmode
113 /// Only supported from 2021.1 and later. You can use BurstCompiler.IsLoadAdditionalLibrarySupported() to confirm it is available.
114 /// </summary>
115 /// <param name="pathToLibBurstGenerated">Absolute filesystem location of bursted library to load</param>
116 /// <returns>true if the library was loaded successfully</returns>
117 public static bool LoadAdditionalLibrary(string pathToLibBurstGenerated)
118 {
119 if (BurstCompiler.IsLoadAdditionalLibrarySupported())
120 {
121 return LoadAdditionalLibraryInternal(pathToLibBurstGenerated);
122 }
123 return false;
124 }
125
126 internal static bool LoadAdditionalLibraryInternal(string pathToLibBurstGenerated)
127 {
128 return (bool)typeof(Unity.Burst.LowLevel.BurstCompilerService).GetMethod("LoadBurstLibrary").Invoke(null, new object[] { pathToLibBurstGenerated });
129 }
130
131
132#if UNITY_2022_1_OR_NEWER
133 [Preserve]
134 internal static unsafe void RuntimeLog(byte* message, int logType, byte* fileName, int lineNumber)
135 {
136 Unity.Burst.LowLevel.BurstCompilerService.RuntimeLog((byte*) 0, (Unity.Burst.LowLevel.BurstCompilerService.BurstLogType)logType, message, fileName, lineNumber);
137 }
138#endif
139
140 internal static void Initialize()
141 {
142 }
143
144 // Prevent BurstCompilerService.Log from being stripped, introduce PreserveAttribute to avoid
145 //requiring a unityengine using directive, il2cpp will see the attribute and know to not strip
146 //the Log method and its BurstCompilerService.Log dependency
147 internal class PreserveAttribute : System.Attribute {}
148
149 [Preserve]
150 internal static void PreventRequiredAttributeStrip()
151 {
152 new BurstDiscardAttribute();
153 // We also need to retain [Condition("UNITY_ASSERTION")] attributes in order to compile
154 // some assertion correctly (i.e. not compile them)
155 new ConditionalAttribute("HEJSA");
156 new JobProducerTypeAttribute(typeof(BurstRuntime));
157 }
158
159 [Preserve]
160 internal static unsafe void Log(byte* message, int logType, byte* fileName, int lineNumber)
161 {
162 Unity.Burst.LowLevel.BurstCompilerService.Log((byte*) 0, (Unity.Burst.LowLevel.BurstCompilerService.BurstLogType)logType, message, (byte*) 0, lineNumber);
163 }
164
165#endif // !BURST_COMPILER_SHARED
166
167
168 /// <summary>
169 /// Return a pointer to read-only memory consisting of the literal UTF-8 bytes of a string constant.
170 /// </summary>
171 /// <param name="str">A string which must a string literal</param>
172 /// <param name="byteCount">Receives the number of UTF-8 encoded bytes the constant contains (excluding null terminator)</param>
173 /// <returns>A pointer to constant data representing the UTF-8 encoded bytes of the string literal, terminated with a null terminator</returns>
174 public unsafe static byte* GetUTF8LiteralPointer(string str, out int byteCount)
175 {
176 throw new NotImplementedException("This function only works from Burst");
177 }
178
179 }
180}