ASoftFallback.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. private const uint Crc32RevPoly = 0xedb88320;
  34. private const uint Crc32cRevPoly = 0x82f63b78;
  35. public static uint Crc32b(uint Crc, byte Val) => Crc32 (Crc, Crc32RevPoly, Val);
  36. public static uint Crc32h(uint Crc, byte Val) => Crc32h(Crc, Crc32RevPoly, Val);
  37. public static uint Crc32w(uint Crc, byte Val) => Crc32w(Crc, Crc32RevPoly, Val);
  38. public static uint Crc32x(uint Crc, byte Val) => Crc32x(Crc, Crc32RevPoly, Val);
  39. public static uint Crc32cb(uint Crc, byte Val) => Crc32 (Crc, Crc32cRevPoly, Val);
  40. public static uint Crc32ch(uint Crc, byte Val) => Crc32h(Crc, Crc32cRevPoly, Val);
  41. public static uint Crc32cw(uint Crc, byte Val) => Crc32w(Crc, Crc32cRevPoly, Val);
  42. public static uint Crc32cx(uint Crc, byte Val) => Crc32x(Crc, Crc32cRevPoly, Val);
  43. private static uint Crc32h(uint Crc, uint Poly, ushort Val)
  44. {
  45. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  46. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  47. return Crc;
  48. }
  49. private static uint Crc32w(uint Crc, uint Poly, uint Val)
  50. {
  51. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  52. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  53. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  54. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  55. return Crc;
  56. }
  57. private static uint Crc32x(uint Crc, uint Poly, ulong Val)
  58. {
  59. Crc = Crc32(Crc, Poly, (byte)(Val >> 0));
  60. Crc = Crc32(Crc, Poly, (byte)(Val >> 8));
  61. Crc = Crc32(Crc, Poly, (byte)(Val >> 16));
  62. Crc = Crc32(Crc, Poly, (byte)(Val >> 24));
  63. Crc = Crc32(Crc, Poly, (byte)(Val >> 32));
  64. Crc = Crc32(Crc, Poly, (byte)(Val >> 40));
  65. Crc = Crc32(Crc, Poly, (byte)(Val >> 48));
  66. Crc = Crc32(Crc, Poly, (byte)(Val >> 56));
  67. return Crc;
  68. }
  69. private static uint Crc32(uint Crc, uint Poly, byte Val)
  70. {
  71. Crc ^= Val;
  72. for (int Bit = 7; Bit >= 0; Bit--)
  73. {
  74. uint Mask = (uint)(-(int)(Crc & 1));
  75. Crc = (Crc >> 1) ^ (Poly & Mask);
  76. }
  77. return Crc;
  78. }
  79. public static uint ReverseBits8(uint Value)
  80. {
  81. Value = ((Value & 0xaa) >> 1) | ((Value & 0x55) << 1);
  82. Value = ((Value & 0xcc) >> 2) | ((Value & 0x33) << 2);
  83. Value = ((Value & 0xf0) >> 4) | ((Value & 0x0f) << 4);
  84. return Value;
  85. }
  86. public static uint ReverseBits16(uint Value)
  87. {
  88. Value = ((Value & 0xaaaa) >> 1) | ((Value & 0x5555) << 1);
  89. Value = ((Value & 0xcccc) >> 2) | ((Value & 0x3333) << 2);
  90. Value = ((Value & 0xf0f0) >> 4) | ((Value & 0x0f0f) << 4);
  91. Value = ((Value & 0xff00) >> 8) | ((Value & 0x00ff) << 8);
  92. return Value;
  93. }
  94. public static uint ReverseBits32(uint Value)
  95. {
  96. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  97. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  98. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  99. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  100. return (Value >> 16) | (Value << 16);
  101. }
  102. public static ulong ReverseBits64(ulong Value)
  103. {
  104. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  105. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  106. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  107. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  108. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  109. return (Value >> 32) | (Value << 32);
  110. }
  111. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  112. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  113. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  114. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  115. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  116. private enum RevSize
  117. {
  118. Rev16,
  119. Rev32,
  120. Rev64
  121. }
  122. private static ulong ReverseBytes(ulong Value, RevSize Size)
  123. {
  124. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  125. if (Size == RevSize.Rev16)
  126. {
  127. return Value;
  128. }
  129. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  130. if (Size == RevSize.Rev32)
  131. {
  132. return Value;
  133. }
  134. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  135. if (Size == RevSize.Rev64)
  136. {
  137. return Value;
  138. }
  139. throw new ArgumentException(nameof(Size));
  140. }
  141. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  142. public static int SatF32ToS32(float Value)
  143. {
  144. if (float.IsNaN(Value)) return 0;
  145. return Value > int.MaxValue ? int.MaxValue :
  146. Value < int.MinValue ? int.MinValue : (int)Value;
  147. }
  148. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  149. public static long SatF32ToS64(float Value)
  150. {
  151. if (float.IsNaN(Value)) return 0;
  152. return Value > long.MaxValue ? long.MaxValue :
  153. Value < long.MinValue ? long.MinValue : (long)Value;
  154. }
  155. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  156. public static uint SatF32ToU32(float Value)
  157. {
  158. if (float.IsNaN(Value)) return 0;
  159. return Value > uint.MaxValue ? uint.MaxValue :
  160. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  161. }
  162. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  163. public static ulong SatF32ToU64(float Value)
  164. {
  165. if (float.IsNaN(Value)) return 0;
  166. return Value > ulong.MaxValue ? ulong.MaxValue :
  167. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  168. }
  169. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170. public static int SatF64ToS32(double Value)
  171. {
  172. if (double.IsNaN(Value)) return 0;
  173. return Value > int.MaxValue ? int.MaxValue :
  174. Value < int.MinValue ? int.MinValue : (int)Value;
  175. }
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. public static long SatF64ToS64(double Value)
  178. {
  179. if (double.IsNaN(Value)) return 0;
  180. return Value > long.MaxValue ? long.MaxValue :
  181. Value < long.MinValue ? long.MinValue : (long)Value;
  182. }
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. public static uint SatF64ToU32(double Value)
  185. {
  186. if (double.IsNaN(Value)) return 0;
  187. return Value > uint.MaxValue ? uint.MaxValue :
  188. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  189. }
  190. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  191. public static ulong SatF64ToU64(double Value)
  192. {
  193. if (double.IsNaN(Value)) return 0;
  194. return Value > ulong.MaxValue ? ulong.MaxValue :
  195. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  196. }
  197. public static long SMulHi128(long LHS, long RHS)
  198. {
  199. return (long)(BigInteger.Multiply(LHS, RHS) >> 64);
  200. }
  201. public static ulong UMulHi128(ulong LHS, ulong RHS)
  202. {
  203. return (ulong)(BigInteger.Multiply(LHS, RHS) >> 64);
  204. }
  205. public static int CountSetBits8(byte Value)
  206. {
  207. return (Value >> 0) & 1 + (Value >> 1) & 1 +
  208. (Value >> 2) & 1 + (Value >> 3) & 1 +
  209. (Value >> 4) & 1 + (Value >> 5) & 1 +
  210. (Value >> 6) & 1 + (Value >> 7);
  211. }
  212. public static float RoundF(float Value, int Fpcr)
  213. {
  214. switch ((ARoundMode)((Fpcr >> 22) & 3))
  215. {
  216. case ARoundMode.ToNearest: return MathF.Round (Value);
  217. case ARoundMode.TowardsPlusInfinity: return MathF.Ceiling (Value);
  218. case ARoundMode.TowardsMinusInfinity: return MathF.Floor (Value);
  219. case ARoundMode.TowardsZero: return MathF.Truncate(Value);
  220. }
  221. throw new InvalidOperationException();
  222. }
  223. public static double Round(double Value, int Fpcr)
  224. {
  225. switch ((ARoundMode)((Fpcr >> 22) & 3))
  226. {
  227. case ARoundMode.ToNearest: return Math.Round (Value);
  228. case ARoundMode.TowardsPlusInfinity: return Math.Ceiling (Value);
  229. case ARoundMode.TowardsMinusInfinity: return Math.Floor (Value);
  230. case ARoundMode.TowardsZero: return Math.Truncate(Value);
  231. }
  232. throw new InvalidOperationException();
  233. }
  234. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  235. {
  236. return Tbl(Vector, 8, Tb0);
  237. }
  238. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  239. {
  240. return Tbl(Vector, 16, Tb0);
  241. }
  242. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  243. {
  244. return Tbl(Vector, 8, Tb0, Tb1);
  245. }
  246. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  247. {
  248. return Tbl(Vector, 16, Tb0, Tb1);
  249. }
  250. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  251. {
  252. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  253. }
  254. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  255. {
  256. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  257. }
  258. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  259. {
  260. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  261. }
  262. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  263. {
  264. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  265. }
  266. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  267. {
  268. AVec Res = new AVec();
  269. byte[] Table = new byte[Tb.Length * 16];
  270. for (int Index = 0; Index < Tb.Length; Index++)
  271. for (int Index2 = 0; Index2 < 16; Index2++)
  272. {
  273. Table[Index * 16 + Index2] = (byte)VectorExtractIntZx(Tb[Index], Index2, 0);
  274. }
  275. for (int Index = 0; Index < Bytes; Index++)
  276. {
  277. byte TblIdx = (byte)VectorExtractIntZx(Vector, Index, 0);
  278. if (TblIdx < Table.Length)
  279. {
  280. Res = VectorInsertInt(Table[TblIdx], Res, Index, 0);
  281. }
  282. }
  283. return Res;
  284. }
  285. public static ulong VectorExtractIntZx(AVec Vector, int Index, int Size)
  286. {
  287. switch (Size)
  288. {
  289. case 0: return Vector.ExtractByte (Index);
  290. case 1: return Vector.ExtractUInt16(Index);
  291. case 2: return Vector.ExtractUInt32(Index);
  292. case 3: return Vector.ExtractUInt64(Index);
  293. }
  294. throw new ArgumentOutOfRangeException(nameof(Size));
  295. }
  296. public static long VectorExtractIntSx(AVec Vector, int Index, int Size)
  297. {
  298. switch (Size)
  299. {
  300. case 0: return (sbyte)Vector.ExtractByte (Index);
  301. case 1: return (short)Vector.ExtractUInt16(Index);
  302. case 2: return (int)Vector.ExtractUInt32(Index);
  303. case 3: return (long)Vector.ExtractUInt64(Index);
  304. }
  305. throw new ArgumentOutOfRangeException(nameof(Size));
  306. }
  307. public static float VectorExtractSingle(AVec Vector, int Index)
  308. {
  309. return Vector.ExtractSingle(Index);
  310. }
  311. public static double VectorExtractDouble(AVec Vector, int Index)
  312. {
  313. return Vector.ExtractDouble(Index);
  314. }
  315. public static AVec VectorInsertSingle(float Value, AVec Vector, int Index)
  316. {
  317. return AVec.InsertSingle(Vector, Index, Value);
  318. }
  319. public static AVec VectorInsertDouble(double Value, AVec Vector, int Index)
  320. {
  321. return AVec.InsertDouble(Vector, Index, Value);
  322. }
  323. public static AVec VectorInsertInt(ulong Value, AVec Vector, int Index, int Size)
  324. {
  325. switch (Size)
  326. {
  327. case 0: return AVec.InsertByte (Vector, Index, (byte)Value);
  328. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  329. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  330. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  331. }
  332. throw new ArgumentOutOfRangeException(nameof(Size));
  333. }
  334. }
  335. }