SoftFloat.cs 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767
  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 -float.NaN;
  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. ExecutionContext context = NativeInterface.GetContext();
  511. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  512. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  513. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  514. if (!done)
  515. {
  516. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  517. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  518. if (inf1 && inf2 && sign1 == !sign2)
  519. {
  520. result = FPDefaultNaN();
  521. FPProcessException(FPException.InvalidOp, context);
  522. }
  523. else if ((inf1 && !sign1) || (inf2 && !sign2))
  524. {
  525. result = FPInfinity(false);
  526. }
  527. else if ((inf1 && sign1) || (inf2 && sign2))
  528. {
  529. result = FPInfinity(true);
  530. }
  531. else if (zero1 && zero2 && sign1 == sign2)
  532. {
  533. result = FPZero(sign1);
  534. }
  535. else
  536. {
  537. result = value1 + value2;
  538. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  539. {
  540. context.Fpsr |= FPSR.Ufc;
  541. result = FPZero(result < 0f);
  542. }
  543. }
  544. }
  545. return result;
  546. }
  547. public static int FPCompare(float value1, float value2, bool signalNaNs)
  548. {
  549. ExecutionContext context = NativeInterface.GetContext();
  550. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out _, context);
  551. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out _, context);
  552. int result;
  553. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  554. {
  555. result = 0b0011;
  556. if (type1 == FPType.SNaN || type2 == FPType.SNaN || signalNaNs)
  557. {
  558. FPProcessException(FPException.InvalidOp, context);
  559. }
  560. }
  561. else
  562. {
  563. if (value1 == value2)
  564. {
  565. result = 0b0110;
  566. }
  567. else if (value1 < value2)
  568. {
  569. result = 0b1000;
  570. }
  571. else
  572. {
  573. result = 0b0010;
  574. }
  575. }
  576. return result;
  577. }
  578. public static float FPCompareEQ(float value1, float value2)
  579. {
  580. ExecutionContext context = NativeInterface.GetContext();
  581. value1 = value1.FPUnpack(out FPType type1, out _, out _, context);
  582. value2 = value2.FPUnpack(out FPType type2, out _, out _, context);
  583. float result;
  584. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  585. {
  586. result = ZerosOrOnes(false);
  587. if (type1 == FPType.SNaN || type2 == FPType.SNaN)
  588. {
  589. FPProcessException(FPException.InvalidOp, context);
  590. }
  591. }
  592. else
  593. {
  594. result = ZerosOrOnes(value1 == value2);
  595. }
  596. return result;
  597. }
  598. public static float FPCompareGE(float value1, float value2)
  599. {
  600. ExecutionContext context = NativeInterface.GetContext();
  601. value1 = value1.FPUnpack(out FPType type1, out _, out _, context);
  602. value2 = value2.FPUnpack(out FPType type2, out _, out _, context);
  603. float result;
  604. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  605. {
  606. result = ZerosOrOnes(false);
  607. FPProcessException(FPException.InvalidOp, context);
  608. }
  609. else
  610. {
  611. result = ZerosOrOnes(value1 >= value2);
  612. }
  613. return result;
  614. }
  615. public static float FPCompareGT(float value1, float value2)
  616. {
  617. ExecutionContext context = NativeInterface.GetContext();
  618. value1 = value1.FPUnpack(out FPType type1, out _, out _, context);
  619. value2 = value2.FPUnpack(out FPType type2, out _, out _, context);
  620. float result;
  621. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  622. {
  623. result = ZerosOrOnes(false);
  624. FPProcessException(FPException.InvalidOp, context);
  625. }
  626. else
  627. {
  628. result = ZerosOrOnes(value1 > value2);
  629. }
  630. return result;
  631. }
  632. public static float FPCompareLE(float value1, float value2)
  633. {
  634. return FPCompareGE(value2, value1);
  635. }
  636. public static float FPCompareLT(float value1, float value2)
  637. {
  638. return FPCompareGT(value2, value1);
  639. }
  640. public static float FPDiv(float value1, float value2)
  641. {
  642. ExecutionContext context = NativeInterface.GetContext();
  643. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  644. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  645. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  646. if (!done)
  647. {
  648. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  649. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  650. if ((inf1 && inf2) || (zero1 && zero2))
  651. {
  652. result = FPDefaultNaN();
  653. FPProcessException(FPException.InvalidOp, context);
  654. }
  655. else if (inf1 || zero2)
  656. {
  657. result = FPInfinity(sign1 ^ sign2);
  658. if (!inf1)
  659. {
  660. FPProcessException(FPException.DivideByZero, context);
  661. }
  662. }
  663. else if (zero1 || inf2)
  664. {
  665. result = FPZero(sign1 ^ sign2);
  666. }
  667. else
  668. {
  669. result = value1 / value2;
  670. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  671. {
  672. context.Fpsr |= FPSR.Ufc;
  673. result = FPZero(result < 0f);
  674. }
  675. }
  676. }
  677. return result;
  678. }
  679. public static float FPMax(float value1, float value2)
  680. {
  681. ExecutionContext context = NativeInterface.GetContext();
  682. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  683. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  684. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  685. if (!done)
  686. {
  687. if (value1 > value2)
  688. {
  689. if (type1 == FPType.Infinity)
  690. {
  691. result = FPInfinity(sign1);
  692. }
  693. else if (type1 == FPType.Zero)
  694. {
  695. result = FPZero(sign1 && sign2);
  696. }
  697. else
  698. {
  699. result = value1;
  700. }
  701. }
  702. else
  703. {
  704. if (type2 == FPType.Infinity)
  705. {
  706. result = FPInfinity(sign2);
  707. }
  708. else if (type2 == FPType.Zero)
  709. {
  710. result = FPZero(sign1 && sign2);
  711. }
  712. else
  713. {
  714. result = value2;
  715. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  716. {
  717. context.Fpsr |= FPSR.Ufc;
  718. result = FPZero(result < 0f);
  719. }
  720. }
  721. }
  722. }
  723. return result;
  724. }
  725. public static float FPMaxNum(float value1, float value2)
  726. {
  727. ExecutionContext context = NativeInterface.GetContext();
  728. value1.FPUnpack(out FPType type1, out _, out _, context);
  729. value2.FPUnpack(out FPType type2, out _, out _, context);
  730. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  731. {
  732. value1 = FPInfinity(true);
  733. }
  734. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  735. {
  736. value2 = FPInfinity(true);
  737. }
  738. return FPMax(value1, value2);
  739. }
  740. public static float FPMin(float value1, float value2)
  741. {
  742. ExecutionContext context = NativeInterface.GetContext();
  743. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  744. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  745. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  746. if (!done)
  747. {
  748. if (value1 < value2)
  749. {
  750. if (type1 == FPType.Infinity)
  751. {
  752. result = FPInfinity(sign1);
  753. }
  754. else if (type1 == FPType.Zero)
  755. {
  756. result = FPZero(sign1 || sign2);
  757. }
  758. else
  759. {
  760. result = value1;
  761. }
  762. }
  763. else
  764. {
  765. if (type2 == FPType.Infinity)
  766. {
  767. result = FPInfinity(sign2);
  768. }
  769. else if (type2 == FPType.Zero)
  770. {
  771. result = FPZero(sign1 || sign2);
  772. }
  773. else
  774. {
  775. result = value2;
  776. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  777. {
  778. context.Fpsr |= FPSR.Ufc;
  779. result = FPZero(result < 0f);
  780. }
  781. }
  782. }
  783. }
  784. return result;
  785. }
  786. public static float FPMinNum(float value1, float value2)
  787. {
  788. ExecutionContext context = NativeInterface.GetContext();
  789. value1.FPUnpack(out FPType type1, out _, out _, context);
  790. value2.FPUnpack(out FPType type2, out _, out _, context);
  791. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  792. {
  793. value1 = FPInfinity(false);
  794. }
  795. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  796. {
  797. value2 = FPInfinity(false);
  798. }
  799. return FPMin(value1, value2);
  800. }
  801. public static float FPMul(float value1, float value2)
  802. {
  803. ExecutionContext context = NativeInterface.GetContext();
  804. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  805. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  806. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  807. if (!done)
  808. {
  809. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  810. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  811. if ((inf1 && zero2) || (zero1 && inf2))
  812. {
  813. result = FPDefaultNaN();
  814. FPProcessException(FPException.InvalidOp, context);
  815. }
  816. else if (inf1 || inf2)
  817. {
  818. result = FPInfinity(sign1 ^ sign2);
  819. }
  820. else if (zero1 || zero2)
  821. {
  822. result = FPZero(sign1 ^ sign2);
  823. }
  824. else
  825. {
  826. result = value1 * value2;
  827. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  828. {
  829. context.Fpsr |= FPSR.Ufc;
  830. result = FPZero(result < 0f);
  831. }
  832. }
  833. }
  834. return result;
  835. }
  836. public static float FPMulAdd(float valueA, float value1, float value2)
  837. {
  838. ExecutionContext context = NativeInterface.GetContext();
  839. valueA = valueA.FPUnpack(out FPType typeA, out bool signA, out uint addend, context);
  840. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  841. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  842. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  843. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  844. float result = FPProcessNaNs3(typeA, type1, type2, addend, op1, op2, out bool done, context);
  845. if (typeA == FPType.QNaN && ((inf1 && zero2) || (zero1 && inf2)))
  846. {
  847. result = FPDefaultNaN();
  848. FPProcessException(FPException.InvalidOp, context);
  849. }
  850. if (!done)
  851. {
  852. bool infA = typeA == FPType.Infinity; bool zeroA = typeA == FPType.Zero;
  853. bool signP = sign1 ^ sign2;
  854. bool infP = inf1 || inf2;
  855. bool zeroP = zero1 || zero2;
  856. if ((inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP))
  857. {
  858. result = FPDefaultNaN();
  859. FPProcessException(FPException.InvalidOp, context);
  860. }
  861. else if ((infA && !signA) || (infP && !signP))
  862. {
  863. result = FPInfinity(false);
  864. }
  865. else if ((infA && signA) || (infP && signP))
  866. {
  867. result = FPInfinity(true);
  868. }
  869. else if (zeroA && zeroP && signA == signP)
  870. {
  871. result = FPZero(signA);
  872. }
  873. else
  874. {
  875. result = MathF.FusedMultiplyAdd(value1, value2, valueA);
  876. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  877. {
  878. context.Fpsr |= FPSR.Ufc;
  879. result = FPZero(result < 0f);
  880. }
  881. }
  882. }
  883. return result;
  884. }
  885. public static float FPMulSub(float valueA, float value1, float value2)
  886. {
  887. value1 = value1.FPNeg();
  888. return FPMulAdd(valueA, value1, value2);
  889. }
  890. public static float FPMulX(float value1, float value2)
  891. {
  892. ExecutionContext context = NativeInterface.GetContext();
  893. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  894. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  895. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  896. if (!done)
  897. {
  898. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  899. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  900. if ((inf1 && zero2) || (zero1 && inf2))
  901. {
  902. result = FPTwo(sign1 ^ sign2);
  903. }
  904. else if (inf1 || inf2)
  905. {
  906. result = FPInfinity(sign1 ^ sign2);
  907. }
  908. else if (zero1 || zero2)
  909. {
  910. result = FPZero(sign1 ^ sign2);
  911. }
  912. else
  913. {
  914. result = value1 * value2;
  915. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  916. {
  917. context.Fpsr |= FPSR.Ufc;
  918. result = FPZero(result < 0f);
  919. }
  920. }
  921. }
  922. return result;
  923. }
  924. public static float FPNegMulAdd(float valueA, float value1, float value2)
  925. {
  926. valueA = valueA.FPNeg();
  927. value1 = value1.FPNeg();
  928. return FPMulAdd(valueA, value1, value2);
  929. }
  930. public static float FPNegMulSub(float valueA, float value1, float value2)
  931. {
  932. valueA = valueA.FPNeg();
  933. return FPMulAdd(valueA, value1, value2);
  934. }
  935. public static float FPRecipEstimate(float value)
  936. {
  937. ExecutionContext context = NativeInterface.GetContext();
  938. value.FPUnpack(out FPType type, out bool sign, out uint op, context);
  939. float result;
  940. if (type == FPType.SNaN || type == FPType.QNaN)
  941. {
  942. result = FPProcessNaN(type, op, context);
  943. }
  944. else if (type == FPType.Infinity)
  945. {
  946. result = FPZero(sign);
  947. }
  948. else if (type == FPType.Zero)
  949. {
  950. result = FPInfinity(sign);
  951. FPProcessException(FPException.DivideByZero, context);
  952. }
  953. else if (MathF.Abs(value) < MathF.Pow(2f, -128))
  954. {
  955. bool overflowToInf;
  956. switch (context.Fpcr.GetRoundingMode())
  957. {
  958. default:
  959. case FPRoundingMode.ToNearest: overflowToInf = true; break;
  960. case FPRoundingMode.TowardsPlusInfinity: overflowToInf = !sign; break;
  961. case FPRoundingMode.TowardsMinusInfinity: overflowToInf = sign; break;
  962. case FPRoundingMode.TowardsZero: overflowToInf = false; break;
  963. }
  964. result = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  965. FPProcessException(FPException.Overflow, context);
  966. FPProcessException(FPException.Inexact, context);
  967. }
  968. else if ((context.Fpcr & FPCR.Fz) != 0 && (MathF.Abs(value) >= MathF.Pow(2f, 126)))
  969. {
  970. result = FPZero(sign);
  971. context.Fpsr |= FPSR.Ufc;
  972. }
  973. else
  974. {
  975. ulong fraction = (ulong)(op & 0x007FFFFFu) << 29;
  976. uint exp = (op & 0x7F800000u) >> 23;
  977. if (exp == 0u)
  978. {
  979. if ((fraction & 0x0008000000000000ul) == 0ul)
  980. {
  981. fraction = (fraction & 0x0003FFFFFFFFFFFFul) << 2;
  982. exp -= 1u;
  983. }
  984. else
  985. {
  986. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  987. }
  988. }
  989. uint scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  990. uint resultExp = 253u - exp;
  991. uint estimate = (uint)SoftFloat.RecipEstimateTable[scaled - 256u] + 256u;
  992. fraction = (ulong)(estimate & 0xFFu) << 44;
  993. if (resultExp == 0u)
  994. {
  995. fraction = ((fraction & 0x000FFFFFFFFFFFFEul) | 0x0010000000000000ul) >> 1;
  996. }
  997. else if (resultExp + 1u == 0u)
  998. {
  999. fraction = ((fraction & 0x000FFFFFFFFFFFFCul) | 0x0010000000000000ul) >> 2;
  1000. resultExp = 0u;
  1001. }
  1002. result = BitConverter.Int32BitsToSingle(
  1003. (int)((sign ? 1u : 0u) << 31 | (resultExp & 0xFFu) << 23 | (uint)(fraction >> 29) & 0x007FFFFFu));
  1004. }
  1005. return result;
  1006. }
  1007. public static float FPRecipStepFused(float value1, float value2)
  1008. {
  1009. ExecutionContext context = NativeInterface.GetContext();
  1010. value1 = value1.FPNeg();
  1011. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  1012. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  1013. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1014. if (!done)
  1015. {
  1016. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1017. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1018. if ((inf1 && zero2) || (zero1 && inf2))
  1019. {
  1020. result = FPTwo(false);
  1021. }
  1022. else if (inf1 || inf2)
  1023. {
  1024. result = FPInfinity(sign1 ^ sign2);
  1025. }
  1026. else
  1027. {
  1028. result = MathF.FusedMultiplyAdd(value1, value2, 2f);
  1029. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1030. {
  1031. context.Fpsr |= FPSR.Ufc;
  1032. result = FPZero(result < 0f);
  1033. }
  1034. }
  1035. }
  1036. return result;
  1037. }
  1038. public static float FPRecpX(float value)
  1039. {
  1040. ExecutionContext context = NativeInterface.GetContext();
  1041. value.FPUnpack(out FPType type, out bool sign, out uint op, context);
  1042. float result;
  1043. if (type == FPType.SNaN || type == FPType.QNaN)
  1044. {
  1045. result = FPProcessNaN(type, op, context);
  1046. }
  1047. else
  1048. {
  1049. uint notExp = (~op >> 23) & 0xFFu;
  1050. uint maxExp = 0xFEu;
  1051. result = BitConverter.Int32BitsToSingle(
  1052. (int)((sign ? 1u : 0u) << 31 | (notExp == 0xFFu ? maxExp : notExp) << 23));
  1053. }
  1054. return result;
  1055. }
  1056. public static float FPRSqrtEstimate(float value)
  1057. {
  1058. ExecutionContext context = NativeInterface.GetContext();
  1059. value.FPUnpack(out FPType type, out bool sign, out uint op, context);
  1060. float result;
  1061. if (type == FPType.SNaN || type == FPType.QNaN)
  1062. {
  1063. result = FPProcessNaN(type, op, context);
  1064. }
  1065. else if (type == FPType.Zero)
  1066. {
  1067. result = FPInfinity(sign);
  1068. FPProcessException(FPException.DivideByZero, context);
  1069. }
  1070. else if (sign)
  1071. {
  1072. result = FPDefaultNaN();
  1073. FPProcessException(FPException.InvalidOp, context);
  1074. }
  1075. else if (type == FPType.Infinity)
  1076. {
  1077. result = FPZero(false);
  1078. }
  1079. else
  1080. {
  1081. ulong fraction = (ulong)(op & 0x007FFFFFu) << 29;
  1082. uint exp = (op & 0x7F800000u) >> 23;
  1083. if (exp == 0u)
  1084. {
  1085. while ((fraction & 0x0008000000000000ul) == 0ul)
  1086. {
  1087. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1088. exp -= 1u;
  1089. }
  1090. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1091. }
  1092. uint scaled;
  1093. if ((exp & 1u) == 0u)
  1094. {
  1095. scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1096. }
  1097. else
  1098. {
  1099. scaled = (uint)(((fraction & 0x000FE00000000000ul) | 0x0010000000000000ul) >> 45);
  1100. }
  1101. uint resultExp = (380u - exp) >> 1;
  1102. uint estimate = (uint)SoftFloat.RecipSqrtEstimateTable[scaled - 128u] + 256u;
  1103. result = BitConverter.Int32BitsToSingle((int)((resultExp & 0xFFu) << 23 | (estimate & 0xFFu) << 15));
  1104. }
  1105. return result;
  1106. }
  1107. public static float FPRSqrtStepFused(float value1, float value2)
  1108. {
  1109. ExecutionContext context = NativeInterface.GetContext();
  1110. value1 = value1.FPNeg();
  1111. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  1112. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  1113. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1114. if (!done)
  1115. {
  1116. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1117. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1118. if ((inf1 && zero2) || (zero1 && inf2))
  1119. {
  1120. result = FPOnePointFive(false);
  1121. }
  1122. else if (inf1 || inf2)
  1123. {
  1124. result = FPInfinity(sign1 ^ sign2);
  1125. }
  1126. else
  1127. {
  1128. result = MathF.FusedMultiplyAdd(value1, value2, 3f) / 2f;
  1129. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1130. {
  1131. context.Fpsr |= FPSR.Ufc;
  1132. result = FPZero(result < 0f);
  1133. }
  1134. }
  1135. }
  1136. return result;
  1137. }
  1138. public static float FPSqrt(float value)
  1139. {
  1140. ExecutionContext context = NativeInterface.GetContext();
  1141. value = value.FPUnpack(out FPType type, out bool sign, out uint op, context);
  1142. float result;
  1143. if (type == FPType.SNaN || type == FPType.QNaN)
  1144. {
  1145. result = FPProcessNaN(type, op, context);
  1146. }
  1147. else if (type == FPType.Zero)
  1148. {
  1149. result = FPZero(sign);
  1150. }
  1151. else if (type == FPType.Infinity && !sign)
  1152. {
  1153. result = FPInfinity(sign);
  1154. }
  1155. else if (sign)
  1156. {
  1157. result = FPDefaultNaN();
  1158. FPProcessException(FPException.InvalidOp, context);
  1159. }
  1160. else
  1161. {
  1162. result = MathF.Sqrt(value);
  1163. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1164. {
  1165. context.Fpsr |= FPSR.Ufc;
  1166. result = FPZero(result < 0f);
  1167. }
  1168. }
  1169. return result;
  1170. }
  1171. public static float FPSub(float value1, float value2)
  1172. {
  1173. ExecutionContext context = NativeInterface.GetContext();
  1174. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out uint op1, context);
  1175. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out uint op2, context);
  1176. float result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1177. if (!done)
  1178. {
  1179. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1180. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1181. if (inf1 && inf2 && sign1 == sign2)
  1182. {
  1183. result = FPDefaultNaN();
  1184. FPProcessException(FPException.InvalidOp, context);
  1185. }
  1186. else if ((inf1 && !sign1) || (inf2 && sign2))
  1187. {
  1188. result = FPInfinity(false);
  1189. }
  1190. else if ((inf1 && sign1) || (inf2 && !sign2))
  1191. {
  1192. result = FPInfinity(true);
  1193. }
  1194. else if (zero1 && zero2 && sign1 == !sign2)
  1195. {
  1196. result = FPZero(sign1);
  1197. }
  1198. else
  1199. {
  1200. result = value1 - value2;
  1201. if ((context.Fpcr & FPCR.Fz) != 0 && float.IsSubnormal(result))
  1202. {
  1203. context.Fpsr |= FPSR.Ufc;
  1204. result = FPZero(result < 0f);
  1205. }
  1206. }
  1207. }
  1208. return result;
  1209. }
  1210. private static float FPDefaultNaN()
  1211. {
  1212. return -float.NaN;
  1213. }
  1214. private static float FPInfinity(bool sign)
  1215. {
  1216. return sign ? float.NegativeInfinity : float.PositiveInfinity;
  1217. }
  1218. private static float FPZero(bool sign)
  1219. {
  1220. return sign ? -0f : +0f;
  1221. }
  1222. private static float FPMaxNormal(bool sign)
  1223. {
  1224. return sign ? float.MinValue : float.MaxValue;
  1225. }
  1226. private static float FPTwo(bool sign)
  1227. {
  1228. return sign ? -2f : +2f;
  1229. }
  1230. private static float FPOnePointFive(bool sign)
  1231. {
  1232. return sign ? -1.5f : +1.5f;
  1233. }
  1234. private static float FPNeg(this float value)
  1235. {
  1236. return -value;
  1237. }
  1238. private static float ZerosOrOnes(bool ones)
  1239. {
  1240. return BitConverter.Int32BitsToSingle(ones ? -1 : 0);
  1241. }
  1242. private static float FPUnpack(
  1243. this float value,
  1244. out FPType type,
  1245. out bool sign,
  1246. out uint valueBits,
  1247. ExecutionContext context)
  1248. {
  1249. valueBits = (uint)BitConverter.SingleToInt32Bits(value);
  1250. sign = (~valueBits & 0x80000000u) == 0u;
  1251. if ((valueBits & 0x7F800000u) == 0u)
  1252. {
  1253. if ((valueBits & 0x007FFFFFu) == 0u || (context.Fpcr & FPCR.Fz) != 0)
  1254. {
  1255. type = FPType.Zero;
  1256. value = FPZero(sign);
  1257. if ((valueBits & 0x007FFFFFu) != 0u)
  1258. {
  1259. FPProcessException(FPException.InputDenorm, context);
  1260. }
  1261. }
  1262. else
  1263. {
  1264. type = FPType.Nonzero;
  1265. }
  1266. }
  1267. else if ((~valueBits & 0x7F800000u) == 0u)
  1268. {
  1269. if ((valueBits & 0x007FFFFFu) == 0u)
  1270. {
  1271. type = FPType.Infinity;
  1272. }
  1273. else
  1274. {
  1275. type = (~valueBits & 0x00400000u) == 0u ? FPType.QNaN : FPType.SNaN;
  1276. value = FPZero(sign);
  1277. }
  1278. }
  1279. else
  1280. {
  1281. type = FPType.Nonzero;
  1282. }
  1283. return value;
  1284. }
  1285. private static float FPProcessNaNs(
  1286. FPType type1,
  1287. FPType type2,
  1288. uint op1,
  1289. uint op2,
  1290. out bool done,
  1291. ExecutionContext context)
  1292. {
  1293. done = true;
  1294. if (type1 == FPType.SNaN)
  1295. {
  1296. return FPProcessNaN(type1, op1, context);
  1297. }
  1298. else if (type2 == FPType.SNaN)
  1299. {
  1300. return FPProcessNaN(type2, op2, context);
  1301. }
  1302. else if (type1 == FPType.QNaN)
  1303. {
  1304. return FPProcessNaN(type1, op1, context);
  1305. }
  1306. else if (type2 == FPType.QNaN)
  1307. {
  1308. return FPProcessNaN(type2, op2, context);
  1309. }
  1310. done = false;
  1311. return FPZero(false);
  1312. }
  1313. private static float FPProcessNaNs3(
  1314. FPType type1,
  1315. FPType type2,
  1316. FPType type3,
  1317. uint op1,
  1318. uint op2,
  1319. uint op3,
  1320. out bool done,
  1321. ExecutionContext context)
  1322. {
  1323. done = true;
  1324. if (type1 == FPType.SNaN)
  1325. {
  1326. return FPProcessNaN(type1, op1, context);
  1327. }
  1328. else if (type2 == FPType.SNaN)
  1329. {
  1330. return FPProcessNaN(type2, op2, context);
  1331. }
  1332. else if (type3 == FPType.SNaN)
  1333. {
  1334. return FPProcessNaN(type3, op3, context);
  1335. }
  1336. else if (type1 == FPType.QNaN)
  1337. {
  1338. return FPProcessNaN(type1, op1, context);
  1339. }
  1340. else if (type2 == FPType.QNaN)
  1341. {
  1342. return FPProcessNaN(type2, op2, context);
  1343. }
  1344. else if (type3 == FPType.QNaN)
  1345. {
  1346. return FPProcessNaN(type3, op3, context);
  1347. }
  1348. done = false;
  1349. return FPZero(false);
  1350. }
  1351. private static float FPProcessNaN(FPType type, uint op, ExecutionContext context)
  1352. {
  1353. if (type == FPType.SNaN)
  1354. {
  1355. op |= 1u << 22;
  1356. FPProcessException(FPException.InvalidOp, context);
  1357. }
  1358. if ((context.Fpcr & FPCR.Dn) != 0)
  1359. {
  1360. return FPDefaultNaN();
  1361. }
  1362. return BitConverter.Int32BitsToSingle((int)op);
  1363. }
  1364. private static void FPProcessException(FPException exc, ExecutionContext context)
  1365. {
  1366. int enable = (int)exc + 8;
  1367. if ((context.Fpcr & (FPCR)(1 << enable)) != 0)
  1368. {
  1369. throw new NotImplementedException("Floating-point trap handling.");
  1370. }
  1371. else
  1372. {
  1373. context.Fpsr |= (FPSR)(1 << (int)exc);
  1374. }
  1375. }
  1376. }
  1377. static class SoftFloat64
  1378. {
  1379. public static double FPAdd(double value1, double value2)
  1380. {
  1381. ExecutionContext context = NativeInterface.GetContext();
  1382. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1383. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1384. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1385. if (!done)
  1386. {
  1387. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1388. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1389. if (inf1 && inf2 && sign1 == !sign2)
  1390. {
  1391. result = FPDefaultNaN();
  1392. FPProcessException(FPException.InvalidOp, context);
  1393. }
  1394. else if ((inf1 && !sign1) || (inf2 && !sign2))
  1395. {
  1396. result = FPInfinity(false);
  1397. }
  1398. else if ((inf1 && sign1) || (inf2 && sign2))
  1399. {
  1400. result = FPInfinity(true);
  1401. }
  1402. else if (zero1 && zero2 && sign1 == sign2)
  1403. {
  1404. result = FPZero(sign1);
  1405. }
  1406. else
  1407. {
  1408. result = value1 + value2;
  1409. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1410. {
  1411. context.Fpsr |= FPSR.Ufc;
  1412. result = FPZero(result < 0d);
  1413. }
  1414. }
  1415. }
  1416. return result;
  1417. }
  1418. public static int FPCompare(double value1, double value2, bool signalNaNs)
  1419. {
  1420. ExecutionContext context = NativeInterface.GetContext();
  1421. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out _, context);
  1422. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out _, context);
  1423. int result;
  1424. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1425. {
  1426. result = 0b0011;
  1427. if (type1 == FPType.SNaN || type2 == FPType.SNaN || signalNaNs)
  1428. {
  1429. FPProcessException(FPException.InvalidOp, context);
  1430. }
  1431. }
  1432. else
  1433. {
  1434. if (value1 == value2)
  1435. {
  1436. result = 0b0110;
  1437. }
  1438. else if (value1 < value2)
  1439. {
  1440. result = 0b1000;
  1441. }
  1442. else
  1443. {
  1444. result = 0b0010;
  1445. }
  1446. }
  1447. return result;
  1448. }
  1449. public static double FPCompareEQ(double value1, double value2)
  1450. {
  1451. ExecutionContext context = NativeInterface.GetContext();
  1452. value1 = value1.FPUnpack(out FPType type1, out _, out _, context);
  1453. value2 = value2.FPUnpack(out FPType type2, out _, out _, context);
  1454. double result;
  1455. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1456. {
  1457. result = ZerosOrOnes(false);
  1458. if (type1 == FPType.SNaN || type2 == FPType.SNaN)
  1459. {
  1460. FPProcessException(FPException.InvalidOp, context);
  1461. }
  1462. }
  1463. else
  1464. {
  1465. result = ZerosOrOnes(value1 == value2);
  1466. }
  1467. return result;
  1468. }
  1469. public static double FPCompareGE(double value1, double value2)
  1470. {
  1471. ExecutionContext context = NativeInterface.GetContext();
  1472. value1 = value1.FPUnpack(out FPType type1, out _, out _, context);
  1473. value2 = value2.FPUnpack(out FPType type2, out _, out _, context);
  1474. double result;
  1475. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1476. {
  1477. result = ZerosOrOnes(false);
  1478. FPProcessException(FPException.InvalidOp, context);
  1479. }
  1480. else
  1481. {
  1482. result = ZerosOrOnes(value1 >= value2);
  1483. }
  1484. return result;
  1485. }
  1486. public static double FPCompareGT(double value1, double value2)
  1487. {
  1488. ExecutionContext context = NativeInterface.GetContext();
  1489. value1 = value1.FPUnpack(out FPType type1, out _, out _, context);
  1490. value2 = value2.FPUnpack(out FPType type2, out _, out _, context);
  1491. double result;
  1492. if (type1 == FPType.SNaN || type1 == FPType.QNaN || type2 == FPType.SNaN || type2 == FPType.QNaN)
  1493. {
  1494. result = ZerosOrOnes(false);
  1495. FPProcessException(FPException.InvalidOp, context);
  1496. }
  1497. else
  1498. {
  1499. result = ZerosOrOnes(value1 > value2);
  1500. }
  1501. return result;
  1502. }
  1503. public static double FPCompareLE(double value1, double value2)
  1504. {
  1505. return FPCompareGE(value2, value1);
  1506. }
  1507. public static double FPCompareLT(double value1, double value2)
  1508. {
  1509. return FPCompareGT(value2, value1);
  1510. }
  1511. public static double FPDiv(double value1, double value2)
  1512. {
  1513. ExecutionContext context = NativeInterface.GetContext();
  1514. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1515. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1516. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1517. if (!done)
  1518. {
  1519. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1520. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1521. if ((inf1 && inf2) || (zero1 && zero2))
  1522. {
  1523. result = FPDefaultNaN();
  1524. FPProcessException(FPException.InvalidOp, context);
  1525. }
  1526. else if (inf1 || zero2)
  1527. {
  1528. result = FPInfinity(sign1 ^ sign2);
  1529. if (!inf1)
  1530. {
  1531. FPProcessException(FPException.DivideByZero, context);
  1532. }
  1533. }
  1534. else if (zero1 || inf2)
  1535. {
  1536. result = FPZero(sign1 ^ sign2);
  1537. }
  1538. else
  1539. {
  1540. result = value1 / value2;
  1541. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1542. {
  1543. context.Fpsr |= FPSR.Ufc;
  1544. result = FPZero(result < 0d);
  1545. }
  1546. }
  1547. }
  1548. return result;
  1549. }
  1550. public static double FPMax(double value1, double value2)
  1551. {
  1552. ExecutionContext context = NativeInterface.GetContext();
  1553. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1554. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1555. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1556. if (!done)
  1557. {
  1558. if (value1 > value2)
  1559. {
  1560. if (type1 == FPType.Infinity)
  1561. {
  1562. result = FPInfinity(sign1);
  1563. }
  1564. else if (type1 == FPType.Zero)
  1565. {
  1566. result = FPZero(sign1 && sign2);
  1567. }
  1568. else
  1569. {
  1570. result = value1;
  1571. }
  1572. }
  1573. else
  1574. {
  1575. if (type2 == FPType.Infinity)
  1576. {
  1577. result = FPInfinity(sign2);
  1578. }
  1579. else if (type2 == FPType.Zero)
  1580. {
  1581. result = FPZero(sign1 && sign2);
  1582. }
  1583. else
  1584. {
  1585. result = value2;
  1586. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1587. {
  1588. context.Fpsr |= FPSR.Ufc;
  1589. result = FPZero(result < 0d);
  1590. }
  1591. }
  1592. }
  1593. }
  1594. return result;
  1595. }
  1596. public static double FPMaxNum(double value1, double value2)
  1597. {
  1598. ExecutionContext context = NativeInterface.GetContext();
  1599. value1.FPUnpack(out FPType type1, out _, out _, context);
  1600. value2.FPUnpack(out FPType type2, out _, out _, context);
  1601. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  1602. {
  1603. value1 = FPInfinity(true);
  1604. }
  1605. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  1606. {
  1607. value2 = FPInfinity(true);
  1608. }
  1609. return FPMax(value1, value2);
  1610. }
  1611. public static double FPMin(double value1, double value2)
  1612. {
  1613. ExecutionContext context = NativeInterface.GetContext();
  1614. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1615. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1616. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1617. if (!done)
  1618. {
  1619. if (value1 < value2)
  1620. {
  1621. if (type1 == FPType.Infinity)
  1622. {
  1623. result = FPInfinity(sign1);
  1624. }
  1625. else if (type1 == FPType.Zero)
  1626. {
  1627. result = FPZero(sign1 || sign2);
  1628. }
  1629. else
  1630. {
  1631. result = value1;
  1632. }
  1633. }
  1634. else
  1635. {
  1636. if (type2 == FPType.Infinity)
  1637. {
  1638. result = FPInfinity(sign2);
  1639. }
  1640. else if (type2 == FPType.Zero)
  1641. {
  1642. result = FPZero(sign1 || sign2);
  1643. }
  1644. else
  1645. {
  1646. result = value2;
  1647. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1648. {
  1649. context.Fpsr |= FPSR.Ufc;
  1650. result = FPZero(result < 0d);
  1651. }
  1652. }
  1653. }
  1654. }
  1655. return result;
  1656. }
  1657. public static double FPMinNum(double value1, double value2)
  1658. {
  1659. ExecutionContext context = NativeInterface.GetContext();
  1660. value1.FPUnpack(out FPType type1, out _, out _, context);
  1661. value2.FPUnpack(out FPType type2, out _, out _, context);
  1662. if (type1 == FPType.QNaN && type2 != FPType.QNaN)
  1663. {
  1664. value1 = FPInfinity(false);
  1665. }
  1666. else if (type1 != FPType.QNaN && type2 == FPType.QNaN)
  1667. {
  1668. value2 = FPInfinity(false);
  1669. }
  1670. return FPMin(value1, value2);
  1671. }
  1672. public static double FPMul(double value1, double value2)
  1673. {
  1674. ExecutionContext context = NativeInterface.GetContext();
  1675. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1676. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1677. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1678. if (!done)
  1679. {
  1680. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1681. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1682. if ((inf1 && zero2) || (zero1 && inf2))
  1683. {
  1684. result = FPDefaultNaN();
  1685. FPProcessException(FPException.InvalidOp, context);
  1686. }
  1687. else if (inf1 || inf2)
  1688. {
  1689. result = FPInfinity(sign1 ^ sign2);
  1690. }
  1691. else if (zero1 || zero2)
  1692. {
  1693. result = FPZero(sign1 ^ sign2);
  1694. }
  1695. else
  1696. {
  1697. result = value1 * value2;
  1698. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1699. {
  1700. context.Fpsr |= FPSR.Ufc;
  1701. result = FPZero(result < 0d);
  1702. }
  1703. }
  1704. }
  1705. return result;
  1706. }
  1707. public static double FPMulAdd(double valueA, double value1, double value2)
  1708. {
  1709. ExecutionContext context = NativeInterface.GetContext();
  1710. valueA = valueA.FPUnpack(out FPType typeA, out bool signA, out ulong addend, context);
  1711. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1712. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1713. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1714. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1715. double result = FPProcessNaNs3(typeA, type1, type2, addend, op1, op2, out bool done, context);
  1716. if (typeA == FPType.QNaN && ((inf1 && zero2) || (zero1 && inf2)))
  1717. {
  1718. result = FPDefaultNaN();
  1719. FPProcessException(FPException.InvalidOp, context);
  1720. }
  1721. if (!done)
  1722. {
  1723. bool infA = typeA == FPType.Infinity; bool zeroA = typeA == FPType.Zero;
  1724. bool signP = sign1 ^ sign2;
  1725. bool infP = inf1 || inf2;
  1726. bool zeroP = zero1 || zero2;
  1727. if ((inf1 && zero2) || (zero1 && inf2) || (infA && infP && signA != signP))
  1728. {
  1729. result = FPDefaultNaN();
  1730. FPProcessException(FPException.InvalidOp, context);
  1731. }
  1732. else if ((infA && !signA) || (infP && !signP))
  1733. {
  1734. result = FPInfinity(false);
  1735. }
  1736. else if ((infA && signA) || (infP && signP))
  1737. {
  1738. result = FPInfinity(true);
  1739. }
  1740. else if (zeroA && zeroP && signA == signP)
  1741. {
  1742. result = FPZero(signA);
  1743. }
  1744. else
  1745. {
  1746. result = Math.FusedMultiplyAdd(value1, value2, valueA);
  1747. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1748. {
  1749. context.Fpsr |= FPSR.Ufc;
  1750. result = FPZero(result < 0d);
  1751. }
  1752. }
  1753. }
  1754. return result;
  1755. }
  1756. public static double FPMulSub(double valueA, double value1, double value2)
  1757. {
  1758. value1 = value1.FPNeg();
  1759. return FPMulAdd(valueA, value1, value2);
  1760. }
  1761. public static double FPMulX(double value1, double value2)
  1762. {
  1763. ExecutionContext context = NativeInterface.GetContext();
  1764. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1765. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1766. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1767. if (!done)
  1768. {
  1769. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1770. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1771. if ((inf1 && zero2) || (zero1 && inf2))
  1772. {
  1773. result = FPTwo(sign1 ^ sign2);
  1774. }
  1775. else if (inf1 || inf2)
  1776. {
  1777. result = FPInfinity(sign1 ^ sign2);
  1778. }
  1779. else if (zero1 || zero2)
  1780. {
  1781. result = FPZero(sign1 ^ sign2);
  1782. }
  1783. else
  1784. {
  1785. result = value1 * value2;
  1786. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1787. {
  1788. context.Fpsr |= FPSR.Ufc;
  1789. result = FPZero(result < 0d);
  1790. }
  1791. }
  1792. }
  1793. return result;
  1794. }
  1795. public static double FPNegMulAdd(double valueA, double value1, double value2)
  1796. {
  1797. valueA = valueA.FPNeg();
  1798. value1 = value1.FPNeg();
  1799. return FPMulAdd(valueA, value1, value2);
  1800. }
  1801. public static double FPNegMulSub(double valueA, double value1, double value2)
  1802. {
  1803. valueA = valueA.FPNeg();
  1804. return FPMulAdd(valueA, value1, value2);
  1805. }
  1806. public static double FPRecipEstimate(double value)
  1807. {
  1808. ExecutionContext context = NativeInterface.GetContext();
  1809. value.FPUnpack(out FPType type, out bool sign, out ulong op, context);
  1810. double result;
  1811. if (type == FPType.SNaN || type == FPType.QNaN)
  1812. {
  1813. result = FPProcessNaN(type, op, context);
  1814. }
  1815. else if (type == FPType.Infinity)
  1816. {
  1817. result = FPZero(sign);
  1818. }
  1819. else if (type == FPType.Zero)
  1820. {
  1821. result = FPInfinity(sign);
  1822. FPProcessException(FPException.DivideByZero, context);
  1823. }
  1824. else if (Math.Abs(value) < Math.Pow(2d, -1024))
  1825. {
  1826. bool overflowToInf;
  1827. switch (context.Fpcr.GetRoundingMode())
  1828. {
  1829. default:
  1830. case FPRoundingMode.ToNearest: overflowToInf = true; break;
  1831. case FPRoundingMode.TowardsPlusInfinity: overflowToInf = !sign; break;
  1832. case FPRoundingMode.TowardsMinusInfinity: overflowToInf = sign; break;
  1833. case FPRoundingMode.TowardsZero: overflowToInf = false; break;
  1834. }
  1835. result = overflowToInf ? FPInfinity(sign) : FPMaxNormal(sign);
  1836. FPProcessException(FPException.Overflow, context);
  1837. FPProcessException(FPException.Inexact, context);
  1838. }
  1839. else if ((context.Fpcr & FPCR.Fz) != 0 && (Math.Abs(value) >= Math.Pow(2d, 1022)))
  1840. {
  1841. result = FPZero(sign);
  1842. context.Fpsr |= FPSR.Ufc;
  1843. }
  1844. else
  1845. {
  1846. ulong fraction = op & 0x000FFFFFFFFFFFFFul;
  1847. uint exp = (uint)((op & 0x7FF0000000000000ul) >> 52);
  1848. if (exp == 0u)
  1849. {
  1850. if ((fraction & 0x0008000000000000ul) == 0ul)
  1851. {
  1852. fraction = (fraction & 0x0003FFFFFFFFFFFFul) << 2;
  1853. exp -= 1u;
  1854. }
  1855. else
  1856. {
  1857. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1858. }
  1859. }
  1860. uint scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1861. uint resultExp = 2045u - exp;
  1862. uint estimate = (uint)SoftFloat.RecipEstimateTable[scaled - 256u] + 256u;
  1863. fraction = (ulong)(estimate & 0xFFu) << 44;
  1864. if (resultExp == 0u)
  1865. {
  1866. fraction = ((fraction & 0x000FFFFFFFFFFFFEul) | 0x0010000000000000ul) >> 1;
  1867. }
  1868. else if (resultExp + 1u == 0u)
  1869. {
  1870. fraction = ((fraction & 0x000FFFFFFFFFFFFCul) | 0x0010000000000000ul) >> 2;
  1871. resultExp = 0u;
  1872. }
  1873. result = BitConverter.Int64BitsToDouble(
  1874. (long)((sign ? 1ul : 0ul) << 63 | (resultExp & 0x7FFul) << 52 | (fraction & 0x000FFFFFFFFFFFFFul)));
  1875. }
  1876. return result;
  1877. }
  1878. public static double FPRecipStepFused(double value1, double value2)
  1879. {
  1880. ExecutionContext context = NativeInterface.GetContext();
  1881. value1 = value1.FPNeg();
  1882. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1883. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1884. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1885. if (!done)
  1886. {
  1887. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1888. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1889. if ((inf1 && zero2) || (zero1 && inf2))
  1890. {
  1891. result = FPTwo(false);
  1892. }
  1893. else if (inf1 || inf2)
  1894. {
  1895. result = FPInfinity(sign1 ^ sign2);
  1896. }
  1897. else
  1898. {
  1899. result = Math.FusedMultiplyAdd(value1, value2, 2d);
  1900. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  1901. {
  1902. context.Fpsr |= FPSR.Ufc;
  1903. result = FPZero(result < 0d);
  1904. }
  1905. }
  1906. }
  1907. return result;
  1908. }
  1909. public static double FPRecpX(double value)
  1910. {
  1911. ExecutionContext context = NativeInterface.GetContext();
  1912. value.FPUnpack(out FPType type, out bool sign, out ulong op, context);
  1913. double result;
  1914. if (type == FPType.SNaN || type == FPType.QNaN)
  1915. {
  1916. result = FPProcessNaN(type, op, context);
  1917. }
  1918. else
  1919. {
  1920. ulong notExp = (~op >> 52) & 0x7FFul;
  1921. ulong maxExp = 0x7FEul;
  1922. result = BitConverter.Int64BitsToDouble(
  1923. (long)((sign ? 1ul : 0ul) << 63 | (notExp == 0x7FFul ? maxExp : notExp) << 52));
  1924. }
  1925. return result;
  1926. }
  1927. public static double FPRSqrtEstimate(double value)
  1928. {
  1929. ExecutionContext context = NativeInterface.GetContext();
  1930. value.FPUnpack(out FPType type, out bool sign, out ulong op, context);
  1931. double result;
  1932. if (type == FPType.SNaN || type == FPType.QNaN)
  1933. {
  1934. result = FPProcessNaN(type, op, context);
  1935. }
  1936. else if (type == FPType.Zero)
  1937. {
  1938. result = FPInfinity(sign);
  1939. FPProcessException(FPException.DivideByZero, context);
  1940. }
  1941. else if (sign)
  1942. {
  1943. result = FPDefaultNaN();
  1944. FPProcessException(FPException.InvalidOp, context);
  1945. }
  1946. else if (type == FPType.Infinity)
  1947. {
  1948. result = FPZero(false);
  1949. }
  1950. else
  1951. {
  1952. ulong fraction = op & 0x000FFFFFFFFFFFFFul;
  1953. uint exp = (uint)((op & 0x7FF0000000000000ul) >> 52);
  1954. if (exp == 0u)
  1955. {
  1956. while ((fraction & 0x0008000000000000ul) == 0ul)
  1957. {
  1958. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1959. exp -= 1u;
  1960. }
  1961. fraction = (fraction & 0x0007FFFFFFFFFFFFul) << 1;
  1962. }
  1963. uint scaled;
  1964. if ((exp & 1u) == 0u)
  1965. {
  1966. scaled = (uint)(((fraction & 0x000FF00000000000ul) | 0x0010000000000000ul) >> 44);
  1967. }
  1968. else
  1969. {
  1970. scaled = (uint)(((fraction & 0x000FE00000000000ul) | 0x0010000000000000ul) >> 45);
  1971. }
  1972. uint resultExp = (3068u - exp) >> 1;
  1973. uint estimate = (uint)SoftFloat.RecipSqrtEstimateTable[scaled - 128u] + 256u;
  1974. result = BitConverter.Int64BitsToDouble((long)((resultExp & 0x7FFul) << 52 | (estimate & 0xFFul) << 44));
  1975. }
  1976. return result;
  1977. }
  1978. public static double FPRSqrtStepFused(double value1, double value2)
  1979. {
  1980. ExecutionContext context = NativeInterface.GetContext();
  1981. value1 = value1.FPNeg();
  1982. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  1983. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  1984. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  1985. if (!done)
  1986. {
  1987. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  1988. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  1989. if ((inf1 && zero2) || (zero1 && inf2))
  1990. {
  1991. result = FPOnePointFive(false);
  1992. }
  1993. else if (inf1 || inf2)
  1994. {
  1995. result = FPInfinity(sign1 ^ sign2);
  1996. }
  1997. else
  1998. {
  1999. result = Math.FusedMultiplyAdd(value1, value2, 3d) / 2d;
  2000. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2001. {
  2002. context.Fpsr |= FPSR.Ufc;
  2003. result = FPZero(result < 0d);
  2004. }
  2005. }
  2006. }
  2007. return result;
  2008. }
  2009. public static double FPSqrt(double value)
  2010. {
  2011. ExecutionContext context = NativeInterface.GetContext();
  2012. value = value.FPUnpack(out FPType type, out bool sign, out ulong op, context);
  2013. double result;
  2014. if (type == FPType.SNaN || type == FPType.QNaN)
  2015. {
  2016. result = FPProcessNaN(type, op, context);
  2017. }
  2018. else if (type == FPType.Zero)
  2019. {
  2020. result = FPZero(sign);
  2021. }
  2022. else if (type == FPType.Infinity && !sign)
  2023. {
  2024. result = FPInfinity(sign);
  2025. }
  2026. else if (sign)
  2027. {
  2028. result = FPDefaultNaN();
  2029. FPProcessException(FPException.InvalidOp, context);
  2030. }
  2031. else
  2032. {
  2033. result = Math.Sqrt(value);
  2034. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2035. {
  2036. context.Fpsr |= FPSR.Ufc;
  2037. result = FPZero(result < 0d);
  2038. }
  2039. }
  2040. return result;
  2041. }
  2042. public static double FPSub(double value1, double value2)
  2043. {
  2044. ExecutionContext context = NativeInterface.GetContext();
  2045. value1 = value1.FPUnpack(out FPType type1, out bool sign1, out ulong op1, context);
  2046. value2 = value2.FPUnpack(out FPType type2, out bool sign2, out ulong op2, context);
  2047. double result = FPProcessNaNs(type1, type2, op1, op2, out bool done, context);
  2048. if (!done)
  2049. {
  2050. bool inf1 = type1 == FPType.Infinity; bool zero1 = type1 == FPType.Zero;
  2051. bool inf2 = type2 == FPType.Infinity; bool zero2 = type2 == FPType.Zero;
  2052. if (inf1 && inf2 && sign1 == sign2)
  2053. {
  2054. result = FPDefaultNaN();
  2055. FPProcessException(FPException.InvalidOp, context);
  2056. }
  2057. else if ((inf1 && !sign1) || (inf2 && sign2))
  2058. {
  2059. result = FPInfinity(false);
  2060. }
  2061. else if ((inf1 && sign1) || (inf2 && !sign2))
  2062. {
  2063. result = FPInfinity(true);
  2064. }
  2065. else if (zero1 && zero2 && sign1 == !sign2)
  2066. {
  2067. result = FPZero(sign1);
  2068. }
  2069. else
  2070. {
  2071. result = value1 - value2;
  2072. if ((context.Fpcr & FPCR.Fz) != 0 && double.IsSubnormal(result))
  2073. {
  2074. context.Fpsr |= FPSR.Ufc;
  2075. result = FPZero(result < 0d);
  2076. }
  2077. }
  2078. }
  2079. return result;
  2080. }
  2081. private static double FPDefaultNaN()
  2082. {
  2083. return -double.NaN;
  2084. }
  2085. private static double FPInfinity(bool sign)
  2086. {
  2087. return sign ? double.NegativeInfinity : double.PositiveInfinity;
  2088. }
  2089. private static double FPZero(bool sign)
  2090. {
  2091. return sign ? -0d : +0d;
  2092. }
  2093. private static double FPMaxNormal(bool sign)
  2094. {
  2095. return sign ? double.MinValue : double.MaxValue;
  2096. }
  2097. private static double FPTwo(bool sign)
  2098. {
  2099. return sign ? -2d : +2d;
  2100. }
  2101. private static double FPOnePointFive(bool sign)
  2102. {
  2103. return sign ? -1.5d : +1.5d;
  2104. }
  2105. private static double FPNeg(this double value)
  2106. {
  2107. return -value;
  2108. }
  2109. private static double ZerosOrOnes(bool ones)
  2110. {
  2111. return BitConverter.Int64BitsToDouble(ones ? -1L : 0L);
  2112. }
  2113. private static double FPUnpack(
  2114. this double value,
  2115. out FPType type,
  2116. out bool sign,
  2117. out ulong valueBits,
  2118. ExecutionContext context)
  2119. {
  2120. valueBits = (ulong)BitConverter.DoubleToInt64Bits(value);
  2121. sign = (~valueBits & 0x8000000000000000ul) == 0ul;
  2122. if ((valueBits & 0x7FF0000000000000ul) == 0ul)
  2123. {
  2124. if ((valueBits & 0x000FFFFFFFFFFFFFul) == 0ul || (context.Fpcr & FPCR.Fz) != 0)
  2125. {
  2126. type = FPType.Zero;
  2127. value = FPZero(sign);
  2128. if ((valueBits & 0x000FFFFFFFFFFFFFul) != 0ul)
  2129. {
  2130. FPProcessException(FPException.InputDenorm, context);
  2131. }
  2132. }
  2133. else
  2134. {
  2135. type = FPType.Nonzero;
  2136. }
  2137. }
  2138. else if ((~valueBits & 0x7FF0000000000000ul) == 0ul)
  2139. {
  2140. if ((valueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  2141. {
  2142. type = FPType.Infinity;
  2143. }
  2144. else
  2145. {
  2146. type = (~valueBits & 0x0008000000000000ul) == 0ul ? FPType.QNaN : FPType.SNaN;
  2147. value = FPZero(sign);
  2148. }
  2149. }
  2150. else
  2151. {
  2152. type = FPType.Nonzero;
  2153. }
  2154. return value;
  2155. }
  2156. private static double FPProcessNaNs(
  2157. FPType type1,
  2158. FPType type2,
  2159. ulong op1,
  2160. ulong op2,
  2161. out bool done,
  2162. ExecutionContext context)
  2163. {
  2164. done = true;
  2165. if (type1 == FPType.SNaN)
  2166. {
  2167. return FPProcessNaN(type1, op1, context);
  2168. }
  2169. else if (type2 == FPType.SNaN)
  2170. {
  2171. return FPProcessNaN(type2, op2, context);
  2172. }
  2173. else if (type1 == FPType.QNaN)
  2174. {
  2175. return FPProcessNaN(type1, op1, context);
  2176. }
  2177. else if (type2 == FPType.QNaN)
  2178. {
  2179. return FPProcessNaN(type2, op2, context);
  2180. }
  2181. done = false;
  2182. return FPZero(false);
  2183. }
  2184. private static double FPProcessNaNs3(
  2185. FPType type1,
  2186. FPType type2,
  2187. FPType type3,
  2188. ulong op1,
  2189. ulong op2,
  2190. ulong op3,
  2191. out bool done,
  2192. ExecutionContext context)
  2193. {
  2194. done = true;
  2195. if (type1 == FPType.SNaN)
  2196. {
  2197. return FPProcessNaN(type1, op1, context);
  2198. }
  2199. else if (type2 == FPType.SNaN)
  2200. {
  2201. return FPProcessNaN(type2, op2, context);
  2202. }
  2203. else if (type3 == FPType.SNaN)
  2204. {
  2205. return FPProcessNaN(type3, op3, context);
  2206. }
  2207. else if (type1 == FPType.QNaN)
  2208. {
  2209. return FPProcessNaN(type1, op1, context);
  2210. }
  2211. else if (type2 == FPType.QNaN)
  2212. {
  2213. return FPProcessNaN(type2, op2, context);
  2214. }
  2215. else if (type3 == FPType.QNaN)
  2216. {
  2217. return FPProcessNaN(type3, op3, context);
  2218. }
  2219. done = false;
  2220. return FPZero(false);
  2221. }
  2222. private static double FPProcessNaN(FPType type, ulong op, ExecutionContext context)
  2223. {
  2224. if (type == FPType.SNaN)
  2225. {
  2226. op |= 1ul << 51;
  2227. FPProcessException(FPException.InvalidOp, context);
  2228. }
  2229. if ((context.Fpcr & FPCR.Dn) != 0)
  2230. {
  2231. return FPDefaultNaN();
  2232. }
  2233. return BitConverter.Int64BitsToDouble((long)op);
  2234. }
  2235. private static void FPProcessException(FPException exc, ExecutionContext context)
  2236. {
  2237. int enable = (int)exc + 8;
  2238. if ((context.Fpcr & (FPCR)(1 << enable)) != 0)
  2239. {
  2240. throw new NotImplementedException("Floating-point trap handling.");
  2241. }
  2242. else
  2243. {
  2244. context.Fpsr |= (FPSR)(1 << (int)exc);
  2245. }
  2246. }
  2247. }
  2248. }