ASoftFallback.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. using ChocolArm64.State;
  2. using ChocolArm64.Translation;
  3. using System;
  4. namespace ChocolArm64.Instruction
  5. {
  6. static class ASoftFallback
  7. {
  8. public static void EmitCall(AILEmitterCtx Context, string Name64, string Name128)
  9. {
  10. bool IsSimd64 = Context.CurrOp.RegisterSize == ARegisterSize.SIMD64;
  11. Context.EmitCall(typeof(ASoftFallback), IsSimd64 ? Name64 : Name128);
  12. }
  13. public static void EmitCall(AILEmitterCtx Context, string MthdName)
  14. {
  15. Context.EmitCall(typeof(ASoftFallback), MthdName);
  16. }
  17. public static uint CountLeadingZeros32(uint Value) => (uint)CountLeadingZeros(Value, 32);
  18. public static ulong CountLeadingZeros64(ulong Value) => (ulong)CountLeadingZeros(Value, 64);
  19. private static ulong CountLeadingZeros(ulong Value, int Size)
  20. {
  21. int HighBit = Size - 1;
  22. for (int Bit = HighBit; Bit >= 0; Bit--)
  23. {
  24. if (((Value >> Bit) & 1) != 0)
  25. {
  26. return (ulong)(HighBit - Bit);
  27. }
  28. }
  29. return (ulong)Size;
  30. }
  31. public static uint ReverseBits32(uint Value)
  32. {
  33. Value = ((Value & 0xaaaaaaaa) >> 1) | ((Value & 0x55555555) << 1);
  34. Value = ((Value & 0xcccccccc) >> 2) | ((Value & 0x33333333) << 2);
  35. Value = ((Value & 0xf0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f) << 4);
  36. Value = ((Value & 0xff00ff00) >> 8) | ((Value & 0x00ff00ff) << 8);
  37. return (Value >> 16) | (Value << 16);
  38. }
  39. public static ulong ReverseBits64(ulong Value)
  40. {
  41. Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1) | ((Value & 0x5555555555555555) << 1);
  42. Value = ((Value & 0xcccccccccccccccc) >> 2) | ((Value & 0x3333333333333333) << 2);
  43. Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4);
  44. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  45. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  46. return (Value >> 32) | (Value << 32);
  47. }
  48. public static uint ReverseBytes16_32(uint Value) => (uint)ReverseBytes16_64(Value);
  49. public static uint ReverseBytes32_32(uint Value) => (uint)ReverseBytes32_64(Value);
  50. public static ulong ReverseBytes16_64(ulong Value) => ReverseBytes(Value, RevSize.Rev16);
  51. public static ulong ReverseBytes32_64(ulong Value) => ReverseBytes(Value, RevSize.Rev32);
  52. public static ulong ReverseBytes64(ulong Value) => ReverseBytes(Value, RevSize.Rev64);
  53. private enum RevSize
  54. {
  55. Rev16,
  56. Rev32,
  57. Rev64
  58. }
  59. private static ulong ReverseBytes(ulong Value, RevSize Size)
  60. {
  61. Value = ((Value & 0xff00ff00ff00ff00) >> 8) | ((Value & 0x00ff00ff00ff00ff) << 8);
  62. if (Size == RevSize.Rev16)
  63. {
  64. return Value;
  65. }
  66. Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16);
  67. if (Size == RevSize.Rev32)
  68. {
  69. return Value;
  70. }
  71. Value = ((Value & 0xffffffff00000000) >> 32) | ((Value & 0x00000000ffffffff) << 32);
  72. if (Size == RevSize.Rev64)
  73. {
  74. return Value;
  75. }
  76. throw new ArgumentException(nameof(Size));
  77. }
  78. public static int SatDoubleToInt32(double Value, int FBits = 0)
  79. {
  80. if (FBits != 0) Value *= Math.Pow(2, FBits);
  81. return Value > int.MaxValue ? int.MaxValue :
  82. Value < int.MinValue ? int.MinValue : (int)Value;
  83. }
  84. public static long SatDoubleToInt64(double Value, int FBits = 0)
  85. {
  86. if (FBits != 0) Value *= Math.Pow(2, FBits);
  87. return Value > long.MaxValue ? long.MaxValue :
  88. Value < long.MinValue ? long.MinValue : (long)Value;
  89. }
  90. public static uint SatDoubleToUInt32(double Value, int FBits = 0)
  91. {
  92. if (FBits != 0) Value *= Math.Pow(2, FBits);
  93. return Value > uint.MaxValue ? uint.MaxValue :
  94. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  95. }
  96. public static ulong SatDoubleToUInt64(double Value, int FBits = 0)
  97. {
  98. if (FBits != 0) Value *= Math.Pow(2, FBits);
  99. return Value > ulong.MaxValue ? ulong.MaxValue :
  100. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  101. }
  102. public static int SatSingleToInt32(float Value, int FBits = 0)
  103. {
  104. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  105. return Value > int.MaxValue ? int.MaxValue :
  106. Value < int.MinValue ? int.MinValue : (int)Value;
  107. }
  108. public static long SatSingleToInt64(float Value, int FBits = 0)
  109. {
  110. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  111. return Value > long.MaxValue ? long.MaxValue :
  112. Value < long.MinValue ? long.MinValue : (long)Value;
  113. }
  114. public static uint SatSingleToUInt32(float Value, int FBits = 0)
  115. {
  116. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  117. return Value > uint.MaxValue ? uint.MaxValue :
  118. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  119. }
  120. public static ulong SatSingleToUInt64(float Value, int FBits = 0)
  121. {
  122. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  123. return Value > ulong.MaxValue ? ulong.MaxValue :
  124. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  125. }
  126. public static ulong SMulHi128(ulong LHS, ulong RHS)
  127. {
  128. long LLo = (uint)(LHS >> 0);
  129. long LHi = (int)(LHS >> 32);
  130. long RLo = (uint)(RHS >> 0);
  131. long RHi = (int)(RHS >> 32);
  132. long LHiRHi = LHi * RHi;
  133. long LHiRLo = LHi * RLo;
  134. long LLoRHi = LLo * RHi;
  135. long LLoRLo = LLo * RLo;
  136. long Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  137. long ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  138. return (ulong)ResHi;
  139. }
  140. public static ulong UMulHi128(ulong LHS, ulong RHS)
  141. {
  142. ulong LLo = (uint)(LHS >> 0);
  143. ulong LHi = (uint)(LHS >> 32);
  144. ulong RLo = (uint)(RHS >> 0);
  145. ulong RHi = (uint)(RHS >> 32);
  146. ulong LHiRHi = LHi * RHi;
  147. ulong LHiRLo = LHi * RLo;
  148. ulong LLoRHi = LLo * RHi;
  149. ulong LLoRLo = LLo * RLo;
  150. ulong Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  151. ulong ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  152. return ResHi;
  153. }
  154. public static AVec Addp_S(AVec Vector, int Size)
  155. {
  156. ulong Low = ExtractVec(Vector, 0, Size);
  157. ulong High = ExtractVec(Vector, 1, Size);
  158. return InsertVec(new AVec(), 0, Size, Low + High);
  159. }
  160. public static AVec Addp64(AVec LHS, AVec RHS, int Size)
  161. {
  162. return Addp(LHS, RHS, Size, 8);
  163. }
  164. public static AVec Addp128(AVec LHS, AVec RHS, int Size)
  165. {
  166. return Addp(LHS, RHS, Size, 16);
  167. }
  168. private static AVec Addp(AVec LHS, AVec RHS, int Size, int Bytes)
  169. {
  170. AVec Res = new AVec();
  171. int Elems = Bytes >> Size;
  172. int Half = Elems >> 1;
  173. for (int Index = 0; Index < Elems; Index++)
  174. {
  175. int Elem = (Index & (Half - 1)) << 1;
  176. ulong L = Index < Half
  177. ? ExtractVec(LHS, Elem + 0, Size)
  178. : ExtractVec(RHS, Elem + 0, Size);
  179. ulong R = Index < Half
  180. ? ExtractVec(LHS, Elem + 1, Size)
  181. : ExtractVec(RHS, Elem + 1, Size);
  182. Res = InsertVec(Res, Index, Size, L + R);
  183. }
  184. return Res;
  185. }
  186. public static AVec Bic_Vi64(AVec Res, ulong Imm, int Size)
  187. {
  188. return Bic_Vi(Res, Imm, Size, 8);
  189. }
  190. public static AVec Bic_Vi128(AVec Res, ulong Imm, int Size)
  191. {
  192. return Bic_Vi(Res, Imm, Size, 16);
  193. }
  194. private static AVec Bic_Vi(AVec Res, ulong Imm, int Size, int Bytes)
  195. {
  196. int Elems = Bytes >> Size;
  197. for (int Index = 0; Index < Elems; Index++)
  198. {
  199. ulong Value = ExtractVec(Res, Index, Size);
  200. Res = InsertVec(Res, Index, Size, Value & ~Imm);
  201. }
  202. return Res;
  203. }
  204. public static AVec Cnt64(AVec Vector)
  205. {
  206. AVec Res = new AVec();
  207. Res.B0 = (byte)CountSetBits8(Vector.B0);
  208. Res.B1 = (byte)CountSetBits8(Vector.B1);
  209. Res.B2 = (byte)CountSetBits8(Vector.B2);
  210. Res.B3 = (byte)CountSetBits8(Vector.B3);
  211. Res.B4 = (byte)CountSetBits8(Vector.B4);
  212. Res.B5 = (byte)CountSetBits8(Vector.B5);
  213. Res.B6 = (byte)CountSetBits8(Vector.B6);
  214. Res.B7 = (byte)CountSetBits8(Vector.B7);
  215. return Res;
  216. }
  217. public static AVec Cnt128(AVec Vector)
  218. {
  219. AVec Res = new AVec();
  220. Res.B0 = (byte)CountSetBits8(Vector.B0);
  221. Res.B1 = (byte)CountSetBits8(Vector.B1);
  222. Res.B2 = (byte)CountSetBits8(Vector.B2);
  223. Res.B3 = (byte)CountSetBits8(Vector.B3);
  224. Res.B4 = (byte)CountSetBits8(Vector.B4);
  225. Res.B5 = (byte)CountSetBits8(Vector.B5);
  226. Res.B6 = (byte)CountSetBits8(Vector.B6);
  227. Res.B7 = (byte)CountSetBits8(Vector.B7);
  228. Res.B8 = (byte)CountSetBits8(Vector.B8);
  229. Res.B9 = (byte)CountSetBits8(Vector.B9);
  230. Res.B10 = (byte)CountSetBits8(Vector.B10);
  231. Res.B11 = (byte)CountSetBits8(Vector.B11);
  232. Res.B12 = (byte)CountSetBits8(Vector.B12);
  233. Res.B13 = (byte)CountSetBits8(Vector.B13);
  234. Res.B14 = (byte)CountSetBits8(Vector.B14);
  235. Res.B15 = (byte)CountSetBits8(Vector.B15);
  236. return Res;
  237. }
  238. private static int CountSetBits8(byte Value)
  239. {
  240. return (Value >> 0) & 1 + (Value >> 1) & 1 +
  241. (Value >> 2) & 1 + (Value >> 3) & 1 +
  242. (Value >> 4) & 1 + (Value >> 5) & 1 +
  243. (Value >> 6) & 1 + (Value >> 7);
  244. }
  245. public static AVec Dup_Gp64(ulong Value, int Size)
  246. {
  247. return Dup_Gp(Value, Size, 8);
  248. }
  249. public static AVec Dup_Gp128(ulong Value, int Size)
  250. {
  251. return Dup_Gp(Value, Size, 16);
  252. }
  253. private static AVec Dup_Gp(ulong Value, int Size, int Bytes)
  254. {
  255. AVec Res = new AVec();
  256. for (int Index = 0; Index < (Bytes >> Size); Index++)
  257. {
  258. Res = InsertVec(Res, Index, Size, Value);
  259. }
  260. return Res;
  261. }
  262. public static AVec Dup_S(AVec Vector, int Elem, int Size)
  263. {
  264. return InsertVec(new AVec(), 0, Size, ExtractVec(Vector, Elem, Size));
  265. }
  266. public static AVec Dup_V64(AVec Vector, int Elem, int Size)
  267. {
  268. return Dup_V(Vector, Elem, Size, 8);
  269. }
  270. public static AVec Dup_V128(AVec Vector, int Elem, int Size)
  271. {
  272. return Dup_V(Vector, Elem, Size, 16);
  273. }
  274. private static AVec Dup_V(AVec Vector, int Elem, int Size, int Bytes)
  275. {
  276. AVec Res = new AVec();
  277. ulong Value = ExtractVec(Vector, Elem, Size);
  278. for (Elem = 0; Elem < (Bytes >> Size); Elem++)
  279. {
  280. Res = InsertVec(Res, Elem, Size, Value);
  281. }
  282. return Res;
  283. }
  284. public static AVec Fadd64(AVec LHS, AVec RHS, int Size)
  285. {
  286. return Fadd(LHS, RHS, Size, 2);
  287. }
  288. public static AVec Fadd128(AVec LHS, AVec RHS, int Size)
  289. {
  290. return Fadd(LHS, RHS, Size, 4);
  291. }
  292. private static AVec Fadd(AVec LHS, AVec RHS, int Size, int Bytes)
  293. {
  294. AVec Res = new AVec();
  295. int Elems = Bytes >> Size;
  296. if (Size == 0)
  297. {
  298. for (int Index = 0; Index < Elems; Index++)
  299. {
  300. float L = LHS.ExtractSingle(Index);
  301. float R = RHS.ExtractSingle(Index);
  302. Res = AVec.InsertSingle(Res, Index, L + R);
  303. }
  304. }
  305. else
  306. {
  307. for (int Index = 0; Index < Elems; Index++)
  308. {
  309. double L = LHS.ExtractDouble(Index);
  310. double R = RHS.ExtractDouble(Index);
  311. Res = AVec.InsertDouble(Res, Index, L + R);
  312. }
  313. }
  314. return Res;
  315. }
  316. public static AVec Fcvtzs_V64(AVec Vector, int Size)
  317. {
  318. return Fcvtzs_V(Vector, Size, 2);
  319. }
  320. public static AVec Fcvtzs_V128(AVec Vector, int Size)
  321. {
  322. return Fcvtzs_V(Vector, Size, 4);
  323. }
  324. private static AVec Fcvtzs_V(AVec Vector, int Size, int Bytes)
  325. {
  326. AVec Res = new AVec();
  327. int Elems = Bytes >> Size;
  328. if (Size == 0)
  329. {
  330. for (int Index = 0; Index < Elems; Index++)
  331. {
  332. float Value = Vector.ExtractSingle(Index);
  333. Res = InsertSVec(Res, Index, Size + 2, SatSingleToInt32(Value));
  334. }
  335. }
  336. else
  337. {
  338. for (int Index = 0; Index < Elems; Index++)
  339. {
  340. double Value = Vector.ExtractDouble(Index);
  341. Res = InsertSVec(Res, Index, Size + 2, SatDoubleToInt64(Value));
  342. }
  343. }
  344. return Res;
  345. }
  346. public static AVec Fcvtzu_V_64(AVec Vector, int FBits, int Size)
  347. {
  348. return Fcvtzu_V(Vector, FBits, Size, 2);
  349. }
  350. public static AVec Fcvtzu_V_128(AVec Vector, int FBits, int Size)
  351. {
  352. return Fcvtzu_V(Vector, FBits, Size, 4);
  353. }
  354. private static AVec Fcvtzu_V(AVec Vector, int FBits, int Size, int Bytes)
  355. {
  356. AVec Res = new AVec();
  357. int Elems = Bytes >> Size;
  358. if (Size == 0)
  359. {
  360. for (int Index = 0; Index < Elems; Index++)
  361. {
  362. float Value = Vector.ExtractSingle(Index);
  363. Res = InsertVec(Res, Index, Size + 2, SatSingleToUInt32(Value, FBits));
  364. }
  365. }
  366. else
  367. {
  368. for (int Index = 0; Index < Elems; Index++)
  369. {
  370. double Value = Vector.ExtractDouble(Index);
  371. Res = InsertVec(Res, Index, Size + 2, SatDoubleToUInt64(Value, FBits));
  372. }
  373. }
  374. return Res;
  375. }
  376. public static AVec Fmla64(AVec Res, AVec LHS, AVec RHS, int Size)
  377. {
  378. return Fmla(Res, LHS, RHS, Size, 2);
  379. }
  380. public static AVec Fmla128(AVec Res, AVec LHS, AVec RHS, int Size)
  381. {
  382. return Fmla(Res, LHS, RHS, Size, 4);
  383. }
  384. private static AVec Fmla(AVec Res, AVec LHS, AVec RHS, int Size, int Bytes)
  385. {
  386. int Elems = Bytes >> Size;
  387. if (Size == 0)
  388. {
  389. for (int Index = 0; Index < Elems; Index++)
  390. {
  391. float L = LHS.ExtractSingle(Index);
  392. float R = RHS.ExtractSingle(Index);
  393. float Addend = Res.ExtractSingle(Index);
  394. Res = AVec.InsertSingle(Res, Index, Addend + L * R);
  395. }
  396. }
  397. else
  398. {
  399. for (int Index = 0; Index < Elems; Index++)
  400. {
  401. double L = LHS.ExtractDouble(Index);
  402. double R = RHS.ExtractDouble(Index);
  403. double Addend = Res.ExtractDouble(Index);
  404. Res = AVec.InsertDouble(Res, Index, Addend + L * R);
  405. }
  406. }
  407. return Res;
  408. }
  409. public static AVec Fmla_Ve64(AVec Res, AVec LHS, AVec RHS, int SIdx, int Size)
  410. {
  411. return Fmla_Ve(Res, LHS, RHS, SIdx, Size, 2);
  412. }
  413. public static AVec Fmla_Ve128(AVec Res, AVec LHS, AVec RHS, int SIdx, int Size)
  414. {
  415. return Fmla_Ve(Res, LHS, RHS, SIdx, Size, 4);
  416. }
  417. private static AVec Fmla_Ve(AVec Res, AVec LHS, AVec RHS, int SIdx, int Size, int Bytes)
  418. {
  419. int Elems = Bytes >> Size;
  420. if (Size == 0)
  421. {
  422. float R = RHS.ExtractSingle(SIdx);
  423. for (int Index = 0; Index < Elems; Index++)
  424. {
  425. float L = LHS.ExtractSingle(Index);
  426. float Addend = Res.ExtractSingle(Index);
  427. Res = AVec.InsertSingle(Res, Index, Addend + L * R);
  428. }
  429. }
  430. else
  431. {
  432. double R = RHS.ExtractDouble(SIdx);
  433. for (int Index = 0; Index < Elems; Index++)
  434. {
  435. double L = LHS.ExtractDouble(Index);
  436. double Addend = Res.ExtractDouble(Index);
  437. Res = AVec.InsertDouble(Res, Index, Addend + L * R);
  438. }
  439. }
  440. return Res;
  441. }
  442. public static AVec Fmov_S(ulong Value, int Elem, int Size)
  443. {
  444. return InsertVec(new AVec(), Elem, Size, Value);
  445. }
  446. public static AVec Fmul64(AVec LHS, AVec RHS, int Size)
  447. {
  448. return Fmul(LHS, RHS, Size, 2);
  449. }
  450. public static AVec Fmul128(AVec LHS, AVec RHS, int Size)
  451. {
  452. return Fmul(LHS, RHS, Size, 4);
  453. }
  454. private static AVec Fmul(AVec LHS, AVec RHS, int Size, int Bytes)
  455. {
  456. AVec Res = new AVec();
  457. int Elems = Bytes >> Size;
  458. if (Size == 0)
  459. {
  460. for (int Index = 0; Index < Elems; Index++)
  461. {
  462. float L = LHS.ExtractSingle(Index);
  463. float R = RHS.ExtractSingle(Index);
  464. Res = AVec.InsertSingle(Res, Index, L * R);
  465. }
  466. }
  467. else
  468. {
  469. for (int Index = 0; Index < Elems; Index++)
  470. {
  471. double L = LHS.ExtractDouble(Index);
  472. double R = RHS.ExtractDouble(Index);
  473. Res = AVec.InsertDouble(Res, Index, L * R);
  474. }
  475. }
  476. return Res;
  477. }
  478. public static AVec Fmul_Ve64(AVec LHS, AVec RHS, int SIdx, int Size)
  479. {
  480. return Fmul_Ve(LHS, RHS, SIdx, Size, 2);
  481. }
  482. public static AVec Fmul_Ve128(AVec LHS, AVec RHS, int SIdx, int Size)
  483. {
  484. return Fmul_Ve(LHS, RHS, SIdx, Size, 4);
  485. }
  486. private static AVec Fmul_Ve(AVec LHS, AVec RHS, int SIdx, int Size, int Bytes)
  487. {
  488. AVec Res = new AVec();
  489. int Elems = Bytes >> Size;
  490. if (Size == 0)
  491. {
  492. float R = RHS.ExtractSingle(SIdx);
  493. for (int Index = 0; Index < Elems; Index++)
  494. {
  495. float L = LHS.ExtractSingle(Index);
  496. Res = AVec.InsertSingle(Res, Index, L * R);
  497. }
  498. }
  499. else
  500. {
  501. double R = RHS.ExtractDouble(SIdx);
  502. for (int Index = 0; Index < Elems; Index++)
  503. {
  504. double L = LHS.ExtractDouble(Index);
  505. Res = AVec.InsertDouble(Res, Index, L * R);
  506. }
  507. }
  508. return Res;
  509. }
  510. public static AVec Fsub64(AVec LHS, AVec RHS, int Size)
  511. {
  512. return Fsub(LHS, RHS, Size, 2);
  513. }
  514. public static AVec Fsub128(AVec LHS, AVec RHS, int Size)
  515. {
  516. return Fsub(LHS, RHS, Size, 4);
  517. }
  518. private static AVec Fsub(AVec LHS, AVec RHS, int Size, int Bytes)
  519. {
  520. AVec Res = new AVec();
  521. int Elems = Bytes >> Size;
  522. if (Size == 0)
  523. {
  524. for (int Index = 0; Index < Elems; Index++)
  525. {
  526. float L = LHS.ExtractSingle(Index);
  527. float R = RHS.ExtractSingle(Index);
  528. Res = AVec.InsertSingle(Res, Index, L - R);
  529. }
  530. }
  531. else
  532. {
  533. for (int Index = 0; Index < Elems; Index++)
  534. {
  535. double L = LHS.ExtractDouble(Index);
  536. double R = RHS.ExtractDouble(Index);
  537. Res = AVec.InsertDouble(Res, Index, L - R);
  538. }
  539. }
  540. return Res;
  541. }
  542. public static AVec Ins_Gp(AVec Res, ulong Value, int Elem, int Size)
  543. {
  544. return InsertVec(Res, Elem, Size, Value);
  545. }
  546. public static AVec Ins_V(AVec Res, AVec Value, int Src, int Dst, int Size)
  547. {
  548. return InsertVec(Res, Dst, Size, ExtractVec(Value, Src, Size));;
  549. }
  550. public static AVec Orr_Vi64(AVec Res, ulong Imm, int Size)
  551. {
  552. return Orr_Vi(Res, Imm, Size, 8);
  553. }
  554. public static AVec Orr_Vi128(AVec Res, ulong Imm, int Size)
  555. {
  556. return Orr_Vi(Res, Imm, Size, 16);
  557. }
  558. private static AVec Orr_Vi(AVec Res, ulong Imm, int Size, int Bytes)
  559. {
  560. int Elems = Bytes >> Size;
  561. for (int Index = 0; Index < Elems; Index++)
  562. {
  563. ulong Value = ExtractVec(Res, Index, Size);
  564. Res = InsertVec(Res, Index, Size, Value | Imm);
  565. }
  566. return Res;
  567. }
  568. public static AVec Saddw(AVec LHS, AVec RHS, int Size)
  569. {
  570. return Saddw_(LHS, RHS, Size, false);
  571. }
  572. public static AVec Saddw2(AVec LHS, AVec RHS, int Size)
  573. {
  574. return Saddw_(LHS, RHS, Size, true);
  575. }
  576. private static AVec Saddw_(AVec LHS, AVec RHS, int Size, bool High)
  577. {
  578. AVec Res = new AVec();
  579. int Elems = 8 >> Size;
  580. int Part = High ? Elems : 0;
  581. for (int Index = 0; Index < Elems; Index++)
  582. {
  583. long L = ExtractSVec(LHS, Index, Size + 1);
  584. long R = ExtractSVec(RHS, Index + Part, Size);
  585. Res = InsertSVec(Res, Index, Size + 1, L + R);
  586. }
  587. return Res;
  588. }
  589. public static AVec Scvtf_V64(AVec Vector, int Size)
  590. {
  591. return Scvtf_V(Vector, Size, 2);
  592. }
  593. public static AVec Scvtf_V128(AVec Vector, int Size)
  594. {
  595. return Scvtf_V(Vector, Size, 4);
  596. }
  597. private static AVec Scvtf_V(AVec Vector, int Size, int Bytes)
  598. {
  599. AVec Res = new AVec();
  600. int Elems = Bytes >> Size;
  601. if (Size == 0)
  602. {
  603. for (int Index = 0; Index < Elems; Index++)
  604. {
  605. int Value = (int)ExtractSVec(Vector, Index, Size + 2);
  606. Res = AVec.InsertSingle(Res, Index, Value);
  607. }
  608. }
  609. else
  610. {
  611. for (int Index = 0; Index < Elems; Index++)
  612. {
  613. long Value = ExtractSVec(Vector, Index, Size + 2);
  614. Res = AVec.InsertDouble(Res, Index, Value);
  615. }
  616. }
  617. return Res;
  618. }
  619. public static AVec Shl64(AVec Vector, int Shift, int Size)
  620. {
  621. return Shl(Vector, Shift, Size, 8);
  622. }
  623. public static AVec Shl128(AVec Vector, int Shift, int Size)
  624. {
  625. return Shl(Vector, Shift, Size, 16);
  626. }
  627. private static AVec Shl(AVec Vector, int Shift, int Size, int Bytes)
  628. {
  629. AVec Res = new AVec();
  630. int Elems = Bytes >> Size;
  631. for (int Index = 0; Index < Elems; Index++)
  632. {
  633. ulong Value = ExtractVec(Vector, Index, Size);
  634. Res = InsertVec(Res, Index, Size, Value << Shift);
  635. }
  636. return Res;
  637. }
  638. public static AVec Sshll(AVec Vector, int Shift, int Size)
  639. {
  640. return Sshll_(Vector, Shift, Size, false);
  641. }
  642. public static AVec Sshll2(AVec Vector, int Shift, int Size)
  643. {
  644. return Sshll_(Vector, Shift, Size, true);
  645. }
  646. private static AVec Sshll_(AVec Vector, int Shift, int Size, bool High)
  647. {
  648. AVec Res = new AVec();
  649. int Elems = 8 >> Size;
  650. int Part = High ? Elems : 0;
  651. for (int Index = 0; Index < Elems; Index++)
  652. {
  653. long Value = ExtractSVec(Vector, Index + Part, Size);
  654. Res = InsertSVec(Res, Index, Size + 1, Value << Shift);
  655. }
  656. return Res;
  657. }
  658. public static AVec Sshr64(AVec Vector, int Shift, int Size)
  659. {
  660. return Sshr(Vector, Shift, Size, 8);
  661. }
  662. public static AVec Sshr128(AVec Vector, int Shift, int Size)
  663. {
  664. return Sshr(Vector, Shift, Size, 16);
  665. }
  666. private static AVec Sshr(AVec Vector, int Shift, int Size, int Bytes)
  667. {
  668. AVec Res = new AVec();
  669. int Elems = Bytes >> Size;
  670. for (int Index = 0; Index < Elems; Index++)
  671. {
  672. long Value = ExtractSVec(Vector, Index, Size);
  673. Res = InsertSVec(Res, Index, Size, Value >> Shift);
  674. }
  675. return Res;
  676. }
  677. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  678. {
  679. return Tbl(Vector, 8, Tb0);
  680. }
  681. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  682. {
  683. return Tbl(Vector, 16, Tb0);
  684. }
  685. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  686. {
  687. return Tbl(Vector, 8, Tb0, Tb1);
  688. }
  689. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  690. {
  691. return Tbl(Vector, 16, Tb0, Tb1);
  692. }
  693. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  694. {
  695. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  696. }
  697. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  698. {
  699. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  700. }
  701. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  702. {
  703. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  704. }
  705. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  706. {
  707. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  708. }
  709. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  710. {
  711. AVec Res = new AVec();
  712. byte[] Table = new byte[Tb.Length * 16];
  713. for (int Index = 0; Index < Tb.Length; Index++)
  714. for (int Index2 = 0; Index2 < 16; Index2++)
  715. {
  716. Table[Index * 16 + Index2] = (byte)ExtractVec(Tb[Index], Index2, 0);
  717. }
  718. for (int Index = 0; Index < Bytes; Index++)
  719. {
  720. byte TblIdx = (byte)ExtractVec(Vector, Index, 0);
  721. if (TblIdx < Table.Length)
  722. {
  723. Res = InsertVec(Res, Index, 0, Table[TblIdx]);
  724. }
  725. }
  726. return Res;
  727. }
  728. public static AVec Uaddlv64(AVec Vector, int Size)
  729. {
  730. return Uaddlv(Vector, Size, 8);
  731. }
  732. public static AVec Uaddlv128(AVec Vector, int Size)
  733. {
  734. return Uaddlv(Vector, Size, 16);
  735. }
  736. private static AVec Uaddlv(AVec Vector, int Size, int Bytes)
  737. {
  738. int Elems = Bytes >> Size;
  739. ulong Sum = 0;
  740. for (int Index = 0; Index < Elems; Index++)
  741. {
  742. Sum += ExtractVec(Vector, Index, Size);
  743. }
  744. return InsertVec(new AVec(), 0, 3, Sum);
  745. }
  746. public static AVec Uaddw(AVec LHS, AVec RHS, int Size)
  747. {
  748. return Uaddw_(LHS, RHS, Size, false);
  749. }
  750. public static AVec Uaddw2(AVec LHS, AVec RHS, int Size)
  751. {
  752. return Uaddw_(LHS, RHS, Size, true);
  753. }
  754. private static AVec Uaddw_(AVec LHS, AVec RHS, int Size, bool High)
  755. {
  756. AVec Res = new AVec();
  757. int Elems = 8 >> Size;
  758. int Part = High ? Elems : 0;
  759. for (int Index = 0; Index < Elems; Index++)
  760. {
  761. ulong L = ExtractVec(LHS, Index, Size + 1);
  762. ulong R = ExtractVec(RHS, Index + Part, Size);
  763. Res = InsertVec(Res, Index, Size + 1, L + R);
  764. }
  765. return Res;
  766. }
  767. public static AVec Ucvtf_V_F(AVec Vector)
  768. {
  769. return new AVec()
  770. {
  771. S0 = (uint)Vector.W0,
  772. S1 = (uint)Vector.W1,
  773. S2 = (uint)Vector.W2,
  774. S3 = (uint)Vector.W3
  775. };
  776. }
  777. public static AVec Ucvtf_V_D(AVec Vector)
  778. {
  779. return new AVec()
  780. {
  781. D0 = (ulong)Vector.X0,
  782. D1 = (ulong)Vector.X1
  783. };
  784. }
  785. public static AVec Ushll(AVec Vector, int Shift, int Size)
  786. {
  787. return Ushll_(Vector, Shift, Size, false);
  788. }
  789. public static AVec Ushll2(AVec Vector, int Shift, int Size)
  790. {
  791. return Ushll_(Vector, Shift, Size, true);
  792. }
  793. private static AVec Ushll_(AVec Vector, int Shift, int Size, bool High)
  794. {
  795. AVec Res = new AVec();
  796. int Elems = 8 >> Size;
  797. int Part = High ? Elems : 0;
  798. for (int Index = 0; Index < Elems; Index++)
  799. {
  800. ulong Value = ExtractVec(Vector, Index + Part, Size);
  801. Res = InsertVec(Res, Index, Size + 1, Value << Shift);
  802. }
  803. return Res;
  804. }
  805. public static AVec Ushr64(AVec Vector, int Shift, int Size)
  806. {
  807. return Ushr(Vector, Shift, Size, 8);
  808. }
  809. public static AVec Ushr128(AVec Vector, int Shift, int Size)
  810. {
  811. return Ushr(Vector, Shift, Size, 16);
  812. }
  813. private static AVec Ushr(AVec Vector, int Shift, int Size, int Bytes)
  814. {
  815. AVec Res = new AVec();
  816. int Elems = Bytes >> Size;
  817. for (int Index = 0; Index < Elems; Index++)
  818. {
  819. ulong Value = ExtractVec(Vector, Index, Size);
  820. Res = InsertVec(Res, Index, Size, Value >> Shift);
  821. }
  822. return Res;
  823. }
  824. public static AVec Usra64(AVec Res, AVec Vector, int Shift, int Size)
  825. {
  826. return Usra(Res, Vector, Shift, Size, 8);
  827. }
  828. public static AVec Usra128(AVec Res, AVec Vector, int Shift, int Size)
  829. {
  830. return Usra(Res, Vector, Shift, Size, 16);
  831. }
  832. private static AVec Usra(AVec Res, AVec Vector, int Shift, int Size, int Bytes)
  833. {
  834. int Elems = Bytes >> Size;
  835. for (int Index = 0; Index < Elems; Index++)
  836. {
  837. ulong Value = ExtractVec(Vector, Index, Size);
  838. ulong Addend = ExtractVec(Res, Index, Size);
  839. Res = InsertVec(Res, Index, Size, Addend + (Value >> Shift));
  840. }
  841. return Res;
  842. }
  843. public static AVec Uzp1_V64(AVec LHS, AVec RHS, int Size)
  844. {
  845. return Uzp(LHS, RHS, Size, 0, 8);
  846. }
  847. public static AVec Uzp1_V128(AVec LHS, AVec RHS, int Size)
  848. {
  849. return Uzp(LHS, RHS, Size, 0, 16);
  850. }
  851. public static AVec Uzp2_V64(AVec LHS, AVec RHS, int Size)
  852. {
  853. return Uzp(LHS, RHS, Size, 1, 8);
  854. }
  855. public static AVec Uzp2_V128(AVec LHS, AVec RHS, int Size)
  856. {
  857. return Uzp(LHS, RHS, Size, 1, 16);
  858. }
  859. private static AVec Uzp(AVec LHS, AVec RHS, int Size, int Part, int Bytes)
  860. {
  861. AVec Res = new AVec();
  862. int Elems = Bytes >> Size;
  863. int Half = Elems >> 1;
  864. for (int Index = 0; Index < Elems; Index++)
  865. {
  866. int Elem = (Index & (Half - 1)) << 1;
  867. ulong Value = Index < Half
  868. ? ExtractVec(LHS, Elem + Part, Size)
  869. : ExtractVec(RHS, Elem + Part, Size);
  870. Res = InsertVec(Res, Index, Size, Value);
  871. }
  872. return Res;
  873. }
  874. public static AVec Xtn(AVec Vector, int Size)
  875. {
  876. return Xtn_(Vector, Size, false);
  877. }
  878. public static AVec Xtn2(AVec Vector, int Size)
  879. {
  880. return Xtn_(Vector, Size, true);
  881. }
  882. private static AVec Xtn_(AVec Vector, int Size, bool High)
  883. {
  884. AVec Res = new AVec();
  885. int Elems = 8 >> Size;
  886. int Part = High ? Elems : 0;
  887. for (int Index = 0; Index < Elems; Index++)
  888. {
  889. ulong Value = ExtractVec(Vector, Index, Size + 1);
  890. Res = InsertVec(Res, Index + Part, Size, Value);
  891. }
  892. return Res;
  893. }
  894. public static ulong ExtractVec(AVec Vector, int Index, int Size)
  895. {
  896. switch (Size)
  897. {
  898. case 0: return Vector.ExtractByte(Index);
  899. case 1: return Vector.ExtractUInt16(Index);
  900. case 2: return Vector.ExtractUInt32(Index);
  901. case 3: return Vector.ExtractUInt64(Index);
  902. }
  903. throw new ArgumentOutOfRangeException(nameof(Size));
  904. }
  905. public static long ExtractSVec(AVec Vector, int Index, int Size)
  906. {
  907. switch (Size)
  908. {
  909. case 0: return (sbyte)Vector.ExtractByte(Index);
  910. case 1: return (short)Vector.ExtractUInt16(Index);
  911. case 2: return (int)Vector.ExtractUInt32(Index);
  912. case 3: return (long)Vector.ExtractUInt64(Index);
  913. }
  914. throw new ArgumentOutOfRangeException(nameof(Size));
  915. }
  916. public static AVec InsertVec(AVec Vector, int Index, int Size, ulong Value)
  917. {
  918. switch (Size)
  919. {
  920. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  921. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  922. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  923. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  924. }
  925. throw new ArgumentOutOfRangeException(nameof(Size));
  926. }
  927. public static AVec InsertSVec(AVec Vector, int Index, int Size, long Value)
  928. {
  929. switch (Size)
  930. {
  931. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  932. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  933. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  934. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  935. }
  936. throw new ArgumentOutOfRangeException(nameof(Size));
  937. }
  938. }
  939. }