SoftFloat.cs 101 KB

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