ASoftFallback.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 float RoundF(float Value, int Fpcr)
  152. {
  153. switch ((ARoundMode)((Fpcr >> 22) & 3))
  154. {
  155. case ARoundMode.ToNearest: return MathF.Round (Value);
  156. case ARoundMode.TowardsPlusInfinity: return MathF.Ceiling (Value);
  157. case ARoundMode.TowardsMinusInfinity: return MathF.Floor (Value);
  158. case ARoundMode.TowardsZero: return MathF.Truncate(Value);
  159. }
  160. throw new InvalidOperationException();
  161. }
  162. public static double Round(double Value, int Fpcr)
  163. {
  164. switch ((ARoundMode)((Fpcr >> 22) & 3))
  165. {
  166. case ARoundMode.ToNearest: return Math.Round (Value);
  167. case ARoundMode.TowardsPlusInfinity: return Math.Ceiling (Value);
  168. case ARoundMode.TowardsMinusInfinity: return Math.Floor (Value);
  169. case ARoundMode.TowardsZero: return Math.Truncate(Value);
  170. }
  171. throw new InvalidOperationException();
  172. }
  173. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  174. {
  175. return Tbl(Vector, 8, Tb0);
  176. }
  177. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  178. {
  179. return Tbl(Vector, 16, Tb0);
  180. }
  181. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  182. {
  183. return Tbl(Vector, 8, Tb0, Tb1);
  184. }
  185. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  186. {
  187. return Tbl(Vector, 16, Tb0, Tb1);
  188. }
  189. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  190. {
  191. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  192. }
  193. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  194. {
  195. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  196. }
  197. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  198. {
  199. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  200. }
  201. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  202. {
  203. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  204. }
  205. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  206. {
  207. AVec Res = new AVec();
  208. byte[] Table = new byte[Tb.Length * 16];
  209. for (int Index = 0; Index < Tb.Length; Index++)
  210. for (int Index2 = 0; Index2 < 16; Index2++)
  211. {
  212. Table[Index * 16 + Index2] = (byte)VectorExtractIntZx(Tb[Index], Index2, 0);
  213. }
  214. for (int Index = 0; Index < Bytes; Index++)
  215. {
  216. byte TblIdx = (byte)VectorExtractIntZx(Vector, Index, 0);
  217. if (TblIdx < Table.Length)
  218. {
  219. Res = VectorInsertInt(Table[TblIdx], Res, Index, 0);
  220. }
  221. }
  222. return Res;
  223. }
  224. public static ulong VectorExtractIntZx(AVec Vector, int Index, int Size)
  225. {
  226. switch (Size)
  227. {
  228. case 0: return Vector.ExtractByte (Index);
  229. case 1: return Vector.ExtractUInt16(Index);
  230. case 2: return Vector.ExtractUInt32(Index);
  231. case 3: return Vector.ExtractUInt64(Index);
  232. }
  233. throw new ArgumentOutOfRangeException(nameof(Size));
  234. }
  235. public static long VectorExtractIntSx(AVec Vector, int Index, int Size)
  236. {
  237. switch (Size)
  238. {
  239. case 0: return (sbyte)Vector.ExtractByte (Index);
  240. case 1: return (short)Vector.ExtractUInt16(Index);
  241. case 2: return (int)Vector.ExtractUInt32(Index);
  242. case 3: return (long)Vector.ExtractUInt64(Index);
  243. }
  244. throw new ArgumentOutOfRangeException(nameof(Size));
  245. }
  246. public static float VectorExtractSingle(AVec Vector, int Index)
  247. {
  248. return Vector.ExtractSingle(Index);
  249. }
  250. public static double VectorExtractDouble(AVec Vector, int Index)
  251. {
  252. return Vector.ExtractDouble(Index);
  253. }
  254. public static AVec VectorInsertSingle(float Value, AVec Vector, int Index)
  255. {
  256. return AVec.InsertSingle(Vector, Index, Value);
  257. }
  258. public static AVec VectorInsertDouble(double Value, AVec Vector, int Index)
  259. {
  260. return AVec.InsertDouble(Vector, Index, Value);
  261. }
  262. public static AVec VectorInsertInt(ulong Value, AVec Vector, int Index, int Size)
  263. {
  264. switch (Size)
  265. {
  266. case 0: return AVec.InsertByte (Vector, Index, (byte)Value);
  267. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  268. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  269. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  270. }
  271. throw new ArgumentOutOfRangeException(nameof(Size));
  272. }
  273. }
  274. }