ASoftFallback.cs 16 KB

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