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 #include "bid_internal.h"
31 /*****************************************************************************
32 * BID64_to_int32_rnint
33 ****************************************************************************/
35 #if DECIMAL_CALL_BY_REFERENCE
37 __bid64_to_int32_rnint (int *pres
,
39 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
44 __bid64_to_int32_rnint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
50 int exp
; // unbiased exponent
51 // Note: C1 represents x_significand (UINT64)
54 unsigned int x_nr_bits
;
57 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
61 // check for NaN or Infinity
62 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
64 *pfpsf
|= INVALID_EXCEPTION
;
65 // return Integer Indefinite
70 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
71 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
72 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
73 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
74 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
75 if (C1
> 9999999999999999ull) { // non-canonical
80 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
81 C1
= x
& MASK_BINARY_SIG1
;
84 // check for zeros (possibly from non-canonical values)
90 // x is not special and is not zero
92 // q = nr. of decimal digits in x (1 <= q <= 54)
93 // determine first the nr. of bits in x
94 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
95 // split the 64-bit value in two 32-bit halves to avoid rounding errors
96 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
97 tmp1
.d
= (double) (C1
>> 32); // exact conversion
99 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
101 tmp1
.d
= (double) C1
; // exact conversion
103 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
105 } else { // if x < 2^53
106 tmp1
.d
= (double) C1
; // exact conversion
108 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
110 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
112 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
113 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
116 exp
= x_exp
- 398; // unbiased exponent
118 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
120 *pfpsf
|= INVALID_EXCEPTION
;
121 // return Integer Indefinite
124 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
125 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
126 // so x rounded to an integer may or may not fit in a signed 32-bit int
127 // the cases that do not fit are identified here; the ones that fit
128 // fall through and will be handled with other cases further,
129 // under '1 <= q + exp <= 10'
130 if (x_sign
) { // if n < 0 and q + exp = 10
131 // if n < -2^31 - 1/2 then n is too large
132 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31+1/2
133 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000005, 1<=q<=16
134 // <=> C * 10^(11-q) > 0x500000005, 1<=q<=16
136 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
137 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
138 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
139 if (tmp64
> 0x500000005ull
) {
141 *pfpsf
|= INVALID_EXCEPTION
;
142 // return Integer Indefinite
146 // else cases that can be rounded to a 32-bit int fall through
147 // to '1 <= q + exp <= 10'
148 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
149 // C * 10^(11-q) > 0x500000005 <=>
150 // C > 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
151 // (scale 2^31+1/2 up)
152 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
153 tmp64
= 0x500000005ull
* __bid_ten2k64
[q
- 11];
156 *pfpsf
|= INVALID_EXCEPTION
;
157 // return Integer Indefinite
161 // else cases that can be rounded to a 32-bit int fall through
162 // to '1 <= q + exp <= 10'
164 } else { // if n > 0 and q + exp = 10
165 // if n >= 2^31 - 1/2 then n is too large
166 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
167 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
168 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
170 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
171 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
172 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
173 if (tmp64
>= 0x4fffffffbull
) {
175 *pfpsf
|= INVALID_EXCEPTION
;
176 // return Integer Indefinite
180 // else cases that can be rounded to a 32-bit int fall through
181 // to '1 <= q + exp <= 10'
182 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
183 // C * 10^(11-q) >= 0x4fffffffb <=>
184 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
185 // (scale 2^31-1/2 up)
186 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
187 tmp64
= 0x4fffffffbull
* __bid_ten2k64
[q
- 11];
190 *pfpsf
|= INVALID_EXCEPTION
;
191 // return Integer Indefinite
195 // else cases that can be rounded to a 32-bit int fall through
196 // to '1 <= q + exp <= 10'
200 // n is not too large to be converted to int32: -2^31 - 1/2 <= n < 2^31 - 1/2
201 // Note: some of the cases tested for above fall through to this point
202 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
206 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
207 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
212 if (C1
<= __bid_midpoint64
[ind
]) {
213 res
= 0x00000000; // return 0
214 } else if (x_sign
) { // n < 0
215 res
= 0xffffffff; // return -1
217 res
= 0x00000001; // return +1
219 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
220 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
221 // to nearest to a 32-bit signed integer
222 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
223 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
224 // chop off ind digits from the lower part of C1
225 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
226 C1
= C1
+ __bid_midpoint64
[ind
- 1];
227 // calculate C* and f*
228 // C* is actually floor(C*) in this case
229 // C* and f* need shifting and masking, as shown by
230 // __bid_shiftright128[] and __bid_maskhigh128[]
232 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
233 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
234 // the approximation of 10^(-x) was rounded up to 54 bits
235 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
237 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
238 fstar
.w
[0] = P128
.w
[0];
239 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
240 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
241 // if (0 < f* < 10^(-x)) then the result is a midpoint
242 // if floor(C*) is even then C* = floor(C*) - logical right
243 // shift; C* has p decimal digits, correct by Prop. 1)
244 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
245 // shift; C* has p decimal digits, correct by Pr. 1)
247 // C* = floor(C*) (logical right shift; C has p decimal digits,
248 // correct by Property 1)
251 // shift right C* by Ex-64 = __bid_shiftright128[ind]
252 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
253 Cstar
= Cstar
>> shift
;
255 // if the result was a midpoint it was rounded away from zero, so
256 // it will need a correction
257 // check for midpoints
258 if ((fstar
.w
[1] == 0) && fstar
.w
[0]
259 && (fstar
.w
[0] <= __bid_ten2mk128trunc
[ind
- 1].w
[1])) {
260 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
261 // __bid_ten2mk128[ind -1].w[1]
262 // the result is a midpoint; round to nearest
263 if (Cstar
& 0x01) { // Cstar is odd; MP in [EVEN, ODD]
264 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
265 Cstar
--; // Cstar is now even
266 } // else MP in [ODD, EVEN]
272 } else if (exp
== 0) {
274 // res = +/-C (exact)
279 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
280 // res = +/-C * 10^exp (exact)
282 res
= -C1
* __bid_ten2k64
[exp
];
284 res
= C1
* __bid_ten2k64
[exp
];
290 /*****************************************************************************
291 * BID64_to_int32_xrnint
292 ****************************************************************************/
294 #if DECIMAL_CALL_BY_REFERENCE
296 __bid64_to_int32_xrnint (int *pres
,
298 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
303 __bid64_to_int32_xrnint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
309 int exp
; // unbiased exponent
310 // Note: C1 represents x_significand (UINT64)
313 unsigned int x_nr_bits
;
316 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
320 // check for NaN or Infinity
321 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
323 *pfpsf
|= INVALID_EXCEPTION
;
324 // return Integer Indefinite
329 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
330 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
331 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
332 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
333 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
334 if (C1
> 9999999999999999ull) { // non-canonical
339 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
340 C1
= x
& MASK_BINARY_SIG1
;
343 // check for zeros (possibly from non-canonical values)
349 // x is not special and is not zero
351 // q = nr. of decimal digits in x (1 <= q <= 54)
352 // determine first the nr. of bits in x
353 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
354 // split the 64-bit value in two 32-bit halves to avoid rounding errors
355 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
356 tmp1
.d
= (double) (C1
>> 32); // exact conversion
358 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
360 tmp1
.d
= (double) C1
; // exact conversion
362 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
364 } else { // if x < 2^53
365 tmp1
.d
= (double) C1
; // exact conversion
367 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
369 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
371 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
372 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
375 exp
= x_exp
- 398; // unbiased exponent
377 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
379 *pfpsf
|= INVALID_EXCEPTION
;
380 // return Integer Indefinite
383 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
384 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
385 // so x rounded to an integer may or may not fit in a signed 32-bit int
386 // the cases that do not fit are identified here; the ones that fit
387 // fall through and will be handled with other cases further,
388 // under '1 <= q + exp <= 10'
389 if (x_sign
) { // if n < 0 and q + exp = 10
390 // if n < -2^31 - 1/2 then n is too large
391 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31+1/2
392 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000005, 1<=q<=16
393 // <=> C * 10^(11-q) > 0x500000005, 1<=q<=16
395 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
396 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
397 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
398 if (tmp64
> 0x500000005ull
) {
400 *pfpsf
|= INVALID_EXCEPTION
;
401 // return Integer Indefinite
405 // else cases that can be rounded to a 32-bit int fall through
406 // to '1 <= q + exp <= 10'
407 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
408 // C * 10^(11-q) > 0x500000005 <=>
409 // C > 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
410 // (scale 2^31+1/2 up)
411 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
412 tmp64
= 0x500000005ull
* __bid_ten2k64
[q
- 11];
415 *pfpsf
|= INVALID_EXCEPTION
;
416 // return Integer Indefinite
420 // else cases that can be rounded to a 32-bit int fall through
421 // to '1 <= q + exp <= 10'
423 } else { // if n > 0 and q + exp = 10
424 // if n >= 2^31 - 1/2 then n is too large
425 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
426 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
427 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
429 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
430 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
431 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
432 if (tmp64
>= 0x4fffffffbull
) {
434 *pfpsf
|= INVALID_EXCEPTION
;
435 // return Integer Indefinite
439 // else cases that can be rounded to a 32-bit int fall through
440 // to '1 <= q + exp <= 10'
441 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
442 // C * 10^(11-q) >= 0x4fffffffb <=>
443 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
444 // (scale 2^31-1/2 up)
445 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
446 tmp64
= 0x4fffffffbull
* __bid_ten2k64
[q
- 11];
449 *pfpsf
|= INVALID_EXCEPTION
;
450 // return Integer Indefinite
454 // else cases that can be rounded to a 32-bit int fall through
455 // to '1 <= q + exp <= 10'
459 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
460 // Note: some of the cases tested for above fall through to this point
461 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
463 *pfpsf
|= INEXACT_EXCEPTION
;
467 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
468 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
473 if (C1
<= __bid_midpoint64
[ind
]) {
474 res
= 0x00000000; // return 0
475 } else if (x_sign
) { // n < 0
476 res
= 0xffffffff; // return -1
478 res
= 0x00000001; // return +1
481 *pfpsf
|= INEXACT_EXCEPTION
;
482 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
483 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
484 // to nearest to a 32-bit signed integer
485 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
486 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
487 // chop off ind digits from the lower part of C1
488 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
489 C1
= C1
+ __bid_midpoint64
[ind
- 1];
490 // calculate C* and f*
491 // C* is actually floor(C*) in this case
492 // C* and f* need shifting and masking, as shown by
493 // __bid_shiftright128[] and __bid_maskhigh128[]
495 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
496 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
497 // the approximation of 10^(-x) was rounded up to 54 bits
498 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
500 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
501 fstar
.w
[0] = P128
.w
[0];
502 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
503 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
504 // if (0 < f* < 10^(-x)) then the result is a midpoint
505 // if floor(C*) is even then C* = floor(C*) - logical right
506 // shift; C* has p decimal digits, correct by Prop. 1)
507 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
508 // shift; C* has p decimal digits, correct by Pr. 1)
510 // C* = floor(C*) (logical right shift; C has p decimal digits,
511 // correct by Property 1)
514 // shift right C* by Ex-64 = __bid_shiftright128[ind]
515 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
516 Cstar
= Cstar
>> shift
;
517 // determine inexactness of the rounding of C*
518 // if (0 < f* - 1/2 < 10^(-x)) then
519 // the result is exact
520 // else // if (f* - 1/2 > T*) then
521 // the result is inexact
523 if (fstar
.w
[0] > 0x8000000000000000ull
) {
524 // f* > 1/2 and the result may be exact
525 tmp64
= fstar
.w
[0] - 0x8000000000000000ull
; // f* - 1/2
526 if ((tmp64
> __bid_ten2mk128trunc
[ind
- 1].w
[1])) {
527 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
528 // __bid_ten2mk128[ind -1].w[1]
529 // set the inexact flag
530 *pfpsf
|= INEXACT_EXCEPTION
;
531 } // else the result is exact
532 } else { // the result is inexact; f2* <= 1/2
533 // set the inexact flag
534 *pfpsf
|= INEXACT_EXCEPTION
;
536 } else { // if 3 <= ind - 1 <= 14
537 if (fstar
.w
[1] > __bid_one_half128
[ind
- 1]
538 || (fstar
.w
[1] == __bid_one_half128
[ind
- 1]
540 // f2* > 1/2 and the result may be exact
541 // Calculate f2* - 1/2
542 tmp64
= fstar
.w
[1] - __bid_one_half128
[ind
- 1];
543 if (tmp64
|| fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
544 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
545 // __bid_ten2mk128[ind -1].w[1]
546 // set the inexact flag
547 *pfpsf
|= INEXACT_EXCEPTION
;
548 } // else the result is exact
549 } else { // the result is inexact; f2* <= 1/2
550 // set the inexact flag
551 *pfpsf
|= INEXACT_EXCEPTION
;
555 // if the result was a midpoint it was rounded away from zero, so
556 // it will need a correction
557 // check for midpoints
558 if ((fstar
.w
[1] == 0) && fstar
.w
[0]
559 && (fstar
.w
[0] <= __bid_ten2mk128trunc
[ind
- 1].w
[1])) {
560 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
561 // __bid_ten2mk128[ind -1].w[1]
562 // the result is a midpoint; round to nearest
563 if (Cstar
& 0x01) { // Cstar is odd; MP in [EVEN, ODD]
564 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
565 Cstar
--; // Cstar is now even
566 } // else MP in [ODD, EVEN]
572 } else if (exp
== 0) {
574 // res = +/-C (exact)
579 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
580 // res = +/-C * 10^exp (exact)
582 res
= -C1
* __bid_ten2k64
[exp
];
584 res
= C1
* __bid_ten2k64
[exp
];
590 /*****************************************************************************
591 * BID64_to_int32_floor
592 ****************************************************************************/
594 #if DECIMAL_CALL_BY_REFERENCE
596 __bid64_to_int32_floor (int *pres
,
598 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
603 __bid64_to_int32_floor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
609 int exp
; // unbiased exponent
610 // Note: C1 represents x_significand (UINT64)
613 unsigned int x_nr_bits
;
616 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
620 // check for NaN or Infinity
621 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
623 *pfpsf
|= INVALID_EXCEPTION
;
624 // return Integer Indefinite
629 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
630 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
631 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
632 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
633 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
634 if (C1
> 9999999999999999ull) { // non-canonical
639 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
640 C1
= x
& MASK_BINARY_SIG1
;
643 // check for zeros (possibly from non-canonical values)
649 // x is not special and is not zero
651 // q = nr. of decimal digits in x (1 <= q <= 54)
652 // determine first the nr. of bits in x
653 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
654 // split the 64-bit value in two 32-bit halves to avoid rounding errors
655 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
656 tmp1
.d
= (double) (C1
>> 32); // exact conversion
658 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
660 tmp1
.d
= (double) C1
; // exact conversion
662 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
664 } else { // if x < 2^53
665 tmp1
.d
= (double) C1
; // exact conversion
667 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
669 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
671 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
672 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
675 exp
= x_exp
- 398; // unbiased exponent
677 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
679 *pfpsf
|= INVALID_EXCEPTION
;
680 // return Integer Indefinite
683 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
684 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
685 // so x rounded to an integer may or may not fit in a signed 32-bit int
686 // the cases that do not fit are identified here; the ones that fit
687 // fall through and will be handled with other cases further,
688 // under '1 <= q + exp <= 10'
689 if (x_sign
) { // if n < 0 and q + exp = 10
690 // if n < -2^31 then n is too large
691 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
692 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
693 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
695 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
696 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
697 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
698 if (tmp64
> 0x500000000ull
) {
700 *pfpsf
|= INVALID_EXCEPTION
;
701 // return Integer Indefinite
705 // else cases that can be rounded to a 32-bit int fall through
706 // to '1 <= q + exp <= 10'
707 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
708 // C * 10^(11-q) > 0x500000000 <=>
709 // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
711 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
712 tmp64
= 0x500000000ull
* __bid_ten2k64
[q
- 11];
715 *pfpsf
|= INVALID_EXCEPTION
;
716 // return Integer Indefinite
720 // else cases that can be rounded to a 32-bit int fall through
721 // to '1 <= q + exp <= 10'
723 } else { // if n > 0 and q + exp = 10
724 // if n >= 2^31 then n is too large
725 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
726 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
727 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
729 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
730 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
731 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
732 if (tmp64
>= 0x500000000ull
) {
734 *pfpsf
|= INVALID_EXCEPTION
;
735 // return Integer Indefinite
739 // else cases that can be rounded to a 32-bit int fall through
740 // to '1 <= q + exp <= 10'
741 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
742 // C * 10^(11-q) >= 0x500000000 <=>
743 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
745 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
746 tmp64
= 0x500000000ull
* __bid_ten2k64
[q
- 11];
749 *pfpsf
|= INVALID_EXCEPTION
;
750 // return Integer Indefinite
754 // else cases that can be rounded to a 32-bit int fall through
755 // to '1 <= q + exp <= 10'
759 // n is not too large to be converted to int32: -2^31 <= n < 2^31
760 // Note: some of the cases tested for above fall through to this point
761 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
768 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
769 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
770 // to nearest to a 32-bit signed integer
771 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
772 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
773 // chop off ind digits from the lower part of C1
774 // C1 fits in 64 bits
775 // calculate C* and f*
776 // C* is actually floor(C*) in this case
777 // C* and f* need shifting and masking, as shown by
778 // __bid_shiftright128[] and __bid_maskhigh128[]
780 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
782 // the approximation of 10^(-x) was rounded up to 54 bits
783 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
785 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
786 fstar
.w
[0] = P128
.w
[0];
787 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
788 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
789 // C* = floor(C*) (logical right shift; C has p decimal digits,
790 // correct by Property 1)
793 // shift right C* by Ex-64 = __bid_shiftright128[ind]
794 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
795 Cstar
= Cstar
>> shift
;
796 // determine inexactness of the rounding of C*
797 // if (0 < f* < 10^(-x)) then
798 // the result is exact
799 // else // if (f* > T*) then
800 // the result is inexact
802 if (fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
803 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
804 // __bid_ten2mk128[ind -1].w[1]
805 if (x_sign
) { // negative and inexact
808 } // else the result is exact
809 } else { // if 3 <= ind - 1 <= 14
810 if (fstar
.w
[1] || fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
811 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
812 // __bid_ten2mk128[ind -1].w[1]
813 if (x_sign
) { // negative and inexact
816 } // else the result is exact
823 } else if (exp
== 0) {
825 // res = +/-C (exact)
830 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
831 // res = +/-C * 10^exp (exact)
833 res
= -C1
* __bid_ten2k64
[exp
];
835 res
= C1
* __bid_ten2k64
[exp
];
841 /*****************************************************************************
842 * BID64_to_int32_xfloor
843 ****************************************************************************/
845 #if DECIMAL_CALL_BY_REFERENCE
847 __bid64_to_int32_xfloor (int *pres
,
849 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
854 __bid64_to_int32_xfloor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
860 int exp
; // unbiased exponent
861 // Note: C1 represents x_significand (UINT64)
864 unsigned int x_nr_bits
;
867 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
871 // check for NaN or Infinity
872 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
874 *pfpsf
|= INVALID_EXCEPTION
;
875 // return Integer Indefinite
880 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
881 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
882 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
883 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
884 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
885 if (C1
> 9999999999999999ull) { // non-canonical
890 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
891 C1
= x
& MASK_BINARY_SIG1
;
894 // check for zeros (possibly from non-canonical values)
900 // x is not special and is not zero
902 // q = nr. of decimal digits in x (1 <= q <= 54)
903 // determine first the nr. of bits in x
904 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
905 // split the 64-bit value in two 32-bit halves to avoid rounding errors
906 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
907 tmp1
.d
= (double) (C1
>> 32); // exact conversion
909 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
911 tmp1
.d
= (double) C1
; // exact conversion
913 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
915 } else { // if x < 2^53
916 tmp1
.d
= (double) C1
; // exact conversion
918 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
920 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
922 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
923 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
926 exp
= x_exp
- 398; // unbiased exponent
928 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
930 *pfpsf
|= INVALID_EXCEPTION
;
931 // return Integer Indefinite
934 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
935 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
936 // so x rounded to an integer may or may not fit in a signed 32-bit int
937 // the cases that do not fit are identified here; the ones that fit
938 // fall through and will be handled with other cases further,
939 // under '1 <= q + exp <= 10'
940 if (x_sign
) { // if n < 0 and q + exp = 10
941 // if n < -2^31 then n is too large
942 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
943 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
944 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
946 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
947 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
948 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
949 if (tmp64
> 0x500000000ull
) {
951 *pfpsf
|= INVALID_EXCEPTION
;
952 // return Integer Indefinite
956 // else cases that can be rounded to a 32-bit int fall through
957 // to '1 <= q + exp <= 10'
958 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
959 // C * 10^(11-q) > 0x500000000 <=>
960 // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
962 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
963 tmp64
= 0x500000000ull
* __bid_ten2k64
[q
- 11];
966 *pfpsf
|= INVALID_EXCEPTION
;
967 // return Integer Indefinite
971 // else cases that can be rounded to a 32-bit int fall through
972 // to '1 <= q + exp <= 10'
974 } else { // if n > 0 and q + exp = 10
975 // if n >= 2^31 then n is too large
976 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
977 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
978 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
980 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
981 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
982 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
983 if (tmp64
>= 0x500000000ull
) {
985 *pfpsf
|= INVALID_EXCEPTION
;
986 // return Integer Indefinite
990 // else cases that can be rounded to a 32-bit int fall through
991 // to '1 <= q + exp <= 10'
992 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
993 // C * 10^(11-q) >= 0x500000000 <=>
994 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
996 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
997 tmp64
= 0x500000000ull
* __bid_ten2k64
[q
- 11];
1000 *pfpsf
|= INVALID_EXCEPTION
;
1001 // return Integer Indefinite
1005 // else cases that can be rounded to a 32-bit int fall through
1006 // to '1 <= q + exp <= 10'
1010 // n is not too large to be converted to int32: -2^31 <= n < 2^31
1011 // Note: some of the cases tested for above fall through to this point
1012 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1014 *pfpsf
|= INEXACT_EXCEPTION
;
1021 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1022 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
1023 // to nearest to a 32-bit signed integer
1024 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1025 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1026 // chop off ind digits from the lower part of C1
1027 // C1 fits in 64 bits
1028 // calculate C* and f*
1029 // C* is actually floor(C*) in this case
1030 // C* and f* need shifting and masking, as shown by
1031 // __bid_shiftright128[] and __bid_maskhigh128[]
1033 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1034 // C* = C1 * 10^(-x)
1035 // the approximation of 10^(-x) was rounded up to 54 bits
1036 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
1038 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
1039 fstar
.w
[0] = P128
.w
[0];
1040 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1041 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1042 // C* = floor(C*) (logical right shift; C has p decimal digits,
1043 // correct by Property 1)
1044 // n = C* * 10^(e+x)
1046 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1047 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
1048 Cstar
= Cstar
>> shift
;
1049 // determine inexactness of the rounding of C*
1050 // if (0 < f* < 10^(-x)) then
1051 // the result is exact
1052 // else // if (f* > T*) then
1053 // the result is inexact
1055 if (fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
1056 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1057 // __bid_ten2mk128[ind -1].w[1]
1058 if (x_sign
) { // negative and inexact
1061 // set the inexact flag
1062 *pfpsf
|= INEXACT_EXCEPTION
;
1063 } // else the result is exact
1064 } else { // if 3 <= ind - 1 <= 14
1065 if (fstar
.w
[1] || fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
1066 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1067 // __bid_ten2mk128[ind -1].w[1]
1068 if (x_sign
) { // negative and inexact
1071 // set the inexact flag
1072 *pfpsf
|= INEXACT_EXCEPTION
;
1073 } // else the result is exact
1080 } else if (exp
== 0) {
1082 // res = +/-C (exact)
1087 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1088 // res = +/-C * 10^exp (exact)
1090 res
= -C1
* __bid_ten2k64
[exp
];
1092 res
= C1
* __bid_ten2k64
[exp
];
1098 /*****************************************************************************
1099 * BID64_to_int32_ceil
1100 ****************************************************************************/
1102 #if DECIMAL_CALL_BY_REFERENCE
1104 __bid64_to_int32_ceil (int *pres
,
1106 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1111 __bid64_to_int32_ceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1117 int exp
; // unbiased exponent
1118 // Note: C1 represents x_significand (UINT64)
1120 BID_UI64DOUBLE tmp1
;
1121 unsigned int x_nr_bits
;
1124 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1128 // check for NaN or Infinity
1129 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1131 *pfpsf
|= INVALID_EXCEPTION
;
1132 // return Integer Indefinite
1137 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1138 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1139 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1140 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1141 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1142 if (C1
> 9999999999999999ull) { // non-canonical
1147 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1148 C1
= x
& MASK_BINARY_SIG1
;
1151 // check for zeros (possibly from non-canonical values)
1157 // x is not special and is not zero
1159 // q = nr. of decimal digits in x (1 <= q <= 54)
1160 // determine first the nr. of bits in x
1161 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1162 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1163 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1164 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1166 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1167 } else { // x < 2^32
1168 tmp1
.d
= (double) C1
; // exact conversion
1170 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1172 } else { // if x < 2^53
1173 tmp1
.d
= (double) C1
; // exact conversion
1175 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1177 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
1179 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
1180 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
1183 exp
= x_exp
- 398; // unbiased exponent
1185 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1187 *pfpsf
|= INVALID_EXCEPTION
;
1188 // return Integer Indefinite
1191 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1192 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1193 // so x rounded to an integer may or may not fit in a signed 32-bit int
1194 // the cases that do not fit are identified here; the ones that fit
1195 // fall through and will be handled with other cases further,
1196 // under '1 <= q + exp <= 10'
1197 if (x_sign
) { // if n < 0 and q + exp = 10
1198 // if n <= -2^31 - 1 then n is too large
1199 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1200 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1201 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1203 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1204 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1205 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1206 if (tmp64
>= 0x50000000aull
) {
1208 *pfpsf
|= INVALID_EXCEPTION
;
1209 // return Integer Indefinite
1213 // else cases that can be rounded to a 32-bit int fall through
1214 // to '1 <= q + exp <= 10'
1215 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1216 // C * 10^(11-q) >= 0x50000000a <=>
1217 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1218 // (scale 2^31+1 up)
1219 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1220 tmp64
= 0x50000000aull
* __bid_ten2k64
[q
- 11];
1223 *pfpsf
|= INVALID_EXCEPTION
;
1224 // return Integer Indefinite
1228 // else cases that can be rounded to a 32-bit int fall through
1229 // to '1 <= q + exp <= 10'
1231 } else { // if n > 0 and q + exp = 10
1232 // if n > 2^31 - 1 then n is too large
1233 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
1234 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
1235 // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
1237 // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
1238 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1239 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1240 if (tmp64
> 0x4fffffff6ull
) {
1242 *pfpsf
|= INVALID_EXCEPTION
;
1243 // return Integer Indefinite
1247 // else cases that can be rounded to a 32-bit int fall through
1248 // to '1 <= q + exp <= 10'
1249 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1250 // C * 10^(11-q) > 0x4fffffff6 <=>
1251 // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
1252 // (scale 2^31-1 up)
1253 // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
1254 tmp64
= 0x4fffffff6ull
* __bid_ten2k64
[q
- 11];
1257 *pfpsf
|= INVALID_EXCEPTION
;
1258 // return Integer Indefinite
1262 // else cases that can be rounded to a 32-bit int fall through
1263 // to '1 <= q + exp <= 10'
1267 // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
1268 // Note: some of the cases tested for above fall through to this point
1269 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1276 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1277 // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
1278 // to nearest to a 32-bit signed integer
1279 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1280 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1281 // chop off ind digits from the lower part of C1
1282 // C1 fits in 64 bits
1283 // calculate C* and f*
1284 // C* is actually floor(C*) in this case
1285 // C* and f* need shifting and masking, as shown by
1286 // __bid_shiftright128[] and __bid_maskhigh128[]
1288 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1289 // C* = C1 * 10^(-x)
1290 // the approximation of 10^(-x) was rounded up to 54 bits
1291 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
1293 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
1294 fstar
.w
[0] = P128
.w
[0];
1295 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1296 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1297 // C* = floor(C*) (logical right shift; C has p decimal digits,
1298 // correct by Property 1)
1299 // n = C* * 10^(e+x)
1301 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1302 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
1303 Cstar
= Cstar
>> shift
;
1304 // determine inexactness of the rounding of C*
1305 // if (0 < f* < 10^(-x)) then
1306 // the result is exact
1307 // else // if (f* > T*) then
1308 // the result is inexact
1310 if (fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
1311 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1312 // __bid_ten2mk128[ind -1].w[1]
1313 if (!x_sign
) { // positive and inexact
1316 } // else the result is exact
1317 } else { // if 3 <= ind - 1 <= 14
1318 if (fstar
.w
[1] || fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
1319 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1320 // __bid_ten2mk128[ind -1].w[1]
1321 if (!x_sign
) { // positive and inexact
1324 } // else the result is exact
1331 } else if (exp
== 0) {
1333 // res = +/-C (exact)
1338 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1339 // res = +/-C * 10^exp (exact)
1341 res
= -C1
* __bid_ten2k64
[exp
];
1343 res
= C1
* __bid_ten2k64
[exp
];
1349 /*****************************************************************************
1350 * BID64_to_int32_xceil
1351 ****************************************************************************/
1353 #if DECIMAL_CALL_BY_REFERENCE
1355 __bid64_to_int32_xceil (int *pres
,
1357 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1362 __bid64_to_int32_xceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1368 int exp
; // unbiased exponent
1369 // Note: C1 represents x_significand (UINT64)
1371 BID_UI64DOUBLE tmp1
;
1372 unsigned int x_nr_bits
;
1375 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1379 // check for NaN or Infinity
1380 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1382 *pfpsf
|= INVALID_EXCEPTION
;
1383 // return Integer Indefinite
1388 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1389 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1390 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1391 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1392 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1393 if (C1
> 9999999999999999ull) { // non-canonical
1398 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1399 C1
= x
& MASK_BINARY_SIG1
;
1402 // check for zeros (possibly from non-canonical values)
1408 // x is not special and is not zero
1410 // q = nr. of decimal digits in x (1 <= q <= 54)
1411 // determine first the nr. of bits in x
1412 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1413 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1414 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1415 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1417 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1418 } else { // x < 2^32
1419 tmp1
.d
= (double) C1
; // exact conversion
1421 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1423 } else { // if x < 2^53
1424 tmp1
.d
= (double) C1
; // exact conversion
1426 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1428 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
1430 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
1431 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
1434 exp
= x_exp
- 398; // unbiased exponent
1436 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1438 *pfpsf
|= INVALID_EXCEPTION
;
1439 // return Integer Indefinite
1442 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1443 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1444 // so x rounded to an integer may or may not fit in a signed 32-bit int
1445 // the cases that do not fit are identified here; the ones that fit
1446 // fall through and will be handled with other cases further,
1447 // under '1 <= q + exp <= 10'
1448 if (x_sign
) { // if n < 0 and q + exp = 10
1449 // if n <= -2^31 - 1 then n is too large
1450 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1451 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1452 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1454 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1455 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1456 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1457 if (tmp64
>= 0x50000000aull
) {
1459 *pfpsf
|= INVALID_EXCEPTION
;
1460 // return Integer Indefinite
1464 // else cases that can be rounded to a 32-bit int fall through
1465 // to '1 <= q + exp <= 10'
1466 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1467 // C * 10^(11-q) >= 0x50000000a <=>
1468 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1469 // (scale 2^31+1 up)
1470 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1471 tmp64
= 0x50000000aull
* __bid_ten2k64
[q
- 11];
1474 *pfpsf
|= INVALID_EXCEPTION
;
1475 // return Integer Indefinite
1479 // else cases that can be rounded to a 32-bit int fall through
1480 // to '1 <= q + exp <= 10'
1482 } else { // if n > 0 and q + exp = 10
1483 // if n > 2^31 - 1 then n is too large
1484 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
1485 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
1486 // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
1488 // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
1489 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1490 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1491 if (tmp64
> 0x4fffffff6ull
) {
1493 *pfpsf
|= INVALID_EXCEPTION
;
1494 // return Integer Indefinite
1498 // else cases that can be rounded to a 32-bit int fall through
1499 // to '1 <= q + exp <= 10'
1500 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1501 // C * 10^(11-q) > 0x4fffffff6 <=>
1502 // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
1503 // (scale 2^31-1 up)
1504 // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
1505 tmp64
= 0x4fffffff6ull
* __bid_ten2k64
[q
- 11];
1508 *pfpsf
|= INVALID_EXCEPTION
;
1509 // return Integer Indefinite
1513 // else cases that can be rounded to a 32-bit int fall through
1514 // to '1 <= q + exp <= 10'
1518 // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
1519 // Note: some of the cases tested for above fall through to this point
1520 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1522 *pfpsf
|= INEXACT_EXCEPTION
;
1529 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1530 // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
1531 // to nearest to a 32-bit signed integer
1532 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1533 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1534 // chop off ind digits from the lower part of C1
1535 // C1 fits in 64 bits
1536 // calculate C* and f*
1537 // C* is actually floor(C*) in this case
1538 // C* and f* need shifting and masking, as shown by
1539 // __bid_shiftright128[] and __bid_maskhigh128[]
1541 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1542 // C* = C1 * 10^(-x)
1543 // the approximation of 10^(-x) was rounded up to 54 bits
1544 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
1546 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
1547 fstar
.w
[0] = P128
.w
[0];
1548 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1549 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1550 // C* = floor(C*) (logical right shift; C has p decimal digits,
1551 // correct by Property 1)
1552 // n = C* * 10^(e+x)
1554 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1555 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
1556 Cstar
= Cstar
>> shift
;
1557 // determine inexactness of the rounding of C*
1558 // if (0 < f* < 10^(-x)) then
1559 // the result is exact
1560 // else // if (f* > T*) then
1561 // the result is inexact
1563 if (fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
1564 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1565 // __bid_ten2mk128[ind -1].w[1]
1566 if (!x_sign
) { // positive and inexact
1569 // set the inexact flag
1570 *pfpsf
|= INEXACT_EXCEPTION
;
1571 } // else the result is exact
1572 } else { // if 3 <= ind - 1 <= 14
1573 if (fstar
.w
[1] || fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
1574 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1575 // __bid_ten2mk128[ind -1].w[1]
1576 if (!x_sign
) { // positive and inexact
1579 // set the inexact flag
1580 *pfpsf
|= INEXACT_EXCEPTION
;
1581 } // else the result is exact
1588 } else if (exp
== 0) {
1590 // res = +/-C (exact)
1595 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1596 // res = +/-C * 10^exp (exact)
1598 res
= -C1
* __bid_ten2k64
[exp
];
1600 res
= C1
* __bid_ten2k64
[exp
];
1606 /*****************************************************************************
1607 * BID64_to_int32_int
1608 ****************************************************************************/
1610 #if DECIMAL_CALL_BY_REFERENCE
1612 __bid64_to_int32_int (int *pres
,
1614 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1619 __bid64_to_int32_int (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1625 int exp
; // unbiased exponent
1626 // Note: C1 represents x_significand (UINT64)
1628 BID_UI64DOUBLE tmp1
;
1629 unsigned int x_nr_bits
;
1632 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1635 // check for NaN or Infinity
1636 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1638 *pfpsf
|= INVALID_EXCEPTION
;
1639 // return Integer Indefinite
1644 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1645 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1646 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1647 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1648 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1649 if (C1
> 9999999999999999ull) { // non-canonical
1654 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1655 C1
= x
& MASK_BINARY_SIG1
;
1658 // check for zeros (possibly from non-canonical values)
1664 // x is not special and is not zero
1666 // q = nr. of decimal digits in x (1 <= q <= 54)
1667 // determine first the nr. of bits in x
1668 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1669 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1670 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1671 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1673 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1674 } else { // x < 2^32
1675 tmp1
.d
= (double) C1
; // exact conversion
1677 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1679 } else { // if x < 2^53
1680 tmp1
.d
= (double) C1
; // exact conversion
1682 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1684 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
1686 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
1687 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
1690 exp
= x_exp
- 398; // unbiased exponent
1692 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1694 *pfpsf
|= INVALID_EXCEPTION
;
1695 // return Integer Indefinite
1698 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1699 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1700 // so x rounded to an integer may or may not fit in a signed 32-bit int
1701 // the cases that do not fit are identified here; the ones that fit
1702 // fall through and will be handled with other cases further,
1703 // under '1 <= q + exp <= 10'
1704 if (x_sign
) { // if n < 0 and q + exp = 10
1705 // if n <= -2^31 - 1 then n is too large
1706 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1707 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1708 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1710 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1711 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1712 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1713 if (tmp64
>= 0x50000000aull
) {
1715 *pfpsf
|= INVALID_EXCEPTION
;
1716 // return Integer Indefinite
1720 // else cases that can be rounded to a 32-bit int fall through
1721 // to '1 <= q + exp <= 10'
1722 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1723 // C * 10^(11-q) >= 0x50000000a <=>
1724 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1725 // (scale 2^31+1 up)
1726 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1727 tmp64
= 0x50000000aull
* __bid_ten2k64
[q
- 11];
1730 *pfpsf
|= INVALID_EXCEPTION
;
1731 // return Integer Indefinite
1735 // else cases that can be rounded to a 32-bit int fall through
1736 // to '1 <= q + exp <= 10'
1738 } else { // if n > 0 and q + exp = 10
1739 // if n >= 2^31 then n is too large
1740 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
1741 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
1742 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
1744 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
1745 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1746 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1747 if (tmp64
>= 0x500000000ull
) {
1749 *pfpsf
|= INVALID_EXCEPTION
;
1750 // return Integer Indefinite
1754 // else cases that can be rounded to a 32-bit int fall through
1755 // to '1 <= q + exp <= 10'
1756 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1757 // C * 10^(11-q) >= 0x500000000 <=>
1758 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
1759 // (scale 2^31-1 up)
1760 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
1761 tmp64
= 0x500000000ull
* __bid_ten2k64
[q
- 11];
1764 *pfpsf
|= INVALID_EXCEPTION
;
1765 // return Integer Indefinite
1769 // else cases that can be rounded to a 32-bit int fall through
1770 // to '1 <= q + exp <= 10'
1774 // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
1775 // Note: some of the cases tested for above fall through to this point
1776 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1780 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1781 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
1782 // to nearest to a 32-bit signed integer
1783 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1784 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1785 // chop off ind digits from the lower part of C1
1786 // C1 fits in 64 bits
1787 // calculate C* and f*
1788 // C* is actually floor(C*) in this case
1789 // C* and f* need shifting and masking, as shown by
1790 // __bid_shiftright128[] and __bid_maskhigh128[]
1792 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1793 // C* = C1 * 10^(-x)
1794 // the approximation of 10^(-x) was rounded up to 54 bits
1795 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
1797 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1798 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1799 // C* = floor(C*) (logical right shift; C has p decimal digits,
1800 // correct by Property 1)
1801 // n = C* * 10^(e+x)
1803 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1804 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
1805 Cstar
= Cstar
>> shift
;
1810 } else if (exp
== 0) {
1812 // res = +/-C (exact)
1817 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1818 // res = +/-C * 10^exp (exact)
1820 res
= -C1
* __bid_ten2k64
[exp
];
1822 res
= C1
* __bid_ten2k64
[exp
];
1828 /*****************************************************************************
1829 * BID64_to_int32_xint
1830 ****************************************************************************/
1832 #if DECIMAL_CALL_BY_REFERENCE
1834 __bid64_to_int32_xint (int *pres
,
1836 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1841 __bid64_to_int32_xint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1847 int exp
; // unbiased exponent
1848 // Note: C1 represents x_significand (UINT64)
1850 BID_UI64DOUBLE tmp1
;
1851 unsigned int x_nr_bits
;
1854 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1858 // check for NaN or Infinity
1859 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1861 *pfpsf
|= INVALID_EXCEPTION
;
1862 // return Integer Indefinite
1867 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1868 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1869 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1870 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1871 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1872 if (C1
> 9999999999999999ull) { // non-canonical
1877 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1878 C1
= x
& MASK_BINARY_SIG1
;
1881 // check for zeros (possibly from non-canonical values)
1887 // x is not special and is not zero
1889 // q = nr. of decimal digits in x (1 <= q <= 54)
1890 // determine first the nr. of bits in x
1891 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1892 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1893 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1894 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1896 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1897 } else { // x < 2^32
1898 tmp1
.d
= (double) C1
; // exact conversion
1900 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1902 } else { // if x < 2^53
1903 tmp1
.d
= (double) C1
; // exact conversion
1905 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1907 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
1909 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
1910 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
1913 exp
= x_exp
- 398; // unbiased exponent
1915 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1917 *pfpsf
|= INVALID_EXCEPTION
;
1918 // return Integer Indefinite
1921 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1922 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1923 // so x rounded to an integer may or may not fit in a signed 32-bit int
1924 // the cases that do not fit are identified here; the ones that fit
1925 // fall through and will be handled with other cases further,
1926 // under '1 <= q + exp <= 10'
1927 if (x_sign
) { // if n < 0 and q + exp = 10
1928 // if n <= -2^31 - 1 then n is too large
1929 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1930 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1931 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1933 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1934 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1935 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1936 if (tmp64
>= 0x50000000aull
) {
1938 *pfpsf
|= INVALID_EXCEPTION
;
1939 // return Integer Indefinite
1943 // else cases that can be rounded to a 32-bit int fall through
1944 // to '1 <= q + exp <= 10'
1945 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1946 // C * 10^(11-q) >= 0x50000000a <=>
1947 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1948 // (scale 2^31+1 up)
1949 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1950 tmp64
= 0x50000000aull
* __bid_ten2k64
[q
- 11];
1953 *pfpsf
|= INVALID_EXCEPTION
;
1954 // return Integer Indefinite
1958 // else cases that can be rounded to a 32-bit int fall through
1959 // to '1 <= q + exp <= 10'
1961 } else { // if n > 0 and q + exp = 10
1962 // if n >= 2^31 then n is too large
1963 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
1964 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
1965 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
1967 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
1968 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
1969 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1970 if (tmp64
>= 0x500000000ull
) {
1972 *pfpsf
|= INVALID_EXCEPTION
;
1973 // return Integer Indefinite
1977 // else cases that can be rounded to a 32-bit int fall through
1978 // to '1 <= q + exp <= 10'
1979 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1980 // C * 10^(11-q) >= 0x500000000 <=>
1981 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
1982 // (scale 2^31-1 up)
1983 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
1984 tmp64
= 0x500000000ull
* __bid_ten2k64
[q
- 11];
1987 *pfpsf
|= INVALID_EXCEPTION
;
1988 // return Integer Indefinite
1992 // else cases that can be rounded to a 32-bit int fall through
1993 // to '1 <= q + exp <= 10'
1997 // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
1998 // Note: some of the cases tested for above fall through to this point
1999 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
2001 *pfpsf
|= INEXACT_EXCEPTION
;
2005 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2006 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
2007 // to nearest to a 32-bit signed integer
2008 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2009 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2010 // chop off ind digits from the lower part of C1
2011 // C1 fits in 64 bits
2012 // calculate C* and f*
2013 // C* is actually floor(C*) in this case
2014 // C* and f* need shifting and masking, as shown by
2015 // __bid_shiftright128[] and __bid_maskhigh128[]
2017 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
2018 // C* = C1 * 10^(-x)
2019 // the approximation of 10^(-x) was rounded up to 54 bits
2020 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
2022 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
2023 fstar
.w
[0] = P128
.w
[0];
2024 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
2025 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
2026 // C* = floor(C*) (logical right shift; C has p decimal digits,
2027 // correct by Property 1)
2028 // n = C* * 10^(e+x)
2030 // shift right C* by Ex-64 = __bid_shiftright128[ind]
2031 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
2032 Cstar
= Cstar
>> shift
;
2033 // determine inexactness of the rounding of C*
2034 // if (0 < f* < 10^(-x)) then
2035 // the result is exact
2036 // else // if (f* > T*) then
2037 // the result is inexact
2039 if (fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
2040 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2041 // __bid_ten2mk128[ind -1].w[1]
2042 // set the inexact flag
2043 *pfpsf
|= INEXACT_EXCEPTION
;
2044 } // else the result is exact
2045 } else { // if 3 <= ind - 1 <= 14
2046 if (fstar
.w
[1] || fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
2047 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2048 // __bid_ten2mk128[ind -1].w[1]
2049 // set the inexact flag
2050 *pfpsf
|= INEXACT_EXCEPTION
;
2051 } // else the result is exact
2058 } else if (exp
== 0) {
2060 // res = +/-C (exact)
2065 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2066 // res = +/-C * 10^exp (exact)
2068 res
= -C1
* __bid_ten2k64
[exp
];
2070 res
= C1
* __bid_ten2k64
[exp
];
2076 /*****************************************************************************
2077 * BID64_to_int32_rninta
2078 ****************************************************************************/
2080 #if DECIMAL_CALL_BY_REFERENCE
2082 __bid64_to_int32_rninta (int *pres
,
2084 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2089 __bid64_to_int32_rninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2095 int exp
; // unbiased exponent
2096 // Note: C1 represents x_significand (UINT64)
2098 BID_UI64DOUBLE tmp1
;
2099 unsigned int x_nr_bits
;
2102 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
2105 // check for NaN or Infinity
2106 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
2108 *pfpsf
|= INVALID_EXCEPTION
;
2109 // return Integer Indefinite
2114 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
2115 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2116 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
2117 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
2118 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
2119 if (C1
> 9999999999999999ull) { // non-canonical
2124 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
2125 C1
= x
& MASK_BINARY_SIG1
;
2128 // check for zeros (possibly from non-canonical values)
2134 // x is not special and is not zero
2136 // q = nr. of decimal digits in x (1 <= q <= 54)
2137 // determine first the nr. of bits in x
2138 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
2139 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2140 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
2141 tmp1
.d
= (double) (C1
>> 32); // exact conversion
2143 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2144 } else { // x < 2^32
2145 tmp1
.d
= (double) C1
; // exact conversion
2147 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2149 } else { // if x < 2^53
2150 tmp1
.d
= (double) C1
; // exact conversion
2152 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2154 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
2156 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
2157 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
2160 exp
= x_exp
- 398; // unbiased exponent
2162 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
2164 *pfpsf
|= INVALID_EXCEPTION
;
2165 // return Integer Indefinite
2168 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
2169 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
2170 // so x rounded to an integer may or may not fit in a signed 32-bit int
2171 // the cases that do not fit are identified here; the ones that fit
2172 // fall through and will be handled with other cases further,
2173 // under '1 <= q + exp <= 10'
2174 if (x_sign
) { // if n < 0 and q + exp = 10
2175 // if n <= -2^31 - 1/2 then n is too large
2176 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
2177 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
2178 // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
2180 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2181 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
2182 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2183 if (tmp64
>= 0x500000005ull
) {
2185 *pfpsf
|= INVALID_EXCEPTION
;
2186 // return Integer Indefinite
2190 // else cases that can be rounded to a 32-bit int fall through
2191 // to '1 <= q + exp <= 10'
2192 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2193 // C * 10^(11-q) >= 0x500000005 <=>
2194 // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
2195 // (scale 2^31+1/2 up)
2196 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
2197 tmp64
= 0x500000005ull
* __bid_ten2k64
[q
- 11];
2200 *pfpsf
|= INVALID_EXCEPTION
;
2201 // return Integer Indefinite
2205 // else cases that can be rounded to a 32-bit int fall through
2206 // to '1 <= q + exp <= 10'
2208 } else { // if n > 0 and q + exp = 10
2209 // if n >= 2^31 - 1/2 then n is too large
2210 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
2211 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
2212 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
2214 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2215 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
2216 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2217 if (tmp64
>= 0x4fffffffbull
) {
2219 *pfpsf
|= INVALID_EXCEPTION
;
2220 // return Integer Indefinite
2224 // else cases that can be rounded to a 32-bit int fall through
2225 // to '1 <= q + exp <= 10'
2226 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2227 // C * 10^(11-q) >= 0x4fffffffb <=>
2228 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
2229 // (scale 2^31-1/2 up)
2230 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
2231 tmp64
= 0x4fffffffbull
* __bid_ten2k64
[q
- 11];
2234 *pfpsf
|= INVALID_EXCEPTION
;
2235 // return Integer Indefinite
2239 // else cases that can be rounded to a 32-bit int fall through
2240 // to '1 <= q + exp <= 10'
2244 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
2245 // Note: some of the cases tested for above fall through to this point
2246 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2250 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2251 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
2256 if (C1
< __bid_midpoint64
[ind
]) {
2257 res
= 0x00000000; // return 0
2258 } else if (x_sign
) { // n < 0
2259 res
= 0xffffffff; // return -1
2261 res
= 0x00000001; // return +1
2263 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2264 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
2265 // to nearest away to a 32-bit signed integer
2266 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2267 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2268 // chop off ind digits from the lower part of C1
2269 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2270 C1
= C1
+ __bid_midpoint64
[ind
- 1];
2271 // calculate C* and f*
2272 // C* is actually floor(C*) in this case
2273 // C* and f* need shifting and masking, as shown by
2274 // __bid_shiftright128[] and __bid_maskhigh128[]
2276 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
2277 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2278 // the approximation of 10^(-x) was rounded up to 54 bits
2279 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
2281 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
2282 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
2283 // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
2284 // correct by Pr. 1)
2285 // n = C* * 10^(e+x)
2287 // shift right C* by Ex-64 = __bid_shiftright128[ind]
2288 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
2289 Cstar
= Cstar
>> shift
;
2291 // if the result was a midpoint it was rounded away from zero
2296 } else if (exp
== 0) {
2298 // res = +/-C (exact)
2303 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2304 // res = +/-C * 10^exp (exact)
2306 res
= -C1
* __bid_ten2k64
[exp
];
2308 res
= C1
* __bid_ten2k64
[exp
];
2314 /*****************************************************************************
2315 * BID64_to_int32_xrninta
2316 ****************************************************************************/
2318 #if DECIMAL_CALL_BY_REFERENCE
2320 __bid64_to_int32_xrninta (int *pres
,
2322 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2327 __bid64_to_int32_xrninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2333 int exp
; // unbiased exponent
2334 // Note: C1 represents x_significand (UINT64)
2336 BID_UI64DOUBLE tmp1
;
2337 unsigned int x_nr_bits
;
2340 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
2344 // check for NaN or Infinity
2345 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
2347 *pfpsf
|= INVALID_EXCEPTION
;
2348 // return Integer Indefinite
2353 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
2354 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2355 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
2356 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
2357 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
2358 if (C1
> 9999999999999999ull) { // non-canonical
2363 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
2364 C1
= x
& MASK_BINARY_SIG1
;
2367 // check for zeros (possibly from non-canonical values)
2373 // x is not special and is not zero
2375 // q = nr. of decimal digits in x (1 <= q <= 54)
2376 // determine first the nr. of bits in x
2377 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
2378 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2379 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
2380 tmp1
.d
= (double) (C1
>> 32); // exact conversion
2382 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2383 } else { // x < 2^32
2384 tmp1
.d
= (double) C1
; // exact conversion
2386 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2388 } else { // if x < 2^53
2389 tmp1
.d
= (double) C1
; // exact conversion
2391 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2393 q
= __bid_nr_digits
[x_nr_bits
- 1].digits
;
2395 q
= __bid_nr_digits
[x_nr_bits
- 1].digits1
;
2396 if (C1
>= __bid_nr_digits
[x_nr_bits
- 1].threshold_lo
)
2399 exp
= x_exp
- 398; // unbiased exponent
2401 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
2403 *pfpsf
|= INVALID_EXCEPTION
;
2404 // return Integer Indefinite
2407 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
2408 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
2409 // so x rounded to an integer may or may not fit in a signed 32-bit int
2410 // the cases that do not fit are identified here; the ones that fit
2411 // fall through and will be handled with other cases further,
2412 // under '1 <= q + exp <= 10'
2413 if (x_sign
) { // if n < 0 and q + exp = 10
2414 // if n <= -2^31 - 1/2 then n is too large
2415 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
2416 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
2417 // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
2419 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2420 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
2421 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2422 if (tmp64
>= 0x500000005ull
) {
2424 *pfpsf
|= INVALID_EXCEPTION
;
2425 // return Integer Indefinite
2429 // else cases that can be rounded to a 32-bit int fall through
2430 // to '1 <= q + exp <= 10'
2431 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2432 // C * 10^(11-q) >= 0x500000005 <=>
2433 // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
2434 // (scale 2^31+1/2 up)
2435 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
2436 tmp64
= 0x500000005ull
* __bid_ten2k64
[q
- 11];
2439 *pfpsf
|= INVALID_EXCEPTION
;
2440 // return Integer Indefinite
2444 // else cases that can be rounded to a 32-bit int fall through
2445 // to '1 <= q + exp <= 10'
2447 } else { // if n > 0 and q + exp = 10
2448 // if n >= 2^31 - 1/2 then n is too large
2449 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
2450 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
2451 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
2453 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2454 tmp64
= C1
* __bid_ten2k64
[11 - q
]; // C scaled up to 11-digit int
2455 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2456 if (tmp64
>= 0x4fffffffbull
) {
2458 *pfpsf
|= INVALID_EXCEPTION
;
2459 // return Integer Indefinite
2463 // else cases that can be rounded to a 32-bit int fall through
2464 // to '1 <= q + exp <= 10'
2465 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2466 // C * 10^(11-q) >= 0x4fffffffb <=>
2467 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
2468 // (scale 2^31-1/2 up)
2469 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
2470 tmp64
= 0x4fffffffbull
* __bid_ten2k64
[q
- 11];
2473 *pfpsf
|= INVALID_EXCEPTION
;
2474 // return Integer Indefinite
2478 // else cases that can be rounded to a 32-bit int fall through
2479 // to '1 <= q + exp <= 10'
2483 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
2484 // Note: some of the cases tested for above fall through to this point
2485 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2487 *pfpsf
|= INEXACT_EXCEPTION
;
2491 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2492 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
2497 if (C1
< __bid_midpoint64
[ind
]) {
2498 res
= 0x00000000; // return 0
2499 } else if (x_sign
) { // n < 0
2500 res
= 0xffffffff; // return -1
2502 res
= 0x00000001; // return +1
2505 *pfpsf
|= INEXACT_EXCEPTION
;
2506 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2507 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
2508 // to nearest away to a 32-bit signed integer
2509 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2510 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2511 // chop off ind digits from the lower part of C1
2512 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2513 C1
= C1
+ __bid_midpoint64
[ind
- 1];
2514 // calculate C* and f*
2515 // C* is actually floor(C*) in this case
2516 // C* and f* need shifting and masking, as shown by
2517 // __bid_shiftright128[] and __bid_maskhigh128[]
2519 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
2520 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2521 // the approximation of 10^(-x) was rounded up to 54 bits
2522 __mul_64x64_to_128MACH (P128
, C1
, __bid_ten2mk64
[ind
- 1]);
2524 fstar
.w
[1] = P128
.w
[1] & __bid_maskhigh128
[ind
- 1];
2525 fstar
.w
[0] = P128
.w
[0];
2526 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
2527 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
2528 // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
2529 // correct by Pr. 1)
2530 // n = C* * 10^(e+x)
2532 // shift right C* by Ex-64 = __bid_shiftright128[ind]
2533 shift
= __bid_shiftright128
[ind
- 1]; // 0 <= shift <= 39
2534 Cstar
= Cstar
>> shift
;
2535 // determine inexactness of the rounding of C*
2536 // if (0 < f* - 1/2 < 10^(-x)) then
2537 // the result is exact
2538 // else // if (f* - 1/2 > T*) then
2539 // the result is inexact
2541 if (fstar
.w
[0] > 0x8000000000000000ull
) {
2542 // f* > 1/2 and the result may be exact
2543 tmp64
= fstar
.w
[0] - 0x8000000000000000ull
; // f* - 1/2
2544 if ((tmp64
> __bid_ten2mk128trunc
[ind
- 1].w
[1])) {
2545 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2546 // __bid_ten2mk128[ind -1].w[1]
2547 // set the inexact flag
2548 *pfpsf
|= INEXACT_EXCEPTION
;
2549 } // else the result is exact
2550 } else { // the result is inexact; f2* <= 1/2
2551 // set the inexact flag
2552 *pfpsf
|= INEXACT_EXCEPTION
;
2554 } else { // if 3 <= ind - 1 <= 14
2555 if (fstar
.w
[1] > __bid_one_half128
[ind
- 1]
2556 || (fstar
.w
[1] == __bid_one_half128
[ind
- 1]
2558 // f2* > 1/2 and the result may be exact
2559 // Calculate f2* - 1/2
2560 tmp64
= fstar
.w
[1] - __bid_one_half128
[ind
- 1];
2561 if (tmp64
|| fstar
.w
[0] > __bid_ten2mk128trunc
[ind
- 1].w
[1]) {
2562 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2563 // __bid_ten2mk128[ind -1].w[1]
2564 // set the inexact flag
2565 *pfpsf
|= INEXACT_EXCEPTION
;
2566 } // else the result is exact
2567 } else { // the result is inexact; f2* <= 1/2
2568 // set the inexact flag
2569 *pfpsf
|= INEXACT_EXCEPTION
;
2573 // if the result was a midpoint it was rounded away from zero
2578 } else if (exp
== 0) {
2580 // res = +/-C (exact)
2585 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2586 // res = +/-C * 10^exp (exact)
2588 res
= -C1
* __bid_ten2k64
[exp
];
2590 res
= C1
* __bid_ten2k64
[exp
];