ASoftFallback.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. using System.Numerics;
  5. using System.Runtime.CompilerServices;
  6. namespace ChocolArm64.Instruction
  7. {
  8. static class ASoftFallback
  9. {
  10. public static void EmitCall(AILEmitterCtx Context, string Name64, string Name128)
  11. {
  12. bool IsSimd64 = Context.CurrOp.RegisterSize == ARegisterSize.SIMD64;
  13. Context.EmitCall(typeof(ASoftFallback), IsSimd64 ? Name64 : Name128);
  14. }
  15. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  16. {
  17. Context.EmitCall(typeof(ASoftFallback), MthdName);
  18. }
  19. public static uint CountLeadingZeros32(uint Value) => (uint)CountLeadingZeros(Value, 32);
  20. public static ulong CountLeadingZeros64(ulong Value) => (ulong)CountLeadingZeros(Value, 64);
  21. private static ulong CountLeadingZeros(ulong Value, int Size)
  22. {
  23. int HighBit = Size - 1;
  24. for (int Bit = HighBit; Bit >= 0; Bit--)
  25. {
  26. if (((Value >> Bit) & 1) != 0)
  27. {
  28. return (ulong)(HighBit - Bit);
  29. }
  30. }
  31. return (ulong)Size;
  32. }
  33. public static uint ReverseBits32(uint Value)
  34. {
  35. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  36. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  37. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  38. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  39. return (Value >> 16) | (Value << 16);
  40. }
  41. public static ulong ReverseBits64(ulong Value)
  42. {
  43. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  44. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  45. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  46. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  47. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  48. return (Value >> 32) | (Value << 32);
  49. }
  50. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  51. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  52. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  53. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  54. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  55. private enum RevSize
  56. {
  57. Rev16,
  58. Rev32,
  59. Rev64
  60. }
  61. private static ulong ReverseBytes(ulong Value, RevSize Size)
  62. {
  63. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  64. if (Size == RevSize.Rev16)
  65. {
  66. return Value;
  67. }
  68. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  69. if (Size == RevSize.Rev32)
  70. {
  71. return Value;
  72. }
  73. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  74. if (Size == RevSize.Rev64)
  75. {
  76. return Value;
  77. }
  78. throw new ArgumentException(nameof(Size));
  79. }
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static int SatF32ToS32(float Value)
  82. {
  83. if (float.IsNaN(Value)) return 0;
  84. return Value > int.MaxValue ? int.MaxValue :
  85. Value < int.MinValue ? int.MinValue : (int)Value;
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static long SatF32ToS64(float Value)
  89. {
  90. if (float.IsNaN(Value)) return 0;
  91. return Value > long.MaxValue ? long.MaxValue :
  92. Value < long.MinValue ? long.MinValue : (long)Value;
  93. }
  94. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  95. public static uint SatF32ToU32(float Value)
  96. {
  97. if (float.IsNaN(Value)) return 0;
  98. return Value > uint.MaxValue ? uint.MaxValue :
  99. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  100. }
  101. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  102. public static ulong SatF32ToU64(float Value)
  103. {
  104. if (float.IsNaN(Value)) return 0;
  105. return Value > ulong.MaxValue ? ulong.MaxValue :
  106. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  107. }
  108. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  109. public static int SatF64ToS32(double Value)
  110. {
  111. if (double.IsNaN(Value)) return 0;
  112. return Value > int.MaxValue ? int.MaxValue :
  113. Value < int.MinValue ? int.MinValue : (int)Value;
  114. }
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. public static long SatF64ToS64(double Value)
  117. {
  118. if (double.IsNaN(Value)) return 0;
  119. return Value > long.MaxValue ? long.MaxValue :
  120. Value < long.MinValue ? long.MinValue : (long)Value;
  121. }
  122. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  123. public static uint SatF64ToU32(double Value)
  124. {
  125. if (double.IsNaN(Value)) return 0;
  126. return Value > uint.MaxValue ? uint.MaxValue :
  127. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  128. }
  129. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  130. public static ulong SatF64ToU64(double Value)
  131. {
  132. if (double.IsNaN(Value)) return 0;
  133. return Value > ulong.MaxValue ? ulong.MaxValue :
  134. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  135. }
  136. public static long SMulHi128(long LHS, long RHS)
  137. {
  138. return (long)(BigInteger.Multiply(LHS, RHS) >> 64);
  139. }
  140. public static ulong UMulHi128(ulong LHS, ulong RHS)
  141. {
  142. return (ulong)(BigInteger.Multiply(LHS, RHS) >> 64);
  143. }
  144. public static int CountSetBits8(byte Value)
  145. {
  146. return (Value >> 0) & 1 + (Value >> 1) & 1 +
  147. (Value >> 2) & 1 + (Value >> 3) & 1 +
  148. (Value >> 4) & 1 + (Value >> 5) & 1 +
  149. (Value >> 6) & 1 + (Value >> 7);
  150. }
  151. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  152. {
  153. return Tbl(Vector, 8, Tb0);
  154. }
  155. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  156. {
  157. return Tbl(Vector, 16, Tb0);
  158. }
  159. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  160. {
  161. return Tbl(Vector, 8, Tb0, Tb1);
  162. }
  163. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  164. {
  165. return Tbl(Vector, 16, Tb0, Tb1);
  166. }
  167. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  168. {
  169. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  170. }
  171. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  172. {
  173. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  174. }
  175. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  176. {
  177. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  178. }
  179. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  180. {
  181. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  182. }
  183. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  184. {
  185. AVec Res = new AVec();
  186. byte[] Table = new byte[Tb.Length * 16];
  187. for (int Index = 0; Index < Tb.Length; Index++)
  188. for (int Index2 = 0; Index2 < 16; Index2++)
  189. {
  190. Table[Index * 16 + Index2] = (byte)VectorExtractIntZx(Tb[Index], Index2, 0);
  191. }
  192. for (int Index = 0; Index < Bytes; Index++)
  193. {
  194. byte TblIdx = (byte)VectorExtractIntZx(Vector, Index, 0);
  195. if (TblIdx < Table.Length)
  196. {
  197. Res = VectorInsertInt(Table[TblIdx], Res, Index, 0);
  198. }
  199. }
  200. return Res;
  201. }
  202. public static ulong VectorExtractIntZx(AVec Vector, int Index, int Size)
  203. {
  204. switch (Size)
  205. {
  206. case 0: return Vector.ExtractByte (Index);
  207. case 1: return Vector.ExtractUInt16(Index);
  208. case 2: return Vector.ExtractUInt32(Index);
  209. case 3: return Vector.ExtractUInt64(Index);
  210. }
  211. throw new ArgumentOutOfRangeException(nameof(Size));
  212. }
  213. public static long VectorExtractIntSx(AVec Vector, int Index, int Size)
  214. {
  215. switch (Size)
  216. {
  217. case 0: return (sbyte)Vector.ExtractByte (Index);
  218. case 1: return (short)Vector.ExtractUInt16(Index);
  219. case 2: return (int)Vector.ExtractUInt32(Index);
  220. case 3: return (long)Vector.ExtractUInt64(Index);
  221. }
  222. throw new ArgumentOutOfRangeException(nameof(Size));
  223. }
  224. public static float VectorExtractSingle(AVec Vector, int Index)
  225. {
  226. return Vector.ExtractSingle(Index);
  227. }
  228. public static double VectorExtractDouble(AVec Vector, int Index)
  229. {
  230. return Vector.ExtractDouble(Index);
  231. }
  232. public static AVec VectorInsertSingle(float Value, AVec Vector, int Index)
  233. {
  234. return AVec.InsertSingle(Vector, Index, Value);
  235. }
  236. public static AVec VectorInsertDouble(double Value, AVec Vector, int Index)
  237. {
  238. return AVec.InsertDouble(Vector, Index, Value);
  239. }
  240. public static AVec VectorInsertInt(ulong Value, AVec Vector, int Index, int Size)
  241. {
  242. switch (Size)
  243. {
  244. case 0: return AVec.InsertByte (Vector, Index, (byte)Value);
  245. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  246. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  247. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  248. }
  249. throw new ArgumentOutOfRangeException(nameof(Size));
  250. }
  251. }
  252. }