bid_dpd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* Copyright (C) 2007-2022 Free Software Foundation, Inc.
  2. This file is part of GCC.
  3. GCC is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 3, or (at your option) any later
  6. version.
  7. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. for more details.
  11. Under Section 7 of GPL version 3, you are granted additional
  12. permissions described in the GCC Runtime Library Exception, version
  13. 3.1, as published by the Free Software Foundation.
  14. You should have received a copy of the GNU General Public License and
  15. a copy of the GCC Runtime Library Exception along with this program;
  16. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #define DECNUMDIGITS 34 // work with up to 34 digits
  19. #include "bid_internal.h"
  20. #include "bid_b2d.h"
  21. UINT32
  22. bid_to_bid32 (UINT32 ba) {
  23. UINT32 res;
  24. UINT32 sign, comb, exp;
  25. UINT32 trailing;
  26. UINT32 bcoeff;
  27. sign = (ba & 0x80000000ul);
  28. comb = (ba & 0x7ff00000ul) >> 20;
  29. trailing = (ba & 0x000ffffful);
  30. if ((comb & 0x780) == 0x780) {
  31. ba &= 0xfff0000ul;
  32. return ba;
  33. } else {
  34. if ((comb & 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
  35. exp = (comb >> 1) & 0xff;
  36. bcoeff = ((8 + (comb & 1)) << 20) | trailing;
  37. } else {
  38. exp = (comb >> 3) & 0xff;
  39. bcoeff = ((comb & 7) << 20) | trailing;
  40. }
  41. if (bcoeff >= 10000000)
  42. bcoeff = 0;
  43. res = very_fast_get_BID32 (sign, exp, bcoeff);
  44. }
  45. return res;
  46. }
  47. UINT64
  48. bid_to_bid64 (UINT64 ba) {
  49. UINT64 res;
  50. UINT64 sign, comb, exp;
  51. UINT64 trailing;
  52. UINT64 bcoeff;
  53. sign = (ba & 0x8000000000000000ull);
  54. comb = (ba & 0x7ffc000000000000ull) >> 50;
  55. trailing = (ba & 0x0003ffffffffffffull);
  56. if ((comb & 0x1e00) == 0x1e00) {
  57. ba &= 0xfff000000000000ULL;
  58. return ba;
  59. } else {
  60. if ((comb & 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
  61. exp = (comb >> 1) & 0x3ff;
  62. bcoeff = ((8 + (comb & 1)) << 50) | trailing;
  63. } else {
  64. exp = (comb >> 3) & 0x3ff;
  65. bcoeff = ((comb & 7) << 50) | trailing;
  66. }
  67. if (bcoeff >= 10000000000000000ull)
  68. bcoeff = 0ull;
  69. res = very_fast_get_BID64 (sign, exp, bcoeff);
  70. }
  71. return res;
  72. }
  73. #if DECIMAL_CALL_BY_REFERENCE
  74. void
  75. bid_to_dpd32 (UINT32 * pres, UINT32 * pba) {
  76. UINT32 ba = *pba;
  77. #else
  78. UINT32
  79. bid_to_dpd32 (UINT32 ba) {
  80. #endif
  81. UINT32 res;
  82. UINT32 sign, comb, exp, trailing;
  83. UINT32 b0, b1, b2;
  84. UINT32 bcoeff, dcoeff;
  85. UINT32 nanb = 0;
  86. sign = (ba & 0x80000000);
  87. comb = (ba & 0x7ff00000) >> 20;
  88. trailing = (ba & 0xfffff);
  89. // Detect infinity, and return canonical infinity
  90. if ((comb & 0x7c0) == 0x780) {
  91. res = sign | 0x78000000;
  92. BID_RETURN (res);
  93. // Detect NaN, and canonicalize trailing
  94. } else if ((comb & 0x7c0) == 0x7c0) {
  95. if (trailing > 999999)
  96. trailing = 0;
  97. nanb = ba & 0xfe000000;
  98. exp = 0;
  99. bcoeff = trailing;
  100. } else { // Normal number
  101. if ((comb & 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
  102. exp = (comb >> 1) & 0xff;
  103. bcoeff = ((8 + (comb & 1)) << 20) | trailing;
  104. } else {
  105. exp = (comb >> 3) & 0xff;
  106. bcoeff = ((comb & 7) << 20) | trailing;
  107. }
  108. // Zero the coefficient if non-canonical (>= 10^7)
  109. if (bcoeff >= 10000000)
  110. bcoeff = 0;
  111. }
  112. b0 = bcoeff / 1000000;
  113. b1 = (bcoeff / 1000) % 1000;
  114. b2 = bcoeff % 1000;
  115. dcoeff = (b2d[b1] << 10) | b2d[b2];
  116. if (b0 >= 8) // is b0 8 or 9?
  117. res =
  118. sign |
  119. ((0x600 | ((exp >> 6) << 7) | ((b0 & 1) << 6) | (exp & 0x3f)) <<
  120. 20) | dcoeff;
  121. else // else b0 is 0..7
  122. res =
  123. sign | ((((exp >> 6) << 9) | (b0 << 6) | (exp & 0x3f)) << 20) |
  124. dcoeff;
  125. res |= nanb;
  126. BID_RETURN (res);
  127. }
  128. #if DECIMAL_CALL_BY_REFERENCE
  129. void
  130. bid_to_dpd64 (UINT64 * pres, UINT64 * pba) {
  131. UINT64 ba = *pba;
  132. #else
  133. UINT64
  134. bid_to_dpd64 (UINT64 ba) {
  135. #endif
  136. UINT64 res;
  137. UINT64 sign, comb, exp;
  138. UINT64 trailing;
  139. UINT32 b0, b1, b2, b3, b4, b5;
  140. UINT64 bcoeff;
  141. UINT64 dcoeff;
  142. UINT32 yhi, ylo;
  143. UINT64 nanb = 0;
  144. //printf("arg bid "FMT_LLX16" \n", ba);
  145. sign = (ba & 0x8000000000000000ull);
  146. comb = (ba & 0x7ffc000000000000ull) >> 50;
  147. trailing = (ba & 0x0003ffffffffffffull);
  148. // Detect infinity, and return canonical infinity
  149. if ((comb & 0x1f00) == 0x1e00) {
  150. res = sign | 0x7800000000000000ull;
  151. BID_RETURN (res);
  152. // Detect NaN, and canonicalize trailing
  153. } else if ((comb & 0x1e00) == 0x1e00) {
  154. if (trailing > 999999999999999ull)
  155. trailing = 0;
  156. nanb = ba & 0xfe00000000000000ull;
  157. exp = 0;
  158. bcoeff = trailing;
  159. } else { // Normal number
  160. if ((comb & 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
  161. exp = (comb >> 1) & 0x3ff;
  162. bcoeff = ((8 + (comb & 1)) << 50) | trailing;
  163. } else {
  164. exp = (comb >> 3) & 0x3ff;
  165. bcoeff = ((comb & 7) << 50) | trailing;
  166. }
  167. // Zero the coefficient if it is non-canonical (>= 10^16)
  168. if (bcoeff >= 10000000000000000ull)
  169. bcoeff = 0;
  170. }
  171. // Floor(2^61 / 10^9)
  172. #define D61 (2305843009ull)
  173. // Multipy the binary coefficient by ceil(2^64 / 1000), and take the upper
  174. // 64-bits in order to compute a division by 1000.
  175. #if 1
  176. yhi =
  177. ((UINT64) D61 *
  178. (UINT64) (UINT32) (bcoeff >> (UINT64) 27)) >> (UINT64) 34;
  179. ylo = bcoeff - 1000000000ull * yhi;
  180. if (ylo >= 1000000000) {
  181. ylo = ylo - 1000000000;
  182. yhi = yhi + 1;
  183. }
  184. #else
  185. yhi = bcoeff / 1000000000ull;
  186. ylo = bcoeff % 1000000000ull;
  187. #endif
  188. // yhi = ABBBCCC ylo = DDDEEEFFF
  189. b5 = ylo % 1000; // b5 = FFF
  190. b3 = ylo / 1000000; // b3 = DDD
  191. b4 = (ylo / 1000) - (1000 * b3); // b4 = EEE
  192. b2 = yhi % 1000; // b2 = CCC
  193. b0 = yhi / 1000000; // b0 = A
  194. b1 = (yhi / 1000) - (1000 * b0); // b1 = BBB
  195. dcoeff = b2d[b5] | b2d2[b4] | b2d3[b3] | b2d4[b2] | b2d5[b1];
  196. if (b0 >= 8) // is b0 8 or 9?
  197. res =
  198. sign |
  199. ((0x1800 | ((exp >> 8) << 9) | ((b0 & 1) << 8) | (exp & 0xff)) <<
  200. 50) | dcoeff;
  201. else // else b0 is 0..7
  202. res =
  203. sign | ((((exp >> 8) << 11) | (b0 << 8) | (exp & 0xff)) << 50) |
  204. dcoeff;
  205. res |= nanb;
  206. BID_RETURN (res);
  207. }
  208. #if DECIMAL_CALL_BY_REFERENCE
  209. void
  210. dpd_to_bid32 (UINT32 * pres, UINT32 * pda) {
  211. UINT32 da = *pda;
  212. #else
  213. UINT32
  214. dpd_to_bid32 (UINT32 da) {
  215. #endif
  216. UINT32 in = *(UINT32 *) & da;
  217. UINT32 res;
  218. UINT32 sign, comb, exp;
  219. UINT32 trailing;
  220. UINT32 d0 = 0, d1, d2;
  221. UINT64 bcoeff;
  222. UINT32 nanb = 0;
  223. sign = (in & 0x80000000);
  224. comb = (in & 0x7ff00000) >> 20;
  225. trailing = (in & 0x000fffff);
  226. if ((comb & 0x7e0) == 0x780) { // G0..G4 = 1111 -> Inf
  227. res = in & 0xf8000000;
  228. BID_RETURN (res);
  229. } else if ((comb & 0x7c0) == 0x7c0) { // G0..G5 = 11111 -> NaN
  230. nanb = in & 0xfe000000;
  231. exp = 0;
  232. } else { // Normal number
  233. if ((comb & 0x600) == 0x600) { // G0..G1 = 11 -> d0 = 8 + G4
  234. d0 = ((comb >> 6) & 1) | 8;
  235. exp = ((comb & 0x180) >> 1) | (comb & 0x3f);
  236. } else {
  237. d0 = (comb >> 6) & 0x7;
  238. exp = ((comb & 0x600) >> 3) | (comb & 0x3f);
  239. }
  240. }
  241. d1 = d2b2[(trailing >> 10) & 0x3ff];
  242. d2 = d2b[(trailing) & 0x3ff];
  243. bcoeff = d2 + d1 + (1000000 * d0);
  244. if (bcoeff < 0x800000) {
  245. res = (exp << 23) | bcoeff | sign;
  246. } else {
  247. res = (exp << 21) | sign | 0x60000000 | (bcoeff & 0x1fffff);
  248. }
  249. res |= nanb;
  250. BID_RETURN (res);
  251. }
  252. #if DECIMAL_CALL_BY_REFERENCE
  253. void
  254. dpd_to_bid64 (UINT64 * pres, UINT64 * pda) {
  255. UINT64 da = *pda;
  256. #else
  257. UINT64
  258. dpd_to_bid64 (UINT64 da) {
  259. #endif
  260. UINT64 in = *(UINT64 *) & da;
  261. UINT64 res;
  262. UINT64 sign, comb, exp;
  263. UINT64 trailing;
  264. // UINT64 d0, d1, d2, d3, d4, d5;
  265. UINT64 d1, d2;
  266. UINT32 d0, d3, d4, d5;
  267. UINT64 bcoeff;
  268. UINT64 nanb = 0;
  269. //printf("arg dpd "FMT_LLX16" \n", in);
  270. sign = (in & 0x8000000000000000ull);
  271. comb = (in & 0x7ffc000000000000ull) >> 50;
  272. trailing = (in & 0x0003ffffffffffffull);
  273. if ((comb & 0x1f00) == 0x1e00) { // G0..G4 = 1111 -> Inf
  274. res = in & 0xf800000000000000ull;
  275. BID_RETURN (res);
  276. } else if ((comb & 0x1f00) == 0x1f00) { // G0..G5 = 11111 -> NaN
  277. nanb = in & 0xfe00000000000000ull;
  278. exp = 0;
  279. d0 = 0;
  280. } else { // Normal number
  281. if ((comb & 0x1800) == 0x1800) { // G0..G1 = 11 -> d0 = 8 + G4
  282. d0 = ((comb >> 8) & 1) | 8;
  283. // d0 = (comb & 0x0100 ? 9 : 8);
  284. exp = (comb & 0x600) >> 1;
  285. // exp = (comb & 0x0400 ? 1 : 0) * 0x200 + (comb & 0x0200 ? 1 : 0) * 0x100; // exp leading bits are G2..G3
  286. } else {
  287. d0 = (comb >> 8) & 0x7;
  288. exp = (comb & 0x1800) >> 3;
  289. // exp = (comb & 0x1000 ? 1 : 0) * 0x200 + (comb & 0x0800 ? 1 : 0) * 0x100; // exp loading bits are G0..G1
  290. }
  291. }
  292. d1 = d2b5[(trailing >> 40) & 0x3ff];
  293. d2 = d2b4[(trailing >> 30) & 0x3ff];
  294. d3 = d2b3[(trailing >> 20) & 0x3ff];
  295. d4 = d2b2[(trailing >> 10) & 0x3ff];
  296. d5 = d2b[(trailing) & 0x3ff];
  297. bcoeff = (d5 + d4 + d3) + d2 + d1 + (1000000000000000ull * d0);
  298. exp += (comb & 0xff);
  299. res = very_fast_get_BID64 (sign, exp, bcoeff);
  300. res |= nanb;
  301. BID_RETURN (res);
  302. }
  303. #if DECIMAL_CALL_BY_REFERENCE
  304. void
  305. bid_to_dpd128 (UINT128 * pres, UINT128 * pba) {
  306. UINT128 ba = *pba;
  307. #else
  308. UINT128
  309. bid_to_dpd128 (UINT128 ba) {
  310. #endif
  311. UINT128 res;
  312. UINT128 sign;
  313. UINT32 comb, exp;
  314. UINT128 trailing;
  315. UINT128 d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11;
  316. UINT128 bcoeff;
  317. UINT128 dcoeff;
  318. UINT64 nanb = 0;
  319. sign.w[1] = (ba.w[HIGH_128W] & 0x8000000000000000ull);
  320. sign.w[0] = 0;
  321. comb = (ba.w[HIGH_128W] & 0x7fffc00000000000ull) >> 46;
  322. trailing.w[1] = (ba.w[HIGH_128W] & 0x00003fffffffffffull);
  323. trailing.w[0] = ba.w[LOW_128W];
  324. exp = 0;
  325. if ((comb & 0x1f000) == 0x1e000) { // G0..G4 = 1111 -> Inf
  326. res.w[HIGH_128W] = ba.w[HIGH_128W] & 0xf800000000000000ull;
  327. res.w[LOW_128W] = 0;
  328. BID_RETURN (res);
  329. // Detect NaN, and canonicalize trailing
  330. } else if ((comb & 0x1f000) == 0x1f000) {
  331. if ((trailing.w[1] > 0x0000314dc6448d93ULL) || // significand is non-canonical
  332. ((trailing.w[1] == 0x0000314dc6448d93ULL)
  333. && (trailing.w[0] >= 0x38c15b0a00000000ULL))
  334. // significand is non-canonical
  335. ) {
  336. trailing.w[1] = trailing.w[0] = 0ull;
  337. }
  338. bcoeff.w[1] = trailing.w[1];
  339. bcoeff.w[0] = trailing.w[0];
  340. nanb = ba.w[HIGH_128W] & 0xfe00000000000000ull;
  341. exp = 0;
  342. } else { // Normal number
  343. if ((comb & 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
  344. exp = (comb >> 1) & 0x3fff;
  345. bcoeff.w[1] =
  346. ((UINT64) (8 + (comb & 1)) << (UINT64) 46) | trailing.w[1];
  347. bcoeff.w[0] = trailing.w[0];
  348. } else {
  349. exp = (comb >> 3) & 0x3fff;
  350. bcoeff.w[1] =
  351. ((UINT64) (comb & 7) << (UINT64) 46) | trailing.w[1];
  352. bcoeff.w[0] = trailing.w[0];
  353. }
  354. // Zero the coefficient if non-canonical (>= 10^34)
  355. if (bcoeff.w[1] > 0x1ed09bead87c0ull ||
  356. (bcoeff.w[1] == 0x1ed09bead87c0ull
  357. && bcoeff.w[0] >= 0x378D8E6400000000ull)) {
  358. bcoeff.w[0] = bcoeff.w[1] = 0;
  359. }
  360. }
  361. // Constant 2^128 / 1000 + 1
  362. {
  363. UINT128 t;
  364. UINT64 t2;
  365. UINT128 d1000;
  366. UINT128 b11, b10, b9, b8, b7, b6, b5, b4, b3, b2, b1;
  367. d1000.w[1] = 0x4189374BC6A7EFull;
  368. d1000.w[0] = 0x9DB22D0E56041894ull;
  369. __mul_128x128_high (b11, bcoeff, d1000);
  370. __mul_128x128_high (b10, b11, d1000);
  371. __mul_128x128_high (b9, b10, d1000);
  372. __mul_128x128_high (b8, b9, d1000);
  373. __mul_128x128_high (b7, b8, d1000);
  374. __mul_128x128_high (b6, b7, d1000);
  375. __mul_128x128_high (b5, b6, d1000);
  376. __mul_128x128_high (b4, b5, d1000);
  377. __mul_128x128_high (b3, b4, d1000);
  378. __mul_128x128_high (b2, b3, d1000);
  379. __mul_128x128_high (b1, b2, d1000);
  380. __mul_64x128_full (t2, t, 1000ull, b11);
  381. __sub_128_128 (d11, bcoeff, t);
  382. __mul_64x128_full (t2, t, 1000ull, b10);
  383. __sub_128_128 (d10, b11, t);
  384. __mul_64x128_full (t2, t, 1000ull, b9);
  385. __sub_128_128 (d9, b10, t);
  386. __mul_64x128_full (t2, t, 1000ull, b8);
  387. __sub_128_128 (d8, b9, t);
  388. __mul_64x128_full (t2, t, 1000ull, b7);
  389. __sub_128_128 (d7, b8, t);
  390. __mul_64x128_full (t2, t, 1000ull, b6);
  391. __sub_128_128 (d6, b7, t);
  392. __mul_64x128_full (t2, t, 1000ull, b5);
  393. __sub_128_128 (d5, b6, t);
  394. __mul_64x128_full (t2, t, 1000ull, b4);
  395. __sub_128_128 (d4, b5, t);
  396. __mul_64x128_full (t2, t, 1000ull, b3);
  397. __sub_128_128 (d3, b4, t);
  398. __mul_64x128_full (t2, t, 1000ull, b2);
  399. __sub_128_128 (d2, b3, t);
  400. __mul_64x128_full (t2, t, 1000ull, b1);
  401. __sub_128_128 (d1, b2, t);
  402. d0 = b1;
  403. }
  404. dcoeff.w[0] = b2d[d11.w[0]] | (b2d[d10.w[0]] << 10) |
  405. (b2d[d9.w[0]] << 20) | (b2d[d8.w[0]] << 30) | (b2d[d7.w[0]] << 40) |
  406. (b2d[d6.w[0]] << 50) | (b2d[d5.w[0]] << 60);
  407. dcoeff.w[1] =
  408. (b2d[d5.w[0]] >> 4) | (b2d[d4.w[0]] << 6) | (b2d[d3.w[0]] << 16) |
  409. (b2d[d2.w[0]] << 26) | (b2d[d1.w[0]] << 36);
  410. res.w[0] = dcoeff.w[0];
  411. if (d0.w[0] >= 8) {
  412. res.w[1] =
  413. sign.
  414. w[1] |
  415. ((0x18000 | ((exp >> 12) << 13) | ((d0.w[0] & 1) << 12) |
  416. (exp & 0xfff)) << 46) | dcoeff.w[1];
  417. } else {
  418. res.w[1] =
  419. sign.
  420. w[1] | ((((exp >> 12) << 15) | (d0.w[0] << 12) | (exp & 0xfff))
  421. << 46) | dcoeff.w[1];
  422. }
  423. res.w[1] |= nanb;
  424. BID_SWAP128 (res);
  425. BID_RETURN (res);
  426. }
  427. #if DECIMAL_CALL_BY_REFERENCE
  428. void
  429. dpd_to_bid128 (UINT128 * pres, UINT128 * pda) {
  430. UINT128 da = *pda;
  431. #else
  432. UINT128
  433. dpd_to_bid128 (UINT128 da) {
  434. #endif
  435. UINT128 in = *(UINT128 *) & da;
  436. UINT128 res;
  437. UINT128 sign;
  438. UINT64 exp, comb;
  439. UINT128 trailing;
  440. UINT64 d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11;
  441. UINT128 bcoeff;
  442. UINT64 tl, th;
  443. UINT64 nanb = 0;
  444. sign.w[1] = (in.w[HIGH_128W] & 0x8000000000000000ull);
  445. sign.w[0] = 0;
  446. comb = (in.w[HIGH_128W] & 0x7fffc00000000000ull) >> 46;
  447. trailing.w[1] = (in.w[HIGH_128W] & 0x00003fffffffffffull);
  448. trailing.w[0] = in.w[LOW_128W];
  449. exp = 0;
  450. if ((comb & 0x1f000) == 0x1e000) { // G0..G4 = 1111 -> Inf
  451. res.w[HIGH_128W] = in.w[HIGH_128W] & 0xf800000000000000ull;
  452. res.w[LOW_128W] = 0ull;
  453. BID_RETURN (res);
  454. } else if ((comb & 0x1f000) == 0x1f000) { // G0..G4 = 11111 -> NaN
  455. nanb = in.w[HIGH_128W] & 0xfe00000000000000ull;
  456. exp = 0;
  457. d0 = 0;
  458. } else { // Normal number
  459. if ((comb & 0x18000) == 0x18000) { // G0..G1 = 11 -> d0 = 8 + G4
  460. d0 = 8 + (comb & 0x01000 ? 1 : 0);
  461. exp =
  462. (comb & 0x04000 ? 1 : 0) * 0x2000 +
  463. (comb & 0x02000 ? 1 : 0) * 0x1000;
  464. // exp leading bits are G2..G3
  465. } else {
  466. d0 =
  467. 4 * (comb & 0x04000 ? 1 : 0) + 2 * (comb & 0x2000 ? 1 : 0) +
  468. (comb & 0x1000 ? 1 : 0);
  469. exp =
  470. (comb & 0x10000 ? 1 : 0) * 0x2000 +
  471. (comb & 0x08000 ? 1 : 0) * 0x1000;
  472. // exp loading bits are G0..G1
  473. }
  474. }
  475. d11 = d2b[(trailing.w[0]) & 0x3ff];
  476. d10 = d2b[(trailing.w[0] >> 10) & 0x3ff];
  477. d9 = d2b[(trailing.w[0] >> 20) & 0x3ff];
  478. d8 = d2b[(trailing.w[0] >> 30) & 0x3ff];
  479. d7 = d2b[(trailing.w[0] >> 40) & 0x3ff];
  480. d6 = d2b[(trailing.w[0] >> 50) & 0x3ff];
  481. d5 = d2b[(trailing.w[0] >> 60) | ((trailing.w[1] & 0x3f) << 4)];
  482. d4 = d2b[(trailing.w[1] >> 6) & 0x3ff];
  483. d3 = d2b[(trailing.w[1] >> 16) & 0x3ff];
  484. d2 = d2b[(trailing.w[1] >> 26) & 0x3ff];
  485. d1 = d2b[(trailing.w[1] >> 36) & 0x3ff];
  486. tl =
  487. d11 + (d10 * 1000ull) + (d9 * 1000000ull) + (d8 * 1000000000ull) +
  488. (d7 * 1000000000000ull) + (d6 * 1000000000000000ull);
  489. th =
  490. d5 + (d4 * 1000ull) + (d3 * 1000000ull) + (d2 * 1000000000ull) +
  491. (d1 * 1000000000000ull) + (d0 * 1000000000000000ull);
  492. __mul_64x64_to_128 (bcoeff, th, 1000000000000000000ull);
  493. __add_128_64 (bcoeff, bcoeff, tl);
  494. if (!nanb)
  495. exp += (comb & 0xfff);
  496. res.w[0] = bcoeff.w[0];
  497. res.w[1] = (exp << 49) | sign.w[1] | bcoeff.w[1];
  498. res.w[1] |= nanb;
  499. BID_SWAP128 (res);
  500. BID_RETURN (res);
  501. }
  502. UINT128
  503. bid_to_bid128 (UINT128 bq) {
  504. UINT128 res;
  505. UINT64 sign, comb, exp;
  506. UINT64 trailing;
  507. UINT64 bcoeff;
  508. UINT128 rq;
  509. UINT128 qcoeff;
  510. UINT64 ba, bb;
  511. ba = *((UINT64 *) & bq + HIGH_128W);
  512. bb = *((UINT64 *) & bq + LOW_128W);
  513. sign = (ba & 0x8000000000000000ull);
  514. comb = (ba & 0x7fffc00000000000ull) >> 46;
  515. trailing = (ba & 0x00003fffffffffffull);
  516. if ((comb & 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
  517. exp = (comb >> 1) & 0x3fff;
  518. bcoeff = ((8 + (comb & 1)) << 46) | trailing;
  519. } else {
  520. exp = (comb >> 3) & 0x3fff;
  521. bcoeff = ((comb & 7) << 46) | trailing;
  522. }
  523. if ((comb & 0x1f000) == 0x1f000) { //NaN
  524. ba &= 0xfe003fffffffffffULL; // make exponent 0
  525. bcoeff &= 0x00003fffffffffffull; // NaN payloat is only T.
  526. if ((bcoeff > 0x0000314dc6448d93ULL) || // significand is non-canonical
  527. ((bcoeff == 0x0000314dc6448d93ULL)
  528. && (bb >= 0x38c15b0a00000000ULL))
  529. // significand is non-canonical
  530. ) {
  531. bcoeff = 0ull;
  532. ba &= ~0x00003fffffffffffull;
  533. bb = 0ull;
  534. }
  535. *((UINT64 *) & rq + HIGH_128W) = ba;
  536. *((UINT64 *) & rq + LOW_128W) = bb;
  537. return rq;
  538. } else if ((comb & 0x1e000) == 0x1e000) { //Inf
  539. ba &= 0xf800000000000000ULL; // make exponent and significand 0
  540. bb = 0;
  541. *((UINT64 *) & rq + HIGH_128W) = ba;
  542. *((UINT64 *) & rq + LOW_128W) = bb;
  543. return rq;
  544. }
  545. if ((bcoeff > 0x0001ed09bead87c0ull)
  546. || ((bcoeff == 0x0001ed09bead87c0ull)
  547. && (bb > 0x378d8e63ffffffffull))) {
  548. // significand is non-canonical
  549. bcoeff = 0ull;
  550. bb = 0ull;
  551. }
  552. *((UINT64 *) & qcoeff + 1) = bcoeff;
  553. *((UINT64 *) & qcoeff + 0) = bb;
  554. get_BID128_fast (&res, sign, exp, qcoeff);
  555. BID_SWAP128 (res);
  556. return res;
  557. }
  558. UINT32
  559. bid32_canonize (UINT32 ba) {
  560. FPSC bidrnd;
  561. unsigned int rnd = 0;
  562. UINT32 res;
  563. UINT32 sign, comb, exp;
  564. UINT32 trailing;
  565. UINT32 bcoeff;
  566. sign = (ba & 0x80000000);
  567. comb = (ba & 0x7ff00000) >> 20;
  568. trailing = (ba & 0x000fffff);
  569. if ((comb & 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
  570. exp = (comb >> 1) & 0xff;
  571. bcoeff = ((8 + (comb & 1)) << 20) | trailing;
  572. } else {
  573. exp = (comb >> 3) & 0xff;
  574. bcoeff = ((comb & 7) << 20) | trailing;
  575. }
  576. if ((comb & 0x7c0) == 0x7c0) { //NaN
  577. ba &= 0xfe0fffff; // make exponent 0
  578. bcoeff &= 0x000fffff; // NaN payloat is only T.
  579. if (bcoeff >= 1000000)
  580. ba &= 0xfff00000; //treat non-canonical significand
  581. return ba;
  582. } else if ((comb & 0x780) == 0x780) { //Inf
  583. ba &= 0xf8000000; // make exponent and significand 0
  584. return ba;
  585. }
  586. if (bcoeff >= 10000000)
  587. bcoeff = 0;
  588. rnd = bidrnd = ROUNDING_TO_NEAREST;
  589. res = get_BID32 (sign, exp, bcoeff, rnd, &bidrnd);
  590. return res;
  591. }
  592. UINT64
  593. bid64_canonize (UINT64 ba) {
  594. UINT64 res;
  595. UINT64 sign, comb, exp;
  596. UINT64 trailing;
  597. UINT64 bcoeff;
  598. sign = (ba & 0x8000000000000000ull);
  599. comb = (ba & 0x7ffc000000000000ull) >> 50;
  600. trailing = (ba & 0x0003ffffffffffffull);
  601. if ((comb & 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
  602. exp = (comb >> 1) & 0x3ff;
  603. bcoeff = ((8 + (comb & 1)) << 50) | trailing;
  604. } else {
  605. exp = (comb >> 3) & 0x3ff;
  606. bcoeff = ((comb & 7) << 50) | trailing;
  607. }
  608. if ((comb & 0x1f00) == 0x1f00) { //NaN
  609. ba &= 0xfe03ffffffffffffULL; // make exponent 0
  610. bcoeff &= 0x0003ffffffffffffull; // NaN payloat is only T.
  611. if (bcoeff >= 1000000000000000ull)
  612. ba &= 0xfe00000000000000ull; // treat non canonical significand and zero G6-G12
  613. return ba;
  614. } else if ((comb & 0x1e00) == 0x1e00) { //Inf
  615. ba &= 0xf800000000000000ULL; // make exponent and significand 0
  616. return ba;
  617. }
  618. if (bcoeff >= 10000000000000000ull) {
  619. bcoeff = 0ull;
  620. }
  621. res = very_fast_get_BID64 (sign, exp, bcoeff);
  622. return res;
  623. }
  624. UINT128
  625. bid128_canonize (UINT128 bq) {
  626. UINT128 res;
  627. UINT64 sign, comb, exp;
  628. UINT64 trailing;
  629. UINT64 bcoeff;
  630. UINT128 rq;
  631. UINT128 qcoeff;
  632. UINT64 ba, bb;
  633. ba = *((UINT64 *) & bq + HIGH_128W);
  634. bb = *((UINT64 *) & bq + LOW_128W);
  635. sign = (ba & 0x8000000000000000ull);
  636. comb = (ba & 0x7fffc00000000000ull) >> 46;
  637. trailing = (ba & 0x00003fffffffffffull);
  638. if ((comb & 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
  639. exp = (comb >> 1) & 0x3fff;
  640. bcoeff = ((8 + (comb & 1)) << 46) | trailing;
  641. } else {
  642. exp = (comb >> 3) & 0x3fff;
  643. bcoeff = ((comb & 7) << 46) | trailing;
  644. }
  645. if ((comb & 0x1f000) == 0x1f000) { //NaN
  646. ba &= 0xfe003fffffffffffULL; // make exponent 0
  647. bcoeff &= 0x00003fffffffffffull; // NaN payload is only T.
  648. if ((bcoeff > 0x0000314dc6448d93ULL) || // significand is non-canonical
  649. ((bcoeff == 0x0000314dc6448d93ULL)
  650. && (bb >= 0x38c15b0a00000000ULL))
  651. // significand is non-canonical
  652. ) {
  653. bcoeff = 0ull;
  654. ba &= ~0x00003fffffffffffull;
  655. bb = 0ull;
  656. }
  657. *((UINT64 *) & rq + HIGH_128W) = ba;
  658. *((UINT64 *) & rq + LOW_128W) = bb;
  659. return rq;
  660. } else if ((comb & 0x1e000) == 0x1e000) { //Inf
  661. ba &= 0xf800000000000000ULL; // make exponent and significand 0
  662. bb = 0;
  663. *((UINT64 *) & rq + HIGH_128W) = ba;
  664. *((UINT64 *) & rq + LOW_128W) = bb;
  665. return rq;
  666. }
  667. if ((bcoeff > 0x0001ed09bead87c0ull) || // significand is non-canonical
  668. ((bcoeff == 0x0001ed09bead87c0ull)
  669. && (bb > 0x378d8e63ffffffffull))
  670. // significand is non-canonical
  671. ) {
  672. bcoeff = 0ull;
  673. bb = 0ull;
  674. }
  675. *((UINT64 *) & qcoeff + 1) = bcoeff;
  676. *((UINT64 *) & qcoeff + 0) = bb;
  677. get_BID128_fast (&res, sign, exp, qcoeff);
  678. BID_SWAP128 (res);
  679. return res;
  680. }