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
= nr_digits
[x_nr_bits
- 1].digits
;
112 q
= nr_digits
[x_nr_bits
- 1].digits1
;
113 if (C1
>= 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
* 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
* 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
* 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
* 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
<= 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
+ 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 // shiftright128[] and maskhigh128[]
232 // kx = 10^(-x) = 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
, ten2mk64
[ind
- 1]);
237 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
238 fstar
.w
[0] = P128
.w
[0];
239 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
240 // if x=1, T*=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 = shiftright128[ind]
252 shift
= 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] <= ten2mk128trunc
[ind
- 1].w
[1])) {
260 // ten2mk128trunc[ind -1].w[1] is identical to
261 // 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
* ten2k64
[exp
];
284 res
= C1
* 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
= nr_digits
[x_nr_bits
- 1].digits
;
371 q
= nr_digits
[x_nr_bits
- 1].digits1
;
372 if (C1
>= 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
* 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
* 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
* 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
* 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
<= 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
+ 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 // shiftright128[] and maskhigh128[]
495 // kx = 10^(-x) = 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
, ten2mk64
[ind
- 1]);
500 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
501 fstar
.w
[0] = P128
.w
[0];
502 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
503 // if x=1, T*=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 = shiftright128[ind]
515 shift
= 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
> ten2mk128trunc
[ind
- 1].w
[1])) {
527 // ten2mk128trunc[ind -1].w[1] is identical to
528 // 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] > onehalf128
[ind
- 1] ||
538 (fstar
.w
[1] == onehalf128
[ind
- 1] && fstar
.w
[0])) {
539 // f2* > 1/2 and the result may be exact
540 // Calculate f2* - 1/2
541 tmp64
= fstar
.w
[1] - onehalf128
[ind
- 1];
542 if (tmp64
|| fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
543 // ten2mk128trunc[ind -1].w[1] is identical to
544 // ten2mk128[ind -1].w[1]
545 // set the inexact flag
546 *pfpsf
|= INEXACT_EXCEPTION
;
547 } // else the result is exact
548 } else { // the result is inexact; f2* <= 1/2
549 // set the inexact flag
550 *pfpsf
|= INEXACT_EXCEPTION
;
554 // if the result was a midpoint it was rounded away from zero, so
555 // it will need a correction
556 // check for midpoints
557 if ((fstar
.w
[1] == 0) && fstar
.w
[0]
558 && (fstar
.w
[0] <= ten2mk128trunc
[ind
- 1].w
[1])) {
559 // ten2mk128trunc[ind -1].w[1] is identical to
560 // ten2mk128[ind -1].w[1]
561 // the result is a midpoint; round to nearest
562 if (Cstar
& 0x01) { // Cstar is odd; MP in [EVEN, ODD]
563 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
564 Cstar
--; // Cstar is now even
565 } // else MP in [ODD, EVEN]
571 } else if (exp
== 0) {
573 // res = +/-C (exact)
578 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
579 // res = +/-C * 10^exp (exact)
581 res
= -C1
* ten2k64
[exp
];
583 res
= C1
* ten2k64
[exp
];
589 /*****************************************************************************
590 * BID64_to_int32_floor
591 ****************************************************************************/
593 #if DECIMAL_CALL_BY_REFERENCE
595 bid64_to_int32_floor (int *pres
,
597 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
602 bid64_to_int32_floor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
608 int exp
; // unbiased exponent
609 // Note: C1 represents x_significand (UINT64)
612 unsigned int x_nr_bits
;
615 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
619 // check for NaN or Infinity
620 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
622 *pfpsf
|= INVALID_EXCEPTION
;
623 // return Integer Indefinite
628 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
629 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
630 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
631 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
632 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
633 if (C1
> 9999999999999999ull) { // non-canonical
638 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
639 C1
= x
& MASK_BINARY_SIG1
;
642 // check for zeros (possibly from non-canonical values)
648 // x is not special and is not zero
650 // q = nr. of decimal digits in x (1 <= q <= 54)
651 // determine first the nr. of bits in x
652 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
653 // split the 64-bit value in two 32-bit halves to avoid rounding errors
654 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
655 tmp1
.d
= (double) (C1
>> 32); // exact conversion
657 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
659 tmp1
.d
= (double) C1
; // exact conversion
661 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
663 } else { // if x < 2^53
664 tmp1
.d
= (double) C1
; // exact conversion
666 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
668 q
= nr_digits
[x_nr_bits
- 1].digits
;
670 q
= nr_digits
[x_nr_bits
- 1].digits1
;
671 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
674 exp
= x_exp
- 398; // unbiased exponent
676 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
678 *pfpsf
|= INVALID_EXCEPTION
;
679 // return Integer Indefinite
682 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
683 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
684 // so x rounded to an integer may or may not fit in a signed 32-bit int
685 // the cases that do not fit are identified here; the ones that fit
686 // fall through and will be handled with other cases further,
687 // under '1 <= q + exp <= 10'
688 if (x_sign
) { // if n < 0 and q + exp = 10
689 // if n < -2^31 then n is too large
690 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
691 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
692 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
694 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
695 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
696 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
697 if (tmp64
> 0x500000000ull
) {
699 *pfpsf
|= INVALID_EXCEPTION
;
700 // return Integer Indefinite
704 // else cases that can be rounded to a 32-bit int fall through
705 // to '1 <= q + exp <= 10'
706 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
707 // C * 10^(11-q) > 0x500000000 <=>
708 // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
710 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
711 tmp64
= 0x500000000ull
* ten2k64
[q
- 11];
714 *pfpsf
|= INVALID_EXCEPTION
;
715 // return Integer Indefinite
719 // else cases that can be rounded to a 32-bit int fall through
720 // to '1 <= q + exp <= 10'
722 } else { // if n > 0 and q + exp = 10
723 // if n >= 2^31 then n is too large
724 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
725 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
726 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
728 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
729 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
730 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
731 if (tmp64
>= 0x500000000ull
) {
733 *pfpsf
|= INVALID_EXCEPTION
;
734 // return Integer Indefinite
738 // else cases that can be rounded to a 32-bit int fall through
739 // to '1 <= q + exp <= 10'
740 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
741 // C * 10^(11-q) >= 0x500000000 <=>
742 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
744 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
745 tmp64
= 0x500000000ull
* ten2k64
[q
- 11];
748 *pfpsf
|= INVALID_EXCEPTION
;
749 // return Integer Indefinite
753 // else cases that can be rounded to a 32-bit int fall through
754 // to '1 <= q + exp <= 10'
758 // n is not too large to be converted to int32: -2^31 <= n < 2^31
759 // Note: some of the cases tested for above fall through to this point
760 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
767 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
768 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
769 // to nearest to a 32-bit signed integer
770 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
771 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
772 // chop off ind digits from the lower part of C1
773 // C1 fits in 64 bits
774 // calculate C* and f*
775 // C* is actually floor(C*) in this case
776 // C* and f* need shifting and masking, as shown by
777 // shiftright128[] and maskhigh128[]
779 // kx = 10^(-x) = ten2mk64[ind - 1]
781 // the approximation of 10^(-x) was rounded up to 54 bits
782 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
784 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
785 fstar
.w
[0] = P128
.w
[0];
786 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
787 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
788 // C* = floor(C*) (logical right shift; C has p decimal digits,
789 // correct by Property 1)
792 // shift right C* by Ex-64 = shiftright128[ind]
793 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
794 Cstar
= Cstar
>> shift
;
795 // determine inexactness of the rounding of C*
796 // if (0 < f* < 10^(-x)) then
797 // the result is exact
798 // else // if (f* > T*) then
799 // the result is inexact
801 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
802 // ten2mk128trunc[ind -1].w[1] is identical to
803 // ten2mk128[ind -1].w[1]
804 if (x_sign
) { // negative and inexact
807 } // else the result is exact
808 } else { // if 3 <= ind - 1 <= 14
809 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
810 // ten2mk128trunc[ind -1].w[1] is identical to
811 // ten2mk128[ind -1].w[1]
812 if (x_sign
) { // negative and inexact
815 } // else the result is exact
822 } else if (exp
== 0) {
824 // res = +/-C (exact)
829 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
830 // res = +/-C * 10^exp (exact)
832 res
= -C1
* ten2k64
[exp
];
834 res
= C1
* ten2k64
[exp
];
840 /*****************************************************************************
841 * BID64_to_int32_xfloor
842 ****************************************************************************/
844 #if DECIMAL_CALL_BY_REFERENCE
846 bid64_to_int32_xfloor (int *pres
,
848 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
853 bid64_to_int32_xfloor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
859 int exp
; // unbiased exponent
860 // Note: C1 represents x_significand (UINT64)
863 unsigned int x_nr_bits
;
866 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
870 // check for NaN or Infinity
871 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
873 *pfpsf
|= INVALID_EXCEPTION
;
874 // return Integer Indefinite
879 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
880 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
881 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
882 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
883 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
884 if (C1
> 9999999999999999ull) { // non-canonical
889 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
890 C1
= x
& MASK_BINARY_SIG1
;
893 // check for zeros (possibly from non-canonical values)
899 // x is not special and is not zero
901 // q = nr. of decimal digits in x (1 <= q <= 54)
902 // determine first the nr. of bits in x
903 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
904 // split the 64-bit value in two 32-bit halves to avoid rounding errors
905 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
906 tmp1
.d
= (double) (C1
>> 32); // exact conversion
908 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
910 tmp1
.d
= (double) C1
; // exact conversion
912 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
914 } else { // if x < 2^53
915 tmp1
.d
= (double) C1
; // exact conversion
917 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
919 q
= nr_digits
[x_nr_bits
- 1].digits
;
921 q
= nr_digits
[x_nr_bits
- 1].digits1
;
922 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
925 exp
= x_exp
- 398; // unbiased exponent
927 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
929 *pfpsf
|= INVALID_EXCEPTION
;
930 // return Integer Indefinite
933 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
934 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
935 // so x rounded to an integer may or may not fit in a signed 32-bit int
936 // the cases that do not fit are identified here; the ones that fit
937 // fall through and will be handled with other cases further,
938 // under '1 <= q + exp <= 10'
939 if (x_sign
) { // if n < 0 and q + exp = 10
940 // if n < -2^31 then n is too large
941 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
942 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
943 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
945 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
946 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
947 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
948 if (tmp64
> 0x500000000ull
) {
950 *pfpsf
|= INVALID_EXCEPTION
;
951 // return Integer Indefinite
955 // else cases that can be rounded to a 32-bit int fall through
956 // to '1 <= q + exp <= 10'
957 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
958 // C * 10^(11-q) > 0x500000000 <=>
959 // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
961 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
962 tmp64
= 0x500000000ull
* ten2k64
[q
- 11];
965 *pfpsf
|= INVALID_EXCEPTION
;
966 // return Integer Indefinite
970 // else cases that can be rounded to a 32-bit int fall through
971 // to '1 <= q + exp <= 10'
973 } else { // if n > 0 and q + exp = 10
974 // if n >= 2^31 then n is too large
975 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
976 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
977 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
979 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
980 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
981 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
982 if (tmp64
>= 0x500000000ull
) {
984 *pfpsf
|= INVALID_EXCEPTION
;
985 // return Integer Indefinite
989 // else cases that can be rounded to a 32-bit int fall through
990 // to '1 <= q + exp <= 10'
991 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
992 // C * 10^(11-q) >= 0x500000000 <=>
993 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
995 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
996 tmp64
= 0x500000000ull
* ten2k64
[q
- 11];
999 *pfpsf
|= INVALID_EXCEPTION
;
1000 // return Integer Indefinite
1004 // else cases that can be rounded to a 32-bit int fall through
1005 // to '1 <= q + exp <= 10'
1009 // n is not too large to be converted to int32: -2^31 <= n < 2^31
1010 // Note: some of the cases tested for above fall through to this point
1011 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1013 *pfpsf
|= INEXACT_EXCEPTION
;
1020 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1021 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
1022 // to nearest to a 32-bit signed integer
1023 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1024 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1025 // chop off ind digits from the lower part of C1
1026 // C1 fits in 64 bits
1027 // calculate C* and f*
1028 // C* is actually floor(C*) in this case
1029 // C* and f* need shifting and masking, as shown by
1030 // shiftright128[] and maskhigh128[]
1032 // kx = 10^(-x) = ten2mk64[ind - 1]
1033 // C* = C1 * 10^(-x)
1034 // the approximation of 10^(-x) was rounded up to 54 bits
1035 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1037 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
1038 fstar
.w
[0] = P128
.w
[0];
1039 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1040 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1041 // C* = floor(C*) (logical right shift; C has p decimal digits,
1042 // correct by Property 1)
1043 // n = C* * 10^(e+x)
1045 // shift right C* by Ex-64 = shiftright128[ind]
1046 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1047 Cstar
= Cstar
>> shift
;
1048 // determine inexactness of the rounding of C*
1049 // if (0 < f* < 10^(-x)) then
1050 // the result is exact
1051 // else // if (f* > T*) then
1052 // the result is inexact
1054 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1055 // ten2mk128trunc[ind -1].w[1] is identical to
1056 // ten2mk128[ind -1].w[1]
1057 if (x_sign
) { // negative and inexact
1060 // set the inexact flag
1061 *pfpsf
|= INEXACT_EXCEPTION
;
1062 } // else the result is exact
1063 } else { // if 3 <= ind - 1 <= 14
1064 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1065 // ten2mk128trunc[ind -1].w[1] is identical to
1066 // ten2mk128[ind -1].w[1]
1067 if (x_sign
) { // negative and inexact
1070 // set the inexact flag
1071 *pfpsf
|= INEXACT_EXCEPTION
;
1072 } // else the result is exact
1079 } else if (exp
== 0) {
1081 // res = +/-C (exact)
1086 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1087 // res = +/-C * 10^exp (exact)
1089 res
= -C1
* ten2k64
[exp
];
1091 res
= C1
* ten2k64
[exp
];
1097 /*****************************************************************************
1098 * BID64_to_int32_ceil
1099 ****************************************************************************/
1101 #if DECIMAL_CALL_BY_REFERENCE
1103 bid64_to_int32_ceil (int *pres
,
1105 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1110 bid64_to_int32_ceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1116 int exp
; // unbiased exponent
1117 // Note: C1 represents x_significand (UINT64)
1119 BID_UI64DOUBLE tmp1
;
1120 unsigned int x_nr_bits
;
1123 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1127 // check for NaN or Infinity
1128 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1130 *pfpsf
|= INVALID_EXCEPTION
;
1131 // return Integer Indefinite
1136 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1137 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1138 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1139 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1140 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1141 if (C1
> 9999999999999999ull) { // non-canonical
1146 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1147 C1
= x
& MASK_BINARY_SIG1
;
1150 // check for zeros (possibly from non-canonical values)
1156 // x is not special and is not zero
1158 // q = nr. of decimal digits in x (1 <= q <= 54)
1159 // determine first the nr. of bits in x
1160 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1161 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1162 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1163 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1165 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1166 } else { // x < 2^32
1167 tmp1
.d
= (double) C1
; // exact conversion
1169 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1171 } else { // if x < 2^53
1172 tmp1
.d
= (double) C1
; // exact conversion
1174 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1176 q
= nr_digits
[x_nr_bits
- 1].digits
;
1178 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1179 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1182 exp
= x_exp
- 398; // unbiased exponent
1184 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1186 *pfpsf
|= INVALID_EXCEPTION
;
1187 // return Integer Indefinite
1190 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1191 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1192 // so x rounded to an integer may or may not fit in a signed 32-bit int
1193 // the cases that do not fit are identified here; the ones that fit
1194 // fall through and will be handled with other cases further,
1195 // under '1 <= q + exp <= 10'
1196 if (x_sign
) { // if n < 0 and q + exp = 10
1197 // if n <= -2^31 - 1 then n is too large
1198 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1199 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1200 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1202 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1203 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1204 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1205 if (tmp64
>= 0x50000000aull
) {
1207 *pfpsf
|= INVALID_EXCEPTION
;
1208 // return Integer Indefinite
1212 // else cases that can be rounded to a 32-bit int fall through
1213 // to '1 <= q + exp <= 10'
1214 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1215 // C * 10^(11-q) >= 0x50000000a <=>
1216 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1217 // (scale 2^31+1 up)
1218 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1219 tmp64
= 0x50000000aull
* ten2k64
[q
- 11];
1222 *pfpsf
|= INVALID_EXCEPTION
;
1223 // return Integer Indefinite
1227 // else cases that can be rounded to a 32-bit int fall through
1228 // to '1 <= q + exp <= 10'
1230 } else { // if n > 0 and q + exp = 10
1231 // if n > 2^31 - 1 then n is too large
1232 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
1233 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
1234 // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
1236 // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
1237 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1238 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1239 if (tmp64
> 0x4fffffff6ull
) {
1241 *pfpsf
|= INVALID_EXCEPTION
;
1242 // return Integer Indefinite
1246 // else cases that can be rounded to a 32-bit int fall through
1247 // to '1 <= q + exp <= 10'
1248 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1249 // C * 10^(11-q) > 0x4fffffff6 <=>
1250 // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
1251 // (scale 2^31-1 up)
1252 // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
1253 tmp64
= 0x4fffffff6ull
* ten2k64
[q
- 11];
1256 *pfpsf
|= INVALID_EXCEPTION
;
1257 // return Integer Indefinite
1261 // else cases that can be rounded to a 32-bit int fall through
1262 // to '1 <= q + exp <= 10'
1266 // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
1267 // Note: some of the cases tested for above fall through to this point
1268 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1275 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1276 // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
1277 // to nearest to a 32-bit signed integer
1278 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1279 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1280 // chop off ind digits from the lower part of C1
1281 // C1 fits in 64 bits
1282 // calculate C* and f*
1283 // C* is actually floor(C*) in this case
1284 // C* and f* need shifting and masking, as shown by
1285 // shiftright128[] and maskhigh128[]
1287 // kx = 10^(-x) = ten2mk64[ind - 1]
1288 // C* = C1 * 10^(-x)
1289 // the approximation of 10^(-x) was rounded up to 54 bits
1290 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1292 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
1293 fstar
.w
[0] = P128
.w
[0];
1294 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1295 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1296 // C* = floor(C*) (logical right shift; C has p decimal digits,
1297 // correct by Property 1)
1298 // n = C* * 10^(e+x)
1300 // shift right C* by Ex-64 = shiftright128[ind]
1301 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1302 Cstar
= Cstar
>> shift
;
1303 // determine inexactness of the rounding of C*
1304 // if (0 < f* < 10^(-x)) then
1305 // the result is exact
1306 // else // if (f* > T*) then
1307 // the result is inexact
1309 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1310 // ten2mk128trunc[ind -1].w[1] is identical to
1311 // ten2mk128[ind -1].w[1]
1312 if (!x_sign
) { // positive and inexact
1315 } // else the result is exact
1316 } else { // if 3 <= ind - 1 <= 14
1317 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1318 // ten2mk128trunc[ind -1].w[1] is identical to
1319 // ten2mk128[ind -1].w[1]
1320 if (!x_sign
) { // positive and inexact
1323 } // else the result is exact
1330 } else if (exp
== 0) {
1332 // res = +/-C (exact)
1337 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1338 // res = +/-C * 10^exp (exact)
1340 res
= -C1
* ten2k64
[exp
];
1342 res
= C1
* ten2k64
[exp
];
1348 /*****************************************************************************
1349 * BID64_to_int32_xceil
1350 ****************************************************************************/
1352 #if DECIMAL_CALL_BY_REFERENCE
1354 bid64_to_int32_xceil (int *pres
,
1356 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1361 bid64_to_int32_xceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1367 int exp
; // unbiased exponent
1368 // Note: C1 represents x_significand (UINT64)
1370 BID_UI64DOUBLE tmp1
;
1371 unsigned int x_nr_bits
;
1374 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1378 // check for NaN or Infinity
1379 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1381 *pfpsf
|= INVALID_EXCEPTION
;
1382 // return Integer Indefinite
1387 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1388 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1389 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1390 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1391 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1392 if (C1
> 9999999999999999ull) { // non-canonical
1397 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1398 C1
= x
& MASK_BINARY_SIG1
;
1401 // check for zeros (possibly from non-canonical values)
1407 // x is not special and is not zero
1409 // q = nr. of decimal digits in x (1 <= q <= 54)
1410 // determine first the nr. of bits in x
1411 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1412 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1413 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1414 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1416 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1417 } else { // x < 2^32
1418 tmp1
.d
= (double) C1
; // exact conversion
1420 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1422 } else { // if x < 2^53
1423 tmp1
.d
= (double) C1
; // exact conversion
1425 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1427 q
= nr_digits
[x_nr_bits
- 1].digits
;
1429 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1430 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1433 exp
= x_exp
- 398; // unbiased exponent
1435 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1437 *pfpsf
|= INVALID_EXCEPTION
;
1438 // return Integer Indefinite
1441 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1442 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1443 // so x rounded to an integer may or may not fit in a signed 32-bit int
1444 // the cases that do not fit are identified here; the ones that fit
1445 // fall through and will be handled with other cases further,
1446 // under '1 <= q + exp <= 10'
1447 if (x_sign
) { // if n < 0 and q + exp = 10
1448 // if n <= -2^31 - 1 then n is too large
1449 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1450 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1451 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1453 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1454 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1455 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1456 if (tmp64
>= 0x50000000aull
) {
1458 *pfpsf
|= INVALID_EXCEPTION
;
1459 // return Integer Indefinite
1463 // else cases that can be rounded to a 32-bit int fall through
1464 // to '1 <= q + exp <= 10'
1465 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1466 // C * 10^(11-q) >= 0x50000000a <=>
1467 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1468 // (scale 2^31+1 up)
1469 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1470 tmp64
= 0x50000000aull
* ten2k64
[q
- 11];
1473 *pfpsf
|= INVALID_EXCEPTION
;
1474 // return Integer Indefinite
1478 // else cases that can be rounded to a 32-bit int fall through
1479 // to '1 <= q + exp <= 10'
1481 } else { // if n > 0 and q + exp = 10
1482 // if n > 2^31 - 1 then n is too large
1483 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
1484 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
1485 // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
1487 // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
1488 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1489 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1490 if (tmp64
> 0x4fffffff6ull
) {
1492 *pfpsf
|= INVALID_EXCEPTION
;
1493 // return Integer Indefinite
1497 // else cases that can be rounded to a 32-bit int fall through
1498 // to '1 <= q + exp <= 10'
1499 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1500 // C * 10^(11-q) > 0x4fffffff6 <=>
1501 // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
1502 // (scale 2^31-1 up)
1503 // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
1504 tmp64
= 0x4fffffff6ull
* ten2k64
[q
- 11];
1507 *pfpsf
|= INVALID_EXCEPTION
;
1508 // return Integer Indefinite
1512 // else cases that can be rounded to a 32-bit int fall through
1513 // to '1 <= q + exp <= 10'
1517 // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
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 *pfpsf
|= INEXACT_EXCEPTION
;
1528 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1529 // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
1530 // to nearest to a 32-bit signed integer
1531 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1532 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1533 // chop off ind digits from the lower part of C1
1534 // C1 fits in 64 bits
1535 // calculate C* and f*
1536 // C* is actually floor(C*) in this case
1537 // C* and f* need shifting and masking, as shown by
1538 // shiftright128[] and maskhigh128[]
1540 // kx = 10^(-x) = ten2mk64[ind - 1]
1541 // C* = C1 * 10^(-x)
1542 // the approximation of 10^(-x) was rounded up to 54 bits
1543 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1545 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
1546 fstar
.w
[0] = P128
.w
[0];
1547 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1548 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1549 // C* = floor(C*) (logical right shift; C has p decimal digits,
1550 // correct by Property 1)
1551 // n = C* * 10^(e+x)
1553 // shift right C* by Ex-64 = shiftright128[ind]
1554 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1555 Cstar
= Cstar
>> shift
;
1556 // determine inexactness of the rounding of C*
1557 // if (0 < f* < 10^(-x)) then
1558 // the result is exact
1559 // else // if (f* > T*) then
1560 // the result is inexact
1562 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1563 // ten2mk128trunc[ind -1].w[1] is identical to
1564 // ten2mk128[ind -1].w[1]
1565 if (!x_sign
) { // positive and inexact
1568 // set the inexact flag
1569 *pfpsf
|= INEXACT_EXCEPTION
;
1570 } // else the result is exact
1571 } else { // if 3 <= ind - 1 <= 14
1572 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
1573 // ten2mk128trunc[ind -1].w[1] is identical to
1574 // ten2mk128[ind -1].w[1]
1575 if (!x_sign
) { // positive and inexact
1578 // set the inexact flag
1579 *pfpsf
|= INEXACT_EXCEPTION
;
1580 } // else the result is exact
1587 } else if (exp
== 0) {
1589 // res = +/-C (exact)
1594 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1595 // res = +/-C * 10^exp (exact)
1597 res
= -C1
* ten2k64
[exp
];
1599 res
= C1
* ten2k64
[exp
];
1605 /*****************************************************************************
1606 * BID64_to_int32_int
1607 ****************************************************************************/
1609 #if DECIMAL_CALL_BY_REFERENCE
1611 bid64_to_int32_int (int *pres
,
1613 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1618 bid64_to_int32_int (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1624 int exp
; // unbiased exponent
1625 // Note: C1 represents x_significand (UINT64)
1627 BID_UI64DOUBLE tmp1
;
1628 unsigned int x_nr_bits
;
1631 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1634 // check for NaN or Infinity
1635 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1637 *pfpsf
|= INVALID_EXCEPTION
;
1638 // return Integer Indefinite
1643 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1644 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1645 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1646 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1647 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1648 if (C1
> 9999999999999999ull) { // non-canonical
1653 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1654 C1
= x
& MASK_BINARY_SIG1
;
1657 // check for zeros (possibly from non-canonical values)
1663 // x is not special and is not zero
1665 // q = nr. of decimal digits in x (1 <= q <= 54)
1666 // determine first the nr. of bits in x
1667 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1668 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1669 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1670 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1672 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1673 } else { // x < 2^32
1674 tmp1
.d
= (double) C1
; // exact conversion
1676 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1678 } else { // if x < 2^53
1679 tmp1
.d
= (double) C1
; // exact conversion
1681 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1683 q
= nr_digits
[x_nr_bits
- 1].digits
;
1685 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1686 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1689 exp
= x_exp
- 398; // unbiased exponent
1691 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1693 *pfpsf
|= INVALID_EXCEPTION
;
1694 // return Integer Indefinite
1697 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1698 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1699 // so x rounded to an integer may or may not fit in a signed 32-bit int
1700 // the cases that do not fit are identified here; the ones that fit
1701 // fall through and will be handled with other cases further,
1702 // under '1 <= q + exp <= 10'
1703 if (x_sign
) { // if n < 0 and q + exp = 10
1704 // if n <= -2^31 - 1 then n is too large
1705 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1706 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1707 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1709 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1710 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1711 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1712 if (tmp64
>= 0x50000000aull
) {
1714 *pfpsf
|= INVALID_EXCEPTION
;
1715 // return Integer Indefinite
1719 // else cases that can be rounded to a 32-bit int fall through
1720 // to '1 <= q + exp <= 10'
1721 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1722 // C * 10^(11-q) >= 0x50000000a <=>
1723 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1724 // (scale 2^31+1 up)
1725 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1726 tmp64
= 0x50000000aull
* ten2k64
[q
- 11];
1729 *pfpsf
|= INVALID_EXCEPTION
;
1730 // return Integer Indefinite
1734 // else cases that can be rounded to a 32-bit int fall through
1735 // to '1 <= q + exp <= 10'
1737 } else { // if n > 0 and q + exp = 10
1738 // if n >= 2^31 then n is too large
1739 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
1740 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
1741 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
1743 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
1744 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1745 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1746 if (tmp64
>= 0x500000000ull
) {
1748 *pfpsf
|= INVALID_EXCEPTION
;
1749 // return Integer Indefinite
1753 // else cases that can be rounded to a 32-bit int fall through
1754 // to '1 <= q + exp <= 10'
1755 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1756 // C * 10^(11-q) >= 0x500000000 <=>
1757 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
1758 // (scale 2^31-1 up)
1759 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
1760 tmp64
= 0x500000000ull
* ten2k64
[q
- 11];
1763 *pfpsf
|= INVALID_EXCEPTION
;
1764 // return Integer Indefinite
1768 // else cases that can be rounded to a 32-bit int fall through
1769 // to '1 <= q + exp <= 10'
1773 // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
1774 // Note: some of the cases tested for above fall through to this point
1775 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1779 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1780 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
1781 // to nearest to a 32-bit signed integer
1782 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1783 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
1784 // chop off ind digits from the lower part of C1
1785 // C1 fits in 64 bits
1786 // calculate C* and f*
1787 // C* is actually floor(C*) in this case
1788 // C* and f* need shifting and masking, as shown by
1789 // shiftright128[] and maskhigh128[]
1791 // kx = 10^(-x) = ten2mk64[ind - 1]
1792 // C* = C1 * 10^(-x)
1793 // the approximation of 10^(-x) was rounded up to 54 bits
1794 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
1796 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
1797 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
1798 // C* = floor(C*) (logical right shift; C has p decimal digits,
1799 // correct by Property 1)
1800 // n = C* * 10^(e+x)
1802 // shift right C* by Ex-64 = shiftright128[ind]
1803 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
1804 Cstar
= Cstar
>> shift
;
1809 } else if (exp
== 0) {
1811 // res = +/-C (exact)
1816 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1817 // res = +/-C * 10^exp (exact)
1819 res
= -C1
* ten2k64
[exp
];
1821 res
= C1
* ten2k64
[exp
];
1827 /*****************************************************************************
1828 * BID64_to_int32_xint
1829 ****************************************************************************/
1831 #if DECIMAL_CALL_BY_REFERENCE
1833 bid64_to_int32_xint (int *pres
,
1835 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1840 bid64_to_int32_xint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1846 int exp
; // unbiased exponent
1847 // Note: C1 represents x_significand (UINT64)
1849 BID_UI64DOUBLE tmp1
;
1850 unsigned int x_nr_bits
;
1853 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
1857 // check for NaN or Infinity
1858 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
1860 *pfpsf
|= INVALID_EXCEPTION
;
1861 // return Integer Indefinite
1866 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
1867 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1868 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
1869 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
1870 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
1871 if (C1
> 9999999999999999ull) { // non-canonical
1876 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
1877 C1
= x
& MASK_BINARY_SIG1
;
1880 // check for zeros (possibly from non-canonical values)
1886 // x is not special and is not zero
1888 // q = nr. of decimal digits in x (1 <= q <= 54)
1889 // determine first the nr. of bits in x
1890 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
1891 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1892 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
1893 tmp1
.d
= (double) (C1
>> 32); // exact conversion
1895 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1896 } else { // x < 2^32
1897 tmp1
.d
= (double) C1
; // exact conversion
1899 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1901 } else { // if x < 2^53
1902 tmp1
.d
= (double) C1
; // exact conversion
1904 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
1906 q
= nr_digits
[x_nr_bits
- 1].digits
;
1908 q
= nr_digits
[x_nr_bits
- 1].digits1
;
1909 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
1912 exp
= x_exp
- 398; // unbiased exponent
1914 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1916 *pfpsf
|= INVALID_EXCEPTION
;
1917 // return Integer Indefinite
1920 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1921 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1922 // so x rounded to an integer may or may not fit in a signed 32-bit int
1923 // the cases that do not fit are identified here; the ones that fit
1924 // fall through and will be handled with other cases further,
1925 // under '1 <= q + exp <= 10'
1926 if (x_sign
) { // if n < 0 and q + exp = 10
1927 // if n <= -2^31 - 1 then n is too large
1928 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1929 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1930 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1932 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1933 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1934 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1935 if (tmp64
>= 0x50000000aull
) {
1937 *pfpsf
|= INVALID_EXCEPTION
;
1938 // return Integer Indefinite
1942 // else cases that can be rounded to a 32-bit int fall through
1943 // to '1 <= q + exp <= 10'
1944 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1945 // C * 10^(11-q) >= 0x50000000a <=>
1946 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1947 // (scale 2^31+1 up)
1948 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1949 tmp64
= 0x50000000aull
* ten2k64
[q
- 11];
1952 *pfpsf
|= INVALID_EXCEPTION
;
1953 // return Integer Indefinite
1957 // else cases that can be rounded to a 32-bit int fall through
1958 // to '1 <= q + exp <= 10'
1960 } else { // if n > 0 and q + exp = 10
1961 // if n >= 2^31 then n is too large
1962 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
1963 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
1964 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
1966 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
1967 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
1968 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1969 if (tmp64
>= 0x500000000ull
) {
1971 *pfpsf
|= INVALID_EXCEPTION
;
1972 // return Integer Indefinite
1976 // else cases that can be rounded to a 32-bit int fall through
1977 // to '1 <= q + exp <= 10'
1978 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1979 // C * 10^(11-q) >= 0x500000000 <=>
1980 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
1981 // (scale 2^31-1 up)
1982 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
1983 tmp64
= 0x500000000ull
* ten2k64
[q
- 11];
1986 *pfpsf
|= INVALID_EXCEPTION
;
1987 // return Integer Indefinite
1991 // else cases that can be rounded to a 32-bit int fall through
1992 // to '1 <= q + exp <= 10'
1996 // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
1997 // Note: some of the cases tested for above fall through to this point
1998 if ((q
+ exp
) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
2000 *pfpsf
|= INEXACT_EXCEPTION
;
2004 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2005 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
2006 // to nearest to a 32-bit signed integer
2007 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2008 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2009 // chop off ind digits from the lower part of C1
2010 // C1 fits in 64 bits
2011 // calculate C* and f*
2012 // C* is actually floor(C*) in this case
2013 // C* and f* need shifting and masking, as shown by
2014 // shiftright128[] and maskhigh128[]
2016 // kx = 10^(-x) = ten2mk64[ind - 1]
2017 // C* = C1 * 10^(-x)
2018 // the approximation of 10^(-x) was rounded up to 54 bits
2019 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
2021 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
2022 fstar
.w
[0] = P128
.w
[0];
2023 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
2024 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
2025 // C* = floor(C*) (logical right shift; C has p decimal digits,
2026 // correct by Property 1)
2027 // n = C* * 10^(e+x)
2029 // shift right C* by Ex-64 = shiftright128[ind]
2030 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
2031 Cstar
= Cstar
>> shift
;
2032 // determine inexactness of the rounding of C*
2033 // if (0 < f* < 10^(-x)) then
2034 // the result is exact
2035 // else // if (f* > T*) then
2036 // the result is inexact
2038 if (fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
2039 // ten2mk128trunc[ind -1].w[1] is identical to
2040 // ten2mk128[ind -1].w[1]
2041 // set the inexact flag
2042 *pfpsf
|= INEXACT_EXCEPTION
;
2043 } // else the result is exact
2044 } else { // if 3 <= ind - 1 <= 14
2045 if (fstar
.w
[1] || fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
2046 // ten2mk128trunc[ind -1].w[1] is identical to
2047 // ten2mk128[ind -1].w[1]
2048 // set the inexact flag
2049 *pfpsf
|= INEXACT_EXCEPTION
;
2050 } // else the result is exact
2057 } else if (exp
== 0) {
2059 // res = +/-C (exact)
2064 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2065 // res = +/-C * 10^exp (exact)
2067 res
= -C1
* ten2k64
[exp
];
2069 res
= C1
* ten2k64
[exp
];
2075 /*****************************************************************************
2076 * BID64_to_int32_rninta
2077 ****************************************************************************/
2079 #if DECIMAL_CALL_BY_REFERENCE
2081 bid64_to_int32_rninta (int *pres
,
2083 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2088 bid64_to_int32_rninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2094 int exp
; // unbiased exponent
2095 // Note: C1 represents x_significand (UINT64)
2097 BID_UI64DOUBLE tmp1
;
2098 unsigned int x_nr_bits
;
2101 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
2104 // check for NaN or Infinity
2105 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
2107 *pfpsf
|= INVALID_EXCEPTION
;
2108 // return Integer Indefinite
2113 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
2114 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2115 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
2116 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
2117 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
2118 if (C1
> 9999999999999999ull) { // non-canonical
2123 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
2124 C1
= x
& MASK_BINARY_SIG1
;
2127 // check for zeros (possibly from non-canonical values)
2133 // x is not special and is not zero
2135 // q = nr. of decimal digits in x (1 <= q <= 54)
2136 // determine first the nr. of bits in x
2137 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
2138 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2139 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
2140 tmp1
.d
= (double) (C1
>> 32); // exact conversion
2142 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2143 } else { // x < 2^32
2144 tmp1
.d
= (double) C1
; // exact conversion
2146 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2148 } else { // if x < 2^53
2149 tmp1
.d
= (double) C1
; // exact conversion
2151 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2153 q
= nr_digits
[x_nr_bits
- 1].digits
;
2155 q
= nr_digits
[x_nr_bits
- 1].digits1
;
2156 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
2159 exp
= x_exp
- 398; // unbiased exponent
2161 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
2163 *pfpsf
|= INVALID_EXCEPTION
;
2164 // return Integer Indefinite
2167 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
2168 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
2169 // so x rounded to an integer may or may not fit in a signed 32-bit int
2170 // the cases that do not fit are identified here; the ones that fit
2171 // fall through and will be handled with other cases further,
2172 // under '1 <= q + exp <= 10'
2173 if (x_sign
) { // if n < 0 and q + exp = 10
2174 // if n <= -2^31 - 1/2 then n is too large
2175 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
2176 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
2177 // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
2179 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2180 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
2181 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2182 if (tmp64
>= 0x500000005ull
) {
2184 *pfpsf
|= INVALID_EXCEPTION
;
2185 // return Integer Indefinite
2189 // else cases that can be rounded to a 32-bit int fall through
2190 // to '1 <= q + exp <= 10'
2191 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2192 // C * 10^(11-q) >= 0x500000005 <=>
2193 // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
2194 // (scale 2^31+1/2 up)
2195 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
2196 tmp64
= 0x500000005ull
* ten2k64
[q
- 11];
2199 *pfpsf
|= INVALID_EXCEPTION
;
2200 // return Integer Indefinite
2204 // else cases that can be rounded to a 32-bit int fall through
2205 // to '1 <= q + exp <= 10'
2207 } else { // if n > 0 and q + exp = 10
2208 // if n >= 2^31 - 1/2 then n is too large
2209 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
2210 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
2211 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
2213 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2214 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
2215 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2216 if (tmp64
>= 0x4fffffffbull
) {
2218 *pfpsf
|= INVALID_EXCEPTION
;
2219 // return Integer Indefinite
2223 // else cases that can be rounded to a 32-bit int fall through
2224 // to '1 <= q + exp <= 10'
2225 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2226 // C * 10^(11-q) >= 0x4fffffffb <=>
2227 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
2228 // (scale 2^31-1/2 up)
2229 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
2230 tmp64
= 0x4fffffffbull
* ten2k64
[q
- 11];
2233 *pfpsf
|= INVALID_EXCEPTION
;
2234 // return Integer Indefinite
2238 // else cases that can be rounded to a 32-bit int fall through
2239 // to '1 <= q + exp <= 10'
2243 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
2244 // Note: some of the cases tested for above fall through to this point
2245 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2249 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2250 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
2255 if (C1
< midpoint64
[ind
]) {
2256 res
= 0x00000000; // return 0
2257 } else if (x_sign
) { // n < 0
2258 res
= 0xffffffff; // return -1
2260 res
= 0x00000001; // return +1
2262 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2263 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
2264 // to nearest away to a 32-bit signed integer
2265 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2266 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2267 // chop off ind digits from the lower part of C1
2268 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2269 C1
= C1
+ midpoint64
[ind
- 1];
2270 // calculate C* and f*
2271 // C* is actually floor(C*) in this case
2272 // C* and f* need shifting and masking, as shown by
2273 // shiftright128[] and maskhigh128[]
2275 // kx = 10^(-x) = ten2mk64[ind - 1]
2276 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2277 // the approximation of 10^(-x) was rounded up to 54 bits
2278 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
2280 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
2281 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
2282 // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
2283 // correct by Pr. 1)
2284 // n = C* * 10^(e+x)
2286 // shift right C* by Ex-64 = shiftright128[ind]
2287 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
2288 Cstar
= Cstar
>> shift
;
2290 // if the result was a midpoint it was rounded away from zero
2295 } else if (exp
== 0) {
2297 // res = +/-C (exact)
2302 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2303 // res = +/-C * 10^exp (exact)
2305 res
= -C1
* ten2k64
[exp
];
2307 res
= C1
* ten2k64
[exp
];
2313 /*****************************************************************************
2314 * BID64_to_int32_xrninta
2315 ****************************************************************************/
2317 #if DECIMAL_CALL_BY_REFERENCE
2319 bid64_to_int32_xrninta (int *pres
,
2321 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2326 bid64_to_int32_xrninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2332 int exp
; // unbiased exponent
2333 // Note: C1 represents x_significand (UINT64)
2335 BID_UI64DOUBLE tmp1
;
2336 unsigned int x_nr_bits
;
2339 UINT64 Cstar
; // C* represents up to 16 decimal digits ~ 54 bits
2343 // check for NaN or Infinity
2344 if ((x
& MASK_NAN
) == MASK_NAN
|| (x
& MASK_INF
) == MASK_INF
) {
2346 *pfpsf
|= INVALID_EXCEPTION
;
2347 // return Integer Indefinite
2352 x_sign
= x
& MASK_SIGN
; // 0 for positive, MASK_SIGN for negative
2353 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2354 if ((x
& MASK_STEERING_BITS
) == MASK_STEERING_BITS
) {
2355 x_exp
= (x
& MASK_BINARY_EXPONENT2
) >> 51; // biased
2356 C1
= (x
& MASK_BINARY_SIG2
) | MASK_BINARY_OR2
;
2357 if (C1
> 9999999999999999ull) { // non-canonical
2362 x_exp
= (x
& MASK_BINARY_EXPONENT1
) >> 53; // biased
2363 C1
= x
& MASK_BINARY_SIG1
;
2366 // check for zeros (possibly from non-canonical values)
2372 // x is not special and is not zero
2374 // q = nr. of decimal digits in x (1 <= q <= 54)
2375 // determine first the nr. of bits in x
2376 if (C1
>= 0x0020000000000000ull
) { // x >= 2^53
2377 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2378 if (C1
>= 0x0000000100000000ull
) { // x >= 2^32
2379 tmp1
.d
= (double) (C1
>> 32); // exact conversion
2381 33 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2382 } else { // x < 2^32
2383 tmp1
.d
= (double) C1
; // exact conversion
2385 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2387 } else { // if x < 2^53
2388 tmp1
.d
= (double) C1
; // exact conversion
2390 1 + ((((unsigned int) (tmp1
.ui64
>> 52)) & 0x7ff) - 0x3ff);
2392 q
= nr_digits
[x_nr_bits
- 1].digits
;
2394 q
= nr_digits
[x_nr_bits
- 1].digits1
;
2395 if (C1
>= nr_digits
[x_nr_bits
- 1].threshold_lo
)
2398 exp
= x_exp
- 398; // unbiased exponent
2400 if ((q
+ exp
) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
2402 *pfpsf
|= INVALID_EXCEPTION
;
2403 // return Integer Indefinite
2406 } else if ((q
+ exp
) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
2407 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
2408 // so x rounded to an integer may or may not fit in a signed 32-bit int
2409 // the cases that do not fit are identified here; the ones that fit
2410 // fall through and will be handled with other cases further,
2411 // under '1 <= q + exp <= 10'
2412 if (x_sign
) { // if n < 0 and q + exp = 10
2413 // if n <= -2^31 - 1/2 then n is too large
2414 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
2415 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
2416 // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
2418 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2419 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
2420 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2421 if (tmp64
>= 0x500000005ull
) {
2423 *pfpsf
|= INVALID_EXCEPTION
;
2424 // return Integer Indefinite
2428 // else cases that can be rounded to a 32-bit int fall through
2429 // to '1 <= q + exp <= 10'
2430 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2431 // C * 10^(11-q) >= 0x500000005 <=>
2432 // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
2433 // (scale 2^31+1/2 up)
2434 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
2435 tmp64
= 0x500000005ull
* ten2k64
[q
- 11];
2438 *pfpsf
|= INVALID_EXCEPTION
;
2439 // return Integer Indefinite
2443 // else cases that can be rounded to a 32-bit int fall through
2444 // to '1 <= q + exp <= 10'
2446 } else { // if n > 0 and q + exp = 10
2447 // if n >= 2^31 - 1/2 then n is too large
2448 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
2449 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
2450 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
2452 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2453 tmp64
= C1
* ten2k64
[11 - q
]; // C scaled up to 11-digit int
2454 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2455 if (tmp64
>= 0x4fffffffbull
) {
2457 *pfpsf
|= INVALID_EXCEPTION
;
2458 // return Integer Indefinite
2462 // else cases that can be rounded to a 32-bit int fall through
2463 // to '1 <= q + exp <= 10'
2464 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2465 // C * 10^(11-q) >= 0x4fffffffb <=>
2466 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
2467 // (scale 2^31-1/2 up)
2468 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
2469 tmp64
= 0x4fffffffbull
* ten2k64
[q
- 11];
2472 *pfpsf
|= INVALID_EXCEPTION
;
2473 // return Integer Indefinite
2477 // else cases that can be rounded to a 32-bit int fall through
2478 // to '1 <= q + exp <= 10'
2482 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
2483 // Note: some of the cases tested for above fall through to this point
2484 if ((q
+ exp
) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2486 *pfpsf
|= INEXACT_EXCEPTION
;
2490 } else if ((q
+ exp
) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2491 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
2496 if (C1
< midpoint64
[ind
]) {
2497 res
= 0x00000000; // return 0
2498 } else if (x_sign
) { // n < 0
2499 res
= 0xffffffff; // return -1
2501 res
= 0x00000001; // return +1
2504 *pfpsf
|= INEXACT_EXCEPTION
;
2505 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2506 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
2507 // to nearest away to a 32-bit signed integer
2508 if (exp
< 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2509 ind
= -exp
; // 1 <= ind <= 15; ind is a synonym for 'x'
2510 // chop off ind digits from the lower part of C1
2511 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2512 C1
= C1
+ midpoint64
[ind
- 1];
2513 // calculate C* and f*
2514 // C* is actually floor(C*) in this case
2515 // C* and f* need shifting and masking, as shown by
2516 // shiftright128[] and maskhigh128[]
2518 // kx = 10^(-x) = ten2mk64[ind - 1]
2519 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2520 // the approximation of 10^(-x) was rounded up to 54 bits
2521 __mul_64x64_to_128MACH (P128
, C1
, ten2mk64
[ind
- 1]);
2523 fstar
.w
[1] = P128
.w
[1] & maskhigh128
[ind
- 1];
2524 fstar
.w
[0] = P128
.w
[0];
2525 // the top Ex bits of 10^(-x) are T* = ten2mk128trunc[ind].w[0], e.g.
2526 // if x=1, T*=ten2mk128trunc[0].w[0]=0x1999999999999999
2527 // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
2528 // correct by Pr. 1)
2529 // n = C* * 10^(e+x)
2531 // shift right C* by Ex-64 = shiftright128[ind]
2532 shift
= shiftright128
[ind
- 1]; // 0 <= shift <= 39
2533 Cstar
= Cstar
>> shift
;
2534 // determine inexactness of the rounding of C*
2535 // if (0 < f* - 1/2 < 10^(-x)) then
2536 // the result is exact
2537 // else // if (f* - 1/2 > T*) then
2538 // the result is inexact
2540 if (fstar
.w
[0] > 0x8000000000000000ull
) {
2541 // f* > 1/2 and the result may be exact
2542 tmp64
= fstar
.w
[0] - 0x8000000000000000ull
; // f* - 1/2
2543 if ((tmp64
> ten2mk128trunc
[ind
- 1].w
[1])) {
2544 // ten2mk128trunc[ind -1].w[1] is identical to
2545 // ten2mk128[ind -1].w[1]
2546 // set the inexact flag
2547 *pfpsf
|= INEXACT_EXCEPTION
;
2548 } // else the result is exact
2549 } else { // the result is inexact; f2* <= 1/2
2550 // set the inexact flag
2551 *pfpsf
|= INEXACT_EXCEPTION
;
2553 } else { // if 3 <= ind - 1 <= 14
2554 if (fstar
.w
[1] > onehalf128
[ind
- 1] ||
2555 (fstar
.w
[1] == onehalf128
[ind
- 1] && fstar
.w
[0])) {
2556 // f2* > 1/2 and the result may be exact
2557 // Calculate f2* - 1/2
2558 tmp64
= fstar
.w
[1] - onehalf128
[ind
- 1];
2559 if (tmp64
|| fstar
.w
[0] > ten2mk128trunc
[ind
- 1].w
[1]) {
2560 // ten2mk128trunc[ind -1].w[1] is identical to
2561 // ten2mk128[ind -1].w[1]
2562 // set the inexact flag
2563 *pfpsf
|= INEXACT_EXCEPTION
;
2564 } // else the result is exact
2565 } else { // the result is inexact; f2* <= 1/2
2566 // set the inexact flag
2567 *pfpsf
|= INEXACT_EXCEPTION
;
2571 // if the result was a midpoint it was rounded away from zero
2576 } else if (exp
== 0) {
2578 // res = +/-C (exact)
2583 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2584 // res = +/-C * 10^exp (exact)
2586 res
= -C1
* ten2k64
[exp
];
2588 res
= C1
* ten2k64
[exp
];