暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using Unity.Burst;
  3. #if !BURST_INTERNAL
  4. using AOT;
  5. using UnityEngine;
  6. #endif
  7. using System.Runtime.InteropServices;
  8. namespace Unity.Burst.Intrinsics
  9. {
  10. #if !BURST_INTERNAL
  11. [BurstCompile]
  12. #endif
  13. public unsafe static partial class X86
  14. {
  15. /// <summary>
  16. /// The 32-bit MXCSR register contains control and status information for SSE and AVX SIMD floating-point operations.
  17. /// </summary>
  18. [Flags]
  19. public enum MXCSRBits
  20. {
  21. /// <summary>
  22. /// Bit 15 (FTZ) of the MXCSR register enables the flush-to-zero mode, which controls the masked response to a SIMD floating-point underflow condition.
  23. /// </summary>
  24. /// <remarks>
  25. /// When the underflow exception is masked and the flush-to-zero mode is enabled, the processor performs the following operations when it detects a floating-point underflow condition.
  26. /// - Returns a zero result with the sign of the true result
  27. /// - Sets the precision and underflow exception flags.
  28. ///
  29. /// If the underflow exception is not masked, the flush-to-zero bit is ignored.
  30. ///
  31. /// The flush-to-zero mode is not compatible with IEEE Standard 754. The IEEE-mandated masked response to under-flow is to deliver the denormalized result.
  32. /// The flush-to-zero mode is provided primarily for performance reasons. At the cost of a slight precision loss, faster execution can be achieved for applications where underflows
  33. /// are common and rounding the underflow result to zero can be tolerated. The flush-to-zero bit is cleared upon a power-up or reset of the processor, disabling the flush-to-zero mode.
  34. /// </remarks>
  35. FlushToZero = 1 << 15,
  36. /// <summary>
  37. /// Mask for rounding control bits.
  38. /// </summary>
  39. ///
  40. /// The rounding modes have no effect on comparison operations, operations that produce exact results, or operations that produce NaN results.
  41. RoundingControlMask = (1 << 14) | (1 << 13),
  42. /// <summary>
  43. /// Rounded result is the closest to the infinitely precise result. If two values are equally close, the result is the even value (that is, the one with the least-significant bit of zero). Default.
  44. /// </summary>
  45. RoundToNearest = 0,
  46. /// <summary>
  47. /// Rounded result is closest to but no greater than the infinitely precise result.
  48. /// </summary>
  49. RoundDown = (1 << 13),
  50. /// <summary>
  51. /// Rounded result is closest to but no less than the infinitely precise result.
  52. /// </summary>
  53. RoundUp = (1 << 14),
  54. /// <summary>
  55. /// Rounded result is closest to but no greater in absolute value than the infinitely precise result.
  56. /// </summary>
  57. RoundTowardZero = (1 << 13) | (1 << 14),
  58. /// <summary>Bits 7 through 12 provide individual mask bits for the SIMD floating-point exceptions. An exception type is masked if the corresponding mask bit is set, and it is unmasked if the bit is clear. These mask bits are set upon a power-up or reset. This causes all SIMD floating-point exceptions to be initially masked.</summary>
  59. PrecisionMask = 1 << 12,
  60. /// <summary>Bits 7 through 12 provide individual mask bits for the SIMD floating-point exceptions. An exception type is masked if the corresponding mask bit is set, and it is unmasked if the bit is clear. These mask bits are set upon a power-up or reset. This causes all SIMD floating-point exceptions to be initially masked.</summary>
  61. UnderflowMask = 1 << 11,
  62. /// <summary>Bits 7 through 12 provide individual mask bits for the SIMD floating-point exceptions. An exception type is masked if the corresponding mask bit is set, and it is unmasked if the bit is clear. These mask bits are set upon a power-up or reset. This causes all SIMD floating-point exceptions to be initially masked.</summary>
  63. OverflowMask = 1 << 10,
  64. /// <summary>Bits 7 through 12 provide individual mask bits for the SIMD floating-point exceptions. An exception type is masked if the corresponding mask bit is set, and it is unmasked if the bit is clear. These mask bits are set upon a power-up or reset. This causes all SIMD floating-point exceptions to be initially masked.</summary>
  65. DivideByZeroMask = 1 << 9,
  66. /// <summary>Bits 7 through 12 provide individual mask bits for the SIMD floating-point exceptions. An exception type is masked if the corresponding mask bit is set, and it is unmasked if the bit is clear. These mask bits are set upon a power-up or reset. This causes all SIMD floating-point exceptions to be initially masked.</summary>
  67. DenormalOperationMask = 1 << 8,
  68. /// <summary>Bits 7 through 12 provide individual mask bits for the SIMD floating-point exceptions. An exception type is masked if the corresponding mask bit is set, and it is unmasked if the bit is clear. These mask bits are set upon a power-up or reset. This causes all SIMD floating-point exceptions to be initially masked.</summary>
  69. InvalidOperationMask = 1 << 7,
  70. /// <summary>
  71. /// Combine all bits for exception masking into one mask for convenience.
  72. /// </summary>
  73. ExceptionMask = PrecisionMask | UnderflowMask | OverflowMask | DivideByZeroMask | DenormalOperationMask | InvalidOperationMask,
  74. /// <summary>
  75. /// Bit 6 (DAZ) of the MXCSR register enables the denormals-are-zeros mode, which controls the processor’s response to a SIMD floating-point denormal operand condition.
  76. /// </summary>
  77. ///
  78. /// When the denormals-are-zeros flag is set, the processor converts all denormal source operands to a zero with the sign of the original operand before performing any computations on them.
  79. /// The processor does not set the denormal-operand exception flag (DE), regardless of the setting of the denormal-operand exception mask bit (DM); and it does not generate a denormal-operand
  80. /// exception if the exception is unmasked.The denormals-are-zeros mode is not compatible with IEEE Standard 754.
  81. ///
  82. /// The denormals-are-zeros mode is provided to improve processor performance for applications such as streaming media processing, where rounding a denormal operand to zero does not
  83. /// appreciably affect the quality of the processed data. The denormals-are-zeros flag is cleared upon a power-up or reset of the processor, disabling the denormals-are-zeros mode.
  84. ///
  85. /// The denormals-are-zeros mode was introduced in the Pentium 4 and Intel Xeon processor with the SSE2 extensions; however, it is fully compatible with the SSE SIMD floating-point instructions
  86. /// (that is, the denormals-are-zeros flag affects the operation of the SSE SIMD floating-point instructions). In earlier IA-32 processors and in some models of the Pentium 4 processor, this flag
  87. /// (bit 6) is reserved. Attempting to set bit 6 of the MXCSR register on processors that do not support the DAZ flag will cause a general-protection exception (#GP).
  88. DenormalsAreZeroes = 1 << 6,
  89. /// <summary>Bits 0 through 5 of the MXCSR register indicate whether a SIMD floating-point exception has been detected. They are "sticky" flags. That is, after a flag is set, it remains set until explicitly cleared. To clear these flags, use the LDMXCSR or the FXRSTOR instruction to write zeroes to them.</summary>
  90. PrecisionFlag = 1 << 5,
  91. /// <summary>Bits 0 through 5 of the MXCSR register indicate whether a SIMD floating-point exception has been detected. They are "sticky" flags. That is, after a flag is set, it remains set until explicitly cleared. To clear these flags, use the LDMXCSR or the FXRSTOR instruction to write zeroes to them.</summary>
  92. UnderflowFlag = 1 << 4,
  93. /// <summary>Bits 0 through 5 of the MXCSR register indicate whether a SIMD floating-point exception has been detected. They are "sticky" flags. That is, after a flag is set, it remains set until explicitly cleared. To clear these flags, use the LDMXCSR or the FXRSTOR instruction to write zeroes to them.</summary>
  94. OverflowFlag = 1 << 3,
  95. /// <summary>Bits 0 through 5 of the MXCSR register indicate whether a SIMD floating-point exception has been detected. They are "sticky" flags. That is, after a flag is set, it remains set until explicitly cleared. To clear these flags, use the LDMXCSR or the FXRSTOR instruction to write zeroes to them.</summary>
  96. DivideByZeroFlag = 1 << 2,
  97. /// <summary>Bits 0 through 5 of the MXCSR register indicate whether a SIMD floating-point exception has been detected. They are "sticky" flags. That is, after a flag is set, it remains set until explicitly cleared. To clear these flags, use the LDMXCSR or the FXRSTOR instruction to write zeroes to them.</summary>
  98. DenormalFlag = 1 << 1,
  99. /// <summary>Bits 0 through 5 of the MXCSR register indicate whether a SIMD floating-point exception has been detected. They are "sticky" flags. That is, after a flag is set, it remains set until explicitly cleared. To clear these flags, use the LDMXCSR or the FXRSTOR instruction to write zeroes to them.</summary>
  100. InvalidOperationFlag = 1 << 0,
  101. /// <summary>
  102. /// Combines all bits for flags into one mask for convenience.
  103. /// </summary>
  104. FlagMask = PrecisionFlag | UnderflowFlag | OverflowFlag | DivideByZeroFlag | DenormalFlag | InvalidOperationFlag,
  105. }
  106. /// <summary>
  107. /// Rounding mode flags
  108. /// </summary>
  109. [Flags]
  110. public enum RoundingMode
  111. {
  112. /// <summary>
  113. /// Round to the nearest integer
  114. /// </summary>
  115. FROUND_TO_NEAREST_INT = 0x00,
  116. /// <summary>
  117. /// Round to negative infinity
  118. /// </summary>
  119. FROUND_TO_NEG_INF = 0x01,
  120. /// <summary>
  121. /// Round to positive infinity
  122. /// </summary>
  123. FROUND_TO_POS_INF = 0x02,
  124. /// <summary>
  125. /// Round to zero
  126. /// </summary>
  127. FROUND_TO_ZERO = 0x03,
  128. /// <summary>
  129. /// Round to current direction
  130. /// </summary>
  131. FROUND_CUR_DIRECTION = 0x04,
  132. /// <summary>
  133. /// Do not suppress exceptions
  134. /// </summary>
  135. FROUND_RAISE_EXC = 0x00,
  136. /// <summary>
  137. /// Suppress exceptions
  138. /// </summary>
  139. FROUND_NO_EXC = 0x08,
  140. /// <summary>
  141. /// Round to the nearest integer without suppressing exceptions
  142. /// </summary>
  143. FROUND_NINT = FROUND_TO_NEAREST_INT | FROUND_RAISE_EXC,
  144. /// <summary>
  145. /// Round using Floor function without suppressing exceptions
  146. /// </summary>
  147. FROUND_FLOOR = FROUND_TO_NEG_INF | FROUND_RAISE_EXC,
  148. /// <summary>
  149. /// Round using Ceiling function without suppressing exceptions
  150. /// </summary>
  151. FROUND_CEIL = FROUND_TO_POS_INF | FROUND_RAISE_EXC,
  152. /// <summary>
  153. /// Round by truncating without suppressing exceptions
  154. /// </summary>
  155. FROUND_TRUNC = FROUND_TO_ZERO | FROUND_RAISE_EXC,
  156. /// <summary>
  157. /// Round using MXCSR.RC without suppressing exceptions
  158. /// </summary>
  159. FROUND_RINT = FROUND_CUR_DIRECTION | FROUND_RAISE_EXC,
  160. /// <summary>
  161. /// Round using MXCSR.RC and suppressing exceptions
  162. /// </summary>
  163. FROUND_NEARBYINT = FROUND_CUR_DIRECTION | FROUND_NO_EXC,
  164. /// <summary>
  165. /// Round to nearest integer and suppressing exceptions
  166. /// </summary>
  167. FROUND_NINT_NOEXC = FROUND_TO_NEAREST_INT | FROUND_NO_EXC,
  168. /// <summary>
  169. /// Round using Floor function and suppressing exceptions
  170. /// </summary>
  171. FROUND_FLOOR_NOEXC = FROUND_TO_NEG_INF | FROUND_NO_EXC,
  172. /// <summary>
  173. /// Round using Ceiling function and suppressing exceptions
  174. /// </summary>
  175. FROUND_CEIL_NOEXC = FROUND_TO_POS_INF | FROUND_NO_EXC,
  176. /// <summary>
  177. /// Round by truncating and suppressing exceptions
  178. /// </summary>
  179. FROUND_TRUNC_NOEXC = FROUND_TO_ZERO | FROUND_NO_EXC,
  180. /// <summary>
  181. /// Round using MXCSR.RC and suppressing exceptions
  182. /// </summary>
  183. FROUND_RINT_NOEXC = FROUND_CUR_DIRECTION | FROUND_NO_EXC,
  184. }
  185. internal struct RoundingScope : IDisposable
  186. {
  187. private MXCSRBits OldBits;
  188. public RoundingScope(MXCSRBits roundingMode)
  189. {
  190. OldBits = MXCSR;
  191. MXCSR = (OldBits & ~MXCSRBits.RoundingControlMask) | roundingMode;
  192. }
  193. public void Dispose()
  194. {
  195. MXCSR = OldBits;
  196. }
  197. }
  198. #if !BURST_INTERNAL
  199. private static void BurstIntrinsicSetCSRFromManaged(int _) { }
  200. private static int BurstIntrinsicGetCSRFromManaged() { return 0; }
  201. internal static int getcsr_raw() => DoGetCSRTrampoline();
  202. internal static void setcsr_raw(int bits) => DoSetCSRTrampoline(bits);
  203. [BurstCompile(CompileSynchronously = true)]
  204. private static void DoSetCSRTrampoline(int bits)
  205. {
  206. if (Sse.IsSseSupported)
  207. BurstIntrinsicSetCSRFromManaged(bits);
  208. }
  209. [BurstCompile(CompileSynchronously = true)]
  210. private static int DoGetCSRTrampoline()
  211. {
  212. if (Sse.IsSseSupported)
  213. return BurstIntrinsicGetCSRFromManaged();
  214. return 0;
  215. }
  216. #elif BURST_INTERNAL
  217. // Internally inside burst for unit tests we can't recurse from tests into burst again,
  218. // so we pinvoke to a dummy wrapper DLL that exposes CSR manipulation
  219. [DllImport("burst-dllimport-native", EntryPoint = "x86_getcsr")]
  220. internal static extern int getcsr_raw();
  221. [DllImport("burst-dllimport-native", EntryPoint = "x86_setcsr")]
  222. internal static extern void setcsr_raw(int bits);
  223. #endif
  224. /// <summary>
  225. /// Allows access to the CSR register
  226. /// </summary>
  227. public static MXCSRBits MXCSR
  228. {
  229. [BurstTargetCpu(BurstTargetCpu.X64_SSE2)]
  230. get
  231. {
  232. return (MXCSRBits)getcsr_raw();
  233. }
  234. [BurstTargetCpu(BurstTargetCpu.X64_SSE2)]
  235. set
  236. {
  237. setcsr_raw((int)value);
  238. }
  239. }
  240. }
  241. }