SoftFloat.cs 110 KB

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