ASoftFallback.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace ChocolArm64.Instruction
  6. {
  7. static class ASoftFallback
  8. {
  9. public static void EmitCall(AILEmitterCtx Context, string Name64, string Name128)
  10. {
  11. bool IsSimd64 = Context.CurrOp.RegisterSize == ARegisterSize.SIMD64;
  12. Context.EmitCall(typeof(ASoftFallback), IsSimd64 ? Name64 : Name128);
  13. }
  14. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  15. {
  16. Context.EmitCall(typeof(ASoftFallback), MthdName);
  17. }
  18. public static uint CountLeadingZeros32(uint Value) => (uint)CountLeadingZeros(Value, 32);
  19. public static ulong CountLeadingZeros64(ulong Value) => (ulong)CountLeadingZeros(Value, 64);
  20. private static ulong CountLeadingZeros(ulong Value, int Size)
  21. {
  22. int HighBit = Size - 1;
  23. for (int Bit = HighBit; Bit >= 0; Bit--)
  24. {
  25. if (((Value >> Bit) & 1) != 0)
  26. {
  27. return (ulong)(HighBit - Bit);
  28. }
  29. }
  30. return (ulong)Size;
  31. }
  32. public static uint ReverseBits32(uint Value)
  33. {
  34. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  35. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  36. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  37. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  38. return (Value >> 16) | (Value << 16);
  39. }
  40. public static ulong ReverseBits64(ulong Value)
  41. {
  42. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  43. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  44. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  45. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  46. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  47. return (Value >> 32) | (Value << 32);
  48. }
  49. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  50. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  51. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  52. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  53. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  54. private enum RevSize
  55. {
  56. Rev16,
  57. Rev32,
  58. Rev64
  59. }
  60. private static ulong ReverseBytes(ulong Value, RevSize Size)
  61. {
  62. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  63. if (Size == RevSize.Rev16)
  64. {
  65. return Value;
  66. }
  67. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  68. if (Size == RevSize.Rev32)
  69. {
  70. return Value;
  71. }
  72. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  73. if (Size == RevSize.Rev64)
  74. {
  75. return Value;
  76. }
  77. throw new ArgumentException(nameof(Size));
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. public static int SatF32ToS32(float Value)
  81. {
  82. return Value > int.MaxValue ? int.MaxValue :
  83. Value < int.MinValue ? int.MinValue : (int)Value;
  84. }
  85. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  86. public static long SatF32ToS64(float Value)
  87. {
  88. return Value > long.MaxValue ? long.MaxValue :
  89. Value < long.MinValue ? long.MinValue : (long)Value;
  90. }
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public static uint SatF32ToU32(float Value)
  93. {
  94. return Value > uint.MaxValue ? uint.MaxValue :
  95. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  96. }
  97. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  98. public static ulong SatF32ToU64(float Value)
  99. {
  100. return Value > ulong.MaxValue ? ulong.MaxValue :
  101. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  102. }
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. public static int SatF64ToS32(double Value)
  105. {
  106. return Value > int.MaxValue ? int.MaxValue :
  107. Value < int.MinValue ? int.MinValue : (int)Value;
  108. }
  109. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  110. public static long SatF64ToS64(double Value)
  111. {
  112. return Value > long.MaxValue ? long.MaxValue :
  113. Value < long.MinValue ? long.MinValue : (long)Value;
  114. }
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. public static uint SatF64ToU32(double Value)
  117. {
  118. return Value > uint.MaxValue ? uint.MaxValue :
  119. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  120. }
  121. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  122. public static ulong SatF64ToU64(double Value)
  123. {
  124. return Value > ulong.MaxValue ? ulong.MaxValue :
  125. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  126. }
  127. public static ulong SMulHi128(ulong LHS, ulong RHS)
  128. {
  129. long LLo = (uint)(LHS >> 0);
  130. long LHi = (int)(LHS >> 32);
  131. long RLo = (uint)(RHS >> 0);
  132. long RHi = (int)(RHS >> 32);
  133. long LHiRHi = LHi * RHi;
  134. long LHiRLo = LHi * RLo;
  135. long LLoRHi = LLo * RHi;
  136. long LLoRLo = LLo * RLo;
  137. long Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  138. long ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  139. return (ulong)ResHi;
  140. }
  141. public static ulong UMulHi128(ulong LHS, ulong RHS)
  142. {
  143. ulong LLo = (uint)(LHS >> 0);
  144. ulong LHi = (uint)(LHS >> 32);
  145. ulong RLo = (uint)(RHS >> 0);
  146. ulong RHi = (uint)(RHS >> 32);
  147. ulong LHiRHi = LHi * RHi;
  148. ulong LHiRLo = LHi * RLo;
  149. ulong LLoRHi = LLo * RHi;
  150. ulong LLoRLo = LLo * RLo;
  151. ulong Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  152. ulong ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  153. return ResHi;
  154. }
  155. public static int CountSetBits8(byte Value)
  156. {
  157. return (Value >> 0) & 1 + (Value >> 1) & 1 +
  158. (Value >> 2) & 1 + (Value >> 3) & 1 +
  159. (Value >> 4) & 1 + (Value >> 5) & 1 +
  160. (Value >> 6) & 1 + (Value >> 7);
  161. }
  162. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  163. {
  164. return Tbl(Vector, 8, Tb0);
  165. }
  166. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  167. {
  168. return Tbl(Vector, 16, Tb0);
  169. }
  170. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  171. {
  172. return Tbl(Vector, 8, Tb0, Tb1);
  173. }
  174. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  175. {
  176. return Tbl(Vector, 16, Tb0, Tb1);
  177. }
  178. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  179. {
  180. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  181. }
  182. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  183. {
  184. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  185. }
  186. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  187. {
  188. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  189. }
  190. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  191. {
  192. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  193. }
  194. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  195. {
  196. AVec Res = new AVec();
  197. byte[] Table = new byte[Tb.Length * 16];
  198. for (int Index = 0; Index < Tb.Length; Index++)
  199. for (int Index2 = 0; Index2 < 16; Index2++)
  200. {
  201. Table[Index * 16 + Index2] = (byte)VectorExtractIntZx(Tb[Index], Index2, 0);
  202. }
  203. for (int Index = 0; Index < Bytes; Index++)
  204. {
  205. byte TblIdx = (byte)VectorExtractIntZx(Vector, Index, 0);
  206. if (TblIdx < Table.Length)
  207. {
  208. Res = VectorInsertInt(Table[TblIdx], Res, Index, 0);
  209. }
  210. }
  211. return Res;
  212. }
  213. public static ulong VectorExtractIntZx(AVec Vector, int Index, int Size)
  214. {
  215. switch (Size)
  216. {
  217. case 0: return Vector.ExtractByte (Index);
  218. case 1: return Vector.ExtractUInt16(Index);
  219. case 2: return Vector.ExtractUInt32(Index);
  220. case 3: return Vector.ExtractUInt64(Index);
  221. }
  222. throw new ArgumentOutOfRangeException(nameof(Size));
  223. }
  224. public static long VectorExtractIntSx(AVec Vector, int Index, int Size)
  225. {
  226. switch (Size)
  227. {
  228. case 0: return (sbyte)Vector.ExtractByte (Index);
  229. case 1: return (short)Vector.ExtractUInt16(Index);
  230. case 2: return (int)Vector.ExtractUInt32(Index);
  231. case 3: return (long)Vector.ExtractUInt64(Index);
  232. }
  233. throw new ArgumentOutOfRangeException(nameof(Size));
  234. }
  235. public static float VectorExtractSingle(AVec Vector, int Index)
  236. {
  237. return Vector.ExtractSingle(Index);
  238. }
  239. public static double VectorExtractDouble(AVec Vector, int Index)
  240. {
  241. return Vector.ExtractDouble(Index);
  242. }
  243. public static AVec VectorInsertSingle(float Value, AVec Vector, int Index)
  244. {
  245. return AVec.InsertSingle(Vector, Index, Value);
  246. }
  247. public static AVec VectorInsertDouble(double Value, AVec Vector, int Index)
  248. {
  249. return AVec.InsertDouble(Vector, Index, Value);
  250. }
  251. public static AVec VectorInsertInt(ulong Value, AVec Vector, int Index, int Size)
  252. {
  253. switch (Size)
  254. {
  255. case 0: return AVec.InsertByte (Vector, Index, (byte)Value);
  256. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  257. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  258. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  259. }
  260. throw new ArgumentOutOfRangeException(nameof(Size));
  261. }
  262. }
  263. }