SoftFloat.cs 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235
  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. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  736. {
  737. context.Fpsr |= FPSR.Ufc;
  738. result = FPZero(result < 0f);
  739. }
  740. }
  741. }
  742. else
  743. {
  744. if (type2 == FPType.Infinity)
  745. {
  746. result = FPInfinity(sign2);
  747. }
  748. else if (type2 == FPType.Zero)
  749. {
  750. result = FPZero(sign1 && sign2);
  751. }
  752. else
  753. {
  754. result = value2;
  755. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  756. {
  757. context.Fpsr |= FPSR.Ufc;
  758. result = FPZero(result < 0f);
  759. }
  760. }
  761. }
  762. }
  763. return result;
  764. }
  765. public static float FPMaxNum(float value1, float value2)
  766. {
  767. return FPMaxNumFpscr(value1, value2, false);
  768. }
  769. public static float FPMaxNumFpscr(float value1, float value2, bool standardFpscr)
  770. {
  771. ExecutionContext context = NativeInterface.GetContext();
  772. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  773. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  774. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  775. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  776. {
  777. value1 = FPInfinity(true);
  778. }
  779. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  780. {
  781. value2 = FPInfinity(true);
  782. }
  783. return FPMaxFpscr(value1, value2, standardFpscr);
  784. }
  785. public static float FPMin(float value1, float value2)
  786. {
  787. return FPMinFpscr(value1, value2, false);
  788. }
  789. public static float FPMinFpscr(float value1, float value2, bool standardFpscr)
  790. {
  791. ExecutionContext context = NativeInterface.GetContext();
  792. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  793. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  794. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  795. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  796. if (!done)
  797. {
  798. if (value1 < value2)
  799. {
  800. if (type1 == FPType.Infinity)
  801. {
  802. result = FPInfinity(sign1);
  803. }
  804. else if (type1 == FPType.Zero)
  805. {
  806. result = FPZero(sign1 || sign2);
  807. }
  808. else
  809. {
  810. result = value1;
  811. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  812. {
  813. context.Fpsr |= FPSR.Ufc;
  814. result = FPZero(result < 0f);
  815. }
  816. }
  817. }
  818. else
  819. {
  820. if (type2 == FPType.Infinity)
  821. {
  822. result = FPInfinity(sign2);
  823. }
  824. else if (type2 == FPType.Zero)
  825. {
  826. result = FPZero(sign1 || sign2);
  827. }
  828. else
  829. {
  830. result = value2;
  831. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  832. {
  833. context.Fpsr |= FPSR.Ufc;
  834. result = FPZero(result < 0f);
  835. }
  836. }
  837. }
  838. }
  839. return result;
  840. }
  841. public static float FPMinNum(float value1, float value2)
  842. {
  843. return FPMinNumFpscr(value1, value2, false);
  844. }
  845. public static float FPMinNumFpscr(float value1, float value2, bool standardFpscr)
  846. {
  847. ExecutionContext context = NativeInterface.GetContext();
  848. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  849. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  850. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  851. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  852. {
  853. value1 = FPInfinity(false);
  854. }
  855. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  856. {
  857. value2 = FPInfinity(false);
  858. }
  859. return FPMinFpscr(value1, value2, standardFpscr);
  860. }
  861. public static float FPMul(float value1, float value2)
  862. {
  863. return FPMulFpscr(value1, value2, false);
  864. }
  865. public static float FPMulFpscr(float value1, float value2, bool standardFpscr)
  866. {
  867. ExecutionContext context = NativeInterface.GetContext();
  868. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  869. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  870. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  871. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  872. if (!done)
  873. {
  874. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  875. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  876. if ((inf1 && zero2) || (zero1 && inf2))
  877. {
  878. result = FPDefaultNaN();
  879. FPProcessException(FPException.InvalidOp, context, fpcr);
  880. }
  881. else if (inf1 || inf2)
  882. {
  883. result = FPInfinity(sign1 ^ sign2);
  884. }
  885. else if (zero1 || zero2)
  886. {
  887. result = FPZero(sign1 ^ sign2);
  888. }
  889. else
  890. {
  891. result = value1 * value2;
  892. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  893. {
  894. context.Fpsr |= FPSR.Ufc;
  895. result = FPZero(result < 0f);
  896. }
  897. }
  898. }
  899. return result;
  900. }
  901. public static float FPMulAdd(float valueA, float value1, float value2)
  902. {
  903. return FPMulAddFpscr(valueA, value1, value2, false);
  904. }
  905. public static float FPMulAddFpscr(float valueA, float value1, float value2, bool standardFpscr)
  906. {
  907. ExecutionContext context = NativeInterface.GetContext();
  908. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  909. valueA = valueA.FPUnpack(out FPType typeA, out bool signA, out uint addend, context, fpcr);
  910. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  911. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  912. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  913. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  914. float result = FPProcessNaNs3(typeA, type1, type2, addend, op1, op2, out bool done, context, fpcr);
  915. if (typeA == FPType.QNaN && ((inf1 && zero2) || (zero1 && inf2)))
  916. {
  917. result = FPDefaultNaN();
  918. FPProcessException(FPException.InvalidOp, context, fpcr);
  919. }
  920. if (!done)
  921. {
  922. bool infA = typeA == FPType.Infinity; bool zeroA = typeA == FPType.Zero;
  923. bool signP = sign1 ^ sign2;
  924. bool infP = inf1 || inf2;
  925. bool zeroP = zero1 || zero2;
  926. if ((inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP))
  927. {
  928. result = FPDefaultNaN();
  929. FPProcessException(FPException.InvalidOp, context, fpcr);
  930. }
  931. else if ((infA && !signA) || (infP && !signP))
  932. {
  933. result = FPInfinity(false);
  934. }
  935. else if ((infA && signA) || (infP && signP))
  936. {
  937. result = FPInfinity(true);
  938. }
  939. else if (zeroA && zeroP && signA == signP)
  940. {
  941. result = FPZero(signA);
  942. }
  943. else
  944. {
  945. result = MathF.FusedMultiplyAdd(value1, value2, valueA);
  946. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  947. {
  948. context.Fpsr |= FPSR.Ufc;
  949. result = FPZero(result < 0f);
  950. }
  951. }
  952. }
  953. return result;
  954. }
  955. public static float FPMulSub(float valueA, float value1, float value2)
  956. {
  957. value1 = value1.FPNeg();
  958. return FPMulAdd(valueA, value1, value2);
  959. }
  960. public static float FPMulSubFpscr(float valueA, float value1, float value2, bool standardFpscr)
  961. {
  962. value1 = value1.FPNeg();
  963. return FPMulAddFpscr(valueA, value1, value2, standardFpscr);
  964. }
  965. public static float FPMulX(float value1, float value2)
  966. {
  967. ExecutionContext context = NativeInterface.GetContext();
  968. FPCR fpcr = context.Fpcr;
  969. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  970. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  971. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  972. if (!done)
  973. {
  974. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  975. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  976. if ((inf1 && zero2) || (zero1 && inf2))
  977. {
  978. result = FPTwo(sign1 ^ sign2);
  979. }
  980. else if (inf1 || inf2)
  981. {
  982. result = FPInfinity(sign1 ^ sign2);
  983. }
  984. else if (zero1 || zero2)
  985. {
  986. result = FPZero(sign1 ^ sign2);
  987. }
  988. else
  989. {
  990. result = value1 * value2;
  991. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  992. {
  993. context.Fpsr |= FPSR.Ufc;
  994. result = FPZero(result < 0f);
  995. }
  996. }
  997. }
  998. return result;
  999. }
  1000. public static float FPNegMulAdd(float valueA, float value1, float value2)
  1001. {
  1002. valueA = valueA.FPNeg();
  1003. value1 = value1.FPNeg();
  1004. return FPMulAdd(valueA, value1, value2);
  1005. }
  1006. public static float FPNegMulSub(float valueA, float value1, float value2)
  1007. {
  1008. valueA = valueA.FPNeg();
  1009. return FPMulAdd(valueA, value1, value2);
  1010. }
  1011. public static float FPRecipEstimate(float value)
  1012. {
  1013. return FPRecipEstimateFpscr(value, false);
  1014. }
  1015. public static float FPRecipEstimateFpscr(float value, bool standardFpscr)
  1016. {
  1017. ExecutionContext context = NativeInterface.GetContext();
  1018. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1019. value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1020. float result;
  1021. if (type == FPType.SNaN || type == FPType.QNaN)
  1022. {
  1023. result = FPProcessNaN(type, op, context, fpcr);
  1024. }
  1025. else if (type == FPType.Infinity)
  1026. {
  1027. result = FPZero(sign);
  1028. }
  1029. else if (type == FPType.Zero)
  1030. {
  1031. result = FPInfinity(sign);
  1032. FPProcessException(FPException.DivideByZero, context, fpcr);
  1033. }
  1034. else if (MathF.Abs(value) < MathF.Pow(2f, -128))
  1035. {
  1036. bool overflowToInf;
  1037. switch (fpcr.GetRoundingMode())
  1038. {
  1039. default:
  1040. case FPRoundingMode.ToNearest: overflowToInf = true; break;
  1041. case FPRoundingMode.TowardsPlusInfinity: overflowToInf = !sign; break;
  1042. case FPRoundingMode.TowardsMinusInfinity: overflowToInf = sign; break;
  1043. case FPRoundingMode.TowardsZero: overflowToInf = false; break;
  1044. }
  1045. result = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  1046. FPProcessException(FPException.Overflow, context, fpcr);
  1047. FPProcessException(FPException.Inexact, context, fpcr);
  1048. }
  1049. else if ((fpcr & FPCR.Fz) != 0 && (MathF.Abs(value) >= MathF.Pow(2f, 126)))
  1050. {
  1051. result = FPZero(sign);
  1052. context.Fpsr |= FPSR.Ufc;
  1053. }
  1054. else
  1055. {
  1056. ulong fraction = (ulong)(op & 0x007FFFFFu) << 29;
  1057. uint exp = (op & 0x7F800000u) >> 23;
  1058. if (exp == 0u)
  1059. {
  1060. if ((fraction & 0x0008000000000000ul) == 0ul)
  1061. {
  1062. fraction = (fraction & 0x0003FFFFFFFFFFFFul) << 2;
  1063. exp -= 1u;
  1064. }
  1065. else
  1066. {
  1067. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1068. }
  1069. }
  1070. uint scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1071. uint resultExp = 253u - exp;
  1072. uint estimate = (uint)SoftFloat.RecipEstimateTable[scaled - 256u] + 256u;
  1073. fraction = (ulong)(estimate & 0xFFu) << 44;
  1074. if (resultExp == 0u)
  1075. {
  1076. fraction = ((fraction & 0x000FFFFFFFFFFFFEul) | 0x0010000000000000ul) >> 1;
  1077. }
  1078. else if (resultExp + 1u == 0u)
  1079. {
  1080. fraction = ((fraction & 0x000FFFFFFFFFFFFCul) | 0x0010000000000000ul) >> 2;
  1081. resultExp = 0u;
  1082. }
  1083. result = BitConverter.Int32BitsToSingle(
  1084. (int)((sign ? 1u : 0u) << 31 | (resultExp & 0xFFu) << 23 | (uint)(fraction >> 29) & 0x007FFFFFu));
  1085. }
  1086. return result;
  1087. }
  1088. public static float FPRecipStep(float value1, float value2)
  1089. {
  1090. ExecutionContext context = NativeInterface.GetContext();
  1091. FPCR fpcr = context.StandardFpcrValue;
  1092. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1093. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1094. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1095. if (!done)
  1096. {
  1097. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1098. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1099. float product;
  1100. if ((inf1 && zero2) || (zero1 && inf2))
  1101. {
  1102. product = FPZero(false);
  1103. }
  1104. else
  1105. {
  1106. product = FPMulFpscr(value1, value2, true);
  1107. }
  1108. result = FPSubFpscr(FPTwo(false), product, true);
  1109. }
  1110. return result;
  1111. }
  1112. public static float FPRecipStepFused(float value1, float value2)
  1113. {
  1114. ExecutionContext context = NativeInterface.GetContext();
  1115. FPCR fpcr = context.Fpcr;
  1116. value1 = value1.FPNeg();
  1117. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1118. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1119. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1120. if (!done)
  1121. {
  1122. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1123. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1124. if ((inf1 && zero2) || (zero1 && inf2))
  1125. {
  1126. result = FPTwo(false);
  1127. }
  1128. else if (inf1 || inf2)
  1129. {
  1130. result = FPInfinity(sign1 ^ sign2);
  1131. }
  1132. else
  1133. {
  1134. result = MathF.FusedMultiplyAdd(value1, value2, 2f);
  1135. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1136. {
  1137. context.Fpsr |= FPSR.Ufc;
  1138. result = FPZero(result < 0f);
  1139. }
  1140. }
  1141. }
  1142. return result;
  1143. }
  1144. public static float FPRecpX(float value)
  1145. {
  1146. ExecutionContext context = NativeInterface.GetContext();
  1147. FPCR fpcr = context.Fpcr;
  1148. value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1149. float result;
  1150. if (type == FPType.SNaN || type == FPType.QNaN)
  1151. {
  1152. result = FPProcessNaN(type, op, context, fpcr);
  1153. }
  1154. else
  1155. {
  1156. uint notExp = (~op >> 23) & 0xFFu;
  1157. uint maxExp = 0xFEu;
  1158. result = BitConverter.Int32BitsToSingle(
  1159. (int)((sign ? 1u : 0u) << 31 | (notExp == 0xFFu ? maxExp : notExp) << 23));
  1160. }
  1161. return result;
  1162. }
  1163. public static float FPRSqrtEstimate(float value)
  1164. {
  1165. return FPRSqrtEstimateFpscr(value, false);
  1166. }
  1167. public static float FPRSqrtEstimateFpscr(float value, bool standardFpscr)
  1168. {
  1169. ExecutionContext context = NativeInterface.GetContext();
  1170. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1171. value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1172. float result;
  1173. if (type == FPType.SNaN || type == FPType.QNaN)
  1174. {
  1175. result = FPProcessNaN(type, op, context, fpcr);
  1176. }
  1177. else if (type == FPType.Zero)
  1178. {
  1179. result = FPInfinity(sign);
  1180. FPProcessException(FPException.DivideByZero, context, fpcr);
  1181. }
  1182. else if (sign)
  1183. {
  1184. result = FPDefaultNaN();
  1185. FPProcessException(FPException.InvalidOp, context, fpcr);
  1186. }
  1187. else if (type == FPType.Infinity)
  1188. {
  1189. result = FPZero(false);
  1190. }
  1191. else
  1192. {
  1193. ulong fraction = (ulong)(op & 0x007FFFFFu) << 29;
  1194. uint exp = (op & 0x7F800000u) >> 23;
  1195. if (exp == 0u)
  1196. {
  1197. while ((fraction & 0x0008000000000000ul) == 0ul)
  1198. {
  1199. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1200. exp -= 1u;
  1201. }
  1202. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1203. }
  1204. uint scaled;
  1205. if ((exp & 1u) == 0u)
  1206. {
  1207. scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1208. }
  1209. else
  1210. {
  1211. scaled = (uint)(((fraction & 0x000FE00000000000ul) | 0x0010000000000000ul) >> 45);
  1212. }
  1213. uint resultExp = (380u - exp) >> 1;
  1214. uint estimate = (uint)SoftFloat.RecipSqrtEstimateTable[scaled - 128u] + 256u;
  1215. result = BitConverter.Int32BitsToSingle((int)((resultExp & 0xFFu) << 23 | (estimate & 0xFFu) << 15));
  1216. }
  1217. return result;
  1218. }
  1219. public static float FPHalvedSub(float value1, float value2, ExecutionContext context, FPCR fpcr)
  1220. {
  1221. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1222. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1223. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1224. if (!done)
  1225. {
  1226. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1227. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1228. if (inf1 && inf2 && sign1 == sign2)
  1229. {
  1230. result = FPDefaultNaN();
  1231. FPProcessException(FPException.InvalidOp, context, fpcr);
  1232. }
  1233. else if ((inf1 && !sign1) || (inf2 && sign2))
  1234. {
  1235. result = FPInfinity(false);
  1236. }
  1237. else if ((inf1 && sign1) || (inf2 && !sign2))
  1238. {
  1239. result = FPInfinity(true);
  1240. }
  1241. else if (zero1 && zero2 && sign1 == !sign2)
  1242. {
  1243. result = FPZero(sign1);
  1244. }
  1245. else
  1246. {
  1247. result = (value1 - value2) / 2.0f;
  1248. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1249. {
  1250. context.Fpsr |= FPSR.Ufc;
  1251. result = FPZero(result < 0f);
  1252. }
  1253. }
  1254. }
  1255. return result;
  1256. }
  1257. public static float FPRSqrtStep(float value1, float value2)
  1258. {
  1259. ExecutionContext context = NativeInterface.GetContext();
  1260. FPCR fpcr = context.StandardFpcrValue;
  1261. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1262. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1263. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1264. if (!done)
  1265. {
  1266. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1267. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1268. float product;
  1269. if ((inf1 && zero2) || (zero1 && inf2))
  1270. {
  1271. product = FPZero(false);
  1272. }
  1273. else
  1274. {
  1275. product = FPMulFpscr(value1, value2, true);
  1276. }
  1277. result = FPHalvedSub(FPThree(false), product, context, fpcr);
  1278. }
  1279. return result;
  1280. }
  1281. public static float FPRSqrtStepFused(float value1, float value2)
  1282. {
  1283. ExecutionContext context = NativeInterface.GetContext();
  1284. FPCR fpcr = context.Fpcr;
  1285. value1 = value1.FPNeg();
  1286. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1287. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1288. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1289. if (!done)
  1290. {
  1291. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1292. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1293. if ((inf1 && zero2) || (zero1 && inf2))
  1294. {
  1295. result = FPOnePointFive(false);
  1296. }
  1297. else if (inf1 || inf2)
  1298. {
  1299. result = FPInfinity(sign1 ^ sign2);
  1300. }
  1301. else
  1302. {
  1303. result = MathF.FusedMultiplyAdd(value1, value2, 3f) / 2f;
  1304. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1305. {
  1306. context.Fpsr |= FPSR.Ufc;
  1307. result = FPZero(result < 0f);
  1308. }
  1309. }
  1310. }
  1311. return result;
  1312. }
  1313. public static float FPSqrt(float value)
  1314. {
  1315. ExecutionContext context = NativeInterface.GetContext();
  1316. FPCR fpcr = context.Fpcr;
  1317. value = value.FPUnpack(out FPType type, out bool sign, out uint op, context, fpcr);
  1318. float result;
  1319. if (type == FPType.SNaN || type == FPType.QNaN)
  1320. {
  1321. result = FPProcessNaN(type, op, context, fpcr);
  1322. }
  1323. else if (type == FPType.Zero)
  1324. {
  1325. result = FPZero(sign);
  1326. }
  1327. else if (type == FPType.Infinity && !sign)
  1328. {
  1329. result = FPInfinity(sign);
  1330. }
  1331. else if (sign)
  1332. {
  1333. result = FPDefaultNaN();
  1334. FPProcessException(FPException.InvalidOp, context, fpcr);
  1335. }
  1336. else
  1337. {
  1338. result = MathF.Sqrt(value);
  1339. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1340. {
  1341. context.Fpsr |= FPSR.Ufc;
  1342. result = FPZero(result < 0f);
  1343. }
  1344. }
  1345. return result;
  1346. }
  1347. public static float FPSub(float value1, float value2)
  1348. {
  1349. return FPSubFpscr(value1, value2, false);
  1350. }
  1351. public static float FPSubFpscr(float value1, float value2, bool standardFpscr)
  1352. {
  1353. ExecutionContext context = NativeInterface.GetContext();
  1354. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1355. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context, fpcr);
  1356. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context, fpcr);
  1357. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1358. if (!done)
  1359. {
  1360. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1361. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1362. if (inf1 && inf2 && sign1 == sign2)
  1363. {
  1364. result = FPDefaultNaN();
  1365. FPProcessException(FPException.InvalidOp, context, fpcr);
  1366. }
  1367. else if ((inf1 && !sign1) || (inf2 && sign2))
  1368. {
  1369. result = FPInfinity(false);
  1370. }
  1371. else if ((inf1 && sign1) || (inf2 && !sign2))
  1372. {
  1373. result = FPInfinity(true);
  1374. }
  1375. else if (zero1 && zero2 && sign1 == !sign2)
  1376. {
  1377. result = FPZero(sign1);
  1378. }
  1379. else
  1380. {
  1381. result = value1 - value2;
  1382. if ((fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1383. {
  1384. context.Fpsr |= FPSR.Ufc;
  1385. result = FPZero(result < 0f);
  1386. }
  1387. }
  1388. }
  1389. return result;
  1390. }
  1391. private static float FPDefaultNaN()
  1392. {
  1393. return BitConverter.Int32BitsToSingle(0x7fc00000);
  1394. }
  1395. private static float FPInfinity(bool sign)
  1396. {
  1397. return sign ? float.NegativeInfinity : float.PositiveInfinity;
  1398. }
  1399. private static float FPZero(bool sign)
  1400. {
  1401. return sign ? -0f : +0f;
  1402. }
  1403. private static float FPMaxNormal(bool sign)
  1404. {
  1405. return sign ? float.MinValue : float.MaxValue;
  1406. }
  1407. private static float FPTwo(bool sign)
  1408. {
  1409. return sign ? -2f : +2f;
  1410. }
  1411. private static float FPThree(bool sign)
  1412. {
  1413. return sign ? -3f : +3f;
  1414. }
  1415. private static float FPOnePointFive(bool sign)
  1416. {
  1417. return sign ? -1.5f : +1.5f;
  1418. }
  1419. private static float FPNeg(this float value)
  1420. {
  1421. return -value;
  1422. }
  1423. private static float ZerosOrOnes(bool ones)
  1424. {
  1425. return BitConverter.Int32BitsToSingle(ones ? -1 : 0);
  1426. }
  1427. private static float FPUnpack(
  1428. this float value,
  1429. out FPType type,
  1430. out bool sign,
  1431. out uint valueBits,
  1432. ExecutionContext context,
  1433. FPCR fpcr)
  1434. {
  1435. valueBits = (uint)BitConverter.SingleToInt32Bits(value);
  1436. sign = (~valueBits & 0x80000000u) == 0u;
  1437. if ((valueBits & 0x7F800000u) == 0u)
  1438. {
  1439. if ((valueBits & 0x007FFFFFu) == 0u || (fpcr & FPCR.Fz) != 0)
  1440. {
  1441. type = FPType.Zero;
  1442. value = FPZero(sign);
  1443. if ((valueBits & 0x007FFFFFu) != 0u)
  1444. {
  1445. FPProcessException(FPException.InputDenorm, context, fpcr);
  1446. }
  1447. }
  1448. else
  1449. {
  1450. type = FPType.Nonzero;
  1451. }
  1452. }
  1453. else if ((~valueBits & 0x7F800000u) == 0u)
  1454. {
  1455. if ((valueBits & 0x007FFFFFu) == 0u)
  1456. {
  1457. type = FPType.Infinity;
  1458. }
  1459. else
  1460. {
  1461. type = (~valueBits & 0x00400000u) == 0u ? FPType.QNaN : FPType.SNaN;
  1462. value = FPZero(sign);
  1463. }
  1464. }
  1465. else
  1466. {
  1467. type = FPType.Nonzero;
  1468. }
  1469. return value;
  1470. }
  1471. private static float FPProcessNaNs(
  1472. FPType type1,
  1473. FPType type2,
  1474. uint op1,
  1475. uint op2,
  1476. out bool done,
  1477. ExecutionContext context,
  1478. FPCR fpcr)
  1479. {
  1480. done = true;
  1481. if (type1 == FPType.SNaN)
  1482. {
  1483. return FPProcessNaN(type1, op1, context, fpcr);
  1484. }
  1485. else if (type2 == FPType.SNaN)
  1486. {
  1487. return FPProcessNaN(type2, op2, context, fpcr);
  1488. }
  1489. else if (type1 == FPType.QNaN)
  1490. {
  1491. return FPProcessNaN(type1, op1, context, fpcr);
  1492. }
  1493. else if (type2 == FPType.QNaN)
  1494. {
  1495. return FPProcessNaN(type2, op2, context, fpcr);
  1496. }
  1497. done = false;
  1498. return FPZero(false);
  1499. }
  1500. private static float FPProcessNaNs3(
  1501. FPType type1,
  1502. FPType type2,
  1503. FPType type3,
  1504. uint op1,
  1505. uint op2,
  1506. uint op3,
  1507. out bool done,
  1508. ExecutionContext context,
  1509. FPCR fpcr)
  1510. {
  1511. done = true;
  1512. if (type1 == FPType.SNaN)
  1513. {
  1514. return FPProcessNaN(type1, op1, context, fpcr);
  1515. }
  1516. else if (type2 == FPType.SNaN)
  1517. {
  1518. return FPProcessNaN(type2, op2, context, fpcr);
  1519. }
  1520. else if (type3 == FPType.SNaN)
  1521. {
  1522. return FPProcessNaN(type3, op3, context, fpcr);
  1523. }
  1524. else if (type1 == FPType.QNaN)
  1525. {
  1526. return FPProcessNaN(type1, op1, context, fpcr);
  1527. }
  1528. else if (type2 == FPType.QNaN)
  1529. {
  1530. return FPProcessNaN(type2, op2, context, fpcr);
  1531. }
  1532. else if (type3 == FPType.QNaN)
  1533. {
  1534. return FPProcessNaN(type3, op3, context, fpcr);
  1535. }
  1536. done = false;
  1537. return FPZero(false);
  1538. }
  1539. private static float FPProcessNaN(FPType type, uint op, ExecutionContext context, FPCR fpcr)
  1540. {
  1541. if (type == FPType.SNaN)
  1542. {
  1543. op |= 1u << 22;
  1544. FPProcessException(FPException.InvalidOp, context, fpcr);
  1545. }
  1546. if ((fpcr & FPCR.Dn) != 0)
  1547. {
  1548. return FPDefaultNaN();
  1549. }
  1550. return BitConverter.Int32BitsToSingle((int)op);
  1551. }
  1552. private static void FPProcessException(FPException exc, ExecutionContext context, FPCR fpcr)
  1553. {
  1554. int enable = (int)exc + 8;
  1555. if ((fpcr & (FPCR)(1 << enable)) != 0)
  1556. {
  1557. throw new NotImplementedException("Floating-point trap handling.");
  1558. }
  1559. else
  1560. {
  1561. context.Fpsr |= (FPSR)(1 << (int)exc);
  1562. }
  1563. }
  1564. }
  1565. static class SoftFloat64
  1566. {
  1567. public static double FPAdd(double value1, double value2)
  1568. {
  1569. return FPAddFpscr(value1, value2, false);
  1570. }
  1571. public static double FPAddFpscr(double value1, double value2, bool standardFpscr)
  1572. {
  1573. ExecutionContext context = NativeInterface.GetContext();
  1574. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1575. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1576. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1577. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1578. if (!done)
  1579. {
  1580. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1581. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1582. if (inf1 && inf2 && sign1 == !sign2)
  1583. {
  1584. result = FPDefaultNaN();
  1585. FPProcessException(FPException.InvalidOp, context, fpcr);
  1586. }
  1587. else if ((inf1 && !sign1) || (inf2 && !sign2))
  1588. {
  1589. result = FPInfinity(false);
  1590. }
  1591. else if ((inf1 && sign1) || (inf2 && sign2))
  1592. {
  1593. result = FPInfinity(true);
  1594. }
  1595. else if (zero1 && zero2 && sign1 == sign2)
  1596. {
  1597. result = FPZero(sign1);
  1598. }
  1599. else
  1600. {
  1601. result = value1 + value2;
  1602. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1603. {
  1604. context.Fpsr |= FPSR.Ufc;
  1605. result = FPZero(result < 0d);
  1606. }
  1607. }
  1608. }
  1609. return result;
  1610. }
  1611. public static int FPCompare(double value1, double value2, bool signalNaNs)
  1612. {
  1613. ExecutionContext context = NativeInterface.GetContext();
  1614. FPCR fpcr = context.Fpcr;
  1615. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out _, context, fpcr);
  1616. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out _, context, fpcr);
  1617. int result;
  1618. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1619. {
  1620. result = 0b0011;
  1621. if (type1 == FPType.SNaN || type2 == FPType.SNaN || signalNaNs)
  1622. {
  1623. FPProcessException(FPException.InvalidOp, context, fpcr);
  1624. }
  1625. }
  1626. else
  1627. {
  1628. if (value1 == value2)
  1629. {
  1630. result = 0b0110;
  1631. }
  1632. else if (value1 < value2)
  1633. {
  1634. result = 0b1000;
  1635. }
  1636. else
  1637. {
  1638. result = 0b0010;
  1639. }
  1640. }
  1641. return result;
  1642. }
  1643. public static double FPCompareEQ(double value1, double value2)
  1644. {
  1645. return FPCompareEQFpscr(value1, value2, false);
  1646. }
  1647. public static double FPCompareEQFpscr(double value1, double value2, bool standardFpscr)
  1648. {
  1649. ExecutionContext context = NativeInterface.GetContext();
  1650. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1651. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1652. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1653. double result;
  1654. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1655. {
  1656. result = ZerosOrOnes(false);
  1657. if (type1 == FPType.SNaN || type2 == FPType.SNaN)
  1658. {
  1659. FPProcessException(FPException.InvalidOp, context, fpcr);
  1660. }
  1661. }
  1662. else
  1663. {
  1664. result = ZerosOrOnes(value1 == value2);
  1665. }
  1666. return result;
  1667. }
  1668. public static double FPCompareGE(double value1, double value2)
  1669. {
  1670. return FPCompareGEFpscr(value1, value2, false);
  1671. }
  1672. public static double FPCompareGEFpscr(double value1, double value2, bool standardFpscr)
  1673. {
  1674. ExecutionContext context = NativeInterface.GetContext();
  1675. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1676. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1677. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1678. double result;
  1679. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1680. {
  1681. result = ZerosOrOnes(false);
  1682. FPProcessException(FPException.InvalidOp, context, fpcr);
  1683. }
  1684. else
  1685. {
  1686. result = ZerosOrOnes(value1 >= value2);
  1687. }
  1688. return result;
  1689. }
  1690. public static double FPCompareGT(double value1, double value2)
  1691. {
  1692. return FPCompareGTFpscr(value1, value2, false);
  1693. }
  1694. public static double FPCompareGTFpscr(double value1, double value2, bool standardFpscr)
  1695. {
  1696. ExecutionContext context = NativeInterface.GetContext();
  1697. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1698. value1 = value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1699. value2 = value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1700. double result;
  1701. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1702. {
  1703. result = ZerosOrOnes(false);
  1704. FPProcessException(FPException.InvalidOp, context, fpcr);
  1705. }
  1706. else
  1707. {
  1708. result = ZerosOrOnes(value1 > value2);
  1709. }
  1710. return result;
  1711. }
  1712. public static double FPCompareLE(double value1, double value2)
  1713. {
  1714. return FPCompareGE(value2, value1);
  1715. }
  1716. public static double FPCompareLT(double value1, double value2)
  1717. {
  1718. return FPCompareGT(value2, value1);
  1719. }
  1720. public static double FPCompareLEFpscr(double value1, double value2, bool standardFpscr)
  1721. {
  1722. return FPCompareGEFpscr(value2, value1, standardFpscr);
  1723. }
  1724. public static double FPCompareLTFpscr(double value1, double value2, bool standardFpscr)
  1725. {
  1726. return FPCompareGTFpscr(value2, value1, standardFpscr);
  1727. }
  1728. public static double FPDiv(double value1, double value2)
  1729. {
  1730. ExecutionContext context = NativeInterface.GetContext();
  1731. FPCR fpcr = context.Fpcr;
  1732. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1733. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1734. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1735. if (!done)
  1736. {
  1737. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1738. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1739. if ((inf1 && inf2) || (zero1 && zero2))
  1740. {
  1741. result = FPDefaultNaN();
  1742. FPProcessException(FPException.InvalidOp, context, fpcr);
  1743. }
  1744. else if (inf1 || zero2)
  1745. {
  1746. result = FPInfinity(sign1 ^ sign2);
  1747. if (!inf1)
  1748. {
  1749. FPProcessException(FPException.DivideByZero, context, fpcr);
  1750. }
  1751. }
  1752. else if (zero1 || inf2)
  1753. {
  1754. result = FPZero(sign1 ^ sign2);
  1755. }
  1756. else
  1757. {
  1758. result = value1 / value2;
  1759. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1760. {
  1761. context.Fpsr |= FPSR.Ufc;
  1762. result = FPZero(result < 0d);
  1763. }
  1764. }
  1765. }
  1766. return result;
  1767. }
  1768. public static double FPMax(double value1, double value2)
  1769. {
  1770. return FPMaxFpscr(value1, value2, false);
  1771. }
  1772. public static double FPMaxFpscr(double value1, double value2, bool standardFpscr)
  1773. {
  1774. ExecutionContext context = NativeInterface.GetContext();
  1775. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1776. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1777. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1778. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1779. if (!done)
  1780. {
  1781. if (value1 > value2)
  1782. {
  1783. if (type1 == FPType.Infinity)
  1784. {
  1785. result = FPInfinity(sign1);
  1786. }
  1787. else if (type1 == FPType.Zero)
  1788. {
  1789. result = FPZero(sign1 && sign2);
  1790. }
  1791. else
  1792. {
  1793. result = value1;
  1794. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1795. {
  1796. context.Fpsr |= FPSR.Ufc;
  1797. result = FPZero(result < 0d);
  1798. }
  1799. }
  1800. }
  1801. else
  1802. {
  1803. if (type2 == FPType.Infinity)
  1804. {
  1805. result = FPInfinity(sign2);
  1806. }
  1807. else if (type2 == FPType.Zero)
  1808. {
  1809. result = FPZero(sign1 && sign2);
  1810. }
  1811. else
  1812. {
  1813. result = value2;
  1814. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1815. {
  1816. context.Fpsr |= FPSR.Ufc;
  1817. result = FPZero(result < 0d);
  1818. }
  1819. }
  1820. }
  1821. }
  1822. return result;
  1823. }
  1824. public static double FPMaxNum(double value1, double value2)
  1825. {
  1826. return FPMaxNumFpscr(value1, value2, false);
  1827. }
  1828. public static double FPMaxNumFpscr(double value1, double value2, bool standardFpscr)
  1829. {
  1830. ExecutionContext context = NativeInterface.GetContext();
  1831. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1832. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1833. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1834. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  1835. {
  1836. value1 = FPInfinity(true);
  1837. }
  1838. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  1839. {
  1840. value2 = FPInfinity(true);
  1841. }
  1842. return FPMaxFpscr(value1, value2, standardFpscr);
  1843. }
  1844. public static double FPMin(double value1, double value2)
  1845. {
  1846. return FPMinFpscr(value1, value2, false);
  1847. }
  1848. public static double FPMinFpscr(double value1, double value2, bool standardFpscr)
  1849. {
  1850. ExecutionContext context = NativeInterface.GetContext();
  1851. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1852. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1853. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1854. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1855. if (!done)
  1856. {
  1857. if (value1 < value2)
  1858. {
  1859. if (type1 == FPType.Infinity)
  1860. {
  1861. result = FPInfinity(sign1);
  1862. }
  1863. else if (type1 == FPType.Zero)
  1864. {
  1865. result = FPZero(sign1 || sign2);
  1866. }
  1867. else
  1868. {
  1869. result = value1;
  1870. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1871. {
  1872. context.Fpsr |= FPSR.Ufc;
  1873. result = FPZero(result < 0d);
  1874. }
  1875. }
  1876. }
  1877. else
  1878. {
  1879. if (type2 == FPType.Infinity)
  1880. {
  1881. result = FPInfinity(sign2);
  1882. }
  1883. else if (type2 == FPType.Zero)
  1884. {
  1885. result = FPZero(sign1 || sign2);
  1886. }
  1887. else
  1888. {
  1889. result = value2;
  1890. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1891. {
  1892. context.Fpsr |= FPSR.Ufc;
  1893. result = FPZero(result < 0d);
  1894. }
  1895. }
  1896. }
  1897. }
  1898. return result;
  1899. }
  1900. public static double FPMinNum(double value1, double value2)
  1901. {
  1902. return FPMinNumFpscr(value1, value2, false);
  1903. }
  1904. public static double FPMinNumFpscr(double value1, double value2, bool standardFpscr)
  1905. {
  1906. ExecutionContext context = NativeInterface.GetContext();
  1907. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1908. value1.FPUnpack(out FPType type1, out _, out _, context, fpcr);
  1909. value2.FPUnpack(out FPType type2, out _, out _, context, fpcr);
  1910. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  1911. {
  1912. value1 = FPInfinity(false);
  1913. }
  1914. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  1915. {
  1916. value2 = FPInfinity(false);
  1917. }
  1918. return FPMinFpscr(value1, value2, standardFpscr);
  1919. }
  1920. public static double FPMul(double value1, double value2)
  1921. {
  1922. return FPMulFpscr(value1, value2, false);
  1923. }
  1924. public static double FPMulFpscr(double value1, double value2, bool standardFpscr)
  1925. {
  1926. ExecutionContext context = NativeInterface.GetContext();
  1927. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1928. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1929. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1930. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  1931. if (!done)
  1932. {
  1933. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1934. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1935. if ((inf1 && zero2) || (zero1 && inf2))
  1936. {
  1937. result = FPDefaultNaN();
  1938. FPProcessException(FPException.InvalidOp, context, fpcr);
  1939. }
  1940. else if (inf1 || inf2)
  1941. {
  1942. result = FPInfinity(sign1 ^ sign2);
  1943. }
  1944. else if (zero1 || zero2)
  1945. {
  1946. result = FPZero(sign1 ^ sign2);
  1947. }
  1948. else
  1949. {
  1950. result = value1 * value2;
  1951. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1952. {
  1953. context.Fpsr |= FPSR.Ufc;
  1954. result = FPZero(result < 0d);
  1955. }
  1956. }
  1957. }
  1958. return result;
  1959. }
  1960. public static double FPMulAdd(double valueA, double value1, double value2)
  1961. {
  1962. return FPMulAddFpscr(valueA, value1, value2, false);
  1963. }
  1964. public static double FPMulAddFpscr(double valueA, double value1, double value2, bool standardFpscr)
  1965. {
  1966. ExecutionContext context = NativeInterface.GetContext();
  1967. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  1968. valueA = valueA.FPUnpack(out FPType typeA, out bool signA, out ulong addend, context, fpcr);
  1969. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  1970. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  1971. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1972. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1973. double result = FPProcessNaNs3(typeA, type1, type2, addend, op1, op2, out bool done, context, fpcr);
  1974. if (typeA == FPType.QNaN && ((inf1 && zero2) || (zero1 && inf2)))
  1975. {
  1976. result = FPDefaultNaN();
  1977. FPProcessException(FPException.InvalidOp, context, fpcr);
  1978. }
  1979. if (!done)
  1980. {
  1981. bool infA = typeA == FPType.Infinity; bool zeroA = typeA == FPType.Zero;
  1982. bool signP = sign1 ^ sign2;
  1983. bool infP = inf1 || inf2;
  1984. bool zeroP = zero1 || zero2;
  1985. if ((inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP))
  1986. {
  1987. result = FPDefaultNaN();
  1988. FPProcessException(FPException.InvalidOp, context, fpcr);
  1989. }
  1990. else if ((infA && !signA) || (infP && !signP))
  1991. {
  1992. result = FPInfinity(false);
  1993. }
  1994. else if ((infA && signA) || (infP && signP))
  1995. {
  1996. result = FPInfinity(true);
  1997. }
  1998. else if (zeroA && zeroP && signA == signP)
  1999. {
  2000. result = FPZero(signA);
  2001. }
  2002. else
  2003. {
  2004. result = Math.FusedMultiplyAdd(value1, value2, valueA);
  2005. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2006. {
  2007. context.Fpsr |= FPSR.Ufc;
  2008. result = FPZero(result < 0d);
  2009. }
  2010. }
  2011. }
  2012. return result;
  2013. }
  2014. public static double FPMulSub(double valueA, double value1, double value2)
  2015. {
  2016. value1 = value1.FPNeg();
  2017. return FPMulAdd(valueA, value1, value2);
  2018. }
  2019. public static double FPMulSubFpscr(double valueA, double value1, double value2, bool standardFpscr)
  2020. {
  2021. value1 = value1.FPNeg();
  2022. return FPMulAddFpscr(valueA, value1, value2, standardFpscr);
  2023. }
  2024. public static double FPMulX(double value1, double value2)
  2025. {
  2026. ExecutionContext context = NativeInterface.GetContext();
  2027. FPCR fpcr = context.Fpcr;
  2028. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2029. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2030. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2031. if (!done)
  2032. {
  2033. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2034. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2035. if ((inf1 && zero2) || (zero1 && inf2))
  2036. {
  2037. result = FPTwo(sign1 ^ sign2);
  2038. }
  2039. else if (inf1 || inf2)
  2040. {
  2041. result = FPInfinity(sign1 ^ sign2);
  2042. }
  2043. else if (zero1 || zero2)
  2044. {
  2045. result = FPZero(sign1 ^ sign2);
  2046. }
  2047. else
  2048. {
  2049. result = value1 * value2;
  2050. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2051. {
  2052. context.Fpsr |= FPSR.Ufc;
  2053. result = FPZero(result < 0d);
  2054. }
  2055. }
  2056. }
  2057. return result;
  2058. }
  2059. public static double FPNegMulAdd(double valueA, double value1, double value2)
  2060. {
  2061. valueA = valueA.FPNeg();
  2062. value1 = value1.FPNeg();
  2063. return FPMulAdd(valueA, value1, value2);
  2064. }
  2065. public static double FPNegMulSub(double valueA, double value1, double value2)
  2066. {
  2067. valueA = valueA.FPNeg();
  2068. return FPMulAdd(valueA, value1, value2);
  2069. }
  2070. public static double FPRecipEstimate(double value)
  2071. {
  2072. return FPRecipEstimateFpscr(value, false);
  2073. }
  2074. public static double FPRecipEstimateFpscr(double value, bool standardFpscr)
  2075. {
  2076. ExecutionContext context = NativeInterface.GetContext();
  2077. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2078. value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2079. double result;
  2080. if (type == FPType.SNaN || type == FPType.QNaN)
  2081. {
  2082. result = FPProcessNaN(type, op, context, fpcr);
  2083. }
  2084. else if (type == FPType.Infinity)
  2085. {
  2086. result = FPZero(sign);
  2087. }
  2088. else if (type == FPType.Zero)
  2089. {
  2090. result = FPInfinity(sign);
  2091. FPProcessException(FPException.DivideByZero, context, fpcr);
  2092. }
  2093. else if (Math.Abs(value) < Math.Pow(2d, -1024))
  2094. {
  2095. bool overflowToInf;
  2096. switch (fpcr.GetRoundingMode())
  2097. {
  2098. default:
  2099. case FPRoundingMode.ToNearest: overflowToInf = true; break;
  2100. case FPRoundingMode.TowardsPlusInfinity: overflowToInf = !sign; break;
  2101. case FPRoundingMode.TowardsMinusInfinity: overflowToInf = sign; break;
  2102. case FPRoundingMode.TowardsZero: overflowToInf = false; break;
  2103. }
  2104. result = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  2105. FPProcessException(FPException.Overflow, context, fpcr);
  2106. FPProcessException(FPException.Inexact, context, fpcr);
  2107. }
  2108. else if ((fpcr & FPCR.Fz) != 0 && (Math.Abs(value) >= Math.Pow(2d, 1022)))
  2109. {
  2110. result = FPZero(sign);
  2111. context.Fpsr |= FPSR.Ufc;
  2112. }
  2113. else
  2114. {
  2115. ulong fraction = op & 0x000FFFFFFFFFFFFFul;
  2116. uint exp = (uint)((op & 0x7FF0000000000000ul) >> 52);
  2117. if (exp == 0u)
  2118. {
  2119. if ((fraction & 0x0008000000000000ul) == 0ul)
  2120. {
  2121. fraction = (fraction & 0x0003FFFFFFFFFFFFul) << 2;
  2122. exp -= 1u;
  2123. }
  2124. else
  2125. {
  2126. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  2127. }
  2128. }
  2129. uint scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  2130. uint resultExp = 2045u - exp;
  2131. uint estimate = (uint)SoftFloat.RecipEstimateTable[scaled - 256u] + 256u;
  2132. fraction = (ulong)(estimate & 0xFFu) << 44;
  2133. if (resultExp == 0u)
  2134. {
  2135. fraction = ((fraction & 0x000FFFFFFFFFFFFEul) | 0x0010000000000000ul) >> 1;
  2136. }
  2137. else if (resultExp + 1u == 0u)
  2138. {
  2139. fraction = ((fraction & 0x000FFFFFFFFFFFFCul) | 0x0010000000000000ul) >> 2;
  2140. resultExp = 0u;
  2141. }
  2142. result = BitConverter.Int64BitsToDouble(
  2143. (long)((sign ? 1ul : 0ul) << 63 | (resultExp & 0x7FFul) << 52 | (fraction & 0x000FFFFFFFFFFFFFul)));
  2144. }
  2145. return result;
  2146. }
  2147. public static double FPRecipStep(double value1, double value2)
  2148. {
  2149. ExecutionContext context = NativeInterface.GetContext();
  2150. FPCR fpcr = context.StandardFpcrValue;
  2151. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2152. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2153. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2154. if (!done)
  2155. {
  2156. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2157. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2158. double product;
  2159. if ((inf1 && zero2) || (zero1 && inf2))
  2160. {
  2161. product = FPZero(false);
  2162. }
  2163. else
  2164. {
  2165. product = FPMulFpscr(value1, value2, true);
  2166. }
  2167. result = FPSubFpscr(FPTwo(false), product, true);
  2168. }
  2169. return result;
  2170. }
  2171. public static double FPRecipStepFused(double value1, double value2)
  2172. {
  2173. ExecutionContext context = NativeInterface.GetContext();
  2174. FPCR fpcr = context.Fpcr;
  2175. value1 = value1.FPNeg();
  2176. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2177. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2178. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2179. if (!done)
  2180. {
  2181. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2182. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2183. if ((inf1 && zero2) || (zero1 && inf2))
  2184. {
  2185. result = FPTwo(false);
  2186. }
  2187. else if (inf1 || inf2)
  2188. {
  2189. result = FPInfinity(sign1 ^ sign2);
  2190. }
  2191. else
  2192. {
  2193. result = Math.FusedMultiplyAdd(value1, value2, 2d);
  2194. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2195. {
  2196. context.Fpsr |= FPSR.Ufc;
  2197. result = FPZero(result < 0d);
  2198. }
  2199. }
  2200. }
  2201. return result;
  2202. }
  2203. public static double FPRecpX(double value)
  2204. {
  2205. ExecutionContext context = NativeInterface.GetContext();
  2206. FPCR fpcr = context.Fpcr;
  2207. value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2208. double result;
  2209. if (type == FPType.SNaN || type == FPType.QNaN)
  2210. {
  2211. result = FPProcessNaN(type, op, context, fpcr);
  2212. }
  2213. else
  2214. {
  2215. ulong notExp = (~op >> 52) & 0x7FFul;
  2216. ulong maxExp = 0x7FEul;
  2217. result = BitConverter.Int64BitsToDouble(
  2218. (long)((sign ? 1ul : 0ul) << 63 | (notExp == 0x7FFul ? maxExp : notExp) << 52));
  2219. }
  2220. return result;
  2221. }
  2222. public static double FPRSqrtEstimate(double value)
  2223. {
  2224. return FPRSqrtEstimateFpscr(value, false);
  2225. }
  2226. public static double FPRSqrtEstimateFpscr(double value, bool standardFpscr)
  2227. {
  2228. ExecutionContext context = NativeInterface.GetContext();
  2229. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2230. value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2231. double result;
  2232. if (type == FPType.SNaN || type == FPType.QNaN)
  2233. {
  2234. result = FPProcessNaN(type, op, context, fpcr);
  2235. }
  2236. else if (type == FPType.Zero)
  2237. {
  2238. result = FPInfinity(sign);
  2239. FPProcessException(FPException.DivideByZero, context, fpcr);
  2240. }
  2241. else if (sign)
  2242. {
  2243. result = FPDefaultNaN();
  2244. FPProcessException(FPException.InvalidOp, context, fpcr);
  2245. }
  2246. else if (type == FPType.Infinity)
  2247. {
  2248. result = FPZero(false);
  2249. }
  2250. else
  2251. {
  2252. ulong fraction = op & 0x000FFFFFFFFFFFFFul;
  2253. uint exp = (uint)((op & 0x7FF0000000000000ul) >> 52);
  2254. if (exp == 0u)
  2255. {
  2256. while ((fraction & 0x0008000000000000ul) == 0ul)
  2257. {
  2258. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  2259. exp -= 1u;
  2260. }
  2261. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  2262. }
  2263. uint scaled;
  2264. if ((exp & 1u) == 0u)
  2265. {
  2266. scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  2267. }
  2268. else
  2269. {
  2270. scaled = (uint)(((fraction & 0x000FE00000000000ul) | 0x0010000000000000ul) >> 45);
  2271. }
  2272. uint resultExp = (3068u - exp) >> 1;
  2273. uint estimate = (uint)SoftFloat.RecipSqrtEstimateTable[scaled - 128u] + 256u;
  2274. result = BitConverter.Int64BitsToDouble((long)((resultExp & 0x7FFul) << 52 | (estimate & 0xFFul) << 44));
  2275. }
  2276. return result;
  2277. }
  2278. public static double FPHalvedSub(double value1, double value2, ExecutionContext context, FPCR fpcr)
  2279. {
  2280. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2281. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2282. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2283. if (!done)
  2284. {
  2285. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2286. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2287. if (inf1 && inf2 && sign1 == sign2)
  2288. {
  2289. result = FPDefaultNaN();
  2290. FPProcessException(FPException.InvalidOp, context, fpcr);
  2291. }
  2292. else if ((inf1 && !sign1) || (inf2 && sign2))
  2293. {
  2294. result = FPInfinity(false);
  2295. }
  2296. else if ((inf1 && sign1) || (inf2 && !sign2))
  2297. {
  2298. result = FPInfinity(true);
  2299. }
  2300. else if (zero1 && zero2 && sign1 == !sign2)
  2301. {
  2302. result = FPZero(sign1);
  2303. }
  2304. else
  2305. {
  2306. result = (value1 - value2) / 2.0;
  2307. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2308. {
  2309. context.Fpsr |= FPSR.Ufc;
  2310. result = FPZero(result < 0d);
  2311. }
  2312. }
  2313. }
  2314. return result;
  2315. }
  2316. public static double FPRSqrtStep(double value1, double value2)
  2317. {
  2318. ExecutionContext context = NativeInterface.GetContext();
  2319. FPCR fpcr = context.StandardFpcrValue;
  2320. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2321. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2322. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2323. if (!done)
  2324. {
  2325. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2326. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2327. double product;
  2328. if ((inf1 && zero2) || (zero1 && inf2))
  2329. {
  2330. product = FPZero(false);
  2331. }
  2332. else
  2333. {
  2334. product = FPMulFpscr(value1, value2, true);
  2335. }
  2336. result = FPHalvedSub(FPThree(false), product, context, fpcr);
  2337. }
  2338. return result;
  2339. }
  2340. public static double FPRSqrtStepFused(double value1, double value2)
  2341. {
  2342. ExecutionContext context = NativeInterface.GetContext();
  2343. FPCR fpcr = context.Fpcr;
  2344. value1 = value1.FPNeg();
  2345. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2346. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2347. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2348. if (!done)
  2349. {
  2350. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2351. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2352. if ((inf1 && zero2) || (zero1 && inf2))
  2353. {
  2354. result = FPOnePointFive(false);
  2355. }
  2356. else if (inf1 || inf2)
  2357. {
  2358. result = FPInfinity(sign1 ^ sign2);
  2359. }
  2360. else
  2361. {
  2362. result = Math.FusedMultiplyAdd(value1, value2, 3d) / 2d;
  2363. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2364. {
  2365. context.Fpsr |= FPSR.Ufc;
  2366. result = FPZero(result < 0d);
  2367. }
  2368. }
  2369. }
  2370. return result;
  2371. }
  2372. public static double FPSqrt(double value)
  2373. {
  2374. ExecutionContext context = NativeInterface.GetContext();
  2375. FPCR fpcr = context.Fpcr;
  2376. value = value.FPUnpack(out FPType type, out bool sign, out ulong op, context, fpcr);
  2377. double result;
  2378. if (type == FPType.SNaN || type == FPType.QNaN)
  2379. {
  2380. result = FPProcessNaN(type, op, context, fpcr);
  2381. }
  2382. else if (type == FPType.Zero)
  2383. {
  2384. result = FPZero(sign);
  2385. }
  2386. else if (type == FPType.Infinity && !sign)
  2387. {
  2388. result = FPInfinity(sign);
  2389. }
  2390. else if (sign)
  2391. {
  2392. result = FPDefaultNaN();
  2393. FPProcessException(FPException.InvalidOp, context, fpcr);
  2394. }
  2395. else
  2396. {
  2397. result = Math.Sqrt(value);
  2398. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2399. {
  2400. context.Fpsr |= FPSR.Ufc;
  2401. result = FPZero(result < 0d);
  2402. }
  2403. }
  2404. return result;
  2405. }
  2406. public static double FPSub(double value1, double value2)
  2407. {
  2408. return FPSubFpscr(value1, value2, false);
  2409. }
  2410. public static double FPSubFpscr(double value1, double value2, bool standardFpscr)
  2411. {
  2412. ExecutionContext context = NativeInterface.GetContext();
  2413. FPCR fpcr = standardFpscr ? context.StandardFpcrValue : context.Fpcr;
  2414. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context, fpcr);
  2415. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context, fpcr);
  2416. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context, fpcr);
  2417. if (!done)
  2418. {
  2419. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2420. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2421. if (inf1 && inf2 && sign1 == sign2)
  2422. {
  2423. result = FPDefaultNaN();
  2424. FPProcessException(FPException.InvalidOp, context, fpcr);
  2425. }
  2426. else if ((inf1 && !sign1) || (inf2 && sign2))
  2427. {
  2428. result = FPInfinity(false);
  2429. }
  2430. else if ((inf1 && sign1) || (inf2 && !sign2))
  2431. {
  2432. result = FPInfinity(true);
  2433. }
  2434. else if (zero1 && zero2 && sign1 == !sign2)
  2435. {
  2436. result = FPZero(sign1);
  2437. }
  2438. else
  2439. {
  2440. result = value1 - value2;
  2441. if ((fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2442. {
  2443. context.Fpsr |= FPSR.Ufc;
  2444. result = FPZero(result < 0d);
  2445. }
  2446. }
  2447. }
  2448. return result;
  2449. }
  2450. private static double FPDefaultNaN()
  2451. {
  2452. return BitConverter.Int64BitsToDouble(0x7ff8000000000000);
  2453. }
  2454. private static double FPInfinity(bool sign)
  2455. {
  2456. return sign ? double.NegativeInfinity : double.PositiveInfinity;
  2457. }
  2458. private static double FPZero(bool sign)
  2459. {
  2460. return sign ? -0d : +0d;
  2461. }
  2462. private static double FPMaxNormal(bool sign)
  2463. {
  2464. return sign ? double.MinValue : double.MaxValue;
  2465. }
  2466. private static double FPTwo(bool sign)
  2467. {
  2468. return sign ? -2d : +2d;
  2469. }
  2470. private static double FPThree(bool sign)
  2471. {
  2472. return sign ? -3d : +3d;
  2473. }
  2474. private static double FPOnePointFive(bool sign)
  2475. {
  2476. return sign ? -1.5d : +1.5d;
  2477. }
  2478. private static double FPNeg(this double value)
  2479. {
  2480. return -value;
  2481. }
  2482. private static double ZerosOrOnes(bool ones)
  2483. {
  2484. return BitConverter.Int64BitsToDouble(ones ? -1L : 0L);
  2485. }
  2486. private static double FPUnpack(
  2487. this double value,
  2488. out FPType type,
  2489. out bool sign,
  2490. out ulong valueBits,
  2491. ExecutionContext context,
  2492. FPCR fpcr)
  2493. {
  2494. valueBits = (ulong)BitConverter.DoubleToInt64Bits(value);
  2495. sign = (~valueBits & 0x8000000000000000ul) == 0ul;
  2496. if ((valueBits & 0x7FF0000000000000ul) == 0ul)
  2497. {
  2498. if ((valueBits & 0x000FFFFFFFFFFFFFul) == 0ul || (fpcr & FPCR.Fz) != 0)
  2499. {
  2500. type = FPType.Zero;
  2501. value = FPZero(sign);
  2502. if ((valueBits & 0x000FFFFFFFFFFFFFul) != 0ul)
  2503. {
  2504. FPProcessException(FPException.InputDenorm, context, fpcr);
  2505. }
  2506. }
  2507. else
  2508. {
  2509. type = FPType.Nonzero;
  2510. }
  2511. }
  2512. else if ((~valueBits & 0x7FF0000000000000ul) == 0ul)
  2513. {
  2514. if ((valueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  2515. {
  2516. type = FPType.Infinity;
  2517. }
  2518. else
  2519. {
  2520. type = (~valueBits & 0x0008000000000000ul) == 0ul ? FPType.QNaN : FPType.SNaN;
  2521. value = FPZero(sign);
  2522. }
  2523. }
  2524. else
  2525. {
  2526. type = FPType.Nonzero;
  2527. }
  2528. return value;
  2529. }
  2530. private static double FPProcessNaNs(
  2531. FPType type1,
  2532. FPType type2,
  2533. ulong op1,
  2534. ulong op2,
  2535. out bool done,
  2536. ExecutionContext context,
  2537. FPCR fpcr)
  2538. {
  2539. done = true;
  2540. if (type1 == FPType.SNaN)
  2541. {
  2542. return FPProcessNaN(type1, op1, context, fpcr);
  2543. }
  2544. else if (type2 == FPType.SNaN)
  2545. {
  2546. return FPProcessNaN(type2, op2, context, fpcr);
  2547. }
  2548. else if (type1 == FPType.QNaN)
  2549. {
  2550. return FPProcessNaN(type1, op1, context, fpcr);
  2551. }
  2552. else if (type2 == FPType.QNaN)
  2553. {
  2554. return FPProcessNaN(type2, op2, context, fpcr);
  2555. }
  2556. done = false;
  2557. return FPZero(false);
  2558. }
  2559. private static double FPProcessNaNs3(
  2560. FPType type1,
  2561. FPType type2,
  2562. FPType type3,
  2563. ulong op1,
  2564. ulong op2,
  2565. ulong op3,
  2566. out bool done,
  2567. ExecutionContext context,
  2568. FPCR fpcr)
  2569. {
  2570. done = true;
  2571. if (type1 == FPType.SNaN)
  2572. {
  2573. return FPProcessNaN(type1, op1, context, fpcr);
  2574. }
  2575. else if (type2 == FPType.SNaN)
  2576. {
  2577. return FPProcessNaN(type2, op2, context, fpcr);
  2578. }
  2579. else if (type3 == FPType.SNaN)
  2580. {
  2581. return FPProcessNaN(type3, op3, context, fpcr);
  2582. }
  2583. else if (type1 == FPType.QNaN)
  2584. {
  2585. return FPProcessNaN(type1, op1, context, fpcr);
  2586. }
  2587. else if (type2 == FPType.QNaN)
  2588. {
  2589. return FPProcessNaN(type2, op2, context, fpcr);
  2590. }
  2591. else if (type3 == FPType.QNaN)
  2592. {
  2593. return FPProcessNaN(type3, op3, context, fpcr);
  2594. }
  2595. done = false;
  2596. return FPZero(false);
  2597. }
  2598. private static double FPProcessNaN(FPType type, ulong op, ExecutionContext context, FPCR fpcr)
  2599. {
  2600. if (type == FPType.SNaN)
  2601. {
  2602. op |= 1ul << 51;
  2603. FPProcessException(FPException.InvalidOp, context, fpcr);
  2604. }
  2605. if ((fpcr & FPCR.Dn) != 0)
  2606. {
  2607. return FPDefaultNaN();
  2608. }
  2609. return BitConverter.Int64BitsToDouble((long)op);
  2610. }
  2611. private static void FPProcessException(FPException exc, ExecutionContext context, FPCR fpcr)
  2612. {
  2613. int enable = (int)exc + 8;
  2614. if ((fpcr & (FPCR)(1 << enable)) != 0)
  2615. {
  2616. throw new NotImplementedException("Floating-point trap handling.");
  2617. }
  2618. else
  2619. {
  2620. context.Fpsr |= (FPSR)(1 << (int)exc);
  2621. }
  2622. }
  2623. }
  2624. }