InstEmitSimdArithmetic.cs 131 KB

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