1 /* Copyright (C) 2007-2016 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 #include "bid_internal.h"
26 /*****************************************************************************
27 * BID64_to_uint64_rnint
28 ****************************************************************************/
30 #if DECIMAL_CALL_BY_REFERENCE
32 bid64_to_uint64_rnint (UINT64
* pres
, UINT64
* px
33 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
38 bid64_to_uint64_rnint (UINT64 x
39 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
45 int exp
; // unbiased exponent
46 // Note: C1 represents x_significand (UINT64)
48 unsigned int x_nr_bits
;
52 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
56 // check for NaN or Infinity
57 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
59 *pfpsf
|= INVALID_EXCEPTION
;
60 // return Integer Indefinite
61 res
= 0x8000000000000000ull
;
65 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
66 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
67 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
68 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
69 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
70 if (C1
> 9999999999999999ull) { // non-canonical
75 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
76 C1
= x
& MASK_BINARY_SIG1
;
79 // check for zeros (possibly from non-canonical values)
82 res
= 0x0000000000000000ull
;
85 // x is not special and is not zero
87 // q = nr. of decimal digits in x (1 <= q <= 54)
88 // determine first the nr. of bits in x
89 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
90 // split the 64-bit value in two 32-bit halves to avoid rounding errors
91 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
92 tmp1
.d
= (double) (C1
>> 32); // exact conversion
94 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
96 tmp1
.d
= (double) C1
; // exact conversion
98 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
100 } else { // if x < 2^53
101 tmp1
.d
= (double) C1
; // exact conversion
103 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
105 q
= nr_digits
[x_nr_bits
- 1].digits
;
107 q
= nr_digits
[x_nr_bits
- 1].digits1
;
108 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
111 exp
= x_exp
- 398; // unbiased exponent
113 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
115 *pfpsf
|= INVALID_EXCEPTION
;
116 // return Integer Indefinite
117 res
= 0x8000000000000000ull
;
119 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
120 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
121 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
122 // the cases that do not fit are identified here; the ones that fit
123 // fall through and will be handled with other cases further,
124 // under '1 <= q + exp <= 20'
125 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1/2
126 // => set invalid flag
127 *pfpsf
|= INVALID_EXCEPTION
;
128 // return Integer Indefinite
129 res
= 0x8000000000000000ull
;
131 } else { // if n > 0 and q + exp = 20
132 // if n >= 2^64 - 1/2 then n is too large
133 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64-1/2
134 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64-1/2
135 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65-1)
136 // <=> C * 10^(21-q) >= 0x9fffffffffffffffb, 1<=q<=16
138 // C * 10^20 >= 0x9fffffffffffffffb
139 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
141 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
143 *pfpsf
|= INVALID_EXCEPTION
;
144 // return Integer Indefinite
145 res
= 0x8000000000000000ull
;
148 // else cases that can be rounded to a 64-bit int fall through
149 // to '1 <= q + exp <= 20'
150 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
151 // Note: C * 10^(21-q) has 20 or 21 digits; 0x9fffffffffffffffb
153 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
155 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
157 *pfpsf
|= INVALID_EXCEPTION
;
158 // return Integer Indefinite
159 res
= 0x8000000000000000ull
;
162 // else cases that can be rounded to a 64-bit int fall through
163 // to '1 <= q + exp <= 20'
167 // n is not too large to be converted to int64 if -1/2 <= n < 2^64 - 1/2
168 // Note: some of the cases tested for above fall through to this point
169 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
171 res
= 0x0000000000000000ull
;
173 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
174 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
180 ind
= q
- 1; // 0 <= ind <= 15
181 if (C1
<= midpoint64
[ind
]) {
182 res
= 0x0000000000000000ull
; // return 0
183 } else if (!x_sign
) { // n > 0
184 res
= 0x0000000000000001ull
; // return +1
186 res
= 0x8000000000000000ull
;
187 *pfpsf
|= INVALID_EXCEPTION
;
190 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
191 // x <= -1 or 1 <= x < 2^64-1/2 so if positive x can be rounded
192 // to nearest to a 64-bit unsigned signed integer
193 if (x_sign
) { // x <= -1
195 *pfpsf
|= INVALID_EXCEPTION
;
196 // return Integer Indefinite
197 res
= 0x8000000000000000ull
;
200 // 1 <= x < 2^64-1/2 so x can be rounded
201 // to nearest to a 64-bit unsigned integer
202 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
203 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
204 // chop off ind digits from the lower part of C1
205 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
206 C1
= C1
+ midpoint64
[ind
- 1];
207 // calculate C* and f*
208 // C* is actually floor(C*) in this case
209 // C* and f* need shifting and masking, as shown by
210 // shiftright128[] and maskhigh128[]
212 // kx = 10^(-x) = ten2mk64[ind - 1]
213 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
214 // the approximation of 10^(-x) was rounded up to 54 bits
215 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
217 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
218 fstar
.w
[0] = P128
.w
[0];
219 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
220 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
221 // if (0 < f* < 10^(-x)) then the result is a midpoint
222 // if floor(C*) is even then C* = floor(C*) - logical right
223 // shift; C* has p decimal digits, correct by Prop. 1)
224 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
225 // shift; C* has p decimal digits, correct by Pr. 1)
227 // C* = floor(C*) (logical right shift; C has p decimal digits,
228 // correct by Property 1)
231 // shift right C* by Ex-64 = shiftright128[ind]
232 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
233 Cstar
= Cstar
>> shift
;
235 // if the result was a midpoint it was rounded away from zero, so
236 // it will need a correction
237 // check for midpoints
238 if ((fstar
.w
[1] == 0) && fstar
.w
[0] &&
239 (fstar
.w
[0] <= ten2mk128trunc
[ind
- 1].w
[1])) {
240 // ten2mk128trunc[ind -1].w[1] is identical to
241 // ten2mk128[ind -1].w[1]
242 // the result is a midpoint; round to nearest
243 if (Cstar
& 0x01) { // Cstar is odd; MP in [EVEN, ODD]
244 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
245 Cstar
--; // Cstar is now even
246 } // else MP in [ODD, EVEN]
248 res
= Cstar
; // the result is positive
249 } else if (exp
== 0) {
252 res
= C1
; // the result is positive
253 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
254 // res = +C * 10^exp (exact)
255 res
= C1
* ten2k64
[exp
]; // the result is positive
261 /*****************************************************************************
262 * BID64_to_uint64_xrnint
263 ****************************************************************************/
265 #if DECIMAL_CALL_BY_REFERENCE
267 bid64_to_uint64_xrnint (UINT64
* pres
, UINT64
* px
268 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
273 bid64_to_uint64_xrnint (UINT64 x
274 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
280 int exp
; // unbiased exponent
281 // Note: C1 represents x_significand (UINT64)
284 unsigned int x_nr_bits
;
288 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
292 // check for NaN or Infinity
293 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
295 *pfpsf
|= INVALID_EXCEPTION
;
296 // return Integer Indefinite
297 res
= 0x8000000000000000ull
;
301 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
302 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
303 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
304 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
305 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
306 if (C1
> 9999999999999999ull) { // non-canonical
311 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
312 C1
= x
& MASK_BINARY_SIG1
;
315 // check for zeros (possibly from non-canonical values)
318 res
= 0x0000000000000000ull
;
321 // x is not special and is not zero
323 // q = nr. of decimal digits in x (1 <= q <= 54)
324 // determine first the nr. of bits in x
325 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
326 // split the 64-bit value in two 32-bit halves to avoid rounding errors
327 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
328 tmp1
.d
= (double) (C1
>> 32); // exact conversion
330 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
332 tmp1
.d
= (double) C1
; // exact conversion
334 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
336 } else { // if x < 2^53
337 tmp1
.d
= (double) C1
; // exact conversion
339 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
341 q
= nr_digits
[x_nr_bits
- 1].digits
;
343 q
= nr_digits
[x_nr_bits
- 1].digits1
;
344 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
347 exp
= x_exp
- 398; // unbiased exponent
349 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
351 *pfpsf
|= INVALID_EXCEPTION
;
352 // return Integer Indefinite
353 res
= 0x8000000000000000ull
;
355 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
356 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
357 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
358 // the cases that do not fit are identified here; the ones that fit
359 // fall through and will be handled with other cases further,
360 // under '1 <= q + exp <= 20'
361 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1/2
362 // => set invalid flag
363 *pfpsf
|= INVALID_EXCEPTION
;
364 // return Integer Indefinite
365 res
= 0x8000000000000000ull
;
367 } else { // if n > 0 and q + exp = 20
368 // if n >= 2^64 - 1/2 then n is too large
369 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64-1/2
370 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64-1/2
371 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65-1)
372 // <=> C * 10^(21-q) >= 0x9fffffffffffffffb, 1<=q<=16
374 // C * 10^20 >= 0x9fffffffffffffffb
375 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
377 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
379 *pfpsf
|= INVALID_EXCEPTION
;
380 // return Integer Indefinite
381 res
= 0x8000000000000000ull
;
384 // else cases that can be rounded to a 64-bit int fall through
385 // to '1 <= q + exp <= 20'
386 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
387 // Note: C * 10^(21-q) has 20 or 21 digits; 0x9fffffffffffffffb
389 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
391 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
393 *pfpsf
|= INVALID_EXCEPTION
;
394 // return Integer Indefinite
395 res
= 0x8000000000000000ull
;
398 // else cases that can be rounded to a 64-bit int fall through
399 // to '1 <= q + exp <= 20'
403 // n is not too large to be converted to int64 if -1/2 <= n < 2^64 - 1/2
404 // Note: some of the cases tested for above fall through to this point
405 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
407 *pfpsf
|= INEXACT_EXCEPTION
;
409 res
= 0x0000000000000000ull
;
411 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
412 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
418 ind
= q
- 1; // 0 <= ind <= 15
419 if (C1
<= midpoint64
[ind
]) {
420 res
= 0x0000000000000000ull
; // return 0
421 } else if (!x_sign
) { // n > 0
422 res
= 0x0000000000000001ull
; // return +1
424 res
= 0x8000000000000000ull
;
425 *pfpsf
|= INVALID_EXCEPTION
;
429 *pfpsf
|= INEXACT_EXCEPTION
;
430 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
431 // x <= -1 or 1 <= x < 2^64-1/2 so if positive x can be rounded
432 // to nearest to a 64-bit unsigned signed integer
433 if (x_sign
) { // x <= -1
435 *pfpsf
|= INVALID_EXCEPTION
;
436 // return Integer Indefinite
437 res
= 0x8000000000000000ull
;
440 // 1 <= x < 2^64-1/2 so x can be rounded
441 // to nearest to a 64-bit unsigned integer
442 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
443 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
444 // chop off ind digits from the lower part of C1
445 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
446 C1
= C1
+ midpoint64
[ind
- 1];
447 // calculate C* and f*
448 // C* is actually floor(C*) in this case
449 // C* and f* need shifting and masking, as shown by
450 // shiftright128[] and maskhigh128[]
452 // kx = 10^(-x) = ten2mk64[ind - 1]
453 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
454 // the approximation of 10^(-x) was rounded up to 54 bits
455 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
457 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
458 fstar
.w
[0] = P128
.w
[0];
459 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
460 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
461 // if (0 < f* < 10^(-x)) then the result is a midpoint
462 // if floor(C*) is even then C* = floor(C*) - logical right
463 // shift; C* has p decimal digits, correct by Prop. 1)
464 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
465 // shift; C* has p decimal digits, correct by Pr. 1)
467 // C* = floor(C*) (logical right shift; C has p decimal digits,
468 // correct by Property 1)
471 // shift right C* by Ex-64 = shiftright128[ind]
472 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
473 Cstar
= Cstar
>> shift
;
474 // determine inexactness of the rounding of C*
475 // if (0 < f* - 1/2 < 10^(-x)) then
476 // the result is exact
477 // else // if (f* - 1/2 > T*) then
478 // the result is inexact
479 if (ind
- 1 <= 2) { // fstar.w[1] is 0
480 if (fstar
.w
[0] > 0x8000000000000000ull
) {
481 // f* > 1/2 and the result may be exact
482 tmp64
= fstar
.w
[0] - 0x8000000000000000ull
; // f* - 1/2
483 if ((tmp64
> ten2mk128trunc
[ind
- 1].w
[1])) {
484 // ten2mk128trunc[ind -1].w[1] is identical to
485 // ten2mk128[ind -1].w[1]
486 // set the inexact flag
487 *pfpsf
|= INEXACT_EXCEPTION
;
488 } // else the result is exact
489 } else { // the result is inexact; f2* <= 1/2
490 // set the inexact flag
491 *pfpsf
|= INEXACT_EXCEPTION
;
493 } else { // if 3 <= ind - 1 <= 14
494 if (fstar
.w
[1] > onehalf128
[ind
- 1] ||
495 (fstar
.w
[1] == onehalf128
[ind
- 1] && fstar
.w
[0])) {
496 // f2* > 1/2 and the result may be exact
497 // Calculate f2* - 1/2
498 tmp64
= fstar
.w
[1] - onehalf128
[ind
- 1];
499 if (tmp64
|| fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
500 // ten2mk128trunc[ind -1].w[1] is identical to
501 // ten2mk128[ind -1].w[1]
502 // set the inexact flag
503 *pfpsf
|= INEXACT_EXCEPTION
;
504 } // else the result is exact
505 } else { // the result is inexact; f2* <= 1/2
506 // set the inexact flag
507 *pfpsf
|= INEXACT_EXCEPTION
;
511 // if the result was a midpoint it was rounded away from zero, so
512 // it will need a correction
513 // check for midpoints
514 if ((fstar
.w
[1] == 0) && fstar
.w
[0] &&
515 (fstar
.w
[0] <= ten2mk128trunc
[ind
- 1].w
[1])) {
516 // ten2mk128trunc[ind -1].w[1] is identical to
517 // ten2mk128[ind -1].w[1]
518 // the result is a midpoint; round to nearest
519 if (Cstar
& 0x01) { // Cstar is odd; MP in [EVEN, ODD]
520 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
521 Cstar
--; // Cstar is now even
522 } // else MP in [ODD, EVEN]
524 res
= Cstar
; // the result is positive
525 } else if (exp
== 0) {
528 res
= C1
; // the result is positive
529 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
530 // res = +C * 10^exp (exact)
531 res
= C1
* ten2k64
[exp
]; // the result is positive
537 /*****************************************************************************
538 * BID64_to_uint64_floor
539 ****************************************************************************/
541 #if DECIMAL_CALL_BY_REFERENCE
543 bid64_to_uint64_floor (UINT64
* pres
, UINT64
* px
544 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
549 bid64_to_uint64_floor (UINT64 x
550 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
556 int exp
; // unbiased exponent
557 // Note: C1 represents x_significand (UINT64)
559 unsigned int x_nr_bits
;
563 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
566 // check for NaN or Infinity
567 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
569 *pfpsf
|= INVALID_EXCEPTION
;
570 // return Integer Indefinite
571 res
= 0x8000000000000000ull
;
575 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
576 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
577 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
578 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
579 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
580 if (C1
> 9999999999999999ull) { // non-canonical
585 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
586 C1
= x
& MASK_BINARY_SIG1
;
589 // check for zeros (possibly from non-canonical values)
592 res
= 0x0000000000000000ull
;
595 // x is not special and is not zero
597 if (x_sign
) { // if n < 0 the conversion is invalid
599 *pfpsf
|= INVALID_EXCEPTION
;
600 // return Integer Indefinite
601 res
= 0x8000000000000000ull
;
604 // q = nr. of decimal digits in x (1 <= q <= 54)
605 // determine first the nr. of bits in x
606 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
607 // split the 64-bit value in two 32-bit halves to avoid rounding errors
608 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
609 tmp1
.d
= (double) (C1
>> 32); // exact conversion
611 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
613 tmp1
.d
= (double) C1
; // exact conversion
615 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
617 } else { // if x < 2^53
618 tmp1
.d
= (double) C1
; // exact conversion
620 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
622 q
= nr_digits
[x_nr_bits
- 1].digits
;
624 q
= nr_digits
[x_nr_bits
- 1].digits1
;
625 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
628 exp
= x_exp
- 398; // unbiased exponent
630 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
632 *pfpsf
|= INVALID_EXCEPTION
;
633 // return Integer Indefinite
634 res
= 0x8000000000000000ull
;
636 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
637 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
638 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
639 // the cases that do not fit are identified here; the ones that fit
640 // fall through and will be handled with other cases further,
641 // under '1 <= q + exp <= 20'
642 // n > 0 and q + exp = 20
643 // if n >= 2^64 then n is too large
644 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64
645 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64
646 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65)
647 // <=> C * 10^(21-q) >= 0xa0000000000000000, 1<=q<=16
649 // C * 10^20 >= 0xa0000000000000000
650 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
651 if (C
.w
[1] >= 0x0a) {
652 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
654 *pfpsf
|= INVALID_EXCEPTION
;
655 // return Integer Indefinite
656 res
= 0x8000000000000000ull
;
659 // else cases that can be rounded to a 64-bit int fall through
660 // to '1 <= q + exp <= 20'
661 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
662 // Note: C * 10^(21-q) has 20 or 21 digits; 0xa0000000000000000
664 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
665 if (C
.w
[1] >= 0x0a) {
666 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
668 *pfpsf
|= INVALID_EXCEPTION
;
669 // return Integer Indefinite
670 res
= 0x8000000000000000ull
;
673 // else cases that can be rounded to a 64-bit int fall through
674 // to '1 <= q + exp <= 20'
677 // n is not too large to be converted to int64 if -1 < n < 2^64
678 // Note: some of the cases tested for above fall through to this point
679 if ((q
+ exp
) <= 0) { // n = +0.[0...0]c(0)c(1)...c(q-1)
681 res
= 0x0000000000000000ull
;
683 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
684 // 1 <= x < 2^64 so x can be rounded
685 // to nearest to a 64-bit unsigned signed integer
686 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
687 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
688 // chop off ind digits from the lower part of C1
689 // C1 fits in 64 bits
690 // calculate C* and f*
691 // C* is actually floor(C*) in this case
692 // C* and f* need shifting and masking, as shown by
693 // shiftright128[] and maskhigh128[]
695 // kx = 10^(-x) = ten2mk64[ind - 1]
697 // the approximation of 10^(-x) was rounded up to 54 bits
698 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
700 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
701 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
702 // C* = floor(C*) (logical right shift; C has p decimal digits,
703 // correct by Property 1)
706 // shift right C* by Ex-64 = shiftright128[ind]
707 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
708 Cstar
= Cstar
>> shift
;
709 res
= Cstar
; // the result is positive
710 } else if (exp
== 0) {
713 res
= C1
; // the result is positive
714 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
715 // res = +C * 10^exp (exact)
716 res
= C1
* ten2k64
[exp
]; // the result is positive
722 /*****************************************************************************
723 * BID64_to_uint64_xfloor
724 ****************************************************************************/
726 #if DECIMAL_CALL_BY_REFERENCE
728 bid64_to_uint64_xfloor (UINT64
* pres
, UINT64
* px
729 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
734 bid64_to_uint64_xfloor (UINT64 x
735 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
741 int exp
; // unbiased exponent
742 // Note: C1 represents x_significand (UINT64)
744 unsigned int x_nr_bits
;
748 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
752 // check for NaN or Infinity
753 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
755 *pfpsf
|= INVALID_EXCEPTION
;
756 // return Integer Indefinite
757 res
= 0x8000000000000000ull
;
761 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
762 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
763 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
764 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
765 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
766 if (C1
> 9999999999999999ull) { // non-canonical
771 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
772 C1
= x
& MASK_BINARY_SIG1
;
775 // check for zeros (possibly from non-canonical values)
778 res
= 0x0000000000000000ull
;
781 // x is not special and is not zero
783 if (x_sign
) { // if n < 0 the conversion is invalid
785 *pfpsf
|= INVALID_EXCEPTION
;
786 // return Integer Indefinite
787 res
= 0x8000000000000000ull
;
790 // q = nr. of decimal digits in x (1 <= q <= 54)
791 // determine first the nr. of bits in x
792 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
793 // split the 64-bit value in two 32-bit halves to avoid rounding errors
794 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
795 tmp1
.d
= (double) (C1
>> 32); // exact conversion
797 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
799 tmp1
.d
= (double) C1
; // exact conversion
801 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
803 } else { // if x < 2^53
804 tmp1
.d
= (double) C1
; // exact conversion
806 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
808 q
= nr_digits
[x_nr_bits
- 1].digits
;
810 q
= nr_digits
[x_nr_bits
- 1].digits1
;
811 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
814 exp
= x_exp
- 398; // unbiased exponent
816 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
818 *pfpsf
|= INVALID_EXCEPTION
;
819 // return Integer Indefinite
820 res
= 0x8000000000000000ull
;
822 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
823 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
824 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
825 // the cases that do not fit are identified here; the ones that fit
826 // fall through and will be handled with other cases further,
827 // under '1 <= q + exp <= 20'
828 // n > 0 and q + exp = 20
829 // if n >= 2^64 then n is too large
830 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64
831 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64
832 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65)
833 // <=> C * 10^(21-q) >= 0xa0000000000000000, 1<=q<=16
835 // C * 10^20 >= 0xa0000000000000000
836 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
837 if (C
.w
[1] >= 0x0a) {
838 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
840 *pfpsf
|= INVALID_EXCEPTION
;
841 // return Integer Indefinite
842 res
= 0x8000000000000000ull
;
845 // else cases that can be rounded to a 64-bit int fall through
846 // to '1 <= q + exp <= 20'
847 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
848 // Note: C * 10^(21-q) has 20 or 21 digits; 0xa0000000000000000
850 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
851 if (C
.w
[1] >= 0x0a) {
852 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
854 *pfpsf
|= INVALID_EXCEPTION
;
855 // return Integer Indefinite
856 res
= 0x8000000000000000ull
;
859 // else cases that can be rounded to a 64-bit int fall through
860 // to '1 <= q + exp <= 20'
863 // n is not too large to be converted to int64 if -1 < n < 2^64
864 // Note: some of the cases tested for above fall through to this point
865 if ((q
+ exp
) <= 0) { // n = +0.[0...0]c(0)c(1)...c(q-1)
867 *pfpsf
|= INEXACT_EXCEPTION
;
869 res
= 0x0000000000000000ull
;
871 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
872 // 1 <= x < 2^64 so x can be rounded
873 // to nearest to a 64-bit unsigned signed integer
874 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
875 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
876 // chop off ind digits from the lower part of C1
877 // C1 fits in 64 bits
878 // calculate C* and f*
879 // C* is actually floor(C*) in this case
880 // C* and f* need shifting and masking, as shown by
881 // shiftright128[] and maskhigh128[]
883 // kx = 10^(-x) = ten2mk64[ind - 1]
885 // the approximation of 10^(-x) was rounded up to 54 bits
886 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
888 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
889 fstar
.w
[0] = P128
.w
[0];
890 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
891 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
892 // C* = floor(C*) (logical right shift; C has p decimal digits,
893 // correct by Property 1)
896 // shift right C* by Ex-64 = shiftright128[ind]
897 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
898 Cstar
= Cstar
>> shift
;
899 // determine inexactness of the rounding of C*
900 // if (0 < f* < 10^(-x)) then
901 // the result is exact
902 // else // if (f* > T*) then
903 // the result is inexact
904 if (ind
- 1 <= 2) { // fstar.w[1] is 0
905 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
906 // ten2mk128trunc[ind -1].w[1] is identical to
907 // ten2mk128[ind -1].w[1]
908 // set the inexact flag
909 *pfpsf
|= INEXACT_EXCEPTION
;
910 } // else the result is exact
911 } else { // if 3 <= ind - 1 <= 14
912 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
913 // ten2mk128trunc[ind -1].w[1] is identical to
914 // ten2mk128[ind -1].w[1]
915 // set the inexact flag
916 *pfpsf
|= INEXACT_EXCEPTION
;
917 } // else the result is exact
920 res
= Cstar
; // the result is positive
921 } else if (exp
== 0) {
924 res
= C1
; // the result is positive
925 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
926 // res = +C * 10^exp (exact)
927 res
= C1
* ten2k64
[exp
]; // the result is positive
933 /*****************************************************************************
934 * BID64_to_uint64_ceil
935 ****************************************************************************/
937 #if DECIMAL_CALL_BY_REFERENCE
939 bid64_to_uint64_ceil (UINT64
* pres
, UINT64
* px
940 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
945 bid64_to_uint64_ceil (UINT64 x
946 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
952 int exp
; // unbiased exponent
953 // Note: C1 represents x_significand (UINT64)
955 unsigned int x_nr_bits
;
959 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
963 // check for NaN or Infinity
964 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
966 *pfpsf
|= INVALID_EXCEPTION
;
967 // return Integer Indefinite
968 res
= 0x8000000000000000ull
;
972 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
973 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
974 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
975 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
976 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
977 if (C1
> 9999999999999999ull) { // non-canonical
982 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
983 C1
= x
& MASK_BINARY_SIG1
;
986 // check for zeros (possibly from non-canonical values)
989 res
= 0x0000000000000000ull
;
992 // x is not special and is not zero
994 // q = nr. of decimal digits in x (1 <= q <= 54)
995 // determine first the nr. of bits in x
996 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
997 // split the 64-bit value in two 32-bit halves to avoid rounding errors
998 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
999 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1001 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1002 } else { // x < 2^32
1003 tmp1
.d
= (double) C1
; // exact conversion
1005 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1007 } else { // if x < 2^53
1008 tmp1
.d
= (double) C1
; // exact conversion
1010 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1012 q
= nr_digits
[x_nr_bits
- 1].digits
;
1014 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1015 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1018 exp
= x_exp
- 398; // unbiased exponent
1020 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
1022 *pfpsf
|= INVALID_EXCEPTION
;
1023 // return Integer Indefinite
1024 res
= 0x8000000000000000ull
;
1026 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
1027 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1028 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
1029 // the cases that do not fit are identified here; the ones that fit
1030 // fall through and will be handled with other cases further,
1031 // under '1 <= q + exp <= 20'
1032 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1
1033 // => set invalid flag
1034 *pfpsf
|= INVALID_EXCEPTION
;
1035 // return Integer Indefinite
1036 res
= 0x8000000000000000ull
;
1038 } else { // if n > 0 and q + exp = 20
1039 // if n > 2^64 - 1 then n is too large
1040 // <=> c(0)c(1)...c(19).c(20)...c(q-1) > 2^64 - 1
1041 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 > 2^64 - 1
1042 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65 - 2)
1043 // <=> C * 10^(21-q) > 0x9fffffffffffffff6, 1<=q<=16
1045 // C * 10^20 > 0x9fffffffffffffff6
1046 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
1047 if (C
.w
[1] > 0x09 ||
1048 (C
.w
[1] == 0x09 && C
.w
[0] > 0xfffffffffffffff6ull
)) {
1050 *pfpsf
|= INVALID_EXCEPTION
;
1051 // return Integer Indefinite
1052 res
= 0x8000000000000000ull
;
1055 // else cases that can be rounded to a 64-bit int fall through
1056 // to '1 <= q + exp <= 20'
1057 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
1058 // Note: C * 10^(21-q) has 20 or 21 digits; 0x9fffffffffffffff6
1060 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
1061 if (C
.w
[1] > 0x09 ||
1062 (C
.w
[1] == 0x09 && C
.w
[0] > 0xfffffffffffffff6ull
)) {
1064 *pfpsf
|= INVALID_EXCEPTION
;
1065 // return Integer Indefinite
1066 res
= 0x8000000000000000ull
;
1069 // else cases that can be rounded to a 64-bit int fall through
1070 // to '1 <= q + exp <= 20'
1074 // n is not too large to be converted to int64 if -1 < n < 2^64
1075 // Note: some of the cases tested for above fall through to this point
1076 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1079 res
= 0x0000000000000000ull
;
1081 res
= 0x0000000000000001ull
;
1083 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
1084 // x <= -1 or 1 <= x <= 2^64 - 1 so if positive x can be rounded
1085 // to nearest to a 64-bit unsigned signed integer
1086 if (x_sign
) { // x <= -1
1088 *pfpsf
|= INVALID_EXCEPTION
;
1089 // return Integer Indefinite
1090 res
= 0x8000000000000000ull
;
1093 // 1 <= x <= 2^64 - 1 so x can be rounded
1094 // to nearest to a 64-bit unsigned integer
1095 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
1096 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1097 // chop off ind digits from the lower part of C1
1098 // C1 fits in 64 bits
1099 // calculate C* and f*
1100 // C* is actually floor(C*) in this case
1101 // C* and f* need shifting and masking, as shown by
1102 // shiftright128[] and maskhigh128[]
1104 // kx = 10^(-x) = ten2mk64[ind - 1]
1105 // C* = C1 * 10^(-x)
1106 // the approximation of 10^(-x) was rounded up to 54 bits
1107 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1109 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
1110 fstar
.w
[0] = P128
.w
[0];
1111 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1112 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1113 // C* = floor(C*) (logical right shift; C has p decimal digits,
1114 // correct by Property 1)
1115 // n = C* * 10^(e+x)
1117 // shift right C* by Ex-64 = shiftright128[ind]
1118 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1119 Cstar
= Cstar
>> shift
;
1120 // determine inexactness of the rounding of C*
1121 // if (0 < f* < 10^(-x)) then
1122 // the result is exact
1123 // else // if (f* > T*) then
1124 // the result is inexact
1125 if (ind
- 1 <= 2) { // fstar.w[1] is 0
1126 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1127 // ten2mk128trunc[ind -1].w[1] is identical to
1128 // ten2mk128[ind -1].w[1]
1130 } // else the result is exact
1131 } else { // if 3 <= ind - 1 <= 14
1132 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1133 // ten2mk128trunc[ind -1].w[1] is identical to
1134 // ten2mk128[ind -1].w[1]
1136 } // else the result is exact
1139 res
= Cstar
; // the result is positive
1140 } else if (exp
== 0) {
1143 res
= C1
; // the result is positive
1144 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1145 // res = +C * 10^exp (exact)
1146 res
= C1
* ten2k64
[exp
]; // the result is positive
1152 /*****************************************************************************
1153 * BID64_to_uint64_xceil
1154 ****************************************************************************/
1156 #if DECIMAL_CALL_BY_REFERENCE
1158 bid64_to_uint64_xceil (UINT64
* pres
, UINT64
* px
1159 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1164 bid64_to_uint64_xceil (UINT64 x
1165 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1171 int exp
; // unbiased exponent
1172 // Note: C1 represents x_significand (UINT64)
1173 BID_UI64DOUBLE tmp1
;
1174 unsigned int x_nr_bits
;
1178 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1182 // check for NaN or Infinity
1183 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1185 *pfpsf
|= INVALID_EXCEPTION
;
1186 // return Integer Indefinite
1187 res
= 0x8000000000000000ull
;
1191 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1192 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1193 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1194 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1195 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1196 if (C1
> 9999999999999999ull) { // non-canonical
1201 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1202 C1
= x
& MASK_BINARY_SIG1
;
1205 // check for zeros (possibly from non-canonical values)
1208 res
= 0x0000000000000000ull
;
1211 // x is not special and is not zero
1213 // q = nr. of decimal digits in x (1 <= q <= 54)
1214 // determine first the nr. of bits in x
1215 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1216 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1217 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1218 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1220 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1221 } else { // x < 2^32
1222 tmp1
.d
= (double) C1
; // exact conversion
1224 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1226 } else { // if x < 2^53
1227 tmp1
.d
= (double) C1
; // exact conversion
1229 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1231 q
= nr_digits
[x_nr_bits
- 1].digits
;
1233 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1234 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1237 exp
= x_exp
- 398; // unbiased exponent
1239 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
1241 *pfpsf
|= INVALID_EXCEPTION
;
1242 // return Integer Indefinite
1243 res
= 0x8000000000000000ull
;
1245 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
1246 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1247 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
1248 // the cases that do not fit are identified here; the ones that fit
1249 // fall through and will be handled with other cases further,
1250 // under '1 <= q + exp <= 20'
1251 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1
1252 // => set invalid flag
1253 *pfpsf
|= INVALID_EXCEPTION
;
1254 // return Integer Indefinite
1255 res
= 0x8000000000000000ull
;
1257 } else { // if n > 0 and q + exp = 20
1258 // if n > 2^64 - 1 then n is too large
1259 // <=> c(0)c(1)...c(19).c(20)...c(q-1) > 2^64 - 1
1260 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 > 2^64 - 1
1261 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65 - 2)
1262 // <=> C * 10^(21-q) > 0x9fffffffffffffff6, 1<=q<=16
1264 // C * 10^20 > 0x9fffffffffffffff6
1265 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
1266 if (C
.w
[1] > 0x09 ||
1267 (C
.w
[1] == 0x09 && C
.w
[0] > 0xfffffffffffffff6ull
)) {
1269 *pfpsf
|= INVALID_EXCEPTION
;
1270 // return Integer Indefinite
1271 res
= 0x8000000000000000ull
;
1274 // else cases that can be rounded to a 64-bit int fall through
1275 // to '1 <= q + exp <= 20'
1276 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
1277 // Note: C * 10^(21-q) has 20 or 21 digits; 0x9fffffffffffffff6
1279 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
1280 if (C
.w
[1] > 0x09 ||
1281 (C
.w
[1] == 0x09 && C
.w
[0] > 0xfffffffffffffff6ull
)) {
1283 *pfpsf
|= INVALID_EXCEPTION
;
1284 // return Integer Indefinite
1285 res
= 0x8000000000000000ull
;
1288 // else cases that can be rounded to a 64-bit int fall through
1289 // to '1 <= q + exp <= 20'
1293 // n is not too large to be converted to int64 if -1 < n < 2^64
1294 // Note: some of the cases tested for above fall through to this point
1295 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1297 *pfpsf
|= INEXACT_EXCEPTION
;
1300 res
= 0x0000000000000000ull
;
1302 res
= 0x0000000000000001ull
;
1304 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
1305 // x <= -1 or 1 <= x <= 2^64 - 1 so if positive x can be rounded
1306 // to nearest to a 64-bit unsigned signed integer
1307 if (x_sign
) { // x <= -1
1309 *pfpsf
|= INVALID_EXCEPTION
;
1310 // return Integer Indefinite
1311 res
= 0x8000000000000000ull
;
1314 // 1 <= x <= 2^64 - 1 so x can be rounded
1315 // to nearest to a 64-bit unsigned integer
1316 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
1317 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1318 // chop off ind digits from the lower part of C1
1319 // C1 fits in 64 bits
1320 // calculate C* and f*
1321 // C* is actually floor(C*) in this case
1322 // C* and f* need shifting and masking, as shown by
1323 // shiftright128[] and maskhigh128[]
1325 // kx = 10^(-x) = ten2mk64[ind - 1]
1326 // C* = C1 * 10^(-x)
1327 // the approximation of 10^(-x) was rounded up to 54 bits
1328 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1330 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
1331 fstar
.w
[0] = P128
.w
[0];
1332 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1333 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1334 // C* = floor(C*) (logical right shift; C has p decimal digits,
1335 // correct by Property 1)
1336 // n = C* * 10^(e+x)
1338 // shift right C* by Ex-64 = shiftright128[ind]
1339 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1340 Cstar
= Cstar
>> shift
;
1341 // determine inexactness of the rounding of C*
1342 // if (0 < f* < 10^(-x)) then
1343 // the result is exact
1344 // else // if (f* > T*) then
1345 // the result is inexact
1346 if (ind
- 1 <= 2) { // fstar.w[1] is 0
1347 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1348 // ten2mk128trunc[ind -1].w[1] is identical to
1349 // ten2mk128[ind -1].w[1]
1351 // set the inexact flag
1352 *pfpsf
|= INEXACT_EXCEPTION
;
1353 } // else the result is exact
1354 } else { // if 3 <= ind - 1 <= 14
1355 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1356 // ten2mk128trunc[ind -1].w[1] is identical to
1357 // ten2mk128[ind -1].w[1]
1359 // set the inexact flag
1360 *pfpsf
|= INEXACT_EXCEPTION
;
1361 } // else the result is exact
1364 res
= Cstar
; // the result is positive
1365 } else if (exp
== 0) {
1368 res
= C1
; // the result is positive
1369 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1370 // res = +C * 10^exp (exact)
1371 res
= C1
* ten2k64
[exp
]; // the result is positive
1377 /*****************************************************************************
1378 * BID64_to_uint64_int
1379 ****************************************************************************/
1381 #if DECIMAL_CALL_BY_REFERENCE
1383 bid64_to_uint64_int (UINT64
* pres
, UINT64
* px
1384 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM
)
1389 bid64_to_uint64_int (UINT64 x
1390 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM
)
1396 int exp
; // unbiased exponent
1397 // Note: C1 represents x_significand (UINT64)
1398 BID_UI64DOUBLE tmp1
;
1399 unsigned int x_nr_bits
;
1403 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1406 // check for NaN or Infinity
1407 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1409 *pfpsf
|= INVALID_EXCEPTION
;
1410 // return Integer Indefinite
1411 res
= 0x8000000000000000ull
;
1415 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1416 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1417 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1418 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1419 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1420 if (C1
> 9999999999999999ull) { // non-canonical
1425 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1426 C1
= x
& MASK_BINARY_SIG1
;
1429 // check for zeros (possibly from non-canonical values)
1432 res
= 0x0000000000000000ull
;
1435 // x is not special and is not zero
1437 // q = nr. of decimal digits in x (1 <= q <= 54)
1438 // determine first the nr. of bits in x
1439 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1440 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1441 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1442 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1444 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1445 } else { // x < 2^32
1446 tmp1
.d
= (double) C1
; // exact conversion
1448 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1450 } else { // if x < 2^53
1451 tmp1
.d
= (double) C1
; // exact conversion
1453 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1455 q
= nr_digits
[x_nr_bits
- 1].digits
;
1457 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1458 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1461 exp
= x_exp
- 398; // unbiased exponent
1463 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
1465 *pfpsf
|= INVALID_EXCEPTION
;
1466 // return Integer Indefinite
1467 res
= 0x8000000000000000ull
;
1469 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
1470 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1471 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
1472 // the cases that do not fit are identified here; the ones that fit
1473 // fall through and will be handled with other cases further,
1474 // under '1 <= q + exp <= 20'
1475 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1
1476 // => set invalid flag
1477 *pfpsf
|= INVALID_EXCEPTION
;
1478 // return Integer Indefinite
1479 res
= 0x8000000000000000ull
;
1481 } else { // if n > 0 and q + exp = 20
1482 // if n >= 2^64 then n is too large
1483 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64
1484 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64
1485 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65)
1486 // <=> C * 10^(21-q) >= 0xa0000000000000000, 1<=q<=16
1488 // C * 10^20 >= 0xa0000000000000000
1489 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
1490 if (C
.w
[1] >= 0x0a) {
1491 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
1493 *pfpsf
|= INVALID_EXCEPTION
;
1494 // return Integer Indefinite
1495 res
= 0x8000000000000000ull
;
1498 // else cases that can be rounded to a 64-bit int fall through
1499 // to '1 <= q + exp <= 20'
1500 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
1501 // Note: C * 10^(21-q) has 20 or 21 digits; 0xa0000000000000000
1503 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
1504 if (C
.w
[1] >= 0x0a) {
1505 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
1507 *pfpsf
|= INVALID_EXCEPTION
;
1508 // return Integer Indefinite
1509 res
= 0x8000000000000000ull
;
1512 // else cases that can be rounded to a 64-bit int fall through
1513 // to '1 <= q + exp <= 20'
1517 // n is not too large to be converted to int64 if -1 < n < 2^64
1518 // Note: some of the cases tested for above fall through to this point
1519 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1521 res
= 0x0000000000000000ull
;
1523 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
1524 // x <= -1 or 1 <= x < 2^64 so if positive x can be rounded
1525 // to nearest to a 64-bit unsigned signed integer
1526 if (x_sign
) { // x <= -1
1528 *pfpsf
|= INVALID_EXCEPTION
;
1529 // return Integer Indefinite
1530 res
= 0x8000000000000000ull
;
1533 // 1 <= x < 2^64 so x can be rounded
1534 // to nearest to a 64-bit unsigned integer
1535 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
1536 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1537 // chop off ind digits from the lower part of C1
1538 // C1 fits in 64 bits
1539 // calculate C* and f*
1540 // C* is actually floor(C*) in this case
1541 // C* and f* need shifting and masking, as shown by
1542 // shiftright128[] and maskhigh128[]
1544 // kx = 10^(-x) = ten2mk64[ind - 1]
1545 // C* = C1 * 10^(-x)
1546 // the approximation of 10^(-x) was rounded up to 54 bits
1547 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1549 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1550 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1551 // C* = floor(C*) (logical right shift; C has p decimal digits,
1552 // correct by Property 1)
1553 // n = C* * 10^(e+x)
1555 // shift right C* by Ex-64 = shiftright128[ind]
1556 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1557 Cstar
= Cstar
>> shift
;
1558 res
= Cstar
; // the result is positive
1559 } else if (exp
== 0) {
1562 res
= C1
; // the result is positive
1563 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1564 // res = +C * 10^exp (exact)
1565 res
= C1
* ten2k64
[exp
]; // the result is positive
1571 /*****************************************************************************
1572 * BID64_to_uint64_xint
1573 ****************************************************************************/
1575 #if DECIMAL_CALL_BY_REFERENCE
1577 bid64_to_uint64_xint (UINT64
* pres
, UINT64
* px
1578 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1583 bid64_to_uint64_xint (UINT64 x
1584 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1590 int exp
; // unbiased exponent
1591 // Note: C1 represents x_significand (UINT64)
1592 BID_UI64DOUBLE tmp1
;
1593 unsigned int x_nr_bits
;
1597 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1601 // check for NaN or Infinity
1602 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1604 *pfpsf
|= INVALID_EXCEPTION
;
1605 // return Integer Indefinite
1606 res
= 0x8000000000000000ull
;
1610 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1611 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1612 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1613 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1614 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1615 if (C1
> 9999999999999999ull) { // non-canonical
1620 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1621 C1
= x
& MASK_BINARY_SIG1
;
1624 // check for zeros (possibly from non-canonical values)
1627 res
= 0x0000000000000000ull
;
1630 // x is not special and is not zero
1632 // q = nr. of decimal digits in x (1 <= q <= 54)
1633 // determine first the nr. of bits in x
1634 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1635 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1636 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1637 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1639 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1640 } else { // x < 2^32
1641 tmp1
.d
= (double) C1
; // exact conversion
1643 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1645 } else { // if x < 2^53
1646 tmp1
.d
= (double) C1
; // exact conversion
1648 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1650 q
= nr_digits
[x_nr_bits
- 1].digits
;
1652 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1653 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1656 exp
= x_exp
- 398; // unbiased exponent
1658 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
1660 *pfpsf
|= INVALID_EXCEPTION
;
1661 // return Integer Indefinite
1662 res
= 0x8000000000000000ull
;
1664 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
1665 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1666 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
1667 // the cases that do not fit are identified here; the ones that fit
1668 // fall through and will be handled with other cases further,
1669 // under '1 <= q + exp <= 20'
1670 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1
1671 // => set invalid flag
1672 *pfpsf
|= INVALID_EXCEPTION
;
1673 // return Integer Indefinite
1674 res
= 0x8000000000000000ull
;
1676 } else { // if n > 0 and q + exp = 20
1677 // if n >= 2^64 then n is too large
1678 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64
1679 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64
1680 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65)
1681 // <=> C * 10^(21-q) >= 0xa0000000000000000, 1<=q<=16
1683 // C * 10^20 >= 0xa0000000000000000
1684 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
1685 if (C
.w
[1] >= 0x0a) {
1686 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
1688 *pfpsf
|= INVALID_EXCEPTION
;
1689 // return Integer Indefinite
1690 res
= 0x8000000000000000ull
;
1693 // else cases that can be rounded to a 64-bit int fall through
1694 // to '1 <= q + exp <= 20'
1695 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
1696 // Note: C * 10^(21-q) has 20 or 21 digits; 0xa0000000000000000
1698 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
1699 if (C
.w
[1] >= 0x0a) {
1700 // actually C.w[1] == 0x0a && C.w[0] >= 0x0000000000000000ull) {
1702 *pfpsf
|= INVALID_EXCEPTION
;
1703 // return Integer Indefinite
1704 res
= 0x8000000000000000ull
;
1707 // else cases that can be rounded to a 64-bit int fall through
1708 // to '1 <= q + exp <= 20'
1712 // n is not too large to be converted to int64 if -1 < n < 2^64
1713 // Note: some of the cases tested for above fall through to this point
1714 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1716 *pfpsf
|= INEXACT_EXCEPTION
;
1718 res
= 0x0000000000000000ull
;
1720 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
1721 // x <= -1 or 1 <= x < 2^64 so if positive x can be rounded
1722 // to nearest to a 64-bit unsigned signed integer
1723 if (x_sign
) { // x <= -1
1725 *pfpsf
|= INVALID_EXCEPTION
;
1726 // return Integer Indefinite
1727 res
= 0x8000000000000000ull
;
1730 // 1 <= x < 2^64 so x can be rounded
1731 // to nearest to a 64-bit unsigned integer
1732 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
1733 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1734 // chop off ind digits from the lower part of C1
1735 // C1 fits in 64 bits
1736 // calculate C* and f*
1737 // C* is actually floor(C*) in this case
1738 // C* and f* need shifting and masking, as shown by
1739 // shiftright128[] and maskhigh128[]
1741 // kx = 10^(-x) = ten2mk64[ind - 1]
1742 // C* = C1 * 10^(-x)
1743 // the approximation of 10^(-x) was rounded up to 54 bits
1744 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1746 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
1747 fstar
.w
[0] = P128
.w
[0];
1748 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1749 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1750 // C* = floor(C*) (logical right shift; C has p decimal digits,
1751 // correct by Property 1)
1752 // n = C* * 10^(e+x)
1754 // shift right C* by Ex-64 = shiftright128[ind]
1755 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1756 Cstar
= Cstar
>> shift
;
1757 // determine inexactness of the rounding of C*
1758 // if (0 < f* < 10^(-x)) then
1759 // the result is exact
1760 // else // if (f* > T*) then
1761 // the result is inexact
1762 if (ind
- 1 <= 2) { // fstar.w[1] is 0
1763 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1764 // ten2mk128trunc[ind -1].w[1] is identical to
1765 // ten2mk128[ind -1].w[1]
1766 // set the inexact flag
1767 *pfpsf
|= INEXACT_EXCEPTION
;
1768 } // else the result is exact
1769 } else { // if 3 <= ind - 1 <= 14
1770 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1771 // ten2mk128trunc[ind -1].w[1] is identical to
1772 // ten2mk128[ind -1].w[1]
1773 // set the inexact flag
1774 *pfpsf
|= INEXACT_EXCEPTION
;
1775 } // else the result is exact
1778 res
= Cstar
; // the result is positive
1779 } else if (exp
== 0) {
1782 res
= C1
; // the result is positive
1783 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1784 // res = +C * 10^exp (exact)
1785 res
= C1
* ten2k64
[exp
]; // the result is positive
1791 /*****************************************************************************
1792 * BID64_to_uint64_rninta
1793 ****************************************************************************/
1795 #if DECIMAL_CALL_BY_REFERENCE
1797 bid64_to_uint64_rninta (UINT64
* pres
, UINT64
* px
1798 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1803 bid64_to_uint64_rninta (UINT64 x
1804 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1810 int exp
; // unbiased exponent
1811 // Note: C1 represents x_significand (UINT64)
1812 BID_UI64DOUBLE tmp1
;
1813 unsigned int x_nr_bits
;
1817 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1820 // check for NaN or Infinity
1821 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1823 *pfpsf
|= INVALID_EXCEPTION
;
1824 // return Integer Indefinite
1825 res
= 0x8000000000000000ull
;
1829 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1830 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1831 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1832 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1833 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1834 if (C1
> 9999999999999999ull) { // non-canonical
1839 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1840 C1
= x
& MASK_BINARY_SIG1
;
1843 // check for zeros (possibly from non-canonical values)
1846 res
= 0x0000000000000000ull
;
1849 // x is not special and is not zero
1851 // q = nr. of decimal digits in x (1 <= q <= 54)
1852 // determine first the nr. of bits in x
1853 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1854 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1855 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1856 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1858 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1859 } else { // x < 2^32
1860 tmp1
.d
= (double) C1
; // exact conversion
1862 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1864 } else { // if x < 2^53
1865 tmp1
.d
= (double) C1
; // exact conversion
1867 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1869 q
= nr_digits
[x_nr_bits
- 1].digits
;
1871 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1872 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1875 exp
= x_exp
- 398; // unbiased exponent
1877 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
1879 *pfpsf
|= INVALID_EXCEPTION
;
1880 // return Integer Indefinite
1881 res
= 0x8000000000000000ull
;
1883 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
1884 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1885 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
1886 // the cases that do not fit are identified here; the ones that fit
1887 // fall through and will be handled with other cases further,
1888 // under '1 <= q + exp <= 20'
1889 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1/2
1890 // => set invalid flag
1891 *pfpsf
|= INVALID_EXCEPTION
;
1892 // return Integer Indefinite
1893 res
= 0x8000000000000000ull
;
1895 } else { // if n > 0 and q + exp = 20
1896 // if n >= 2^64 - 1/2 then n is too large
1897 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64-1/2
1898 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64-1/2
1899 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65-1)
1900 // <=> C * 10^(21-q) >= 0x9fffffffffffffffb, 1<=q<=16
1902 // C * 10^20 >= 0x9fffffffffffffffb
1903 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
1904 if (C
.w
[1] > 0x09 ||
1905 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
1907 *pfpsf
|= INVALID_EXCEPTION
;
1908 // return Integer Indefinite
1909 res
= 0x8000000000000000ull
;
1912 // else cases that can be rounded to a 64-bit int fall through
1913 // to '1 <= q + exp <= 20'
1914 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
1915 // Note: C * 10^(21-q) has 20 or 21 digits; 0x9fffffffffffffffb
1917 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
1918 if (C
.w
[1] > 0x09 ||
1919 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
1921 *pfpsf
|= INVALID_EXCEPTION
;
1922 // return Integer Indefinite
1923 res
= 0x8000000000000000ull
;
1926 // else cases that can be rounded to a 64-bit int fall through
1927 // to '1 <= q + exp <= 20'
1931 // n is not too large to be converted to int64 if -1/2 <= n < 2^64 - 1/2
1932 // Note: some of the cases tested for above fall through to this point
1933 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
1935 res
= 0x0000000000000000ull
;
1937 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
1938 // if 0.c(0)c(1)...c(q-1) < 0.5 <=> c(0)c(1)...c(q-1) < 5 * 10^(q-1)
1944 ind
= q
- 1; // 0 <= ind <= 15
1945 if (C1
< midpoint64
[ind
]) {
1946 res
= 0x0000000000000000ull
; // return 0
1947 } else if (!x_sign
) { // n > 0
1948 res
= 0x0000000000000001ull
; // return +1
1949 } else { // if n < 0
1950 res
= 0x8000000000000000ull
;
1951 *pfpsf
|= INVALID_EXCEPTION
;
1954 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
1955 // x <= -1 or 1 <= x < 2^64-1/2 so if positive x can be rounded
1956 // to nearest to a 64-bit unsigned signed integer
1957 if (x_sign
) { // x <= -1
1959 *pfpsf
|= INVALID_EXCEPTION
;
1960 // return Integer Indefinite
1961 res
= 0x8000000000000000ull
;
1964 // 1 <= x < 2^64-1/2 so x can be rounded
1965 // to nearest to a 64-bit unsigned integer
1966 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
1967 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1968 // chop off ind digits from the lower part of C1
1969 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
1970 C1
= C1
+ midpoint64
[ind
- 1];
1971 // calculate C* and f*
1972 // C* is actually floor(C*) in this case
1973 // C* and f* need shifting and masking, as shown by
1974 // shiftright128[] and maskhigh128[]
1976 // kx = 10^(-x) = ten2mk64[ind - 1]
1977 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
1978 // the approximation of 10^(-x) was rounded up to 54 bits
1979 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1981 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1982 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1983 // if (0 < f* < 10^(-x)) then the result is a midpoint
1984 // if floor(C*) is even then C* = floor(C*) - logical right
1985 // shift; C* has p decimal digits, correct by Prop. 1)
1986 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
1987 // shift; C* has p decimal digits, correct by Pr. 1)
1989 // C* = floor(C*) (logical right shift; C has p decimal digits,
1990 // correct by Property 1)
1991 // n = C* * 10^(e+x)
1993 // shift right C* by Ex-64 = shiftright128[ind]
1994 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1995 Cstar
= Cstar
>> shift
;
1997 // if the result was a midpoint it was rounded away from zero
1998 res
= Cstar
; // the result is positive
1999 } else if (exp
== 0) {
2002 res
= C1
; // the result is positive
2003 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2004 // res = +C * 10^exp (exact)
2005 res
= C1
* ten2k64
[exp
]; // the result is positive
2011 /*****************************************************************************
2012 * BID64_to_uint64_xrninta
2013 ****************************************************************************/
2015 #if DECIMAL_CALL_BY_REFERENCE
2017 bid64_to_uint64_xrninta (UINT64
* pres
, UINT64
* px
2018 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2023 bid64_to_uint64_xrninta (UINT64 x
2024 _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2030 int exp
; // unbiased exponent
2031 // Note: C1 represents x_significand (UINT64)
2033 BID_UI64DOUBLE tmp1
;
2034 unsigned int x_nr_bits
;
2038 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
2042 // check for NaN or Infinity
2043 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
2045 *pfpsf
|= INVALID_EXCEPTION
;
2046 // return Integer Indefinite
2047 res
= 0x8000000000000000ull
;
2051 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
2052 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2053 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
2054 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
2055 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
2056 if (C1
> 9999999999999999ull) { // non-canonical
2061 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
2062 C1
= x
& MASK_BINARY_SIG1
;
2065 // check for zeros (possibly from non-canonical values)
2068 res
= 0x0000000000000000ull
;
2071 // x is not special and is not zero
2073 // q = nr. of decimal digits in x (1 <= q <= 54)
2074 // determine first the nr. of bits in x
2075 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
2076 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2077 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
2078 tmp1
.d
= (double) (C1
>> 32); // exact conversion
2080 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2081 } else { // x < 2^32
2082 tmp1
.d
= (double) C1
; // exact conversion
2084 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2086 } else { // if x < 2^53
2087 tmp1
.d
= (double) C1
; // exact conversion
2089 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2091 q
= nr_digits
[x_nr_bits
- 1].digits
;
2093 q
= nr_digits
[x_nr_bits
- 1].digits1
;
2094 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
2097 exp
= x_exp
- 398; // unbiased exponent
2099 if ((q
+ exp
) > 20) { // x >= 10^20 ~= 2^66.45... (cannot fit in 64 bits)
2101 *pfpsf
|= INVALID_EXCEPTION
;
2102 // return Integer Indefinite
2103 res
= 0x8000000000000000ull
;
2105 } else if ((q
+ exp
) == 20) { // x = c(0)c(1)...c(19).c(20)...c(q-1)
2106 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
2107 // so x rounded to an integer may or may not fit in an unsigned 64-bit int
2108 // the cases that do not fit are identified here; the ones that fit
2109 // fall through and will be handled with other cases further,
2110 // under '1 <= q + exp <= 20'
2111 if (x_sign
) { // if n < 0 and q + exp = 20 then x is much less than -1/2
2112 // => set invalid flag
2113 *pfpsf
|= INVALID_EXCEPTION
;
2114 // return Integer Indefinite
2115 res
= 0x8000000000000000ull
;
2117 } else { // if n > 0 and q + exp = 20
2118 // if n >= 2^64 - 1/2 then n is too large
2119 // <=> c(0)c(1)...c(19).c(20)...c(q-1) >= 2^64-1/2
2120 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^20 >= 2^64-1/2
2121 // <=> 0.c(0)c(1)...c(19)c(20)...c(q-1) * 10^21 >= 5*(2^65-1)
2122 // <=> C * 10^(21-q) >= 0x9fffffffffffffffb, 1<=q<=16
2124 // C * 10^20 >= 0x9fffffffffffffffb
2125 __mul_128x64_to_128 (C
, C1
, ten2k128
[0]); // 10^20 * C
2126 if (C
.w
[1] > 0x09 ||
2127 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
2129 *pfpsf
|= INVALID_EXCEPTION
;
2130 // return Integer Indefinite
2131 res
= 0x8000000000000000ull
;
2134 // else cases that can be rounded to a 64-bit int fall through
2135 // to '1 <= q + exp <= 20'
2136 } else { // if (2 <= q <= 16) => 5 <= 21 - q <= 19
2137 // Note: C * 10^(21-q) has 20 or 21 digits; 0x9fffffffffffffffb
2139 __mul_64x64_to_128MACH (C
, C1
, ten2k64
[21 - q
]);
2140 if (C
.w
[1] > 0x09 ||
2141 (C
.w
[1] == 0x09 && C
.w
[0] >= 0xfffffffffffffffbull
)) {
2143 *pfpsf
|= INVALID_EXCEPTION
;
2144 // return Integer Indefinite
2145 res
= 0x8000000000000000ull
;
2148 // else cases that can be rounded to a 64-bit int fall through
2149 // to '1 <= q + exp <= 20'
2153 // n is not too large to be converted to int64 if -1/2 <= n < 2^64 - 1/2
2154 // Note: some of the cases tested for above fall through to this point
2155 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2157 *pfpsf
|= INEXACT_EXCEPTION
;
2159 res
= 0x0000000000000000ull
;
2161 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2162 // if 0.c(0)c(1)...c(q-1) < 0.5 <=> c(0)c(1)...c(q-1) < 5 * 10^(q-1)
2168 ind
= q
- 1; // 0 <= ind <= 15
2169 if (C1
< midpoint64
[ind
]) {
2170 res
= 0x0000000000000000ull
; // return 0
2171 } else if (!x_sign
) { // n > 0
2172 res
= 0x0000000000000001ull
; // return +1
2173 } else { // if n < 0
2174 res
= 0x8000000000000000ull
;
2175 *pfpsf
|= INVALID_EXCEPTION
;
2179 *pfpsf
|= INEXACT_EXCEPTION
;
2180 } else { // if (1 <= q + exp <= 20, 1 <= q <= 16, -15 <= exp <= 19)
2181 // x <= -1 or 1 <= x < 2^64-1/2 so if positive x can be rounded
2182 // to nearest to a 64-bit unsigned signed integer
2183 if (x_sign
) { // x <= -1
2185 *pfpsf
|= INVALID_EXCEPTION
;
2186 // return Integer Indefinite
2187 res
= 0x8000000000000000ull
;
2190 // 1 <= x < 2^64-1/2 so x can be rounded
2191 // to nearest to a 64-bit unsigned integer
2192 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 20
2193 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2194 // chop off ind digits from the lower part of C1
2195 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2196 C1
= C1
+ midpoint64
[ind
- 1];
2197 // calculate C* and f*
2198 // C* is actually floor(C*) in this case
2199 // C* and f* need shifting and masking, as shown by
2200 // shiftright128[] and maskhigh128[]
2202 // kx = 10^(-x) = ten2mk64[ind - 1]
2203 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2204 // the approximation of 10^(-x) was rounded up to 54 bits
2205 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
2207 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
2208 fstar
.w
[0] = P128
.w
[0];
2209 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
2210 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
2211 // if (0 < f* < 10^(-x)) then the result is a midpoint
2212 // if floor(C*) is even then C* = floor(C*) - logical right
2213 // shift; C* has p decimal digits, correct by Prop. 1)
2214 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
2215 // shift; C* has p decimal digits, correct by Pr. 1)
2217 // C* = floor(C*) (logical right shift; C has p decimal digits,
2218 // correct by Property 1)
2219 // n = C* * 10^(e+x)
2221 // shift right C* by Ex-64 = shiftright128[ind]
2222 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
2223 Cstar
= Cstar
>> shift
;
2224 // determine inexactness of the rounding of C*
2225 // if (0 < f* - 1/2 < 10^(-x)) then
2226 // the result is exact
2227 // else // if (f* - 1/2 > T*) then
2228 // the result is inexact
2229 if (ind
- 1 <= 2) { // fstar.w[1] is 0
2230 if (fstar
.w
[0] > 0x8000000000000000ull
) {
2231 // f* > 1/2 and the result may be exact
2232 tmp64
= fstar
.w
[0] - 0x8000000000000000ull
; // f* - 1/2
2233 if ((tmp64
> ten2mk128trunc
[ind
- 1].w
[1])) {
2234 // ten2mk128trunc[ind -1].w[1] is identical to
2235 // ten2mk128[ind -1].w[1]
2236 // set the inexact flag
2237 *pfpsf
|= INEXACT_EXCEPTION
;
2238 } // else the result is exact
2239 } else { // the result is inexact; f2* <= 1/2
2240 // set the inexact flag
2241 *pfpsf
|= INEXACT_EXCEPTION
;
2243 } else { // if 3 <= ind - 1 <= 14
2244 if (fstar
.w
[1] > onehalf128
[ind
- 1] ||
2245 (fstar
.w
[1] == onehalf128
[ind
- 1] && fstar
.w
[0])) {
2246 // f2* > 1/2 and the result may be exact
2247 // Calculate f2* - 1/2
2248 tmp64
= fstar
.w
[1] - onehalf128
[ind
- 1];
2249 if (tmp64
|| fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
2250 // ten2mk128trunc[ind -1].w[1] is identical to
2251 // ten2mk128[ind -1].w[1]
2252 // set the inexact flag
2253 *pfpsf
|= INEXACT_EXCEPTION
;
2254 } // else the result is exact
2255 } else { // the result is inexact; f2* <= 1/2
2256 // set the inexact flag
2257 *pfpsf
|= INEXACT_EXCEPTION
;
2261 // if the result was a midpoint it was rounded away from zero
2262 res
= Cstar
; // the result is positive
2263 } else if (exp
== 0) {
2266 res
= C1
; // the result is positive
2267 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2268 // res = +C * 10^exp (exact)
2269 res
= C1
* ten2k64
[exp
]; // the result is positive