1 /* Copyright (C) 2007-2024 Free Software Foundation, Inc.
3 This file is part of GCC.
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
24 #define DECNUMDIGITS 34 // work with up to 34 digits
26 #include "bid_internal.h"
30 bid_to_bid32 (UINT32 ba
) {
32 UINT32 sign
, comb
, exp
;
36 sign
= (ba
& 0x80000000ul
);
37 comb
= (ba
& 0x7ff00000ul
) >> 20;
38 trailing
= (ba
& 0x000ffffful
);
40 if ((comb
& 0x780) == 0x780) {
44 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
45 exp
= (comb
>> 1) & 0xff;
46 bcoeff
= ((8 + (comb
& 1)) << 20) | trailing
;
48 exp
= (comb
>> 3) & 0xff;
49 bcoeff
= ((comb
& 7) << 20) | trailing
;
51 if (bcoeff
>= 10000000)
53 res
= very_fast_get_BID32 (sign
, exp
, bcoeff
);
59 bid_to_bid64 (UINT64 ba
) {
61 UINT64 sign
, comb
, exp
;
65 sign
= (ba
& 0x8000000000000000ull
);
66 comb
= (ba
& 0x7ffc000000000000ull
) >> 50;
67 trailing
= (ba
& 0x0003ffffffffffffull
);
69 if ((comb
& 0x1e00) == 0x1e00) {
70 ba
&= 0xfff000000000000ULL
;
73 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
74 exp
= (comb
>> 1) & 0x3ff;
75 bcoeff
= ((8 + (comb
& 1)) << 50) | trailing
;
77 exp
= (comb
>> 3) & 0x3ff;
78 bcoeff
= ((comb
& 7) << 50) | trailing
;
80 if (bcoeff
>= 10000000000000000ull)
82 res
= very_fast_get_BID64 (sign
, exp
, bcoeff
);
87 #if DECIMAL_CALL_BY_REFERENCE
89 bid_to_dpd32 (UINT32
* pres
, UINT32
* pba
) {
93 bid_to_dpd32 (UINT32 ba
) {
97 UINT32 sign
, comb
, exp
, trailing
;
99 UINT32 bcoeff
, dcoeff
;
102 sign
= (ba
& 0x80000000);
103 comb
= (ba
& 0x7ff00000) >> 20;
104 trailing
= (ba
& 0xfffff);
106 // Detect infinity, and return canonical infinity
107 if ((comb
& 0x7c0) == 0x780) {
108 res
= sign
| 0x78000000;
110 // Detect NaN, and canonicalize trailing
111 } else if ((comb
& 0x7c0) == 0x7c0) {
112 if (trailing
> 999999)
114 nanb
= ba
& 0xfe000000;
117 } else { // Normal number
118 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
119 exp
= (comb
>> 1) & 0xff;
120 bcoeff
= ((8 + (comb
& 1)) << 20) | trailing
;
122 exp
= (comb
>> 3) & 0xff;
123 bcoeff
= ((comb
& 7) << 20) | trailing
;
125 // Zero the coefficient if non-canonical (>= 10^7)
126 if (bcoeff
>= 10000000)
130 b0
= bcoeff
/ 1000000;
131 b1
= (bcoeff
/ 1000) % 1000;
133 dcoeff
= (b2d
[b1
] << 10) | b2d
[b2
];
135 if (b0
>= 8) // is b0 8 or 9?
138 ((0x600 | ((exp
>> 6) << 7) | ((b0
& 1) << 6) | (exp
& 0x3f)) <<
140 else // else b0 is 0..7
142 sign
| ((((exp
>> 6) << 9) | (b0
<< 6) | (exp
& 0x3f)) << 20) |
150 #if DECIMAL_CALL_BY_REFERENCE
152 bid_to_dpd64 (UINT64
* pres
, UINT64
* pba
) {
156 bid_to_dpd64 (UINT64 ba
) {
160 UINT64 sign
, comb
, exp
;
162 UINT32 b0
, b1
, b2
, b3
, b4
, b5
;
168 //printf("arg bid "FMT_LLX16" \n", ba);
169 sign
= (ba
& 0x8000000000000000ull
);
170 comb
= (ba
& 0x7ffc000000000000ull
) >> 50;
171 trailing
= (ba
& 0x0003ffffffffffffull
);
173 // Detect infinity, and return canonical infinity
174 if ((comb
& 0x1f00) == 0x1e00) {
175 res
= sign
| 0x7800000000000000ull
;
177 // Detect NaN, and canonicalize trailing
178 } else if ((comb
& 0x1e00) == 0x1e00) {
179 if (trailing
> 999999999999999ull)
181 nanb
= ba
& 0xfe00000000000000ull
;
184 } else { // Normal number
185 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
186 exp
= (comb
>> 1) & 0x3ff;
187 bcoeff
= ((8 + (comb
& 1)) << 50) | trailing
;
189 exp
= (comb
>> 3) & 0x3ff;
190 bcoeff
= ((comb
& 7) << 50) | trailing
;
193 // Zero the coefficient if it is non-canonical (>= 10^16)
194 if (bcoeff
>= 10000000000000000ull)
198 // Floor(2^61 / 10^9)
199 #define D61 (2305843009ull)
201 // Multipy the binary coefficient by ceil(2^64 / 1000), and take the upper
202 // 64-bits in order to compute a division by 1000.
207 (UINT64
) (UINT32
) (bcoeff
>> (UINT64
) 27)) >> (UINT64
) 34;
208 ylo
= bcoeff
- 1000000000ull * yhi
;
209 if (ylo
>= 1000000000) {
210 ylo
= ylo
- 1000000000;
214 yhi
= bcoeff
/ 1000000000ull;
215 ylo
= bcoeff
% 1000000000ull;
218 // yhi = ABBBCCC ylo = DDDEEEFFF
219 b5
= ylo
% 1000; // b5 = FFF
220 b3
= ylo
/ 1000000; // b3 = DDD
221 b4
= (ylo
/ 1000) - (1000 * b3
); // b4 = EEE
222 b2
= yhi
% 1000; // b2 = CCC
223 b0
= yhi
/ 1000000; // b0 = A
224 b1
= (yhi
/ 1000) - (1000 * b0
); // b1 = BBB
226 dcoeff
= b2d
[b5
] | b2d2
[b4
] | b2d3
[b3
] | b2d4
[b2
] | b2d5
[b1
];
228 if (b0
>= 8) // is b0 8 or 9?
231 ((0x1800 | ((exp
>> 8) << 9) | ((b0
& 1) << 8) | (exp
& 0xff)) <<
233 else // else b0 is 0..7
235 sign
| ((((exp
>> 8) << 11) | (b0
<< 8) | (exp
& 0xff)) << 50) |
243 #if DECIMAL_CALL_BY_REFERENCE
245 dpd_to_bid32 (UINT32
* pres
, UINT32
* pda
) {
249 dpd_to_bid32 (UINT32 da
) {
251 UINT32 in
= *(UINT32
*) & da
;
254 UINT32 sign
, comb
, exp
;
256 UINT32 d0
= 0, d1
, d2
;
260 sign
= (in
& 0x80000000);
261 comb
= (in
& 0x7ff00000) >> 20;
262 trailing
= (in
& 0x000fffff);
264 if ((comb
& 0x7e0) == 0x780) { // G0..G4 = 1111 -> Inf
265 res
= in
& 0xf8000000;
267 } else if ((comb
& 0x7c0) == 0x7c0) { // G0..G5 = 11111 -> NaN
268 nanb
= in
& 0xfe000000;
270 } else { // Normal number
271 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> d0 = 8 + G4
272 d0
= ((comb
>> 6) & 1) | 8;
273 exp
= ((comb
& 0x180) >> 1) | (comb
& 0x3f);
275 d0
= (comb
>> 6) & 0x7;
276 exp
= ((comb
& 0x600) >> 3) | (comb
& 0x3f);
279 d1
= d2b2
[(trailing
>> 10) & 0x3ff];
280 d2
= d2b
[(trailing
) & 0x3ff];
282 bcoeff
= d2
+ d1
+ (1000000 * d0
);
283 if (bcoeff
< 0x800000) {
284 res
= (exp
<< 23) | bcoeff
| sign
;
286 res
= (exp
<< 21) | sign
| 0x60000000 | (bcoeff
& 0x1fffff);
294 #if DECIMAL_CALL_BY_REFERENCE
296 dpd_to_bid64 (UINT64
* pres
, UINT64
* pda
) {
300 dpd_to_bid64 (UINT64 da
) {
302 UINT64 in
= *(UINT64
*) & da
;
305 UINT64 sign
, comb
, exp
;
307 // UINT64 d0, d1, d2, d3, d4, d5;
310 UINT32 d0
, d3
, d4
, d5
;
314 //printf("arg dpd "FMT_LLX16" \n", in);
315 sign
= (in
& 0x8000000000000000ull
);
316 comb
= (in
& 0x7ffc000000000000ull
) >> 50;
317 trailing
= (in
& 0x0003ffffffffffffull
);
319 if ((comb
& 0x1f00) == 0x1e00) { // G0..G4 = 1111 -> Inf
320 res
= in
& 0xf800000000000000ull
;
322 } else if ((comb
& 0x1f00) == 0x1f00) { // G0..G5 = 11111 -> NaN
323 nanb
= in
& 0xfe00000000000000ull
;
326 } else { // Normal number
327 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> d0 = 8 + G4
328 d0
= ((comb
>> 8) & 1) | 8;
329 // d0 = (comb & 0x0100 ? 9 : 8);
330 exp
= (comb
& 0x600) >> 1;
331 // exp = (comb & 0x0400 ? 1 : 0) * 0x200 + (comb & 0x0200 ? 1 : 0) * 0x100; // exp leading bits are G2..G3
333 d0
= (comb
>> 8) & 0x7;
334 exp
= (comb
& 0x1800) >> 3;
335 // exp = (comb & 0x1000 ? 1 : 0) * 0x200 + (comb & 0x0800 ? 1 : 0) * 0x100; // exp loading bits are G0..G1
338 d1
= d2b5
[(trailing
>> 40) & 0x3ff];
339 d2
= d2b4
[(trailing
>> 30) & 0x3ff];
340 d3
= d2b3
[(trailing
>> 20) & 0x3ff];
341 d4
= d2b2
[(trailing
>> 10) & 0x3ff];
342 d5
= d2b
[(trailing
) & 0x3ff];
344 bcoeff
= (d5
+ d4
+ d3
) + d2
+ d1
+ (1000000000000000ull * d0
);
345 exp
+= (comb
& 0xff);
346 res
= very_fast_get_BID64 (sign
, exp
, bcoeff
);
353 #if DECIMAL_CALL_BY_REFERENCE
355 bid_to_dpd128 (UINT128
* pres
, UINT128
* pba
) {
359 bid_to_dpd128 (UINT128 ba
) {
366 UINT128 d0
, d1
, d2
, d3
, d4
, d5
, d6
, d7
, d8
, d9
, d10
, d11
;
371 sign
.w
[1] = (ba
.w
[HIGH_128W
] & 0x8000000000000000ull
);
373 comb
= (ba
.w
[HIGH_128W
] & 0x7fffc00000000000ull
) >> 46;
374 trailing
.w
[1] = (ba
.w
[HIGH_128W
] & 0x00003fffffffffffull
);
375 trailing
.w
[0] = ba
.w
[LOW_128W
];
378 if ((comb
& 0x1f000) == 0x1e000) { // G0..G4 = 1111 -> Inf
379 res
.w
[HIGH_128W
] = ba
.w
[HIGH_128W
] & 0xf800000000000000ull
;
382 // Detect NaN, and canonicalize trailing
383 } else if ((comb
& 0x1f000) == 0x1f000) {
384 if ((trailing
.w
[1] > 0x0000314dc6448d93ULL
) || // significand is non-canonical
385 ((trailing
.w
[1] == 0x0000314dc6448d93ULL
)
386 && (trailing
.w
[0] >= 0x38c15b0a00000000ULL
))
387 // significand is non-canonical
389 trailing
.w
[1] = trailing
.w
[0] = 0ull;
391 bcoeff
.w
[1] = trailing
.w
[1];
392 bcoeff
.w
[0] = trailing
.w
[0];
393 nanb
= ba
.w
[HIGH_128W
] & 0xfe00000000000000ull
;
395 } else { // Normal number
396 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
397 exp
= (comb
>> 1) & 0x3fff;
399 ((UINT64
) (8 + (comb
& 1)) << (UINT64
) 46) | trailing
.w
[1];
400 bcoeff
.w
[0] = trailing
.w
[0];
402 exp
= (comb
>> 3) & 0x3fff;
404 ((UINT64
) (comb
& 7) << (UINT64
) 46) | trailing
.w
[1];
405 bcoeff
.w
[0] = trailing
.w
[0];
407 // Zero the coefficient if non-canonical (>= 10^34)
408 if (bcoeff
.w
[1] > 0x1ed09bead87c0ull
||
409 (bcoeff
.w
[1] == 0x1ed09bead87c0ull
410 && bcoeff
.w
[0] >= 0x378D8E6400000000ull
)) {
411 bcoeff
.w
[0] = bcoeff
.w
[1] = 0;
414 // Constant 2^128 / 1000 + 1
419 UINT128 b11
, b10
, b9
, b8
, b7
, b6
, b5
, b4
, b3
, b2
, b1
;
420 d1000
.w
[1] = 0x4189374BC6A7EFull
;
421 d1000
.w
[0] = 0x9DB22D0E56041894ull
;
422 __mul_128x128_high (b11
, bcoeff
, d1000
);
423 __mul_128x128_high (b10
, b11
, d1000
);
424 __mul_128x128_high (b9
, b10
, d1000
);
425 __mul_128x128_high (b8
, b9
, d1000
);
426 __mul_128x128_high (b7
, b8
, d1000
);
427 __mul_128x128_high (b6
, b7
, d1000
);
428 __mul_128x128_high (b5
, b6
, d1000
);
429 __mul_128x128_high (b4
, b5
, d1000
);
430 __mul_128x128_high (b3
, b4
, d1000
);
431 __mul_128x128_high (b2
, b3
, d1000
);
432 __mul_128x128_high (b1
, b2
, d1000
);
435 __mul_64x128_full (t2
, t
, 1000ull, b11
);
436 __sub_128_128 (d11
, bcoeff
, t
);
437 __mul_64x128_full (t2
, t
, 1000ull, b10
);
438 __sub_128_128 (d10
, b11
, t
);
439 __mul_64x128_full (t2
, t
, 1000ull, b9
);
440 __sub_128_128 (d9
, b10
, t
);
441 __mul_64x128_full (t2
, t
, 1000ull, b8
);
442 __sub_128_128 (d8
, b9
, t
);
443 __mul_64x128_full (t2
, t
, 1000ull, b7
);
444 __sub_128_128 (d7
, b8
, t
);
445 __mul_64x128_full (t2
, t
, 1000ull, b6
);
446 __sub_128_128 (d6
, b7
, t
);
447 __mul_64x128_full (t2
, t
, 1000ull, b5
);
448 __sub_128_128 (d5
, b6
, t
);
449 __mul_64x128_full (t2
, t
, 1000ull, b4
);
450 __sub_128_128 (d4
, b5
, t
);
451 __mul_64x128_full (t2
, t
, 1000ull, b3
);
452 __sub_128_128 (d3
, b4
, t
);
453 __mul_64x128_full (t2
, t
, 1000ull, b2
);
454 __sub_128_128 (d2
, b3
, t
);
455 __mul_64x128_full (t2
, t
, 1000ull, b1
);
456 __sub_128_128 (d1
, b2
, t
);
461 dcoeff
.w
[0] = b2d
[d11
.w
[0]] | (b2d
[d10
.w
[0]] << 10) |
462 (b2d
[d9
.w
[0]] << 20) | (b2d
[d8
.w
[0]] << 30) | (b2d
[d7
.w
[0]] << 40) |
463 (b2d
[d6
.w
[0]] << 50) | (b2d
[d5
.w
[0]] << 60);
465 (b2d
[d5
.w
[0]] >> 4) | (b2d
[d4
.w
[0]] << 6) | (b2d
[d3
.w
[0]] << 16) |
466 (b2d
[d2
.w
[0]] << 26) | (b2d
[d1
.w
[0]] << 36);
468 res
.w
[0] = dcoeff
.w
[0];
473 ((0x18000 | ((exp
>> 12) << 13) | ((d0
.w
[0] & 1) << 12) |
474 (exp
& 0xfff)) << 46) | dcoeff
.w
[1];
478 w
[1] | ((((exp
>> 12) << 15) | (d0
.w
[0] << 12) | (exp
& 0xfff))
479 << 46) | dcoeff
.w
[1];
488 #if DECIMAL_CALL_BY_REFERENCE
490 dpd_to_bid128 (UINT128
* pres
, UINT128
* pda
) {
494 dpd_to_bid128 (UINT128 da
) {
496 UINT128 in
= *(UINT128
*) & da
;
502 UINT64 d0
, d1
, d2
, d3
, d4
, d5
, d6
, d7
, d8
, d9
, d10
, d11
;
507 sign
.w
[1] = (in
.w
[HIGH_128W
] & 0x8000000000000000ull
);
509 comb
= (in
.w
[HIGH_128W
] & 0x7fffc00000000000ull
) >> 46;
510 trailing
.w
[1] = (in
.w
[HIGH_128W
] & 0x00003fffffffffffull
);
511 trailing
.w
[0] = in
.w
[LOW_128W
];
514 if ((comb
& 0x1f000) == 0x1e000) { // G0..G4 = 1111 -> Inf
515 res
.w
[HIGH_128W
] = in
.w
[HIGH_128W
] & 0xf800000000000000ull
;
516 res
.w
[LOW_128W
] = 0ull;
518 } else if ((comb
& 0x1f000) == 0x1f000) { // G0..G4 = 11111 -> NaN
519 nanb
= in
.w
[HIGH_128W
] & 0xfe00000000000000ull
;
522 } else { // Normal number
523 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> d0 = 8 + G4
524 d0
= 8 + (comb
& 0x01000 ? 1 : 0);
526 (comb
& 0x04000 ? 1 : 0) * 0x2000 +
527 (comb
& 0x02000 ? 1 : 0) * 0x1000;
528 // exp leading bits are G2..G3
531 4 * (comb
& 0x04000 ? 1 : 0) + 2 * (comb
& 0x2000 ? 1 : 0) +
532 (comb
& 0x1000 ? 1 : 0);
534 (comb
& 0x10000 ? 1 : 0) * 0x2000 +
535 (comb
& 0x08000 ? 1 : 0) * 0x1000;
536 // exp loading bits are G0..G1
540 d11
= d2b
[(trailing
.w
[0]) & 0x3ff];
541 d10
= d2b
[(trailing
.w
[0] >> 10) & 0x3ff];
542 d9
= d2b
[(trailing
.w
[0] >> 20) & 0x3ff];
543 d8
= d2b
[(trailing
.w
[0] >> 30) & 0x3ff];
544 d7
= d2b
[(trailing
.w
[0] >> 40) & 0x3ff];
545 d6
= d2b
[(trailing
.w
[0] >> 50) & 0x3ff];
546 d5
= d2b
[(trailing
.w
[0] >> 60) | ((trailing
.w
[1] & 0x3f) << 4)];
547 d4
= d2b
[(trailing
.w
[1] >> 6) & 0x3ff];
548 d3
= d2b
[(trailing
.w
[1] >> 16) & 0x3ff];
549 d2
= d2b
[(trailing
.w
[1] >> 26) & 0x3ff];
550 d1
= d2b
[(trailing
.w
[1] >> 36) & 0x3ff];
553 d11
+ (d10
* 1000ull) + (d9
* 1000000ull) + (d8
* 1000000000ull) +
554 (d7
* 1000000000000ull) + (d6
* 1000000000000000ull);
556 d5
+ (d4
* 1000ull) + (d3
* 1000000ull) + (d2
* 1000000000ull) +
557 (d1
* 1000000000000ull) + (d0
* 1000000000000000ull);
558 __mul_64x64_to_128 (bcoeff
, th
, 1000000000000000000ull);
559 __add_128_64 (bcoeff
, bcoeff
, tl
);
562 exp
+= (comb
& 0xfff);
564 res
.w
[0] = bcoeff
.w
[0];
565 res
.w
[1] = (exp
<< 49) | sign
.w
[1] | bcoeff
.w
[1];
574 bid_to_bid128 (UINT128 bq
) {
576 UINT64 sign
, comb
, exp
;
584 ba
= *((UINT64
*) & bq
+ HIGH_128W
);
585 bb
= *((UINT64
*) & bq
+ LOW_128W
);
587 sign
= (ba
& 0x8000000000000000ull
);
588 comb
= (ba
& 0x7fffc00000000000ull
) >> 46;
589 trailing
= (ba
& 0x00003fffffffffffull
);
591 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
592 exp
= (comb
>> 1) & 0x3fff;
593 bcoeff
= ((8 + (comb
& 1)) << 46) | trailing
;
595 exp
= (comb
>> 3) & 0x3fff;
596 bcoeff
= ((comb
& 7) << 46) | trailing
;
599 if ((comb
& 0x1f000) == 0x1f000) { //NaN
600 ba
&= 0xfe003fffffffffffULL
; // make exponent 0
601 bcoeff
&= 0x00003fffffffffffull
; // NaN payloat is only T.
602 if ((bcoeff
> 0x0000314dc6448d93ULL
) || // significand is non-canonical
603 ((bcoeff
== 0x0000314dc6448d93ULL
)
604 && (bb
>= 0x38c15b0a00000000ULL
))
605 // significand is non-canonical
608 ba
&= ~0x00003fffffffffffull
;
611 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
612 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
614 } else if ((comb
& 0x1e000) == 0x1e000) { //Inf
615 ba
&= 0xf800000000000000ULL
; // make exponent and significand 0
617 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
618 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
622 if ((bcoeff
> 0x0001ed09bead87c0ull
)
623 || ((bcoeff
== 0x0001ed09bead87c0ull
)
624 && (bb
> 0x378d8e63ffffffffull
))) {
625 // significand is non-canonical
630 *((UINT64
*) & qcoeff
+ 1) = bcoeff
;
631 *((UINT64
*) & qcoeff
+ 0) = bb
;
633 get_BID128_fast (&res
, sign
, exp
, qcoeff
);
640 bid32_canonize (UINT32 ba
) {
642 unsigned int rnd
= 0;
645 UINT32 sign
, comb
, exp
;
649 sign
= (ba
& 0x80000000);
650 comb
= (ba
& 0x7ff00000) >> 20;
651 trailing
= (ba
& 0x000fffff);
653 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
654 exp
= (comb
>> 1) & 0xff;
655 bcoeff
= ((8 + (comb
& 1)) << 20) | trailing
;
657 exp
= (comb
>> 3) & 0xff;
658 bcoeff
= ((comb
& 7) << 20) | trailing
;
661 if ((comb
& 0x7c0) == 0x7c0) { //NaN
662 ba
&= 0xfe0fffff; // make exponent 0
663 bcoeff
&= 0x000fffff; // NaN payloat is only T.
664 if (bcoeff
>= 1000000)
665 ba
&= 0xfff00000; //treat non-canonical significand
667 } else if ((comb
& 0x780) == 0x780) { //Inf
668 ba
&= 0xf8000000; // make exponent and significand 0
672 if (bcoeff
>= 10000000)
674 rnd
= bidrnd
= ROUNDING_TO_NEAREST
;
675 res
= get_BID32 (sign
, exp
, bcoeff
, rnd
, &bidrnd
);
680 bid64_canonize (UINT64 ba
) {
682 UINT64 sign
, comb
, exp
;
686 sign
= (ba
& 0x8000000000000000ull
);
687 comb
= (ba
& 0x7ffc000000000000ull
) >> 50;
688 trailing
= (ba
& 0x0003ffffffffffffull
);
691 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
692 exp
= (comb
>> 1) & 0x3ff;
693 bcoeff
= ((8 + (comb
& 1)) << 50) | trailing
;
695 exp
= (comb
>> 3) & 0x3ff;
696 bcoeff
= ((comb
& 7) << 50) | trailing
;
699 if ((comb
& 0x1f00) == 0x1f00) { //NaN
700 ba
&= 0xfe03ffffffffffffULL
; // make exponent 0
701 bcoeff
&= 0x0003ffffffffffffull
; // NaN payloat is only T.
702 if (bcoeff
>= 1000000000000000ull)
703 ba
&= 0xfe00000000000000ull
; // treat non canonical significand and zero G6-G12
705 } else if ((comb
& 0x1e00) == 0x1e00) { //Inf
706 ba
&= 0xf800000000000000ULL
; // make exponent and significand 0
710 if (bcoeff
>= 10000000000000000ull) {
713 res
= very_fast_get_BID64 (sign
, exp
, bcoeff
);
718 bid128_canonize (UINT128 bq
) {
720 UINT64 sign
, comb
, exp
;
728 ba
= *((UINT64
*) & bq
+ HIGH_128W
);
729 bb
= *((UINT64
*) & bq
+ LOW_128W
);
731 sign
= (ba
& 0x8000000000000000ull
);
732 comb
= (ba
& 0x7fffc00000000000ull
) >> 46;
733 trailing
= (ba
& 0x00003fffffffffffull
);
735 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
736 exp
= (comb
>> 1) & 0x3fff;
737 bcoeff
= ((8 + (comb
& 1)) << 46) | trailing
;
739 exp
= (comb
>> 3) & 0x3fff;
740 bcoeff
= ((comb
& 7) << 46) | trailing
;
743 if ((comb
& 0x1f000) == 0x1f000) { //NaN
744 ba
&= 0xfe003fffffffffffULL
; // make exponent 0
745 bcoeff
&= 0x00003fffffffffffull
; // NaN payload is only T.
747 if ((bcoeff
> 0x0000314dc6448d93ULL
) || // significand is non-canonical
748 ((bcoeff
== 0x0000314dc6448d93ULL
)
749 && (bb
>= 0x38c15b0a00000000ULL
))
750 // significand is non-canonical
753 ba
&= ~0x00003fffffffffffull
;
756 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
757 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
759 } else if ((comb
& 0x1e000) == 0x1e000) { //Inf
760 ba
&= 0xf800000000000000ULL
; // make exponent and significand 0
762 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
763 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
767 if ((bcoeff
> 0x0001ed09bead87c0ull
) || // significand is non-canonical
768 ((bcoeff
== 0x0001ed09bead87c0ull
)
769 && (bb
> 0x378d8e63ffffffffull
))
770 // significand is non-canonical
776 *((UINT64
*) & qcoeff
+ 1) = bcoeff
;
777 *((UINT64
*) & qcoeff
+ 0) = bb
;
779 get_BID128_fast (&res
, sign
, exp
, qcoeff
);