AVectorHelper.cs 21 KB

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