SoftFloat.cs 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735
  1. using ARMeilleure.State;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. namespace ARMeilleure.Instructions
  6. {
  7. static class SoftFloat
  8. {
  9. static SoftFloat()
  10. {
  11. RecipEstimateTable = BuildRecipEstimateTable();
  12. RecipSqrtEstimateTable = BuildRecipSqrtEstimateTable();
  13. }
  14. public static readonly byte[] RecipEstimateTable;
  15. public static readonly byte[] RecipSqrtEstimateTable;
  16. private static byte[] BuildRecipEstimateTable()
  17. {
  18. byte[] tbl = new byte[256];
  19. for (int idx = 0; idx < 256; idx++)
  20. {
  21. uint src = (uint)idx + 256u;
  22. Debug.Assert(256u <= src && src < 512u);
  23. src = (src << 1) + 1u;
  24. uint aux = (1u << 19) / src;
  25. uint dst = (aux + 1u) >> 1;
  26. Debug.Assert(256u <= dst && dst < 512u);
  27. tbl[idx] = (byte)(dst - 256u);
  28. }
  29. return tbl;
  30. }
  31. private static byte[] BuildRecipSqrtEstimateTable()
  32. {
  33. byte[] tbl = new byte[384];
  34. for (int idx = 0; idx < 384; idx++)
  35. {
  36. uint src = (uint)idx + 128u;
  37. Debug.Assert(128u <= src && src < 512u);
  38. if (src < 256u)
  39. {
  40. src = (src << 1) + 1u;
  41. }
  42. else
  43. {
  44. src = (src >> 1) << 1;
  45. src = (src + 1u) << 1;
  46. }
  47. uint aux = 512u;
  48. while (src * (aux + 1u) * (aux + 1u) < (1u << 28))
  49. {
  50. aux++;
  51. }
  52. uint dst = (aux + 1u) >> 1;
  53. Debug.Assert(256u <= dst && dst < 512u);
  54. tbl[idx] = (byte)(dst - 256u);
  55. }
  56. return tbl;
  57. }
  58. public static void FPProcessException(FPException exc, ExecutionContext context)
  59. {
  60. FPProcessException(exc, context, context.Fpcr);
  61. }
  62. public static void FPProcessException(FPException exc, ExecutionContext context, FPCR fpcr)
  63. {
  64. int enable = (int)exc + 8;
  65. if ((fpcr & (FPCR)(1 << enable)) != 0)
  66. {
  67. throw new NotImplementedException("Floating-point trap handling.");
  68. }
  69. else
  70. {
  71. context.Fpsr |= (FPSR)(1 << (int)exc);
  72. }
  73. }
  74. public static FPRoundingMode GetRoundingMode(this FPCR fpcr)
  75. {
  76. const int RModeShift = 22;
  77. return (FPRoundingMode)(((uint)fpcr >> RModeShift) & 3u);
  78. }
  79. }
  80. static class SoftFloat16
  81. {
  82. public static ushort FPDefaultNaN()
  83. {
  84. return (ushort)0x7E00u;
  85. }
  86. public static ushort FPInfinity(bool sign)
  87. {
  88. return sign ? (ushort)0xFC00u : (ushort)0x7C00u;
  89. }
  90. public static ushort FPZero(bool sign)
  91. {
  92. return sign ? (ushort)0x8000u : (ushort)0x0000u;
  93. }
  94. public static ushort FPMaxNormal(bool sign)
  95. {
  96. return sign ? (ushort)0xFBFFu : (ushort)0x7BFFu;
  97. }
  98. public static double FPUnpackCv(
  99. this ushort valueBits,
  100. out FPType type,
  101. out bool sign,
  102. ExecutionContext context)
  103. {
  104. sign = (~(uint)valueBits & 0x8000u) == 0u;
  105. uint exp16 = ((uint)valueBits & 0x7C00u) >> 10;
  106. uint frac16 = (uint)valueBits & 0x03FFu;
  107. double real;
  108. if (exp16 == 0u)
  109. {
  110. if (frac16 == 0u)
  111. {
  112. type = FPType.Zero;
  113. real = 0d;
  114. }
  115. else
  116. {
  117. type = FPType.Nonzero; // Subnormal.
  118. real = Math.Pow(2d, -14) * ((double)frac16 * Math.Pow(2d, -10));
  119. }
  120. }
  121. else if (exp16 == 0x1Fu && (context.Fpcr & FPCR.Ahp) == 0)
  122. {
  123. if (frac16 == 0u)
  124. {
  125. type = FPType.Infinity;
  126. real = Math.Pow(2d, 1000);
  127. }
  128. else
  129. {
  130. type = (~frac16 & 0x0200u) == 0u ? FPType.QNaN : FPType.SNaN;
  131. real = 0d;
  132. }
  133. }
  134. else
  135. {
  136. type = FPType.Nonzero; // Normal.
  137. real = Math.Pow(2d, (int)exp16 - 15) * (1d + (double)frac16 * Math.Pow(2d, -10));
  138. }
  139. return sign ? -real : real;
  140. }
  141. public static ushort FPRoundCv(double real, ExecutionContext context)
  142. {
  143. const int MinimumExp = -14;
  144. const int E = 5;
  145. const int F = 10;
  146. bool sign;
  147. double mantissa;
  148. if (real < 0d)
  149. {
  150. sign = true;
  151. mantissa = -real;
  152. }
  153. else
  154. {
  155. sign = false;
  156. mantissa = real;
  157. }
  158. int exponent = 0;
  159. while (mantissa < 1d)
  160. {
  161. mantissa *= 2d;
  162. exponent--;
  163. }
  164. while (mantissa >= 2d)
  165. {
  166. mantissa /= 2d;
  167. exponent++;
  168. }
  169. uint biasedExp = (uint)Math.Max(exponent - MinimumExp + 1, 0);
  170. if (biasedExp == 0u)
  171. {
  172. mantissa /= Math.Pow(2d, MinimumExp - exponent);
  173. }
  174. uint intMant = (uint)Math.Floor(mantissa * Math.Pow(2d, F));
  175. double error = mantissa * Math.Pow(2d, F) - (double)intMant;
  176. if (biasedExp == 0u && (error != 0d || (context.Fpcr & FPCR.Ufe) != 0))
  177. {
  178. SoftFloat.FPProcessException(FPException.Underflow, context);
  179. }
  180. bool overflowToInf;
  181. bool roundUp;
  182. switch (context.Fpcr.GetRoundingMode())
  183. {
  184. case FPRoundingMode.ToNearest:
  185. roundUp = (error > 0.5d || (error == 0.5d && (intMant & 1u) == 1u));
  186. overflowToInf = true;
  187. break;
  188. case FPRoundingMode.TowardsPlusInfinity:
  189. roundUp = (error != 0d && !sign);
  190. overflowToInf = !sign;
  191. break;
  192. case FPRoundingMode.TowardsMinusInfinity:
  193. roundUp = (error != 0d && sign);
  194. overflowToInf = sign;
  195. break;
  196. case FPRoundingMode.TowardsZero:
  197. roundUp = false;
  198. overflowToInf = false;
  199. break;
  200. default:
  201. throw new ArgumentException($"Invalid rounding mode \"{context.Fpcr.GetRoundingMode()}\".");
  202. }
  203. if (roundUp)
  204. {
  205. intMant++;
  206. if (intMant == 1u << F)
  207. {
  208. biasedExp = 1u;
  209. }
  210. if (intMant == 1u << (F + 1))
  211. {
  212. biasedExp++;
  213. intMant >>= 1;
  214. }
  215. }
  216. ushort resultBits;
  217. if ((context.Fpcr & FPCR.Ahp) == 0)
  218. {
  219. if (biasedExp >= (1u << E) - 1u)
  220. {
  221. resultBits = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  222. SoftFloat.FPProcessException(FPException.Overflow, context);
  223. error = 1d;
  224. }
  225. else
  226. {
  227. resultBits = (ushort)((sign ? 1u : 0u) << 15 | (biasedExp & 0x1Fu) << 10 | (intMant & 0x03FFu));
  228. }
  229. }
  230. else
  231. {
  232. if (biasedExp >= 1u << E)
  233. {
  234. resultBits = (ushort)((sign ? 1u : 0u) << 15 | 0x7FFFu);
  235. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  236. error = 0d;
  237. }
  238. else
  239. {
  240. resultBits = (ushort)((sign ? 1u : 0u) << 15 | (biasedExp & 0x1Fu) << 10 | (intMant & 0x03FFu));
  241. }
  242. }
  243. if (error != 0d)
  244. {
  245. SoftFloat.FPProcessException(FPException.Inexact, context);
  246. }
  247. return resultBits;
  248. }
  249. }
  250. static class SoftFloat16_32
  251. {
  252. [UnmanagedCallersOnly]
  253. public static float FPConvert(ushort valueBits)
  254. {
  255. ExecutionContext context = NativeInterface.GetContext();
  256. double real = valueBits.FPUnpackCv(out FPType type, out bool sign, context);
  257. float result;
  258. if (type == FPType.SNaN || type == FPType.QNaN)
  259. {
  260. if ((context.Fpcr & FPCR.Dn) != 0)
  261. {
  262. result = SoftFloat32.FPDefaultNaN();
  263. }
  264. else
  265. {
  266. result = FPConvertNaN(valueBits);
  267. }
  268. if (type == FPType.SNaN)
  269. {
  270. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  271. }
  272. }
  273. else if (type == FPType.Infinity)
  274. {
  275. result = SoftFloat32.FPInfinity(sign);
  276. }
  277. else if (type == FPType.Zero)
  278. {
  279. result = SoftFloat32.FPZero(sign);
  280. }
  281. else
  282. {
  283. result = FPRoundCv(real, context);
  284. }
  285. return result;
  286. }
  287. private static float FPRoundCv(double real, ExecutionContext context)
  288. {
  289. const int MinimumExp = -126;
  290. const int E = 8;
  291. const int F = 23;
  292. bool sign;
  293. double mantissa;
  294. if (real < 0d)
  295. {
  296. sign = true;
  297. mantissa = -real;
  298. }
  299. else
  300. {
  301. sign = false;
  302. mantissa = real;
  303. }
  304. int exponent = 0;
  305. while (mantissa < 1d)
  306. {
  307. mantissa *= 2d;
  308. exponent--;
  309. }
  310. while (mantissa >= 2d)
  311. {
  312. mantissa /= 2d;
  313. exponent++;
  314. }
  315. if ((context.Fpcr & FPCR.Fz) != 0 && exponent < MinimumExp)
  316. {
  317. context.Fpsr |= FPSR.Ufc;
  318. return SoftFloat32.FPZero(sign);
  319. }
  320. uint biasedExp = (uint)Math.Max(exponent - MinimumExp + 1, 0);
  321. if (biasedExp == 0u)
  322. {
  323. mantissa /= Math.Pow(2d, MinimumExp - exponent);
  324. }
  325. uint intMant = (uint)Math.Floor(mantissa * Math.Pow(2d, F));
  326. double error = mantissa * Math.Pow(2d, F) - (double)intMant;
  327. if (biasedExp == 0u && (error != 0d || (context.Fpcr & FPCR.Ufe) != 0))
  328. {
  329. SoftFloat.FPProcessException(FPException.Underflow, context);
  330. }
  331. bool overflowToInf;
  332. bool roundUp;
  333. switch (context.Fpcr.GetRoundingMode())
  334. {
  335. case FPRoundingMode.ToNearest:
  336. roundUp = (error > 0.5d || (error == 0.5d && (intMant & 1u) == 1u));
  337. overflowToInf = true;
  338. break;
  339. case FPRoundingMode.TowardsPlusInfinity:
  340. roundUp = (error != 0d && !sign);
  341. overflowToInf = !sign;
  342. break;
  343. case FPRoundingMode.TowardsMinusInfinity:
  344. roundUp = (error != 0d && sign);
  345. overflowToInf = sign;
  346. break;
  347. case FPRoundingMode.TowardsZero:
  348. roundUp = false;
  349. overflowToInf = false;
  350. break;
  351. default:
  352. throw new ArgumentException($"Invalid rounding mode \"{context.Fpcr.GetRoundingMode()}\".");
  353. }
  354. if (roundUp)
  355. {
  356. intMant++;
  357. if (intMant == 1u << F)
  358. {
  359. biasedExp = 1u;
  360. }
  361. if (intMant == 1u << (F + 1))
  362. {
  363. biasedExp++;
  364. intMant >>= 1;
  365. }
  366. }
  367. float result;
  368. if (biasedExp >= (1u << E) - 1u)
  369. {
  370. result = overflowToInf ? SoftFloat32.FPInfinity(sign) : SoftFloat32.FPMaxNormal(sign);
  371. SoftFloat.FPProcessException(FPException.Overflow, context);
  372. error = 1d;
  373. }
  374. else
  375. {
  376. result = BitConverter.Int32BitsToSingle(
  377. (int)((sign ? 1u : 0u) << 31 | (biasedExp & 0xFFu) << 23 | (intMant & 0x007FFFFFu)));
  378. }
  379. if (error != 0d)
  380. {
  381. SoftFloat.FPProcessException(FPException.Inexact, context);
  382. }
  383. return result;
  384. }
  385. private static float FPConvertNaN(ushort valueBits)
  386. {
  387. return BitConverter.Int32BitsToSingle(
  388. (int)(((uint)valueBits & 0x8000u) << 16 | 0x7FC00000u | ((uint)valueBits & 0x01FFu) << 13));
  389. }
  390. }
  391. static class SoftFloat16_64
  392. {
  393. [UnmanagedCallersOnly]
  394. public static double FPConvert(ushort valueBits)
  395. {
  396. ExecutionContext context = NativeInterface.GetContext();
  397. double real = valueBits.FPUnpackCv(out FPType type, out bool sign, context);
  398. double result;
  399. if (type == FPType.SNaN || type == FPType.QNaN)
  400. {
  401. if ((context.Fpcr & FPCR.Dn) != 0)
  402. {
  403. result = SoftFloat64.FPDefaultNaN();
  404. }
  405. else
  406. {
  407. result = FPConvertNaN(valueBits);
  408. }
  409. if (type == FPType.SNaN)
  410. {
  411. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  412. }
  413. }
  414. else if (type == FPType.Infinity)
  415. {
  416. result = SoftFloat64.FPInfinity(sign);
  417. }
  418. else if (type == FPType.Zero)
  419. {
  420. result = SoftFloat64.FPZero(sign);
  421. }
  422. else
  423. {
  424. result = FPRoundCv(real, context);
  425. }
  426. return result;
  427. }
  428. private static double FPRoundCv(double real, ExecutionContext context)
  429. {
  430. const int MinimumExp = -1022;
  431. const int E = 11;
  432. const int F = 52;
  433. bool sign;
  434. double mantissa;
  435. if (real < 0d)
  436. {
  437. sign = true;
  438. mantissa = -real;
  439. }
  440. else
  441. {
  442. sign = false;
  443. mantissa = real;
  444. }
  445. int exponent = 0;
  446. while (mantissa < 1d)
  447. {
  448. mantissa *= 2d;
  449. exponent--;
  450. }
  451. while (mantissa >= 2d)
  452. {
  453. mantissa /= 2d;
  454. exponent++;
  455. }
  456. if ((context.Fpcr & FPCR.Fz) != 0 && exponent < MinimumExp)
  457. {
  458. context.Fpsr |= FPSR.Ufc;
  459. return SoftFloat64.FPZero(sign);
  460. }
  461. uint biasedExp = (uint)Math.Max(exponent - MinimumExp + 1, 0);
  462. if (biasedExp == 0u)
  463. {
  464. mantissa /= Math.Pow(2d, MinimumExp - exponent);
  465. }
  466. ulong intMant = (ulong)Math.Floor(mantissa * Math.Pow(2d, F));
  467. double error = mantissa * Math.Pow(2d, F) - (double)intMant;
  468. if (biasedExp == 0u && (error != 0d || (context.Fpcr & FPCR.Ufe) != 0))
  469. {
  470. SoftFloat.FPProcessException(FPException.Underflow, context);
  471. }
  472. bool overflowToInf;
  473. bool roundUp;
  474. switch (context.Fpcr.GetRoundingMode())
  475. {
  476. case FPRoundingMode.ToNearest:
  477. roundUp = (error > 0.5d || (error == 0.5d && (intMant & 1u) == 1u));
  478. overflowToInf = true;
  479. break;
  480. case FPRoundingMode.TowardsPlusInfinity:
  481. roundUp = (error != 0d && !sign);
  482. overflowToInf = !sign;
  483. break;
  484. case FPRoundingMode.TowardsMinusInfinity:
  485. roundUp = (error != 0d && sign);
  486. overflowToInf = sign;
  487. break;
  488. case FPRoundingMode.TowardsZero:
  489. roundUp = false;
  490. overflowToInf = false;
  491. break;
  492. default:
  493. throw new ArgumentException($"Invalid rounding mode \"{context.Fpcr.GetRoundingMode()}\".");
  494. }
  495. if (roundUp)
  496. {
  497. intMant++;
  498. if (intMant == 1ul << F)
  499. {
  500. biasedExp = 1u;
  501. }
  502. if (intMant == 1ul << (F + 1))
  503. {
  504. biasedExp++;
  505. intMant >>= 1;
  506. }
  507. }
  508. double result;
  509. if (biasedExp >= (1u << E) - 1u)
  510. {
  511. result = overflowToInf ? SoftFloat64.FPInfinity(sign) : SoftFloat64.FPMaxNormal(sign);
  512. SoftFloat.FPProcessException(FPException.Overflow, context);
  513. error = 1d;
  514. }
  515. else
  516. {
  517. result = BitConverter.Int64BitsToDouble(
  518. (long)((sign ? 1ul : 0ul) << 63 | (biasedExp & 0x7FFul) << 52 | (intMant & 0x000FFFFFFFFFFFFFul)));
  519. }
  520. if (error != 0d)
  521. {
  522. SoftFloat.FPProcessException(FPException.Inexact, context);
  523. }
  524. return result;
  525. }
  526. private static double FPConvertNaN(ushort valueBits)
  527. {
  528. return BitConverter.Int64BitsToDouble(
  529. (long)(((ulong)valueBits & 0x8000ul) << 48 | 0x7FF8000000000000ul | ((ulong)valueBits & 0x01FFul) << 42));
  530. }
  531. }
  532. static class SoftFloat32_16
  533. {
  534. [UnmanagedCallersOnly]
  535. public static ushort FPConvert(float value)
  536. {
  537. ExecutionContext context = NativeInterface.GetContext();
  538. double real = value.FPUnpackCv(out FPType type, out bool sign, out uint valueBits, context);
  539. bool altHp = (context.Fpcr & FPCR.Ahp) != 0;
  540. ushort resultBits;
  541. if (type == FPType.SNaN || type == FPType.QNaN)
  542. {
  543. if (altHp)
  544. {
  545. resultBits = SoftFloat16.FPZero(sign);
  546. }
  547. else if ((context.Fpcr & FPCR.Dn) != 0)
  548. {
  549. resultBits = SoftFloat16.FPDefaultNaN();
  550. }
  551. else
  552. {
  553. resultBits = FPConvertNaN(valueBits);
  554. }
  555. if (type == FPType.SNaN || altHp)
  556. {
  557. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  558. }
  559. }
  560. else if (type == FPType.Infinity)
  561. {
  562. if (altHp)
  563. {
  564. resultBits = (ushort)((sign ? 1u : 0u) << 15 | 0x7FFFu);
  565. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  566. }
  567. else
  568. {
  569. resultBits = SoftFloat16.FPInfinity(sign);
  570. }
  571. }
  572. else if (type == FPType.Zero)
  573. {
  574. resultBits = SoftFloat16.FPZero(sign);
  575. }
  576. else
  577. {
  578. resultBits = SoftFloat16.FPRoundCv(real, context);
  579. }
  580. return resultBits;
  581. }
  582. private static double FPUnpackCv(
  583. this float value,
  584. out FPType type,
  585. out bool sign,
  586. out uint valueBits,
  587. ExecutionContext context)
  588. {
  589. valueBits = (uint)BitConverter.SingleToInt32Bits(value);
  590. sign = (~valueBits & 0x80000000u) == 0u;
  591. uint exp32 = (valueBits & 0x7F800000u) >> 23;
  592. uint frac32 = valueBits & 0x007FFFFFu;
  593. double real;
  594. if (exp32 == 0u)
  595. {
  596. if (frac32 == 0u || (context.Fpcr & FPCR.Fz) != 0)
  597. {
  598. type = FPType.Zero;
  599. real = 0d;
  600. if (frac32 != 0u)
  601. {
  602. SoftFloat.FPProcessException(FPException.InputDenorm, context);
  603. }
  604. }
  605. else
  606. {
  607. type = FPType.Nonzero; // Subnormal.
  608. real = Math.Pow(2d, -126) * ((double)frac32 * Math.Pow(2d, -23));
  609. }
  610. }
  611. else if (exp32 == 0xFFu)
  612. {
  613. if (frac32 == 0u)
  614. {
  615. type = FPType.Infinity;
  616. real = Math.Pow(2d, 1000);
  617. }
  618. else
  619. {
  620. type = (~frac32 & 0x00400000u) == 0u ? FPType.QNaN : FPType.SNaN;
  621. real = 0d;
  622. }
  623. }
  624. else
  625. {
  626. type = FPType.Nonzero; // Normal.
  627. real = Math.Pow(2d, (int)exp32 - 127) * (1d + (double)frac32 * Math.Pow(2d, -23));
  628. }
  629. return sign ? -real : real;
  630. }
  631. private static ushort FPConvertNaN(uint valueBits)
  632. {
  633. return (ushort)((valueBits & 0x80000000u) >> 16 | 0x7E00u | (valueBits & 0x003FE000u) >> 13);
  634. }
  635. }
  636. static class SoftFloat32
  637. {
  638. [UnmanagedCallersOnly]
  639. public static float FPAdd(float value1, float value2)
  640. {
  641. return FPAddFpscrImpl(value1, value2, false);
  642. }
  643. [UnmanagedCallersOnly]
  644. public static float FPAddFpscr(float value1, float value2, byte standardFpscr)
  645. {
  646. return FPAddFpscrImpl(value1, value2, standardFpscr == 1);
  647. }
  648. private static float FPAddFpscrImpl(float value1, float value2, bool standardFpscr)
  649. {
  650. ExecutionContext context = NativeInterface.GetContext();
  651. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  652. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  653. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  654. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  655. if (!done)
  656. {
  657. bool inf1 = type1 == FPType.Infinity;
  658. bool zero1 = type1 == FPType.Zero;
  659. bool inf2 = type2 == FPType.Infinity;
  660. bool zero2 = type2 == FPType.Zero;
  661. if (inf1 && inf2 && sign1 == !sign2)
  662. {
  663. result = FPDefaultNaN();
  664. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  665. }
  666. else if ((inf1 && !sign1) || (inf2 && !sign2))
  667. {
  668. result = FPInfinity(false);
  669. }
  670. else if ((inf1 && sign1) || (inf2 && sign2))
  671. {
  672. result = FPInfinity(true);
  673. }
  674. else if (zero1 && zero2 && sign1 == sign2)
  675. {
  676. result = FPZero(sign1);
  677. }
  678. else
  679. {
  680. result = value1 + value2;
  681. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  682. {
  683. context.Fpsr |= FPSR.Ufc;
  684. result = FPZero(result < 0f);
  685. }
  686. }
  687. }
  688. return result;
  689. }
  690. [UnmanagedCallersOnly]
  691. public static int FPCompare(float value1, float value2, byte signalNaNs)
  692. {
  693. ExecutionContext context = NativeInterface.GetContext();
  694. FPCR fpcr = context.Fpcr;
  695. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  696. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  697. int result;
  698. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  699. {
  700. result = 0b0011;
  701. if (type1 == FPType.SNaN || type2 == FPType.SNaN || signalNaNs == 1)
  702. {
  703. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  704. }
  705. }
  706. else
  707. {
  708. if (value1 == value2)
  709. {
  710. result = 0b0110;
  711. }
  712. else if (value1 < value2)
  713. {
  714. result = 0b1000;
  715. }
  716. else
  717. {
  718. result = 0b0010;
  719. }
  720. }
  721. return result;
  722. }
  723. [UnmanagedCallersOnly]
  724. public static float FPCompareEQ(float value1, float value2)
  725. {
  726. return FPCompareEQFpscrImpl(value1, value2, false);
  727. }
  728. private static float FPCompareEQFpscrImpl(float value1, float value2, bool standardFpscr)
  729. {
  730. ExecutionContext context = NativeInterface.GetContext();
  731. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  732. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  733. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  734. float result;
  735. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  736. {
  737. result = ZerosOrOnes(false);
  738. if (type1 == FPType.SNaN || type2 == FPType.SNaN)
  739. {
  740. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  741. }
  742. }
  743. else
  744. {
  745. result = ZerosOrOnes(value1 == value2);
  746. }
  747. return result;
  748. }
  749. [UnmanagedCallersOnly]
  750. public static float FPCompareEQFpscr(float value1, float value2, byte standardFpscr)
  751. {
  752. return FPCompareEQFpscrImpl(value1, value2, standardFpscr == 1);
  753. }
  754. [UnmanagedCallersOnly]
  755. public static float FPCompareGE(float value1, float value2)
  756. {
  757. return FPCompareGEFpscrImpl(value1, value2, false);
  758. }
  759. [UnmanagedCallersOnly]
  760. public static float FPCompareGEFpscr(float value1, float value2, byte standardFpscr)
  761. {
  762. return FPCompareGEFpscrImpl(value1, value2, standardFpscr == 1);
  763. }
  764. private static float FPCompareGEFpscrImpl(float value1, float value2, bool standardFpscr)
  765. {
  766. ExecutionContext context = NativeInterface.GetContext();
  767. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  768. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  769. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  770. float result;
  771. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  772. {
  773. result = ZerosOrOnes(false);
  774. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  775. }
  776. else
  777. {
  778. result = ZerosOrOnes(value1 >= value2);
  779. }
  780. return result;
  781. }
  782. [UnmanagedCallersOnly]
  783. public static float FPCompareGT(float value1, float value2)
  784. {
  785. return FPCompareGTFpscrImpl(value1, value2, false);
  786. }
  787. [UnmanagedCallersOnly]
  788. public static float FPCompareGTFpscr(float value1, float value2, byte standardFpscr)
  789. {
  790. return FPCompareGTFpscrImpl(value1, value2, standardFpscr == 1);
  791. }
  792. private static float FPCompareGTFpscrImpl(float value1, float value2, bool standardFpscr)
  793. {
  794. ExecutionContext context = NativeInterface.GetContext();
  795. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  796. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  797. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  798. float result;
  799. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  800. {
  801. result = ZerosOrOnes(false);
  802. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  803. }
  804. else
  805. {
  806. result = ZerosOrOnes(value1 > value2);
  807. }
  808. return result;
  809. }
  810. [UnmanagedCallersOnly]
  811. public static float FPCompareLE(float value1, float value2)
  812. {
  813. return FPCompareGEFpscrImpl(value2, value1, false);
  814. }
  815. [UnmanagedCallersOnly]
  816. public static float FPCompareLT(float value1, float value2)
  817. {
  818. return FPCompareGTFpscrImpl(value2, value1, false);
  819. }
  820. [UnmanagedCallersOnly]
  821. public static float FPCompareLEFpscr(float value1, float value2, byte standardFpscr)
  822. {
  823. return FPCompareGEFpscrImpl(value2, value1, standardFpscr == 1);
  824. }
  825. [UnmanagedCallersOnly]
  826. public static float FPCompareLTFpscr(float value1, float value2, byte standardFpscr)
  827. {
  828. return FPCompareGEFpscrImpl(value2, value1, standardFpscr == 1);
  829. }
  830. [UnmanagedCallersOnly]
  831. public static float FPDiv(float value1, float value2)
  832. {
  833. ExecutionContext context = NativeInterface.GetContext();
  834. FPCR fpcr = context.Fpcr;
  835. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  836. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  837. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  838. if (!done)
  839. {
  840. bool inf1 = type1 == FPType.Infinity;
  841. bool zero1 = type1 == FPType.Zero;
  842. bool inf2 = type2 == FPType.Infinity;
  843. bool zero2 = type2 == FPType.Zero;
  844. if ((inf1 && inf2) || (zero1 && zero2))
  845. {
  846. result = FPDefaultNaN();
  847. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  848. }
  849. else if (inf1 || zero2)
  850. {
  851. result = FPInfinity(sign1 ^ sign2);
  852. if (!inf1)
  853. {
  854. SoftFloat.FPProcessException(FPException.DivideByZero, context, fpcr);
  855. }
  856. }
  857. else if (zero1 || inf2)
  858. {
  859. result = FPZero(sign1 ^ sign2);
  860. }
  861. else
  862. {
  863. result = value1 / value2;
  864. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  865. {
  866. context.Fpsr |= FPSR.Ufc;
  867. result = FPZero(result < 0f);
  868. }
  869. }
  870. }
  871. return result;
  872. }
  873. [UnmanagedCallersOnly]
  874. public static float FPMax(float value1, float value2)
  875. {
  876. return FPMaxFpscrImpl(value1, value2, false);
  877. }
  878. [UnmanagedCallersOnly]
  879. public static float FPMaxFpscr(float value1, float value2, byte standardFpscr)
  880. {
  881. return FPMaxFpscrImpl(value1, value2, standardFpscr == 1);
  882. }
  883. private static float FPMaxFpscrImpl(float value1, float value2, bool standardFpscr)
  884. {
  885. ExecutionContext context = NativeInterface.GetContext();
  886. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  887. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  888. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  889. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  890. if (!done)
  891. {
  892. if (value1 > value2)
  893. {
  894. if (type1 == FPType.Infinity)
  895. {
  896. result = FPInfinity(sign1);
  897. }
  898. else if (type1 == FPType.Zero)
  899. {
  900. result = FPZero(sign1 && sign2);
  901. }
  902. else
  903. {
  904. result = value1;
  905. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  906. {
  907. context.Fpsr |= FPSR.Ufc;
  908. result = FPZero(result < 0f);
  909. }
  910. }
  911. }
  912. else
  913. {
  914. if (type2 == FPType.Infinity)
  915. {
  916. result = FPInfinity(sign2);
  917. }
  918. else if (type2 == FPType.Zero)
  919. {
  920. result = FPZero(sign1 && sign2);
  921. }
  922. else
  923. {
  924. result = value2;
  925. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  926. {
  927. context.Fpsr |= FPSR.Ufc;
  928. result = FPZero(result < 0f);
  929. }
  930. }
  931. }
  932. }
  933. return result;
  934. }
  935. [UnmanagedCallersOnly]
  936. public static float FPMaxNum(float value1, float value2)
  937. {
  938. return FPMaxNumFpscrImpl(value1, value2, false);
  939. }
  940. private static float FPMaxNumFpscrImpl(float value1, float value2, bool standardFpscr)
  941. {
  942. ExecutionContext context = NativeInterface.GetContext();
  943. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  944. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  945. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  946. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  947. {
  948. value1 = FPInfinity(true);
  949. }
  950. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  951. {
  952. value2 = FPInfinity(true);
  953. }
  954. return FPMaxFpscrImpl(value1, value2, standardFpscr);
  955. }
  956. [UnmanagedCallersOnly]
  957. public static float FPMaxNumFpscr(float value1, float value2, byte standardFpscr)
  958. {
  959. return FPMaxNumFpscrImpl(value1, value2, standardFpscr == 1);
  960. }
  961. [UnmanagedCallersOnly]
  962. public static float FPMin(float value1, float value2)
  963. {
  964. return FPMinFpscrImpl(value1, value2, false);
  965. }
  966. [UnmanagedCallersOnly]
  967. public static float FPMinFpscr(float value1, float value2, byte standardFpscr)
  968. {
  969. return FPMinFpscrImpl(value1, value2, standardFpscr == 1);
  970. }
  971. private static float FPMinFpscrImpl(float value1, float value2, bool standardFpscr)
  972. {
  973. ExecutionContext context = NativeInterface.GetContext();
  974. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  975. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  976. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  977. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  978. if (!done)
  979. {
  980. if (value1 < value2)
  981. {
  982. if (type1 == FPType.Infinity)
  983. {
  984. result = FPInfinity(sign1);
  985. }
  986. else if (type1 == FPType.Zero)
  987. {
  988. result = FPZero(sign1 || sign2);
  989. }
  990. else
  991. {
  992. result = value1;
  993. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  994. {
  995. context.Fpsr |= FPSR.Ufc;
  996. result = FPZero(result < 0f);
  997. }
  998. }
  999. }
  1000. else
  1001. {
  1002. if (type2 == FPType.Infinity)
  1003. {
  1004. result = FPInfinity(sign2);
  1005. }
  1006. else if (type2 == FPType.Zero)
  1007. {
  1008. result = FPZero(sign1 || sign2);
  1009. }
  1010. else
  1011. {
  1012. result = value2;
  1013. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1014. {
  1015. context.Fpsr |= FPSR.Ufc;
  1016. result = FPZero(result < 0f);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. return result;
  1022. }
  1023. [UnmanagedCallersOnly]
  1024. public static float FPMinNum(float value1, float value2)
  1025. {
  1026. return FPMinNumFpscrImpl(value1, value2, false);
  1027. }
  1028. [UnmanagedCallersOnly]
  1029. public static float FPMinNumFpscr(float value1, float value2, byte standardFpscr)
  1030. {
  1031. return FPMinNumFpscrImpl(value1, value2, standardFpscr == 1);
  1032. }
  1033. private static float FPMinNumFpscrImpl(float value1, float value2, bool standardFpscr)
  1034. {
  1035. ExecutionContext context = NativeInterface.GetContext();
  1036. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1037. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1038. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1039. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  1040. {
  1041. value1 = FPInfinity(false);
  1042. }
  1043. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  1044. {
  1045. value2 = FPInfinity(false);
  1046. }
  1047. return FPMinFpscrImpl(value1, value2, standardFpscr);
  1048. }
  1049. [UnmanagedCallersOnly]
  1050. public static float FPMul(float value1, float value2)
  1051. {
  1052. return FPMulFpscrImpl(value1, value2, false);
  1053. }
  1054. [UnmanagedCallersOnly]
  1055. public static float FPMulFpscr(float value1, float value2, byte standardFpscr)
  1056. {
  1057. return FPMulFpscrImpl(value1, value2, standardFpscr == 1);
  1058. }
  1059. private static float FPMulFpscrImpl(float value1, float value2, bool standardFpscr)
  1060. {
  1061. ExecutionContext context = NativeInterface.GetContext();
  1062. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1063. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1064. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1065. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1066. if (!done)
  1067. {
  1068. bool inf1 = type1 == FPType.Infinity;
  1069. bool zero1 = type1 == FPType.Zero;
  1070. bool inf2 = type2 == FPType.Infinity;
  1071. bool zero2 = type2 == FPType.Zero;
  1072. if ((inf1 && zero2) || (zero1 && inf2))
  1073. {
  1074. result = FPDefaultNaN();
  1075. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1076. }
  1077. else if (inf1 || inf2)
  1078. {
  1079. result = FPInfinity(sign1 ^ sign2);
  1080. }
  1081. else if (zero1 || zero2)
  1082. {
  1083. result = FPZero(sign1 ^ sign2);
  1084. }
  1085. else
  1086. {
  1087. result = value1 * value2;
  1088. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1089. {
  1090. context.Fpsr |= FPSR.Ufc;
  1091. result = FPZero(result < 0f);
  1092. }
  1093. }
  1094. }
  1095. return result;
  1096. }
  1097. [UnmanagedCallersOnly]
  1098. public static float FPMulAdd(float valueA, float value1, float value2)
  1099. {
  1100. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  1101. }
  1102. [UnmanagedCallersOnly]
  1103. public static float FPMulAddFpscr(float valueA, float value1, float value2, byte standardFpscr)
  1104. {
  1105. return FPMulAddFpscrImpl(valueA, value1, value2, standardFpscr == 1);
  1106. }
  1107. private static float FPMulAddFpscrImpl(float valueA, float value1, float value2, bool standardFpscr)
  1108. {
  1109. ExecutionContext context = NativeInterface.GetContext();
  1110. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1111. valueA = valueA.FPUnpack(out FPType typeA, out bool signA, out uint addend, context, fpcr);
  1112. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1113. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1114. bool inf1 = type1 == FPType.Infinity;
  1115. bool zero1 = type1 == FPType.Zero;
  1116. bool inf2 = type2 == FPType.Infinity;
  1117. bool zero2 = type2 == FPType.Zero;
  1118. float result = FPProcessNaNs3(typeA, type1, type2, addend, op1, op2, out bool done, context, fpcr);
  1119. if (typeA == FPType.QNaN && ((inf1 && zero2) || (zero1 && inf2)))
  1120. {
  1121. result = FPDefaultNaN();
  1122. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1123. }
  1124. if (!done)
  1125. {
  1126. bool infA = typeA == FPType.Infinity;
  1127. bool zeroA = typeA == FPType.Zero;
  1128. bool signP = sign1 ^ sign2;
  1129. bool infP = inf1 || inf2;
  1130. bool zeroP = zero1 || zero2;
  1131. if ((inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP))
  1132. {
  1133. result = FPDefaultNaN();
  1134. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1135. }
  1136. else if ((infA && !signA) || (infP && !signP))
  1137. {
  1138. result = FPInfinity(false);
  1139. }
  1140. else if ((infA && signA) || (infP && signP))
  1141. {
  1142. result = FPInfinity(true);
  1143. }
  1144. else if (zeroA && zeroP && signA == signP)
  1145. {
  1146. result = FPZero(signA);
  1147. }
  1148. else
  1149. {
  1150. result = MathF.FusedMultiplyAdd(value1, value2, valueA);
  1151. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1152. {
  1153. context.Fpsr |= FPSR.Ufc;
  1154. result = FPZero(result < 0f);
  1155. }
  1156. }
  1157. }
  1158. return result;
  1159. }
  1160. [UnmanagedCallersOnly]
  1161. public static float FPMulSub(float valueA, float value1, float value2)
  1162. {
  1163. value1 = value1.FPNeg();
  1164. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  1165. }
  1166. [UnmanagedCallersOnly]
  1167. public static float FPMulSubFpscr(float valueA, float value1, float value2, byte standardFpscr)
  1168. {
  1169. value1 = value1.FPNeg();
  1170. return FPMulAddFpscrImpl(valueA, value1, value2, standardFpscr == 1);
  1171. }
  1172. [UnmanagedCallersOnly]
  1173. public static float FPMulX(float value1, float value2)
  1174. {
  1175. ExecutionContext context = NativeInterface.GetContext();
  1176. FPCR fpcr = context.Fpcr;
  1177. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1178. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1179. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1180. if (!done)
  1181. {
  1182. bool inf1 = type1 == FPType.Infinity;
  1183. bool zero1 = type1 == FPType.Zero;
  1184. bool inf2 = type2 == FPType.Infinity;
  1185. bool zero2 = type2 == FPType.Zero;
  1186. if ((inf1 && zero2) || (zero1 && inf2))
  1187. {
  1188. result = FPTwo(sign1 ^ sign2);
  1189. }
  1190. else if (inf1 || inf2)
  1191. {
  1192. result = FPInfinity(sign1 ^ sign2);
  1193. }
  1194. else if (zero1 || zero2)
  1195. {
  1196. result = FPZero(sign1 ^ sign2);
  1197. }
  1198. else
  1199. {
  1200. result = value1 * value2;
  1201. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1202. {
  1203. context.Fpsr |= FPSR.Ufc;
  1204. result = FPZero(result < 0f);
  1205. }
  1206. }
  1207. }
  1208. return result;
  1209. }
  1210. [UnmanagedCallersOnly]
  1211. public static float FPNegMulAdd(float valueA, float value1, float value2)
  1212. {
  1213. valueA = valueA.FPNeg();
  1214. value1 = value1.FPNeg();
  1215. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  1216. }
  1217. [UnmanagedCallersOnly]
  1218. public static float FPNegMulSub(float valueA, float value1, float value2)
  1219. {
  1220. valueA = valueA.FPNeg();
  1221. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  1222. }
  1223. [UnmanagedCallersOnly]
  1224. public static float FPRecipEstimate(float value)
  1225. {
  1226. return FPRecipEstimateFpscrImpl(value, false);
  1227. }
  1228. [UnmanagedCallersOnly]
  1229. public static float FPRecipEstimateFpscr(float value, byte standardFpscr)
  1230. {
  1231. return FPRecipEstimateFpscrImpl(value, standardFpscr == 1);
  1232. }
  1233. private static float FPRecipEstimateFpscrImpl(float value, bool standardFpscr)
  1234. {
  1235. ExecutionContext context = NativeInterface.GetContext();
  1236. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1237. value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1238. float result;
  1239. if (type == FPType.SNaN || type == FPType.QNaN)
  1240. {
  1241. result = FPProcessNaN(type, op, context, fpcr);
  1242. }
  1243. else if (type == FPType.Infinity)
  1244. {
  1245. result = FPZero(sign);
  1246. }
  1247. else if (type == FPType.Zero)
  1248. {
  1249. result = FPInfinity(sign);
  1250. SoftFloat.FPProcessException(FPException.DivideByZero, context, fpcr);
  1251. }
  1252. else if (MathF.Abs(value) < MathF.Pow(2f, -128))
  1253. {
  1254. var overflowToInf = fpcr.GetRoundingMode() switch
  1255. {
  1256. FPRoundingMode.ToNearest => true,
  1257. FPRoundingMode.TowardsPlusInfinity => !sign,
  1258. FPRoundingMode.TowardsMinusInfinity => sign,
  1259. FPRoundingMode.TowardsZero => false,
  1260. _ => throw new ArgumentException($"Invalid rounding mode \"{fpcr.GetRoundingMode()}\"."),
  1261. };
  1262. result = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  1263. SoftFloat.FPProcessException(FPException.Overflow, context, fpcr);
  1264. SoftFloat.FPProcessException(FPException.Inexact, context, fpcr);
  1265. }
  1266. else if ((fpcr & FPCR.Fz) != 0 && (MathF.Abs(value) >= MathF.Pow(2f, 126)))
  1267. {
  1268. result = FPZero(sign);
  1269. context.Fpsr |= FPSR.Ufc;
  1270. }
  1271. else
  1272. {
  1273. ulong fraction = (ulong)(op & 0x007FFFFFu) << 29;
  1274. uint exp = (op & 0x7F800000u) >> 23;
  1275. if (exp == 0u)
  1276. {
  1277. if ((fraction & 0x0008000000000000ul) == 0ul)
  1278. {
  1279. fraction = (fraction & 0x0003FFFFFFFFFFFFul) << 2;
  1280. exp -= 1u;
  1281. }
  1282. else
  1283. {
  1284. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1285. }
  1286. }
  1287. uint scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1288. uint resultExp = 253u - exp;
  1289. uint estimate = (uint)SoftFloat.RecipEstimateTable[scaled - 256u] + 256u;
  1290. fraction = (ulong)(estimate & 0xFFu) << 44;
  1291. if (resultExp == 0u)
  1292. {
  1293. fraction = ((fraction & 0x000FFFFFFFFFFFFEul) | 0x0010000000000000ul) >> 1;
  1294. }
  1295. else if (resultExp + 1u == 0u)
  1296. {
  1297. fraction = ((fraction & 0x000FFFFFFFFFFFFCul) | 0x0010000000000000ul) >> 2;
  1298. resultExp = 0u;
  1299. }
  1300. result = BitConverter.Int32BitsToSingle(
  1301. (int)((sign ? 1u : 0u) << 31 | (resultExp & 0xFFu) << 23 | (uint)(fraction >> 29) & 0x007FFFFFu));
  1302. }
  1303. return result;
  1304. }
  1305. [UnmanagedCallersOnly]
  1306. public static float FPRecipStep(float value1, float value2)
  1307. {
  1308. ExecutionContext context = NativeInterface.GetContext();
  1309. FPCR fpcr = context.StandardFpcrValue;
  1310. value1 = value1.FPUnpack(out FPType type1, out _, out uint op1, context, fpcr);
  1311. value2 = value2.FPUnpack(out FPType type2, out _, out uint op2, context, fpcr);
  1312. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1313. if (!done)
  1314. {
  1315. bool inf1 = type1 == FPType.Infinity;
  1316. bool zero1 = type1 == FPType.Zero;
  1317. bool inf2 = type2 == FPType.Infinity;
  1318. bool zero2 = type2 == FPType.Zero;
  1319. float product;
  1320. if ((inf1 && zero2) || (zero1 && inf2))
  1321. {
  1322. product = FPZero(false);
  1323. }
  1324. else
  1325. {
  1326. product = FPMulFpscrImpl(value1, value2, true);
  1327. }
  1328. result = FPSubFpscrImpl(FPTwo(false), product, true);
  1329. }
  1330. return result;
  1331. }
  1332. [UnmanagedCallersOnly]
  1333. public static float FPRecipStepFused(float value1, float value2)
  1334. {
  1335. ExecutionContext context = NativeInterface.GetContext();
  1336. FPCR fpcr = context.Fpcr;
  1337. value1 = value1.FPNeg();
  1338. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1339. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1340. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1341. if (!done)
  1342. {
  1343. bool inf1 = type1 == FPType.Infinity;
  1344. bool zero1 = type1 == FPType.Zero;
  1345. bool inf2 = type2 == FPType.Infinity;
  1346. bool zero2 = type2 == FPType.Zero;
  1347. if ((inf1 && zero2) || (zero1 && inf2))
  1348. {
  1349. result = FPTwo(false);
  1350. }
  1351. else if (inf1 || inf2)
  1352. {
  1353. result = FPInfinity(sign1 ^ sign2);
  1354. }
  1355. else
  1356. {
  1357. result = MathF.FusedMultiplyAdd(value1, value2, 2f);
  1358. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1359. {
  1360. context.Fpsr |= FPSR.Ufc;
  1361. result = FPZero(result < 0f);
  1362. }
  1363. }
  1364. }
  1365. return result;
  1366. }
  1367. [UnmanagedCallersOnly]
  1368. public static float FPRecpX(float value)
  1369. {
  1370. ExecutionContext context = NativeInterface.GetContext();
  1371. FPCR fpcr = context.Fpcr;
  1372. value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1373. float result;
  1374. if (type == FPType.SNaN || type == FPType.QNaN)
  1375. {
  1376. result = FPProcessNaN(type, op, context, fpcr);
  1377. }
  1378. else
  1379. {
  1380. uint notExp = (~op >> 23) & 0xFFu;
  1381. uint maxExp = 0xFEu;
  1382. result = BitConverter.Int32BitsToSingle(
  1383. (int)((sign ? 1u : 0u) << 31 | (notExp == 0xFFu ? maxExp : notExp) << 23));
  1384. }
  1385. return result;
  1386. }
  1387. [UnmanagedCallersOnly]
  1388. public static float FPRSqrtEstimate(float value)
  1389. {
  1390. return FPRSqrtEstimateFpscrImpl(value, false);
  1391. }
  1392. [UnmanagedCallersOnly]
  1393. public static float FPRSqrtEstimateFpscr(float value, byte standardFpscr)
  1394. {
  1395. return FPRSqrtEstimateFpscrImpl(value, standardFpscr == 1);
  1396. }
  1397. private static float FPRSqrtEstimateFpscrImpl(float value, bool standardFpscr)
  1398. {
  1399. ExecutionContext context = NativeInterface.GetContext();
  1400. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1401. value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1402. float result;
  1403. if (type == FPType.SNaN || type == FPType.QNaN)
  1404. {
  1405. result = FPProcessNaN(type, op, context, fpcr);
  1406. }
  1407. else if (type == FPType.Zero)
  1408. {
  1409. result = FPInfinity(sign);
  1410. SoftFloat.FPProcessException(FPException.DivideByZero, context, fpcr);
  1411. }
  1412. else if (sign)
  1413. {
  1414. result = FPDefaultNaN();
  1415. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1416. }
  1417. else if (type == FPType.Infinity)
  1418. {
  1419. result = FPZero(false);
  1420. }
  1421. else
  1422. {
  1423. ulong fraction = (ulong)(op & 0x007FFFFFu) << 29;
  1424. uint exp = (op & 0x7F800000u) >> 23;
  1425. if (exp == 0u)
  1426. {
  1427. while ((fraction & 0x0008000000000000ul) == 0ul)
  1428. {
  1429. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1430. exp -= 1u;
  1431. }
  1432. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1433. }
  1434. uint scaled;
  1435. if ((exp & 1u) == 0u)
  1436. {
  1437. scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1438. }
  1439. else
  1440. {
  1441. scaled = (uint)(((fraction & 0x000FE00000000000ul) | 0x0010000000000000ul) >> 45);
  1442. }
  1443. uint resultExp = (380u - exp) >> 1;
  1444. uint estimate = (uint)SoftFloat.RecipSqrtEstimateTable[scaled - 128u] + 256u;
  1445. result = BitConverter.Int32BitsToSingle((int)((resultExp & 0xFFu) << 23 | (estimate & 0xFFu) << 15));
  1446. }
  1447. return result;
  1448. }
  1449. public static float FPHalvedSub(float value1, float value2, ExecutionContext context, FPCR fpcr)
  1450. {
  1451. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1452. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1453. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1454. if (!done)
  1455. {
  1456. bool inf1 = type1 == FPType.Infinity;
  1457. bool zero1 = type1 == FPType.Zero;
  1458. bool inf2 = type2 == FPType.Infinity;
  1459. bool zero2 = type2 == FPType.Zero;
  1460. if (inf1 && inf2 && sign1 == sign2)
  1461. {
  1462. result = FPDefaultNaN();
  1463. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1464. }
  1465. else if ((inf1 && !sign1) || (inf2 && sign2))
  1466. {
  1467. result = FPInfinity(false);
  1468. }
  1469. else if ((inf1 && sign1) || (inf2 && !sign2))
  1470. {
  1471. result = FPInfinity(true);
  1472. }
  1473. else if (zero1 && zero2 && sign1 == !sign2)
  1474. {
  1475. result = FPZero(sign1);
  1476. }
  1477. else
  1478. {
  1479. result = (value1 - value2) / 2.0f;
  1480. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1481. {
  1482. context.Fpsr |= FPSR.Ufc;
  1483. result = FPZero(result < 0f);
  1484. }
  1485. }
  1486. }
  1487. return result;
  1488. }
  1489. [UnmanagedCallersOnly]
  1490. public static float FPRSqrtStep(float value1, float value2)
  1491. {
  1492. ExecutionContext context = NativeInterface.GetContext();
  1493. FPCR fpcr = context.StandardFpcrValue;
  1494. value1 = value1.FPUnpack(out FPType type1, out _, out uint op1, context, fpcr);
  1495. value2 = value2.FPUnpack(out FPType type2, out _, out uint op2, context, fpcr);
  1496. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1497. if (!done)
  1498. {
  1499. bool inf1 = type1 == FPType.Infinity;
  1500. bool zero1 = type1 == FPType.Zero;
  1501. bool inf2 = type2 == FPType.Infinity;
  1502. bool zero2 = type2 == FPType.Zero;
  1503. float product;
  1504. if ((inf1 && zero2) || (zero1 && inf2))
  1505. {
  1506. product = FPZero(false);
  1507. }
  1508. else
  1509. {
  1510. product = FPMulFpscrImpl(value1, value2, true);
  1511. }
  1512. result = FPHalvedSub(FPThree(false), product, context, fpcr);
  1513. }
  1514. return result;
  1515. }
  1516. [UnmanagedCallersOnly]
  1517. public static float FPRSqrtStepFused(float value1, float value2)
  1518. {
  1519. ExecutionContext context = NativeInterface.GetContext();
  1520. FPCR fpcr = context.Fpcr;
  1521. value1 = value1.FPNeg();
  1522. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1523. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1524. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1525. if (!done)
  1526. {
  1527. bool inf1 = type1 == FPType.Infinity;
  1528. bool zero1 = type1 == FPType.Zero;
  1529. bool inf2 = type2 == FPType.Infinity;
  1530. bool zero2 = type2 == FPType.Zero;
  1531. if ((inf1 && zero2) || (zero1 && inf2))
  1532. {
  1533. result = FPOnePointFive(false);
  1534. }
  1535. else if (inf1 || inf2)
  1536. {
  1537. result = FPInfinity(sign1 ^ sign2);
  1538. }
  1539. else
  1540. {
  1541. result = MathF.FusedMultiplyAdd(value1, value2, 3f) / 2f;
  1542. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1543. {
  1544. context.Fpsr |= FPSR.Ufc;
  1545. result = FPZero(result < 0f);
  1546. }
  1547. }
  1548. }
  1549. return result;
  1550. }
  1551. [UnmanagedCallersOnly]
  1552. public static float FPSqrt(float value)
  1553. {
  1554. ExecutionContext context = NativeInterface.GetContext();
  1555. FPCR fpcr = context.Fpcr;
  1556. value = value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1557. float result;
  1558. if (type == FPType.SNaN || type == FPType.QNaN)
  1559. {
  1560. result = FPProcessNaN(type, op, context, fpcr);
  1561. }
  1562. else if (type == FPType.Zero)
  1563. {
  1564. result = FPZero(sign);
  1565. }
  1566. else if (type == FPType.Infinity && !sign)
  1567. {
  1568. result = FPInfinity(sign);
  1569. }
  1570. else if (sign)
  1571. {
  1572. result = FPDefaultNaN();
  1573. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1574. }
  1575. else
  1576. {
  1577. result = MathF.Sqrt(value);
  1578. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1579. {
  1580. context.Fpsr |= FPSR.Ufc;
  1581. result = FPZero(result < 0f);
  1582. }
  1583. }
  1584. return result;
  1585. }
  1586. [UnmanagedCallersOnly]
  1587. public static float FPSub(float value1, float value2)
  1588. {
  1589. return FPSubFpscrImpl(value1, value2, false);
  1590. }
  1591. private static float FPSubFpscrImpl(float value1, float value2, bool standardFpscr)
  1592. {
  1593. ExecutionContext context = NativeInterface.GetContext();
  1594. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1595. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1596. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1597. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1598. if (!done)
  1599. {
  1600. bool inf1 = type1 == FPType.Infinity;
  1601. bool zero1 = type1 == FPType.Zero;
  1602. bool inf2 = type2 == FPType.Infinity;
  1603. bool zero2 = type2 == FPType.Zero;
  1604. if (inf1 && inf2 && sign1 == sign2)
  1605. {
  1606. result = FPDefaultNaN();
  1607. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1608. }
  1609. else if ((inf1 && !sign1) || (inf2 && sign2))
  1610. {
  1611. result = FPInfinity(false);
  1612. }
  1613. else if ((inf1 && sign1) || (inf2 && !sign2))
  1614. {
  1615. result = FPInfinity(true);
  1616. }
  1617. else if (zero1 && zero2 && sign1 == !sign2)
  1618. {
  1619. result = FPZero(sign1);
  1620. }
  1621. else
  1622. {
  1623. result = value1 - value2;
  1624. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1625. {
  1626. context.Fpsr |= FPSR.Ufc;
  1627. result = FPZero(result < 0f);
  1628. }
  1629. }
  1630. }
  1631. return result;
  1632. }
  1633. public static float FPDefaultNaN()
  1634. {
  1635. return BitConverter.Int32BitsToSingle(0x7fc00000);
  1636. }
  1637. public static float FPInfinity(bool sign)
  1638. {
  1639. return sign ? float.NegativeInfinity : float.PositiveInfinity;
  1640. }
  1641. public static float FPZero(bool sign)
  1642. {
  1643. return sign ? -0f : +0f;
  1644. }
  1645. public static float FPMaxNormal(bool sign)
  1646. {
  1647. return sign ? float.MinValue : float.MaxValue;
  1648. }
  1649. private static float FPTwo(bool sign)
  1650. {
  1651. return sign ? -2f : +2f;
  1652. }
  1653. private static float FPThree(bool sign)
  1654. {
  1655. return sign ? -3f : +3f;
  1656. }
  1657. private static float FPOnePointFive(bool sign)
  1658. {
  1659. return sign ? -1.5f : +1.5f;
  1660. }
  1661. private static float FPNeg(this float value)
  1662. {
  1663. return -value;
  1664. }
  1665. private static float ZerosOrOnes(bool ones)
  1666. {
  1667. return BitConverter.Int32BitsToSingle(ones ? -1 : 0);
  1668. }
  1669. private static float FPUnpack(
  1670. this float value,
  1671. out FPType type,
  1672. out bool sign,
  1673. out uint valueBits,
  1674. ExecutionContext context,
  1675. FPCR fpcr)
  1676. {
  1677. valueBits = (uint)BitConverter.SingleToInt32Bits(value);
  1678. sign = (~valueBits & 0x80000000u) == 0u;
  1679. if ((valueBits & 0x7F800000u) == 0u)
  1680. {
  1681. if ((valueBits & 0x007FFFFFu) == 0u || (fpcr & FPCR.Fz) != 0)
  1682. {
  1683. type = FPType.Zero;
  1684. value = FPZero(sign);
  1685. if ((valueBits & 0x007FFFFFu) != 0u)
  1686. {
  1687. SoftFloat.FPProcessException(FPException.InputDenorm, context, fpcr);
  1688. }
  1689. }
  1690. else
  1691. {
  1692. type = FPType.Nonzero;
  1693. }
  1694. }
  1695. else if ((~valueBits & 0x7F800000u) == 0u)
  1696. {
  1697. if ((valueBits & 0x007FFFFFu) == 0u)
  1698. {
  1699. type = FPType.Infinity;
  1700. }
  1701. else
  1702. {
  1703. type = (~valueBits & 0x00400000u) == 0u ? FPType.QNaN : FPType.SNaN;
  1704. value = FPZero(sign);
  1705. }
  1706. }
  1707. else
  1708. {
  1709. type = FPType.Nonzero;
  1710. }
  1711. return value;
  1712. }
  1713. private static float FPProcessNaNs(
  1714. FPType type1,
  1715. FPType type2,
  1716. uint op1,
  1717. uint op2,
  1718. out bool done,
  1719. ExecutionContext context,
  1720. FPCR fpcr)
  1721. {
  1722. done = true;
  1723. if (type1 == FPType.SNaN)
  1724. {
  1725. return FPProcessNaN(type1, op1, context, fpcr);
  1726. }
  1727. else if (type2 == FPType.SNaN)
  1728. {
  1729. return FPProcessNaN(type2, op2, context, fpcr);
  1730. }
  1731. else if (type1 == FPType.QNaN)
  1732. {
  1733. return FPProcessNaN(type1, op1, context, fpcr);
  1734. }
  1735. else if (type2 == FPType.QNaN)
  1736. {
  1737. return FPProcessNaN(type2, op2, context, fpcr);
  1738. }
  1739. done = false;
  1740. return FPZero(false);
  1741. }
  1742. private static float FPProcessNaNs3(
  1743. FPType type1,
  1744. FPType type2,
  1745. FPType type3,
  1746. uint op1,
  1747. uint op2,
  1748. uint op3,
  1749. out bool done,
  1750. ExecutionContext context,
  1751. FPCR fpcr)
  1752. {
  1753. done = true;
  1754. if (type1 == FPType.SNaN)
  1755. {
  1756. return FPProcessNaN(type1, op1, context, fpcr);
  1757. }
  1758. else if (type2 == FPType.SNaN)
  1759. {
  1760. return FPProcessNaN(type2, op2, context, fpcr);
  1761. }
  1762. else if (type3 == FPType.SNaN)
  1763. {
  1764. return FPProcessNaN(type3, op3, context, fpcr);
  1765. }
  1766. else if (type1 == FPType.QNaN)
  1767. {
  1768. return FPProcessNaN(type1, op1, context, fpcr);
  1769. }
  1770. else if (type2 == FPType.QNaN)
  1771. {
  1772. return FPProcessNaN(type2, op2, context, fpcr);
  1773. }
  1774. else if (type3 == FPType.QNaN)
  1775. {
  1776. return FPProcessNaN(type3, op3, context, fpcr);
  1777. }
  1778. done = false;
  1779. return FPZero(false);
  1780. }
  1781. private static float FPProcessNaN(FPType type, uint op, ExecutionContext context, FPCR fpcr)
  1782. {
  1783. if (type == FPType.SNaN)
  1784. {
  1785. op |= 1u << 22;
  1786. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1787. }
  1788. if ((fpcr & FPCR.Dn) != 0)
  1789. {
  1790. return FPDefaultNaN();
  1791. }
  1792. return BitConverter.Int32BitsToSingle((int)op);
  1793. }
  1794. }
  1795. static class SoftFloat64_16
  1796. {
  1797. [UnmanagedCallersOnly]
  1798. public static ushort FPConvert(double value)
  1799. {
  1800. ExecutionContext context = NativeInterface.GetContext();
  1801. double real = value.FPUnpackCv(out FPType type, out bool sign, out ulong valueBits, context);
  1802. bool altHp = (context.Fpcr & FPCR.Ahp) != 0;
  1803. ushort resultBits;
  1804. if (type == FPType.SNaN || type == FPType.QNaN)
  1805. {
  1806. if (altHp)
  1807. {
  1808. resultBits = SoftFloat16.FPZero(sign);
  1809. }
  1810. else if ((context.Fpcr & FPCR.Dn) != 0)
  1811. {
  1812. resultBits = SoftFloat16.FPDefaultNaN();
  1813. }
  1814. else
  1815. {
  1816. resultBits = FPConvertNaN(valueBits);
  1817. }
  1818. if (type == FPType.SNaN || altHp)
  1819. {
  1820. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  1821. }
  1822. }
  1823. else if (type == FPType.Infinity)
  1824. {
  1825. if (altHp)
  1826. {
  1827. resultBits = (ushort)((sign ? 1u : 0u) << 15 | 0x7FFFu);
  1828. SoftFloat.FPProcessException(FPException.InvalidOp, context);
  1829. }
  1830. else
  1831. {
  1832. resultBits = SoftFloat16.FPInfinity(sign);
  1833. }
  1834. }
  1835. else if (type == FPType.Zero)
  1836. {
  1837. resultBits = SoftFloat16.FPZero(sign);
  1838. }
  1839. else
  1840. {
  1841. resultBits = SoftFloat16.FPRoundCv(real, context);
  1842. }
  1843. return resultBits;
  1844. }
  1845. private static double FPUnpackCv(
  1846. this double value,
  1847. out FPType type,
  1848. out bool sign,
  1849. out ulong valueBits,
  1850. ExecutionContext context)
  1851. {
  1852. valueBits = (ulong)BitConverter.DoubleToInt64Bits(value);
  1853. sign = (~valueBits & 0x8000000000000000ul) == 0u;
  1854. ulong exp64 = (valueBits & 0x7FF0000000000000ul) >> 52;
  1855. ulong frac64 = valueBits & 0x000FFFFFFFFFFFFFul;
  1856. double real;
  1857. if (exp64 == 0u)
  1858. {
  1859. if (frac64 == 0u || (context.Fpcr & FPCR.Fz) != 0)
  1860. {
  1861. type = FPType.Zero;
  1862. real = 0d;
  1863. if (frac64 != 0u)
  1864. {
  1865. SoftFloat.FPProcessException(FPException.InputDenorm, context);
  1866. }
  1867. }
  1868. else
  1869. {
  1870. type = FPType.Nonzero; // Subnormal.
  1871. real = Math.Pow(2d, -1022) * ((double)frac64 * Math.Pow(2d, -52));
  1872. }
  1873. }
  1874. else if (exp64 == 0x7FFul)
  1875. {
  1876. if (frac64 == 0u)
  1877. {
  1878. type = FPType.Infinity;
  1879. real = Math.Pow(2d, 1000000);
  1880. }
  1881. else
  1882. {
  1883. type = (~frac64 & 0x0008000000000000ul) == 0u ? FPType.QNaN : FPType.SNaN;
  1884. real = 0d;
  1885. }
  1886. }
  1887. else
  1888. {
  1889. type = FPType.Nonzero; // Normal.
  1890. real = Math.Pow(2d, (int)exp64 - 1023) * (1d + (double)frac64 * Math.Pow(2d, -52));
  1891. }
  1892. return sign ? -real : real;
  1893. }
  1894. private static ushort FPConvertNaN(ulong valueBits)
  1895. {
  1896. return (ushort)((valueBits & 0x8000000000000000ul) >> 48 | 0x7E00u | (valueBits & 0x0007FC0000000000ul) >> 42);
  1897. }
  1898. }
  1899. static class SoftFloat64
  1900. {
  1901. [UnmanagedCallersOnly]
  1902. public static double FPAdd(double value1, double value2)
  1903. {
  1904. return FPAddFpscrImpl(value1, value2, false);
  1905. }
  1906. [UnmanagedCallersOnly]
  1907. public static double FPAddFpscr(double value1, double value2, byte standardFpscr)
  1908. {
  1909. return FPAddFpscrImpl(value1, value2, standardFpscr == 1);
  1910. }
  1911. private static double FPAddFpscrImpl(double value1, double value2, bool standardFpscr)
  1912. {
  1913. ExecutionContext context = NativeInterface.GetContext();
  1914. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1915. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1916. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1917. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1918. if (!done)
  1919. {
  1920. bool inf1 = type1 == FPType.Infinity;
  1921. bool zero1 = type1 == FPType.Zero;
  1922. bool inf2 = type2 == FPType.Infinity;
  1923. bool zero2 = type2 == FPType.Zero;
  1924. if (inf1 && inf2 && sign1 == !sign2)
  1925. {
  1926. result = FPDefaultNaN();
  1927. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1928. }
  1929. else if ((inf1 && !sign1) || (inf2 && !sign2))
  1930. {
  1931. result = FPInfinity(false);
  1932. }
  1933. else if ((inf1 && sign1) || (inf2 && sign2))
  1934. {
  1935. result = FPInfinity(true);
  1936. }
  1937. else if (zero1 && zero2 && sign1 == sign2)
  1938. {
  1939. result = FPZero(sign1);
  1940. }
  1941. else
  1942. {
  1943. result = value1 + value2;
  1944. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1945. {
  1946. context.Fpsr |= FPSR.Ufc;
  1947. result = FPZero(result < 0d);
  1948. }
  1949. }
  1950. }
  1951. return result;
  1952. }
  1953. [UnmanagedCallersOnly]
  1954. public static int FPCompare(double value1, double value2, byte signalNaNs)
  1955. {
  1956. ExecutionContext context = NativeInterface.GetContext();
  1957. FPCR fpcr = context.Fpcr;
  1958. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1959. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1960. int result;
  1961. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1962. {
  1963. result = 0b0011;
  1964. if (type1 == FPType.SNaN || type2 == FPType.SNaN || signalNaNs == 1)
  1965. {
  1966. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  1967. }
  1968. }
  1969. else
  1970. {
  1971. if (value1 == value2)
  1972. {
  1973. result = 0b0110;
  1974. }
  1975. else if (value1 < value2)
  1976. {
  1977. result = 0b1000;
  1978. }
  1979. else
  1980. {
  1981. result = 0b0010;
  1982. }
  1983. }
  1984. return result;
  1985. }
  1986. [UnmanagedCallersOnly]
  1987. public static double FPCompareEQ(double value1, double value2)
  1988. {
  1989. return FPCompareEQFpscrImpl(value1, value2, false);
  1990. }
  1991. [UnmanagedCallersOnly]
  1992. public static double FPCompareEQFpscr(double value1, double value2, byte standardFpscr)
  1993. {
  1994. return FPCompareEQFpscrImpl(value1, value2, standardFpscr == 1);
  1995. }
  1996. private static double FPCompareEQFpscrImpl(double value1, double value2, bool standardFpscr)
  1997. {
  1998. ExecutionContext context = NativeInterface.GetContext();
  1999. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2000. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  2001. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  2002. double result;
  2003. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  2004. {
  2005. result = ZerosOrOnes(false);
  2006. if (type1 == FPType.SNaN || type2 == FPType.SNaN)
  2007. {
  2008. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2009. }
  2010. }
  2011. else
  2012. {
  2013. result = ZerosOrOnes(value1 == value2);
  2014. }
  2015. return result;
  2016. }
  2017. [UnmanagedCallersOnly]
  2018. public static double FPCompareGE(double value1, double value2)
  2019. {
  2020. return FPCompareGEFpscrImpl(value1, value2, false);
  2021. }
  2022. [UnmanagedCallersOnly]
  2023. public static double FPCompareGEFpscr(double value1, double value2, byte standardFpscr)
  2024. {
  2025. return FPCompareGEFpscrImpl(value1, value2, standardFpscr == 1);
  2026. }
  2027. private static double FPCompareGEFpscrImpl(double value1, double value2, bool standardFpscr)
  2028. {
  2029. ExecutionContext context = NativeInterface.GetContext();
  2030. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2031. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  2032. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  2033. double result;
  2034. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  2035. {
  2036. result = ZerosOrOnes(false);
  2037. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2038. }
  2039. else
  2040. {
  2041. result = ZerosOrOnes(value1 >= value2);
  2042. }
  2043. return result;
  2044. }
  2045. [UnmanagedCallersOnly]
  2046. public static double FPCompareGT(double value1, double value2)
  2047. {
  2048. return FPCompareGTFpscrImpl(value1, value2, false);
  2049. }
  2050. [UnmanagedCallersOnly]
  2051. public static double FPCompareGTFpscr(double value1, double value2, byte standardFpscr)
  2052. {
  2053. return FPCompareGTFpscrImpl(value1, value2, standardFpscr == 1);
  2054. }
  2055. private static double FPCompareGTFpscrImpl(double value1, double value2, bool standardFpscr)
  2056. {
  2057. ExecutionContext context = NativeInterface.GetContext();
  2058. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2059. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  2060. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  2061. double result;
  2062. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  2063. {
  2064. result = ZerosOrOnes(false);
  2065. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2066. }
  2067. else
  2068. {
  2069. result = ZerosOrOnes(value1 > value2);
  2070. }
  2071. return result;
  2072. }
  2073. [UnmanagedCallersOnly]
  2074. public static double FPCompareLE(double value1, double value2)
  2075. {
  2076. return FPCompareGEFpscrImpl(value2, value1, false);
  2077. }
  2078. [UnmanagedCallersOnly]
  2079. public static double FPCompareLT(double value1, double value2)
  2080. {
  2081. return FPCompareGTFpscrImpl(value2, value1, false);
  2082. }
  2083. [UnmanagedCallersOnly]
  2084. public static double FPCompareLEFpscr(double value1, double value2, byte standardFpscr)
  2085. {
  2086. return FPCompareGEFpscrImpl(value2, value1, standardFpscr == 1);
  2087. }
  2088. [UnmanagedCallersOnly]
  2089. public static double FPCompareLTFpscr(double value1, double value2, byte standardFpscr)
  2090. {
  2091. return FPCompareGTFpscrImpl(value2, value1, standardFpscr == 1);
  2092. }
  2093. [UnmanagedCallersOnly]
  2094. public static double FPDiv(double value1, double value2)
  2095. {
  2096. ExecutionContext context = NativeInterface.GetContext();
  2097. FPCR fpcr = context.Fpcr;
  2098. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2099. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2100. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2101. if (!done)
  2102. {
  2103. bool inf1 = type1 == FPType.Infinity;
  2104. bool zero1 = type1 == FPType.Zero;
  2105. bool inf2 = type2 == FPType.Infinity;
  2106. bool zero2 = type2 == FPType.Zero;
  2107. if ((inf1 && inf2) || (zero1 && zero2))
  2108. {
  2109. result = FPDefaultNaN();
  2110. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2111. }
  2112. else if (inf1 || zero2)
  2113. {
  2114. result = FPInfinity(sign1 ^ sign2);
  2115. if (!inf1)
  2116. {
  2117. SoftFloat.FPProcessException(FPException.DivideByZero, context, fpcr);
  2118. }
  2119. }
  2120. else if (zero1 || inf2)
  2121. {
  2122. result = FPZero(sign1 ^ sign2);
  2123. }
  2124. else
  2125. {
  2126. result = value1 / value2;
  2127. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2128. {
  2129. context.Fpsr |= FPSR.Ufc;
  2130. result = FPZero(result < 0d);
  2131. }
  2132. }
  2133. }
  2134. return result;
  2135. }
  2136. [UnmanagedCallersOnly]
  2137. public static double FPMax(double value1, double value2)
  2138. {
  2139. return FPMaxFpscrImpl(value1, value2, false);
  2140. }
  2141. [UnmanagedCallersOnly]
  2142. public static double FPMaxFpscr(double value1, double value2, byte standardFpscr)
  2143. {
  2144. return FPMaxFpscrImpl(value1, value2, standardFpscr == 1);
  2145. }
  2146. private static double FPMaxFpscrImpl(double value1, double value2, bool standardFpscr)
  2147. {
  2148. ExecutionContext context = NativeInterface.GetContext();
  2149. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2150. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2151. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2152. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2153. if (!done)
  2154. {
  2155. if (value1 > value2)
  2156. {
  2157. if (type1 == FPType.Infinity)
  2158. {
  2159. result = FPInfinity(sign1);
  2160. }
  2161. else if (type1 == FPType.Zero)
  2162. {
  2163. result = FPZero(sign1 && sign2);
  2164. }
  2165. else
  2166. {
  2167. result = value1;
  2168. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2169. {
  2170. context.Fpsr |= FPSR.Ufc;
  2171. result = FPZero(result < 0d);
  2172. }
  2173. }
  2174. }
  2175. else
  2176. {
  2177. if (type2 == FPType.Infinity)
  2178. {
  2179. result = FPInfinity(sign2);
  2180. }
  2181. else if (type2 == FPType.Zero)
  2182. {
  2183. result = FPZero(sign1 && sign2);
  2184. }
  2185. else
  2186. {
  2187. result = value2;
  2188. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2189. {
  2190. context.Fpsr |= FPSR.Ufc;
  2191. result = FPZero(result < 0d);
  2192. }
  2193. }
  2194. }
  2195. }
  2196. return result;
  2197. }
  2198. [UnmanagedCallersOnly]
  2199. public static double FPMaxNum(double value1, double value2)
  2200. {
  2201. return FPMaxNumFpscrImpl(value1, value2, false);
  2202. }
  2203. [UnmanagedCallersOnly]
  2204. public static double FPMaxNumFpscr(double value1, double value2, byte standardFpscr)
  2205. {
  2206. return FPMaxNumFpscrImpl(value1, value2, standardFpscr == 1);
  2207. }
  2208. private static double FPMaxNumFpscrImpl(double value1, double value2, bool standardFpscr)
  2209. {
  2210. ExecutionContext context = NativeInterface.GetContext();
  2211. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2212. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  2213. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  2214. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  2215. {
  2216. value1 = FPInfinity(true);
  2217. }
  2218. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  2219. {
  2220. value2 = FPInfinity(true);
  2221. }
  2222. return FPMaxFpscrImpl(value1, value2, standardFpscr);
  2223. }
  2224. [UnmanagedCallersOnly]
  2225. public static double FPMin(double value1, double value2)
  2226. {
  2227. return FPMinFpscrImpl(value1, value2, false);
  2228. }
  2229. [UnmanagedCallersOnly]
  2230. public static double FPMinFpscr(double value1, double value2, byte standardFpscr)
  2231. {
  2232. return FPMinFpscrImpl(value1, value2, standardFpscr == 1);
  2233. }
  2234. private static double FPMinFpscrImpl(double value1, double value2, bool standardFpscr)
  2235. {
  2236. ExecutionContext context = NativeInterface.GetContext();
  2237. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2238. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2239. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2240. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2241. if (!done)
  2242. {
  2243. if (value1 < value2)
  2244. {
  2245. if (type1 == FPType.Infinity)
  2246. {
  2247. result = FPInfinity(sign1);
  2248. }
  2249. else if (type1 == FPType.Zero)
  2250. {
  2251. result = FPZero(sign1 || sign2);
  2252. }
  2253. else
  2254. {
  2255. result = value1;
  2256. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2257. {
  2258. context.Fpsr |= FPSR.Ufc;
  2259. result = FPZero(result < 0d);
  2260. }
  2261. }
  2262. }
  2263. else
  2264. {
  2265. if (type2 == FPType.Infinity)
  2266. {
  2267. result = FPInfinity(sign2);
  2268. }
  2269. else if (type2 == FPType.Zero)
  2270. {
  2271. result = FPZero(sign1 || sign2);
  2272. }
  2273. else
  2274. {
  2275. result = value2;
  2276. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2277. {
  2278. context.Fpsr |= FPSR.Ufc;
  2279. result = FPZero(result < 0d);
  2280. }
  2281. }
  2282. }
  2283. }
  2284. return result;
  2285. }
  2286. [UnmanagedCallersOnly]
  2287. public static double FPMinNum(double value1, double value2)
  2288. {
  2289. return FPMinNumFpscrImpl(value1, value2, false);
  2290. }
  2291. [UnmanagedCallersOnly]
  2292. public static double FPMinNumFpscr(double value1, double value2, byte standardFpscr)
  2293. {
  2294. return FPMinNumFpscrImpl(value1, value2, standardFpscr == 1);
  2295. }
  2296. private static double FPMinNumFpscrImpl(double value1, double value2, bool standardFpscr)
  2297. {
  2298. ExecutionContext context = NativeInterface.GetContext();
  2299. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2300. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  2301. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  2302. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  2303. {
  2304. value1 = FPInfinity(false);
  2305. }
  2306. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  2307. {
  2308. value2 = FPInfinity(false);
  2309. }
  2310. return FPMinFpscrImpl(value1, value2, standardFpscr);
  2311. }
  2312. [UnmanagedCallersOnly]
  2313. public static double FPMul(double value1, double value2)
  2314. {
  2315. return FPMulFpscrImpl(value1, value2, false);
  2316. }
  2317. [UnmanagedCallersOnly]
  2318. public static double FPMulFpscr(double value1, double value2, byte standardFpscr)
  2319. {
  2320. return FPMulFpscrImpl(value1, value2, standardFpscr == 1);
  2321. }
  2322. private static double FPMulFpscrImpl(double value1, double value2, bool standardFpscr)
  2323. {
  2324. ExecutionContext context = NativeInterface.GetContext();
  2325. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2326. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2327. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2328. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2329. if (!done)
  2330. {
  2331. bool inf1 = type1 == FPType.Infinity;
  2332. bool zero1 = type1 == FPType.Zero;
  2333. bool inf2 = type2 == FPType.Infinity;
  2334. bool zero2 = type2 == FPType.Zero;
  2335. if ((inf1 && zero2) || (zero1 && inf2))
  2336. {
  2337. result = FPDefaultNaN();
  2338. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2339. }
  2340. else if (inf1 || inf2)
  2341. {
  2342. result = FPInfinity(sign1 ^ sign2);
  2343. }
  2344. else if (zero1 || zero2)
  2345. {
  2346. result = FPZero(sign1 ^ sign2);
  2347. }
  2348. else
  2349. {
  2350. result = value1 * value2;
  2351. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2352. {
  2353. context.Fpsr |= FPSR.Ufc;
  2354. result = FPZero(result < 0d);
  2355. }
  2356. }
  2357. }
  2358. return result;
  2359. }
  2360. [UnmanagedCallersOnly]
  2361. public static double FPMulAdd(double valueA, double value1, double value2)
  2362. {
  2363. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  2364. }
  2365. [UnmanagedCallersOnly]
  2366. public static double FPMulAddFpscr(double valueA, double value1, double value2, byte standardFpscr)
  2367. {
  2368. return FPMulAddFpscrImpl(valueA, value1, value2, standardFpscr == 1);
  2369. }
  2370. private static double FPMulAddFpscrImpl(double valueA, double value1, double value2, bool standardFpscr)
  2371. {
  2372. ExecutionContext context = NativeInterface.GetContext();
  2373. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2374. valueA = valueA.FPUnpack(out FPType typeA, out bool signA, out ulong addend, context, fpcr);
  2375. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2376. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2377. bool inf1 = type1 == FPType.Infinity;
  2378. bool zero1 = type1 == FPType.Zero;
  2379. bool inf2 = type2 == FPType.Infinity;
  2380. bool zero2 = type2 == FPType.Zero;
  2381. double result = FPProcessNaNs3(typeA, type1, type2, addend, op1, op2, out bool done, context, fpcr);
  2382. if (typeA == FPType.QNaN && ((inf1 && zero2) || (zero1 && inf2)))
  2383. {
  2384. result = FPDefaultNaN();
  2385. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2386. }
  2387. if (!done)
  2388. {
  2389. bool infA = typeA == FPType.Infinity;
  2390. bool zeroA = typeA == FPType.Zero;
  2391. bool signP = sign1 ^ sign2;
  2392. bool infP = inf1 || inf2;
  2393. bool zeroP = zero1 || zero2;
  2394. if ((inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP))
  2395. {
  2396. result = FPDefaultNaN();
  2397. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2398. }
  2399. else if ((infA && !signA) || (infP && !signP))
  2400. {
  2401. result = FPInfinity(false);
  2402. }
  2403. else if ((infA && signA) || (infP && signP))
  2404. {
  2405. result = FPInfinity(true);
  2406. }
  2407. else if (zeroA && zeroP && signA == signP)
  2408. {
  2409. result = FPZero(signA);
  2410. }
  2411. else
  2412. {
  2413. result = Math.FusedMultiplyAdd(value1, value2, valueA);
  2414. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2415. {
  2416. context.Fpsr |= FPSR.Ufc;
  2417. result = FPZero(result < 0d);
  2418. }
  2419. }
  2420. }
  2421. return result;
  2422. }
  2423. [UnmanagedCallersOnly]
  2424. public static double FPMulSub(double valueA, double value1, double value2)
  2425. {
  2426. value1 = value1.FPNeg();
  2427. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  2428. }
  2429. [UnmanagedCallersOnly]
  2430. public static double FPMulSubFpscr(double valueA, double value1, double value2, byte standardFpscr)
  2431. {
  2432. value1 = value1.FPNeg();
  2433. return FPMulAddFpscrImpl(valueA, value1, value2, standardFpscr == 1);
  2434. }
  2435. [UnmanagedCallersOnly]
  2436. public static double FPMulX(double value1, double value2)
  2437. {
  2438. ExecutionContext context = NativeInterface.GetContext();
  2439. FPCR fpcr = context.Fpcr;
  2440. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2441. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2442. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2443. if (!done)
  2444. {
  2445. bool inf1 = type1 == FPType.Infinity;
  2446. bool zero1 = type1 == FPType.Zero;
  2447. bool inf2 = type2 == FPType.Infinity;
  2448. bool zero2 = type2 == FPType.Zero;
  2449. if ((inf1 && zero2) || (zero1 && inf2))
  2450. {
  2451. result = FPTwo(sign1 ^ sign2);
  2452. }
  2453. else if (inf1 || inf2)
  2454. {
  2455. result = FPInfinity(sign1 ^ sign2);
  2456. }
  2457. else if (zero1 || zero2)
  2458. {
  2459. result = FPZero(sign1 ^ sign2);
  2460. }
  2461. else
  2462. {
  2463. result = value1 * value2;
  2464. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2465. {
  2466. context.Fpsr |= FPSR.Ufc;
  2467. result = FPZero(result < 0d);
  2468. }
  2469. }
  2470. }
  2471. return result;
  2472. }
  2473. [UnmanagedCallersOnly]
  2474. public static double FPNegMulAdd(double valueA, double value1, double value2)
  2475. {
  2476. valueA = valueA.FPNeg();
  2477. value1 = value1.FPNeg();
  2478. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  2479. }
  2480. [UnmanagedCallersOnly]
  2481. public static double FPNegMulSub(double valueA, double value1, double value2)
  2482. {
  2483. valueA = valueA.FPNeg();
  2484. return FPMulAddFpscrImpl(valueA, value1, value2, false);
  2485. }
  2486. [UnmanagedCallersOnly]
  2487. public static double FPRecipEstimate(double value)
  2488. {
  2489. return FPRecipEstimateFpscrImpl(value, false);
  2490. }
  2491. [UnmanagedCallersOnly]
  2492. public static double FPRecipEstimateFpscr(double value, byte standardFpscr)
  2493. {
  2494. return FPRecipEstimateFpscrImpl(value, standardFpscr == 1);
  2495. }
  2496. private static double FPRecipEstimateFpscrImpl(double value, bool standardFpscr)
  2497. {
  2498. ExecutionContext context = NativeInterface.GetContext();
  2499. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2500. value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2501. double result;
  2502. if (type == FPType.SNaN || type == FPType.QNaN)
  2503. {
  2504. result = FPProcessNaN(type, op, context, fpcr);
  2505. }
  2506. else if (type == FPType.Infinity)
  2507. {
  2508. result = FPZero(sign);
  2509. }
  2510. else if (type == FPType.Zero)
  2511. {
  2512. result = FPInfinity(sign);
  2513. SoftFloat.FPProcessException(FPException.DivideByZero, context, fpcr);
  2514. }
  2515. else if (Math.Abs(value) < Math.Pow(2d, -1024))
  2516. {
  2517. var overflowToInf = fpcr.GetRoundingMode() switch
  2518. {
  2519. FPRoundingMode.ToNearest => true,
  2520. FPRoundingMode.TowardsPlusInfinity => !sign,
  2521. FPRoundingMode.TowardsMinusInfinity => sign,
  2522. FPRoundingMode.TowardsZero => false,
  2523. _ => throw new ArgumentException($"Invalid rounding mode \"{fpcr.GetRoundingMode()}\"."),
  2524. };
  2525. result = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  2526. SoftFloat.FPProcessException(FPException.Overflow, context, fpcr);
  2527. SoftFloat.FPProcessException(FPException.Inexact, context, fpcr);
  2528. }
  2529. else if ((fpcr & FPCR.Fz) != 0 && (Math.Abs(value) >= Math.Pow(2d, 1022)))
  2530. {
  2531. result = FPZero(sign);
  2532. context.Fpsr |= FPSR.Ufc;
  2533. }
  2534. else
  2535. {
  2536. ulong fraction = op & 0x000FFFFFFFFFFFFFul;
  2537. uint exp = (uint)((op & 0x7FF0000000000000ul) >> 52);
  2538. if (exp == 0u)
  2539. {
  2540. if ((fraction & 0x0008000000000000ul) == 0ul)
  2541. {
  2542. fraction = (fraction & 0x0003FFFFFFFFFFFFul) << 2;
  2543. exp -= 1u;
  2544. }
  2545. else
  2546. {
  2547. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  2548. }
  2549. }
  2550. uint scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  2551. uint resultExp = 2045u - exp;
  2552. uint estimate = (uint)SoftFloat.RecipEstimateTable[scaled - 256u] + 256u;
  2553. fraction = (ulong)(estimate & 0xFFu) << 44;
  2554. if (resultExp == 0u)
  2555. {
  2556. fraction = ((fraction & 0x000FFFFFFFFFFFFEul) | 0x0010000000000000ul) >> 1;
  2557. }
  2558. else if (resultExp + 1u == 0u)
  2559. {
  2560. fraction = ((fraction & 0x000FFFFFFFFFFFFCul) | 0x0010000000000000ul) >> 2;
  2561. resultExp = 0u;
  2562. }
  2563. result = BitConverter.Int64BitsToDouble(
  2564. (long)((sign ? 1ul : 0ul) << 63 | (resultExp & 0x7FFul) << 52 | (fraction & 0x000FFFFFFFFFFFFFul)));
  2565. }
  2566. return result;
  2567. }
  2568. [UnmanagedCallersOnly]
  2569. public static double FPRecipStep(double value1, double value2)
  2570. {
  2571. ExecutionContext context = NativeInterface.GetContext();
  2572. FPCR fpcr = context.StandardFpcrValue;
  2573. value1 = value1.FPUnpack(out FPType type1, out _, out ulong op1, context, fpcr);
  2574. value2 = value2.FPUnpack(out FPType type2, out _, out ulong op2, context, fpcr);
  2575. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2576. if (!done)
  2577. {
  2578. bool inf1 = type1 == FPType.Infinity;
  2579. bool zero1 = type1 == FPType.Zero;
  2580. bool inf2 = type2 == FPType.Infinity;
  2581. bool zero2 = type2 == FPType.Zero;
  2582. double product;
  2583. if ((inf1 && zero2) || (zero1 && inf2))
  2584. {
  2585. product = FPZero(false);
  2586. }
  2587. else
  2588. {
  2589. product = FPMulFpscrImpl(value1, value2, true);
  2590. }
  2591. result = FPSubFpscr(FPTwo(false), product, true);
  2592. }
  2593. return result;
  2594. }
  2595. [UnmanagedCallersOnly]
  2596. public static double FPRecipStepFused(double value1, double value2)
  2597. {
  2598. ExecutionContext context = NativeInterface.GetContext();
  2599. FPCR fpcr = context.Fpcr;
  2600. value1 = value1.FPNeg();
  2601. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2602. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2603. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2604. if (!done)
  2605. {
  2606. bool inf1 = type1 == FPType.Infinity;
  2607. bool zero1 = type1 == FPType.Zero;
  2608. bool inf2 = type2 == FPType.Infinity;
  2609. bool zero2 = type2 == FPType.Zero;
  2610. if ((inf1 && zero2) || (zero1 && inf2))
  2611. {
  2612. result = FPTwo(false);
  2613. }
  2614. else if (inf1 || inf2)
  2615. {
  2616. result = FPInfinity(sign1 ^ sign2);
  2617. }
  2618. else
  2619. {
  2620. result = Math.FusedMultiplyAdd(value1, value2, 2d);
  2621. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2622. {
  2623. context.Fpsr |= FPSR.Ufc;
  2624. result = FPZero(result < 0d);
  2625. }
  2626. }
  2627. }
  2628. return result;
  2629. }
  2630. [UnmanagedCallersOnly]
  2631. public static double FPRecpX(double value)
  2632. {
  2633. ExecutionContext context = NativeInterface.GetContext();
  2634. FPCR fpcr = context.Fpcr;
  2635. value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2636. double result;
  2637. if (type == FPType.SNaN || type == FPType.QNaN)
  2638. {
  2639. result = FPProcessNaN(type, op, context, fpcr);
  2640. }
  2641. else
  2642. {
  2643. ulong notExp = (~op >> 52) & 0x7FFul;
  2644. ulong maxExp = 0x7FEul;
  2645. result = BitConverter.Int64BitsToDouble(
  2646. (long)((sign ? 1ul : 0ul) << 63 | (notExp == 0x7FFul ? maxExp : notExp) << 52));
  2647. }
  2648. return result;
  2649. }
  2650. [UnmanagedCallersOnly]
  2651. public static double FPRSqrtEstimate(double value)
  2652. {
  2653. return FPRSqrtEstimateFpscrImpl(value, false);
  2654. }
  2655. [UnmanagedCallersOnly]
  2656. public static double FPRSqrtEstimateFpscr(double value, byte standardFpscr)
  2657. {
  2658. return FPRSqrtEstimateFpscrImpl(value, standardFpscr == 1);
  2659. }
  2660. private static double FPRSqrtEstimateFpscrImpl(double value, bool standardFpscr)
  2661. {
  2662. ExecutionContext context = NativeInterface.GetContext();
  2663. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2664. value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2665. double result;
  2666. if (type == FPType.SNaN || type == FPType.QNaN)
  2667. {
  2668. result = FPProcessNaN(type, op, context, fpcr);
  2669. }
  2670. else if (type == FPType.Zero)
  2671. {
  2672. result = FPInfinity(sign);
  2673. SoftFloat.FPProcessException(FPException.DivideByZero, context, fpcr);
  2674. }
  2675. else if (sign)
  2676. {
  2677. result = FPDefaultNaN();
  2678. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2679. }
  2680. else if (type == FPType.Infinity)
  2681. {
  2682. result = FPZero(false);
  2683. }
  2684. else
  2685. {
  2686. ulong fraction = op & 0x000FFFFFFFFFFFFFul;
  2687. uint exp = (uint)((op & 0x7FF0000000000000ul) >> 52);
  2688. if (exp == 0u)
  2689. {
  2690. while ((fraction & 0x0008000000000000ul) == 0ul)
  2691. {
  2692. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  2693. exp -= 1u;
  2694. }
  2695. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  2696. }
  2697. uint scaled;
  2698. if ((exp & 1u) == 0u)
  2699. {
  2700. scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  2701. }
  2702. else
  2703. {
  2704. scaled = (uint)(((fraction & 0x000FE00000000000ul) | 0x0010000000000000ul) >> 45);
  2705. }
  2706. uint resultExp = (3068u - exp) >> 1;
  2707. uint estimate = (uint)SoftFloat.RecipSqrtEstimateTable[scaled - 128u] + 256u;
  2708. result = BitConverter.Int64BitsToDouble((long)((resultExp & 0x7FFul) << 52 | (estimate & 0xFFul) << 44));
  2709. }
  2710. return result;
  2711. }
  2712. public static double FPHalvedSub(double value1, double value2, ExecutionContext context, FPCR fpcr)
  2713. {
  2714. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2715. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2716. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2717. if (!done)
  2718. {
  2719. bool inf1 = type1 == FPType.Infinity;
  2720. bool zero1 = type1 == FPType.Zero;
  2721. bool inf2 = type2 == FPType.Infinity;
  2722. bool zero2 = type2 == FPType.Zero;
  2723. if (inf1 && inf2 && sign1 == sign2)
  2724. {
  2725. result = FPDefaultNaN();
  2726. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2727. }
  2728. else if ((inf1 && !sign1) || (inf2 && sign2))
  2729. {
  2730. result = FPInfinity(false);
  2731. }
  2732. else if ((inf1 && sign1) || (inf2 && !sign2))
  2733. {
  2734. result = FPInfinity(true);
  2735. }
  2736. else if (zero1 && zero2 && sign1 == !sign2)
  2737. {
  2738. result = FPZero(sign1);
  2739. }
  2740. else
  2741. {
  2742. result = (value1 - value2) / 2.0;
  2743. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2744. {
  2745. context.Fpsr |= FPSR.Ufc;
  2746. result = FPZero(result < 0d);
  2747. }
  2748. }
  2749. }
  2750. return result;
  2751. }
  2752. [UnmanagedCallersOnly]
  2753. public static double FPRSqrtStep(double value1, double value2)
  2754. {
  2755. ExecutionContext context = NativeInterface.GetContext();
  2756. FPCR fpcr = context.StandardFpcrValue;
  2757. value1 = value1.FPUnpack(out FPType type1, out _, out ulong op1, context, fpcr);
  2758. value2 = value2.FPUnpack(out FPType type2, out _, out ulong op2, context, fpcr);
  2759. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2760. if (!done)
  2761. {
  2762. bool inf1 = type1 == FPType.Infinity;
  2763. bool zero1 = type1 == FPType.Zero;
  2764. bool inf2 = type2 == FPType.Infinity;
  2765. bool zero2 = type2 == FPType.Zero;
  2766. double product;
  2767. if ((inf1 && zero2) || (zero1 && inf2))
  2768. {
  2769. product = FPZero(false);
  2770. }
  2771. else
  2772. {
  2773. product = FPMulFpscrImpl(value1, value2, true);
  2774. }
  2775. result = FPHalvedSub(FPThree(false), product, context, fpcr);
  2776. }
  2777. return result;
  2778. }
  2779. [UnmanagedCallersOnly]
  2780. public static double FPRSqrtStepFused(double value1, double value2)
  2781. {
  2782. ExecutionContext context = NativeInterface.GetContext();
  2783. FPCR fpcr = context.Fpcr;
  2784. value1 = value1.FPNeg();
  2785. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2786. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2787. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2788. if (!done)
  2789. {
  2790. bool inf1 = type1 == FPType.Infinity;
  2791. bool zero1 = type1 == FPType.Zero;
  2792. bool inf2 = type2 == FPType.Infinity;
  2793. bool zero2 = type2 == FPType.Zero;
  2794. if ((inf1 && zero2) || (zero1 && inf2))
  2795. {
  2796. result = FPOnePointFive(false);
  2797. }
  2798. else if (inf1 || inf2)
  2799. {
  2800. result = FPInfinity(sign1 ^ sign2);
  2801. }
  2802. else
  2803. {
  2804. result = Math.FusedMultiplyAdd(value1, value2, 3d) / 2d;
  2805. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2806. {
  2807. context.Fpsr |= FPSR.Ufc;
  2808. result = FPZero(result < 0d);
  2809. }
  2810. }
  2811. }
  2812. return result;
  2813. }
  2814. [UnmanagedCallersOnly]
  2815. public static double FPSqrt(double value)
  2816. {
  2817. ExecutionContext context = NativeInterface.GetContext();
  2818. FPCR fpcr = context.Fpcr;
  2819. value = value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2820. double result;
  2821. if (type == FPType.SNaN || type == FPType.QNaN)
  2822. {
  2823. result = FPProcessNaN(type, op, context, fpcr);
  2824. }
  2825. else if (type == FPType.Zero)
  2826. {
  2827. result = FPZero(sign);
  2828. }
  2829. else if (type == FPType.Infinity && !sign)
  2830. {
  2831. result = FPInfinity(sign);
  2832. }
  2833. else if (sign)
  2834. {
  2835. result = FPDefaultNaN();
  2836. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2837. }
  2838. else
  2839. {
  2840. result = Math.Sqrt(value);
  2841. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2842. {
  2843. context.Fpsr |= FPSR.Ufc;
  2844. result = FPZero(result < 0d);
  2845. }
  2846. }
  2847. return result;
  2848. }
  2849. [UnmanagedCallersOnly]
  2850. public static double FPSub(double value1, double value2)
  2851. {
  2852. return FPSubFpscr(value1, value2, false);
  2853. }
  2854. public static double FPSubFpscr(double value1, double value2, bool standardFpscr)
  2855. {
  2856. ExecutionContext context = NativeInterface.GetContext();
  2857. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2858. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2859. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2860. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2861. if (!done)
  2862. {
  2863. bool inf1 = type1 == FPType.Infinity;
  2864. bool zero1 = type1 == FPType.Zero;
  2865. bool inf2 = type2 == FPType.Infinity;
  2866. bool zero2 = type2 == FPType.Zero;
  2867. if (inf1 && inf2 && sign1 == sign2)
  2868. {
  2869. result = FPDefaultNaN();
  2870. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  2871. }
  2872. else if ((inf1 && !sign1) || (inf2 && sign2))
  2873. {
  2874. result = FPInfinity(false);
  2875. }
  2876. else if ((inf1 && sign1) || (inf2 && !sign2))
  2877. {
  2878. result = FPInfinity(true);
  2879. }
  2880. else if (zero1 && zero2 && sign1 == !sign2)
  2881. {
  2882. result = FPZero(sign1);
  2883. }
  2884. else
  2885. {
  2886. result = value1 - value2;
  2887. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2888. {
  2889. context.Fpsr |= FPSR.Ufc;
  2890. result = FPZero(result < 0d);
  2891. }
  2892. }
  2893. }
  2894. return result;
  2895. }
  2896. public static double FPDefaultNaN()
  2897. {
  2898. return BitConverter.Int64BitsToDouble(0x7ff8000000000000);
  2899. }
  2900. public static double FPInfinity(bool sign)
  2901. {
  2902. return sign ? double.NegativeInfinity : double.PositiveInfinity;
  2903. }
  2904. public static double FPZero(bool sign)
  2905. {
  2906. return sign ? -0d : +0d;
  2907. }
  2908. public static double FPMaxNormal(bool sign)
  2909. {
  2910. return sign ? double.MinValue : double.MaxValue;
  2911. }
  2912. private static double FPTwo(bool sign)
  2913. {
  2914. return sign ? -2d : +2d;
  2915. }
  2916. private static double FPThree(bool sign)
  2917. {
  2918. return sign ? -3d : +3d;
  2919. }
  2920. private static double FPOnePointFive(bool sign)
  2921. {
  2922. return sign ? -1.5d : +1.5d;
  2923. }
  2924. private static double FPNeg(this double value)
  2925. {
  2926. return -value;
  2927. }
  2928. private static double ZerosOrOnes(bool ones)
  2929. {
  2930. return BitConverter.Int64BitsToDouble(ones ? -1L : 0L);
  2931. }
  2932. private static double FPUnpack(
  2933. this double value,
  2934. out FPType type,
  2935. out bool sign,
  2936. out ulong valueBits,
  2937. ExecutionContext context,
  2938. FPCR fpcr)
  2939. {
  2940. valueBits = (ulong)BitConverter.DoubleToInt64Bits(value);
  2941. sign = (~valueBits & 0x8000000000000000ul) == 0ul;
  2942. if ((valueBits & 0x7FF0000000000000ul) == 0ul)
  2943. {
  2944. if ((valueBits & 0x000FFFFFFFFFFFFFul) == 0ul || (fpcr & FPCR.Fz) != 0)
  2945. {
  2946. type = FPType.Zero;
  2947. value = FPZero(sign);
  2948. if ((valueBits & 0x000FFFFFFFFFFFFFul) != 0ul)
  2949. {
  2950. SoftFloat.FPProcessException(FPException.InputDenorm, context, fpcr);
  2951. }
  2952. }
  2953. else
  2954. {
  2955. type = FPType.Nonzero;
  2956. }
  2957. }
  2958. else if ((~valueBits & 0x7FF0000000000000ul) == 0ul)
  2959. {
  2960. if ((valueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  2961. {
  2962. type = FPType.Infinity;
  2963. }
  2964. else
  2965. {
  2966. type = (~valueBits & 0x0008000000000000ul) == 0ul ? FPType.QNaN : FPType.SNaN;
  2967. value = FPZero(sign);
  2968. }
  2969. }
  2970. else
  2971. {
  2972. type = FPType.Nonzero;
  2973. }
  2974. return value;
  2975. }
  2976. private static double FPProcessNaNs(
  2977. FPType type1,
  2978. FPType type2,
  2979. ulong op1,
  2980. ulong op2,
  2981. out bool done,
  2982. ExecutionContext context,
  2983. FPCR fpcr)
  2984. {
  2985. done = true;
  2986. if (type1 == FPType.SNaN)
  2987. {
  2988. return FPProcessNaN(type1, op1, context, fpcr);
  2989. }
  2990. else if (type2 == FPType.SNaN)
  2991. {
  2992. return FPProcessNaN(type2, op2, context, fpcr);
  2993. }
  2994. else if (type1 == FPType.QNaN)
  2995. {
  2996. return FPProcessNaN(type1, op1, context, fpcr);
  2997. }
  2998. else if (type2 == FPType.QNaN)
  2999. {
  3000. return FPProcessNaN(type2, op2, context, fpcr);
  3001. }
  3002. done = false;
  3003. return FPZero(false);
  3004. }
  3005. private static double FPProcessNaNs3(
  3006. FPType type1,
  3007. FPType type2,
  3008. FPType type3,
  3009. ulong op1,
  3010. ulong op2,
  3011. ulong op3,
  3012. out bool done,
  3013. ExecutionContext context,
  3014. FPCR fpcr)
  3015. {
  3016. done = true;
  3017. if (type1 == FPType.SNaN)
  3018. {
  3019. return FPProcessNaN(type1, op1, context, fpcr);
  3020. }
  3021. else if (type2 == FPType.SNaN)
  3022. {
  3023. return FPProcessNaN(type2, op2, context, fpcr);
  3024. }
  3025. else if (type3 == FPType.SNaN)
  3026. {
  3027. return FPProcessNaN(type3, op3, context, fpcr);
  3028. }
  3029. else if (type1 == FPType.QNaN)
  3030. {
  3031. return FPProcessNaN(type1, op1, context, fpcr);
  3032. }
  3033. else if (type2 == FPType.QNaN)
  3034. {
  3035. return FPProcessNaN(type2, op2, context, fpcr);
  3036. }
  3037. else if (type3 == FPType.QNaN)
  3038. {
  3039. return FPProcessNaN(type3, op3, context, fpcr);
  3040. }
  3041. done = false;
  3042. return FPZero(false);
  3043. }
  3044. private static double FPProcessNaN(FPType type, ulong op, ExecutionContext context, FPCR fpcr)
  3045. {
  3046. if (type == FPType.SNaN)
  3047. {
  3048. op |= 1ul << 51;
  3049. SoftFloat.FPProcessException(FPException.InvalidOp, context, fpcr);
  3050. }
  3051. if ((fpcr & FPCR.Dn) != 0)
  3052. {
  3053. return FPDefaultNaN();
  3054. }
  3055. return BitConverter.Int64BitsToDouble((long)op);
  3056. }
  3057. }
  3058. }