InstEmitSimdHelper32.cs 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.Translation;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.Instructions.InstEmitSimdHelper;
  9. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  10. namespace ARMeilleure.Instructions
  11. {
  12. using Func1I = Func<Operand, Operand>;
  13. using Func2I = Func<Operand, Operand, Operand>;
  14. using Func3I = Func<Operand, Operand, Operand, Operand>;
  15. static class InstEmitSimdHelper32
  16. {
  17. public static (int, int) GetQuadwordAndSubindex(int index, RegisterSize size)
  18. {
  19. switch (size)
  20. {
  21. case RegisterSize.Simd128:
  22. return (index >> 1, 0);
  23. case RegisterSize.Simd64:
  24. case RegisterSize.Int64:
  25. return (index >> 1, index & 1);
  26. case RegisterSize.Int32:
  27. return (index >> 2, index & 3);
  28. }
  29. throw new ArgumentException("Unrecognized Vector Register Size.");
  30. }
  31. public static Operand ExtractScalar(ArmEmitterContext context, OperandType type, int reg)
  32. {
  33. Debug.Assert(type != OperandType.V128);
  34. if (type == OperandType.FP64 || type == OperandType.I64)
  35. {
  36. // From dreg.
  37. return context.VectorExtract(type, GetVecA32(reg >> 1), reg & 1);
  38. }
  39. else
  40. {
  41. // From sreg.
  42. return context.VectorExtract(type, GetVecA32(reg >> 2), reg & 3);
  43. }
  44. }
  45. public static void InsertScalar(ArmEmitterContext context, int reg, Operand value)
  46. {
  47. Debug.Assert(value.Type != OperandType.V128);
  48. Operand vec, insert;
  49. if (value.Type == OperandType.FP64 || value.Type == OperandType.I64)
  50. {
  51. // From dreg.
  52. vec = GetVecA32(reg >> 1);
  53. insert = context.VectorInsert(vec, value, reg & 1);
  54. }
  55. else
  56. {
  57. // From sreg.
  58. vec = GetVecA32(reg >> 2);
  59. insert = context.VectorInsert(vec, value, reg & 3);
  60. }
  61. context.Copy(vec, insert);
  62. }
  63. public static Operand ExtractElement(ArmEmitterContext context, int reg, int size, bool signed)
  64. {
  65. return EmitVectorExtract32(context, reg >> (4 - size), reg & ((16 >> size) - 1), size, signed);
  66. }
  67. public static void EmitVectorImmUnaryOp32(ArmEmitterContext context, Func1I emit)
  68. {
  69. IOpCode32SimdImm op = (IOpCode32SimdImm)context.CurrOp;
  70. Operand imm = Const(op.Immediate);
  71. int elems = op.Elems;
  72. (int index, int subIndex) = GetQuadwordAndSubindex(op.Vd, op.RegisterSize);
  73. Operand vec = GetVecA32(index);
  74. Operand res = vec;
  75. for (int item = 0; item < elems; item++)
  76. {
  77. res = EmitVectorInsert(context, res, emit(imm), item + subIndex * elems, op.Size);
  78. }
  79. context.Copy(vec, res);
  80. }
  81. public static void EmitScalarUnaryOpF32(ArmEmitterContext context, Func1I emit)
  82. {
  83. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  84. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  85. Operand m = ExtractScalar(context, type, op.Vm);
  86. InsertScalar(context, op.Vd, emit(m));
  87. }
  88. public static void EmitScalarBinaryOpF32(ArmEmitterContext context, Func2I emit)
  89. {
  90. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  91. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  92. Operand n = ExtractScalar(context, type, op.Vn);
  93. Operand m = ExtractScalar(context, type, op.Vm);
  94. InsertScalar(context, op.Vd, emit(n, m));
  95. }
  96. public static void EmitScalarBinaryOpI32(ArmEmitterContext context, Func2I emit)
  97. {
  98. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  99. OperandType type = (op.Size & 1) != 0 ? OperandType.I64 : OperandType.I32;
  100. if (op.Size < 2)
  101. {
  102. throw new NotSupportedException("Cannot perform a scalar SIMD operation on integers smaller than 32 bits.");
  103. }
  104. Operand n = ExtractScalar(context, type, op.Vn);
  105. Operand m = ExtractScalar(context, type, op.Vm);
  106. InsertScalar(context, op.Vd, emit(n, m));
  107. }
  108. public static void EmitScalarTernaryOpF32(ArmEmitterContext context, Func3I emit)
  109. {
  110. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  111. OperandType type = (op.Size & 1) != 0 ? OperandType.FP64 : OperandType.FP32;
  112. Operand a = ExtractScalar(context, type, op.Vd);
  113. Operand n = ExtractScalar(context, type, op.Vn);
  114. Operand m = ExtractScalar(context, type, op.Vm);
  115. InsertScalar(context, op.Vd, emit(a, n, m));
  116. }
  117. public static void EmitVectorUnaryOpF32(ArmEmitterContext context, Func1I emit)
  118. {
  119. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  120. int sizeF = op.Size & 1;
  121. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  122. int elems = op.GetBytesCount() >> sizeF + 2;
  123. Operand res = GetVecA32(op.Qd);
  124. for (int index = 0; index < elems; index++)
  125. {
  126. Operand me = context.VectorExtract(type, GetVecA32(op.Qm), op.Fm + index);
  127. res = context.VectorInsert(res, emit(me), op.Fd + index);
  128. }
  129. context.Copy(GetVecA32(op.Qd), res);
  130. }
  131. public static void EmitVectorBinaryOpF32(ArmEmitterContext context, Func2I emit)
  132. {
  133. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  134. int sizeF = op.Size & 1;
  135. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  136. int elems = op.GetBytesCount() >> (sizeF + 2);
  137. Operand res = GetVecA32(op.Qd);
  138. for (int index = 0; index < elems; index++)
  139. {
  140. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  141. Operand me = context.VectorExtract(type, GetVecA32(op.Qm), op.Fm + index);
  142. res = context.VectorInsert(res, emit(ne, me), op.Fd + index);
  143. }
  144. context.Copy(GetVecA32(op.Qd), res);
  145. }
  146. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Func3I emit)
  147. {
  148. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  149. int sizeF = op.Size & 1;
  150. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  151. int elems = op.GetBytesCount() >> sizeF + 2;
  152. Operand res = GetVecA32(op.Qd);
  153. for (int index = 0; index < elems; index++)
  154. {
  155. Operand de = context.VectorExtract(type, GetVecA32(op.Qd), op.Fd + index);
  156. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  157. Operand me = context.VectorExtract(type, GetVecA32(op.Qm), op.Fm + index);
  158. res = context.VectorInsert(res, emit(de, ne, me), op.Fd + index);
  159. }
  160. context.Copy(GetVecA32(op.Qd), res);
  161. }
  162. // Integer
  163. public static void EmitVectorUnaryOpI32(ArmEmitterContext context, Func1I emit, bool signed)
  164. {
  165. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  166. Operand res = GetVecA32(op.Qd);
  167. int elems = op.GetBytesCount() >> op.Size;
  168. for (int index = 0; index < elems; index++)
  169. {
  170. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  171. res = EmitVectorInsert(context, res, emit(me), op.Id + index, op.Size);
  172. }
  173. context.Copy(GetVecA32(op.Qd), res);
  174. }
  175. public static void EmitVectorBinaryOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  176. {
  177. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  178. Operand res = GetVecA32(op.Qd);
  179. int elems = op.GetBytesCount() >> op.Size;
  180. for (int index = 0; index < elems; index++)
  181. {
  182. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  183. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  184. res = EmitVectorInsert(context, res, emit(ne, me), op.Id + index, op.Size);
  185. }
  186. context.Copy(GetVecA32(op.Qd), res);
  187. }
  188. public static void EmitVectorBinaryLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  189. {
  190. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  191. Operand res = context.VectorZero();
  192. int elems = op.GetBytesCount() >> op.Size;
  193. for (int index = 0; index < elems; index++)
  194. {
  195. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  196. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  197. if (op.Size == 2)
  198. {
  199. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  200. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  201. }
  202. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  203. }
  204. context.Copy(GetVecA32(op.Qd), res);
  205. }
  206. public static void EmitVectorBinaryWideOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  207. {
  208. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  209. Operand res = context.VectorZero();
  210. int elems = op.GetBytesCount() >> op.Size;
  211. for (int index = 0; index < elems; index++)
  212. {
  213. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size + 1, signed);
  214. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  215. if (op.Size == 2)
  216. {
  217. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  218. }
  219. res = EmitVectorInsert(context, res, emit(ne, me), index, op.Size + 1);
  220. }
  221. context.Copy(GetVecA32(op.Qd), res);
  222. }
  223. public static void EmitVectorImmBinaryQdQmOpZx32(ArmEmitterContext context, Func2I emit)
  224. {
  225. EmitVectorImmBinaryQdQmOpI32(context, emit, false);
  226. }
  227. public static void EmitVectorImmBinaryQdQmOpSx32(ArmEmitterContext context, Func2I emit)
  228. {
  229. EmitVectorImmBinaryQdQmOpI32(context, emit, true);
  230. }
  231. public static void EmitVectorImmBinaryQdQmOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  232. {
  233. OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
  234. Operand res = GetVecA32(op.Qd);
  235. int elems = op.GetBytesCount() >> op.Size;
  236. for (int index = 0; index < elems; index++)
  237. {
  238. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  239. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  240. res = EmitVectorInsert(context, res, emit(de, me), op.Id + index, op.Size);
  241. }
  242. context.Copy(GetVecA32(op.Qd), res);
  243. }
  244. public static void EmitVectorTernaryLongOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  245. {
  246. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  247. Operand res = context.VectorZero();
  248. int elems = op.GetBytesCount() >> op.Size;
  249. for (int index = 0; index < elems; index++)
  250. {
  251. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size + 1, signed);
  252. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  253. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  254. if (op.Size == 2)
  255. {
  256. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  257. me = signed ? context.SignExtend32(OperandType.I64, me) : context.ZeroExtend32(OperandType.I64, me);
  258. }
  259. res = EmitVectorInsert(context, res, emit(de, ne, me), index, op.Size + 1);
  260. }
  261. context.Copy(GetVecA32(op.Qd), res);
  262. }
  263. public static void EmitVectorTernaryOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  264. {
  265. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  266. Operand res = GetVecA32(op.Qd);
  267. int elems = op.GetBytesCount() >> op.Size;
  268. for (int index = 0; index < elems; index++)
  269. {
  270. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  271. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  272. Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, signed);
  273. res = EmitVectorInsert(context, res, emit(de, ne, me), op.Id + index, op.Size);
  274. }
  275. context.Copy(GetVecA32(op.Qd), res);
  276. }
  277. public static void EmitVectorUnaryOpSx32(ArmEmitterContext context, Func1I emit)
  278. {
  279. EmitVectorUnaryOpI32(context, emit, true);
  280. }
  281. public static void EmitVectorBinaryOpSx32(ArmEmitterContext context, Func2I emit)
  282. {
  283. EmitVectorBinaryOpI32(context, emit, true);
  284. }
  285. public static void EmitVectorTernaryOpSx32(ArmEmitterContext context, Func3I emit)
  286. {
  287. EmitVectorTernaryOpI32(context, emit, true);
  288. }
  289. public static void EmitVectorUnaryOpZx32(ArmEmitterContext context, Func1I emit)
  290. {
  291. EmitVectorUnaryOpI32(context, emit, false);
  292. }
  293. public static void EmitVectorBinaryOpZx32(ArmEmitterContext context, Func2I emit)
  294. {
  295. EmitVectorBinaryOpI32(context, emit, false);
  296. }
  297. public static void EmitVectorTernaryOpZx32(ArmEmitterContext context, Func3I emit)
  298. {
  299. EmitVectorTernaryOpI32(context, emit, false);
  300. }
  301. // Vector by scalar
  302. public static void EmitVectorByScalarOpF32(ArmEmitterContext context, Func2I emit)
  303. {
  304. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  305. int sizeF = op.Size & 1;
  306. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  307. int elems = op.GetBytesCount() >> sizeF + 2;
  308. Operand m = ExtractScalar(context, type, op.Vm);
  309. Operand res = GetVecA32(op.Qd);
  310. for (int index = 0; index < elems; index++)
  311. {
  312. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  313. res = context.VectorInsert(res, emit(ne, m), op.Fd + index);
  314. }
  315. context.Copy(GetVecA32(op.Qd), res);
  316. }
  317. public static void EmitVectorByScalarOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  318. {
  319. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  320. Operand m = ExtractElement(context, op.Vm, op.Size, signed);
  321. Operand res = GetVecA32(op.Qd);
  322. int elems = op.GetBytesCount() >> op.Size;
  323. for (int index = 0; index < elems; index++)
  324. {
  325. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  326. res = EmitVectorInsert(context, res, emit(ne, m), op.Id + index, op.Size);
  327. }
  328. context.Copy(GetVecA32(op.Qd), res);
  329. }
  330. public static void EmitVectorByScalarLongOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  331. {
  332. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  333. Operand m = ExtractElement(context, op.Vm, op.Size, signed);
  334. if (op.Size == 2)
  335. {
  336. m = signed ? context.SignExtend32(OperandType.I64, m) : context.ZeroExtend32(OperandType.I64, m);
  337. }
  338. Operand res = context.VectorZero();
  339. int elems = op.GetBytesCount() >> op.Size;
  340. for (int index = 0; index < elems; index++)
  341. {
  342. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  343. if (op.Size == 2)
  344. {
  345. ne = signed ? context.SignExtend32(OperandType.I64, ne) : context.ZeroExtend32(OperandType.I64, ne);
  346. }
  347. res = EmitVectorInsert(context, res, emit(ne, m), index, op.Size + 1);
  348. }
  349. context.Copy(GetVecA32(op.Qd), res);
  350. }
  351. public static void EmitVectorsByScalarOpF32(ArmEmitterContext context, Func3I emit)
  352. {
  353. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  354. int sizeF = op.Size & 1;
  355. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  356. int elems = op.GetBytesCount() >> sizeF + 2;
  357. Operand m = ExtractScalar(context, type, op.Vm);
  358. Operand res = GetVecA32(op.Qd);
  359. for (int index = 0; index < elems; index++)
  360. {
  361. Operand de = context.VectorExtract(type, GetVecA32(op.Qd), op.Fd + index);
  362. Operand ne = context.VectorExtract(type, GetVecA32(op.Qn), op.Fn + index);
  363. res = context.VectorInsert(res, emit(de, ne, m), op.Fd + index);
  364. }
  365. context.Copy(GetVecA32(op.Qd), res);
  366. }
  367. public static void EmitVectorsByScalarOpI32(ArmEmitterContext context, Func3I emit, bool signed)
  368. {
  369. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  370. Operand m = EmitVectorExtract32(context, op.Vm >> (4 - op.Size), op.Vm & ((1 << (4 - op.Size)) - 1), op.Size, signed);
  371. Operand res = GetVecA32(op.Qd);
  372. int elems = op.GetBytesCount() >> op.Size;
  373. for (int index = 0; index < elems; index++)
  374. {
  375. Operand de = EmitVectorExtract32(context, op.Qd, op.Id + index, op.Size, signed);
  376. Operand ne = EmitVectorExtract32(context, op.Qn, op.In + index, op.Size, signed);
  377. res = EmitVectorInsert(context, res, emit(de, ne, m), op.Id + index, op.Size);
  378. }
  379. context.Copy(GetVecA32(op.Qd), res);
  380. }
  381. // Pairwise
  382. public static void EmitVectorPairwiseOpF32(ArmEmitterContext context, Func2I emit)
  383. {
  384. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  385. int sizeF = op.Size & 1;
  386. OperandType type = sizeF != 0 ? OperandType.FP64 : OperandType.FP32;
  387. int elems = op.GetBytesCount() >> (sizeF + 2);
  388. int pairs = elems >> 1;
  389. Operand res = GetVecA32(op.Qd);
  390. Operand mvec = GetVecA32(op.Qm);
  391. Operand nvec = GetVecA32(op.Qn);
  392. for (int index = 0; index < pairs; index++)
  393. {
  394. int pairIndex = index << 1;
  395. Operand n1 = context.VectorExtract(type, nvec, op.Fn + pairIndex);
  396. Operand n2 = context.VectorExtract(type, nvec, op.Fn + pairIndex + 1);
  397. res = context.VectorInsert(res, emit(n1, n2), op.Fd + index);
  398. Operand m1 = context.VectorExtract(type, mvec, op.Fm + pairIndex);
  399. Operand m2 = context.VectorExtract(type, mvec, op.Fm + pairIndex + 1);
  400. res = context.VectorInsert(res, emit(m1, m2), op.Fd + index + pairs);
  401. }
  402. context.Copy(GetVecA32(op.Qd), res);
  403. }
  404. public static void EmitVectorPairwiseOpI32(ArmEmitterContext context, Func2I emit, bool signed)
  405. {
  406. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  407. int elems = op.GetBytesCount() >> op.Size;
  408. int pairs = elems >> 1;
  409. Operand res = GetVecA32(op.Qd);
  410. for (int index = 0; index < pairs; index++)
  411. {
  412. int pairIndex = index << 1;
  413. Operand n1 = EmitVectorExtract32(context, op.Qn, op.In + pairIndex, op.Size, signed);
  414. Operand n2 = EmitVectorExtract32(context, op.Qn, op.In + pairIndex + 1, op.Size, signed);
  415. Operand m1 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex, op.Size, signed);
  416. Operand m2 = EmitVectorExtract32(context, op.Qm, op.Im + pairIndex + 1, op.Size, signed);
  417. res = EmitVectorInsert(context, res, emit(n1, n2), op.Id + index, op.Size);
  418. res = EmitVectorInsert(context, res, emit(m1, m2), op.Id + index + pairs, op.Size);
  419. }
  420. context.Copy(GetVecA32(op.Qd), res);
  421. }
  422. // Narrow
  423. public static void EmitVectorUnaryNarrowOp32(ArmEmitterContext context, Func1I emit, bool signed = false)
  424. {
  425. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  426. int elems = 8 >> op.Size; // Size contains the target element size. (for when it becomes a doubleword)
  427. Operand res = GetVecA32(op.Qd);
  428. int id = (op.Vd & 1) << (3 - op.Size); // Target doubleword base.
  429. for (int index = 0; index < elems; index++)
  430. {
  431. Operand m = EmitVectorExtract32(context, op.Qm, index, op.Size + 1, signed);
  432. res = EmitVectorInsert(context, res, emit(m), id + index, op.Size);
  433. }
  434. context.Copy(GetVecA32(op.Qd), res);
  435. }
  436. // Intrinsic Helpers
  437. public static Operand EmitMoveDoubleWordToSide(ArmEmitterContext context, Operand input, int originalV, int targetV)
  438. {
  439. Debug.Assert(input.Type == OperandType.V128);
  440. int originalSide = originalV & 1;
  441. int targetSide = targetV & 1;
  442. if (originalSide == targetSide)
  443. {
  444. return input;
  445. }
  446. if (targetSide == 1)
  447. {
  448. return context.AddIntrinsic(Intrinsic.X86Movlhps, input, input); // Low to high.
  449. }
  450. else
  451. {
  452. return context.AddIntrinsic(Intrinsic.X86Movhlps, input, input); // High to low.
  453. }
  454. }
  455. public static Operand EmitDoubleWordInsert(ArmEmitterContext context, Operand target, Operand value, int targetV)
  456. {
  457. Debug.Assert(target.Type == OperandType.V128 && value.Type == OperandType.V128);
  458. int targetSide = targetV & 1;
  459. int shuffleMask = 2;
  460. if (targetSide == 1)
  461. {
  462. return context.AddIntrinsic(Intrinsic.X86Shufpd, target, value, Const(shuffleMask));
  463. }
  464. else
  465. {
  466. return context.AddIntrinsic(Intrinsic.X86Shufpd, value, target, Const(shuffleMask));
  467. }
  468. }
  469. public static Operand EmitScalarInsert(ArmEmitterContext context, Operand target, Operand value, int reg, bool doubleWidth)
  470. {
  471. Debug.Assert(target.Type == OperandType.V128 && value.Type == OperandType.V128);
  472. // Insert from index 0 in value to index in target.
  473. int index = reg & (doubleWidth ? 1 : 3);
  474. if (doubleWidth)
  475. {
  476. if (index == 1)
  477. {
  478. return context.AddIntrinsic(Intrinsic.X86Movlhps, target, value); // Low to high.
  479. }
  480. else
  481. {
  482. return context.AddIntrinsic(Intrinsic.X86Shufpd, value, target, Const(2)); // Low to low, keep high from original.
  483. }
  484. }
  485. else
  486. {
  487. if (Optimizations.UseSse41)
  488. {
  489. return context.AddIntrinsic(Intrinsic.X86Insertps, target, value, Const(index << 4));
  490. }
  491. else
  492. {
  493. target = EmitSwapScalar(context, target, index, doubleWidth); // Swap value to replace into element 0.
  494. target = context.AddIntrinsic(Intrinsic.X86Movss, target, value); // Move the value into element 0 of the vector.
  495. return EmitSwapScalar(context, target, index, doubleWidth); // Swap new value back to the correct index.
  496. }
  497. }
  498. }
  499. public static Operand EmitSwapScalar(ArmEmitterContext context, Operand target, int reg, bool doubleWidth)
  500. {
  501. // Index into 0, 0 into index. This swap happens at the start of an A32 scalar op if required.
  502. int index = reg & (doubleWidth ? 1 : 3);
  503. if (index == 0) return target;
  504. if (doubleWidth)
  505. {
  506. int shuffleMask = 1; // Swap top and bottom. (b0 = 1, b1 = 0)
  507. return context.AddIntrinsic(Intrinsic.X86Shufpd, target, target, Const(shuffleMask));
  508. }
  509. else
  510. {
  511. int shuffleMask = (3 << 6) | (2 << 4) | (1 << 2) | index; // Swap index and 0. (others remain)
  512. shuffleMask &= ~(3 << (index * 2));
  513. return context.AddIntrinsic(Intrinsic.X86Shufps, target, target, Const(shuffleMask));
  514. }
  515. }
  516. // Vector Operand Templates
  517. public static void EmitVectorUnaryOpSimd32(ArmEmitterContext context, Func1I vectorFunc)
  518. {
  519. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  520. Operand m = GetVecA32(op.Qm);
  521. Operand d = GetVecA32(op.Qd);
  522. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  523. {
  524. m = EmitMoveDoubleWordToSide(context, m, op.Vm, op.Vd);
  525. }
  526. Operand res = vectorFunc(m);
  527. if (!op.Q) // Register insert.
  528. {
  529. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  530. }
  531. context.Copy(d, res);
  532. }
  533. public static void EmitVectorUnaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  534. {
  535. OpCode32Simd op = (OpCode32Simd)context.CurrOp;
  536. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  537. EmitVectorUnaryOpSimd32(context, (m) => context.AddIntrinsic(inst, m));
  538. }
  539. public static void EmitVectorBinaryOpSimd32(ArmEmitterContext context, Func2I vectorFunc, int side = -1)
  540. {
  541. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  542. Operand n = GetVecA32(op.Qn);
  543. Operand m = GetVecA32(op.Qm);
  544. Operand d = GetVecA32(op.Qd);
  545. if (side == -1)
  546. {
  547. side = op.Vd;
  548. }
  549. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  550. {
  551. n = EmitMoveDoubleWordToSide(context, n, op.Vn, side);
  552. m = EmitMoveDoubleWordToSide(context, m, op.Vm, side);
  553. }
  554. Operand res = vectorFunc(n, m);
  555. if (!op.Q) // Register insert.
  556. {
  557. if (side != op.Vd)
  558. {
  559. res = EmitMoveDoubleWordToSide(context, res, side, op.Vd);
  560. }
  561. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  562. }
  563. context.Copy(d, res);
  564. }
  565. public static void EmitVectorBinaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  566. {
  567. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  568. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  569. EmitVectorBinaryOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  570. }
  571. public static void EmitVectorTernaryOpSimd32(ArmEmitterContext context, Func3I vectorFunc)
  572. {
  573. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  574. Operand n = GetVecA32(op.Qn);
  575. Operand m = GetVecA32(op.Qm);
  576. Operand d = GetVecA32(op.Qd);
  577. Operand initialD = d;
  578. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  579. {
  580. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  581. m = EmitMoveDoubleWordToSide(context, m, op.Vm, op.Vd);
  582. }
  583. Operand res = vectorFunc(d, n, m);
  584. if (!op.Q) // Register insert.
  585. {
  586. res = EmitDoubleWordInsert(context, initialD, res, op.Vd);
  587. }
  588. context.Copy(initialD, res);
  589. }
  590. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  591. {
  592. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  593. Intrinsic inst1 = (op.Size & 1) != 0 ? inst64pt1 : inst32pt1;
  594. Intrinsic inst2 = (op.Size & 1) != 0 ? inst64pt2 : inst32pt2;
  595. EmitVectorTernaryOpSimd32(context, (d, n, m) =>
  596. {
  597. Operand res = context.AddIntrinsic(inst1, n, m);
  598. return res = context.AddIntrinsic(inst2, d, res);
  599. });
  600. }
  601. public static void EmitVectorTernaryOpF32(ArmEmitterContext context, Intrinsic inst32)
  602. {
  603. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  604. Debug.Assert((op.Size & 1) == 0);
  605. EmitVectorTernaryOpSimd32(context, (d, n, m) =>
  606. {
  607. return context.AddIntrinsic(inst32, d, n, m);
  608. });
  609. }
  610. public static void EmitScalarUnaryOpSimd32(ArmEmitterContext context, Func1I scalarFunc)
  611. {
  612. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  613. bool doubleSize = (op.Size & 1) != 0;
  614. int shift = doubleSize ? 1 : 2;
  615. Operand m = GetVecA32(op.Vm >> shift);
  616. Operand d = GetVecA32(op.Vd >> shift);
  617. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  618. Operand res = scalarFunc(m);
  619. // Insert scalar into vector.
  620. res = EmitScalarInsert(context, d, res, op.Vd, doubleSize);
  621. context.Copy(d, res);
  622. }
  623. public static void EmitScalarUnaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  624. {
  625. OpCode32SimdS op = (OpCode32SimdS)context.CurrOp;
  626. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  627. EmitScalarUnaryOpSimd32(context, (m) => (inst == 0) ? m : context.AddIntrinsic(inst, m));
  628. }
  629. public static void EmitScalarBinaryOpSimd32(ArmEmitterContext context, Func2I scalarFunc)
  630. {
  631. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  632. bool doubleSize = (op.Size & 1) != 0;
  633. int shift = doubleSize ? 1 : 2;
  634. Operand n = GetVecA32(op.Vn >> shift);
  635. Operand m = GetVecA32(op.Vm >> shift);
  636. Operand d = GetVecA32(op.Vd >> shift);
  637. n = EmitSwapScalar(context, n, op.Vn, doubleSize);
  638. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  639. Operand res = scalarFunc(n, m);
  640. // Insert scalar into vector.
  641. res = EmitScalarInsert(context, d, res, op.Vd, doubleSize);
  642. context.Copy(d, res);
  643. }
  644. public static void EmitScalarBinaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  645. {
  646. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  647. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  648. EmitScalarBinaryOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  649. }
  650. public static void EmitScalarTernaryOpSimd32(ArmEmitterContext context, Func3I scalarFunc)
  651. {
  652. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  653. bool doubleSize = (op.Size & 1) != 0;
  654. int shift = doubleSize ? 1 : 2;
  655. Operand n = GetVecA32(op.Vn >> shift);
  656. Operand m = GetVecA32(op.Vm >> shift);
  657. Operand d = GetVecA32(op.Vd >> shift);
  658. Operand initialD = d;
  659. n = EmitSwapScalar(context, n, op.Vn, doubleSize);
  660. m = EmitSwapScalar(context, m, op.Vm, doubleSize);
  661. d = EmitSwapScalar(context, d, op.Vd, doubleSize);
  662. Operand res = scalarFunc(d, n, m);
  663. // Insert scalar into vector.
  664. res = EmitScalarInsert(context, initialD, res, op.Vd, doubleSize);
  665. context.Copy(initialD, res);
  666. }
  667. public static void EmitScalarTernaryOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  668. {
  669. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  670. bool doubleSize = (op.Size & 1) != 0;
  671. Intrinsic inst = doubleSize ? inst64 : inst32;
  672. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  673. {
  674. return context.AddIntrinsic(inst, d, n, m);
  675. });
  676. }
  677. public static void EmitScalarTernaryOpF32(
  678. ArmEmitterContext context,
  679. Intrinsic inst32pt1,
  680. Intrinsic inst64pt1,
  681. Intrinsic inst32pt2,
  682. Intrinsic inst64pt2,
  683. bool isNegD = false)
  684. {
  685. OpCode32SimdRegS op = (OpCode32SimdRegS)context.CurrOp;
  686. bool doubleSize = (op.Size & 1) != 0;
  687. Intrinsic inst1 = doubleSize ? inst64pt1 : inst32pt1;
  688. Intrinsic inst2 = doubleSize ? inst64pt2 : inst32pt2;
  689. EmitScalarTernaryOpSimd32(context, (d, n, m) =>
  690. {
  691. Operand res = context.AddIntrinsic(inst1, n, m);
  692. if (isNegD)
  693. {
  694. Operand mask = doubleSize
  695. ? X86GetScalar(context, -0d)
  696. : X86GetScalar(context, -0f);
  697. d = doubleSize
  698. ? context.AddIntrinsic(Intrinsic.X86Xorpd, mask, d)
  699. : context.AddIntrinsic(Intrinsic.X86Xorps, mask, d);
  700. }
  701. return context.AddIntrinsic(inst2, d, res);
  702. });
  703. }
  704. // By Scalar
  705. public static void EmitVectorByScalarOpSimd32(ArmEmitterContext context, Func2I vectorFunc)
  706. {
  707. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  708. Operand n = GetVecA32(op.Qn);
  709. Operand d = GetVecA32(op.Qd);
  710. int index = op.Vm & 3;
  711. int dupeMask = (index << 6) | (index << 4) | (index << 2) | index;
  712. Operand m = GetVecA32(op.Vm >> 2);
  713. m = context.AddIntrinsic(Intrinsic.X86Shufps, m, m, Const(dupeMask));
  714. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  715. {
  716. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  717. }
  718. Operand res = vectorFunc(n, m);
  719. if (!op.Q) // Register insert.
  720. {
  721. res = EmitDoubleWordInsert(context, d, res, op.Vd);
  722. }
  723. context.Copy(d, res);
  724. }
  725. public static void EmitVectorByScalarOpF32(ArmEmitterContext context, Intrinsic inst32, Intrinsic inst64)
  726. {
  727. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  728. Intrinsic inst = (op.Size & 1) != 0 ? inst64 : inst32;
  729. EmitVectorByScalarOpSimd32(context, (n, m) => context.AddIntrinsic(inst, n, m));
  730. }
  731. public static void EmitVectorsByScalarOpSimd32(ArmEmitterContext context, Func3I vectorFunc)
  732. {
  733. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  734. Operand n = GetVecA32(op.Qn);
  735. Operand d = GetVecA32(op.Qd);
  736. Operand initialD = d;
  737. int index = op.Vm & 3;
  738. int dupeMask = (index << 6) | (index << 4) | (index << 2) | index;
  739. Operand m = GetVecA32(op.Vm >> 2);
  740. m = context.AddIntrinsic(Intrinsic.X86Shufps, m, m, Const(dupeMask));
  741. if (!op.Q) // Register swap: move relevant doubleword to destination side.
  742. {
  743. n = EmitMoveDoubleWordToSide(context, n, op.Vn, op.Vd);
  744. }
  745. Operand res = vectorFunc(d, n, m);
  746. if (!op.Q) // Register insert.
  747. {
  748. res = EmitDoubleWordInsert(context, initialD, res, op.Vd);
  749. }
  750. context.Copy(initialD, res);
  751. }
  752. public static void EmitVectorsByScalarOpF32(ArmEmitterContext context, Intrinsic inst32pt1, Intrinsic inst64pt1, Intrinsic inst32pt2, Intrinsic inst64pt2)
  753. {
  754. OpCode32SimdRegElem op = (OpCode32SimdRegElem)context.CurrOp;
  755. Intrinsic inst1 = (op.Size & 1) != 0 ? inst64pt1 : inst32pt1;
  756. Intrinsic inst2 = (op.Size & 1) != 0 ? inst64pt2 : inst32pt2;
  757. EmitVectorsByScalarOpSimd32(context, (d, n, m) =>
  758. {
  759. Operand res = context.AddIntrinsic(inst1, n, m);
  760. return res = context.AddIntrinsic(inst2, d, res);
  761. });
  762. }
  763. // Pairwise
  764. public static void EmitSse2VectorPairwiseOpF32(ArmEmitterContext context, Intrinsic inst32)
  765. {
  766. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  767. EmitVectorBinaryOpSimd32(context, (n, m) =>
  768. {
  769. Operand unpck = context.AddIntrinsic(Intrinsic.X86Unpcklps, n, m);
  770. Operand part0 = unpck;
  771. Operand part1 = context.AddIntrinsic(Intrinsic.X86Movhlps, unpck, unpck);
  772. return context.AddIntrinsic(inst32, part0, part1);
  773. }, 0);
  774. }
  775. public static void EmitSsse3VectorPairwiseOp32(ArmEmitterContext context, Intrinsic[] inst)
  776. {
  777. OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
  778. EmitVectorBinaryOpSimd32(context, (n, m) =>
  779. {
  780. if (op.RegisterSize == RegisterSize.Simd64)
  781. {
  782. Operand zeroEvenMask = X86GetElements(context, ZeroMask, EvenMasks[op.Size]);
  783. Operand zeroOddMask = X86GetElements(context, ZeroMask, OddMasks[op.Size]);
  784. Operand mN = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m); // m:n
  785. Operand left = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroEvenMask); // 0:even from m:n
  786. Operand right = context.AddIntrinsic(Intrinsic.X86Pshufb, mN, zeroOddMask); // 0:odd from m:n
  787. return context.AddIntrinsic(inst[op.Size], left, right);
  788. }
  789. else if (op.Size < 3)
  790. {
  791. Operand oddEvenMask = X86GetElements(context, OddMasks[op.Size], EvenMasks[op.Size]);
  792. Operand oddEvenN = context.AddIntrinsic(Intrinsic.X86Pshufb, n, oddEvenMask); // odd:even from n
  793. Operand oddEvenM = context.AddIntrinsic(Intrinsic.X86Pshufb, m, oddEvenMask); // odd:even from m
  794. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, oddEvenN, oddEvenM);
  795. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, oddEvenN, oddEvenM);
  796. return context.AddIntrinsic(inst[op.Size], left, right);
  797. }
  798. else
  799. {
  800. Operand left = context.AddIntrinsic(Intrinsic.X86Punpcklqdq, n, m);
  801. Operand right = context.AddIntrinsic(Intrinsic.X86Punpckhqdq, n, m);
  802. return context.AddIntrinsic(inst[3], left, right);
  803. }
  804. }, 0);
  805. }
  806. // Generic Functions
  807. public static Operand EmitSoftFloatCallDefaultFpscr(ArmEmitterContext context, string name, params Operand[] callArgs)
  808. {
  809. IOpCodeSimd op = (IOpCodeSimd)context.CurrOp;
  810. MethodInfo info = (op.Size & 1) == 0
  811. ? typeof(SoftFloat32).GetMethod(name)
  812. : typeof(SoftFloat64).GetMethod(name);
  813. Array.Resize(ref callArgs, callArgs.Length + 1);
  814. callArgs[callArgs.Length - 1] = Const(1);
  815. return context.Call(info, callArgs);
  816. }
  817. public static Operand EmitVectorExtractSx32(ArmEmitterContext context, int reg, int index, int size)
  818. {
  819. return EmitVectorExtract32(context, reg, index, size, true);
  820. }
  821. public static Operand EmitVectorExtractZx32(ArmEmitterContext context, int reg, int index, int size)
  822. {
  823. return EmitVectorExtract32(context, reg, index, size, false);
  824. }
  825. public static Operand EmitVectorExtract32(ArmEmitterContext context, int reg, int index, int size, bool signed)
  826. {
  827. ThrowIfInvalid(index, size);
  828. Operand res = default;
  829. switch (size)
  830. {
  831. case 0:
  832. res = context.VectorExtract8(GetVec(reg), index);
  833. break;
  834. case 1:
  835. res = context.VectorExtract16(GetVec(reg), index);
  836. break;
  837. case 2:
  838. res = context.VectorExtract(OperandType.I32, GetVec(reg), index);
  839. break;
  840. case 3:
  841. res = context.VectorExtract(OperandType.I64, GetVec(reg), index);
  842. break;
  843. }
  844. if (signed)
  845. {
  846. switch (size)
  847. {
  848. case 0: res = context.SignExtend8(OperandType.I32, res); break;
  849. case 1: res = context.SignExtend16(OperandType.I32, res); break;
  850. }
  851. }
  852. else
  853. {
  854. switch (size)
  855. {
  856. case 0: res = context.ZeroExtend8(OperandType.I32, res); break;
  857. case 1: res = context.ZeroExtend16(OperandType.I32, res); break;
  858. }
  859. }
  860. return res;
  861. }
  862. public static Operand EmitPolynomialMultiply(ArmEmitterContext context, Operand op1, Operand op2, int eSize)
  863. {
  864. Debug.Assert(eSize <= 32);
  865. Operand result = eSize == 32 ? Const(0L) : Const(0);
  866. if (eSize == 32)
  867. {
  868. op1 = context.ZeroExtend32(OperandType.I64, op1);
  869. op2 = context.ZeroExtend32(OperandType.I64, op2);
  870. }
  871. for (int i = 0; i < eSize; i++)
  872. {
  873. Operand mask = context.BitwiseAnd(op1, Const(op1.Type, 1L << i));
  874. result = context.BitwiseExclusiveOr(result, context.Multiply(op2, mask));
  875. }
  876. return result;
  877. }
  878. }
  879. }