SoftFloat.cs 84 KB

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