ASoftFallback.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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 SatSingleToInt32(float Value, int FBits)
  79. {
  80. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  81. return Value > int.MaxValue ? int.MaxValue :
  82. Value < int.MinValue ? int.MinValue : (int)Value;
  83. }
  84. public static long SatSingleToInt64(float Value, int FBits)
  85. {
  86. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  87. return Value > long.MaxValue ? long.MaxValue :
  88. Value < long.MinValue ? long.MinValue : (long)Value;
  89. }
  90. public static uint SatSingleToUInt32(float Value, int FBits)
  91. {
  92. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  93. return Value > uint.MaxValue ? uint.MaxValue :
  94. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  95. }
  96. public static ulong SatSingleToUInt64(float Value, int FBits)
  97. {
  98. if (FBits != 0) Value *= MathF.Pow(2, FBits);
  99. return Value > ulong.MaxValue ? ulong.MaxValue :
  100. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  101. }
  102. public static int SatDoubleToInt32(double Value, int FBits)
  103. {
  104. if (FBits != 0) Value *= Math.Pow(2, FBits);
  105. return Value > int.MaxValue ? int.MaxValue :
  106. Value < int.MinValue ? int.MinValue : (int)Value;
  107. }
  108. public static long SatDoubleToInt64(double Value, int FBits)
  109. {
  110. if (FBits != 0) Value *= Math.Pow(2, FBits);
  111. return Value > long.MaxValue ? long.MaxValue :
  112. Value < long.MinValue ? long.MinValue : (long)Value;
  113. }
  114. public static uint SatDoubleToUInt32(double Value, int FBits)
  115. {
  116. if (FBits != 0) Value *= Math.Pow(2, FBits);
  117. return Value > uint.MaxValue ? uint.MaxValue :
  118. Value < uint.MinValue ? uint.MinValue : (uint)Value;
  119. }
  120. public static ulong SatDoubleToUInt64(double Value, int FBits)
  121. {
  122. if (FBits != 0) Value *= Math.Pow(2, FBits);
  123. return Value > ulong.MaxValue ? ulong.MaxValue :
  124. Value < ulong.MinValue ? ulong.MinValue : (ulong)Value;
  125. }
  126. public static float Int32ToSingle(int Value, int FBits)
  127. {
  128. float ValueF = Value;
  129. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  130. return ValueF;
  131. }
  132. public static float Int64ToSingle(long Value, int FBits)
  133. {
  134. float ValueF = Value;
  135. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  136. return ValueF;
  137. }
  138. public static float UInt32ToSingle(uint Value, int FBits)
  139. {
  140. float ValueF = Value;
  141. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  142. return ValueF;
  143. }
  144. public static float UInt64ToSingle(ulong Value, int FBits)
  145. {
  146. float ValueF = Value;
  147. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  148. return ValueF;
  149. }
  150. public static float Int32ToDouble(int Value, int FBits)
  151. {
  152. float ValueF = Value;
  153. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  154. return ValueF;
  155. }
  156. public static float Int64ToDouble(long Value, int FBits)
  157. {
  158. float ValueF = Value;
  159. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  160. return ValueF;
  161. }
  162. public static float UInt32ToDouble(uint Value, int FBits)
  163. {
  164. float ValueF = Value;
  165. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  166. return ValueF;
  167. }
  168. public static float UInt64ToDouble(ulong Value, int FBits)
  169. {
  170. float ValueF = Value;
  171. if (FBits != 0) ValueF *= 1 / MathF.Pow(2, FBits);
  172. return ValueF;
  173. }
  174. public static ulong SMulHi128(ulong LHS, ulong RHS)
  175. {
  176. long LLo = (uint)(LHS >> 0);
  177. long LHi = (int)(LHS >> 32);
  178. long RLo = (uint)(RHS >> 0);
  179. long RHi = (int)(RHS >> 32);
  180. long LHiRHi = LHi * RHi;
  181. long LHiRLo = LHi * RLo;
  182. long LLoRHi = LLo * RHi;
  183. long LLoRLo = LLo * RLo;
  184. long Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  185. long ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  186. return (ulong)ResHi;
  187. }
  188. public static ulong UMulHi128(ulong LHS, ulong RHS)
  189. {
  190. ulong LLo = (uint)(LHS >> 0);
  191. ulong LHi = (uint)(LHS >> 32);
  192. ulong RLo = (uint)(RHS >> 0);
  193. ulong RHi = (uint)(RHS >> 32);
  194. ulong LHiRHi = LHi * RHi;
  195. ulong LHiRLo = LHi * RLo;
  196. ulong LLoRHi = LLo * RHi;
  197. ulong LLoRLo = LLo * RLo;
  198. ulong Carry = ((uint)LHiRLo + ((uint)LLoRHi + (LLoRLo >> 32))) >> 32;
  199. ulong ResHi = LHiRHi + (LHiRLo >> 32) + (LLoRHi >> 32) + Carry;
  200. return ResHi;
  201. }
  202. public static AVec Addp_S(AVec Vector, int Size)
  203. {
  204. ulong Low = ExtractVec(Vector, 0, Size);
  205. ulong High = ExtractVec(Vector, 1, Size);
  206. return InsertVec(new AVec(), 0, Size, Low + High);
  207. }
  208. public static AVec Addp64(AVec LHS, AVec RHS, int Size)
  209. {
  210. return Addp(LHS, RHS, Size, 8);
  211. }
  212. public static AVec Addp128(AVec LHS, AVec RHS, int Size)
  213. {
  214. return Addp(LHS, RHS, Size, 16);
  215. }
  216. private static AVec Addp(AVec LHS, AVec RHS, int Size, int Bytes)
  217. {
  218. AVec Res = new AVec();
  219. int Elems = Bytes >> Size;
  220. int Half = Elems >> 1;
  221. for (int Index = 0; Index < Elems; Index++)
  222. {
  223. int Elem = (Index & (Half - 1)) << 1;
  224. ulong L = Index < Half
  225. ? ExtractVec(LHS, Elem + 0, Size)
  226. : ExtractVec(RHS, Elem + 0, Size);
  227. ulong R = Index < Half
  228. ? ExtractVec(LHS, Elem + 1, Size)
  229. : ExtractVec(RHS, Elem + 1, Size);
  230. Res = InsertVec(Res, Index, Size, L + R);
  231. }
  232. return Res;
  233. }
  234. public static AVec Bic_Vi64(AVec Res, ulong Imm, int Size)
  235. {
  236. return Bic_Vi(Res, Imm, Size, 8);
  237. }
  238. public static AVec Bic_Vi128(AVec Res, ulong Imm, int Size)
  239. {
  240. return Bic_Vi(Res, Imm, Size, 16);
  241. }
  242. private static AVec Bic_Vi(AVec Res, ulong Imm, int Size, int Bytes)
  243. {
  244. int Elems = Bytes >> Size;
  245. for (int Index = 0; Index < Elems; Index++)
  246. {
  247. ulong Value = ExtractVec(Res, Index, Size);
  248. Res = InsertVec(Res, Index, Size, Value & ~Imm);
  249. }
  250. return Res;
  251. }
  252. public static AVec Cnt64(AVec Vector)
  253. {
  254. AVec Res = new AVec();
  255. Res.B0 = (byte)CountSetBits8(Vector.B0);
  256. Res.B1 = (byte)CountSetBits8(Vector.B1);
  257. Res.B2 = (byte)CountSetBits8(Vector.B2);
  258. Res.B3 = (byte)CountSetBits8(Vector.B3);
  259. Res.B4 = (byte)CountSetBits8(Vector.B4);
  260. Res.B5 = (byte)CountSetBits8(Vector.B5);
  261. Res.B6 = (byte)CountSetBits8(Vector.B6);
  262. Res.B7 = (byte)CountSetBits8(Vector.B7);
  263. return Res;
  264. }
  265. public static AVec Cnt128(AVec Vector)
  266. {
  267. AVec Res = new AVec();
  268. Res.B0 = (byte)CountSetBits8(Vector.B0);
  269. Res.B1 = (byte)CountSetBits8(Vector.B1);
  270. Res.B2 = (byte)CountSetBits8(Vector.B2);
  271. Res.B3 = (byte)CountSetBits8(Vector.B3);
  272. Res.B4 = (byte)CountSetBits8(Vector.B4);
  273. Res.B5 = (byte)CountSetBits8(Vector.B5);
  274. Res.B6 = (byte)CountSetBits8(Vector.B6);
  275. Res.B7 = (byte)CountSetBits8(Vector.B7);
  276. Res.B8 = (byte)CountSetBits8(Vector.B8);
  277. Res.B9 = (byte)CountSetBits8(Vector.B9);
  278. Res.B10 = (byte)CountSetBits8(Vector.B10);
  279. Res.B11 = (byte)CountSetBits8(Vector.B11);
  280. Res.B12 = (byte)CountSetBits8(Vector.B12);
  281. Res.B13 = (byte)CountSetBits8(Vector.B13);
  282. Res.B14 = (byte)CountSetBits8(Vector.B14);
  283. Res.B15 = (byte)CountSetBits8(Vector.B15);
  284. return Res;
  285. }
  286. private static int CountSetBits8(byte Value)
  287. {
  288. return (Value >> 0) & 1 + (Value >> 1) & 1 +
  289. (Value >> 2) & 1 + (Value >> 3) & 1 +
  290. (Value >> 4) & 1 + (Value >> 5) & 1 +
  291. (Value >> 6) & 1 + (Value >> 7);
  292. }
  293. public static AVec Dup_Gp64(ulong Value, int Size)
  294. {
  295. return Dup_Gp(Value, Size, 8);
  296. }
  297. public static AVec Dup_Gp128(ulong Value, int Size)
  298. {
  299. return Dup_Gp(Value, Size, 16);
  300. }
  301. private static AVec Dup_Gp(ulong Value, int Size, int Bytes)
  302. {
  303. AVec Res = new AVec();
  304. for (int Index = 0; Index < (Bytes >> Size); Index++)
  305. {
  306. Res = InsertVec(Res, Index, Size, Value);
  307. }
  308. return Res;
  309. }
  310. public static AVec Dup_S(AVec Vector, int Elem, int Size)
  311. {
  312. return InsertVec(new AVec(), 0, Size, ExtractVec(Vector, Elem, Size));
  313. }
  314. public static AVec Dup_V64(AVec Vector, int Elem, int Size)
  315. {
  316. return Dup_V(Vector, Elem, Size, 8);
  317. }
  318. public static AVec Dup_V128(AVec Vector, int Elem, int Size)
  319. {
  320. return Dup_V(Vector, Elem, Size, 16);
  321. }
  322. private static AVec Dup_V(AVec Vector, int Elem, int Size, int Bytes)
  323. {
  324. AVec Res = new AVec();
  325. ulong Value = ExtractVec(Vector, Elem, Size);
  326. for (Elem = 0; Elem < (Bytes >> Size); Elem++)
  327. {
  328. Res = InsertVec(Res, Elem, Size, Value);
  329. }
  330. return Res;
  331. }
  332. public static AVec Fmla64(AVec Res, AVec LHS, AVec RHS, int Size)
  333. {
  334. return Fmla(Res, LHS, RHS, Size, 2);
  335. }
  336. public static AVec Fmla128(AVec Res, AVec LHS, AVec RHS, int Size)
  337. {
  338. return Fmla(Res, LHS, RHS, Size, 4);
  339. }
  340. private static AVec Fmla(AVec Res, AVec LHS, AVec RHS, int Size, int Bytes)
  341. {
  342. int Elems = Bytes >> Size;
  343. if (Size == 0)
  344. {
  345. for (int Index = 0; Index < Elems; Index++)
  346. {
  347. float L = LHS.ExtractSingle(Index);
  348. float R = RHS.ExtractSingle(Index);
  349. float Addend = Res.ExtractSingle(Index);
  350. Res = AVec.InsertSingle(Res, Index, Addend + L * R);
  351. }
  352. }
  353. else
  354. {
  355. for (int Index = 0; Index < Elems; Index++)
  356. {
  357. double L = LHS.ExtractDouble(Index);
  358. double R = RHS.ExtractDouble(Index);
  359. double Addend = Res.ExtractDouble(Index);
  360. Res = AVec.InsertDouble(Res, Index, Addend + L * R);
  361. }
  362. }
  363. return Res;
  364. }
  365. public static AVec Fmla_Ve64(AVec Res, AVec LHS, AVec RHS, int SIdx, int Size)
  366. {
  367. return Fmla_Ve(Res, LHS, RHS, SIdx, Size, 2);
  368. }
  369. public static AVec Fmla_Ve128(AVec Res, AVec LHS, AVec RHS, int SIdx, int Size)
  370. {
  371. return Fmla_Ve(Res, LHS, RHS, SIdx, Size, 4);
  372. }
  373. private static AVec Fmla_Ve(AVec Res, AVec LHS, AVec RHS, int SIdx, int Size, int Bytes)
  374. {
  375. int Elems = Bytes >> Size;
  376. if (Size == 0)
  377. {
  378. float R = RHS.ExtractSingle(SIdx);
  379. for (int Index = 0; Index < Elems; Index++)
  380. {
  381. float L = LHS.ExtractSingle(Index);
  382. float Addend = Res.ExtractSingle(Index);
  383. Res = AVec.InsertSingle(Res, Index, Addend + L * R);
  384. }
  385. }
  386. else
  387. {
  388. double R = RHS.ExtractDouble(SIdx);
  389. for (int Index = 0; Index < Elems; Index++)
  390. {
  391. double L = LHS.ExtractDouble(Index);
  392. double Addend = Res.ExtractDouble(Index);
  393. Res = AVec.InsertDouble(Res, Index, Addend + L * R);
  394. }
  395. }
  396. return Res;
  397. }
  398. public static AVec Fmov_S(ulong Value, int Elem, int Size)
  399. {
  400. return InsertVec(new AVec(), Elem, Size, Value);
  401. }
  402. public static AVec Fmul_Ve64(AVec LHS, AVec RHS, int SIdx, int Size)
  403. {
  404. return Fmul_Ve(LHS, RHS, SIdx, Size, 2);
  405. }
  406. public static AVec Fmul_Ve128(AVec LHS, AVec RHS, int SIdx, int Size)
  407. {
  408. return Fmul_Ve(LHS, RHS, SIdx, Size, 4);
  409. }
  410. private static AVec Fmul_Ve(AVec LHS, AVec RHS, int SIdx, int Size, int Bytes)
  411. {
  412. AVec Res = new AVec();
  413. int Elems = Bytes >> Size;
  414. if (Size == 0)
  415. {
  416. float R = RHS.ExtractSingle(SIdx);
  417. for (int Index = 0; Index < Elems; Index++)
  418. {
  419. float L = LHS.ExtractSingle(Index);
  420. Res = AVec.InsertSingle(Res, Index, L * R);
  421. }
  422. }
  423. else
  424. {
  425. double R = RHS.ExtractDouble(SIdx);
  426. for (int Index = 0; Index < Elems; Index++)
  427. {
  428. double L = LHS.ExtractDouble(Index);
  429. Res = AVec.InsertDouble(Res, Index, L * R);
  430. }
  431. }
  432. return Res;
  433. }
  434. public static AVec Ins_Gp(AVec Res, ulong Value, int Elem, int Size)
  435. {
  436. return InsertVec(Res, Elem, Size, Value);
  437. }
  438. public static AVec Ins_V(AVec Res, AVec Value, int Src, int Dst, int Size)
  439. {
  440. return InsertVec(Res, Dst, Size, ExtractVec(Value, Src, Size));;
  441. }
  442. public static AVec Orr_Vi64(AVec Res, ulong Imm, int Size)
  443. {
  444. return Orr_Vi(Res, Imm, Size, 8);
  445. }
  446. public static AVec Orr_Vi128(AVec Res, ulong Imm, int Size)
  447. {
  448. return Orr_Vi(Res, Imm, Size, 16);
  449. }
  450. private static AVec Orr_Vi(AVec Res, ulong Imm, int Size, int Bytes)
  451. {
  452. int Elems = Bytes >> Size;
  453. for (int Index = 0; Index < Elems; Index++)
  454. {
  455. ulong Value = ExtractVec(Res, Index, Size);
  456. Res = InsertVec(Res, Index, Size, Value | Imm);
  457. }
  458. return Res;
  459. }
  460. public static AVec Saddw(AVec LHS, AVec RHS, int Size)
  461. {
  462. return Saddw_(LHS, RHS, Size, false);
  463. }
  464. public static AVec Saddw2(AVec LHS, AVec RHS, int Size)
  465. {
  466. return Saddw_(LHS, RHS, Size, true);
  467. }
  468. private static AVec Saddw_(AVec LHS, AVec RHS, int Size, bool High)
  469. {
  470. AVec Res = new AVec();
  471. int Elems = 8 >> Size;
  472. int Part = High ? Elems : 0;
  473. for (int Index = 0; Index < Elems; Index++)
  474. {
  475. long L = ExtractSVec(LHS, Index, Size + 1);
  476. long R = ExtractSVec(RHS, Index + Part, Size);
  477. Res = InsertSVec(Res, Index, Size + 1, L + R);
  478. }
  479. return Res;
  480. }
  481. public static AVec Sshll(AVec Vector, int Shift, int Size)
  482. {
  483. return Sshll_(Vector, Shift, Size, false);
  484. }
  485. public static AVec Sshll2(AVec Vector, int Shift, int Size)
  486. {
  487. return Sshll_(Vector, Shift, Size, true);
  488. }
  489. private static AVec Sshll_(AVec Vector, int Shift, int Size, bool High)
  490. {
  491. AVec Res = new AVec();
  492. int Elems = 8 >> Size;
  493. int Part = High ? Elems : 0;
  494. for (int Index = 0; Index < Elems; Index++)
  495. {
  496. long Value = ExtractSVec(Vector, Index + Part, Size);
  497. Res = InsertSVec(Res, Index, Size + 1, Value << Shift);
  498. }
  499. return Res;
  500. }
  501. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  502. {
  503. return Tbl(Vector, 8, Tb0);
  504. }
  505. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  506. {
  507. return Tbl(Vector, 16, Tb0);
  508. }
  509. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  510. {
  511. return Tbl(Vector, 8, Tb0, Tb1);
  512. }
  513. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  514. {
  515. return Tbl(Vector, 16, Tb0, Tb1);
  516. }
  517. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  518. {
  519. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  520. }
  521. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  522. {
  523. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  524. }
  525. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  526. {
  527. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  528. }
  529. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  530. {
  531. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  532. }
  533. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  534. {
  535. AVec Res = new AVec();
  536. byte[] Table = new byte[Tb.Length * 16];
  537. for (int Index = 0; Index < Tb.Length; Index++)
  538. for (int Index2 = 0; Index2 < 16; Index2++)
  539. {
  540. Table[Index * 16 + Index2] = (byte)ExtractVec(Tb[Index], Index2, 0);
  541. }
  542. for (int Index = 0; Index < Bytes; Index++)
  543. {
  544. byte TblIdx = (byte)ExtractVec(Vector, Index, 0);
  545. if (TblIdx < Table.Length)
  546. {
  547. Res = InsertVec(Res, Index, 0, Table[TblIdx]);
  548. }
  549. }
  550. return Res;
  551. }
  552. public static AVec Uaddlv64(AVec Vector, int Size)
  553. {
  554. return Uaddlv(Vector, Size, 8);
  555. }
  556. public static AVec Uaddlv128(AVec Vector, int Size)
  557. {
  558. return Uaddlv(Vector, Size, 16);
  559. }
  560. private static AVec Uaddlv(AVec Vector, int Size, int Bytes)
  561. {
  562. int Elems = Bytes >> Size;
  563. ulong Sum = 0;
  564. for (int Index = 0; Index < Elems; Index++)
  565. {
  566. Sum += ExtractVec(Vector, Index, Size);
  567. }
  568. return InsertVec(new AVec(), 0, 3, Sum);
  569. }
  570. public static AVec Uaddw(AVec LHS, AVec RHS, int Size)
  571. {
  572. return Uaddw_(LHS, RHS, Size, false);
  573. }
  574. public static AVec Uaddw2(AVec LHS, AVec RHS, int Size)
  575. {
  576. return Uaddw_(LHS, RHS, Size, true);
  577. }
  578. private static AVec Uaddw_(AVec LHS, AVec RHS, int Size, bool High)
  579. {
  580. AVec Res = new AVec();
  581. int Elems = 8 >> Size;
  582. int Part = High ? Elems : 0;
  583. for (int Index = 0; Index < Elems; Index++)
  584. {
  585. ulong L = ExtractVec(LHS, Index, Size + 1);
  586. ulong R = ExtractVec(RHS, Index + Part, Size);
  587. Res = InsertVec(Res, Index, Size + 1, L + R);
  588. }
  589. return Res;
  590. }
  591. public static AVec Ucvtf_V_F(AVec Vector)
  592. {
  593. return new AVec()
  594. {
  595. S0 = (uint)Vector.W0,
  596. S1 = (uint)Vector.W1,
  597. S2 = (uint)Vector.W2,
  598. S3 = (uint)Vector.W3
  599. };
  600. }
  601. public static AVec Ucvtf_V_D(AVec Vector)
  602. {
  603. return new AVec()
  604. {
  605. D0 = (ulong)Vector.X0,
  606. D1 = (ulong)Vector.X1
  607. };
  608. }
  609. public static AVec Ushll(AVec Vector, int Shift, int Size)
  610. {
  611. return Ushll_(Vector, Shift, Size, false);
  612. }
  613. public static AVec Ushll2(AVec Vector, int Shift, int Size)
  614. {
  615. return Ushll_(Vector, Shift, Size, true);
  616. }
  617. private static AVec Ushll_(AVec Vector, int Shift, int Size, bool High)
  618. {
  619. AVec Res = new AVec();
  620. int Elems = 8 >> Size;
  621. int Part = High ? Elems : 0;
  622. for (int Index = 0; Index < Elems; Index++)
  623. {
  624. ulong Value = ExtractVec(Vector, Index + Part, Size);
  625. Res = InsertVec(Res, Index, Size + 1, Value << Shift);
  626. }
  627. return Res;
  628. }
  629. public static AVec Uzp1_V64(AVec LHS, AVec RHS, int Size)
  630. {
  631. return Uzp(LHS, RHS, Size, 0, 8);
  632. }
  633. public static AVec Uzp1_V128(AVec LHS, AVec RHS, int Size)
  634. {
  635. return Uzp(LHS, RHS, Size, 0, 16);
  636. }
  637. public static AVec Uzp2_V64(AVec LHS, AVec RHS, int Size)
  638. {
  639. return Uzp(LHS, RHS, Size, 1, 8);
  640. }
  641. public static AVec Uzp2_V128(AVec LHS, AVec RHS, int Size)
  642. {
  643. return Uzp(LHS, RHS, Size, 1, 16);
  644. }
  645. private static AVec Uzp(AVec LHS, AVec RHS, int Size, int Part, int Bytes)
  646. {
  647. AVec Res = new AVec();
  648. int Elems = Bytes >> Size;
  649. int Half = Elems >> 1;
  650. for (int Index = 0; Index < Elems; Index++)
  651. {
  652. int Elem = (Index & (Half - 1)) << 1;
  653. ulong Value = Index < Half
  654. ? ExtractVec(LHS, Elem + Part, Size)
  655. : ExtractVec(RHS, Elem + Part, Size);
  656. Res = InsertVec(Res, Index, Size, Value);
  657. }
  658. return Res;
  659. }
  660. public static AVec Xtn(AVec Vector, int Size)
  661. {
  662. return Xtn_(Vector, Size, false);
  663. }
  664. public static AVec Xtn2(AVec Vector, int Size)
  665. {
  666. return Xtn_(Vector, Size, true);
  667. }
  668. private static AVec Xtn_(AVec Vector, int Size, bool High)
  669. {
  670. AVec Res = new AVec();
  671. int Elems = 8 >> Size;
  672. int Part = High ? Elems : 0;
  673. for (int Index = 0; Index < Elems; Index++)
  674. {
  675. ulong Value = ExtractVec(Vector, Index, Size + 1);
  676. Res = InsertVec(Res, Index + Part, Size, Value);
  677. }
  678. return Res;
  679. }
  680. public static ulong ExtractVec(AVec Vector, int Index, int Size)
  681. {
  682. switch (Size)
  683. {
  684. case 0: return Vector.ExtractByte(Index);
  685. case 1: return Vector.ExtractUInt16(Index);
  686. case 2: return Vector.ExtractUInt32(Index);
  687. case 3: return Vector.ExtractUInt64(Index);
  688. }
  689. throw new ArgumentOutOfRangeException(nameof(Size));
  690. }
  691. public static long ExtractSVec(AVec Vector, int Index, int Size)
  692. {
  693. switch (Size)
  694. {
  695. case 0: return (sbyte)Vector.ExtractByte(Index);
  696. case 1: return (short)Vector.ExtractUInt16(Index);
  697. case 2: return (int)Vector.ExtractUInt32(Index);
  698. case 3: return (long)Vector.ExtractUInt64(Index);
  699. }
  700. throw new ArgumentOutOfRangeException(nameof(Size));
  701. }
  702. public static float VectorExtractSingle(AVec Vector, int Index)
  703. {
  704. return Vector.ExtractSingle(Index);
  705. }
  706. public static double VectorExtractDouble(AVec Vector, int Index)
  707. {
  708. return Vector.ExtractDouble(Index);
  709. }
  710. public static AVec VectorInsertSingle(float Value, AVec Vector, int Index)
  711. {
  712. return AVec.InsertSingle(Vector, Index, Value);
  713. }
  714. public static AVec VectorInsertDouble(double Value, AVec Vector, int Index)
  715. {
  716. return AVec.InsertDouble(Vector, Index, Value);
  717. }
  718. public static AVec VectorInsertInt(ulong Value, AVec Vector, int Index, int Size)
  719. {
  720. switch (Size)
  721. {
  722. case 0: return AVec.InsertByte (Vector, Index, (byte)Value);
  723. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  724. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  725. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  726. }
  727. throw new ArgumentOutOfRangeException(nameof(Size));
  728. }
  729. public static AVec InsertVec(AVec Vector, int Index, int Size, ulong Value)
  730. {
  731. switch (Size)
  732. {
  733. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  734. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  735. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  736. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  737. }
  738. throw new ArgumentOutOfRangeException(nameof(Size));
  739. }
  740. public static AVec InsertSVec(AVec Vector, int Index, int Size, long Value)
  741. {
  742. switch (Size)
  743. {
  744. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  745. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  746. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  747. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  748. }
  749. throw new ArgumentOutOfRangeException(nameof(Size));
  750. }
  751. }
  752. }