InstEmitSimdArithmetic.cs 125 KB

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