AVectorHelper.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.Intrinsics;
  6. using System.Runtime.Intrinsics.X86;
  7. namespace ChocolArm64.Instruction
  8. {
  9. static class AVectorHelper
  10. {
  11. private static readonly Vector128<float> Zero32_128Mask;
  12. static AVectorHelper()
  13. {
  14. if (!Sse2.IsSupported)
  15. {
  16. throw new PlatformNotSupportedException();
  17. }
  18. Zero32_128Mask = Sse.StaticCast<uint, float>(Sse2.SetVector128(0, 0, 0, 0xffffffff));
  19. }
  20. public static void EmitCall(AILEmitterCtx Context, string Name64, string Name128)
  21. {
  22. bool IsSimd64 = Context.CurrOp.RegisterSize == ARegisterSize.SIMD64;
  23. Context.EmitCall(typeof(AVectorHelper), IsSimd64 ? Name64 : Name128);
  24. }
  25. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  26. {
  27. Context.EmitCall(typeof(AVectorHelper), MthdName);
  28. }
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public static int SatF32ToS32(float Value)
  31. {
  32. if (float.IsNaN(Value)) return 0;
  33. return Value > int.MaxValue ? int.MaxValue :
  34. Value < int.MinValue ? int.MinValue : (int)Value;
  35. }
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static long SatF32ToS64(float Value)
  38. {
  39. if (float.IsNaN(Value)) return 0;
  40. return Value > long.MaxValue ? long.MaxValue :
  41. Value < long.MinValue ? long.MinValue : (long)Value;
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public static uint SatF32ToU32(float Value)
  45. {
  46. if (float.IsNaN(Value)) return 0;
  47. return Value > uint.MaxValue ? uint.MaxValue :
  48. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static ulong SatF32ToU64(float Value)
  52. {
  53. if (float.IsNaN(Value)) return 0;
  54. return Value > ulong.MaxValue ? ulong.MaxValue :
  55. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static int SatF64ToS32(double Value)
  59. {
  60. if (double.IsNaN(Value)) return 0;
  61. return Value > int.MaxValue ? int.MaxValue :
  62. Value < int.MinValue ? int.MinValue : (int)Value;
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public static long SatF64ToS64(double Value)
  66. {
  67. if (double.IsNaN(Value)) return 0;
  68. return Value > long.MaxValue ? long.MaxValue :
  69. Value < long.MinValue ? long.MinValue : (long)Value;
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public static uint SatF64ToU32(double Value)
  73. {
  74. if (double.IsNaN(Value)) return 0;
  75. return Value > uint.MaxValue ? uint.MaxValue :
  76. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  77. }
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public static ulong SatF64ToU64(double Value)
  80. {
  81. if (double.IsNaN(Value)) return 0;
  82. return Value > ulong.MaxValue ? ulong.MaxValue :
  83. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  84. }
  85. public static double Round(double Value, int Fpcr)
  86. {
  87. switch ((ARoundMode)((Fpcr >> 22) & 3))
  88. {
  89. case ARoundMode.ToNearest: return Math.Round (Value);
  90. case ARoundMode.TowardsPlusInfinity: return Math.Ceiling (Value);
  91. case ARoundMode.TowardsMinusInfinity: return Math.Floor (Value);
  92. case ARoundMode.TowardsZero: return Math.Truncate(Value);
  93. }
  94. throw new InvalidOperationException();
  95. }
  96. public static float RoundF(float Value, int Fpcr)
  97. {
  98. switch ((ARoundMode)((Fpcr >> 22) & 3))
  99. {
  100. case ARoundMode.ToNearest: return MathF.Round (Value);
  101. case ARoundMode.TowardsPlusInfinity: return MathF.Ceiling (Value);
  102. case ARoundMode.TowardsMinusInfinity: return MathF.Floor (Value);
  103. case ARoundMode.TowardsZero: return MathF.Truncate(Value);
  104. }
  105. throw new InvalidOperationException();
  106. }
  107. public static Vector128<float> Tbl1_V64(
  108. Vector128<float> Vector,
  109. Vector128<float> Tb0)
  110. {
  111. return Tbl(Vector, 8, Tb0);
  112. }
  113. public static Vector128<float> Tbl1_V128(
  114. Vector128<float> Vector,
  115. Vector128<float> Tb0)
  116. {
  117. return Tbl(Vector, 16, Tb0);
  118. }
  119. public static Vector128<float> Tbl2_V64(
  120. Vector128<float> Vector,
  121. Vector128<float> Tb0,
  122. Vector128<float> Tb1)
  123. {
  124. return Tbl(Vector, 8, Tb0, Tb1);
  125. }
  126. public static Vector128<float> Tbl2_V128(
  127. Vector128<float> Vector,
  128. Vector128<float> Tb0,
  129. Vector128<float> Tb1)
  130. {
  131. return Tbl(Vector, 16, Tb0, Tb1);
  132. }
  133. public static Vector128<float> Tbl3_V64(
  134. Vector128<float> Vector,
  135. Vector128<float> Tb0,
  136. Vector128<float> Tb1,
  137. Vector128<float> Tb2)
  138. {
  139. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  140. }
  141. public static Vector128<float> Tbl3_V128(
  142. Vector128<float> Vector,
  143. Vector128<float> Tb0,
  144. Vector128<float> Tb1,
  145. Vector128<float> Tb2)
  146. {
  147. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  148. }
  149. public static Vector128<float> Tbl4_V64(
  150. Vector128<float> Vector,
  151. Vector128<float> Tb0,
  152. Vector128<float> Tb1,
  153. Vector128<float> Tb2,
  154. Vector128<float> Tb3)
  155. {
  156. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  157. }
  158. public static Vector128<float> Tbl4_V128(
  159. Vector128<float> Vector,
  160. Vector128<float> Tb0,
  161. Vector128<float> Tb1,
  162. Vector128<float> Tb2,
  163. Vector128<float> Tb3)
  164. {
  165. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  166. }
  167. private static Vector128<float> Tbl(Vector128<float> Vector, int Bytes, params Vector128<float>[] Tb)
  168. {
  169. Vector128<float> Res = new Vector128<float>();
  170. byte[] Table = new byte[Tb.Length * 16];
  171. for (byte Index = 0; Index < Tb.Length; Index++)
  172. for (byte Index2 = 0; Index2 < 16; Index2++)
  173. {
  174. Table[Index * 16 + Index2] = (byte)VectorExtractIntZx(Tb[Index], Index2, 0);
  175. }
  176. for (byte Index = 0; Index < Bytes; Index++)
  177. {
  178. byte TblIdx = (byte)VectorExtractIntZx(Vector, Index, 0);
  179. if (TblIdx < Table.Length)
  180. {
  181. Res = VectorInsertInt(Table[TblIdx], Res, Index, 0);
  182. }
  183. }
  184. return Res;
  185. }
  186. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  187. public static double VectorExtractDouble(Vector128<float> Vector, byte Index)
  188. {
  189. return BitConverter.Int64BitsToDouble(VectorExtractIntSx(Vector, Index, 3));
  190. }
  191. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  192. public static long VectorExtractIntSx(Vector128<float> Vector, byte Index, int Size)
  193. {
  194. if (Sse41.IsSupported)
  195. {
  196. switch (Size)
  197. {
  198. case 0:
  199. return (sbyte)Sse41.Extract(Sse.StaticCast<float, byte>(Vector), Index);
  200. case 1:
  201. return (short)Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), Index);
  202. case 2:
  203. return Sse41.Extract(Sse.StaticCast<float, int>(Vector), Index);
  204. case 3:
  205. return Sse41.Extract(Sse.StaticCast<float, long>(Vector), Index);
  206. }
  207. throw new ArgumentOutOfRangeException(nameof(Size));
  208. }
  209. else if (Sse2.IsSupported)
  210. {
  211. switch (Size)
  212. {
  213. case 0:
  214. return (sbyte)VectorExtractIntZx(Vector, Index, Size);
  215. case 1:
  216. return (short)VectorExtractIntZx(Vector, Index, Size);
  217. case 2:
  218. return (int)VectorExtractIntZx(Vector, Index, Size);
  219. case 3:
  220. return (long)VectorExtractIntZx(Vector, Index, Size);
  221. }
  222. throw new ArgumentOutOfRangeException(nameof(Size));
  223. }
  224. throw new PlatformNotSupportedException();
  225. }
  226. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  227. public static ulong VectorExtractIntZx(Vector128<float> Vector, byte Index, int Size)
  228. {
  229. if (Sse41.IsSupported)
  230. {
  231. switch (Size)
  232. {
  233. case 0:
  234. return Sse41.Extract(Sse.StaticCast<float, byte>(Vector), Index);
  235. case 1:
  236. return Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), Index);
  237. case 2:
  238. return Sse41.Extract(Sse.StaticCast<float, uint>(Vector), Index);
  239. case 3:
  240. return Sse41.Extract(Sse.StaticCast<float, ulong>(Vector), Index);
  241. }
  242. throw new ArgumentOutOfRangeException(nameof(Size));
  243. }
  244. else if (Sse2.IsSupported)
  245. {
  246. int ShortIdx = Size == 0
  247. ? Index >> 1
  248. : Index << (Size - 1);
  249. ushort Value = Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), (byte)ShortIdx);
  250. switch (Size)
  251. {
  252. case 0:
  253. return (byte)(Value >> (Index & 1) * 8);
  254. case 1:
  255. return Value;
  256. case 2:
  257. case 3:
  258. {
  259. ushort Value1 = Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), (byte)(ShortIdx + 1));
  260. if (Size == 2)
  261. {
  262. return (uint)(Value | (Value1 << 16));
  263. }
  264. ushort Value2 = Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), (byte)(ShortIdx + 2));
  265. ushort Value3 = Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), (byte)(ShortIdx + 3));
  266. return ((ulong)Value << 0) |
  267. ((ulong)Value1 << 16) |
  268. ((ulong)Value2 << 32) |
  269. ((ulong)Value3 << 48);
  270. }
  271. }
  272. throw new ArgumentOutOfRangeException(nameof(Size));
  273. }
  274. throw new PlatformNotSupportedException();
  275. }
  276. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  277. public static float VectorExtractSingle(Vector128<float> Vector, byte Index)
  278. {
  279. if (Sse41.IsSupported)
  280. {
  281. return Sse41.Extract(Vector, Index);
  282. }
  283. else if (Sse2.IsSupported)
  284. {
  285. Vector128<ushort> ShortVector = Sse.StaticCast<float, ushort>(Vector);
  286. int Low = Sse2.Extract(ShortVector, (byte)(Index * 2 + 0));
  287. int High = Sse2.Extract(ShortVector, (byte)(Index * 2 + 1));
  288. return BitConverter.Int32BitsToSingle(Low | (High << 16));
  289. }
  290. throw new PlatformNotSupportedException();
  291. }
  292. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  293. public static Vector128<float> VectorInsertDouble(double Value, Vector128<float> Vector, byte Index)
  294. {
  295. return VectorInsertInt((ulong)BitConverter.DoubleToInt64Bits(Value), Vector, Index, 3);
  296. }
  297. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  298. public static Vector128<float> VectorInsertInt(ulong Value, Vector128<float> Vector, byte Index, int Size)
  299. {
  300. if (Sse41.IsSupported)
  301. {
  302. switch (Size)
  303. {
  304. case 0:
  305. return Sse.StaticCast<byte, float>(Sse41.Insert(Sse.StaticCast<float, byte>(Vector), (byte)Value, Index));
  306. case 1:
  307. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse.StaticCast<float, ushort>(Vector), (ushort)Value, Index));
  308. case 2:
  309. return Sse.StaticCast<uint, float>(Sse41.Insert(Sse.StaticCast<float, uint>(Vector), (uint)Value, Index));
  310. case 3:
  311. return Sse.StaticCast<ulong, float>(Sse41.Insert(Sse.StaticCast<float, ulong>(Vector), Value, Index));
  312. }
  313. throw new ArgumentOutOfRangeException(nameof(Size));
  314. }
  315. else if (Sse2.IsSupported)
  316. {
  317. Vector128<ushort> ShortVector = Sse.StaticCast<float, ushort>(Vector);
  318. int ShortIdx = Size == 0
  319. ? Index >> 1
  320. : Index << (Size - 1);
  321. switch (Size)
  322. {
  323. case 0:
  324. {
  325. ushort ShortVal = Sse2.Extract(Sse.StaticCast<float, ushort>(Vector), (byte)ShortIdx);
  326. int Shift = (Index & 1) * 8;
  327. ShortVal &= (ushort)(0xff00 >> Shift);
  328. ShortVal |= (ushort)((byte)Value << Shift);
  329. return Sse.StaticCast<ushort, float>(Sse2.Insert(ShortVector, ShortVal, (byte)ShortIdx));
  330. }
  331. case 1:
  332. return Sse.StaticCast<ushort, float>(Sse2.Insert(Sse.StaticCast<float, ushort>(Vector), (ushort)Value, Index));
  333. case 2:
  334. case 3:
  335. {
  336. ShortVector = Sse2.Insert(ShortVector, (ushort)(Value >> 0), (byte)(ShortIdx + 0));
  337. ShortVector = Sse2.Insert(ShortVector, (ushort)(Value >> 16), (byte)(ShortIdx + 1));
  338. if (Size == 3)
  339. {
  340. ShortVector = Sse2.Insert(ShortVector, (ushort)(Value >> 32), (byte)(ShortIdx + 2));
  341. ShortVector = Sse2.Insert(ShortVector, (ushort)(Value >> 48), (byte)(ShortIdx + 3));
  342. }
  343. return Sse.StaticCast<ushort, float>(ShortVector);
  344. }
  345. }
  346. throw new ArgumentOutOfRangeException(nameof(Size));
  347. }
  348. throw new PlatformNotSupportedException();
  349. }
  350. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  351. public static Vector128<float> VectorInsertSingle(float Value, Vector128<float> Vector, byte Index)
  352. {
  353. if (Sse41.IsSupported)
  354. {
  355. return Sse41.Insert(Vector, Value, (byte)(Index << 4));
  356. }
  357. else if (Sse2.IsSupported)
  358. {
  359. int IntValue = BitConverter.SingleToInt32Bits(Value);
  360. ushort Low = (ushort)(IntValue >> 0);
  361. ushort High = (ushort)(IntValue >> 16);
  362. Vector128<ushort> ShortVector = Sse.StaticCast<float, ushort>(Vector);
  363. ShortVector = Sse2.Insert(ShortVector, Low, (byte)(Index * 2 + 0));
  364. ShortVector = Sse2.Insert(ShortVector, High, (byte)(Index * 2 + 1));
  365. return Sse.StaticCast<ushort, float>(ShortVector);
  366. }
  367. throw new PlatformNotSupportedException();
  368. }
  369. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  370. public static Vector128<float> VectorZero32_128(Vector128<float> Vector)
  371. {
  372. if (Sse.IsSupported)
  373. {
  374. return Sse.And(Vector, Zero32_128Mask);
  375. }
  376. throw new PlatformNotSupportedException();
  377. }
  378. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  379. public static Vector128<sbyte> VectorSingleToSByte(Vector128<float> Vector)
  380. {
  381. if (Sse.IsSupported)
  382. {
  383. return Sse.StaticCast<float, sbyte>(Vector);
  384. }
  385. throw new PlatformNotSupportedException();
  386. }
  387. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  388. public static Vector128<short> VectorSingleToInt16(Vector128<float> Vector)
  389. {
  390. if (Sse.IsSupported)
  391. {
  392. return Sse.StaticCast<float, short>(Vector);
  393. }
  394. throw new PlatformNotSupportedException();
  395. }
  396. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  397. public static Vector128<int> VectorSingleToInt32(Vector128<float> Vector)
  398. {
  399. if (Sse.IsSupported)
  400. {
  401. return Sse.StaticCast<float, int>(Vector);
  402. }
  403. throw new PlatformNotSupportedException();
  404. }
  405. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  406. public static Vector128<long> VectorSingleToInt64(Vector128<float> Vector)
  407. {
  408. if (Sse.IsSupported)
  409. {
  410. return Sse.StaticCast<float, long>(Vector);
  411. }
  412. throw new PlatformNotSupportedException();
  413. }
  414. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  415. public static Vector128<double> VectorSingleToDouble(Vector128<float> Vector)
  416. {
  417. if (Sse.IsSupported)
  418. {
  419. return Sse.StaticCast<float, double>(Vector);
  420. }
  421. throw new PlatformNotSupportedException();
  422. }
  423. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  424. public static Vector128<float> VectorSByteToSingle(Vector128<sbyte> Vector)
  425. {
  426. if (Sse.IsSupported)
  427. {
  428. return Sse.StaticCast<sbyte, float>(Vector);
  429. }
  430. throw new PlatformNotSupportedException();
  431. }
  432. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  433. public static Vector128<float> VectorInt16ToSingle(Vector128<short> Vector)
  434. {
  435. if (Sse.IsSupported)
  436. {
  437. return Sse.StaticCast<short, float>(Vector);
  438. }
  439. throw new PlatformNotSupportedException();
  440. }
  441. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  442. public static Vector128<float> VectorInt32ToSingle(Vector128<int> Vector)
  443. {
  444. if (Sse.IsSupported)
  445. {
  446. return Sse.StaticCast<int, float>(Vector);
  447. }
  448. throw new PlatformNotSupportedException();
  449. }
  450. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  451. public static Vector128<float> VectorInt64ToSingle(Vector128<long> Vector)
  452. {
  453. if (Sse.IsSupported)
  454. {
  455. return Sse.StaticCast<long, float>(Vector);
  456. }
  457. throw new PlatformNotSupportedException();
  458. }
  459. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  460. public static Vector128<float> VectorDoubleToSingle(Vector128<double> Vector)
  461. {
  462. if (Sse.IsSupported)
  463. {
  464. return Sse.StaticCast<double, float>(Vector);
  465. }
  466. throw new PlatformNotSupportedException();
  467. }
  468. }
  469. }