ASoftFallback.cs 16 KB

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