ASoftFallback.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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 Sshll(AVec Vector, int Shift, int Size)
  620. {
  621. return Sshll_(Vector, Shift, Size, false);
  622. }
  623. public static AVec Sshll2(AVec Vector, int Shift, int Size)
  624. {
  625. return Sshll_(Vector, Shift, Size, true);
  626. }
  627. private static AVec Sshll_(AVec Vector, int Shift, int Size, bool High)
  628. {
  629. AVec Res = new AVec();
  630. int Elems = 8 >> Size;
  631. int Part = High ? Elems : 0;
  632. for (int Index = 0; Index < Elems; Index++)
  633. {
  634. long Value = ExtractSVec(Vector, Index + Part, Size);
  635. Res = InsertSVec(Res, Index, Size + 1, Value << Shift);
  636. }
  637. return Res;
  638. }
  639. public static AVec Tbl1_V64(AVec Vector, AVec Tb0)
  640. {
  641. return Tbl(Vector, 8, Tb0);
  642. }
  643. public static AVec Tbl1_V128(AVec Vector, AVec Tb0)
  644. {
  645. return Tbl(Vector, 16, Tb0);
  646. }
  647. public static AVec Tbl2_V64(AVec Vector, AVec Tb0, AVec Tb1)
  648. {
  649. return Tbl(Vector, 8, Tb0, Tb1);
  650. }
  651. public static AVec Tbl2_V128(AVec Vector, AVec Tb0, AVec Tb1)
  652. {
  653. return Tbl(Vector, 16, Tb0, Tb1);
  654. }
  655. public static AVec Tbl3_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  656. {
  657. return Tbl(Vector, 8, Tb0, Tb1, Tb2);
  658. }
  659. public static AVec Tbl3_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2)
  660. {
  661. return Tbl(Vector, 16, Tb0, Tb1, Tb2);
  662. }
  663. public static AVec Tbl4_V64(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  664. {
  665. return Tbl(Vector, 8, Tb0, Tb1, Tb2, Tb3);
  666. }
  667. public static AVec Tbl4_V128(AVec Vector, AVec Tb0, AVec Tb1, AVec Tb2, AVec Tb3)
  668. {
  669. return Tbl(Vector, 16, Tb0, Tb1, Tb2, Tb3);
  670. }
  671. private static AVec Tbl(AVec Vector, int Bytes, params AVec[] Tb)
  672. {
  673. AVec Res = new AVec();
  674. byte[] Table = new byte[Tb.Length * 16];
  675. for (int Index = 0; Index < Tb.Length; Index++)
  676. for (int Index2 = 0; Index2 < 16; Index2++)
  677. {
  678. Table[Index * 16 + Index2] = (byte)ExtractVec(Tb[Index], Index2, 0);
  679. }
  680. for (int Index = 0; Index < Bytes; Index++)
  681. {
  682. byte TblIdx = (byte)ExtractVec(Vector, Index, 0);
  683. if (TblIdx < Table.Length)
  684. {
  685. Res = InsertVec(Res, Index, 0, Table[TblIdx]);
  686. }
  687. }
  688. return Res;
  689. }
  690. public static AVec Uaddlv64(AVec Vector, int Size)
  691. {
  692. return Uaddlv(Vector, Size, 8);
  693. }
  694. public static AVec Uaddlv128(AVec Vector, int Size)
  695. {
  696. return Uaddlv(Vector, Size, 16);
  697. }
  698. private static AVec Uaddlv(AVec Vector, int Size, int Bytes)
  699. {
  700. int Elems = Bytes >> Size;
  701. ulong Sum = 0;
  702. for (int Index = 0; Index < Elems; Index++)
  703. {
  704. Sum += ExtractVec(Vector, Index, Size);
  705. }
  706. return InsertVec(new AVec(), 0, 3, Sum);
  707. }
  708. public static AVec Uaddw(AVec LHS, AVec RHS, int Size)
  709. {
  710. return Uaddw_(LHS, RHS, Size, false);
  711. }
  712. public static AVec Uaddw2(AVec LHS, AVec RHS, int Size)
  713. {
  714. return Uaddw_(LHS, RHS, Size, true);
  715. }
  716. private static AVec Uaddw_(AVec LHS, AVec RHS, int Size, bool High)
  717. {
  718. AVec Res = new AVec();
  719. int Elems = 8 >> Size;
  720. int Part = High ? Elems : 0;
  721. for (int Index = 0; Index < Elems; Index++)
  722. {
  723. ulong L = ExtractVec(LHS, Index, Size + 1);
  724. ulong R = ExtractVec(RHS, Index + Part, Size);
  725. Res = InsertVec(Res, Index, Size + 1, L + R);
  726. }
  727. return Res;
  728. }
  729. public static AVec Ucvtf_V_F(AVec Vector)
  730. {
  731. return new AVec()
  732. {
  733. S0 = (uint)Vector.W0,
  734. S1 = (uint)Vector.W1,
  735. S2 = (uint)Vector.W2,
  736. S3 = (uint)Vector.W3
  737. };
  738. }
  739. public static AVec Ucvtf_V_D(AVec Vector)
  740. {
  741. return new AVec()
  742. {
  743. D0 = (ulong)Vector.X0,
  744. D1 = (ulong)Vector.X1
  745. };
  746. }
  747. public static AVec Ushll(AVec Vector, int Shift, int Size)
  748. {
  749. return Ushll_(Vector, Shift, Size, false);
  750. }
  751. public static AVec Ushll2(AVec Vector, int Shift, int Size)
  752. {
  753. return Ushll_(Vector, Shift, Size, true);
  754. }
  755. private static AVec Ushll_(AVec Vector, int Shift, int Size, bool High)
  756. {
  757. AVec Res = new AVec();
  758. int Elems = 8 >> Size;
  759. int Part = High ? Elems : 0;
  760. for (int Index = 0; Index < Elems; Index++)
  761. {
  762. ulong Value = ExtractVec(Vector, Index + Part, Size);
  763. Res = InsertVec(Res, Index, Size + 1, Value << Shift);
  764. }
  765. return Res;
  766. }
  767. public static AVec Ushr64(AVec Vector, int Shift, int Size)
  768. {
  769. return Ushr(Vector, Shift, Size, 8);
  770. }
  771. public static AVec Ushr128(AVec Vector, int Shift, int Size)
  772. {
  773. return Ushr(Vector, Shift, Size, 16);
  774. }
  775. private static AVec Ushr(AVec Vector, int Shift, int Size, int Bytes)
  776. {
  777. AVec Res = new AVec();
  778. int Elems = Bytes >> Size;
  779. for (int Index = 0; Index < Elems; Index++)
  780. {
  781. ulong Value = ExtractVec(Vector, Index, Size);
  782. Res = InsertVec(Res, Index, Size, Value >> Shift);
  783. }
  784. return Res;
  785. }
  786. public static AVec Usra64(AVec Res, AVec Vector, int Shift, int Size)
  787. {
  788. return Usra(Res, Vector, Shift, Size, 8);
  789. }
  790. public static AVec Usra128(AVec Res, AVec Vector, int Shift, int Size)
  791. {
  792. return Usra(Res, Vector, Shift, Size, 16);
  793. }
  794. private static AVec Usra(AVec Res, AVec Vector, int Shift, int Size, int Bytes)
  795. {
  796. int Elems = Bytes >> Size;
  797. for (int Index = 0; Index < Elems; Index++)
  798. {
  799. ulong Value = ExtractVec(Vector, Index, Size);
  800. ulong Addend = ExtractVec(Res, Index, Size);
  801. Res = InsertVec(Res, Index, Size, Addend + (Value >> Shift));
  802. }
  803. return Res;
  804. }
  805. public static AVec Uzp1_V64(AVec LHS, AVec RHS, int Size)
  806. {
  807. return Uzp(LHS, RHS, Size, 0, 8);
  808. }
  809. public static AVec Uzp1_V128(AVec LHS, AVec RHS, int Size)
  810. {
  811. return Uzp(LHS, RHS, Size, 0, 16);
  812. }
  813. public static AVec Uzp2_V64(AVec LHS, AVec RHS, int Size)
  814. {
  815. return Uzp(LHS, RHS, Size, 1, 8);
  816. }
  817. public static AVec Uzp2_V128(AVec LHS, AVec RHS, int Size)
  818. {
  819. return Uzp(LHS, RHS, Size, 1, 16);
  820. }
  821. private static AVec Uzp(AVec LHS, AVec RHS, int Size, int Part, int Bytes)
  822. {
  823. AVec Res = new AVec();
  824. int Elems = Bytes >> Size;
  825. int Half = Elems >> 1;
  826. for (int Index = 0; Index < Elems; Index++)
  827. {
  828. int Elem = (Index & (Half - 1)) << 1;
  829. ulong Value = Index < Half
  830. ? ExtractVec(LHS, Elem + Part, Size)
  831. : ExtractVec(RHS, Elem + Part, Size);
  832. Res = InsertVec(Res, Index, Size, Value);
  833. }
  834. return Res;
  835. }
  836. public static AVec Xtn(AVec Vector, int Size)
  837. {
  838. return Xtn_(Vector, Size, false);
  839. }
  840. public static AVec Xtn2(AVec Vector, int Size)
  841. {
  842. return Xtn_(Vector, Size, true);
  843. }
  844. private static AVec Xtn_(AVec Vector, int Size, bool High)
  845. {
  846. AVec Res = new AVec();
  847. int Elems = 8 >> Size;
  848. int Part = High ? Elems : 0;
  849. for (int Index = 0; Index < Elems; Index++)
  850. {
  851. ulong Value = ExtractVec(Vector, Index, Size + 1);
  852. Res = InsertVec(Res, Index + Part, Size, Value);
  853. }
  854. return Res;
  855. }
  856. public static ulong ExtractVec(AVec Vector, int Index, int Size)
  857. {
  858. switch (Size)
  859. {
  860. case 0: return Vector.ExtractByte(Index);
  861. case 1: return Vector.ExtractUInt16(Index);
  862. case 2: return Vector.ExtractUInt32(Index);
  863. case 3: return Vector.ExtractUInt64(Index);
  864. }
  865. throw new ArgumentOutOfRangeException(nameof(Size));
  866. }
  867. public static long ExtractSVec(AVec Vector, int Index, int Size)
  868. {
  869. switch (Size)
  870. {
  871. case 0: return (sbyte)Vector.ExtractByte(Index);
  872. case 1: return (short)Vector.ExtractUInt16(Index);
  873. case 2: return (int)Vector.ExtractUInt32(Index);
  874. case 3: return (long)Vector.ExtractUInt64(Index);
  875. }
  876. throw new ArgumentOutOfRangeException(nameof(Size));
  877. }
  878. public static AVec InsertVec(AVec Vector, int Index, int Size, ulong Value)
  879. {
  880. switch (Size)
  881. {
  882. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  883. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  884. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  885. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  886. }
  887. throw new ArgumentOutOfRangeException(nameof(Size));
  888. }
  889. public static AVec InsertSVec(AVec Vector, int Index, int Size, long Value)
  890. {
  891. switch (Size)
  892. {
  893. case 0: return AVec.InsertByte(Vector, Index, (byte)Value);
  894. case 1: return AVec.InsertUInt16(Vector, Index, (ushort)Value);
  895. case 2: return AVec.InsertUInt32(Vector, Index, (uint)Value);
  896. case 3: return AVec.InsertUInt64(Vector, Index, (ulong)Value);
  897. }
  898. throw new ArgumentOutOfRangeException(nameof(Size));
  899. }
  900. }
  901. }