ASoftFloat.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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. public static float ConvertHalfToSingle(ushort x)
  171. {
  172. uint x_sign = (uint)(x >> 15) & 0x0001;
  173. uint x_exp = (uint)(x >> 10) & 0x001F;
  174. uint x_mantissa = (uint)x & 0x03FF;
  175. if (x_exp == 0 && x_mantissa == 0)
  176. {
  177. // Zero
  178. return BitConverter.Int32BitsToSingle((int)(x_sign << 31));
  179. }
  180. if (x_exp == 0x1F)
  181. {
  182. // NaN or Infinity
  183. return BitConverter.Int32BitsToSingle((int)((x_sign << 31) | 0x7F800000 | (x_mantissa << 13)));
  184. }
  185. int exponent = (int)x_exp - 15;
  186. if (x_exp == 0)
  187. {
  188. // Denormal
  189. x_mantissa <<= 1;
  190. while ((x_mantissa & 0x0400) == 0)
  191. {
  192. x_mantissa <<= 1;
  193. exponent--;
  194. }
  195. x_mantissa &= 0x03FF;
  196. }
  197. uint new_exp = (uint)((exponent + 127) & 0xFF) << 23;
  198. return BitConverter.Int32BitsToSingle((int)((x_sign << 31) | new_exp | (x_mantissa << 13)));
  199. }
  200. }
  201. static class ASoftFloat_32
  202. {
  203. public static float FPAdd(float Value1, float Value2, AThreadState State)
  204. {
  205. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  206. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  207. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  208. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  209. if (!Done)
  210. {
  211. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  212. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  213. if (Inf1 && Inf2 && Sign1 == !Sign2)
  214. {
  215. Result = FPDefaultNaN();
  216. FPProcessException(FPExc.InvalidOp, State);
  217. }
  218. else if ((Inf1 && !Sign1) || (Inf2 && !Sign2))
  219. {
  220. Result = FPInfinity(false);
  221. }
  222. else if ((Inf1 && Sign1) || (Inf2 && Sign2))
  223. {
  224. Result = FPInfinity(true);
  225. }
  226. else if (Zero1 && Zero2 && Sign1 == Sign2)
  227. {
  228. Result = FPZero(Sign1);
  229. }
  230. else
  231. {
  232. Result = Value1 + Value2;
  233. }
  234. }
  235. return Result;
  236. }
  237. public static float FPDiv(float Value1, float Value2, AThreadState State)
  238. {
  239. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPDiv: State.Fpcr = 0x{State.Fpcr:X8}");
  240. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  241. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  242. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  243. if (!Done)
  244. {
  245. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  246. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  247. if ((Inf1 && Inf2) || (Zero1 && Zero2))
  248. {
  249. Result = FPDefaultNaN();
  250. FPProcessException(FPExc.InvalidOp, State);
  251. }
  252. else if (Inf1 || Zero2)
  253. {
  254. Result = FPInfinity(Sign1 ^ Sign2);
  255. if (!Inf1) FPProcessException(FPExc.DivideByZero, State);
  256. }
  257. else if (Zero1 || Inf2)
  258. {
  259. Result = FPZero(Sign1 ^ Sign2);
  260. }
  261. else
  262. {
  263. Result = Value1 / Value2;
  264. }
  265. }
  266. return Result;
  267. }
  268. public static float FPMax(float Value1, float Value2, AThreadState State)
  269. {
  270. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMax: State.Fpcr = 0x{State.Fpcr:X8}");
  271. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  272. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  273. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  274. if (!Done)
  275. {
  276. if (Value1 > Value2)
  277. {
  278. if (Type1 == FPType.Infinity)
  279. {
  280. Result = FPInfinity(Sign1);
  281. }
  282. else if (Type1 == FPType.Zero)
  283. {
  284. Result = FPZero(Sign1 && Sign2);
  285. }
  286. else
  287. {
  288. Result = Value1;
  289. }
  290. }
  291. else
  292. {
  293. if (Type2 == FPType.Infinity)
  294. {
  295. Result = FPInfinity(Sign2);
  296. }
  297. else if (Type2 == FPType.Zero)
  298. {
  299. Result = FPZero(Sign1 && Sign2);
  300. }
  301. else
  302. {
  303. Result = Value2;
  304. }
  305. }
  306. }
  307. return Result;
  308. }
  309. public static float FPMaxNum(float Value1, float Value2, AThreadState State)
  310. {
  311. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_32.FPMaxNum: ");
  312. Value1.FPUnpack(out FPType Type1, out _, out _);
  313. Value2.FPUnpack(out FPType Type2, out _, out _);
  314. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  315. {
  316. Value1 = FPInfinity(true);
  317. }
  318. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  319. {
  320. Value2 = FPInfinity(true);
  321. }
  322. return FPMax(Value1, Value2, State);
  323. }
  324. public static float FPMin(float Value1, float Value2, AThreadState State)
  325. {
  326. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMin: State.Fpcr = 0x{State.Fpcr:X8}");
  327. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  328. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  329. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  330. if (!Done)
  331. {
  332. if (Value1 < Value2)
  333. {
  334. if (Type1 == FPType.Infinity)
  335. {
  336. Result = FPInfinity(Sign1);
  337. }
  338. else if (Type1 == FPType.Zero)
  339. {
  340. Result = FPZero(Sign1 || Sign2);
  341. }
  342. else
  343. {
  344. Result = Value1;
  345. }
  346. }
  347. else
  348. {
  349. if (Type2 == FPType.Infinity)
  350. {
  351. Result = FPInfinity(Sign2);
  352. }
  353. else if (Type2 == FPType.Zero)
  354. {
  355. Result = FPZero(Sign1 || Sign2);
  356. }
  357. else
  358. {
  359. Result = Value2;
  360. }
  361. }
  362. }
  363. return Result;
  364. }
  365. public static float FPMinNum(float Value1, float Value2, AThreadState State)
  366. {
  367. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_32.FPMinNum: ");
  368. Value1.FPUnpack(out FPType Type1, out _, out _);
  369. Value2.FPUnpack(out FPType Type2, out _, out _);
  370. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  371. {
  372. Value1 = FPInfinity(false);
  373. }
  374. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  375. {
  376. Value2 = FPInfinity(false);
  377. }
  378. return FPMin(Value1, Value2, State);
  379. }
  380. public static float FPMul(float Value1, float Value2, AThreadState State)
  381. {
  382. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMul: State.Fpcr = 0x{State.Fpcr:X8}");
  383. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  384. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  385. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  386. if (!Done)
  387. {
  388. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  389. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  390. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  391. {
  392. Result = FPDefaultNaN();
  393. FPProcessException(FPExc.InvalidOp, State);
  394. }
  395. else if (Inf1 || Inf2)
  396. {
  397. Result = FPInfinity(Sign1 ^ Sign2);
  398. }
  399. else if (Zero1 || Zero2)
  400. {
  401. Result = FPZero(Sign1 ^ Sign2);
  402. }
  403. else
  404. {
  405. Result = Value1 * Value2;
  406. }
  407. }
  408. return Result;
  409. }
  410. public static float FPMulAdd(float ValueA, float Value1, float Value2, AThreadState State)
  411. {
  412. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMulAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  413. ValueA = ValueA.FPUnpack(out FPType TypeA, out bool SignA, out uint Addend);
  414. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  415. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  416. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  417. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  418. float Result = FPProcessNaNs3(TypeA, Type1, Type2, Addend, Op1, Op2, State, out bool Done);
  419. if (TypeA == FPType.QNaN && ((Inf1 && Zero2) || (Zero1 && Inf2)))
  420. {
  421. Result = FPDefaultNaN();
  422. FPProcessException(FPExc.InvalidOp, State);
  423. }
  424. if (!Done)
  425. {
  426. bool InfA = TypeA == FPType.Infinity; bool ZeroA = TypeA == FPType.Zero;
  427. bool SignP = Sign1 ^ Sign2;
  428. bool InfP = Inf1 || Inf2;
  429. bool ZeroP = Zero1 || Zero2;
  430. if ((Inf1 && Zero2) || (Zero1 && Inf2) || (InfA && InfP && SignA != SignP))
  431. {
  432. Result = FPDefaultNaN();
  433. FPProcessException(FPExc.InvalidOp, State);
  434. }
  435. else if ((InfA && !SignA) || (InfP && !SignP))
  436. {
  437. Result = FPInfinity(false);
  438. }
  439. else if ((InfA && SignA) || (InfP && SignP))
  440. {
  441. Result = FPInfinity(true);
  442. }
  443. else if (ZeroA && ZeroP && SignA == SignP)
  444. {
  445. Result = FPZero(SignA);
  446. }
  447. else
  448. {
  449. // TODO: When available, use: T MathF.FusedMultiplyAdd(T, T, T);
  450. // https://github.com/dotnet/corefx/issues/31903
  451. Result = ValueA + (Value1 * Value2);
  452. }
  453. }
  454. return Result;
  455. }
  456. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  457. public static float FPMulSub(float ValueA, float Value1, float Value2, AThreadState State)
  458. {
  459. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_32.FPMulSub: ");
  460. Value1 = Value1.FPNeg();
  461. return FPMulAdd(ValueA, Value1, Value2, State);
  462. }
  463. public static float FPMulX(float Value1, float Value2, AThreadState State)
  464. {
  465. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPMulX: State.Fpcr = 0x{State.Fpcr:X8}");
  466. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  467. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  468. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  469. if (!Done)
  470. {
  471. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  472. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  473. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  474. {
  475. Result = FPTwo(Sign1 ^ Sign2);
  476. }
  477. else if (Inf1 || Inf2)
  478. {
  479. Result = FPInfinity(Sign1 ^ Sign2);
  480. }
  481. else if (Zero1 || Zero2)
  482. {
  483. Result = FPZero(Sign1 ^ Sign2);
  484. }
  485. else
  486. {
  487. Result = Value1 * Value2;
  488. }
  489. }
  490. return Result;
  491. }
  492. public static float FPRecipStepFused(float Value1, float Value2, AThreadState State)
  493. {
  494. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPRecipStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  495. Value1 = Value1.FPNeg();
  496. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  497. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  498. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  499. if (!Done)
  500. {
  501. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  502. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  503. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  504. {
  505. Result = FPTwo(false);
  506. }
  507. else if (Inf1 || Inf2)
  508. {
  509. Result = FPInfinity(Sign1 ^ Sign2);
  510. }
  511. else
  512. {
  513. // TODO: When available, use: T MathF.FusedMultiplyAdd(T, T, T);
  514. // https://github.com/dotnet/corefx/issues/31903
  515. Result = 2f + (Value1 * Value2);
  516. }
  517. }
  518. return Result;
  519. }
  520. public static float FPRecpX(float Value, AThreadState State)
  521. {
  522. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPRecpX: State.Fpcr = 0x{State.Fpcr:X8}");
  523. Value.FPUnpack(out FPType Type, out bool Sign, out uint Op);
  524. float Result;
  525. if (Type == FPType.SNaN || Type == FPType.QNaN)
  526. {
  527. Result = FPProcessNaN(Type, Op, State);
  528. }
  529. else
  530. {
  531. uint NotExp = (~Op >> 23) & 0xFFu;
  532. uint MaxExp = 0xFEu;
  533. Result = BitConverter.Int32BitsToSingle(
  534. (int)((Sign ? 1u : 0u) << 31 | (NotExp == 0xFFu ? MaxExp : NotExp) << 23));
  535. }
  536. return Result;
  537. }
  538. public static float FPRSqrtStepFused(float Value1, float Value2, AThreadState State)
  539. {
  540. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPRSqrtStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  541. Value1 = Value1.FPNeg();
  542. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  543. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  544. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  545. if (!Done)
  546. {
  547. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  548. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  549. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  550. {
  551. Result = FPOnePointFive(false);
  552. }
  553. else if (Inf1 || Inf2)
  554. {
  555. Result = FPInfinity(Sign1 ^ Sign2);
  556. }
  557. else
  558. {
  559. // TODO: When available, use: T MathF.FusedMultiplyAdd(T, T, T);
  560. // https://github.com/dotnet/corefx/issues/31903
  561. Result = (3f + (Value1 * Value2)) / 2f;
  562. }
  563. }
  564. return Result;
  565. }
  566. public static float FPSqrt(float Value, AThreadState State)
  567. {
  568. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPSqrt: State.Fpcr = 0x{State.Fpcr:X8}");
  569. Value = Value.FPUnpack(out FPType Type, out bool Sign, out uint Op);
  570. float Result;
  571. if (Type == FPType.SNaN || Type == FPType.QNaN)
  572. {
  573. Result = FPProcessNaN(Type, Op, State);
  574. }
  575. else if (Type == FPType.Zero)
  576. {
  577. Result = FPZero(Sign);
  578. }
  579. else if (Type == FPType.Infinity && !Sign)
  580. {
  581. Result = FPInfinity(Sign);
  582. }
  583. else if (Sign)
  584. {
  585. Result = FPDefaultNaN();
  586. FPProcessException(FPExc.InvalidOp, State);
  587. }
  588. else
  589. {
  590. Result = MathF.Sqrt(Value);
  591. }
  592. return Result;
  593. }
  594. public static float FPSub(float Value1, float Value2, AThreadState State)
  595. {
  596. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_32.FPSub: State.Fpcr = 0x{State.Fpcr:X8}");
  597. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out uint Op1);
  598. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out uint Op2);
  599. float Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  600. if (!Done)
  601. {
  602. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  603. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  604. if (Inf1 && Inf2 && Sign1 == Sign2)
  605. {
  606. Result = FPDefaultNaN();
  607. FPProcessException(FPExc.InvalidOp, State);
  608. }
  609. else if ((Inf1 && !Sign1) || (Inf2 && Sign2))
  610. {
  611. Result = FPInfinity(false);
  612. }
  613. else if ((Inf1 && Sign1) || (Inf2 && !Sign2))
  614. {
  615. Result = FPInfinity(true);
  616. }
  617. else if (Zero1 && Zero2 && Sign1 == !Sign2)
  618. {
  619. Result = FPZero(Sign1);
  620. }
  621. else
  622. {
  623. Result = Value1 - Value2;
  624. }
  625. }
  626. return Result;
  627. }
  628. private enum FPType
  629. {
  630. Nonzero,
  631. Zero,
  632. Infinity,
  633. QNaN,
  634. SNaN
  635. }
  636. private enum FPExc
  637. {
  638. InvalidOp,
  639. DivideByZero,
  640. Overflow,
  641. Underflow,
  642. Inexact,
  643. InputDenorm = 7
  644. }
  645. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  646. private static float FPDefaultNaN()
  647. {
  648. return -float.NaN;
  649. }
  650. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  651. private static float FPInfinity(bool Sign)
  652. {
  653. return Sign ? float.NegativeInfinity : float.PositiveInfinity;
  654. }
  655. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  656. private static float FPZero(bool Sign)
  657. {
  658. return Sign ? -0f : +0f;
  659. }
  660. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  661. private static float FPTwo(bool Sign)
  662. {
  663. return Sign ? -2f : +2f;
  664. }
  665. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  666. private static float FPOnePointFive(bool Sign)
  667. {
  668. return Sign ? -1.5f : +1.5f;
  669. }
  670. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  671. private static float FPNeg(this float Value)
  672. {
  673. return -Value;
  674. }
  675. private static float FPUnpack(this float Value, out FPType Type, out bool Sign, out uint ValueBits)
  676. {
  677. ValueBits = (uint)BitConverter.SingleToInt32Bits(Value);
  678. Sign = (~ValueBits & 0x80000000u) == 0u;
  679. if ((ValueBits & 0x7F800000u) == 0u)
  680. {
  681. if ((ValueBits & 0x007FFFFFu) == 0u)
  682. {
  683. Type = FPType.Zero;
  684. }
  685. else
  686. {
  687. Type = FPType.Nonzero;
  688. }
  689. }
  690. else if ((~ValueBits & 0x7F800000u) == 0u)
  691. {
  692. if ((ValueBits & 0x007FFFFFu) == 0u)
  693. {
  694. Type = FPType.Infinity;
  695. }
  696. else
  697. {
  698. Type = (~ValueBits & 0x00400000u) == 0u
  699. ? FPType.QNaN
  700. : FPType.SNaN;
  701. return FPZero(Sign);
  702. }
  703. }
  704. else
  705. {
  706. Type = FPType.Nonzero;
  707. }
  708. return Value;
  709. }
  710. private static float FPProcessNaNs(
  711. FPType Type1,
  712. FPType Type2,
  713. uint Op1,
  714. uint Op2,
  715. AThreadState State,
  716. out bool Done)
  717. {
  718. Done = true;
  719. if (Type1 == FPType.SNaN)
  720. {
  721. return FPProcessNaN(Type1, Op1, State);
  722. }
  723. else if (Type2 == FPType.SNaN)
  724. {
  725. return FPProcessNaN(Type2, Op2, State);
  726. }
  727. else if (Type1 == FPType.QNaN)
  728. {
  729. return FPProcessNaN(Type1, Op1, State);
  730. }
  731. else if (Type2 == FPType.QNaN)
  732. {
  733. return FPProcessNaN(Type2, Op2, State);
  734. }
  735. Done = false;
  736. return FPZero(false);
  737. }
  738. private static float FPProcessNaNs3(
  739. FPType Type1,
  740. FPType Type2,
  741. FPType Type3,
  742. uint Op1,
  743. uint Op2,
  744. uint Op3,
  745. AThreadState State,
  746. out bool Done)
  747. {
  748. Done = true;
  749. if (Type1 == FPType.SNaN)
  750. {
  751. return FPProcessNaN(Type1, Op1, State);
  752. }
  753. else if (Type2 == FPType.SNaN)
  754. {
  755. return FPProcessNaN(Type2, Op2, State);
  756. }
  757. else if (Type3 == FPType.SNaN)
  758. {
  759. return FPProcessNaN(Type3, Op3, State);
  760. }
  761. else if (Type1 == FPType.QNaN)
  762. {
  763. return FPProcessNaN(Type1, Op1, State);
  764. }
  765. else if (Type2 == FPType.QNaN)
  766. {
  767. return FPProcessNaN(Type2, Op2, State);
  768. }
  769. else if (Type3 == FPType.QNaN)
  770. {
  771. return FPProcessNaN(Type3, Op3, State);
  772. }
  773. Done = false;
  774. return FPZero(false);
  775. }
  776. private static float FPProcessNaN(FPType Type, uint Op, AThreadState State)
  777. {
  778. const int DNBit = 25; // Default NaN mode control bit.
  779. if (Type == FPType.SNaN)
  780. {
  781. Op |= 1u << 22;
  782. FPProcessException(FPExc.InvalidOp, State);
  783. }
  784. if ((State.Fpcr & (1 << DNBit)) != 0)
  785. {
  786. return FPDefaultNaN();
  787. }
  788. return BitConverter.Int32BitsToSingle((int)Op);
  789. }
  790. private static void FPProcessException(FPExc Exc, AThreadState State)
  791. {
  792. int Enable = (int)Exc + 8;
  793. if ((State.Fpcr & (1 << Enable)) != 0)
  794. {
  795. throw new NotImplementedException("floating-point trap handling");
  796. }
  797. else
  798. {
  799. State.Fpsr |= 1 << (int)Exc;
  800. }
  801. }
  802. }
  803. static class ASoftFloat_64
  804. {
  805. public static double FPAdd(double Value1, double Value2, AThreadState State)
  806. {
  807. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  808. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  809. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  810. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  811. if (!Done)
  812. {
  813. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  814. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  815. if (Inf1 && Inf2 && Sign1 == !Sign2)
  816. {
  817. Result = FPDefaultNaN();
  818. FPProcessException(FPExc.InvalidOp, State);
  819. }
  820. else if ((Inf1 && !Sign1) || (Inf2 && !Sign2))
  821. {
  822. Result = FPInfinity(false);
  823. }
  824. else if ((Inf1 && Sign1) || (Inf2 && Sign2))
  825. {
  826. Result = FPInfinity(true);
  827. }
  828. else if (Zero1 && Zero2 && Sign1 == Sign2)
  829. {
  830. Result = FPZero(Sign1);
  831. }
  832. else
  833. {
  834. Result = Value1 + Value2;
  835. }
  836. }
  837. return Result;
  838. }
  839. public static double FPDiv(double Value1, double Value2, AThreadState State)
  840. {
  841. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPDiv: State.Fpcr = 0x{State.Fpcr:X8}");
  842. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  843. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  844. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  845. if (!Done)
  846. {
  847. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  848. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  849. if ((Inf1 && Inf2) || (Zero1 && Zero2))
  850. {
  851. Result = FPDefaultNaN();
  852. FPProcessException(FPExc.InvalidOp, State);
  853. }
  854. else if (Inf1 || Zero2)
  855. {
  856. Result = FPInfinity(Sign1 ^ Sign2);
  857. if (!Inf1) FPProcessException(FPExc.DivideByZero, State);
  858. }
  859. else if (Zero1 || Inf2)
  860. {
  861. Result = FPZero(Sign1 ^ Sign2);
  862. }
  863. else
  864. {
  865. Result = Value1 / Value2;
  866. }
  867. }
  868. return Result;
  869. }
  870. public static double FPMax(double Value1, double Value2, AThreadState State)
  871. {
  872. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMax: State.Fpcr = 0x{State.Fpcr:X8}");
  873. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  874. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  875. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  876. if (!Done)
  877. {
  878. if (Value1 > Value2)
  879. {
  880. if (Type1 == FPType.Infinity)
  881. {
  882. Result = FPInfinity(Sign1);
  883. }
  884. else if (Type1 == FPType.Zero)
  885. {
  886. Result = FPZero(Sign1 && Sign2);
  887. }
  888. else
  889. {
  890. Result = Value1;
  891. }
  892. }
  893. else
  894. {
  895. if (Type2 == FPType.Infinity)
  896. {
  897. Result = FPInfinity(Sign2);
  898. }
  899. else if (Type2 == FPType.Zero)
  900. {
  901. Result = FPZero(Sign1 && Sign2);
  902. }
  903. else
  904. {
  905. Result = Value2;
  906. }
  907. }
  908. }
  909. return Result;
  910. }
  911. public static double FPMaxNum(double Value1, double Value2, AThreadState State)
  912. {
  913. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_64.FPMaxNum: ");
  914. Value1.FPUnpack(out FPType Type1, out _, out _);
  915. Value2.FPUnpack(out FPType Type2, out _, out _);
  916. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  917. {
  918. Value1 = FPInfinity(true);
  919. }
  920. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  921. {
  922. Value2 = FPInfinity(true);
  923. }
  924. return FPMax(Value1, Value2, State);
  925. }
  926. public static double FPMin(double Value1, double Value2, AThreadState State)
  927. {
  928. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMin: State.Fpcr = 0x{State.Fpcr:X8}");
  929. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  930. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  931. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  932. if (!Done)
  933. {
  934. if (Value1 < Value2)
  935. {
  936. if (Type1 == FPType.Infinity)
  937. {
  938. Result = FPInfinity(Sign1);
  939. }
  940. else if (Type1 == FPType.Zero)
  941. {
  942. Result = FPZero(Sign1 || Sign2);
  943. }
  944. else
  945. {
  946. Result = Value1;
  947. }
  948. }
  949. else
  950. {
  951. if (Type2 == FPType.Infinity)
  952. {
  953. Result = FPInfinity(Sign2);
  954. }
  955. else if (Type2 == FPType.Zero)
  956. {
  957. Result = FPZero(Sign1 || Sign2);
  958. }
  959. else
  960. {
  961. Result = Value2;
  962. }
  963. }
  964. }
  965. return Result;
  966. }
  967. public static double FPMinNum(double Value1, double Value2, AThreadState State)
  968. {
  969. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_64.FPMinNum: ");
  970. Value1.FPUnpack(out FPType Type1, out _, out _);
  971. Value2.FPUnpack(out FPType Type2, out _, out _);
  972. if (Type1 == FPType.QNaN && Type2 != FPType.QNaN)
  973. {
  974. Value1 = FPInfinity(false);
  975. }
  976. else if (Type1 != FPType.QNaN && Type2 == FPType.QNaN)
  977. {
  978. Value2 = FPInfinity(false);
  979. }
  980. return FPMin(Value1, Value2, State);
  981. }
  982. public static double FPMul(double Value1, double Value2, AThreadState State)
  983. {
  984. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMul: State.Fpcr = 0x{State.Fpcr:X8}");
  985. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  986. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  987. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  988. if (!Done)
  989. {
  990. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  991. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  992. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  993. {
  994. Result = FPDefaultNaN();
  995. FPProcessException(FPExc.InvalidOp, State);
  996. }
  997. else if (Inf1 || Inf2)
  998. {
  999. Result = FPInfinity(Sign1 ^ Sign2);
  1000. }
  1001. else if (Zero1 || Zero2)
  1002. {
  1003. Result = FPZero(Sign1 ^ Sign2);
  1004. }
  1005. else
  1006. {
  1007. Result = Value1 * Value2;
  1008. }
  1009. }
  1010. return Result;
  1011. }
  1012. public static double FPMulAdd(double ValueA, double Value1, double Value2, AThreadState State)
  1013. {
  1014. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMulAdd: State.Fpcr = 0x{State.Fpcr:X8}");
  1015. ValueA = ValueA.FPUnpack(out FPType TypeA, out bool SignA, out ulong Addend);
  1016. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1017. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1018. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1019. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1020. double Result = FPProcessNaNs3(TypeA, Type1, Type2, Addend, Op1, Op2, State, out bool Done);
  1021. if (TypeA == FPType.QNaN && ((Inf1 && Zero2) || (Zero1 && Inf2)))
  1022. {
  1023. Result = FPDefaultNaN();
  1024. FPProcessException(FPExc.InvalidOp, State);
  1025. }
  1026. if (!Done)
  1027. {
  1028. bool InfA = TypeA == FPType.Infinity; bool ZeroA = TypeA == FPType.Zero;
  1029. bool SignP = Sign1 ^ Sign2;
  1030. bool InfP = Inf1 || Inf2;
  1031. bool ZeroP = Zero1 || Zero2;
  1032. if ((Inf1 && Zero2) || (Zero1 && Inf2) || (InfA && InfP && SignA != SignP))
  1033. {
  1034. Result = FPDefaultNaN();
  1035. FPProcessException(FPExc.InvalidOp, State);
  1036. }
  1037. else if ((InfA && !SignA) || (InfP && !SignP))
  1038. {
  1039. Result = FPInfinity(false);
  1040. }
  1041. else if ((InfA && SignA) || (InfP && SignP))
  1042. {
  1043. Result = FPInfinity(true);
  1044. }
  1045. else if (ZeroA && ZeroP && SignA == SignP)
  1046. {
  1047. Result = FPZero(SignA);
  1048. }
  1049. else
  1050. {
  1051. // TODO: When available, use: T Math.FusedMultiplyAdd(T, T, T);
  1052. // https://github.com/dotnet/corefx/issues/31903
  1053. Result = ValueA + (Value1 * Value2);
  1054. }
  1055. }
  1056. return Result;
  1057. }
  1058. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1059. public static double FPMulSub(double ValueA, double Value1, double Value2, AThreadState State)
  1060. {
  1061. Debug.WriteIf(State.Fpcr != 0, "ASoftFloat_64.FPMulSub: ");
  1062. Value1 = Value1.FPNeg();
  1063. return FPMulAdd(ValueA, Value1, Value2, State);
  1064. }
  1065. public static double FPMulX(double Value1, double Value2, AThreadState State)
  1066. {
  1067. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPMulX: State.Fpcr = 0x{State.Fpcr:X8}");
  1068. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1069. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1070. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1071. if (!Done)
  1072. {
  1073. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1074. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1075. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1076. {
  1077. Result = FPTwo(Sign1 ^ Sign2);
  1078. }
  1079. else if (Inf1 || Inf2)
  1080. {
  1081. Result = FPInfinity(Sign1 ^ Sign2);
  1082. }
  1083. else if (Zero1 || Zero2)
  1084. {
  1085. Result = FPZero(Sign1 ^ Sign2);
  1086. }
  1087. else
  1088. {
  1089. Result = Value1 * Value2;
  1090. }
  1091. }
  1092. return Result;
  1093. }
  1094. public static double FPRecipStepFused(double Value1, double Value2, AThreadState State)
  1095. {
  1096. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPRecipStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  1097. Value1 = Value1.FPNeg();
  1098. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1099. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1100. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1101. if (!Done)
  1102. {
  1103. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1104. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1105. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1106. {
  1107. Result = FPTwo(false);
  1108. }
  1109. else if (Inf1 || Inf2)
  1110. {
  1111. Result = FPInfinity(Sign1 ^ Sign2);
  1112. }
  1113. else
  1114. {
  1115. // TODO: When available, use: T Math.FusedMultiplyAdd(T, T, T);
  1116. // https://github.com/dotnet/corefx/issues/31903
  1117. Result = 2d + (Value1 * Value2);
  1118. }
  1119. }
  1120. return Result;
  1121. }
  1122. public static double FPRecpX(double Value, AThreadState State)
  1123. {
  1124. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPRecpX: State.Fpcr = 0x{State.Fpcr:X8}");
  1125. Value.FPUnpack(out FPType Type, out bool Sign, out ulong Op);
  1126. double Result;
  1127. if (Type == FPType.SNaN || Type == FPType.QNaN)
  1128. {
  1129. Result = FPProcessNaN(Type, Op, State);
  1130. }
  1131. else
  1132. {
  1133. ulong NotExp = (~Op >> 52) & 0x7FFul;
  1134. ulong MaxExp = 0x7FEul;
  1135. Result = BitConverter.Int64BitsToDouble(
  1136. (long)((Sign ? 1ul : 0ul) << 63 | (NotExp == 0x7FFul ? MaxExp : NotExp) << 52));
  1137. }
  1138. return Result;
  1139. }
  1140. public static double FPRSqrtStepFused(double Value1, double Value2, AThreadState State)
  1141. {
  1142. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPRSqrtStepFused: State.Fpcr = 0x{State.Fpcr:X8}");
  1143. Value1 = Value1.FPNeg();
  1144. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1145. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1146. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1147. if (!Done)
  1148. {
  1149. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1150. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1151. if ((Inf1 && Zero2) || (Zero1 && Inf2))
  1152. {
  1153. Result = FPOnePointFive(false);
  1154. }
  1155. else if (Inf1 || Inf2)
  1156. {
  1157. Result = FPInfinity(Sign1 ^ Sign2);
  1158. }
  1159. else
  1160. {
  1161. // TODO: When available, use: T Math.FusedMultiplyAdd(T, T, T);
  1162. // https://github.com/dotnet/corefx/issues/31903
  1163. Result = (3d + (Value1 * Value2)) / 2d;
  1164. }
  1165. }
  1166. return Result;
  1167. }
  1168. public static double FPSqrt(double Value, AThreadState State)
  1169. {
  1170. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPSqrt: State.Fpcr = 0x{State.Fpcr:X8}");
  1171. Value = Value.FPUnpack(out FPType Type, out bool Sign, out ulong Op);
  1172. double Result;
  1173. if (Type == FPType.SNaN || Type == FPType.QNaN)
  1174. {
  1175. Result = FPProcessNaN(Type, Op, State);
  1176. }
  1177. else if (Type == FPType.Zero)
  1178. {
  1179. Result = FPZero(Sign);
  1180. }
  1181. else if (Type == FPType.Infinity && !Sign)
  1182. {
  1183. Result = FPInfinity(Sign);
  1184. }
  1185. else if (Sign)
  1186. {
  1187. Result = FPDefaultNaN();
  1188. FPProcessException(FPExc.InvalidOp, State);
  1189. }
  1190. else
  1191. {
  1192. Result = Math.Sqrt(Value);
  1193. }
  1194. return Result;
  1195. }
  1196. public static double FPSub(double Value1, double Value2, AThreadState State)
  1197. {
  1198. Debug.WriteLineIf(State.Fpcr != 0, $"ASoftFloat_64.FPSub: State.Fpcr = 0x{State.Fpcr:X8}");
  1199. Value1 = Value1.FPUnpack(out FPType Type1, out bool Sign1, out ulong Op1);
  1200. Value2 = Value2.FPUnpack(out FPType Type2, out bool Sign2, out ulong Op2);
  1201. double Result = FPProcessNaNs(Type1, Type2, Op1, Op2, State, out bool Done);
  1202. if (!Done)
  1203. {
  1204. bool Inf1 = Type1 == FPType.Infinity; bool Zero1 = Type1 == FPType.Zero;
  1205. bool Inf2 = Type2 == FPType.Infinity; bool Zero2 = Type2 == FPType.Zero;
  1206. if (Inf1 && Inf2 && Sign1 == Sign2)
  1207. {
  1208. Result = FPDefaultNaN();
  1209. FPProcessException(FPExc.InvalidOp, State);
  1210. }
  1211. else if ((Inf1 && !Sign1) || (Inf2 && Sign2))
  1212. {
  1213. Result = FPInfinity(false);
  1214. }
  1215. else if ((Inf1 && Sign1) || (Inf2 && !Sign2))
  1216. {
  1217. Result = FPInfinity(true);
  1218. }
  1219. else if (Zero1 && Zero2 && Sign1 == !Sign2)
  1220. {
  1221. Result = FPZero(Sign1);
  1222. }
  1223. else
  1224. {
  1225. Result = Value1 - Value2;
  1226. }
  1227. }
  1228. return Result;
  1229. }
  1230. private enum FPType
  1231. {
  1232. Nonzero,
  1233. Zero,
  1234. Infinity,
  1235. QNaN,
  1236. SNaN
  1237. }
  1238. private enum FPExc
  1239. {
  1240. InvalidOp,
  1241. DivideByZero,
  1242. Overflow,
  1243. Underflow,
  1244. Inexact,
  1245. InputDenorm = 7
  1246. }
  1247. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1248. private static double FPDefaultNaN()
  1249. {
  1250. return -double.NaN;
  1251. }
  1252. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1253. private static double FPInfinity(bool Sign)
  1254. {
  1255. return Sign ? double.NegativeInfinity : double.PositiveInfinity;
  1256. }
  1257. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1258. private static double FPZero(bool Sign)
  1259. {
  1260. return Sign ? -0d : +0d;
  1261. }
  1262. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1263. private static double FPTwo(bool Sign)
  1264. {
  1265. return Sign ? -2d : +2d;
  1266. }
  1267. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1268. private static double FPOnePointFive(bool Sign)
  1269. {
  1270. return Sign ? -1.5d : +1.5d;
  1271. }
  1272. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  1273. private static double FPNeg(this double Value)
  1274. {
  1275. return -Value;
  1276. }
  1277. private static double FPUnpack(this double Value, out FPType Type, out bool Sign, out ulong ValueBits)
  1278. {
  1279. ValueBits = (ulong)BitConverter.DoubleToInt64Bits(Value);
  1280. Sign = (~ValueBits & 0x8000000000000000ul) == 0ul;
  1281. if ((ValueBits & 0x7FF0000000000000ul) == 0ul)
  1282. {
  1283. if ((ValueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  1284. {
  1285. Type = FPType.Zero;
  1286. }
  1287. else
  1288. {
  1289. Type = FPType.Nonzero;
  1290. }
  1291. }
  1292. else if ((~ValueBits & 0x7FF0000000000000ul) == 0ul)
  1293. {
  1294. if ((ValueBits & 0x000FFFFFFFFFFFFFul) == 0ul)
  1295. {
  1296. Type = FPType.Infinity;
  1297. }
  1298. else
  1299. {
  1300. Type = (~ValueBits & 0x0008000000000000ul) == 0ul
  1301. ? FPType.QNaN
  1302. : FPType.SNaN;
  1303. return FPZero(Sign);
  1304. }
  1305. }
  1306. else
  1307. {
  1308. Type = FPType.Nonzero;
  1309. }
  1310. return Value;
  1311. }
  1312. private static double FPProcessNaNs(
  1313. FPType Type1,
  1314. FPType Type2,
  1315. ulong Op1,
  1316. ulong Op2,
  1317. AThreadState State,
  1318. out bool Done)
  1319. {
  1320. Done = true;
  1321. if (Type1 == FPType.SNaN)
  1322. {
  1323. return FPProcessNaN(Type1, Op1, State);
  1324. }
  1325. else if (Type2 == FPType.SNaN)
  1326. {
  1327. return FPProcessNaN(Type2, Op2, State);
  1328. }
  1329. else if (Type1 == FPType.QNaN)
  1330. {
  1331. return FPProcessNaN(Type1, Op1, State);
  1332. }
  1333. else if (Type2 == FPType.QNaN)
  1334. {
  1335. return FPProcessNaN(Type2, Op2, State);
  1336. }
  1337. Done = false;
  1338. return FPZero(false);
  1339. }
  1340. private static double FPProcessNaNs3(
  1341. FPType Type1,
  1342. FPType Type2,
  1343. FPType Type3,
  1344. ulong Op1,
  1345. ulong Op2,
  1346. ulong Op3,
  1347. AThreadState State,
  1348. out bool Done)
  1349. {
  1350. Done = true;
  1351. if (Type1 == FPType.SNaN)
  1352. {
  1353. return FPProcessNaN(Type1, Op1, State);
  1354. }
  1355. else if (Type2 == FPType.SNaN)
  1356. {
  1357. return FPProcessNaN(Type2, Op2, State);
  1358. }
  1359. else if (Type3 == FPType.SNaN)
  1360. {
  1361. return FPProcessNaN(Type3, Op3, State);
  1362. }
  1363. else if (Type1 == FPType.QNaN)
  1364. {
  1365. return FPProcessNaN(Type1, Op1, State);
  1366. }
  1367. else if (Type2 == FPType.QNaN)
  1368. {
  1369. return FPProcessNaN(Type2, Op2, State);
  1370. }
  1371. else if (Type3 == FPType.QNaN)
  1372. {
  1373. return FPProcessNaN(Type3, Op3, State);
  1374. }
  1375. Done = false;
  1376. return FPZero(false);
  1377. }
  1378. private static double FPProcessNaN(FPType Type, ulong Op, AThreadState State)
  1379. {
  1380. const int DNBit = 25; // Default NaN mode control bit.
  1381. if (Type == FPType.SNaN)
  1382. {
  1383. Op |= 1ul << 51;
  1384. FPProcessException(FPExc.InvalidOp, State);
  1385. }
  1386. if ((State.Fpcr & (1 << DNBit)) != 0)
  1387. {
  1388. return FPDefaultNaN();
  1389. }
  1390. return BitConverter.Int64BitsToDouble((long)Op);
  1391. }
  1392. private static void FPProcessException(FPExc Exc, AThreadState State)
  1393. {
  1394. int Enable = (int)Exc + 8;
  1395. if ((State.Fpcr & (1 << Enable)) != 0)
  1396. {
  1397. throw new NotImplementedException("floating-point trap handling");
  1398. }
  1399. else
  1400. {
  1401. State.Fpsr |= 1 << (int)Exc;
  1402. }
  1403. }
  1404. }
  1405. }