1 /* Copyright (C) 2007 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 2, or (at your option) any later
10 In addition to the permissions in the GNU General Public License, the
11 Free Software Foundation gives you unlimited permission to link the
12 compiled version of this file into combinations with other programs,
13 and to distribute those combinations without any restriction coming
14 from the use of this file. (The General Public License restrictions
15 do apply in other respects; for example, they cover modification of
16 the file, and distribution when not linked into a combine
19 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
20 WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 You should have received a copy of the GNU General Public License
25 along with GCC; see the file COPYING. If not, write to the Free
26 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 #define DECNUMDIGITS 34 // work with up to 34 digits
31 #include "bid_internal.h"
35 bid_to_bid32 (UINT32 ba
) {
37 UINT32 sign
, comb
, exp
;
41 sign
= (ba
& 0x80000000ul
);
42 comb
= (ba
& 0x7ff00000ul
) >> 20;
43 trailing
= (ba
& 0x000ffffful
);
45 if ((comb
& 0x780) == 0x780) {
49 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
50 exp
= (comb
>> 1) & 0xff;
51 bcoeff
= ((8 + (comb
& 1)) << 20) | trailing
;
53 exp
= (comb
>> 3) & 0xff;
54 bcoeff
= ((comb
& 7) << 20) | trailing
;
56 if (bcoeff
>= 10000000)
58 res
= very_fast_get_BID32 (sign
, exp
, bcoeff
);
64 bid_to_bid64 (UINT64 ba
) {
66 UINT64 sign
, comb
, exp
;
70 sign
= (ba
& 0x8000000000000000ull
);
71 comb
= (ba
& 0x7ffc000000000000ull
) >> 50;
72 trailing
= (ba
& 0x0003ffffffffffffull
);
74 if ((comb
& 0x1e00) == 0x1e00) {
75 ba
&= 0xfff000000000000ULL
;
78 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
79 exp
= (comb
>> 1) & 0x3ff;
80 bcoeff
= ((8 + (comb
& 1)) << 50) | trailing
;
82 exp
= (comb
>> 3) & 0x3ff;
83 bcoeff
= ((comb
& 7) << 50) | trailing
;
85 if (bcoeff
>= 10000000000000000ull)
87 res
= very_fast_get_BID64 (sign
, exp
, bcoeff
);
92 #if DECIMAL_CALL_BY_REFERENCE
94 bid_to_dpd32 (UINT32
* pres
, UINT32
* pba
) {
98 bid_to_dpd32 (UINT32 ba
) {
102 UINT32 sign
, comb
, exp
, trailing
;
104 UINT32 bcoeff
, dcoeff
;
107 sign
= (ba
& 0x80000000);
108 comb
= (ba
& 0x7ff00000) >> 20;
109 trailing
= (ba
& 0xfffff);
111 // Detect infinity, and return canonical infinity
112 if ((comb
& 0x7c0) == 0x780) {
113 res
= sign
| 0x78000000;
115 // Detect NaN, and canonicalize trailing
116 } else if ((comb
& 0x7c0) == 0x7c0) {
117 if (trailing
> 999999)
119 nanb
= ba
& 0xfe000000;
122 } else { // Normal number
123 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
124 exp
= (comb
>> 1) & 0xff;
125 bcoeff
= ((8 + (comb
& 1)) << 20) | trailing
;
127 exp
= (comb
>> 3) & 0xff;
128 bcoeff
= ((comb
& 7) << 20) | trailing
;
130 // Zero the coefficient if non-canonical (>= 10^7)
131 if (bcoeff
>= 10000000)
135 b0
= bcoeff
/ 1000000;
136 b1
= (bcoeff
/ 1000) % 1000;
138 dcoeff
= (b2d
[b1
] << 10) | b2d
[b2
];
140 if (b0
>= 8) // is b0 8 or 9?
143 ((0x600 | ((exp
>> 6) << 7) | ((b0
& 1) << 6) | (exp
& 0x3f)) <<
145 else // else b0 is 0..7
147 sign
| ((((exp
>> 6) << 9) | (b0
<< 6) | (exp
& 0x3f)) << 20) |
155 #if DECIMAL_CALL_BY_REFERENCE
157 bid_to_dpd64 (UINT64
* pres
, UINT64
* pba
) {
161 bid_to_dpd64 (UINT64 ba
) {
165 UINT64 sign
, comb
, exp
;
167 UINT32 b0
, b1
, b2
, b3
, b4
, b5
;
173 //printf("arg bid "FMT_LLX16" \n", ba);
174 sign
= (ba
& 0x8000000000000000ull
);
175 comb
= (ba
& 0x7ffc000000000000ull
) >> 50;
176 trailing
= (ba
& 0x0003ffffffffffffull
);
178 // Detect infinity, and return canonical infinity
179 if ((comb
& 0x1f00) == 0x1e00) {
180 res
= sign
| 0x7800000000000000ull
;
182 // Detect NaN, and canonicalize trailing
183 } else if ((comb
& 0x1e00) == 0x1e00) {
184 if (trailing
> 999999999999999ull)
186 nanb
= ba
& 0xfe00000000000000ull
;
189 } else { // Normal number
190 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
191 exp
= (comb
>> 1) & 0x3ff;
192 bcoeff
= ((8 + (comb
& 1)) << 50) | trailing
;
194 exp
= (comb
>> 3) & 0x3ff;
195 bcoeff
= ((comb
& 7) << 50) | trailing
;
198 // Zero the coefficient if it is non-canonical (>= 10^16)
199 if (bcoeff
>= 10000000000000000ull)
203 // Floor(2^61 / 10^9)
204 #define D61 (2305843009ull)
206 // Multipy the binary coefficient by ceil(2^64 / 1000), and take the upper
207 // 64-bits in order to compute a division by 1000.
212 (UINT64
) (UINT32
) (bcoeff
>> (UINT64
) 27)) >> (UINT64
) 34;
213 ylo
= bcoeff
- 1000000000ull * yhi
;
214 if (ylo
>= 1000000000) {
215 ylo
= ylo
- 1000000000;
219 yhi
= bcoeff
/ 1000000000ull;
220 ylo
= bcoeff
% 1000000000ull;
223 // yhi = ABBBCCC ylo = DDDEEEFFF
224 b5
= ylo
% 1000; // b5 = FFF
225 b3
= ylo
/ 1000000; // b3 = DDD
226 b4
= (ylo
/ 1000) - (1000 * b3
); // b4 = EEE
227 b2
= yhi
% 1000; // b2 = CCC
228 b0
= yhi
/ 1000000; // b0 = A
229 b1
= (yhi
/ 1000) - (1000 * b0
); // b1 = BBB
231 dcoeff
= b2d
[b5
] | b2d2
[b4
] | b2d3
[b3
] | b2d4
[b2
] | b2d5
[b1
];
233 if (b0
>= 8) // is b0 8 or 9?
236 ((0x1800 | ((exp
>> 8) << 9) | ((b0
& 1) << 8) | (exp
& 0xff)) <<
238 else // else b0 is 0..7
240 sign
| ((((exp
>> 8) << 11) | (b0
<< 8) | (exp
& 0xff)) << 50) |
248 #if DECIMAL_CALL_BY_REFERENCE
250 dpd_to_bid32 (UINT32
* pres
, UINT32
* pda
) {
254 dpd_to_bid32 (UINT32 da
) {
256 UINT32 in
= *(UINT32
*) & da
;
259 UINT32 sign
, comb
, exp
;
261 UINT32 d0
= 0, d1
, d2
;
265 sign
= (in
& 0x80000000);
266 comb
= (in
& 0x7ff00000) >> 20;
267 trailing
= (in
& 0x000fffff);
269 if ((comb
& 0x7e0) == 0x780) { // G0..G4 = 1111 -> Inf
270 res
= in
& 0xf8000000;
272 } else if ((comb
& 0x7c0) == 0x7c0) { // G0..G5 = 11111 -> NaN
273 nanb
= in
& 0xfe000000;
275 } else { // Normal number
276 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> d0 = 8 + G4
277 d0
= ((comb
>> 6) & 1) | 8;
278 exp
= ((comb
& 0x180) >> 1) | (comb
& 0x3f);
280 d0
= (comb
>> 6) & 0x7;
281 exp
= ((comb
& 0x600) >> 3) | (comb
& 0x3f);
284 d1
= d2b2
[(trailing
>> 10) & 0x3ff];
285 d2
= d2b
[(trailing
) & 0x3ff];
287 bcoeff
= d2
+ d1
+ (1000000 * d0
);
288 if (bcoeff
< 0x800000) {
289 res
= (exp
<< 23) | bcoeff
| sign
;
291 res
= (exp
<< 21) | sign
| 0x60000000 | (bcoeff
& 0x1fffff);
299 #if DECIMAL_CALL_BY_REFERENCE
301 dpd_to_bid64 (UINT64
* pres
, UINT64
* pda
) {
305 dpd_to_bid64 (UINT64 da
) {
307 UINT64 in
= *(UINT64
*) & da
;
310 UINT64 sign
, comb
, exp
;
312 // UINT64 d0, d1, d2, d3, d4, d5;
315 UINT32 d0
, d3
, d4
, d5
;
319 //printf("arg dpd "FMT_LLX16" \n", in);
320 sign
= (in
& 0x8000000000000000ull
);
321 comb
= (in
& 0x7ffc000000000000ull
) >> 50;
322 trailing
= (in
& 0x0003ffffffffffffull
);
324 if ((comb
& 0x1f00) == 0x1e00) { // G0..G4 = 1111 -> Inf
325 res
= in
& 0xf800000000000000ull
;
327 } else if ((comb
& 0x1f00) == 0x1f00) { // G0..G5 = 11111 -> NaN
328 nanb
= in
& 0xfe00000000000000ull
;
331 } else { // Normal number
332 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> d0 = 8 + G4
333 d0
= ((comb
>> 8) & 1) | 8;
334 // d0 = (comb & 0x0100 ? 9 : 8);
335 exp
= (comb
& 0x600) >> 1;
336 // exp = (comb & 0x0400 ? 1 : 0) * 0x200 + (comb & 0x0200 ? 1 : 0) * 0x100; // exp leading bits are G2..G3
338 d0
= (comb
>> 8) & 0x7;
339 exp
= (comb
& 0x1800) >> 3;
340 // exp = (comb & 0x1000 ? 1 : 0) * 0x200 + (comb & 0x0800 ? 1 : 0) * 0x100; // exp loading bits are G0..G1
343 d1
= d2b5
[(trailing
>> 40) & 0x3ff];
344 d2
= d2b4
[(trailing
>> 30) & 0x3ff];
345 d3
= d2b3
[(trailing
>> 20) & 0x3ff];
346 d4
= d2b2
[(trailing
>> 10) & 0x3ff];
347 d5
= d2b
[(trailing
) & 0x3ff];
349 bcoeff
= (d5
+ d4
+ d3
) + d2
+ d1
+ (1000000000000000ull * d0
);
350 exp
+= (comb
& 0xff);
351 res
= very_fast_get_BID64 (sign
, exp
, bcoeff
);
358 #if DECIMAL_CALL_BY_REFERENCE
360 bid_to_dpd128 (UINT128
* pres
, UINT128
* pba
) {
364 bid_to_dpd128 (UINT128 ba
) {
371 UINT128 d0
, d1
, d2
, d3
, d4
, d5
, d6
, d7
, d8
, d9
, d10
, d11
;
376 sign
.w
[1] = (ba
.w
[HIGH_128W
] & 0x8000000000000000ull
);
378 comb
= (ba
.w
[HIGH_128W
] & 0x7fffc00000000000ull
) >> 46;
379 trailing
.w
[1] = (ba
.w
[HIGH_128W
] & 0x00003fffffffffffull
);
380 trailing
.w
[0] = ba
.w
[LOW_128W
];
383 if ((comb
& 0x1f000) == 0x1e000) { // G0..G4 = 1111 -> Inf
384 res
.w
[HIGH_128W
] = ba
.w
[HIGH_128W
] & 0xf800000000000000ull
;
387 // Detect NaN, and canonicalize trailing
388 } else if ((comb
& 0x1f000) == 0x1f000) {
389 if ((trailing
.w
[1] > 0x0000314dc6448d93ULL
) || // significand is non-canonical
390 ((trailing
.w
[1] == 0x0000314dc6448d93ULL
)
391 && (trailing
.w
[0] >= 0x38c15b0a00000000ULL
))
392 // significand is non-canonical
394 trailing
.w
[1] = trailing
.w
[0] = 0ull;
396 bcoeff
.w
[1] = trailing
.w
[1];
397 bcoeff
.w
[0] = trailing
.w
[0];
398 nanb
= ba
.w
[HIGH_128W
] & 0xfe00000000000000ull
;
400 } else { // Normal number
401 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
402 exp
= (comb
>> 1) & 0x3fff;
404 ((UINT64
) (8 + (comb
& 1)) << (UINT64
) 46) | trailing
.w
[1];
405 bcoeff
.w
[0] = trailing
.w
[0];
407 exp
= (comb
>> 3) & 0x3fff;
409 ((UINT64
) (comb
& 7) << (UINT64
) 46) | trailing
.w
[1];
410 bcoeff
.w
[0] = trailing
.w
[0];
412 // Zero the coefficient if non-canonical (>= 10^34)
413 if (bcoeff
.w
[1] > 0x1ed09bead87c0ull
||
414 (bcoeff
.w
[1] == 0x1ed09bead87c0ull
415 && bcoeff
.w
[0] >= 0x378D8E6400000000ull
)) {
416 bcoeff
.w
[0] = bcoeff
.w
[1] = 0;
419 // Constant 2^128 / 1000 + 1
424 UINT128 b11
, b10
, b9
, b8
, b7
, b6
, b5
, b4
, b3
, b2
, b1
;
425 d1000
.w
[1] = 0x4189374BC6A7EFull
;
426 d1000
.w
[0] = 0x9DB22D0E56041894ull
;
427 __mul_128x128_high (b11
, bcoeff
, d1000
);
428 __mul_128x128_high (b10
, b11
, d1000
);
429 __mul_128x128_high (b9
, b10
, d1000
);
430 __mul_128x128_high (b8
, b9
, d1000
);
431 __mul_128x128_high (b7
, b8
, d1000
);
432 __mul_128x128_high (b6
, b7
, d1000
);
433 __mul_128x128_high (b5
, b6
, d1000
);
434 __mul_128x128_high (b4
, b5
, d1000
);
435 __mul_128x128_high (b3
, b4
, d1000
);
436 __mul_128x128_high (b2
, b3
, d1000
);
437 __mul_128x128_high (b1
, b2
, d1000
);
440 __mul_64x128_full (t2
, t
, 1000ull, b11
);
441 __sub_128_128 (d11
, bcoeff
, t
);
442 __mul_64x128_full (t2
, t
, 1000ull, b10
);
443 __sub_128_128 (d10
, b11
, t
);
444 __mul_64x128_full (t2
, t
, 1000ull, b9
);
445 __sub_128_128 (d9
, b10
, t
);
446 __mul_64x128_full (t2
, t
, 1000ull, b8
);
447 __sub_128_128 (d8
, b9
, t
);
448 __mul_64x128_full (t2
, t
, 1000ull, b7
);
449 __sub_128_128 (d7
, b8
, t
);
450 __mul_64x128_full (t2
, t
, 1000ull, b6
);
451 __sub_128_128 (d6
, b7
, t
);
452 __mul_64x128_full (t2
, t
, 1000ull, b5
);
453 __sub_128_128 (d5
, b6
, t
);
454 __mul_64x128_full (t2
, t
, 1000ull, b4
);
455 __sub_128_128 (d4
, b5
, t
);
456 __mul_64x128_full (t2
, t
, 1000ull, b3
);
457 __sub_128_128 (d3
, b4
, t
);
458 __mul_64x128_full (t2
, t
, 1000ull, b2
);
459 __sub_128_128 (d2
, b3
, t
);
460 __mul_64x128_full (t2
, t
, 1000ull, b1
);
461 __sub_128_128 (d1
, b2
, t
);
466 dcoeff
.w
[0] = b2d
[d11
.w
[0]] | (b2d
[d10
.w
[0]] << 10) |
467 (b2d
[d9
.w
[0]] << 20) | (b2d
[d8
.w
[0]] << 30) | (b2d
[d7
.w
[0]] << 40) |
468 (b2d
[d6
.w
[0]] << 50) | (b2d
[d5
.w
[0]] << 60);
470 (b2d
[d5
.w
[0]] >> 4) | (b2d
[d4
.w
[0]] << 6) | (b2d
[d3
.w
[0]] << 16) |
471 (b2d
[d2
.w
[0]] << 26) | (b2d
[d1
.w
[0]] << 36);
473 res
.w
[0] = dcoeff
.w
[0];
478 ((0x18000 | ((exp
>> 12) << 13) | ((d0
.w
[0] & 1) << 12) |
479 (exp
& 0xfff)) << 46) | dcoeff
.w
[1];
483 w
[1] | ((((exp
>> 12) << 15) | (d0
.w
[0] << 12) | (exp
& 0xfff))
484 << 46) | dcoeff
.w
[1];
493 #if DECIMAL_CALL_BY_REFERENCE
495 dpd_to_bid128 (UINT128
* pres
, UINT128
* pda
) {
499 dpd_to_bid128 (UINT128 da
) {
501 UINT128 in
= *(UINT128
*) & da
;
507 UINT64 d0
, d1
, d2
, d3
, d4
, d5
, d6
, d7
, d8
, d9
, d10
, d11
;
512 sign
.w
[1] = (in
.w
[HIGH_128W
] & 0x8000000000000000ull
);
514 comb
= (in
.w
[HIGH_128W
] & 0x7fffc00000000000ull
) >> 46;
515 trailing
.w
[1] = (in
.w
[HIGH_128W
] & 0x00003fffffffffffull
);
516 trailing
.w
[0] = in
.w
[LOW_128W
];
519 if ((comb
& 0x1f000) == 0x1e000) { // G0..G4 = 1111 -> Inf
520 res
.w
[HIGH_128W
] = in
.w
[HIGH_128W
] & 0xf800000000000000ull
;
521 res
.w
[LOW_128W
] = 0ull;
523 } else if ((comb
& 0x1f000) == 0x1f000) { // G0..G4 = 11111 -> NaN
524 nanb
= in
.w
[HIGH_128W
] & 0xfe00000000000000ull
;
527 } else { // Normal number
528 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> d0 = 8 + G4
529 d0
= 8 + (comb
& 0x01000 ? 1 : 0);
531 (comb
& 0x04000 ? 1 : 0) * 0x2000 +
532 (comb
& 0x02000 ? 1 : 0) * 0x1000;
533 // exp leading bits are G2..G3
536 4 * (comb
& 0x04000 ? 1 : 0) + 2 * (comb
& 0x2000 ? 1 : 0) +
537 (comb
& 0x1000 ? 1 : 0);
539 (comb
& 0x10000 ? 1 : 0) * 0x2000 +
540 (comb
& 0x08000 ? 1 : 0) * 0x1000;
541 // exp loading bits are G0..G1
545 d11
= d2b
[(trailing
.w
[0]) & 0x3ff];
546 d10
= d2b
[(trailing
.w
[0] >> 10) & 0x3ff];
547 d9
= d2b
[(trailing
.w
[0] >> 20) & 0x3ff];
548 d8
= d2b
[(trailing
.w
[0] >> 30) & 0x3ff];
549 d7
= d2b
[(trailing
.w
[0] >> 40) & 0x3ff];
550 d6
= d2b
[(trailing
.w
[0] >> 50) & 0x3ff];
551 d5
= d2b
[(trailing
.w
[0] >> 60) | ((trailing
.w
[1] & 0x3f) << 4)];
552 d4
= d2b
[(trailing
.w
[1] >> 6) & 0x3ff];
553 d3
= d2b
[(trailing
.w
[1] >> 16) & 0x3ff];
554 d2
= d2b
[(trailing
.w
[1] >> 26) & 0x3ff];
555 d1
= d2b
[(trailing
.w
[1] >> 36) & 0x3ff];
558 d11
+ (d10
* 1000ull) + (d9
* 1000000ull) + (d8
* 1000000000ull) +
559 (d7
* 1000000000000ull) + (d6
* 1000000000000000ull);
561 d5
+ (d4
* 1000ull) + (d3
* 1000000ull) + (d2
* 1000000000ull) +
562 (d1
* 1000000000000ull) + (d0
* 1000000000000000ull);
563 __mul_64x64_to_128 (bcoeff
, th
, 1000000000000000000ull);
564 __add_128_64 (bcoeff
, bcoeff
, tl
);
567 exp
+= (comb
& 0xfff);
569 res
.w
[0] = bcoeff
.w
[0];
570 res
.w
[1] = (exp
<< 49) | sign
.w
[1] | bcoeff
.w
[1];
579 bid_to_bid128 (UINT128 bq
) {
581 UINT64 sign
, comb
, exp
;
589 ba
= *((UINT64
*) & bq
+ HIGH_128W
);
590 bb
= *((UINT64
*) & bq
+ LOW_128W
);
592 sign
= (ba
& 0x8000000000000000ull
);
593 comb
= (ba
& 0x7fffc00000000000ull
) >> 46;
594 trailing
= (ba
& 0x00003fffffffffffull
);
596 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
597 exp
= (comb
>> 1) & 0x3fff;
598 bcoeff
= ((8 + (comb
& 1)) << 46) | trailing
;
600 exp
= (comb
>> 3) & 0x3fff;
601 bcoeff
= ((comb
& 7) << 46) | trailing
;
604 if ((comb
& 0x1f000) == 0x1f000) { //NaN
605 ba
&= 0xfe003fffffffffffULL
; // make exponent 0
606 bcoeff
&= 0x00003fffffffffffull
; // NaN payloat is only T.
607 if ((bcoeff
> 0x0000314dc6448d93ULL
) || // significand is non-canonical
608 ((bcoeff
== 0x0000314dc6448d93ULL
)
609 && (bb
>= 0x38c15b0a00000000ULL
))
610 // significand is non-canonical
613 ba
&= ~0x00003fffffffffffull
;
616 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
617 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
619 } else if ((comb
& 0x1e000) == 0x1e000) { //Inf
620 ba
&= 0xf800000000000000ULL
; // make exponent and significand 0
622 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
623 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
627 if ((bcoeff
> 0x0001ed09bead87c0ull
)
628 || ((bcoeff
== 0x0001ed09bead87c0ull
)
629 && (bb
> 0x378d8e63ffffffffull
))) {
630 // significand is non-canonical
635 *((UINT64
*) & qcoeff
+ 1) = bcoeff
;
636 *((UINT64
*) & qcoeff
+ 0) = bb
;
638 get_BID128_fast (&res
, sign
, exp
, qcoeff
);
645 bid32_canonize (UINT32 ba
) {
647 unsigned int rnd
= 0;
650 UINT32 sign
, comb
, exp
;
654 sign
= (ba
& 0x80000000);
655 comb
= (ba
& 0x7ff00000) >> 20;
656 trailing
= (ba
& 0x000fffff);
658 if ((comb
& 0x600) == 0x600) { // G0..G1 = 11 -> exp is G2..G11
659 exp
= (comb
>> 1) & 0xff;
660 bcoeff
= ((8 + (comb
& 1)) << 20) | trailing
;
662 exp
= (comb
>> 3) & 0xff;
663 bcoeff
= ((comb
& 7) << 20) | trailing
;
666 if ((comb
& 0x7c0) == 0x7c0) { //NaN
667 ba
&= 0xfe0fffff; // make exponent 0
668 bcoeff
&= 0x000fffff; // NaN payloat is only T.
669 if (bcoeff
>= 1000000)
670 ba
&= 0xfff00000; //treat non-canonical significand
672 } else if ((comb
& 0x780) == 0x780) { //Inf
673 ba
&= 0xf8000000; // make exponent and significand 0
677 if (bcoeff
>= 10000000)
679 rnd
= bidrnd
= ROUNDING_TO_NEAREST
;
680 res
= get_BID32 (sign
, exp
, bcoeff
, rnd
, &bidrnd
);
685 bid64_canonize (UINT64 ba
) {
687 UINT64 sign
, comb
, exp
;
691 sign
= (ba
& 0x8000000000000000ull
);
692 comb
= (ba
& 0x7ffc000000000000ull
) >> 50;
693 trailing
= (ba
& 0x0003ffffffffffffull
);
696 if ((comb
& 0x1800) == 0x1800) { // G0..G1 = 11 -> exp is G2..G11
697 exp
= (comb
>> 1) & 0x3ff;
698 bcoeff
= ((8 + (comb
& 1)) << 50) | trailing
;
700 exp
= (comb
>> 3) & 0x3ff;
701 bcoeff
= ((comb
& 7) << 50) | trailing
;
704 if ((comb
& 0x1f00) == 0x1f00) { //NaN
705 ba
&= 0xfe03ffffffffffffULL
; // make exponent 0
706 bcoeff
&= 0x0003ffffffffffffull
; // NaN payloat is only T.
707 if (bcoeff
>= 1000000000000000ull)
708 ba
&= 0xfe00000000000000ull
; // treat non canonical significand and zero G6-G12
710 } else if ((comb
& 0x1e00) == 0x1e00) { //Inf
711 ba
&= 0xf800000000000000ULL
; // make exponent and significand 0
715 if (bcoeff
>= 10000000000000000ull) {
718 res
= very_fast_get_BID64 (sign
, exp
, bcoeff
);
723 bid128_canonize (UINT128 bq
) {
725 UINT64 sign
, comb
, exp
;
733 ba
= *((UINT64
*) & bq
+ HIGH_128W
);
734 bb
= *((UINT64
*) & bq
+ LOW_128W
);
736 sign
= (ba
& 0x8000000000000000ull
);
737 comb
= (ba
& 0x7fffc00000000000ull
) >> 46;
738 trailing
= (ba
& 0x00003fffffffffffull
);
740 if ((comb
& 0x18000) == 0x18000) { // G0..G1 = 11 -> exp is G2..G11
741 exp
= (comb
>> 1) & 0x3fff;
742 bcoeff
= ((8 + (comb
& 1)) << 46) | trailing
;
744 exp
= (comb
>> 3) & 0x3fff;
745 bcoeff
= ((comb
& 7) << 46) | trailing
;
748 if ((comb
& 0x1f000) == 0x1f000) { //NaN
749 ba
&= 0xfe003fffffffffffULL
; // make exponent 0
750 bcoeff
&= 0x00003fffffffffffull
; // NaN payload is only T.
752 if ((bcoeff
> 0x0000314dc6448d93ULL
) || // significand is non-canonical
753 ((bcoeff
== 0x0000314dc6448d93ULL
)
754 && (bb
>= 0x38c15b0a00000000ULL
))
755 // significand is non-canonical
758 ba
&= ~0x00003fffffffffffull
;
761 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
762 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
764 } else if ((comb
& 0x1e000) == 0x1e000) { //Inf
765 ba
&= 0xf800000000000000ULL
; // make exponent and significand 0
767 *((UINT64
*) & rq
+ HIGH_128W
) = ba
;
768 *((UINT64
*) & rq
+ LOW_128W
) = bb
;
772 if ((bcoeff
> 0x0001ed09bead87c0ull
) || // significand is non-canonical
773 ((bcoeff
== 0x0001ed09bead87c0ull
)
774 && (bb
> 0x378d8e63ffffffffull
))
775 // significand is non-canonical
781 *((UINT64
*) & qcoeff
+ 1) = bcoeff
;
782 *((UINT64
*) & qcoeff
+ 0) = bb
;
784 get_BID128_fast (&res
, sign
, exp
, qcoeff
);