InstEmitSimdArithmetic.cs 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299
  1. // https://github.com/intel/ARM_NEON_2_x86_SSE/blob/master/NEON_2_SSE.h
  2. using ChocolArm64.Decoders;
  3. using ChocolArm64.State;
  4. using ChocolArm64.Translation;
  5. using System;
  6. using System.Reflection;
  7. using System.Reflection.Emit;
  8. using System.Runtime.Intrinsics;
  9. using System.Runtime.Intrinsics.X86;
  10. using static ChocolArm64.Instructions.InstEmitSimdHelper;
  11. namespace ChocolArm64.Instructions
  12. {
  13. static partial class InstEmit
  14. {
  15. public static void Abs_S(ILEmitterCtx context)
  16. {
  17. EmitScalarUnaryOpSx(context, () => EmitAbs(context));
  18. }
  19. public static void Abs_V(ILEmitterCtx context)
  20. {
  21. EmitVectorUnaryOpSx(context, () => EmitAbs(context));
  22. }
  23. public static void Add_S(ILEmitterCtx context)
  24. {
  25. EmitScalarBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  26. }
  27. public static void Add_V(ILEmitterCtx context)
  28. {
  29. if (Optimizations.UseSse2)
  30. {
  31. EmitSse2Op(context, nameof(Sse2.Add));
  32. }
  33. else
  34. {
  35. EmitVectorBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  36. }
  37. }
  38. public static void Addhn_V(ILEmitterCtx context)
  39. {
  40. EmitHighNarrow(context, () => context.Emit(OpCodes.Add), round: false);
  41. }
  42. public static void Addp_S(ILEmitterCtx context)
  43. {
  44. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  45. EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  46. EmitVectorExtractZx(context, op.Rn, 1, op.Size);
  47. context.Emit(OpCodes.Add);
  48. EmitScalarSet(context, op.Rd, op.Size);
  49. }
  50. public static void Addp_V(ILEmitterCtx context)
  51. {
  52. EmitVectorPairwiseOpZx(context, () => context.Emit(OpCodes.Add));
  53. }
  54. public static void Addv_V(ILEmitterCtx context)
  55. {
  56. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  57. int bytes = op.GetBitsCount() >> 3;
  58. int elems = bytes >> op.Size;
  59. EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  60. for (int index = 1; index < elems; index++)
  61. {
  62. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  63. context.Emit(OpCodes.Add);
  64. }
  65. EmitScalarSet(context, op.Rd, op.Size);
  66. }
  67. public static void Cls_V(ILEmitterCtx context)
  68. {
  69. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  70. int bytes = op.GetBitsCount() >> 3;
  71. int elems = bytes >> op.Size;
  72. int eSize = 8 << op.Size;
  73. for (int index = 0; index < elems; index++)
  74. {
  75. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  76. context.EmitLdc_I4(eSize);
  77. SoftFallback.EmitCall(context, nameof(SoftFallback.CountLeadingSigns));
  78. EmitVectorInsert(context, op.Rd, index, op.Size);
  79. }
  80. if (op.RegisterSize == RegisterSize.Simd64)
  81. {
  82. EmitVectorZeroUpper(context, op.Rd);
  83. }
  84. }
  85. public static void Clz_V(ILEmitterCtx context)
  86. {
  87. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  88. int bytes = op.GetBitsCount() >> 3;
  89. int elems = bytes >> op.Size;
  90. int eSize = 8 << op.Size;
  91. for (int index = 0; index < elems; index++)
  92. {
  93. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  94. if (Lzcnt.IsSupported && eSize == 32)
  95. {
  96. context.Emit(OpCodes.Conv_U4);
  97. context.EmitCall(typeof(Lzcnt).GetMethod(nameof(Lzcnt.LeadingZeroCount), new Type[] { typeof(uint) }));
  98. context.Emit(OpCodes.Conv_U8);
  99. }
  100. else
  101. {
  102. context.EmitLdc_I4(eSize);
  103. SoftFallback.EmitCall(context, nameof(SoftFallback.CountLeadingZeros));
  104. }
  105. EmitVectorInsert(context, op.Rd, index, op.Size);
  106. }
  107. if (op.RegisterSize == RegisterSize.Simd64)
  108. {
  109. EmitVectorZeroUpper(context, op.Rd);
  110. }
  111. }
  112. public static void Cnt_V(ILEmitterCtx context)
  113. {
  114. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  115. int elems = op.RegisterSize == RegisterSize.Simd128 ? 16 : 8;
  116. for (int index = 0; index < elems; index++)
  117. {
  118. EmitVectorExtractZx(context, op.Rn, index, 0);
  119. if (Popcnt.IsSupported)
  120. {
  121. context.EmitCall(typeof(Popcnt).GetMethod(nameof(Popcnt.PopCount), new Type[] { typeof(ulong) }));
  122. }
  123. else
  124. {
  125. SoftFallback.EmitCall(context, nameof(SoftFallback.CountSetBits8));
  126. }
  127. EmitVectorInsert(context, op.Rd, index, 0);
  128. }
  129. if (op.RegisterSize == RegisterSize.Simd64)
  130. {
  131. EmitVectorZeroUpper(context, op.Rd);
  132. }
  133. }
  134. public static void Fabd_S(ILEmitterCtx context)
  135. {
  136. if (Optimizations.FastFP && Optimizations.UseSse2)
  137. {
  138. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  139. int sizeF = op.Size & 1;
  140. if (sizeF == 0)
  141. {
  142. Type[] typesSsv = new Type[] { typeof(float) };
  143. Type[] typesSubAndNot = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  144. context.EmitLdc_R4(-0f);
  145. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  146. context.EmitLdvec(op.Rn);
  147. context.EmitLdvec(op.Rm);
  148. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesSubAndNot));
  149. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesSubAndNot));
  150. context.EmitStvec(op.Rd);
  151. EmitVectorZero32_128(context, op.Rd);
  152. }
  153. else /* if (sizeF == 1) */
  154. {
  155. Type[] typesSsv = new Type[] { typeof(double) };
  156. Type[] typesSubAndNot = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  157. context.EmitLdc_R8(-0d);
  158. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  159. EmitLdvecWithCastToDouble(context, op.Rn);
  160. EmitLdvecWithCastToDouble(context, op.Rm);
  161. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesSubAndNot));
  162. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesSubAndNot));
  163. EmitStvecWithCastFromDouble(context, op.Rd);
  164. EmitVectorZeroUpper(context, op.Rd);
  165. }
  166. }
  167. else
  168. {
  169. EmitScalarBinaryOpF(context, () =>
  170. {
  171. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  172. EmitUnaryMathCall(context, nameof(Math.Abs));
  173. });
  174. }
  175. }
  176. public static void Fabd_V(ILEmitterCtx context)
  177. {
  178. if (Optimizations.FastFP && Optimizations.UseSse2)
  179. {
  180. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  181. int sizeF = op.Size & 1;
  182. if (sizeF == 0)
  183. {
  184. Type[] typesSav = new Type[] { typeof(float) };
  185. Type[] typesSubAndNot = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  186. context.EmitLdc_R4(-0f);
  187. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  188. context.EmitLdvec(op.Rn);
  189. context.EmitLdvec(op.Rm);
  190. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesSubAndNot));
  191. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesSubAndNot));
  192. context.EmitStvec(op.Rd);
  193. if (op.RegisterSize == RegisterSize.Simd64)
  194. {
  195. EmitVectorZeroUpper(context, op.Rd);
  196. }
  197. }
  198. else /* if (sizeF == 1) */
  199. {
  200. Type[] typesSav = new Type[] { typeof(double) };
  201. Type[] typesSubAndNot = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  202. context.EmitLdc_R8(-0d);
  203. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  204. EmitLdvecWithCastToDouble(context, op.Rn);
  205. EmitLdvecWithCastToDouble(context, op.Rm);
  206. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSubAndNot));
  207. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesSubAndNot));
  208. EmitStvecWithCastFromDouble(context, op.Rd);
  209. }
  210. }
  211. else
  212. {
  213. EmitVectorBinaryOpF(context, () =>
  214. {
  215. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  216. EmitUnaryMathCall(context, nameof(Math.Abs));
  217. });
  218. }
  219. }
  220. public static void Fabs_S(ILEmitterCtx context)
  221. {
  222. if (Optimizations.UseSse2)
  223. {
  224. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  225. if (op.Size == 0)
  226. {
  227. Type[] typesSsv = new Type[] { typeof(float) };
  228. Type[] typesAndNot = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  229. context.EmitLdc_R4(-0f);
  230. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  231. context.EmitLdvec(op.Rn);
  232. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesAndNot));
  233. context.EmitStvec(op.Rd);
  234. EmitVectorZero32_128(context, op.Rd);
  235. }
  236. else /* if (op.Size == 1) */
  237. {
  238. Type[] typesSsv = new Type[] { typeof(double) };
  239. Type[] typesAndNot = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  240. context.EmitLdc_R8(-0d);
  241. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  242. EmitLdvecWithCastToDouble(context, op.Rn);
  243. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndNot));
  244. EmitStvecWithCastFromDouble(context, op.Rd);
  245. EmitVectorZeroUpper(context, op.Rd);
  246. }
  247. }
  248. else
  249. {
  250. EmitScalarUnaryOpF(context, () =>
  251. {
  252. EmitUnaryMathCall(context, nameof(Math.Abs));
  253. });
  254. }
  255. }
  256. public static void Fabs_V(ILEmitterCtx context)
  257. {
  258. if (Optimizations.UseSse2)
  259. {
  260. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  261. int sizeF = op.Size & 1;
  262. if (sizeF == 0)
  263. {
  264. Type[] typesSav = new Type[] { typeof(float) };
  265. Type[] typesAndNot = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  266. context.EmitLdc_R4(-0f);
  267. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  268. context.EmitLdvec(op.Rn);
  269. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AndNot), typesAndNot));
  270. context.EmitStvec(op.Rd);
  271. if (op.RegisterSize == RegisterSize.Simd64)
  272. {
  273. EmitVectorZeroUpper(context, op.Rd);
  274. }
  275. }
  276. else /* if (sizeF == 1) */
  277. {
  278. Type[] typesSav = new Type[] { typeof(double) };
  279. Type[] typesAndNot = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  280. context.EmitLdc_R8(-0d);
  281. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  282. EmitLdvecWithCastToDouble(context, op.Rn);
  283. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AndNot), typesAndNot));
  284. EmitStvecWithCastFromDouble(context, op.Rd);
  285. }
  286. }
  287. else
  288. {
  289. EmitVectorUnaryOpF(context, () =>
  290. {
  291. EmitUnaryMathCall(context, nameof(Math.Abs));
  292. });
  293. }
  294. }
  295. public static void Fadd_S(ILEmitterCtx context)
  296. {
  297. if (Optimizations.FastFP && Optimizations.UseSse2)
  298. {
  299. EmitScalarSseOrSse2OpF(context, nameof(Sse.AddScalar));
  300. }
  301. else
  302. {
  303. EmitScalarBinaryOpF(context, () =>
  304. {
  305. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  306. });
  307. }
  308. }
  309. public static void Fadd_V(ILEmitterCtx context)
  310. {
  311. if (Optimizations.FastFP && Optimizations.UseSse2)
  312. {
  313. EmitVectorSseOrSse2OpF(context, nameof(Sse.Add));
  314. }
  315. else
  316. {
  317. EmitVectorBinaryOpF(context, () =>
  318. {
  319. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  320. });
  321. }
  322. }
  323. public static void Faddp_S(ILEmitterCtx context)
  324. {
  325. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  326. int sizeF = op.Size & 1;
  327. if (Optimizations.FastFP && Optimizations.UseSse3)
  328. {
  329. if (sizeF == 0)
  330. {
  331. Type[] typesAddH = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  332. context.EmitLdvec(op.Rn);
  333. context.Emit(OpCodes.Dup);
  334. context.EmitCall(typeof(Sse3).GetMethod(nameof(Sse3.HorizontalAdd), typesAddH));
  335. context.EmitStvec(op.Rd);
  336. EmitVectorZero32_128(context, op.Rd);
  337. }
  338. else /* if (sizeF == 1) */
  339. {
  340. Type[] typesAddH = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  341. EmitLdvecWithCastToDouble(context, op.Rn);
  342. context.Emit(OpCodes.Dup);
  343. context.EmitCall(typeof(Sse3).GetMethod(nameof(Sse3.HorizontalAdd), typesAddH));
  344. EmitStvecWithCastFromDouble(context, op.Rd);
  345. EmitVectorZeroUpper(context, op.Rd);
  346. }
  347. }
  348. else
  349. {
  350. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  351. EmitVectorExtractF(context, op.Rn, 1, sizeF);
  352. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  353. EmitScalarSetF(context, op.Rd, sizeF);
  354. }
  355. }
  356. public static void Faddp_V(ILEmitterCtx context)
  357. {
  358. if (Optimizations.FastFP && Optimizations.UseSse2)
  359. {
  360. EmitVectorPairwiseSseOrSse2OpF(context, nameof(Sse.Add));
  361. }
  362. else
  363. {
  364. EmitVectorPairwiseOpF(context, () =>
  365. {
  366. EmitSoftFloatCall(context, nameof(SoftFloat32.FPAdd));
  367. });
  368. }
  369. }
  370. public static void Fdiv_S(ILEmitterCtx context)
  371. {
  372. if (Optimizations.FastFP && Optimizations.UseSse2)
  373. {
  374. EmitScalarSseOrSse2OpF(context, nameof(Sse.DivideScalar));
  375. }
  376. else
  377. {
  378. EmitScalarBinaryOpF(context, () =>
  379. {
  380. EmitSoftFloatCall(context, nameof(SoftFloat32.FPDiv));
  381. });
  382. }
  383. }
  384. public static void Fdiv_V(ILEmitterCtx context)
  385. {
  386. if (Optimizations.FastFP && Optimizations.UseSse2)
  387. {
  388. EmitVectorSseOrSse2OpF(context, nameof(Sse.Divide));
  389. }
  390. else
  391. {
  392. EmitVectorBinaryOpF(context, () =>
  393. {
  394. EmitSoftFloatCall(context, nameof(SoftFloat32.FPDiv));
  395. });
  396. }
  397. }
  398. public static void Fmadd_S(ILEmitterCtx context) // Fused.
  399. {
  400. if (Optimizations.FastFP && Optimizations.UseSse2)
  401. {
  402. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  403. if (op.Size == 0)
  404. {
  405. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  406. context.EmitLdvec(op.Ra);
  407. context.EmitLdvec(op.Rn);
  408. context.EmitLdvec(op.Rm);
  409. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulAdd));
  410. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.AddScalar), typesMulAdd));
  411. context.EmitStvec(op.Rd);
  412. EmitVectorZero32_128(context, op.Rd);
  413. }
  414. else /* if (op.Size == 1) */
  415. {
  416. Type[] typesMulAdd = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  417. EmitLdvecWithCastToDouble(context, op.Ra);
  418. EmitLdvecWithCastToDouble(context, op.Rn);
  419. EmitLdvecWithCastToDouble(context, op.Rm);
  420. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulAdd));
  421. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.AddScalar), typesMulAdd));
  422. EmitStvecWithCastFromDouble(context, op.Rd);
  423. EmitVectorZeroUpper(context, op.Rd);
  424. }
  425. }
  426. else
  427. {
  428. EmitScalarTernaryRaOpF(context, () =>
  429. {
  430. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd));
  431. });
  432. }
  433. }
  434. public static void Fmax_S(ILEmitterCtx context)
  435. {
  436. if (Optimizations.FastFP && Optimizations.UseSse2)
  437. {
  438. EmitScalarSseOrSse2OpF(context, nameof(Sse.MaxScalar));
  439. }
  440. else
  441. {
  442. EmitScalarBinaryOpF(context, () =>
  443. {
  444. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMax));
  445. });
  446. }
  447. }
  448. public static void Fmax_V(ILEmitterCtx context)
  449. {
  450. if (Optimizations.FastFP && Optimizations.UseSse2)
  451. {
  452. EmitVectorSseOrSse2OpF(context, nameof(Sse.Max));
  453. }
  454. else
  455. {
  456. EmitVectorBinaryOpF(context, () =>
  457. {
  458. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMax));
  459. });
  460. }
  461. }
  462. public static void Fmaxnm_S(ILEmitterCtx context)
  463. {
  464. EmitScalarBinaryOpF(context, () =>
  465. {
  466. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMaxNum));
  467. });
  468. }
  469. public static void Fmaxnm_V(ILEmitterCtx context)
  470. {
  471. EmitVectorBinaryOpF(context, () =>
  472. {
  473. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMaxNum));
  474. });
  475. }
  476. public static void Fmaxp_V(ILEmitterCtx context)
  477. {
  478. if (Optimizations.FastFP && Optimizations.UseSse2)
  479. {
  480. EmitVectorPairwiseSseOrSse2OpF(context, nameof(Sse.Max));
  481. }
  482. else
  483. {
  484. EmitVectorPairwiseOpF(context, () =>
  485. {
  486. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMax));
  487. });
  488. }
  489. }
  490. public static void Fmin_S(ILEmitterCtx context)
  491. {
  492. if (Optimizations.FastFP && Optimizations.UseSse2)
  493. {
  494. EmitScalarSseOrSse2OpF(context, nameof(Sse.MinScalar));
  495. }
  496. else
  497. {
  498. EmitScalarBinaryOpF(context, () =>
  499. {
  500. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMin));
  501. });
  502. }
  503. }
  504. public static void Fmin_V(ILEmitterCtx context)
  505. {
  506. if (Optimizations.FastFP && Optimizations.UseSse2)
  507. {
  508. EmitVectorSseOrSse2OpF(context, nameof(Sse.Min));
  509. }
  510. else
  511. {
  512. EmitVectorBinaryOpF(context, () =>
  513. {
  514. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMin));
  515. });
  516. }
  517. }
  518. public static void Fminnm_S(ILEmitterCtx context)
  519. {
  520. EmitScalarBinaryOpF(context, () =>
  521. {
  522. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMinNum));
  523. });
  524. }
  525. public static void Fminnm_V(ILEmitterCtx context)
  526. {
  527. EmitVectorBinaryOpF(context, () =>
  528. {
  529. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMinNum));
  530. });
  531. }
  532. public static void Fminp_V(ILEmitterCtx context)
  533. {
  534. if (Optimizations.FastFP && Optimizations.UseSse2)
  535. {
  536. EmitVectorPairwiseSseOrSse2OpF(context, nameof(Sse.Min));
  537. }
  538. else
  539. {
  540. EmitVectorPairwiseOpF(context, () =>
  541. {
  542. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMin));
  543. });
  544. }
  545. }
  546. public static void Fmla_Se(ILEmitterCtx context)
  547. {
  548. EmitScalarTernaryOpByElemF(context, () =>
  549. {
  550. context.Emit(OpCodes.Mul);
  551. context.Emit(OpCodes.Add);
  552. });
  553. }
  554. public static void Fmla_V(ILEmitterCtx context) // Fused.
  555. {
  556. if (Optimizations.FastFP && Optimizations.UseSse2)
  557. {
  558. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  559. int sizeF = op.Size & 1;
  560. if (sizeF == 0)
  561. {
  562. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  563. context.EmitLdvec(op.Rd);
  564. context.EmitLdvec(op.Rn);
  565. context.EmitLdvec(op.Rm);
  566. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
  567. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Add), typesMulAdd));
  568. context.EmitStvec(op.Rd);
  569. if (op.RegisterSize == RegisterSize.Simd64)
  570. {
  571. EmitVectorZeroUpper(context, op.Rd);
  572. }
  573. }
  574. else /* if (sizeF == 1) */
  575. {
  576. Type[] typesMulAdd = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  577. EmitLdvecWithCastToDouble(context, op.Rd);
  578. EmitLdvecWithCastToDouble(context, op.Rn);
  579. EmitLdvecWithCastToDouble(context, op.Rm);
  580. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulAdd));
  581. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  582. EmitStvecWithCastFromDouble(context, op.Rd);
  583. }
  584. }
  585. else
  586. {
  587. EmitVectorTernaryOpF(context, () =>
  588. {
  589. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd));
  590. });
  591. }
  592. }
  593. public static void Fmla_Ve(ILEmitterCtx context) // Fused.
  594. {
  595. if (Optimizations.FastFP && Optimizations.UseSse2)
  596. {
  597. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  598. int sizeF = op.Size & 1;
  599. if (sizeF == 0)
  600. {
  601. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  602. Type[] typesMulAdd = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  603. context.EmitLdvec(op.Rd);
  604. context.EmitLdvec(op.Rn);
  605. context.EmitLdvec(op.Rm);
  606. context.Emit(OpCodes.Dup);
  607. context.EmitLdc_I4(op.Index | op.Index << 2 | op.Index << 4 | op.Index << 6);
  608. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  609. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulAdd));
  610. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Add), typesMulAdd));
  611. context.EmitStvec(op.Rd);
  612. if (op.RegisterSize == RegisterSize.Simd64)
  613. {
  614. EmitVectorZeroUpper(context, op.Rd);
  615. }
  616. }
  617. else /* if (sizeF == 1) */
  618. {
  619. Type[] typesSfl = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>), typeof(byte) };
  620. Type[] typesMulAdd = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  621. EmitLdvecWithCastToDouble(context, op.Rd);
  622. EmitLdvecWithCastToDouble(context, op.Rn);
  623. EmitLdvecWithCastToDouble(context, op.Rm);
  624. context.Emit(OpCodes.Dup);
  625. context.EmitLdc_I4(op.Index | op.Index << 1);
  626. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Shuffle), typesSfl));
  627. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulAdd));
  628. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  629. EmitStvecWithCastFromDouble(context, op.Rd);
  630. }
  631. }
  632. else
  633. {
  634. EmitVectorTernaryOpByElemF(context, () =>
  635. {
  636. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulAdd));
  637. });
  638. }
  639. }
  640. public static void Fmls_Se(ILEmitterCtx context)
  641. {
  642. EmitScalarTernaryOpByElemF(context, () =>
  643. {
  644. context.Emit(OpCodes.Mul);
  645. context.Emit(OpCodes.Sub);
  646. });
  647. }
  648. public static void Fmls_V(ILEmitterCtx context) // Fused.
  649. {
  650. if (Optimizations.FastFP && Optimizations.UseSse2)
  651. {
  652. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  653. int sizeF = op.Size & 1;
  654. if (sizeF == 0)
  655. {
  656. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  657. context.EmitLdvec(op.Rd);
  658. context.EmitLdvec(op.Rn);
  659. context.EmitLdvec(op.Rm);
  660. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  661. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  662. context.EmitStvec(op.Rd);
  663. if (op.RegisterSize == RegisterSize.Simd64)
  664. {
  665. EmitVectorZeroUpper(context, op.Rd);
  666. }
  667. }
  668. else /* if (sizeF == 1) */
  669. {
  670. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  671. EmitLdvecWithCastToDouble(context, op.Rd);
  672. EmitLdvecWithCastToDouble(context, op.Rn);
  673. EmitLdvecWithCastToDouble(context, op.Rm);
  674. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  675. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  676. EmitStvecWithCastFromDouble(context, op.Rd);
  677. }
  678. }
  679. else
  680. {
  681. EmitVectorTernaryOpF(context, () =>
  682. {
  683. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub));
  684. });
  685. }
  686. }
  687. public static void Fmls_Ve(ILEmitterCtx context) // Fused.
  688. {
  689. if (Optimizations.FastFP && Optimizations.UseSse2)
  690. {
  691. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  692. int sizeF = op.Size & 1;
  693. if (sizeF == 0)
  694. {
  695. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  696. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  697. context.EmitLdvec(op.Rd);
  698. context.EmitLdvec(op.Rn);
  699. context.EmitLdvec(op.Rm);
  700. context.Emit(OpCodes.Dup);
  701. context.EmitLdc_I4(op.Index | op.Index << 2 | op.Index << 4 | op.Index << 6);
  702. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  703. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  704. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  705. context.EmitStvec(op.Rd);
  706. if (op.RegisterSize == RegisterSize.Simd64)
  707. {
  708. EmitVectorZeroUpper(context, op.Rd);
  709. }
  710. }
  711. else /* if (sizeF == 1) */
  712. {
  713. Type[] typesSfl = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>), typeof(byte) };
  714. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  715. EmitLdvecWithCastToDouble(context, op.Rd);
  716. EmitLdvecWithCastToDouble(context, op.Rn);
  717. EmitLdvecWithCastToDouble(context, op.Rm);
  718. context.Emit(OpCodes.Dup);
  719. context.EmitLdc_I4(op.Index | op.Index << 1);
  720. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Shuffle), typesSfl));
  721. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  722. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  723. EmitStvecWithCastFromDouble(context, op.Rd);
  724. }
  725. }
  726. else
  727. {
  728. EmitVectorTernaryOpByElemF(context, () =>
  729. {
  730. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub));
  731. });
  732. }
  733. }
  734. public static void Fmsub_S(ILEmitterCtx context) // Fused.
  735. {
  736. if (Optimizations.FastFP && Optimizations.UseSse2)
  737. {
  738. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  739. if (op.Size == 0)
  740. {
  741. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  742. context.EmitLdvec(op.Ra);
  743. context.EmitLdvec(op.Rn);
  744. context.EmitLdvec(op.Rm);
  745. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  746. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesMulSub));
  747. context.EmitStvec(op.Rd);
  748. EmitVectorZero32_128(context, op.Rd);
  749. }
  750. else /* if (op.Size == 1) */
  751. {
  752. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  753. EmitLdvecWithCastToDouble(context, op.Ra);
  754. EmitLdvecWithCastToDouble(context, op.Rn);
  755. EmitLdvecWithCastToDouble(context, op.Rm);
  756. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  757. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesMulSub));
  758. EmitStvecWithCastFromDouble(context, op.Rd);
  759. EmitVectorZeroUpper(context, op.Rd);
  760. }
  761. }
  762. else
  763. {
  764. EmitScalarTernaryRaOpF(context, () =>
  765. {
  766. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulSub));
  767. });
  768. }
  769. }
  770. public static void Fmul_S(ILEmitterCtx context)
  771. {
  772. if (Optimizations.FastFP && Optimizations.UseSse2)
  773. {
  774. EmitScalarSseOrSse2OpF(context, nameof(Sse.MultiplyScalar));
  775. }
  776. else
  777. {
  778. EmitScalarBinaryOpF(context, () =>
  779. {
  780. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul));
  781. });
  782. }
  783. }
  784. public static void Fmul_Se(ILEmitterCtx context)
  785. {
  786. EmitScalarBinaryOpByElemF(context, () => context.Emit(OpCodes.Mul));
  787. }
  788. public static void Fmul_V(ILEmitterCtx context)
  789. {
  790. if (Optimizations.FastFP && Optimizations.UseSse2)
  791. {
  792. EmitVectorSseOrSse2OpF(context, nameof(Sse.Multiply));
  793. }
  794. else
  795. {
  796. EmitVectorBinaryOpF(context, () =>
  797. {
  798. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul));
  799. });
  800. }
  801. }
  802. public static void Fmul_Ve(ILEmitterCtx context)
  803. {
  804. if (Optimizations.FastFP && Optimizations.UseSse2)
  805. {
  806. OpCodeSimdRegElemF64 op = (OpCodeSimdRegElemF64)context.CurrOp;
  807. int sizeF = op.Size & 1;
  808. if (sizeF == 0)
  809. {
  810. Type[] typesSfl = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>), typeof(byte) };
  811. Type[] typesMul = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  812. context.EmitLdvec(op.Rn);
  813. context.EmitLdvec(op.Rm);
  814. context.Emit(OpCodes.Dup);
  815. context.EmitLdc_I4(op.Index | op.Index << 2 | op.Index << 4 | op.Index << 6);
  816. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Shuffle), typesSfl));
  817. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMul));
  818. context.EmitStvec(op.Rd);
  819. if (op.RegisterSize == RegisterSize.Simd64)
  820. {
  821. EmitVectorZeroUpper(context, op.Rd);
  822. }
  823. }
  824. else /* if (sizeF == 1) */
  825. {
  826. Type[] typesSfl = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>), typeof(byte) };
  827. Type[] typesMul = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  828. EmitLdvecWithCastToDouble(context, op.Rn);
  829. EmitLdvecWithCastToDouble(context, op.Rm);
  830. context.Emit(OpCodes.Dup);
  831. context.EmitLdc_I4(op.Index | op.Index << 1);
  832. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Shuffle), typesSfl));
  833. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMul));
  834. EmitStvecWithCastFromDouble(context, op.Rd);
  835. }
  836. }
  837. else
  838. {
  839. EmitVectorBinaryOpByElemF(context, () =>
  840. {
  841. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMul));
  842. });
  843. }
  844. }
  845. public static void Fmulx_S(ILEmitterCtx context)
  846. {
  847. EmitScalarBinaryOpF(context, () =>
  848. {
  849. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  850. });
  851. }
  852. public static void Fmulx_Se(ILEmitterCtx context)
  853. {
  854. EmitScalarBinaryOpByElemF(context, () =>
  855. {
  856. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  857. });
  858. }
  859. public static void Fmulx_V(ILEmitterCtx context)
  860. {
  861. EmitVectorBinaryOpF(context, () =>
  862. {
  863. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  864. });
  865. }
  866. public static void Fmulx_Ve(ILEmitterCtx context)
  867. {
  868. EmitVectorBinaryOpByElemF(context, () =>
  869. {
  870. EmitSoftFloatCall(context, nameof(SoftFloat32.FPMulX));
  871. });
  872. }
  873. public static void Fneg_S(ILEmitterCtx context)
  874. {
  875. if (Optimizations.UseSse2)
  876. {
  877. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  878. if (op.Size == 0)
  879. {
  880. Type[] typesSsv = new Type[] { typeof(float) };
  881. Type[] typesXor = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  882. context.EmitLdc_R4(-0f);
  883. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  884. context.EmitLdvec(op.Rn);
  885. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), typesXor));
  886. context.EmitStvec(op.Rd);
  887. EmitVectorZero32_128(context, op.Rd);
  888. }
  889. else /* if (op.Size == 1) */
  890. {
  891. Type[] typesSsv = new Type[] { typeof(double) };
  892. Type[] typesXor = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  893. context.EmitLdc_R8(-0d);
  894. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  895. EmitLdvecWithCastToDouble(context, op.Rn);
  896. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesXor));
  897. EmitStvecWithCastFromDouble(context, op.Rd);
  898. EmitVectorZeroUpper(context, op.Rd);
  899. }
  900. }
  901. else
  902. {
  903. EmitScalarUnaryOpF(context, () => context.Emit(OpCodes.Neg));
  904. }
  905. }
  906. public static void Fneg_V(ILEmitterCtx context)
  907. {
  908. if (Optimizations.UseSse2)
  909. {
  910. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  911. int sizeF = op.Size & 1;
  912. if (sizeF == 0)
  913. {
  914. Type[] typesSav = new Type[] { typeof(float) };
  915. Type[] typesXor = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  916. context.EmitLdc_R4(-0f);
  917. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  918. context.EmitLdvec(op.Rn);
  919. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Xor), typesXor));
  920. context.EmitStvec(op.Rd);
  921. if (op.RegisterSize == RegisterSize.Simd64)
  922. {
  923. EmitVectorZeroUpper(context, op.Rd);
  924. }
  925. }
  926. else /* if (sizeF == 1) */
  927. {
  928. Type[] typesSav = new Type[] { typeof(double) };
  929. Type[] typesXor = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  930. context.EmitLdc_R8(-0d);
  931. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  932. EmitLdvecWithCastToDouble(context, op.Rn);
  933. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesXor));
  934. EmitStvecWithCastFromDouble(context, op.Rd);
  935. }
  936. }
  937. else
  938. {
  939. EmitVectorUnaryOpF(context, () => context.Emit(OpCodes.Neg));
  940. }
  941. }
  942. public static void Fnmadd_S(ILEmitterCtx context)
  943. {
  944. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  945. int sizeF = op.Size & 1;
  946. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  947. context.Emit(OpCodes.Neg);
  948. EmitVectorExtractF(context, op.Rm, 0, sizeF);
  949. context.Emit(OpCodes.Mul);
  950. EmitVectorExtractF(context, op.Ra, 0, sizeF);
  951. context.Emit(OpCodes.Sub);
  952. EmitScalarSetF(context, op.Rd, sizeF);
  953. }
  954. public static void Fnmsub_S(ILEmitterCtx context)
  955. {
  956. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  957. int sizeF = op.Size & 1;
  958. EmitVectorExtractF(context, op.Rn, 0, sizeF);
  959. EmitVectorExtractF(context, op.Rm, 0, sizeF);
  960. context.Emit(OpCodes.Mul);
  961. EmitVectorExtractF(context, op.Ra, 0, sizeF);
  962. context.Emit(OpCodes.Sub);
  963. EmitScalarSetF(context, op.Rd, sizeF);
  964. }
  965. public static void Fnmul_S(ILEmitterCtx context)
  966. {
  967. EmitScalarBinaryOpF(context, () =>
  968. {
  969. context.Emit(OpCodes.Mul);
  970. context.Emit(OpCodes.Neg);
  971. });
  972. }
  973. public static void Frecpe_S(ILEmitterCtx context)
  974. {
  975. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  976. int sizeF = op.Size & 1;
  977. if (Optimizations.FastFP && Optimizations.UseSse
  978. && sizeF == 0)
  979. {
  980. EmitScalarSseOrSse2OpF(context, nameof(Sse.ReciprocalScalar));
  981. }
  982. else
  983. {
  984. EmitScalarUnaryOpF(context, () =>
  985. {
  986. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipEstimate));
  987. });
  988. }
  989. }
  990. public static void Frecpe_V(ILEmitterCtx context)
  991. {
  992. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  993. int sizeF = op.Size & 1;
  994. if (Optimizations.FastFP && Optimizations.UseSse
  995. && sizeF == 0)
  996. {
  997. EmitVectorSseOrSse2OpF(context, nameof(Sse.Reciprocal));
  998. }
  999. else
  1000. {
  1001. EmitVectorUnaryOpF(context, () =>
  1002. {
  1003. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipEstimate));
  1004. });
  1005. }
  1006. }
  1007. public static void Frecps_S(ILEmitterCtx context) // Fused.
  1008. {
  1009. if (Optimizations.FastFP && Optimizations.UseSse2)
  1010. {
  1011. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1012. int sizeF = op.Size & 1;
  1013. if (sizeF == 0)
  1014. {
  1015. Type[] typesSsv = new Type[] { typeof(float) };
  1016. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1017. context.EmitLdc_R4(2f);
  1018. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  1019. context.EmitLdvec(op.Rn);
  1020. context.EmitLdvec(op.Rm);
  1021. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  1022. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesMulSub));
  1023. context.EmitStvec(op.Rd);
  1024. EmitVectorZero32_128(context, op.Rd);
  1025. }
  1026. else /* if (sizeF == 1) */
  1027. {
  1028. Type[] typesSsv = new Type[] { typeof(double) };
  1029. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1030. context.EmitLdc_R8(2d);
  1031. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  1032. EmitLdvecWithCastToDouble(context, op.Rn);
  1033. EmitLdvecWithCastToDouble(context, op.Rm);
  1034. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  1035. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesMulSub));
  1036. EmitStvecWithCastFromDouble(context, op.Rd);
  1037. EmitVectorZeroUpper(context, op.Rd);
  1038. }
  1039. }
  1040. else
  1041. {
  1042. EmitScalarBinaryOpF(context, () =>
  1043. {
  1044. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipStepFused));
  1045. });
  1046. }
  1047. }
  1048. public static void Frecps_V(ILEmitterCtx context) // Fused.
  1049. {
  1050. if (Optimizations.FastFP && Optimizations.UseSse2)
  1051. {
  1052. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1053. int sizeF = op.Size & 1;
  1054. if (sizeF == 0)
  1055. {
  1056. Type[] typesSav = new Type[] { typeof(float) };
  1057. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1058. context.EmitLdc_R4(2f);
  1059. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  1060. context.EmitLdvec(op.Rn);
  1061. context.EmitLdvec(op.Rm);
  1062. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  1063. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  1064. context.EmitStvec(op.Rd);
  1065. if (op.RegisterSize == RegisterSize.Simd64)
  1066. {
  1067. EmitVectorZeroUpper(context, op.Rd);
  1068. }
  1069. }
  1070. else /* if (sizeF == 1) */
  1071. {
  1072. Type[] typesSav = new Type[] { typeof(double) };
  1073. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1074. context.EmitLdc_R8(2d);
  1075. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1076. EmitLdvecWithCastToDouble(context, op.Rn);
  1077. EmitLdvecWithCastToDouble(context, op.Rm);
  1078. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  1079. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  1080. EmitStvecWithCastFromDouble(context, op.Rd);
  1081. }
  1082. }
  1083. else
  1084. {
  1085. EmitVectorBinaryOpF(context, () =>
  1086. {
  1087. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecipStepFused));
  1088. });
  1089. }
  1090. }
  1091. public static void Frecpx_S(ILEmitterCtx context)
  1092. {
  1093. EmitScalarUnaryOpF(context, () =>
  1094. {
  1095. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRecpX));
  1096. });
  1097. }
  1098. public static void Frinta_S(ILEmitterCtx context)
  1099. {
  1100. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1101. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  1102. EmitRoundMathCall(context, MidpointRounding.AwayFromZero);
  1103. EmitScalarSetF(context, op.Rd, op.Size);
  1104. }
  1105. public static void Frinta_V(ILEmitterCtx context)
  1106. {
  1107. EmitVectorUnaryOpF(context, () =>
  1108. {
  1109. EmitRoundMathCall(context, MidpointRounding.AwayFromZero);
  1110. });
  1111. }
  1112. public static void Frinti_S(ILEmitterCtx context)
  1113. {
  1114. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1115. EmitScalarUnaryOpF(context, () =>
  1116. {
  1117. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1118. if (op.Size == 0)
  1119. {
  1120. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1121. }
  1122. else if (op.Size == 1)
  1123. {
  1124. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1125. }
  1126. else
  1127. {
  1128. throw new InvalidOperationException();
  1129. }
  1130. });
  1131. }
  1132. public static void Frinti_V(ILEmitterCtx context)
  1133. {
  1134. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1135. int sizeF = op.Size & 1;
  1136. EmitVectorUnaryOpF(context, () =>
  1137. {
  1138. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1139. if (sizeF == 0)
  1140. {
  1141. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1142. }
  1143. else if (sizeF == 1)
  1144. {
  1145. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1146. }
  1147. else
  1148. {
  1149. throw new InvalidOperationException();
  1150. }
  1151. });
  1152. }
  1153. public static void Frintm_S(ILEmitterCtx context)
  1154. {
  1155. EmitScalarUnaryOpF(context, () =>
  1156. {
  1157. EmitUnaryMathCall(context, nameof(Math.Floor));
  1158. });
  1159. }
  1160. public static void Frintm_V(ILEmitterCtx context)
  1161. {
  1162. EmitVectorUnaryOpF(context, () =>
  1163. {
  1164. EmitUnaryMathCall(context, nameof(Math.Floor));
  1165. });
  1166. }
  1167. public static void Frintn_S(ILEmitterCtx context)
  1168. {
  1169. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1170. EmitVectorExtractF(context, op.Rn, 0, op.Size);
  1171. EmitRoundMathCall(context, MidpointRounding.ToEven);
  1172. EmitScalarSetF(context, op.Rd, op.Size);
  1173. }
  1174. public static void Frintn_V(ILEmitterCtx context)
  1175. {
  1176. EmitVectorUnaryOpF(context, () =>
  1177. {
  1178. EmitRoundMathCall(context, MidpointRounding.ToEven);
  1179. });
  1180. }
  1181. public static void Frintp_S(ILEmitterCtx context)
  1182. {
  1183. EmitScalarUnaryOpF(context, () =>
  1184. {
  1185. EmitUnaryMathCall(context, nameof(Math.Ceiling));
  1186. });
  1187. }
  1188. public static void Frintp_V(ILEmitterCtx context)
  1189. {
  1190. EmitVectorUnaryOpF(context, () =>
  1191. {
  1192. EmitUnaryMathCall(context, nameof(Math.Ceiling));
  1193. });
  1194. }
  1195. public static void Frintx_S(ILEmitterCtx context)
  1196. {
  1197. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1198. EmitScalarUnaryOpF(context, () =>
  1199. {
  1200. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1201. if (op.Size == 0)
  1202. {
  1203. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1204. }
  1205. else if (op.Size == 1)
  1206. {
  1207. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1208. }
  1209. else
  1210. {
  1211. throw new InvalidOperationException();
  1212. }
  1213. });
  1214. }
  1215. public static void Frintx_V(ILEmitterCtx context)
  1216. {
  1217. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1218. EmitVectorUnaryOpF(context, () =>
  1219. {
  1220. context.EmitLdarg(TranslatedSub.StateArgIdx);
  1221. if (op.Size == 0)
  1222. {
  1223. VectorHelper.EmitCall(context, nameof(VectorHelper.RoundF));
  1224. }
  1225. else if (op.Size == 1)
  1226. {
  1227. VectorHelper.EmitCall(context, nameof(VectorHelper.Round));
  1228. }
  1229. else
  1230. {
  1231. throw new InvalidOperationException();
  1232. }
  1233. });
  1234. }
  1235. public static void Frintz_S(ILEmitterCtx context)
  1236. {
  1237. EmitScalarUnaryOpF(context, () =>
  1238. {
  1239. EmitUnaryMathCall(context, nameof(Math.Truncate));
  1240. });
  1241. }
  1242. public static void Frintz_V(ILEmitterCtx context)
  1243. {
  1244. EmitVectorUnaryOpF(context, () =>
  1245. {
  1246. EmitUnaryMathCall(context, nameof(Math.Truncate));
  1247. });
  1248. }
  1249. public static void Frsqrte_S(ILEmitterCtx context)
  1250. {
  1251. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1252. int sizeF = op.Size & 1;
  1253. if (Optimizations.FastFP && Optimizations.UseSse
  1254. && sizeF == 0)
  1255. {
  1256. EmitScalarSseOrSse2OpF(context, nameof(Sse.ReciprocalSqrtScalar));
  1257. }
  1258. else
  1259. {
  1260. EmitScalarUnaryOpF(context, () =>
  1261. {
  1262. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtEstimate));
  1263. });
  1264. }
  1265. }
  1266. public static void Frsqrte_V(ILEmitterCtx context)
  1267. {
  1268. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1269. int sizeF = op.Size & 1;
  1270. if (Optimizations.FastFP && Optimizations.UseSse
  1271. && sizeF == 0)
  1272. {
  1273. EmitVectorSseOrSse2OpF(context, nameof(Sse.ReciprocalSqrt));
  1274. }
  1275. else
  1276. {
  1277. EmitVectorUnaryOpF(context, () =>
  1278. {
  1279. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtEstimate));
  1280. });
  1281. }
  1282. }
  1283. public static void Frsqrts_S(ILEmitterCtx context) // Fused.
  1284. {
  1285. if (Optimizations.FastFP && Optimizations.UseSse2)
  1286. {
  1287. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1288. int sizeF = op.Size & 1;
  1289. if (sizeF == 0)
  1290. {
  1291. Type[] typesSsv = new Type[] { typeof(float) };
  1292. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1293. context.EmitLdc_R4(0.5f);
  1294. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  1295. context.EmitLdc_R4(3f);
  1296. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetScalarVector128), typesSsv));
  1297. context.EmitLdvec(op.Rn);
  1298. context.EmitLdvec(op.Rm);
  1299. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  1300. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SubtractScalar), typesMulSub));
  1301. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.MultiplyScalar), typesMulSub));
  1302. context.EmitStvec(op.Rd);
  1303. EmitVectorZero32_128(context, op.Rd);
  1304. }
  1305. else /* if (sizeF == 1) */
  1306. {
  1307. Type[] typesSsv = new Type[] { typeof(double) };
  1308. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1309. context.EmitLdc_R8(0.5d);
  1310. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  1311. context.EmitLdc_R8(3d);
  1312. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetScalarVector128), typesSsv));
  1313. EmitLdvecWithCastToDouble(context, op.Rn);
  1314. EmitLdvecWithCastToDouble(context, op.Rm);
  1315. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  1316. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SubtractScalar), typesMulSub));
  1317. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.MultiplyScalar), typesMulSub));
  1318. EmitStvecWithCastFromDouble(context, op.Rd);
  1319. EmitVectorZeroUpper(context, op.Rd);
  1320. }
  1321. }
  1322. else
  1323. {
  1324. EmitScalarBinaryOpF(context, () =>
  1325. {
  1326. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtStepFused));
  1327. });
  1328. }
  1329. }
  1330. public static void Frsqrts_V(ILEmitterCtx context) // Fused.
  1331. {
  1332. if (Optimizations.FastFP && Optimizations.UseSse2)
  1333. {
  1334. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1335. int sizeF = op.Size & 1;
  1336. if (sizeF == 0)
  1337. {
  1338. Type[] typesSav = new Type[] { typeof(float) };
  1339. Type[] typesMulSub = new Type[] { typeof(Vector128<float>), typeof(Vector128<float>) };
  1340. context.EmitLdc_R4(0.5f);
  1341. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  1342. context.EmitLdc_R4(3f);
  1343. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.SetAllVector128), typesSav));
  1344. context.EmitLdvec(op.Rn);
  1345. context.EmitLdvec(op.Rm);
  1346. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  1347. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Subtract), typesMulSub));
  1348. context.EmitCall(typeof(Sse).GetMethod(nameof(Sse.Multiply), typesMulSub));
  1349. context.EmitStvec(op.Rd);
  1350. if (op.RegisterSize == RegisterSize.Simd64)
  1351. {
  1352. EmitVectorZeroUpper(context, op.Rd);
  1353. }
  1354. }
  1355. else /* if (sizeF == 1) */
  1356. {
  1357. Type[] typesSav = new Type[] { typeof(double) };
  1358. Type[] typesMulSub = new Type[] { typeof(Vector128<double>), typeof(Vector128<double>) };
  1359. context.EmitLdc_R8(0.5d);
  1360. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1361. context.EmitLdc_R8(3d);
  1362. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1363. EmitLdvecWithCastToDouble(context, op.Rn);
  1364. EmitLdvecWithCastToDouble(context, op.Rm);
  1365. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  1366. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  1367. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Multiply), typesMulSub));
  1368. EmitStvecWithCastFromDouble(context, op.Rd);
  1369. }
  1370. }
  1371. else
  1372. {
  1373. EmitVectorBinaryOpF(context, () =>
  1374. {
  1375. EmitSoftFloatCall(context, nameof(SoftFloat32.FPRSqrtStepFused));
  1376. });
  1377. }
  1378. }
  1379. public static void Fsqrt_S(ILEmitterCtx context)
  1380. {
  1381. if (Optimizations.FastFP && Optimizations.UseSse2)
  1382. {
  1383. EmitScalarSseOrSse2OpF(context, nameof(Sse.SqrtScalar));
  1384. }
  1385. else
  1386. {
  1387. EmitScalarUnaryOpF(context, () =>
  1388. {
  1389. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSqrt));
  1390. });
  1391. }
  1392. }
  1393. public static void Fsqrt_V(ILEmitterCtx context)
  1394. {
  1395. if (Optimizations.FastFP && Optimizations.UseSse2)
  1396. {
  1397. EmitVectorSseOrSse2OpF(context, nameof(Sse.Sqrt));
  1398. }
  1399. else
  1400. {
  1401. EmitVectorUnaryOpF(context, () =>
  1402. {
  1403. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSqrt));
  1404. });
  1405. }
  1406. }
  1407. public static void Fsub_S(ILEmitterCtx context)
  1408. {
  1409. if (Optimizations.FastFP && Optimizations.UseSse2)
  1410. {
  1411. EmitScalarSseOrSse2OpF(context, nameof(Sse.SubtractScalar));
  1412. }
  1413. else
  1414. {
  1415. EmitScalarBinaryOpF(context, () =>
  1416. {
  1417. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  1418. });
  1419. }
  1420. }
  1421. public static void Fsub_V(ILEmitterCtx context)
  1422. {
  1423. if (Optimizations.FastFP && Optimizations.UseSse2)
  1424. {
  1425. EmitVectorSseOrSse2OpF(context, nameof(Sse.Subtract));
  1426. }
  1427. else
  1428. {
  1429. EmitVectorBinaryOpF(context, () =>
  1430. {
  1431. EmitSoftFloatCall(context, nameof(SoftFloat32.FPSub));
  1432. });
  1433. }
  1434. }
  1435. public static void Mla_V(ILEmitterCtx context)
  1436. {
  1437. EmitVectorTernaryOpZx(context, () =>
  1438. {
  1439. context.Emit(OpCodes.Mul);
  1440. context.Emit(OpCodes.Add);
  1441. });
  1442. }
  1443. public static void Mla_Ve(ILEmitterCtx context)
  1444. {
  1445. EmitVectorTernaryOpByElemZx(context, () =>
  1446. {
  1447. context.Emit(OpCodes.Mul);
  1448. context.Emit(OpCodes.Add);
  1449. });
  1450. }
  1451. public static void Mls_V(ILEmitterCtx context)
  1452. {
  1453. EmitVectorTernaryOpZx(context, () =>
  1454. {
  1455. context.Emit(OpCodes.Mul);
  1456. context.Emit(OpCodes.Sub);
  1457. });
  1458. }
  1459. public static void Mls_Ve(ILEmitterCtx context)
  1460. {
  1461. EmitVectorTernaryOpByElemZx(context, () =>
  1462. {
  1463. context.Emit(OpCodes.Mul);
  1464. context.Emit(OpCodes.Sub);
  1465. });
  1466. }
  1467. public static void Mul_V(ILEmitterCtx context)
  1468. {
  1469. EmitVectorBinaryOpZx(context, () => context.Emit(OpCodes.Mul));
  1470. }
  1471. public static void Mul_Ve(ILEmitterCtx context)
  1472. {
  1473. EmitVectorBinaryOpByElemZx(context, () => context.Emit(OpCodes.Mul));
  1474. }
  1475. public static void Neg_S(ILEmitterCtx context)
  1476. {
  1477. EmitScalarUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  1478. }
  1479. public static void Neg_V(ILEmitterCtx context)
  1480. {
  1481. if (Optimizations.UseSse2)
  1482. {
  1483. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  1484. Type[] typesSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1485. string[] namesSzv = new string[] { nameof(VectorHelper.VectorSByteZero),
  1486. nameof(VectorHelper.VectorInt16Zero),
  1487. nameof(VectorHelper.VectorInt32Zero),
  1488. nameof(VectorHelper.VectorInt64Zero) };
  1489. VectorHelper.EmitCall(context, namesSzv[op.Size]);
  1490. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1491. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  1492. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  1493. if (op.RegisterSize == RegisterSize.Simd64)
  1494. {
  1495. EmitVectorZeroUpper(context, op.Rd);
  1496. }
  1497. }
  1498. else
  1499. {
  1500. EmitVectorUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  1501. }
  1502. }
  1503. public static void Raddhn_V(ILEmitterCtx context)
  1504. {
  1505. EmitHighNarrow(context, () => context.Emit(OpCodes.Add), round: true);
  1506. }
  1507. public static void Rsubhn_V(ILEmitterCtx context)
  1508. {
  1509. EmitHighNarrow(context, () => context.Emit(OpCodes.Sub), round: true);
  1510. }
  1511. public static void Saba_V(ILEmitterCtx context)
  1512. {
  1513. EmitVectorTernaryOpSx(context, () =>
  1514. {
  1515. context.Emit(OpCodes.Sub);
  1516. EmitAbs(context);
  1517. context.Emit(OpCodes.Add);
  1518. });
  1519. }
  1520. public static void Sabal_V(ILEmitterCtx context)
  1521. {
  1522. EmitVectorWidenRnRmTernaryOpSx(context, () =>
  1523. {
  1524. context.Emit(OpCodes.Sub);
  1525. EmitAbs(context);
  1526. context.Emit(OpCodes.Add);
  1527. });
  1528. }
  1529. public static void Sabd_V(ILEmitterCtx context)
  1530. {
  1531. EmitVectorBinaryOpSx(context, () =>
  1532. {
  1533. context.Emit(OpCodes.Sub);
  1534. EmitAbs(context);
  1535. });
  1536. }
  1537. public static void Sabdl_V(ILEmitterCtx context)
  1538. {
  1539. EmitVectorWidenRnRmBinaryOpSx(context, () =>
  1540. {
  1541. context.Emit(OpCodes.Sub);
  1542. EmitAbs(context);
  1543. });
  1544. }
  1545. public static void Sadalp_V(ILEmitterCtx context)
  1546. {
  1547. EmitAddLongPairwise(context, signed: true, accumulate: true);
  1548. }
  1549. public static void Saddl_V(ILEmitterCtx context)
  1550. {
  1551. if (Optimizations.UseSse41)
  1552. {
  1553. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1554. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1555. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1556. Type[] typesAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1557. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1558. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  1559. nameof(Sse41.ConvertToVector128Int32),
  1560. nameof(Sse41.ConvertToVector128Int64) };
  1561. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  1562. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1563. context.EmitLdc_I4(numBytes);
  1564. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1565. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1566. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1567. context.EmitLdc_I4(numBytes);
  1568. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1569. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1570. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  1571. EmitStvecWithSignedCast(context, op.Rd, op.Size + 1);
  1572. }
  1573. else
  1574. {
  1575. EmitVectorWidenRnRmBinaryOpSx(context, () => context.Emit(OpCodes.Add));
  1576. }
  1577. }
  1578. public static void Saddlp_V(ILEmitterCtx context)
  1579. {
  1580. EmitAddLongPairwise(context, signed: true, accumulate: false);
  1581. }
  1582. public static void Saddw_V(ILEmitterCtx context)
  1583. {
  1584. if (Optimizations.UseSse41)
  1585. {
  1586. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1587. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1588. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1589. Type[] typesAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1590. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1591. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  1592. nameof(Sse41.ConvertToVector128Int32),
  1593. nameof(Sse41.ConvertToVector128Int64) };
  1594. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  1595. EmitLdvecWithSignedCast(context, op.Rn, op.Size + 1);
  1596. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1597. context.EmitLdc_I4(numBytes);
  1598. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1599. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1600. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  1601. EmitStvecWithSignedCast(context, op.Rd, op.Size + 1);
  1602. }
  1603. else
  1604. {
  1605. EmitVectorWidenRmBinaryOpSx(context, () => context.Emit(OpCodes.Add));
  1606. }
  1607. }
  1608. public static void Shadd_V(ILEmitterCtx context)
  1609. {
  1610. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1611. if (Optimizations.UseSse2 && op.Size > 0)
  1612. {
  1613. Type[] typesSra = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1614. Type[] typesAndXorAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1615. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1616. context.Emit(OpCodes.Dup);
  1617. context.EmitStvectmp();
  1618. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1619. context.Emit(OpCodes.Dup);
  1620. context.EmitStvectmp2();
  1621. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndXorAdd));
  1622. context.EmitLdvectmp();
  1623. context.EmitLdvectmp2();
  1624. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesAndXorAdd));
  1625. context.EmitLdc_I4(1);
  1626. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightArithmetic), typesSra));
  1627. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAndXorAdd));
  1628. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  1629. if (op.RegisterSize == RegisterSize.Simd64)
  1630. {
  1631. EmitVectorZeroUpper(context, op.Rd);
  1632. }
  1633. }
  1634. else
  1635. {
  1636. EmitVectorBinaryOpSx(context, () =>
  1637. {
  1638. context.Emit(OpCodes.Add);
  1639. context.Emit(OpCodes.Ldc_I4_1);
  1640. context.Emit(OpCodes.Shr);
  1641. });
  1642. }
  1643. }
  1644. public static void Shsub_V(ILEmitterCtx context)
  1645. {
  1646. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1647. if (Optimizations.UseSse2 && op.Size < 2)
  1648. {
  1649. Type[] typesSav = new Type[] { IntTypesPerSizeLog2[op.Size] };
  1650. Type[] typesAddSub = new Type[] { VectorIntTypesPerSizeLog2 [op.Size], VectorIntTypesPerSizeLog2 [op.Size] };
  1651. Type[] typesAvg = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  1652. context.EmitLdc_I4(op.Size == 0 ? sbyte.MinValue : short.MinValue);
  1653. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1654. context.EmitStvectmp();
  1655. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1656. context.EmitLdvectmp();
  1657. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAddSub));
  1658. context.Emit(OpCodes.Dup);
  1659. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1660. context.EmitLdvectmp();
  1661. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAddSub));
  1662. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvg));
  1663. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesAddSub));
  1664. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  1665. if (op.RegisterSize == RegisterSize.Simd64)
  1666. {
  1667. EmitVectorZeroUpper(context, op.Rd);
  1668. }
  1669. }
  1670. else
  1671. {
  1672. EmitVectorBinaryOpSx(context, () =>
  1673. {
  1674. context.Emit(OpCodes.Sub);
  1675. context.Emit(OpCodes.Ldc_I4_1);
  1676. context.Emit(OpCodes.Shr);
  1677. });
  1678. }
  1679. }
  1680. public static void Smax_V(ILEmitterCtx context)
  1681. {
  1682. if (Optimizations.UseSse41)
  1683. {
  1684. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1685. Type[] typesMax = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1686. Type typeSse = op.Size == 1 ? typeof(Sse2) : typeof(Sse41);
  1687. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1688. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1689. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Max), typesMax));
  1690. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  1691. if (op.RegisterSize == RegisterSize.Simd64)
  1692. {
  1693. EmitVectorZeroUpper(context, op.Rd);
  1694. }
  1695. }
  1696. else
  1697. {
  1698. Type[] types = new Type[] { typeof(long), typeof(long) };
  1699. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  1700. EmitVectorBinaryOpSx(context, () => context.EmitCall(mthdInfo));
  1701. }
  1702. }
  1703. public static void Smaxp_V(ILEmitterCtx context)
  1704. {
  1705. Type[] types = new Type[] { typeof(long), typeof(long) };
  1706. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  1707. EmitVectorPairwiseOpSx(context, () => context.EmitCall(mthdInfo));
  1708. }
  1709. public static void Smin_V(ILEmitterCtx context)
  1710. {
  1711. if (Optimizations.UseSse41)
  1712. {
  1713. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1714. Type[] typesMin = new Type[] { VectorIntTypesPerSizeLog2[op.Size], VectorIntTypesPerSizeLog2[op.Size] };
  1715. Type typeSse = op.Size == 1 ? typeof(Sse2) : typeof(Sse41);
  1716. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1717. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1718. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Min), typesMin));
  1719. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  1720. if (op.RegisterSize == RegisterSize.Simd64)
  1721. {
  1722. EmitVectorZeroUpper(context, op.Rd);
  1723. }
  1724. }
  1725. else
  1726. {
  1727. Type[] types = new Type[] { typeof(long), typeof(long) };
  1728. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  1729. EmitVectorBinaryOpSx(context, () => context.EmitCall(mthdInfo));
  1730. }
  1731. }
  1732. public static void Sminp_V(ILEmitterCtx context)
  1733. {
  1734. Type[] types = new Type[] { typeof(long), typeof(long) };
  1735. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  1736. EmitVectorPairwiseOpSx(context, () => context.EmitCall(mthdInfo));
  1737. }
  1738. public static void Smlal_V(ILEmitterCtx context)
  1739. {
  1740. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1741. if (Optimizations.UseSse41 && op.Size < 2)
  1742. {
  1743. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1744. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1745. Type[] typesMulAdd = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1746. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1747. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  1748. string nameCvt = op.Size == 0
  1749. ? nameof(Sse41.ConvertToVector128Int16)
  1750. : nameof(Sse41.ConvertToVector128Int32);
  1751. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  1752. EmitLdvecWithSignedCast(context, op.Rd, op.Size + 1);
  1753. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1754. context.EmitLdc_I4(numBytes);
  1755. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1756. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1757. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1758. context.EmitLdc_I4(numBytes);
  1759. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1760. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1761. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulAdd));
  1762. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  1763. EmitStvecWithSignedCast(context, op.Rd, op.Size + 1);
  1764. }
  1765. else
  1766. {
  1767. EmitVectorWidenRnRmTernaryOpSx(context, () =>
  1768. {
  1769. context.Emit(OpCodes.Mul);
  1770. context.Emit(OpCodes.Add);
  1771. });
  1772. }
  1773. }
  1774. public static void Smlal_Ve(ILEmitterCtx context)
  1775. {
  1776. EmitVectorWidenTernaryOpByElemSx(context, () =>
  1777. {
  1778. context.Emit(OpCodes.Mul);
  1779. context.Emit(OpCodes.Add);
  1780. });
  1781. }
  1782. public static void Smlsl_V(ILEmitterCtx context)
  1783. {
  1784. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1785. if (Optimizations.UseSse41 && op.Size < 2)
  1786. {
  1787. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1788. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1789. Type[] typesMulSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1790. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1791. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  1792. string nameCvt = op.Size == 0
  1793. ? nameof(Sse41.ConvertToVector128Int16)
  1794. : nameof(Sse41.ConvertToVector128Int32);
  1795. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  1796. EmitLdvecWithSignedCast(context, op.Rd, op.Size + 1);
  1797. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1798. context.EmitLdc_I4(numBytes);
  1799. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1800. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1801. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1802. context.EmitLdc_I4(numBytes);
  1803. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1804. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  1805. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulSub));
  1806. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  1807. EmitStvecWithSignedCast(context, op.Rd, op.Size + 1);
  1808. }
  1809. else
  1810. {
  1811. EmitVectorWidenRnRmTernaryOpSx(context, () =>
  1812. {
  1813. context.Emit(OpCodes.Mul);
  1814. context.Emit(OpCodes.Sub);
  1815. });
  1816. }
  1817. }
  1818. public static void Smlsl_Ve(ILEmitterCtx context)
  1819. {
  1820. EmitVectorWidenTernaryOpByElemSx(context, () =>
  1821. {
  1822. context.Emit(OpCodes.Mul);
  1823. context.Emit(OpCodes.Sub);
  1824. });
  1825. }
  1826. public static void Smull_V(ILEmitterCtx context)
  1827. {
  1828. EmitVectorWidenRnRmBinaryOpSx(context, () => context.Emit(OpCodes.Mul));
  1829. }
  1830. public static void Smull_Ve(ILEmitterCtx context)
  1831. {
  1832. EmitVectorWidenBinaryOpByElemSx(context, () => context.Emit(OpCodes.Mul));
  1833. }
  1834. public static void Sqabs_S(ILEmitterCtx context)
  1835. {
  1836. EmitScalarSaturatingUnaryOpSx(context, () => EmitAbs(context));
  1837. }
  1838. public static void Sqabs_V(ILEmitterCtx context)
  1839. {
  1840. EmitVectorSaturatingUnaryOpSx(context, () => EmitAbs(context));
  1841. }
  1842. public static void Sqadd_S(ILEmitterCtx context)
  1843. {
  1844. EmitScalarSaturatingBinaryOpSx(context, SaturatingFlags.Add);
  1845. }
  1846. public static void Sqadd_V(ILEmitterCtx context)
  1847. {
  1848. EmitVectorSaturatingBinaryOpSx(context, SaturatingFlags.Add);
  1849. }
  1850. public static void Sqdmulh_S(ILEmitterCtx context)
  1851. {
  1852. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: false), SaturatingFlags.ScalarSx);
  1853. }
  1854. public static void Sqdmulh_V(ILEmitterCtx context)
  1855. {
  1856. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: false), SaturatingFlags.VectorSx);
  1857. }
  1858. public static void Sqneg_S(ILEmitterCtx context)
  1859. {
  1860. EmitScalarSaturatingUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  1861. }
  1862. public static void Sqneg_V(ILEmitterCtx context)
  1863. {
  1864. EmitVectorSaturatingUnaryOpSx(context, () => context.Emit(OpCodes.Neg));
  1865. }
  1866. public static void Sqrdmulh_S(ILEmitterCtx context)
  1867. {
  1868. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: true), SaturatingFlags.ScalarSx);
  1869. }
  1870. public static void Sqrdmulh_V(ILEmitterCtx context)
  1871. {
  1872. EmitSaturatingBinaryOp(context, () => EmitDoublingMultiplyHighHalf(context, round: true), SaturatingFlags.VectorSx);
  1873. }
  1874. public static void Sqsub_S(ILEmitterCtx context)
  1875. {
  1876. EmitScalarSaturatingBinaryOpSx(context, SaturatingFlags.Sub);
  1877. }
  1878. public static void Sqsub_V(ILEmitterCtx context)
  1879. {
  1880. EmitVectorSaturatingBinaryOpSx(context, SaturatingFlags.Sub);
  1881. }
  1882. public static void Sqxtn_S(ILEmitterCtx context)
  1883. {
  1884. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.ScalarSxSx);
  1885. }
  1886. public static void Sqxtn_V(ILEmitterCtx context)
  1887. {
  1888. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.VectorSxSx);
  1889. }
  1890. public static void Sqxtun_S(ILEmitterCtx context)
  1891. {
  1892. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.ScalarSxZx);
  1893. }
  1894. public static void Sqxtun_V(ILEmitterCtx context)
  1895. {
  1896. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.VectorSxZx);
  1897. }
  1898. public static void Srhadd_V(ILEmitterCtx context)
  1899. {
  1900. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1901. if (Optimizations.UseSse2 && op.Size < 2)
  1902. {
  1903. Type[] typesSav = new Type[] { IntTypesPerSizeLog2[op.Size] };
  1904. Type[] typesSubAdd = new Type[] { VectorIntTypesPerSizeLog2 [op.Size], VectorIntTypesPerSizeLog2 [op.Size] };
  1905. Type[] typesAvg = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  1906. context.EmitLdc_I4(op.Size == 0 ? sbyte.MinValue : short.MinValue);
  1907. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.SetAllVector128), typesSav));
  1908. context.Emit(OpCodes.Dup);
  1909. context.EmitStvectmp();
  1910. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1911. context.EmitLdvectmp();
  1912. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSubAdd));
  1913. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1914. context.EmitLdvectmp();
  1915. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSubAdd));
  1916. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvg));
  1917. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesSubAdd));
  1918. EmitStvecWithSignedCast(context, op.Rd, op.Size);
  1919. if (op.RegisterSize == RegisterSize.Simd64)
  1920. {
  1921. EmitVectorZeroUpper(context, op.Rd);
  1922. }
  1923. }
  1924. else
  1925. {
  1926. EmitVectorBinaryOpSx(context, () =>
  1927. {
  1928. context.Emit(OpCodes.Add);
  1929. context.Emit(OpCodes.Ldc_I4_1);
  1930. context.Emit(OpCodes.Add);
  1931. context.Emit(OpCodes.Ldc_I4_1);
  1932. context.Emit(OpCodes.Shr);
  1933. });
  1934. }
  1935. }
  1936. public static void Ssubl_V(ILEmitterCtx context)
  1937. {
  1938. if (Optimizations.UseSse41)
  1939. {
  1940. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1941. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1942. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1943. Type[] typesSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1944. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1945. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  1946. nameof(Sse41.ConvertToVector128Int32),
  1947. nameof(Sse41.ConvertToVector128Int64) };
  1948. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  1949. EmitLdvecWithSignedCast(context, op.Rn, op.Size);
  1950. context.EmitLdc_I4(numBytes);
  1951. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1952. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1953. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1954. context.EmitLdc_I4(numBytes);
  1955. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1956. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1957. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  1958. EmitStvecWithSignedCast(context, op.Rd, op.Size + 1);
  1959. }
  1960. else
  1961. {
  1962. EmitVectorWidenRnRmBinaryOpSx(context, () => context.Emit(OpCodes.Sub));
  1963. }
  1964. }
  1965. public static void Ssubw_V(ILEmitterCtx context)
  1966. {
  1967. if (Optimizations.UseSse41)
  1968. {
  1969. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  1970. Type[] typesSrl = new Type[] { VectorIntTypesPerSizeLog2[op.Size], typeof(byte) };
  1971. Type[] typesCvt = new Type[] { VectorIntTypesPerSizeLog2[op.Size] };
  1972. Type[] typesSub = new Type[] { VectorIntTypesPerSizeLog2[op.Size + 1],
  1973. VectorIntTypesPerSizeLog2[op.Size + 1] };
  1974. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  1975. nameof(Sse41.ConvertToVector128Int32),
  1976. nameof(Sse41.ConvertToVector128Int64) };
  1977. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  1978. EmitLdvecWithSignedCast(context, op.Rn, op.Size + 1);
  1979. EmitLdvecWithSignedCast(context, op.Rm, op.Size);
  1980. context.EmitLdc_I4(numBytes);
  1981. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  1982. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  1983. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  1984. EmitStvecWithSignedCast(context, op.Rd, op.Size + 1);
  1985. }
  1986. else
  1987. {
  1988. EmitVectorWidenRmBinaryOpSx(context, () => context.Emit(OpCodes.Sub));
  1989. }
  1990. }
  1991. public static void Sub_S(ILEmitterCtx context)
  1992. {
  1993. EmitScalarBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  1994. }
  1995. public static void Sub_V(ILEmitterCtx context)
  1996. {
  1997. if (Optimizations.UseSse2)
  1998. {
  1999. EmitSse2Op(context, nameof(Sse2.Subtract));
  2000. }
  2001. else
  2002. {
  2003. EmitVectorBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2004. }
  2005. }
  2006. public static void Subhn_V(ILEmitterCtx context)
  2007. {
  2008. EmitHighNarrow(context, () => context.Emit(OpCodes.Sub), round: false);
  2009. }
  2010. public static void Suqadd_S(ILEmitterCtx context)
  2011. {
  2012. EmitScalarSaturatingBinaryOpSx(context, SaturatingFlags.Accumulate);
  2013. }
  2014. public static void Suqadd_V(ILEmitterCtx context)
  2015. {
  2016. EmitVectorSaturatingBinaryOpSx(context, SaturatingFlags.Accumulate);
  2017. }
  2018. public static void Uaba_V(ILEmitterCtx context)
  2019. {
  2020. EmitVectorTernaryOpZx(context, () =>
  2021. {
  2022. context.Emit(OpCodes.Sub);
  2023. EmitAbs(context);
  2024. context.Emit(OpCodes.Add);
  2025. });
  2026. }
  2027. public static void Uabal_V(ILEmitterCtx context)
  2028. {
  2029. EmitVectorWidenRnRmTernaryOpZx(context, () =>
  2030. {
  2031. context.Emit(OpCodes.Sub);
  2032. EmitAbs(context);
  2033. context.Emit(OpCodes.Add);
  2034. });
  2035. }
  2036. public static void Uabd_V(ILEmitterCtx context)
  2037. {
  2038. EmitVectorBinaryOpZx(context, () =>
  2039. {
  2040. context.Emit(OpCodes.Sub);
  2041. EmitAbs(context);
  2042. });
  2043. }
  2044. public static void Uabdl_V(ILEmitterCtx context)
  2045. {
  2046. EmitVectorWidenRnRmBinaryOpZx(context, () =>
  2047. {
  2048. context.Emit(OpCodes.Sub);
  2049. EmitAbs(context);
  2050. });
  2051. }
  2052. public static void Uadalp_V(ILEmitterCtx context)
  2053. {
  2054. EmitAddLongPairwise(context, signed: false, accumulate: true);
  2055. }
  2056. public static void Uaddl_V(ILEmitterCtx context)
  2057. {
  2058. if (Optimizations.UseSse41)
  2059. {
  2060. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2061. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2062. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2063. Type[] typesAdd = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2064. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2065. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2066. nameof(Sse41.ConvertToVector128Int32),
  2067. nameof(Sse41.ConvertToVector128Int64) };
  2068. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  2069. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2070. context.EmitLdc_I4(numBytes);
  2071. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2072. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2073. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2074. context.EmitLdc_I4(numBytes);
  2075. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2076. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2077. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  2078. EmitStvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2079. }
  2080. else
  2081. {
  2082. EmitVectorWidenRnRmBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  2083. }
  2084. }
  2085. public static void Uaddlp_V(ILEmitterCtx context)
  2086. {
  2087. EmitAddLongPairwise(context, signed: false, accumulate: false);
  2088. }
  2089. public static void Uaddlv_V(ILEmitterCtx context)
  2090. {
  2091. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  2092. int bytes = op.GetBitsCount() >> 3;
  2093. int elems = bytes >> op.Size;
  2094. EmitVectorExtractZx(context, op.Rn, 0, op.Size);
  2095. for (int index = 1; index < elems; index++)
  2096. {
  2097. EmitVectorExtractZx(context, op.Rn, index, op.Size);
  2098. context.Emit(OpCodes.Add);
  2099. }
  2100. EmitScalarSet(context, op.Rd, op.Size + 1);
  2101. }
  2102. public static void Uaddw_V(ILEmitterCtx context)
  2103. {
  2104. if (Optimizations.UseSse41)
  2105. {
  2106. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2107. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2108. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2109. Type[] typesAdd = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2110. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2111. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2112. nameof(Sse41.ConvertToVector128Int32),
  2113. nameof(Sse41.ConvertToVector128Int64) };
  2114. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  2115. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size + 1);
  2116. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2117. context.EmitLdc_I4(numBytes);
  2118. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2119. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2120. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAdd));
  2121. EmitStvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2122. }
  2123. else
  2124. {
  2125. EmitVectorWidenRmBinaryOpZx(context, () => context.Emit(OpCodes.Add));
  2126. }
  2127. }
  2128. public static void Uhadd_V(ILEmitterCtx context)
  2129. {
  2130. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2131. if (Optimizations.UseSse2 && op.Size > 0)
  2132. {
  2133. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2134. Type[] typesAndXorAdd = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2135. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2136. context.Emit(OpCodes.Dup);
  2137. context.EmitStvectmp();
  2138. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2139. context.Emit(OpCodes.Dup);
  2140. context.EmitStvectmp2();
  2141. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.And), typesAndXorAdd));
  2142. context.EmitLdvectmp();
  2143. context.EmitLdvectmp2();
  2144. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Xor), typesAndXorAdd));
  2145. context.EmitLdc_I4(1);
  2146. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical), typesSrl));
  2147. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesAndXorAdd));
  2148. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  2149. if (op.RegisterSize == RegisterSize.Simd64)
  2150. {
  2151. EmitVectorZeroUpper(context, op.Rd);
  2152. }
  2153. }
  2154. else
  2155. {
  2156. EmitVectorBinaryOpZx(context, () =>
  2157. {
  2158. context.Emit(OpCodes.Add);
  2159. context.Emit(OpCodes.Ldc_I4_1);
  2160. context.Emit(OpCodes.Shr_Un);
  2161. });
  2162. }
  2163. }
  2164. public static void Uhsub_V(ILEmitterCtx context)
  2165. {
  2166. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2167. if (Optimizations.UseSse2 && op.Size < 2)
  2168. {
  2169. Type[] typesAvgSub = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2170. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2171. context.Emit(OpCodes.Dup);
  2172. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2173. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvgSub));
  2174. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesAvgSub));
  2175. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  2176. if (op.RegisterSize == RegisterSize.Simd64)
  2177. {
  2178. EmitVectorZeroUpper(context, op.Rd);
  2179. }
  2180. }
  2181. else
  2182. {
  2183. EmitVectorBinaryOpZx(context, () =>
  2184. {
  2185. context.Emit(OpCodes.Sub);
  2186. context.Emit(OpCodes.Ldc_I4_1);
  2187. context.Emit(OpCodes.Shr_Un);
  2188. });
  2189. }
  2190. }
  2191. public static void Umax_V(ILEmitterCtx context)
  2192. {
  2193. if (Optimizations.UseSse41)
  2194. {
  2195. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2196. Type[] typesMax = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2197. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2198. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2199. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2200. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Max), typesMax));
  2201. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  2202. if (op.RegisterSize == RegisterSize.Simd64)
  2203. {
  2204. EmitVectorZeroUpper(context, op.Rd);
  2205. }
  2206. }
  2207. else
  2208. {
  2209. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2210. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  2211. EmitVectorBinaryOpZx(context, () => context.EmitCall(mthdInfo));
  2212. }
  2213. }
  2214. public static void Umaxp_V(ILEmitterCtx context)
  2215. {
  2216. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2217. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Max), types);
  2218. EmitVectorPairwiseOpZx(context, () => context.EmitCall(mthdInfo));
  2219. }
  2220. public static void Umin_V(ILEmitterCtx context)
  2221. {
  2222. if (Optimizations.UseSse41)
  2223. {
  2224. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2225. Type[] typesMin = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2226. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2227. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2228. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2229. context.EmitCall(typeSse.GetMethod(nameof(Sse2.Min), typesMin));
  2230. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  2231. if (op.RegisterSize == RegisterSize.Simd64)
  2232. {
  2233. EmitVectorZeroUpper(context, op.Rd);
  2234. }
  2235. }
  2236. else
  2237. {
  2238. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2239. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  2240. EmitVectorBinaryOpZx(context, () => context.EmitCall(mthdInfo));
  2241. }
  2242. }
  2243. public static void Uminp_V(ILEmitterCtx context)
  2244. {
  2245. Type[] types = new Type[] { typeof(ulong), typeof(ulong) };
  2246. MethodInfo mthdInfo = typeof(Math).GetMethod(nameof(Math.Min), types);
  2247. EmitVectorPairwiseOpZx(context, () => context.EmitCall(mthdInfo));
  2248. }
  2249. public static void Umlal_V(ILEmitterCtx context)
  2250. {
  2251. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2252. if (Optimizations.UseSse41 && op.Size < 2)
  2253. {
  2254. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2255. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2256. Type[] typesMulAdd = new Type[] { VectorIntTypesPerSizeLog2 [op.Size + 1],
  2257. VectorIntTypesPerSizeLog2 [op.Size + 1] };
  2258. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2259. string nameCvt = op.Size == 0
  2260. ? nameof(Sse41.ConvertToVector128Int16)
  2261. : nameof(Sse41.ConvertToVector128Int32);
  2262. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  2263. EmitLdvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2264. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2265. context.EmitLdc_I4(numBytes);
  2266. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2267. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2268. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2269. context.EmitLdc_I4(numBytes);
  2270. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2271. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2272. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulAdd));
  2273. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Add), typesMulAdd));
  2274. EmitStvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2275. }
  2276. else
  2277. {
  2278. EmitVectorWidenRnRmTernaryOpZx(context, () =>
  2279. {
  2280. context.Emit(OpCodes.Mul);
  2281. context.Emit(OpCodes.Add);
  2282. });
  2283. }
  2284. }
  2285. public static void Umlal_Ve(ILEmitterCtx context)
  2286. {
  2287. EmitVectorWidenTernaryOpByElemZx(context, () =>
  2288. {
  2289. context.Emit(OpCodes.Mul);
  2290. context.Emit(OpCodes.Add);
  2291. });
  2292. }
  2293. public static void Umlsl_V(ILEmitterCtx context)
  2294. {
  2295. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2296. if (Optimizations.UseSse41 && op.Size < 2)
  2297. {
  2298. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2299. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2300. Type[] typesMulSub = new Type[] { VectorIntTypesPerSizeLog2 [op.Size + 1],
  2301. VectorIntTypesPerSizeLog2 [op.Size + 1] };
  2302. Type typeSse = op.Size == 0 ? typeof(Sse2) : typeof(Sse41);
  2303. string nameCvt = op.Size == 0
  2304. ? nameof(Sse41.ConvertToVector128Int16)
  2305. : nameof(Sse41.ConvertToVector128Int32);
  2306. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  2307. EmitLdvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2308. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2309. context.EmitLdc_I4(numBytes);
  2310. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2311. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2312. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2313. context.EmitLdc_I4(numBytes);
  2314. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2315. context.EmitCall(typeof(Sse41).GetMethod(nameCvt, typesCvt));
  2316. context.EmitCall(typeSse.GetMethod(nameof(Sse2.MultiplyLow), typesMulSub));
  2317. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesMulSub));
  2318. EmitStvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2319. }
  2320. else
  2321. {
  2322. EmitVectorWidenRnRmTernaryOpZx(context, () =>
  2323. {
  2324. context.Emit(OpCodes.Mul);
  2325. context.Emit(OpCodes.Sub);
  2326. });
  2327. }
  2328. }
  2329. public static void Umlsl_Ve(ILEmitterCtx context)
  2330. {
  2331. EmitVectorWidenTernaryOpByElemZx(context, () =>
  2332. {
  2333. context.Emit(OpCodes.Mul);
  2334. context.Emit(OpCodes.Sub);
  2335. });
  2336. }
  2337. public static void Umull_V(ILEmitterCtx context)
  2338. {
  2339. EmitVectorWidenRnRmBinaryOpZx(context, () => context.Emit(OpCodes.Mul));
  2340. }
  2341. public static void Umull_Ve(ILEmitterCtx context)
  2342. {
  2343. EmitVectorWidenBinaryOpByElemZx(context, () => context.Emit(OpCodes.Mul));
  2344. }
  2345. public static void Uqadd_S(ILEmitterCtx context)
  2346. {
  2347. EmitScalarSaturatingBinaryOpZx(context, SaturatingFlags.Add);
  2348. }
  2349. public static void Uqadd_V(ILEmitterCtx context)
  2350. {
  2351. EmitVectorSaturatingBinaryOpZx(context, SaturatingFlags.Add);
  2352. }
  2353. public static void Uqsub_S(ILEmitterCtx context)
  2354. {
  2355. EmitScalarSaturatingBinaryOpZx(context, SaturatingFlags.Sub);
  2356. }
  2357. public static void Uqsub_V(ILEmitterCtx context)
  2358. {
  2359. EmitVectorSaturatingBinaryOpZx(context, SaturatingFlags.Sub);
  2360. }
  2361. public static void Uqxtn_S(ILEmitterCtx context)
  2362. {
  2363. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.ScalarZxZx);
  2364. }
  2365. public static void Uqxtn_V(ILEmitterCtx context)
  2366. {
  2367. EmitSaturatingNarrowOp(context, SaturatingNarrowFlags.VectorZxZx);
  2368. }
  2369. public static void Urhadd_V(ILEmitterCtx context)
  2370. {
  2371. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2372. if (Optimizations.UseSse2 && op.Size < 2)
  2373. {
  2374. Type[] typesAvg = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], VectorUIntTypesPerSizeLog2[op.Size] };
  2375. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2376. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2377. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Average), typesAvg));
  2378. EmitStvecWithUnsignedCast(context, op.Rd, op.Size);
  2379. if (op.RegisterSize == RegisterSize.Simd64)
  2380. {
  2381. EmitVectorZeroUpper(context, op.Rd);
  2382. }
  2383. }
  2384. else
  2385. {
  2386. EmitVectorBinaryOpZx(context, () =>
  2387. {
  2388. context.Emit(OpCodes.Add);
  2389. context.Emit(OpCodes.Ldc_I4_1);
  2390. context.Emit(OpCodes.Add);
  2391. context.Emit(OpCodes.Ldc_I4_1);
  2392. context.Emit(OpCodes.Shr_Un);
  2393. });
  2394. }
  2395. }
  2396. public static void Usqadd_S(ILEmitterCtx context)
  2397. {
  2398. EmitScalarSaturatingBinaryOpZx(context, SaturatingFlags.Accumulate);
  2399. }
  2400. public static void Usqadd_V(ILEmitterCtx context)
  2401. {
  2402. EmitVectorSaturatingBinaryOpZx(context, SaturatingFlags.Accumulate);
  2403. }
  2404. public static void Usubl_V(ILEmitterCtx context)
  2405. {
  2406. if (Optimizations.UseSse41)
  2407. {
  2408. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2409. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2410. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2411. Type[] typesSub = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2412. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2413. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2414. nameof(Sse41.ConvertToVector128Int32),
  2415. nameof(Sse41.ConvertToVector128Int64) };
  2416. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  2417. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size);
  2418. context.EmitLdc_I4(numBytes);
  2419. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2420. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2421. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2422. context.EmitLdc_I4(numBytes);
  2423. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2424. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2425. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  2426. EmitStvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2427. }
  2428. else
  2429. {
  2430. EmitVectorWidenRnRmBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2431. }
  2432. }
  2433. public static void Usubw_V(ILEmitterCtx context)
  2434. {
  2435. if (Optimizations.UseSse41)
  2436. {
  2437. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2438. Type[] typesSrl = new Type[] { VectorUIntTypesPerSizeLog2[op.Size], typeof(byte) };
  2439. Type[] typesCvt = new Type[] { VectorUIntTypesPerSizeLog2[op.Size] };
  2440. Type[] typesSub = new Type[] { VectorUIntTypesPerSizeLog2[op.Size + 1],
  2441. VectorUIntTypesPerSizeLog2[op.Size + 1] };
  2442. string[] namesCvt = new string[] { nameof(Sse41.ConvertToVector128Int16),
  2443. nameof(Sse41.ConvertToVector128Int32),
  2444. nameof(Sse41.ConvertToVector128Int64) };
  2445. int numBytes = op.RegisterSize == RegisterSize.Simd128 ? 8 : 0;
  2446. EmitLdvecWithUnsignedCast(context, op.Rn, op.Size + 1);
  2447. EmitLdvecWithUnsignedCast(context, op.Rm, op.Size);
  2448. context.EmitLdc_I4(numBytes);
  2449. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.ShiftRightLogical128BitLane), typesSrl));
  2450. context.EmitCall(typeof(Sse41).GetMethod(namesCvt[op.Size], typesCvt));
  2451. context.EmitCall(typeof(Sse2).GetMethod(nameof(Sse2.Subtract), typesSub));
  2452. EmitStvecWithUnsignedCast(context, op.Rd, op.Size + 1);
  2453. }
  2454. else
  2455. {
  2456. EmitVectorWidenRmBinaryOpZx(context, () => context.Emit(OpCodes.Sub));
  2457. }
  2458. }
  2459. private static void EmitAbs(ILEmitterCtx context)
  2460. {
  2461. ILLabel lblTrue = new ILLabel();
  2462. context.Emit(OpCodes.Dup);
  2463. context.Emit(OpCodes.Ldc_I4_0);
  2464. context.Emit(OpCodes.Bge_S, lblTrue);
  2465. context.Emit(OpCodes.Neg);
  2466. context.MarkLabel(lblTrue);
  2467. }
  2468. private static void EmitAddLongPairwise(ILEmitterCtx context, bool signed, bool accumulate)
  2469. {
  2470. OpCodeSimd64 op = (OpCodeSimd64)context.CurrOp;
  2471. int words = op.GetBitsCount() >> 4;
  2472. int pairs = words >> op.Size;
  2473. for (int index = 0; index < pairs; index++)
  2474. {
  2475. int idx = index << 1;
  2476. EmitVectorExtract(context, op.Rn, idx, op.Size, signed);
  2477. EmitVectorExtract(context, op.Rn, idx + 1, op.Size, signed);
  2478. context.Emit(OpCodes.Add);
  2479. if (accumulate)
  2480. {
  2481. EmitVectorExtract(context, op.Rd, index, op.Size + 1, signed);
  2482. context.Emit(OpCodes.Add);
  2483. }
  2484. EmitVectorInsertTmp(context, index, op.Size + 1);
  2485. }
  2486. context.EmitLdvectmp();
  2487. context.EmitStvec(op.Rd);
  2488. if (op.RegisterSize == RegisterSize.Simd64)
  2489. {
  2490. EmitVectorZeroUpper(context, op.Rd);
  2491. }
  2492. }
  2493. private static void EmitDoublingMultiplyHighHalf(ILEmitterCtx context, bool round)
  2494. {
  2495. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2496. int eSize = 8 << op.Size;
  2497. context.Emit(OpCodes.Mul);
  2498. if (!round)
  2499. {
  2500. context.EmitAsr(eSize - 1);
  2501. }
  2502. else
  2503. {
  2504. long roundConst = 1L << (eSize - 1);
  2505. ILLabel lblTrue = new ILLabel();
  2506. context.EmitLsl(1);
  2507. context.EmitLdc_I8(roundConst);
  2508. context.Emit(OpCodes.Add);
  2509. context.EmitAsr(eSize);
  2510. context.Emit(OpCodes.Dup);
  2511. context.EmitLdc_I8((long)int.MinValue);
  2512. context.Emit(OpCodes.Bne_Un_S, lblTrue);
  2513. context.Emit(OpCodes.Neg);
  2514. context.MarkLabel(lblTrue);
  2515. }
  2516. }
  2517. private static void EmitHighNarrow(ILEmitterCtx context, Action emit, bool round)
  2518. {
  2519. OpCodeSimdReg64 op = (OpCodeSimdReg64)context.CurrOp;
  2520. int elems = 8 >> op.Size;
  2521. int eSize = 8 << op.Size;
  2522. int part = op.RegisterSize == RegisterSize.Simd128 ? elems : 0;
  2523. long roundConst = 1L << (eSize - 1);
  2524. if (part != 0)
  2525. {
  2526. context.EmitLdvec(op.Rd);
  2527. context.EmitStvectmp();
  2528. }
  2529. for (int index = 0; index < elems; index++)
  2530. {
  2531. EmitVectorExtractZx(context, op.Rn, index, op.Size + 1);
  2532. EmitVectorExtractZx(context, op.Rm, index, op.Size + 1);
  2533. emit();
  2534. if (round)
  2535. {
  2536. context.EmitLdc_I8(roundConst);
  2537. context.Emit(OpCodes.Add);
  2538. }
  2539. context.EmitLsr(eSize);
  2540. EmitVectorInsertTmp(context, part + index, op.Size);
  2541. }
  2542. context.EmitLdvectmp();
  2543. context.EmitStvec(op.Rd);
  2544. if (part == 0)
  2545. {
  2546. EmitVectorZeroUpper(context, op.Rd);
  2547. }
  2548. }
  2549. }
  2550. }