ASoftFloat.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  1. using ChocolArm64.State;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Runtime.CompilerServices;
  5. namespace ChocolArm64.Instruction
  6. {
  7. static class ASoftFloat
  8. {
  9. static ASoftFloat()
  10. {
  11. RecipEstimateTable = BuildRecipEstimateTable();
  12. InvSqrtEstimateTable = BuildInvSqrtEstimateTable();
  13. }
  14. private static readonly byte[] RecipEstimateTable;
  15. private static readonly byte[] InvSqrtEstimateTable;
  16. private static byte[] BuildRecipEstimateTable()
  17. {
  18. byte[] Table = new byte[256];
  19. for (ulong index = 0; index < 256; index++)
  20. {
  21. ulong a = index | 0x100;
  22. a = (a << 1) + 1;
  23. ulong b = 0x80000 / a;
  24. b = (b + 1) >> 1;
  25. Table[index] = (byte)(b & 0xFF);
  26. }
  27. return Table;
  28. }
  29. private static byte[] BuildInvSqrtEstimateTable()
  30. {
  31. byte[] Table = new byte[512];
  32. for (ulong index = 128; index < 512; index++)
  33. {
  34. ulong a = index;
  35. if (a < 256)
  36. {
  37. a = (a << 1) + 1;
  38. }
  39. else
  40. {
  41. a = (a | 1) << 1;
  42. }
  43. ulong b = 256;
  44. while (a * (b + 1) * (b + 1) < (1ul << 28))
  45. {
  46. b++;
  47. }
  48. b = (b + 1) >> 1;
  49. Table[index] = (byte)(b & 0xFF);
  50. }
  51. return Table;
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public static float RecipEstimate(float x)
  55. {
  56. return (float)RecipEstimate((double)x);
  57. }
  58. public static double RecipEstimate(double x)
  59. {
  60. ulong x_bits = (ulong)BitConverter.DoubleToInt64Bits(x);
  61. ulong x_sign = x_bits & 0x8000000000000000;
  62. ulong x_exp = (x_bits >> 52) & 0x7FF;
  63. ulong scaled = x_bits & ((1ul << 52) - 1);
  64. if (x_exp >= 2045)
  65. {
  66. if (x_exp == 0x7ff && scaled != 0)
  67. {
  68. // NaN
  69. return BitConverter.Int64BitsToDouble((long)(x_bits | 0x0008000000000000));
  70. }
  71. // Infinity, or Out of range -> Zero
  72. return BitConverter.Int64BitsToDouble((long)x_sign);
  73. }
  74. if (x_exp == 0)
  75. {
  76. if (scaled == 0)
  77. {
  78. // Zero -> Infinity
  79. return BitConverter.Int64BitsToDouble((long)(x_sign | 0x7FF0000000000000));
  80. }
  81. // Denormal
  82. if ((scaled & (1ul << 51)) == 0)
  83. {
  84. x_exp = ~0ul;
  85. scaled <<= 2;
  86. }
  87. else
  88. {
  89. scaled <<= 1;
  90. }
  91. }
  92. scaled >>= 44;
  93. scaled &= 0xFF;
  94. ulong result_exp = (2045 - x_exp) & 0x7FF;
  95. ulong estimate = (ulong)RecipEstimateTable[scaled];
  96. ulong fraction = estimate << 44;
  97. if (result_exp == 0)
  98. {
  99. fraction >>= 1;
  100. fraction |= 1ul << 51;
  101. }
  102. else if (result_exp == 0x7FF)
  103. {
  104. result_exp = 0;
  105. fraction >>= 2;
  106. fraction |= 1ul << 50;
  107. }
  108. ulong result = x_sign | (result_exp << 52) | fraction;
  109. return BitConverter.Int64BitsToDouble((long)result);
  110. }
  111. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  112. public static float InvSqrtEstimate(float x)
  113. {
  114. return (float)InvSqrtEstimate((double)x);
  115. }
  116. public static double InvSqrtEstimate(double x)
  117. {
  118. ulong x_bits = (ulong)BitConverter.DoubleToInt64Bits(x);
  119. ulong x_sign = x_bits & 0x8000000000000000;
  120. long x_exp = (long)((x_bits >> 52) & 0x7FF);
  121. ulong scaled = x_bits & ((1ul << 52) - 1);
  122. if (x_exp == 0x7FF && scaled != 0)
  123. {
  124. // NaN
  125. return BitConverter.Int64BitsToDouble((long)(x_bits | 0x0008000000000000));
  126. }
  127. if (x_exp == 0)
  128. {
  129. if (scaled == 0)
  130. {
  131. // Zero -> Infinity
  132. return BitConverter.Int64BitsToDouble((long)(x_sign | 0x7FF0000000000000));
  133. }
  134. // Denormal
  135. while ((scaled & (1 << 51)) == 0)
  136. {
  137. scaled <<= 1;
  138. x_exp--;
  139. }
  140. scaled <<= 1;
  141. }
  142. if (x_sign != 0)
  143. {
  144. // Negative -> NaN
  145. return BitConverter.Int64BitsToDouble((long)0x7FF8000000000000);
  146. }
  147. if (x_exp == 0x7ff && scaled == 0)
  148. {
  149. // Infinity -> Zero
  150. return BitConverter.Int64BitsToDouble((long)x_sign);
  151. }
  152. if (((ulong)x_exp & 1) == 1)
  153. {
  154. scaled >>= 45;
  155. scaled &= 0xFF;
  156. scaled |= 0x80;
  157. }
  158. else
  159. {
  160. scaled >>= 44;
  161. scaled &= 0xFF;
  162. scaled |= 0x100;
  163. }
  164. ulong result_exp = ((ulong)(3068 - x_exp) / 2) & 0x7FF;
  165. ulong estimate = (ulong)InvSqrtEstimateTable[scaled];
  166. ulong fraction = estimate << 44;
  167. ulong result = x_sign | (result_exp << 52) | fraction;
  168. return BitConverter.Int64BitsToDouble((long)result);
  169. }
  170. }
  171. static class ASoftFloat16_32
  172. {
  173. public static float FPConvert(ushort ValueBits, AThreadState State)
  174. {
  175. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat16_32.FPConvert: State.Fpcr = 0x{State.Fpcr:X8}");
  176. double Real = ValueBits.FPUnpackCV(out FPType Type, out bool Sign, State);
  177. float Result;
  178. if (Type == FPType.SNaN || Type == FPType.QNaN)
  179. {
  180. if (State.GetFpcrFlag(FPCR.DN))
  181. {
  182. Result = FPDefaultNaN();
  183. }
  184. else
  185. {
  186. Result = FPConvertNaN(ValueBits);
  187. }
  188. if (Type == FPType.SNaN)
  189. {
  190. FPProcessException(FPExc.InvalidOp, State);
  191. }
  192. }
  193. else if (Type == FPType.Infinity)
  194. {
  195. Result = FPInfinity(Sign);
  196. }
  197. else if (Type == FPType.Zero)
  198. {
  199. Result = FPZero(Sign);
  200. }
  201. else
  202. {
  203. Result = FPRoundCV(Real, State);
  204. }
  205. return Result;
  206. }
  207. private static float FPDefaultNaN()
  208. {
  209. return -float.NaN;
  210. }
  211. private static float FPInfinity(bool Sign)
  212. {
  213. return Sign ? float.NegativeInfinity : float.PositiveInfinity;
  214. }
  215. private static float FPZero(bool Sign)
  216. {
  217. return Sign ? -0f : +0f;
  218. }
  219. private static float FPMaxNormal(bool Sign)
  220. {
  221. return Sign ? float.MinValue : float.MaxValue;
  222. }
  223. private static double FPUnpackCV(this ushort ValueBits, out FPType Type, out bool Sign, AThreadState State)
  224. {
  225. Sign = (~(uint)ValueBits & 0x8000u) == 0u;
  226. uint Exp16 = ((uint)ValueBits & 0x7C00u) >> 10;
  227. uint Frac16 = (uint)ValueBits & 0x03FFu;
  228. double Real;
  229. if (Exp16 == 0u)
  230. {
  231. if (Frac16 == 0u)
  232. {
  233. Type = FPType.Zero;
  234. Real = 0d;
  235. }
  236. else
  237. {
  238. Type = FPType.Nonzero; // Subnormal.
  239. Real = Math.Pow(2d, -14) * ((double)Frac16 * Math.Pow(2d, -10));
  240. }
  241. }
  242. else if (Exp16 == 0x1Fu && !State.GetFpcrFlag(FPCR.AHP))
  243. {
  244. if (Frac16 == 0u)
  245. {
  246. Type = FPType.Infinity;
  247. Real = Math.Pow(2d, 1000);
  248. }
  249. else
  250. {
  251. Type = (~Frac16 & 0x0200u) == 0u ? FPType.QNaN : FPType.SNaN;
  252. Real = 0d;
  253. }
  254. }
  255. else
  256. {
  257. Type = FPType.Nonzero; // Normal.
  258. Real = Math.Pow(2d, (int)Exp16 - 15) * (1d + (double)Frac16 * Math.Pow(2d, -10));
  259. }
  260. return Sign ? -Real : Real;
  261. }
  262. private static float FPRoundCV(double Real, AThreadState State)
  263. {
  264. const int MinimumExp = -126;
  265. const int E = 8;
  266. const int F = 23;
  267. bool Sign;
  268. double Mantissa;
  269. if (Real < 0d)
  270. {
  271. Sign = true;
  272. Mantissa = -Real;
  273. }
  274. else
  275. {
  276. Sign = false;
  277. Mantissa = Real;
  278. }
  279. int Exponent = 0;
  280. while (Mantissa < 1d)
  281. {
  282. Mantissa *= 2d;
  283. Exponent--;
  284. }
  285. while (Mantissa >= 2d)
  286. {
  287. Mantissa /= 2d;
  288. Exponent++;
  289. }
  290. if (State.GetFpcrFlag(FPCR.FZ) && Exponent < MinimumExp)
  291. {
  292. State.SetFpsrFlag(FPSR.UFC);
  293. return FPZero(Sign);
  294. }
  295. uint BiasedExp = (uint)Math.Max(Exponent - MinimumExp + 1, 0);
  296. if (BiasedExp == 0u)
  297. {
  298. Mantissa /= Math.Pow(2d, MinimumExp - Exponent);
  299. }
  300. uint IntMant = (uint)Math.Floor(Mantissa * Math.Pow(2d, F));
  301. double Error = Mantissa * Math.Pow(2d, F) - (double)IntMant;
  302. if (BiasedExp == 0u && (Error != 0d || State.GetFpcrFlag(FPCR.UFE)))
  303. {
  304. FPProcessException(FPExc.Underflow, State);
  305. }
  306. bool OverflowToInf;
  307. bool RoundUp;
  308. switch (State.FPRoundingMode())
  309. {
  310. default:
  311. case ARoundMode.ToNearest:
  312. RoundUp = (Error > 0.5d || (Error == 0.5d && (IntMant & 1u) == 1u));
  313. OverflowToInf = true;
  314. break;
  315. case ARoundMode.TowardsPlusInfinity:
  316. RoundUp = (Error != 0d && !Sign);
  317. OverflowToInf = !Sign;
  318. break;
  319. case ARoundMode.TowardsMinusInfinity:
  320. RoundUp = (Error != 0d && Sign);
  321. OverflowToInf = Sign;
  322. break;
  323. case ARoundMode.TowardsZero:
  324. RoundUp = false;
  325. OverflowToInf = false;
  326. break;
  327. }
  328. if (RoundUp)
  329. {
  330. IntMant++;
  331. if (IntMant == (uint)Math.Pow(2d, F))
  332. {
  333. BiasedExp = 1u;
  334. }
  335. if (IntMant == (uint)Math.Pow(2d, F + 1))
  336. {
  337. BiasedExp++;
  338. IntMant >>= 1;
  339. }
  340. }
  341. float Result;
  342. if (BiasedExp >= (uint)Math.Pow(2d, E) - 1u)
  343. {
  344. Result = OverflowToInf ? FPInfinity(Sign) : FPMaxNormal(Sign);
  345. FPProcessException(FPExc.Overflow, State);
  346. Error = 1d;
  347. }
  348. else
  349. {
  350. Result = BitConverter.Int32BitsToSingle(
  351. (int)((Sign ? 1u : 0u) << 31 | (BiasedExp & 0xFFu) << 23 | (IntMant & 0x007FFFFFu)));
  352. }
  353. if (Error != 0d)
  354. {
  355. FPProcessException(FPExc.Inexact, State);
  356. }
  357. return Result;
  358. }
  359. private static float FPConvertNaN(ushort ValueBits)
  360. {
  361. return BitConverter.Int32BitsToSingle(
  362. (int)(((uint)ValueBits & 0x8000u) << 16 | 0x7FC00000u | ((uint)ValueBits & 0x01FFu) << 13));
  363. }
  364. private static void FPProcessException(FPExc Exc, AThreadState State)
  365. {
  366. int Enable = (int)Exc + 8;
  367. if ((State.Fpcr & (1 << Enable)) != 0)
  368. {
  369. throw new NotImplementedException("floating-point trap handling");
  370. }
  371. else
  372. {
  373. State.Fpsr |= 1 << (int)Exc;
  374. }
  375. }
  376. }
  377. static class ASoftFloat32_16
  378. {
  379. public static ushort FPConvert(float Value, AThreadState State)
  380. {
  381. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat32_16.FPConvert: State.Fpcr = 0x{State.Fpcr:X8}");
  382. double Real = Value.FPUnpackCV(out FPType Type, out bool Sign, State, out uint ValueBits);
  383. bool AltHp = State.GetFpcrFlag(FPCR.AHP);
  384. ushort ResultBits;
  385. if (Type == FPType.SNaN || Type == FPType.QNaN)
  386. {
  387. if (AltHp)
  388. {
  389. ResultBits = FPZero(Sign);
  390. }
  391. else if (State.GetFpcrFlag(FPCR.DN))
  392. {
  393. ResultBits = FPDefaultNaN();
  394. }
  395. else
  396. {
  397. ResultBits = FPConvertNaN(ValueBits);
  398. }
  399. if (Type == FPType.SNaN || AltHp)
  400. {
  401. FPProcessException(FPExc.InvalidOp, State);
  402. }
  403. }
  404. else if (Type == FPType.Infinity)
  405. {
  406. if (AltHp)
  407. {
  408. ResultBits = (ushort)((Sign ? 1u : 0u) << 15 | 0x7FFFu);
  409. FPProcessException(FPExc.InvalidOp, State);
  410. }
  411. else
  412. {
  413. ResultBits = FPInfinity(Sign);
  414. }
  415. }
  416. else if (Type == FPType.Zero)
  417. {
  418. ResultBits = FPZero(Sign);
  419. }
  420. else
  421. {
  422. ResultBits = FPRoundCV(Real, State);
  423. }
  424. return ResultBits;
  425. }
  426. private static ushort FPDefaultNaN()
  427. {
  428. return (ushort)0x7E00u;
  429. }
  430. private static ushort FPInfinity(bool Sign)
  431. {
  432. return Sign ? (ushort)0xFC00u : (ushort)0x7C00u;
  433. }
  434. private static ushort FPZero(bool Sign)
  435. {
  436. return Sign ? (ushort)0x8000u : (ushort)0x0000u;
  437. }
  438. private static ushort FPMaxNormal(bool Sign)
  439. {
  440. return Sign ? (ushort)0xFBFFu : (ushort)0x7BFFu;
  441. }
  442. private static double FPUnpackCV(this float Value, out FPType Type, out bool Sign, AThreadState State, out uint ValueBits)
  443. {
  444. ValueBits = (uint)BitConverter.SingleToInt32Bits(Value);
  445. Sign = (~ValueBits & 0x80000000u) == 0u;
  446. uint Exp32 = (ValueBits & 0x7F800000u) >> 23;
  447. uint Frac32 = ValueBits & 0x007FFFFFu;
  448. double Real;
  449. if (Exp32 == 0u)
  450. {
  451. if (Frac32 == 0u || State.GetFpcrFlag(FPCR.FZ))
  452. {
  453. Type = FPType.Zero;
  454. Real = 0d;
  455. if (Frac32 != 0u) FPProcessException(FPExc.InputDenorm, State);
  456. }
  457. else
  458. {
  459. Type = FPType.Nonzero; // Subnormal.
  460. Real = Math.Pow(2d, -126) * ((double)Frac32 * Math.Pow(2d, -23));
  461. }
  462. }
  463. else if (Exp32 == 0xFFu)
  464. {
  465. if (Frac32 == 0u)
  466. {
  467. Type = FPType.Infinity;
  468. Real = Math.Pow(2d, 1000);
  469. }
  470. else
  471. {
  472. Type = (~Frac32 & 0x00400000u) == 0u ? FPType.QNaN : FPType.SNaN;
  473. Real = 0d;
  474. }
  475. }
  476. else
  477. {
  478. Type = FPType.Nonzero; // Normal.
  479. Real = Math.Pow(2d, (int)Exp32 - 127) * (1d + (double)Frac32 * Math.Pow(2d, -23));
  480. }
  481. return Sign ? -Real : Real;
  482. }
  483. private static ushort FPRoundCV(double Real, AThreadState State)
  484. {
  485. const int MinimumExp = -14;
  486. const int E = 5;
  487. const int F = 10;
  488. bool Sign;
  489. double Mantissa;
  490. if (Real < 0d)
  491. {
  492. Sign = true;
  493. Mantissa = -Real;
  494. }
  495. else
  496. {
  497. Sign = false;
  498. Mantissa = Real;
  499. }
  500. int Exponent = 0;
  501. while (Mantissa < 1d)
  502. {
  503. Mantissa *= 2d;
  504. Exponent--;
  505. }
  506. while (Mantissa >= 2d)
  507. {
  508. Mantissa /= 2d;
  509. Exponent++;
  510. }
  511. uint BiasedExp = (uint)Math.Max(Exponent - MinimumExp + 1, 0);
  512. if (BiasedExp == 0u)
  513. {
  514. Mantissa /= Math.Pow(2d, MinimumExp - Exponent);
  515. }
  516. uint IntMant = (uint)Math.Floor(Mantissa * Math.Pow(2d, F));
  517. double Error = Mantissa * Math.Pow(2d, F) - (double)IntMant;
  518. if (BiasedExp == 0u && (Error != 0d || State.GetFpcrFlag(FPCR.UFE)))
  519. {
  520. FPProcessException(FPExc.Underflow, State);
  521. }
  522. bool OverflowToInf;
  523. bool RoundUp;
  524. switch (State.FPRoundingMode())
  525. {
  526. default:
  527. case ARoundMode.ToNearest:
  528. RoundUp = (Error > 0.5d || (Error == 0.5d && (IntMant & 1u) == 1u));
  529. OverflowToInf = true;
  530. break;
  531. case ARoundMode.TowardsPlusInfinity:
  532. RoundUp = (Error != 0d && !Sign);
  533. OverflowToInf = !Sign;
  534. break;
  535. case ARoundMode.TowardsMinusInfinity:
  536. RoundUp = (Error != 0d && Sign);
  537. OverflowToInf = Sign;
  538. break;
  539. case ARoundMode.TowardsZero:
  540. RoundUp = false;
  541. OverflowToInf = false;
  542. break;
  543. }
  544. if (RoundUp)
  545. {
  546. IntMant++;
  547. if (IntMant == (uint)Math.Pow(2d, F))
  548. {
  549. BiasedExp = 1u;
  550. }
  551. if (IntMant == (uint)Math.Pow(2d, F + 1))
  552. {
  553. BiasedExp++;
  554. IntMant >>= 1;
  555. }
  556. }
  557. ushort ResultBits;
  558. if (!State.GetFpcrFlag(FPCR.AHP))
  559. {
  560. if (BiasedExp >= (uint)Math.Pow(2d, E) - 1u)
  561. {
  562. ResultBits = OverflowToInf ? FPInfinity(Sign) : FPMaxNormal(Sign);
  563. FPProcessException(FPExc.Overflow, State);
  564. Error = 1d;
  565. }
  566. else
  567. {
  568. ResultBits = (ushort)((Sign ? 1u : 0u) << 15 | (BiasedExp & 0x1Fu) << 10 | (IntMant & 0x03FFu));
  569. }
  570. }
  571. else
  572. {
  573. if (BiasedExp >= (uint)Math.Pow(2d, E))
  574. {
  575. ResultBits = (ushort)((Sign ? 1u : 0u) << 15 | 0x7FFFu);
  576. FPProcessException(FPExc.InvalidOp, State);
  577. Error = 0d;
  578. }
  579. else
  580. {
  581. ResultBits = (ushort)((Sign ? 1u : 0u) << 15 | (BiasedExp & 0x1Fu) << 10 | (IntMant & 0x03FFu));
  582. }
  583. }
  584. if (Error != 0d)
  585. {
  586. FPProcessException(FPExc.Inexact, State);
  587. }
  588. return ResultBits;
  589. }
  590. private static ushort FPConvertNaN(uint ValueBits)
  591. {
  592. return (ushort)((ValueBits & 0x80000000u) >> 16 | 0x7E00u | (ValueBits & 0x003FE000u) >> 13);
  593. }
  594. private static void FPProcessException(FPExc Exc, AThreadState State)
  595. {
  596. int Enable = (int)Exc + 8;
  597. if ((State.Fpcr & (1 << Enable)) != 0)
  598. {
  599. throw new NotImplementedException("floating-point trap handling");
  600. }
  601. else
  602. {
  603. State.Fpsr |= 1 << (int)Exc;
  604. }
  605. }
  606. }
  607. static class ASoftFloat_32
  608. {
  609. public static float FPAdd(float Value1, float Value2, AThreadState State)
  610. {
  611. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  612. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  613. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  614. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  615. if (!Done)
  616. {
  617. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  618. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  619. if (Inf1 && Inf2 && Sign1 == !Sign2)
  620. {
  621. Result = FPDefaultNaN();
  622. FPProcessException(FPExc.InvalidOp, State);
  623. }
  624. else if ((Inf1 && !Sign1) || (Inf2 && !Sign2))
  625. {
  626. Result = FPInfinity(false);
  627. }
  628. else if ((Inf1 && Sign1) || (Inf2 && Sign2))
  629. {
  630. Result = FPInfinity(true);
  631. }
  632. else if (Zero1 && Zero2 && Sign1 == Sign2)
  633. {
  634. Result = FPZero(Sign1);
  635. }
  636. else
  637. {
  638. Result = Value1 + Value2;
  639. }
  640. }
  641. return Result;
  642. }
  643. public static float FPDiv(float Value1, float Value2, AThreadState State)
  644. {
  645. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPDiv: State.Fpcr = 0x{State.Fpcr:X8}");
  646. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  647. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  648. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  649. if (!Done)
  650. {
  651. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  652. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  653. if ((Inf1 && Inf2) || (Zero1 && Zero2))
  654. {
  655. Result = FPDefaultNaN();
  656. FPProcessException(FPExc.InvalidOp, State);
  657. }
  658. else if (Inf1 || Zero2)
  659. {
  660. Result = FPInfinity(Sign1 ^ Sign2);
  661. if (!Inf1) FPProcessException(FPExc.DivideByZero, State);
  662. }
  663. else if (Zero1 || Inf2)
  664. {
  665. Result = FPZero(Sign1 ^ Sign2);
  666. }
  667. else
  668. {
  669. Result = Value1 / Value2;
  670. }
  671. }
  672. return Result;
  673. }
  674. public static float FPMax(float Value1, float Value2, AThreadState State)
  675. {
  676. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMax: State.Fpcr = 0x{State.Fpcr:X8}");
  677. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  678. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  679. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  680. if (!Done)
  681. {
  682. if (Value1 > Value2)
  683. {
  684. if (Type1 == FPType.Infinity)
  685. {
  686. Result = FPInfinity(Sign1);
  687. }
  688. else if (Type1 == FPType.Zero)
  689. {
  690. Result = FPZero(Sign1 && Sign2);
  691. }
  692. else
  693. {
  694. Result = Value1;
  695. }
  696. }
  697. else
  698. {
  699. if (Type2 == FPType.Infinity)
  700. {
  701. Result = FPInfinity(Sign2);
  702. }
  703. else if (Type2 == FPType.Zero)
  704. {
  705. Result = FPZero(Sign1 && Sign2);
  706. }
  707. else
  708. {
  709. Result = Value2;
  710. }
  711. }
  712. }
  713. return Result;
  714. }
  715. public static float FPMaxNum(float Value1, float Value2, AThreadState State)
  716. {
  717. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_32.FPMaxNum: ");
  718. Value1.FPUnpack(out FPType Type1, out _, out _);
  719. Value2.FPUnpack(out FPType Type2, out _, out _);
  720. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  721. {
  722. Value1 = FPInfinity(true);
  723. }
  724. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  725. {
  726. Value2 = FPInfinity(true);
  727. }
  728. return FPMax(Value1, Value2, State);
  729. }
  730. public static float FPMin(float Value1, float Value2, AThreadState State)
  731. {
  732. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMin: State.Fpcr = 0x{State.Fpcr:X8}");
  733. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  734. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  735. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  736. if (!Done)
  737. {
  738. if (Value1 < Value2)
  739. {
  740. if (Type1 == FPType.Infinity)
  741. {
  742. Result = FPInfinity(Sign1);
  743. }
  744. else if (Type1 == FPType.Zero)
  745. {
  746. Result = FPZero(Sign1 || Sign2);
  747. }
  748. else
  749. {
  750. Result = Value1;
  751. }
  752. }
  753. else
  754. {
  755. if (Type2 == FPType.Infinity)
  756. {
  757. Result = FPInfinity(Sign2);
  758. }
  759. else if (Type2 == FPType.Zero)
  760. {
  761. Result = FPZero(Sign1 || Sign2);
  762. }
  763. else
  764. {
  765. Result = Value2;
  766. }
  767. }
  768. }
  769. return Result;
  770. }
  771. public static float FPMinNum(float Value1, float Value2, AThreadState State)
  772. {
  773. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_32.FPMinNum: ");
  774. Value1.FPUnpack(out FPType Type1, out _, out _);
  775. Value2.FPUnpack(out FPType Type2, out _, out _);
  776. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  777. {
  778. Value1 = FPInfinity(false);
  779. }
  780. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  781. {
  782. Value2 = FPInfinity(false);
  783. }
  784. return FPMin(Value1, Value2, State);
  785. }
  786. public static float FPMul(float Value1, float Value2, AThreadState State)
  787. {
  788. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMul: State.Fpcr = 0x{State.Fpcr:X8}");
  789. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  790. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  791. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  792. if (!Done)
  793. {
  794. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  795. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  796. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  797. {
  798. Result = FPDefaultNaN();
  799. FPProcessException(FPExc.InvalidOp, State);
  800. }
  801. else if (Inf1 || Inf2)
  802. {
  803. Result = FPInfinity(Sign1 ^ Sign2);
  804. }
  805. else if (Zero1 || Zero2)
  806. {
  807. Result = FPZero(Sign1 ^ Sign2);
  808. }
  809. else
  810. {
  811. Result = Value1 * Value2;
  812. }
  813. }
  814. return Result;
  815. }
  816. public static float FPMulAdd(float ValueA, float Value1, float Value2, AThreadState State)
  817. {
  818. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMulAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  819. ValueA = ValueA.FPUnpack(out FPType TypeA, out bool SignA, out uint Addend);
  820. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  821. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  822. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  823. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  824. float Result = FPProcessNaNs3(TypeA, Type1, Type2, Addend, Op1, Op2, State, out bool Done);
  825. if (TypeA == FPType.QNaN && ((Inf1 && Zero2) || (Zero1 && Inf2)))
  826. {
  827. Result = FPDefaultNaN();
  828. FPProcessException(FPExc.InvalidOp, State);
  829. }
  830. if (!Done)
  831. {
  832. bool InfA = TypeA == FPType.Infinity; bool ZeroA = TypeA == FPType.Zero;
  833. bool SignP = Sign1 ^ Sign2;
  834. bool InfP = Inf1 || Inf2;
  835. bool ZeroP = Zero1 || Zero2;
  836. if ((Inf1 && Zero2) || (Zero1 && Inf2) || (InfA && InfP && SignA != SignP))
  837. {
  838. Result = FPDefaultNaN();
  839. FPProcessException(FPExc.InvalidOp, State);
  840. }
  841. else if ((InfA && !SignA) || (InfP && !SignP))
  842. {
  843. Result = FPInfinity(false);
  844. }
  845. else if ((InfA && SignA) || (InfP && SignP))
  846. {
  847. Result = FPInfinity(true);
  848. }
  849. else if (ZeroA && ZeroP && SignA == SignP)
  850. {
  851. Result = FPZero(SignA);
  852. }
  853. else
  854. {
  855. // TODO: When available, use: T MathF.FusedMultiplyAdd(T, T, T);
  856. // https://github.com/dotnet/corefx/issues/31903
  857. Result = ValueA + (Value1 * Value2);
  858. }
  859. }
  860. return Result;
  861. }
  862. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  863. public static float FPMulSub(float ValueA, float Value1, float Value2, AThreadState State)
  864. {
  865. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_32.FPMulSub: ");
  866. Value1 = Value1.FPNeg();
  867. return FPMulAdd(ValueA, Value1, Value2, State);
  868. }
  869. public static float FPMulX(float Value1, float Value2, AThreadState State)
  870. {
  871. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMulX: State.Fpcr = 0x{State.Fpcr:X8}");
  872. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  873. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  874. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  875. if (!Done)
  876. {
  877. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  878. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  879. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  880. {
  881. Result = FPTwo(Sign1 ^ Sign2);
  882. }
  883. else if (Inf1 || Inf2)
  884. {
  885. Result = FPInfinity(Sign1 ^ Sign2);
  886. }
  887. else if (Zero1 || Zero2)
  888. {
  889. Result = FPZero(Sign1 ^ Sign2);
  890. }
  891. else
  892. {
  893. Result = Value1 * Value2;
  894. }
  895. }
  896. return Result;
  897. }
  898. public static float FPRecipStepFused(float Value1, float Value2, AThreadState State)
  899. {
  900. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPRecipStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  901. Value1 = Value1.FPNeg();
  902. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  903. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  904. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  905. if (!Done)
  906. {
  907. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  908. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  909. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  910. {
  911. Result = FPTwo(false);
  912. }
  913. else if (Inf1 || Inf2)
  914. {
  915. Result = FPInfinity(Sign1 ^ Sign2);
  916. }
  917. else
  918. {
  919. // TODO: When available, use: T MathF.FusedMultiplyAdd(T, T, T);
  920. // https://github.com/dotnet/corefx/issues/31903
  921. Result = 2f + (Value1 * Value2);
  922. }
  923. }
  924. return Result;
  925. }
  926. public static float FPRecpX(float Value, AThreadState State)
  927. {
  928. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPRecpX: State.Fpcr = 0x{State.Fpcr:X8}");
  929. Value.FPUnpack(out FPType Type, out bool Sign, out uint Op);
  930. float Result;
  931. if (Type == FPType.SNaN || Type == FPType.QNaN)
  932. {
  933. Result = FPProcessNaN(Type, Op, State);
  934. }
  935. else
  936. {
  937. uint NotExp = (~Op >> 23) & 0xFFu;
  938. uint MaxExp = 0xFEu;
  939. Result = BitConverter.Int32BitsToSingle(
  940. (int)((Sign ? 1u : 0u) << 31 | (NotExp == 0xFFu ? MaxExp : NotExp) << 23));
  941. }
  942. return Result;
  943. }
  944. public static float FPRSqrtStepFused(float Value1, float Value2, AThreadState State)
  945. {
  946. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPRSqrtStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  947. Value1 = Value1.FPNeg();
  948. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  949. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  950. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  951. if (!Done)
  952. {
  953. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  954. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  955. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  956. {
  957. Result = FPOnePointFive(false);
  958. }
  959. else if (Inf1 || Inf2)
  960. {
  961. Result = FPInfinity(Sign1 ^ Sign2);
  962. }
  963. else
  964. {
  965. // TODO: When available, use: T MathF.FusedMultiplyAdd(T, T, T);
  966. // https://github.com/dotnet/corefx/issues/31903
  967. Result = (3f + (Value1 * Value2)) / 2f;
  968. }
  969. }
  970. return Result;
  971. }
  972. public static float FPSqrt(float Value, AThreadState State)
  973. {
  974. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPSqrt: State.Fpcr = 0x{State.Fpcr:X8}");
  975. Value = Value.FPUnpack(out FPType Type, out bool Sign, out uint Op);
  976. float Result;
  977. if (Type == FPType.SNaN || Type == FPType.QNaN)
  978. {
  979. Result = FPProcessNaN(Type, Op, State);
  980. }
  981. else if (Type == FPType.Zero)
  982. {
  983. Result = FPZero(Sign);
  984. }
  985. else if (Type == FPType.Infinity && !Sign)
  986. {
  987. Result = FPInfinity(Sign);
  988. }
  989. else if (Sign)
  990. {
  991. Result = FPDefaultNaN();
  992. FPProcessException(FPExc.InvalidOp, State);
  993. }
  994. else
  995. {
  996. Result = MathF.Sqrt(Value);
  997. }
  998. return Result;
  999. }
  1000. public static float FPSub(float Value1, float Value2, AThreadState State)
  1001. {
  1002. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPSub: State.Fpcr = 0x{State.Fpcr:X8}");
  1003. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  1004. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  1005. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  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 && Inf2 && Sign1 == Sign2)
  1011. {
  1012. Result = FPDefaultNaN();
  1013. FPProcessException(FPExc.InvalidOp, State);
  1014. }
  1015. else if ((Inf1 && !Sign1) || (Inf2 && Sign2))
  1016. {
  1017. Result = FPInfinity(false);
  1018. }
  1019. else if ((Inf1 && Sign1) || (Inf2 && !Sign2))
  1020. {
  1021. Result = FPInfinity(true);
  1022. }
  1023. else if (Zero1 && Zero2 && Sign1 == !Sign2)
  1024. {
  1025. Result = FPZero(Sign1);
  1026. }
  1027. else
  1028. {
  1029. Result = Value1 - Value2;
  1030. }
  1031. }
  1032. return Result;
  1033. }
  1034. private static float FPDefaultNaN()
  1035. {
  1036. return -float.NaN;
  1037. }
  1038. private static float FPInfinity(bool Sign)
  1039. {
  1040. return Sign ? float.NegativeInfinity : float.PositiveInfinity;
  1041. }
  1042. private static float FPZero(bool Sign)
  1043. {
  1044. return Sign ? -0f : +0f;
  1045. }
  1046. private static float FPTwo(bool Sign)
  1047. {
  1048. return Sign ? -2f : +2f;
  1049. }
  1050. private static float FPOnePointFive(bool Sign)
  1051. {
  1052. return Sign ? -1.5f : +1.5f;
  1053. }
  1054. private static float FPNeg(this float Value)
  1055. {
  1056. return -Value;
  1057. }
  1058. private static float FPUnpack(this float Value, out FPType Type, out bool Sign, out uint ValueBits)
  1059. {
  1060. ValueBits = (uint)BitConverter.SingleToInt32Bits(Value);
  1061. Sign = (~ValueBits & 0x80000000u) == 0u;
  1062. if ((ValueBits & 0x7F800000u) == 0u)
  1063. {
  1064. if ((ValueBits & 0x007FFFFFu) == 0u)
  1065. {
  1066. Type = FPType.Zero;
  1067. }
  1068. else
  1069. {
  1070. Type = FPType.Nonzero;
  1071. }
  1072. }
  1073. else if ((~ValueBits & 0x7F800000u) == 0u)
  1074. {
  1075. if ((ValueBits & 0x007FFFFFu) == 0u)
  1076. {
  1077. Type = FPType.Infinity;
  1078. }
  1079. else
  1080. {
  1081. Type = (~ValueBits & 0x00400000u) == 0u
  1082. ? FPType.QNaN
  1083. : FPType.SNaN;
  1084. return FPZero(Sign);
  1085. }
  1086. }
  1087. else
  1088. {
  1089. Type = FPType.Nonzero;
  1090. }
  1091. return Value;
  1092. }
  1093. private static float FPProcessNaNs(
  1094. FPType Type1,
  1095. FPType Type2,
  1096. uint Op1,
  1097. uint Op2,
  1098. AThreadState State,
  1099. out bool Done)
  1100. {
  1101. Done = true;
  1102. if (Type1 == FPType.SNaN)
  1103. {
  1104. return FPProcessNaN(Type1, Op1, State);
  1105. }
  1106. else if (Type2 == FPType.SNaN)
  1107. {
  1108. return FPProcessNaN(Type2, Op2, State);
  1109. }
  1110. else if (Type1 == FPType.QNaN)
  1111. {
  1112. return FPProcessNaN(Type1, Op1, State);
  1113. }
  1114. else if (Type2 == FPType.QNaN)
  1115. {
  1116. return FPProcessNaN(Type2, Op2, State);
  1117. }
  1118. Done = false;
  1119. return FPZero(false);
  1120. }
  1121. private static float FPProcessNaNs3(
  1122. FPType Type1,
  1123. FPType Type2,
  1124. FPType Type3,
  1125. uint Op1,
  1126. uint Op2,
  1127. uint Op3,
  1128. AThreadState State,
  1129. out bool Done)
  1130. {
  1131. Done = true;
  1132. if (Type1 == FPType.SNaN)
  1133. {
  1134. return FPProcessNaN(Type1, Op1, State);
  1135. }
  1136. else if (Type2 == FPType.SNaN)
  1137. {
  1138. return FPProcessNaN(Type2, Op2, State);
  1139. }
  1140. else if (Type3 == FPType.SNaN)
  1141. {
  1142. return FPProcessNaN(Type3, Op3, State);
  1143. }
  1144. else if (Type1 == FPType.QNaN)
  1145. {
  1146. return FPProcessNaN(Type1, Op1, State);
  1147. }
  1148. else if (Type2 == FPType.QNaN)
  1149. {
  1150. return FPProcessNaN(Type2, Op2, State);
  1151. }
  1152. else if (Type3 == FPType.QNaN)
  1153. {
  1154. return FPProcessNaN(Type3, Op3, State);
  1155. }
  1156. Done = false;
  1157. return FPZero(false);
  1158. }
  1159. private static float FPProcessNaN(FPType Type, uint Op, AThreadState State)
  1160. {
  1161. if (Type == FPType.SNaN)
  1162. {
  1163. Op |= 1u << 22;
  1164. FPProcessException(FPExc.InvalidOp, State);
  1165. }
  1166. if (State.GetFpcrFlag(FPCR.DN))
  1167. {
  1168. return FPDefaultNaN();
  1169. }
  1170. return BitConverter.Int32BitsToSingle((int)Op);
  1171. }
  1172. private static void FPProcessException(FPExc Exc, AThreadState State)
  1173. {
  1174. int Enable = (int)Exc + 8;
  1175. if ((State.Fpcr & (1 << Enable)) != 0)
  1176. {
  1177. throw new NotImplementedException("floating-point trap handling");
  1178. }
  1179. else
  1180. {
  1181. State.Fpsr |= 1 << (int)Exc;
  1182. }
  1183. }
  1184. }
  1185. static class ASoftFloat_64
  1186. {
  1187. public static double FPAdd(double Value1, double Value2, AThreadState State)
  1188. {
  1189. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  1190. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1191. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1192. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1193. if (!Done)
  1194. {
  1195. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1196. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1197. if (Inf1 && Inf2 && Sign1 == !Sign2)
  1198. {
  1199. Result = FPDefaultNaN();
  1200. FPProcessException(FPExc.InvalidOp, State);
  1201. }
  1202. else if ((Inf1 && !Sign1) || (Inf2 && !Sign2))
  1203. {
  1204. Result = FPInfinity(false);
  1205. }
  1206. else if ((Inf1 && Sign1) || (Inf2 && Sign2))
  1207. {
  1208. Result = FPInfinity(true);
  1209. }
  1210. else if (Zero1 && Zero2 && Sign1 == Sign2)
  1211. {
  1212. Result = FPZero(Sign1);
  1213. }
  1214. else
  1215. {
  1216. Result = Value1 + Value2;
  1217. }
  1218. }
  1219. return Result;
  1220. }
  1221. public static double FPDiv(double Value1, double Value2, AThreadState State)
  1222. {
  1223. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPDiv: State.Fpcr = 0x{State.Fpcr:X8}");
  1224. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1225. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1226. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1227. if (!Done)
  1228. {
  1229. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1230. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1231. if ((Inf1 && Inf2) || (Zero1 && Zero2))
  1232. {
  1233. Result = FPDefaultNaN();
  1234. FPProcessException(FPExc.InvalidOp, State);
  1235. }
  1236. else if (Inf1 || Zero2)
  1237. {
  1238. Result = FPInfinity(Sign1 ^ Sign2);
  1239. if (!Inf1) FPProcessException(FPExc.DivideByZero, State);
  1240. }
  1241. else if (Zero1 || Inf2)
  1242. {
  1243. Result = FPZero(Sign1 ^ Sign2);
  1244. }
  1245. else
  1246. {
  1247. Result = Value1 / Value2;
  1248. }
  1249. }
  1250. return Result;
  1251. }
  1252. public static double FPMax(double Value1, double Value2, AThreadState State)
  1253. {
  1254. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMax: State.Fpcr = 0x{State.Fpcr:X8}");
  1255. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1256. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1257. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1258. if (!Done)
  1259. {
  1260. if (Value1 > Value2)
  1261. {
  1262. if (Type1 == FPType.Infinity)
  1263. {
  1264. Result = FPInfinity(Sign1);
  1265. }
  1266. else if (Type1 == FPType.Zero)
  1267. {
  1268. Result = FPZero(Sign1 && Sign2);
  1269. }
  1270. else
  1271. {
  1272. Result = Value1;
  1273. }
  1274. }
  1275. else
  1276. {
  1277. if (Type2 == FPType.Infinity)
  1278. {
  1279. Result = FPInfinity(Sign2);
  1280. }
  1281. else if (Type2 == FPType.Zero)
  1282. {
  1283. Result = FPZero(Sign1 && Sign2);
  1284. }
  1285. else
  1286. {
  1287. Result = Value2;
  1288. }
  1289. }
  1290. }
  1291. return Result;
  1292. }
  1293. public static double FPMaxNum(double Value1, double Value2, AThreadState State)
  1294. {
  1295. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_64.FPMaxNum: ");
  1296. Value1.FPUnpack(out FPType Type1, out _, out _);
  1297. Value2.FPUnpack(out FPType Type2, out _, out _);
  1298. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  1299. {
  1300. Value1 = FPInfinity(true);
  1301. }
  1302. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  1303. {
  1304. Value2 = FPInfinity(true);
  1305. }
  1306. return FPMax(Value1, Value2, State);
  1307. }
  1308. public static double FPMin(double Value1, double Value2, AThreadState State)
  1309. {
  1310. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMin: State.Fpcr = 0x{State.Fpcr:X8}");
  1311. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1312. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1313. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1314. if (!Done)
  1315. {
  1316. if (Value1 < Value2)
  1317. {
  1318. if (Type1 == FPType.Infinity)
  1319. {
  1320. Result = FPInfinity(Sign1);
  1321. }
  1322. else if (Type1 == FPType.Zero)
  1323. {
  1324. Result = FPZero(Sign1 || Sign2);
  1325. }
  1326. else
  1327. {
  1328. Result = Value1;
  1329. }
  1330. }
  1331. else
  1332. {
  1333. if (Type2 == FPType.Infinity)
  1334. {
  1335. Result = FPInfinity(Sign2);
  1336. }
  1337. else if (Type2 == FPType.Zero)
  1338. {
  1339. Result = FPZero(Sign1 || Sign2);
  1340. }
  1341. else
  1342. {
  1343. Result = Value2;
  1344. }
  1345. }
  1346. }
  1347. return Result;
  1348. }
  1349. public static double FPMinNum(double Value1, double Value2, AThreadState State)
  1350. {
  1351. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_64.FPMinNum: ");
  1352. Value1.FPUnpack(out FPType Type1, out _, out _);
  1353. Value2.FPUnpack(out FPType Type2, out _, out _);
  1354. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  1355. {
  1356. Value1 = FPInfinity(false);
  1357. }
  1358. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  1359. {
  1360. Value2 = FPInfinity(false);
  1361. }
  1362. return FPMin(Value1, Value2, State);
  1363. }
  1364. public static double FPMul(double Value1, double Value2, AThreadState State)
  1365. {
  1366. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMul: State.Fpcr = 0x{State.Fpcr:X8}");
  1367. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1368. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1369. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1370. if (!Done)
  1371. {
  1372. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1373. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1374. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1375. {
  1376. Result = FPDefaultNaN();
  1377. FPProcessException(FPExc.InvalidOp, State);
  1378. }
  1379. else if (Inf1 || Inf2)
  1380. {
  1381. Result = FPInfinity(Sign1 ^ Sign2);
  1382. }
  1383. else if (Zero1 || Zero2)
  1384. {
  1385. Result = FPZero(Sign1 ^ Sign2);
  1386. }
  1387. else
  1388. {
  1389. Result = Value1 * Value2;
  1390. }
  1391. }
  1392. return Result;
  1393. }
  1394. public static double FPMulAdd(double ValueA, double Value1, double Value2, AThreadState State)
  1395. {
  1396. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMulAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  1397. ValueA = ValueA.FPUnpack(out FPType TypeA, out bool SignA, out ulong Addend);
  1398. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1399. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1400. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1401. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1402. double Result = FPProcessNaNs3(TypeA, Type1, Type2, Addend, Op1, Op2, State, out bool Done);
  1403. if (TypeA == FPType.QNaN && ((Inf1 && Zero2) || (Zero1 && Inf2)))
  1404. {
  1405. Result = FPDefaultNaN();
  1406. FPProcessException(FPExc.InvalidOp, State);
  1407. }
  1408. if (!Done)
  1409. {
  1410. bool InfA = TypeA == FPType.Infinity; bool ZeroA = TypeA == FPType.Zero;
  1411. bool SignP = Sign1 ^ Sign2;
  1412. bool InfP = Inf1 || Inf2;
  1413. bool ZeroP = Zero1 || Zero2;
  1414. if ((Inf1 && Zero2) || (Zero1 && Inf2) || (InfA && InfP && SignA != SignP))
  1415. {
  1416. Result = FPDefaultNaN();
  1417. FPProcessException(FPExc.InvalidOp, State);
  1418. }
  1419. else if ((InfA && !SignA) || (InfP && !SignP))
  1420. {
  1421. Result = FPInfinity(false);
  1422. }
  1423. else if ((InfA && SignA) || (InfP && SignP))
  1424. {
  1425. Result = FPInfinity(true);
  1426. }
  1427. else if (ZeroA && ZeroP && SignA == SignP)
  1428. {
  1429. Result = FPZero(SignA);
  1430. }
  1431. else
  1432. {
  1433. // TODO: When available, use: T Math.FusedMultiplyAdd(T, T, T);
  1434. // https://github.com/dotnet/corefx/issues/31903
  1435. Result = ValueA + (Value1 * Value2);
  1436. }
  1437. }
  1438. return Result;
  1439. }
  1440. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1441. public static double FPMulSub(double ValueA, double Value1, double Value2, AThreadState State)
  1442. {
  1443. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_64.FPMulSub: ");
  1444. Value1 = Value1.FPNeg();
  1445. return FPMulAdd(ValueA, Value1, Value2, State);
  1446. }
  1447. public static double FPMulX(double Value1, double Value2, AThreadState State)
  1448. {
  1449. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMulX: State.Fpcr = 0x{State.Fpcr:X8}");
  1450. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1451. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1452. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1453. if (!Done)
  1454. {
  1455. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1456. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1457. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1458. {
  1459. Result = FPTwo(Sign1 ^ Sign2);
  1460. }
  1461. else if (Inf1 || Inf2)
  1462. {
  1463. Result = FPInfinity(Sign1 ^ Sign2);
  1464. }
  1465. else if (Zero1 || Zero2)
  1466. {
  1467. Result = FPZero(Sign1 ^ Sign2);
  1468. }
  1469. else
  1470. {
  1471. Result = Value1 * Value2;
  1472. }
  1473. }
  1474. return Result;
  1475. }
  1476. public static double FPRecipStepFused(double Value1, double Value2, AThreadState State)
  1477. {
  1478. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPRecipStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  1479. Value1 = Value1.FPNeg();
  1480. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1481. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1482. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1483. if (!Done)
  1484. {
  1485. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1486. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1487. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1488. {
  1489. Result = FPTwo(false);
  1490. }
  1491. else if (Inf1 || Inf2)
  1492. {
  1493. Result = FPInfinity(Sign1 ^ Sign2);
  1494. }
  1495. else
  1496. {
  1497. // TODO: When available, use: T Math.FusedMultiplyAdd(T, T, T);
  1498. // https://github.com/dotnet/corefx/issues/31903
  1499. Result = 2d + (Value1 * Value2);
  1500. }
  1501. }
  1502. return Result;
  1503. }
  1504. public static double FPRecpX(double Value, AThreadState State)
  1505. {
  1506. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPRecpX: State.Fpcr = 0x{State.Fpcr:X8}");
  1507. Value.FPUnpack(out FPType Type, out bool Sign, out ulong Op);
  1508. double Result;
  1509. if (Type == FPType.SNaN || Type == FPType.QNaN)
  1510. {
  1511. Result = FPProcessNaN(Type, Op, State);
  1512. }
  1513. else
  1514. {
  1515. ulong NotExp = (~Op >> 52) & 0x7FFul;
  1516. ulong MaxExp = 0x7FEul;
  1517. Result = BitConverter.Int64BitsToDouble(
  1518. (long)((Sign ? 1ul : 0ul) << 63 | (NotExp == 0x7FFul ? MaxExp : NotExp) << 52));
  1519. }
  1520. return Result;
  1521. }
  1522. public static double FPRSqrtStepFused(double Value1, double Value2, AThreadState State)
  1523. {
  1524. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPRSqrtStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  1525. Value1 = Value1.FPNeg();
  1526. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1527. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1528. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1529. if (!Done)
  1530. {
  1531. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1532. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1533. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1534. {
  1535. Result = FPOnePointFive(false);
  1536. }
  1537. else if (Inf1 || Inf2)
  1538. {
  1539. Result = FPInfinity(Sign1 ^ Sign2);
  1540. }
  1541. else
  1542. {
  1543. // TODO: When available, use: T Math.FusedMultiplyAdd(T, T, T);
  1544. // https://github.com/dotnet/corefx/issues/31903
  1545. Result = (3d + (Value1 * Value2)) / 2d;
  1546. }
  1547. }
  1548. return Result;
  1549. }
  1550. public static double FPSqrt(double Value, AThreadState State)
  1551. {
  1552. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPSqrt: State.Fpcr = 0x{State.Fpcr:X8}");
  1553. Value = Value.FPUnpack(out FPType Type, out bool Sign, out ulong Op);
  1554. double Result;
  1555. if (Type == FPType.SNaN || Type == FPType.QNaN)
  1556. {
  1557. Result = FPProcessNaN(Type, Op, State);
  1558. }
  1559. else if (Type == FPType.Zero)
  1560. {
  1561. Result = FPZero(Sign);
  1562. }
  1563. else if (Type == FPType.Infinity && !Sign)
  1564. {
  1565. Result = FPInfinity(Sign);
  1566. }
  1567. else if (Sign)
  1568. {
  1569. Result = FPDefaultNaN();
  1570. FPProcessException(FPExc.InvalidOp, State);
  1571. }
  1572. else
  1573. {
  1574. Result = Math.Sqrt(Value);
  1575. }
  1576. return Result;
  1577. }
  1578. public static double FPSub(double Value1, double Value2, AThreadState State)
  1579. {
  1580. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPSub: State.Fpcr = 0x{State.Fpcr:X8}");
  1581. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1582. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1583. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1584. if (!Done)
  1585. {
  1586. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1587. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1588. if (Inf1 && Inf2 && Sign1 == Sign2)
  1589. {
  1590. Result = FPDefaultNaN();
  1591. FPProcessException(FPExc.InvalidOp, State);
  1592. }
  1593. else if ((Inf1 && !Sign1) || (Inf2 && Sign2))
  1594. {
  1595. Result = FPInfinity(false);
  1596. }
  1597. else if ((Inf1 && Sign1) || (Inf2 && !Sign2))
  1598. {
  1599. Result = FPInfinity(true);
  1600. }
  1601. else if (Zero1 && Zero2 && Sign1 == !Sign2)
  1602. {
  1603. Result = FPZero(Sign1);
  1604. }
  1605. else
  1606. {
  1607. Result = Value1 - Value2;
  1608. }
  1609. }
  1610. return Result;
  1611. }
  1612. private static double FPDefaultNaN()
  1613. {
  1614. return -double.NaN;
  1615. }
  1616. private static double FPInfinity(bool Sign)
  1617. {
  1618. return Sign ? double.NegativeInfinity : double.PositiveInfinity;
  1619. }
  1620. private static double FPZero(bool Sign)
  1621. {
  1622. return Sign ? -0d : +0d;
  1623. }
  1624. private static double FPTwo(bool Sign)
  1625. {
  1626. return Sign ? -2d : +2d;
  1627. }
  1628. private static double FPOnePointFive(bool Sign)
  1629. {
  1630. return Sign ? -1.5d : +1.5d;
  1631. }
  1632. private static double FPNeg(this double Value)
  1633. {
  1634. return -Value;
  1635. }
  1636. private static double FPUnpack(this double Value, out FPType Type, out bool Sign, out ulong ValueBits)
  1637. {
  1638. ValueBits = (ulong)BitConverter.DoubleToInt64Bits(Value);
  1639. Sign = (~ValueBits & 0x8000000000000000ul) == 0ul;
  1640. if ((ValueBits & 0x7FF0000000000000ul) == 0ul)
  1641. {
  1642. if ((ValueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  1643. {
  1644. Type = FPType.Zero;
  1645. }
  1646. else
  1647. {
  1648. Type = FPType.Nonzero;
  1649. }
  1650. }
  1651. else if ((~ValueBits & 0x7FF0000000000000ul) == 0ul)
  1652. {
  1653. if ((ValueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  1654. {
  1655. Type = FPType.Infinity;
  1656. }
  1657. else
  1658. {
  1659. Type = (~ValueBits & 0x0008000000000000ul) == 0ul
  1660. ? FPType.QNaN
  1661. : FPType.SNaN;
  1662. return FPZero(Sign);
  1663. }
  1664. }
  1665. else
  1666. {
  1667. Type = FPType.Nonzero;
  1668. }
  1669. return Value;
  1670. }
  1671. private static double FPProcessNaNs(
  1672. FPType Type1,
  1673. FPType Type2,
  1674. ulong Op1,
  1675. ulong Op2,
  1676. AThreadState State,
  1677. out bool Done)
  1678. {
  1679. Done = true;
  1680. if (Type1 == FPType.SNaN)
  1681. {
  1682. return FPProcessNaN(Type1, Op1, State);
  1683. }
  1684. else if (Type2 == FPType.SNaN)
  1685. {
  1686. return FPProcessNaN(Type2, Op2, State);
  1687. }
  1688. else if (Type1 == FPType.QNaN)
  1689. {
  1690. return FPProcessNaN(Type1, Op1, State);
  1691. }
  1692. else if (Type2 == FPType.QNaN)
  1693. {
  1694. return FPProcessNaN(Type2, Op2, State);
  1695. }
  1696. Done = false;
  1697. return FPZero(false);
  1698. }
  1699. private static double FPProcessNaNs3(
  1700. FPType Type1,
  1701. FPType Type2,
  1702. FPType Type3,
  1703. ulong Op1,
  1704. ulong Op2,
  1705. ulong Op3,
  1706. AThreadState State,
  1707. out bool Done)
  1708. {
  1709. Done = true;
  1710. if (Type1 == FPType.SNaN)
  1711. {
  1712. return FPProcessNaN(Type1, Op1, State);
  1713. }
  1714. else if (Type2 == FPType.SNaN)
  1715. {
  1716. return FPProcessNaN(Type2, Op2, State);
  1717. }
  1718. else if (Type3 == FPType.SNaN)
  1719. {
  1720. return FPProcessNaN(Type3, Op3, State);
  1721. }
  1722. else if (Type1 == FPType.QNaN)
  1723. {
  1724. return FPProcessNaN(Type1, Op1, State);
  1725. }
  1726. else if (Type2 == FPType.QNaN)
  1727. {
  1728. return FPProcessNaN(Type2, Op2, State);
  1729. }
  1730. else if (Type3 == FPType.QNaN)
  1731. {
  1732. return FPProcessNaN(Type3, Op3, State);
  1733. }
  1734. Done = false;
  1735. return FPZero(false);
  1736. }
  1737. private static double FPProcessNaN(FPType Type, ulong Op, AThreadState State)
  1738. {
  1739. if (Type == FPType.SNaN)
  1740. {
  1741. Op |= 1ul << 51;
  1742. FPProcessException(FPExc.InvalidOp, State);
  1743. }
  1744. if (State.GetFpcrFlag(FPCR.DN))
  1745. {
  1746. return FPDefaultNaN();
  1747. }
  1748. return BitConverter.Int64BitsToDouble((long)Op);
  1749. }
  1750. private static void FPProcessException(FPExc Exc, AThreadState State)
  1751. {
  1752. int Enable = (int)Exc + 8;
  1753. if ((State.Fpcr & (1 << Enable)) != 0)
  1754. {
  1755. throw new NotImplementedException("floating-point trap handling");
  1756. }
  1757. else
  1758. {
  1759. State.Fpsr |= 1 << (int)Exc;
  1760. }
  1761. }
  1762. }
  1763. }