SoftFloat.cs 83 KB

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