ASoftFallback.cs 14 KB

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