SoftFloat.cs 110 KB

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