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 * BID128_to_int64_rnint
33 ****************************************************************************/
35 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_rnint, x)
40 int exp; // unbiased exponent
41 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
44 unsigned int x_nr_bits;
47 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
52 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
53 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
54 C1.w[1] = x.w[1] & MASK_COEFF;
57 // check for NaN or Infinity
58 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
60 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
61 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
63 *pfpsf |= INVALID_EXCEPTION;
64 // return Integer Indefinite
65 res = 0x8000000000000000ull;
68 *pfpsf |= INVALID_EXCEPTION;
69 // return Integer Indefinite
70 res = 0x8000000000000000ull;
73 } else { // x is not a NaN, so it must be infinity
74 if (!x_sign) { // x is +inf
76 *pfpsf |= INVALID_EXCEPTION;
77 // return Integer Indefinite
78 res = 0x8000000000000000ull;
81 *pfpsf |= INVALID_EXCEPTION;
82 // return Integer Indefinite
83 res = 0x8000000000000000ull;
88 // check for non-canonical values (after the check for special values)
89 if ((C1.w[1] > 0x0001ed09bead87c0ull)
90 || (C1.w[1] == 0x0001ed09bead87c0ull
91 && (C1.w[0] > 0x378d8e63ffffffffull))
92 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
93 res = 0x0000000000000000ull;
95 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
97 res = 0x0000000000000000ull;
99 } else { // x is not special and is not zero
101 // q = nr. of decimal digits in x
102 // determine first the nr. of bits in x
104 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
105 // split the 64-bit value in two 32-bit halves to avoid rounding errors
106 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
107 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
109 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
111 tmp1.d = (double) (C1.w[0]); // exact conversion
113 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
115 } else { // if x < 2^53
116 tmp1.d = (double) C1.w[0]; // exact conversion
118 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
120 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
121 tmp1.d = (double) C1.w[1]; // exact conversion
123 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
125 q = __bid_nr_digits[x_nr_bits - 1].digits;
127 q = __bid_nr_digits[x_nr_bits - 1].digits1;
128 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
129 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
130 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
133 exp = (x_exp >> 49) - 6176;
134 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
136 *pfpsf |= INVALID_EXCEPTION;
137 // return Integer Indefinite
138 res = 0x8000000000000000ull;
140 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
141 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
142 // so x rounded to an integer may or may not fit in a signed 64-bit int
143 // the cases that do not fit are identified here; the ones that fit
144 // fall through and will be handled with other cases further,
145 // under '1 <= q + exp <= 19'
146 if (x_sign) { // if n < 0 and q + exp = 19
147 // if n < -2^63 - 1/2 then n is too large
148 // too large if c(0)c(1)...c(18).c(19)...c(q-1) > 2^63+1/2
149 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 5*(2^64+1), 1<=q<=34
150 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 0x50000000000000005, 1<=q<=34
151 C.w[1] = 0x0000000000000005ull;
152 C.w[0] = 0000000000000005ull;
153 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
154 // 10^(20-q) is 64-bit, and so is C1
155 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
156 } else if (q == 20) {
158 } else { // if 21 <= q <= 34
159 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
161 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] > C.w[0])) {
163 *pfpsf |= INVALID_EXCEPTION;
164 // return Integer Indefinite
165 res = 0x8000000000000000ull;
168 // else cases that can be rounded to a 64-bit int fall through
169 // to '1 <= q + exp <= 19'
170 } else { // if n > 0 and q + exp = 19
171 // if n >= 2^63 - 1/2 then n is too large
172 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63-1/2
173 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64-1), 1<=q<=34
174 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x4fffffffffffffffb, 1<=q<=34
175 C.w[1] = 0x0000000000000004ull;
176 C.w[0] = 0xfffffffffffffffbull;
177 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
178 // 10^(20-q) is 64-bit, and so is C1
179 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
180 } else if (q == 20) {
182 } else { // if 21 <= q <= 34
183 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
185 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
187 *pfpsf |= INVALID_EXCEPTION;
188 // return Integer Indefinite
189 res = 0x8000000000000000ull;
192 // else cases that can be rounded to a 64-bit int fall through
193 // to '1 <= q + exp <= 19'
196 // n is not too large to be converted to int64: -2^63-1/2 <= n < 2^63-1/2
197 // Note: some of the cases tested for above fall through to this point
198 // Restore C1 which may have been modified above
199 C1.w[1] = x.w[1] & MASK_COEFF;
201 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
203 res = 0x0000000000000000ull;
205 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
206 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
211 if (ind <= 18) { // 0 <= ind <= 18
212 if ((C1.w[1] == 0) && (C1.w[0] <= __bid_midpoint64[ind])) {
213 res = 0x0000000000000000ull; // return 0
214 } else if (x_sign) { // n < 0
215 res = 0xffffffffffffffffull; // return -1
217 res = 0x0000000000000001ull; // return +1
219 } else { // 19 <= ind <= 33
220 if ((C1.w[1] < __bid_midpoint128[ind - 19].w[1])
221 || ((C1.w[1] == __bid_midpoint128[ind - 19].w[1])
222 && (C1.w[0] <= __bid_midpoint128[ind - 19].w[0]))) {
223 res = 0x0000000000000000ull; // return 0
224 } else if (x_sign) { // n < 0
225 res = 0xffffffffffffffffull; // return -1
227 res = 0x0000000000000001ull; // return +1
230 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
231 // -2^63-1/2 <= x <= -1 or 1 <= x < 2^63-1/2 so x can be rounded
232 // to nearest to a 64-bit signed integer
233 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
234 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
235 // chop off ind digits from the lower part of C1
236 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 127 bits
239 C1.w[0] = C1.w[0] + __bid_midpoint64[ind - 1];
241 C1.w[0] = C1.w[0] + __bid_midpoint128[ind - 20].w[0];
242 C1.w[1] = C1.w[1] + __bid_midpoint128[ind - 20].w[1];
246 // calculate C* and f*
247 // C* is actually floor(C*) in this case
248 // C* and f* need shifting and masking, as shown by
249 // __bid_shiftright128[] and __bid_maskhigh128[]
251 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
252 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
253 // the approximation of 10^(-x) was rounded up to 118 bits
254 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
255 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
256 Cstar.w[1] = P256.w[3];
257 Cstar.w[0] = P256.w[2];
259 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
260 fstar.w[1] = P256.w[1];
261 fstar.w[0] = P256.w[0];
262 } else { // 22 <= ind - 1 <= 33
264 Cstar.w[0] = P256.w[3];
265 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
266 fstar.w[2] = P256.w[2];
267 fstar.w[1] = P256.w[1];
268 fstar.w[0] = P256.w[0];
270 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
271 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
272 // if (0 < f* < 10^(-x)) then the result is a midpoint
273 // if floor(C*) is even then C* = floor(C*) - logical right
274 // shift; C* has p decimal digits, correct by Prop. 1)
275 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
276 // shift; C* has p decimal digits, correct by Pr. 1)
278 // C* = floor(C*) (logical right shift; C has p decimal digits,
279 // correct by Property 1)
282 // shift right C* by Ex-128 = __bid_shiftright128[ind]
283 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
284 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
286 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
287 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
288 } else { // 22 <= ind - 1 <= 33
289 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
291 // if the result was a midpoint it was rounded away from zero, so
292 // it will need a correction
293 // check for midpoints
294 if ((fstar.w[3] == 0) && (fstar.w[2] == 0)
295 && (fstar.w[1] || fstar.w[0])
296 && (fstar.w[1] < __bid_ten2mk128trunc[ind - 1].w[1]
297 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
298 && fstar.w[0] <= __bid_ten2mk128trunc[ind - 1].w[0]))) {
299 // the result is a midpoint; round to nearest
300 if (Cstar.w[0] & 0x01) { // Cstar.w[0] is odd; MP in [EVEN, ODD]
301 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
302 Cstar.w[0]--; // Cstar.w[0] is now even
303 } // else MP in [ODD, EVEN]
309 } else if (exp == 0) {
311 // res = +/-C (exact)
316 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
317 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
319 res = -C1.w[0] * __bid_ten2k64[exp];
321 res = C1.w[0] * __bid_ten2k64[exp];
328 /*****************************************************************************
329 * BID128_to_int64_xrnint
330 ****************************************************************************/
332 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_xrnint, x)
337 int exp; // unbiased exponent
338 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
339 UINT64 tmp64, tmp64A;
341 unsigned int x_nr_bits;
344 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
349 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
350 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
351 C1.w[1] = x.w[1] & MASK_COEFF;
354 // check for NaN or Infinity
355 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
357 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
358 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
360 *pfpsf |= INVALID_EXCEPTION;
361 // return Integer Indefinite
362 res = 0x8000000000000000ull;
363 } else { // x is QNaN
365 *pfpsf |= INVALID_EXCEPTION;
366 // return Integer Indefinite
367 res = 0x8000000000000000ull;
370 } else { // x is not a NaN, so it must be infinity
371 if (!x_sign) { // x is +inf
373 *pfpsf |= INVALID_EXCEPTION;
374 // return Integer Indefinite
375 res = 0x8000000000000000ull;
376 } else { // x is -inf
378 *pfpsf |= INVALID_EXCEPTION;
379 // return Integer Indefinite
380 res = 0x8000000000000000ull;
385 // check for non-canonical values (after the check for special values)
386 if ((C1.w[1] > 0x0001ed09bead87c0ull)
387 || (C1.w[1] == 0x0001ed09bead87c0ull
388 && (C1.w[0] > 0x378d8e63ffffffffull))
389 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
390 res = 0x0000000000000000ull;
392 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
394 res = 0x0000000000000000ull;
396 } else { // x is not special and is not zero
398 // q = nr. of decimal digits in x
399 // determine first the nr. of bits in x
401 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
402 // split the 64-bit value in two 32-bit halves to avoid rounding errors
403 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
404 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
406 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
408 tmp1.d = (double) (C1.w[0]); // exact conversion
410 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
412 } else { // if x < 2^53
413 tmp1.d = (double) C1.w[0]; // exact conversion
415 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
417 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
418 tmp1.d = (double) C1.w[1]; // exact conversion
420 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
422 q = __bid_nr_digits[x_nr_bits - 1].digits;
424 q = __bid_nr_digits[x_nr_bits - 1].digits1;
425 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
426 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
427 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
430 exp = (x_exp >> 49) - 6176;
431 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
433 *pfpsf |= INVALID_EXCEPTION;
434 // return Integer Indefinite
435 res = 0x8000000000000000ull;
437 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
438 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
439 // so x rounded to an integer may or may not fit in a signed 64-bit int
440 // the cases that do not fit are identified here; the ones that fit
441 // fall through and will be handled with other cases further,
442 // under '1 <= q + exp <= 19'
443 if (x_sign) { // if n < 0 and q + exp = 19
444 // if n < -2^63 - 1/2 then n is too large
445 // too large if c(0)c(1)...c(18).c(19)...c(q-1) > 2^63+1/2
446 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 5*(2^64+1), 1<=q<=34
447 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 0x50000000000000005, 1<=q<=34
448 C.w[1] = 0x0000000000000005ull;
449 C.w[0] = 0000000000000005ull;
450 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
451 // 10^(20-q) is 64-bit, and so is C1
452 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
453 } else if (q == 20) {
455 } else { // if 21 <= q <= 34
456 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
458 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] > C.w[0])) {
460 *pfpsf |= INVALID_EXCEPTION;
461 // return Integer Indefinite
462 res = 0x8000000000000000ull;
465 // else cases that can be rounded to a 64-bit int fall through
466 // to '1 <= q + exp <= 19'
467 } else { // if n > 0 and q + exp = 19
468 // if n >= 2^63 - 1/2 then n is too large
469 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63-1/2
470 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64-1), 1<=q<=34
471 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x4fffffffffffffffb, 1<=q<=34
472 C.w[1] = 0x0000000000000004ull;
473 C.w[0] = 0xfffffffffffffffbull;
474 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
475 // 10^(20-q) is 64-bit, and so is C1
476 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
477 } else if (q == 20) {
479 } else { // if 21 <= q <= 34
480 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
482 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
484 *pfpsf |= INVALID_EXCEPTION;
485 // return Integer Indefinite
486 res = 0x8000000000000000ull;
489 // else cases that can be rounded to a 64-bit int fall through
490 // to '1 <= q + exp <= 19'
493 // n is not too large to be converted to int64: -2^63-1/2 <= n < 2^63-1/2
494 // Note: some of the cases tested for above fall through to this point
495 // Restore C1 which may have been modified above
496 C1.w[1] = x.w[1] & MASK_COEFF;
498 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
500 *pfpsf |= INEXACT_EXCEPTION;
502 res = 0x0000000000000000ull;
504 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
505 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
510 if (ind <= 18) { // 0 <= ind <= 18
511 if ((C1.w[1] == 0) && (C1.w[0] <= __bid_midpoint64[ind])) {
512 res = 0x0000000000000000ull; // return 0
513 } else if (x_sign) { // n < 0
514 res = 0xffffffffffffffffull; // return -1
516 res = 0x0000000000000001ull; // return +1
518 } else { // 19 <= ind <= 33
519 if ((C1.w[1] < __bid_midpoint128[ind - 19].w[1])
520 || ((C1.w[1] == __bid_midpoint128[ind - 19].w[1])
521 && (C1.w[0] <= __bid_midpoint128[ind - 19].w[0]))) {
522 res = 0x0000000000000000ull; // return 0
523 } else if (x_sign) { // n < 0
524 res = 0xffffffffffffffffull; // return -1
526 res = 0x0000000000000001ull; // return +1
530 *pfpsf |= INEXACT_EXCEPTION;
531 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
532 // -2^63-1/2 <= x <= -1 or 1 <= x < 2^63-1/2 so x can be rounded
533 // to nearest to a 64-bit signed integer
534 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
535 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
536 // chop off ind digits from the lower part of C1
537 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 127 bits
540 C1.w[0] = C1.w[0] + __bid_midpoint64[ind - 1];
542 C1.w[0] = C1.w[0] + __bid_midpoint128[ind - 20].w[0];
543 C1.w[1] = C1.w[1] + __bid_midpoint128[ind - 20].w[1];
547 // calculate C* and f*
548 // C* is actually floor(C*) in this case
549 // C* and f* need shifting and masking, as shown by
550 // __bid_shiftright128[] and __bid_maskhigh128[]
552 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
553 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
554 // the approximation of 10^(-x) was rounded up to 118 bits
555 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
556 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
557 Cstar.w[1] = P256.w[3];
558 Cstar.w[0] = P256.w[2];
560 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
561 fstar.w[1] = P256.w[1];
562 fstar.w[0] = P256.w[0];
563 } else { // 22 <= ind - 1 <= 33
565 Cstar.w[0] = P256.w[3];
566 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
567 fstar.w[2] = P256.w[2];
568 fstar.w[1] = P256.w[1];
569 fstar.w[0] = P256.w[0];
571 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
572 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
573 // if (0 < f* < 10^(-x)) then the result is a midpoint
574 // if floor(C*) is even then C* = floor(C*) - logical right
575 // shift; C* has p decimal digits, correct by Prop. 1)
576 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
577 // shift; C* has p decimal digits, correct by Pr. 1)
579 // C* = floor(C*) (logical right shift; C has p decimal digits,
580 // correct by Property 1)
583 // shift right C* by Ex-128 = __bid_shiftright128[ind]
584 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
585 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
587 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
588 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
589 } else { // 22 <= ind - 1 <= 33
590 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
592 // determine inexactness of the rounding of C*
593 // if (0 < f* - 1/2 < 10^(-x)) then
594 // the result is exact
595 // else // if (f* - 1/2 > T*) then
596 // the result is inexact
598 if (fstar.w[1] > 0x8000000000000000ull ||
599 (fstar.w[1] == 0x8000000000000000ull && fstar.w[0] > 0x0ull)) {
600 // f* > 1/2 and the result may be exact
601 tmp64 = fstar.w[1] - 0x8000000000000000ull; // f* - 1/2
602 if ((tmp64 > __bid_ten2mk128trunc[ind - 1].w[1]
603 || (tmp64 == __bid_ten2mk128trunc[ind - 1].w[1]
604 && fstar.w[0] >= __bid_ten2mk128trunc[ind - 1].w[0]))) {
605 // set the inexact flag
606 *pfpsf |= INEXACT_EXCEPTION;
607 } // else the result is exact
608 } else { // the result is inexact; f2* <= 1/2
609 // set the inexact flag
610 *pfpsf |= INEXACT_EXCEPTION;
612 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
613 if (fstar.w[3] > 0x0 || (fstar.w[3] == 0x0
614 && fstar.w[2] > __bid_one_half128[ind - 1]) || (fstar.w[3] == 0x0
615 && fstar.w[2] == __bid_one_half128[ind - 1] && (fstar.w[1]
617 // f2* > 1/2 and the result may be exact
618 // Calculate f2* - 1/2
619 tmp64 = fstar.w[2] - __bid_one_half128[ind - 1];
621 if (tmp64 > fstar.w[2])
624 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
625 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
626 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
627 // set the inexact flag
628 *pfpsf |= INEXACT_EXCEPTION;
629 } // else the result is exact
630 } else { // the result is inexact; f2* <= 1/2
631 // set the inexact flag
632 *pfpsf |= INEXACT_EXCEPTION;
634 } else { // if 22 <= ind <= 33
635 if (fstar.w[3] > __bid_one_half128[ind - 1]
636 || (fstar.w[3] == __bid_one_half128[ind - 1] && (fstar.w[2]
639 // f2* > 1/2 and the result may be exact
640 // Calculate f2* - 1/2
641 tmp64 = fstar.w[3] - __bid_one_half128[ind - 1];
642 if (tmp64 || fstar.w[2]
643 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
644 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
645 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
646 // set the inexact flag
647 *pfpsf |= INEXACT_EXCEPTION;
648 } // else the result is exact
649 } else { // the result is inexact; f2* <= 1/2
650 // set the inexact flag
651 *pfpsf |= INEXACT_EXCEPTION;
655 // if the result was a midpoint it was rounded away from zero, so
656 // it will need a correction
657 // check for midpoints
658 if ((fstar.w[3] == 0) && (fstar.w[2] == 0)
659 && (fstar.w[1] || fstar.w[0])
660 && (fstar.w[1] < __bid_ten2mk128trunc[ind - 1].w[1]
661 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
662 && fstar.w[0] <= __bid_ten2mk128trunc[ind - 1].w[0]))) {
663 // the result is a midpoint; round to nearest
664 if (Cstar.w[0] & 0x01) { // Cstar.w[0] is odd; MP in [EVEN, ODD]
665 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
666 Cstar.w[0]--; // Cstar.w[0] is now even
667 } // else MP in [ODD, EVEN]
673 } else if (exp == 0) {
675 // res = +/-C (exact)
680 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
681 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
683 res = -C1.w[0] * __bid_ten2k64[exp];
685 res = C1.w[0] * __bid_ten2k64[exp];
692 /*****************************************************************************
693 * BID128_to_int64_floor
694 ****************************************************************************/
696 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_floor, x)
701 int exp; // unbiased exponent
702 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
704 unsigned int x_nr_bits;
707 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
712 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
713 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
714 C1.w[1] = x.w[1] & MASK_COEFF;
717 // check for NaN or Infinity
718 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
720 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
721 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
723 *pfpsf |= INVALID_EXCEPTION;
724 // return Integer Indefinite
725 res = 0x8000000000000000ull;
726 } else { // x is QNaN
728 *pfpsf |= INVALID_EXCEPTION;
729 // return Integer Indefinite
730 res = 0x8000000000000000ull;
733 } else { // x is not a NaN, so it must be infinity
734 if (!x_sign) { // x is +inf
736 *pfpsf |= INVALID_EXCEPTION;
737 // return Integer Indefinite
738 res = 0x8000000000000000ull;
739 } else { // x is -inf
741 *pfpsf |= INVALID_EXCEPTION;
742 // return Integer Indefinite
743 res = 0x8000000000000000ull;
748 // check for non-canonical values (after the check for special values)
749 if ((C1.w[1] > 0x0001ed09bead87c0ull)
750 || (C1.w[1] == 0x0001ed09bead87c0ull
751 && (C1.w[0] > 0x378d8e63ffffffffull))
752 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
753 res = 0x0000000000000000ull;
755 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
757 res = 0x0000000000000000ull;
759 } else { // x is not special and is not zero
761 // q = nr. of decimal digits in x
762 // determine first the nr. of bits in x
764 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
765 // split the 64-bit value in two 32-bit halves to avoid rounding errors
766 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
767 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
769 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
771 tmp1.d = (double) (C1.w[0]); // exact conversion
773 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
775 } else { // if x < 2^53
776 tmp1.d = (double) C1.w[0]; // exact conversion
778 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
780 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
781 tmp1.d = (double) C1.w[1]; // exact conversion
783 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
785 q = __bid_nr_digits[x_nr_bits - 1].digits;
787 q = __bid_nr_digits[x_nr_bits - 1].digits1;
788 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
789 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
790 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
793 exp = (x_exp >> 49) - 6176;
795 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
797 *pfpsf |= INVALID_EXCEPTION;
798 // return Integer Indefinite
799 res = 0x8000000000000000ull;
801 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
802 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
803 // so x rounded to an integer may or may not fit in a signed 64-bit int
804 // the cases that do not fit are identified here; the ones that fit
805 // fall through and will be handled with other cases further,
806 // under '1 <= q + exp <= 19'
807 if (x_sign) { // if n < 0 and q + exp = 19
808 // if n < -2^63 then n is too large
809 // too large if c(0)c(1)...c(18).c(19)...c(q-1) > 2^63
810 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 10*2^63, 1<=q<=34
811 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 0x50000000000000000, 1<=q<=34
812 C.w[1] = 0x0000000000000005ull;
813 C.w[0] = 0x0000000000000000ull;
814 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
815 // 10^(20-q) is 64-bit, and so is C1
816 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
817 } else if (q == 20) {
819 } else { // if 21 <= q <= 34
820 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
822 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] > C.w[0])) {
824 *pfpsf |= INVALID_EXCEPTION;
825 // return Integer Indefinite
826 res = 0x8000000000000000ull;
829 // else cases that can be rounded to a 64-bit int fall through
830 // to '1 <= q + exp <= 19'
831 } else { // if n > 0 and q + exp = 19
832 // if n >= 2^63 then n is too large
833 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63
834 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*2^64, 1<=q<=34
835 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x50000000000000000, 1<=q<=34
836 C.w[1] = 0x0000000000000005ull;
837 C.w[0] = 0x0000000000000000ull;
838 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
839 // 10^(20-q) is 64-bit, and so is C1
840 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
841 } else if (q == 20) {
843 } else { // if 21 <= q <= 34
844 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
846 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
848 *pfpsf |= INVALID_EXCEPTION;
849 // return Integer Indefinite
850 res = 0x8000000000000000ull;
853 // else cases that can be rounded to a 64-bit int fall through
854 // to '1 <= q + exp <= 19'
857 // n is not too large to be converted to int64: -2^63-1 < n < 2^63
858 // Note: some of the cases tested for above fall through to this point
859 // Restore C1 which may have been modified above
860 C1.w[1] = x.w[1] & MASK_COEFF;
862 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
865 res = 0xffffffffffffffffull;
867 res = 0x0000000000000000ull;
869 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
870 // -2^63 <= x <= -1 or 1 <= x < 2^63 so x can be rounded
871 // toward zero to a 64-bit signed integer
872 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
873 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
874 // chop off ind digits from the lower part of C1
875 // C1 fits in 127 bits
876 // calculate C* and f*
877 // C* is actually floor(C*) in this case
878 // C* and f* need shifting and masking, as shown by
879 // __bid_shiftright128[] and __bid_maskhigh128[]
881 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
883 // the approximation of 10^(-x) was rounded up to 118 bits
884 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
885 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
886 Cstar.w[1] = P256.w[3];
887 Cstar.w[0] = P256.w[2];
889 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
890 fstar.w[1] = P256.w[1];
891 fstar.w[0] = P256.w[0];
892 } else { // 22 <= ind - 1 <= 33
894 Cstar.w[0] = P256.w[3];
895 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
896 fstar.w[2] = P256.w[2];
897 fstar.w[1] = P256.w[1];
898 fstar.w[0] = P256.w[0];
900 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
901 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
902 // C* = floor(C*) (logical right shift; C has p decimal digits,
903 // correct by Property 1)
906 // shift right C* by Ex-128 = __bid_shiftright128[ind]
907 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
908 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
910 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
911 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
912 } else { // 22 <= ind - 1 <= 33
913 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
915 // if the result is negative and inexact, need to add 1 to it
917 // determine inexactness of the rounding of C*
918 // if (0 < f* < 10^(-x)) then
919 // the result is exact
920 // else // if (f* > T*) then
921 // the result is inexact
923 if (fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
924 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
925 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
926 if (x_sign) { // positive and inexact
928 if (Cstar.w[0] == 0x0)
931 } // else the result is exact
932 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
933 if (fstar.w[2] || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
934 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
935 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
936 if (x_sign) { // positive and inexact
938 if (Cstar.w[0] == 0x0)
941 } // else the result is exact
942 } else { // if 22 <= ind <= 33
943 if (fstar.w[3] || fstar.w[2]
944 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
945 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
946 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
947 if (x_sign) { // positive and inexact
949 if (Cstar.w[0] == 0x0)
952 } // else the result is exact
959 } else if (exp == 0) {
961 // res = +/-C (exact)
966 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
967 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
969 res = -C1.w[0] * __bid_ten2k64[exp];
971 res = C1.w[0] * __bid_ten2k64[exp];
978 /*****************************************************************************
979 * BID128_to_int64_xfloor
980 ****************************************************************************/
982 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_xfloor, x)
987 int exp; // unbiased exponent
988 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
990 unsigned int x_nr_bits;
993 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
998 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
999 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
1000 C1.w[1] = x.w[1] & MASK_COEFF;
1003 // check for NaN or Infinity
1004 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
1006 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
1007 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
1009 *pfpsf |= INVALID_EXCEPTION;
1010 // return Integer Indefinite
1011 res = 0x8000000000000000ull;
1012 } else { // x is QNaN
1014 *pfpsf |= INVALID_EXCEPTION;
1015 // return Integer Indefinite
1016 res = 0x8000000000000000ull;
1019 } else { // x is not a NaN, so it must be infinity
1020 if (!x_sign) { // x is +inf
1022 *pfpsf |= INVALID_EXCEPTION;
1023 // return Integer Indefinite
1024 res = 0x8000000000000000ull;
1025 } else { // x is -inf
1027 *pfpsf |= INVALID_EXCEPTION;
1028 // return Integer Indefinite
1029 res = 0x8000000000000000ull;
1034 // check for non-canonical values (after the check for special values)
1035 if ((C1.w[1] > 0x0001ed09bead87c0ull)
1036 || (C1.w[1] == 0x0001ed09bead87c0ull
1037 && (C1.w[0] > 0x378d8e63ffffffffull))
1038 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
1039 res = 0x0000000000000000ull;
1041 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
1043 res = 0x0000000000000000ull;
1045 } else { // x is not special and is not zero
1047 // q = nr. of decimal digits in x
1048 // determine first the nr. of bits in x
1050 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
1051 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1052 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
1053 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
1055 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1056 } else { // x < 2^32
1057 tmp1.d = (double) (C1.w[0]); // exact conversion
1059 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1061 } else { // if x < 2^53
1062 tmp1.d = (double) C1.w[0]; // exact conversion
1064 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1066 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
1067 tmp1.d = (double) C1.w[1]; // exact conversion
1069 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1071 q = __bid_nr_digits[x_nr_bits - 1].digits;
1073 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1074 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
1075 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
1076 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
1079 exp = (x_exp >> 49) - 6176;
1080 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
1082 *pfpsf |= INVALID_EXCEPTION;
1083 // return Integer Indefinite
1084 res = 0x8000000000000000ull;
1086 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
1087 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1088 // so x rounded to an integer may or may not fit in a signed 64-bit int
1089 // the cases that do not fit are identified here; the ones that fit
1090 // fall through and will be handled with other cases further,
1091 // under '1 <= q + exp <= 19'
1092 if (x_sign) { // if n < 0 and q + exp = 19
1093 // if n < -2^63 then n is too large
1094 // too large if c(0)c(1)...c(18).c(19)...c(q-1) > 2^63
1095 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 10*2^63, 1<=q<=34
1096 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 0x50000000000000000, 1<=q<=34
1097 C.w[1] = 0x0000000000000005ull;
1098 C.w[0] = 0x0000000000000000ull;
1099 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1100 // 10^(20-q) is 64-bit, and so is C1
1101 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1102 } else if (q == 20) {
1104 } else { // if 21 <= q <= 34
1105 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1107 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] > C.w[0])) {
1109 *pfpsf |= INVALID_EXCEPTION;
1110 // return Integer Indefinite
1111 res = 0x8000000000000000ull;
1114 // else cases that can be rounded to a 64-bit int fall through
1115 // to '1 <= q + exp <= 19'
1116 } else { // if n > 0 and q + exp = 19
1117 // if n >= 2^63 then n is too large
1118 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63
1119 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*2^64, 1<=q<=34
1120 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x50000000000000000, 1<=q<=34
1121 C.w[1] = 0x0000000000000005ull;
1122 C.w[0] = 0x0000000000000000ull;
1123 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1124 // 10^(20-q) is 64-bit, and so is C1
1125 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1126 } else if (q == 20) {
1128 } else { // if 21 <= q <= 34
1129 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1131 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
1133 *pfpsf |= INVALID_EXCEPTION;
1134 // return Integer Indefinite
1135 res = 0x8000000000000000ull;
1138 // else cases that can be rounded to a 64-bit int fall through
1139 // to '1 <= q + exp <= 19'
1142 // n is not too large to be converted to int64: -2^63-1 < n < 2^63
1143 // Note: some of the cases tested for above fall through to this point
1144 // Restore C1 which may have been modified above
1145 C1.w[1] = x.w[1] & MASK_COEFF;
1147 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1149 *pfpsf |= INEXACT_EXCEPTION;
1152 res = 0xffffffffffffffffull;
1154 res = 0x0000000000000000ull;
1156 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
1157 // -2^63 <= x <= -1 or 1 <= x < 2^63 so x can be rounded
1158 // toward zero to a 64-bit signed integer
1159 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
1160 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
1161 // chop off ind digits from the lower part of C1
1162 // C1 fits in 127 bits
1163 // calculate C* and f*
1164 // C* is actually floor(C*) in this case
1165 // C* and f* need shifting and masking, as shown by
1166 // __bid_shiftright128[] and __bid_maskhigh128[]
1168 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
1169 // C* = C1 * 10^(-x)
1170 // the approximation of 10^(-x) was rounded up to 118 bits
1171 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
1172 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
1173 Cstar.w[1] = P256.w[3];
1174 Cstar.w[0] = P256.w[2];
1176 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
1177 fstar.w[1] = P256.w[1];
1178 fstar.w[0] = P256.w[0];
1179 } else { // 22 <= ind - 1 <= 33
1181 Cstar.w[0] = P256.w[3];
1182 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
1183 fstar.w[2] = P256.w[2];
1184 fstar.w[1] = P256.w[1];
1185 fstar.w[0] = P256.w[0];
1187 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
1188 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
1189 // C* = floor(C*) (logical right shift; C has p decimal digits,
1190 // correct by Property 1)
1191 // n = C* * 10^(e+x)
1193 // shift right C* by Ex-128 = __bid_shiftright128[ind]
1194 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
1195 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
1197 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
1198 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
1199 } else { // 22 <= ind - 1 <= 33
1200 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
1202 // if the result is negative and inexact, need to add 1 to it
1204 // determine inexactness of the rounding of C*
1205 // if (0 < f* < 10^(-x)) then
1206 // the result is exact
1207 // else // if (f* > T*) then
1208 // the result is inexact
1210 if (fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1211 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1212 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1213 if (x_sign) { // positive and inexact
1215 if (Cstar.w[0] == 0x0)
1218 // set the inexact flag
1219 *pfpsf |= INEXACT_EXCEPTION;
1220 } // else the result is exact
1221 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
1222 if (fstar.w[2] || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1223 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1224 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1225 if (x_sign) { // positive and inexact
1227 if (Cstar.w[0] == 0x0)
1230 // set the inexact flag
1231 *pfpsf |= INEXACT_EXCEPTION;
1232 } // else the result is exact
1233 } else { // if 22 <= ind <= 33
1234 if (fstar.w[3] || fstar.w[2]
1235 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1236 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1237 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1238 if (x_sign) { // positive and inexact
1240 if (Cstar.w[0] == 0x0)
1243 // set the inexact flag
1244 *pfpsf |= INEXACT_EXCEPTION;
1245 } // else the result is exact
1252 } else if (exp == 0) {
1254 // res = +/-C (exact)
1259 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
1260 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
1262 res = -C1.w[0] * __bid_ten2k64[exp];
1264 res = C1.w[0] * __bid_ten2k64[exp];
1271 /*****************************************************************************
1272 * BID128_to_int64_ceil
1273 ****************************************************************************/
1275 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_ceil, x)
1280 int exp; // unbiased exponent
1281 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
1282 BID_UI64DOUBLE tmp1;
1283 unsigned int x_nr_bits;
1286 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
1291 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1292 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
1293 C1.w[1] = x.w[1] & MASK_COEFF;
1296 // check for NaN or Infinity
1297 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
1299 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
1300 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
1302 *pfpsf |= INVALID_EXCEPTION;
1303 // return Integer Indefinite
1304 res = 0x8000000000000000ull;
1305 } else { // x is QNaN
1307 *pfpsf |= INVALID_EXCEPTION;
1308 // return Integer Indefinite
1309 res = 0x8000000000000000ull;
1312 } else { // x is not a NaN, so it must be infinity
1313 if (!x_sign) { // x is +inf
1315 *pfpsf |= INVALID_EXCEPTION;
1316 // return Integer Indefinite
1317 res = 0x8000000000000000ull;
1318 } else { // x is -inf
1320 *pfpsf |= INVALID_EXCEPTION;
1321 // return Integer Indefinite
1322 res = 0x8000000000000000ull;
1327 // check for non-canonical values (after the check for special values)
1328 if ((C1.w[1] > 0x0001ed09bead87c0ull)
1329 || (C1.w[1] == 0x0001ed09bead87c0ull
1330 && (C1.w[0] > 0x378d8e63ffffffffull))
1331 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
1332 res = 0x0000000000000000ull;
1334 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
1336 res = 0x0000000000000000ull;
1338 } else { // x is not special and is not zero
1340 // q = nr. of decimal digits in x
1341 // determine first the nr. of bits in x
1343 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
1344 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1345 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
1346 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
1348 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1349 } else { // x < 2^32
1350 tmp1.d = (double) (C1.w[0]); // exact conversion
1352 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1354 } else { // if x < 2^53
1355 tmp1.d = (double) C1.w[0]; // exact conversion
1357 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1359 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
1360 tmp1.d = (double) C1.w[1]; // exact conversion
1362 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1364 q = __bid_nr_digits[x_nr_bits - 1].digits;
1366 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1367 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
1368 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
1369 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
1372 exp = (x_exp >> 49) - 6176;
1373 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
1375 *pfpsf |= INVALID_EXCEPTION;
1376 // return Integer Indefinite
1377 res = 0x8000000000000000ull;
1379 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
1380 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1381 // so x rounded to an integer may or may not fit in a signed 64-bit int
1382 // the cases that do not fit are identified here; the ones that fit
1383 // fall through and will be handled with other cases further,
1384 // under '1 <= q + exp <= 19'
1385 if (x_sign) { // if n < 0 and q + exp = 19
1386 // if n <= -2^63 - 1 then n is too large
1387 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63+1
1388 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 5*(2^64+2), 1<=q<=34
1389 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 0x5000000000000000a, 1<=q<=34
1390 C.w[1] = 0x0000000000000005ull;
1391 C.w[0] = 0x000000000000000aull;
1392 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1393 // 10^(20-q) is 64-bit, and so is C1
1394 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1395 } else if (q == 20) {
1397 } else { // if 21 <= q <= 34
1398 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1400 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
1402 *pfpsf |= INVALID_EXCEPTION;
1403 // return Integer Indefinite
1404 res = 0x8000000000000000ull;
1407 // else cases that can be rounded to a 64-bit int fall through
1408 // to '1 <= q + exp <= 19'
1409 } else { // if n > 0 and q + exp = 19
1410 // if n > 2^63 - 1 then n is too large
1411 // too large if c(0)c(1)...c(18).c(19)...c(q-1) > 2^63 - 1
1412 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 > 10*(2^63-1), 1<=q<=34
1413 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 > 0x4fffffffffffffff6, 1<=q<=34
1414 C.w[1] = 0x0000000000000004ull;
1415 C.w[0] = 0xfffffffffffffff6ull;
1416 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1417 // 10^(20-q) is 64-bit, and so is C1
1418 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1419 } else if (q == 20) {
1421 } else { // if 21 <= q <= 34
1422 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1424 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] > C.w[0])) {
1426 *pfpsf |= INVALID_EXCEPTION;
1427 // return Integer Indefinite
1428 res = 0x8000000000000000ull;
1431 // else cases that can be rounded to a 64-bit int fall through
1432 // to '1 <= q + exp <= 19'
1435 // n is not too large to be converted to int64: -2^63-1 < n <= 2^63 - 1
1436 // Note: some of the cases tested for above fall through to this point
1437 // Restore C1 which may have been modified above
1438 C1.w[1] = x.w[1] & MASK_COEFF;
1440 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1443 res = 0x0000000000000000ull;
1445 res = 0x0000000000000001ull;
1447 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
1448 // -2^63-1 < x <= -1 or 1 <= x <= 2^63 - 1 so x can be rounded
1449 // up to a 64-bit signed integer
1450 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
1451 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
1452 // chop off ind digits from the lower part of C1
1453 // C1 fits in 127 bits
1454 // calculate C* and f*
1455 // C* is actually floor(C*) in this case
1456 // C* and f* need shifting and masking, as shown by
1457 // __bid_shiftright128[] and __bid_maskhigh128[]
1459 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
1460 // C* = C1 * 10^(-x)
1461 // the approximation of 10^(-x) was rounded up to 118 bits
1462 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
1463 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
1464 Cstar.w[1] = P256.w[3];
1465 Cstar.w[0] = P256.w[2];
1467 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
1468 fstar.w[1] = P256.w[1];
1469 fstar.w[0] = P256.w[0];
1470 } else { // 22 <= ind - 1 <= 33
1472 Cstar.w[0] = P256.w[3];
1473 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
1474 fstar.w[2] = P256.w[2];
1475 fstar.w[1] = P256.w[1];
1476 fstar.w[0] = P256.w[0];
1478 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
1479 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
1480 // C* = floor(C*) (logical right shift; C has p decimal digits,
1481 // correct by Property 1)
1482 // n = C* * 10^(e+x)
1484 // shift right C* by Ex-128 = __bid_shiftright128[ind]
1485 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
1486 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
1488 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
1489 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
1490 } else { // 22 <= ind - 1 <= 33
1491 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
1493 // if the result is positive and inexact, need to add 1 to it
1495 // determine inexactness of the rounding of C*
1496 // if (0 < f* < 10^(-x)) then
1497 // the result is exact
1498 // else // if (f* > T*) then
1499 // the result is inexact
1501 if (fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1502 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1503 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1504 if (!x_sign) { // positive and inexact
1506 if (Cstar.w[0] == 0x0)
1509 } // else the result is exact
1510 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
1511 if (fstar.w[2] || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1512 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1513 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1514 if (!x_sign) { // positive and inexact
1516 if (Cstar.w[0] == 0x0)
1519 } // else the result is exact
1520 } else { // if 22 <= ind <= 33
1521 if (fstar.w[3] || fstar.w[2]
1522 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1523 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1524 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1525 if (!x_sign) { // positive and inexact
1527 if (Cstar.w[0] == 0x0)
1530 } // else the result is exact
1536 } else if (exp == 0) {
1538 // res = +/-C (exact)
1543 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
1544 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
1546 res = -C1.w[0] * __bid_ten2k64[exp];
1548 res = C1.w[0] * __bid_ten2k64[exp];
1555 /*****************************************************************************
1556 * BID128_to_int64_xceil
1557 ****************************************************************************/
1559 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_xceil, x)
1564 int exp; // unbiased exponent
1565 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
1566 BID_UI64DOUBLE tmp1;
1567 unsigned int x_nr_bits;
1570 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
1575 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1576 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
1577 C1.w[1] = x.w[1] & MASK_COEFF;
1580 // check for NaN or Infinity
1581 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
1583 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
1584 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
1586 *pfpsf |= INVALID_EXCEPTION;
1587 // return Integer Indefinite
1588 res = 0x8000000000000000ull;
1589 } else { // x is QNaN
1591 *pfpsf |= INVALID_EXCEPTION;
1592 // return Integer Indefinite
1593 res = 0x8000000000000000ull;
1596 } else { // x is not a NaN, so it must be infinity
1597 if (!x_sign) { // x is +inf
1599 *pfpsf |= INVALID_EXCEPTION;
1600 // return Integer Indefinite
1601 res = 0x8000000000000000ull;
1602 } else { // x is -inf
1604 *pfpsf |= INVALID_EXCEPTION;
1605 // return Integer Indefinite
1606 res = 0x8000000000000000ull;
1611 // check for non-canonical values (after the check for special values)
1612 if ((C1.w[1] > 0x0001ed09bead87c0ull)
1613 || (C1.w[1] == 0x0001ed09bead87c0ull
1614 && (C1.w[0] > 0x378d8e63ffffffffull))
1615 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
1616 res = 0x0000000000000000ull;
1618 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
1620 res = 0x0000000000000000ull;
1622 } else { // x is not special and is not zero
1624 // q = nr. of decimal digits in x
1625 // determine first the nr. of bits in x
1627 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
1628 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1629 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
1630 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
1632 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1633 } else { // x < 2^32
1634 tmp1.d = (double) (C1.w[0]); // exact conversion
1636 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1638 } else { // if x < 2^53
1639 tmp1.d = (double) C1.w[0]; // exact conversion
1641 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1643 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
1644 tmp1.d = (double) C1.w[1]; // exact conversion
1646 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1648 q = __bid_nr_digits[x_nr_bits - 1].digits;
1650 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1651 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
1652 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
1653 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
1656 exp = (x_exp >> 49) - 6176;
1657 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
1659 *pfpsf |= INVALID_EXCEPTION;
1660 // return Integer Indefinite
1661 res = 0x8000000000000000ull;
1663 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
1664 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1665 // so x rounded to an integer may or may not fit in a signed 64-bit int
1666 // the cases that do not fit are identified here; the ones that fit
1667 // fall through and will be handled with other cases further,
1668 // under '1 <= q + exp <= 19'
1669 if (x_sign) { // if n < 0 and q + exp = 19
1670 // if n <= -2^63 - 1 then n is too large
1671 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63+1
1672 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 5*(2^64+2), 1<=q<=34
1673 // <=> 0.c(0)c(1)...c(q-1) * 10^20 > 0x5000000000000000a, 1<=q<=34
1674 C.w[1] = 0x0000000000000005ull;
1675 C.w[0] = 0x000000000000000aull;
1676 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1677 // 10^(20-q) is 64-bit, and so is C1
1678 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1679 } else if (q == 20) {
1681 } else { // if 21 <= q <= 34
1682 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1684 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
1686 *pfpsf |= INVALID_EXCEPTION;
1687 // return Integer Indefinite
1688 res = 0x8000000000000000ull;
1691 // else cases that can be rounded to a 64-bit int fall through
1692 // to '1 <= q + exp <= 19'
1693 } else { // if n > 0 and q + exp = 19
1694 // if n > 2^63 - 1 then n is too large
1695 // too large if c(0)c(1)...c(18).c(19)...c(q-1) > 2^63 - 1
1696 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 > 10*(2^63-1), 1<=q<=34
1697 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 > 0x4fffffffffffffff6, 1<=q<=34
1698 C.w[1] = 0x0000000000000004ull;
1699 C.w[0] = 0xfffffffffffffff6ull;
1700 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1701 // 10^(20-q) is 64-bit, and so is C1
1702 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1703 } else if (q == 20) {
1705 } else { // if 21 <= q <= 34
1706 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1708 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] > C.w[0])) {
1710 *pfpsf |= INVALID_EXCEPTION;
1711 // return Integer Indefinite
1712 res = 0x8000000000000000ull;
1715 // else cases that can be rounded to a 64-bit int fall through
1716 // to '1 <= q + exp <= 19'
1719 // n is not too large to be converted to int64: -2^63-1 < n <= 2^63 - 1
1720 // Note: some of the cases tested for above fall through to this point
1721 // Restore C1 which may have been modified above
1722 C1.w[1] = x.w[1] & MASK_COEFF;
1724 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1726 *pfpsf |= INEXACT_EXCEPTION;
1729 res = 0x0000000000000000ull;
1731 res = 0x0000000000000001ull;
1733 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
1734 // -2^63-1 < x <= -1 or 1 <= x <= 2^63 - 1 so x can be rounded
1735 // up to a 64-bit signed integer
1736 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
1737 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
1738 // chop off ind digits from the lower part of C1
1739 // C1 fits in 127 bits
1740 // calculate C* and f*
1741 // C* is actually floor(C*) in this case
1742 // C* and f* need shifting and masking, as shown by
1743 // __bid_shiftright128[] and __bid_maskhigh128[]
1745 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
1746 // C* = C1 * 10^(-x)
1747 // the approximation of 10^(-x) was rounded up to 118 bits
1748 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
1749 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
1750 Cstar.w[1] = P256.w[3];
1751 Cstar.w[0] = P256.w[2];
1753 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
1754 fstar.w[1] = P256.w[1];
1755 fstar.w[0] = P256.w[0];
1756 } else { // 22 <= ind - 1 <= 33
1758 Cstar.w[0] = P256.w[3];
1759 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
1760 fstar.w[2] = P256.w[2];
1761 fstar.w[1] = P256.w[1];
1762 fstar.w[0] = P256.w[0];
1764 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
1765 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
1766 // C* = floor(C*) (logical right shift; C has p decimal digits,
1767 // correct by Property 1)
1768 // n = C* * 10^(e+x)
1770 // shift right C* by Ex-128 = __bid_shiftright128[ind]
1771 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
1772 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
1774 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
1775 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
1776 } else { // 22 <= ind - 1 <= 33
1777 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
1779 // if the result is positive and inexact, need to add 1 to it
1781 // determine inexactness of the rounding of C*
1782 // if (0 < f* < 10^(-x)) then
1783 // the result is exact
1784 // else // if (f* > T*) then
1785 // the result is inexact
1787 if (fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1788 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1789 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1790 if (!x_sign) { // positive and inexact
1792 if (Cstar.w[0] == 0x0)
1795 // set the inexact flag
1796 *pfpsf |= INEXACT_EXCEPTION;
1797 } // else the result is exact
1798 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
1799 if (fstar.w[2] || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1800 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1801 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1802 if (!x_sign) { // positive and inexact
1804 if (Cstar.w[0] == 0x0)
1807 // set the inexact flag
1808 *pfpsf |= INEXACT_EXCEPTION;
1809 } // else the result is exact
1810 } else { // if 22 <= ind <= 33
1811 if (fstar.w[3] || fstar.w[2]
1812 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
1813 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
1814 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
1815 if (!x_sign) { // positive and inexact
1817 if (Cstar.w[0] == 0x0)
1820 // set the inexact flag
1821 *pfpsf |= INEXACT_EXCEPTION;
1822 } // else the result is exact
1829 } else if (exp == 0) {
1831 // res = +/-C (exact)
1836 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
1837 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
1839 res = -C1.w[0] * __bid_ten2k64[exp];
1841 res = C1.w[0] * __bid_ten2k64[exp];
1848 /*****************************************************************************
1849 * BID128_to_int64_int
1850 ****************************************************************************/
1852 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_int, x)
1857 int exp; // unbiased exponent
1858 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
1859 BID_UI64DOUBLE tmp1;
1860 unsigned int x_nr_bits;
1863 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
1867 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1868 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
1869 C1.w[1] = x.w[1] & MASK_COEFF;
1872 // check for NaN or Infinity
1873 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
1875 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
1876 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
1878 *pfpsf |= INVALID_EXCEPTION;
1879 // return Integer Indefinite
1880 res = 0x8000000000000000ull;
1881 } else { // x is QNaN
1883 *pfpsf |= INVALID_EXCEPTION;
1884 // return Integer Indefinite
1885 res = 0x8000000000000000ull;
1888 } else { // x is not a NaN, so it must be infinity
1889 if (!x_sign) { // x is +inf
1891 *pfpsf |= INVALID_EXCEPTION;
1892 // return Integer Indefinite
1893 res = 0x8000000000000000ull;
1894 } else { // x is -inf
1896 *pfpsf |= INVALID_EXCEPTION;
1897 // return Integer Indefinite
1898 res = 0x8000000000000000ull;
1903 // check for non-canonical values (after the check for special values)
1904 if ((C1.w[1] > 0x0001ed09bead87c0ull)
1905 || (C1.w[1] == 0x0001ed09bead87c0ull
1906 && (C1.w[0] > 0x378d8e63ffffffffull))
1907 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
1908 res = 0x0000000000000000ull;
1910 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
1912 res = 0x0000000000000000ull;
1914 } else { // x is not special and is not zero
1916 // q = nr. of decimal digits in x
1917 // determine first the nr. of bits in x
1919 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
1920 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1921 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
1922 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
1924 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1925 } else { // x < 2^32
1926 tmp1.d = (double) (C1.w[0]); // exact conversion
1928 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1930 } else { // if x < 2^53
1931 tmp1.d = (double) C1.w[0]; // exact conversion
1933 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1935 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
1936 tmp1.d = (double) C1.w[1]; // exact conversion
1938 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1940 q = __bid_nr_digits[x_nr_bits - 1].digits;
1942 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1943 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
1944 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
1945 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
1948 exp = (x_exp >> 49) - 6176;
1949 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
1951 *pfpsf |= INVALID_EXCEPTION;
1952 // return Integer Indefinite
1953 res = 0x8000000000000000ull;
1955 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
1956 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
1957 // so x rounded to an integer may or may not fit in a signed 64-bit int
1958 // the cases that do not fit are identified here; the ones that fit
1959 // fall through and will be handled with other cases further,
1960 // under '1 <= q + exp <= 19'
1961 if (x_sign) { // if n < 0 and q + exp = 19
1962 // if n <= -2^63 - 1 then n is too large
1963 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63+1
1964 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64+2), 1<=q<=34
1965 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 0x5000000000000000a, 1<=q<=34
1966 C.w[1] = 0x0000000000000005ull;
1967 C.w[0] = 0x000000000000000aull;
1968 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1969 // 10^(20-q) is 64-bit, and so is C1
1970 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1971 } else if (q == 20) {
1973 } else { // if 21 <= q <= 34
1974 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
1976 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
1978 *pfpsf |= INVALID_EXCEPTION;
1979 // return Integer Indefinite
1980 res = 0x8000000000000000ull;
1983 // else cases that can be rounded to a 64-bit int fall through
1984 // to '1 <= q + exp <= 19'
1985 } else { // if n > 0 and q + exp = 19
1986 // if n >= 2^63 then n is too large
1987 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63
1988 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*2^64, 1<=q<=34
1989 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x50000000000000000, 1<=q<=34
1990 C.w[1] = 0x0000000000000005ull;
1991 C.w[0] = 0x0000000000000000ull;
1992 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
1993 // 10^(20-q) is 64-bit, and so is C1
1994 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
1995 } else if (q == 20) {
1997 } else { // if 21 <= q <= 34
1998 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2000 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2002 *pfpsf |= INVALID_EXCEPTION;
2003 // return Integer Indefinite
2004 res = 0x8000000000000000ull;
2007 // else cases that can be rounded to a 64-bit int fall through
2008 // to '1 <= q + exp <= 19'
2011 // n is not too large to be converted to int64: -2^63-1 < n < 2^63
2012 // Note: some of the cases tested for above fall through to this point
2013 // Restore C1 which may have been modified above
2014 C1.w[1] = x.w[1] & MASK_COEFF;
2016 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
2018 res = 0x0000000000000000ull;
2020 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
2021 // -2^63-1 < x <= -1 or 1 <= x < 2^63 so x can be rounded
2022 // toward zero to a 64-bit signed integer
2023 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
2024 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
2025 // chop off ind digits from the lower part of C1
2026 // C1 fits in 127 bits
2027 // calculate C* and f*
2028 // C* is actually floor(C*) in this case
2029 // C* and f* need shifting and masking, as shown by
2030 // __bid_shiftright128[] and __bid_maskhigh128[]
2032 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
2033 // C* = C1 * 10^(-x)
2034 // the approximation of 10^(-x) was rounded up to 118 bits
2035 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
2036 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2037 Cstar.w[1] = P256.w[3];
2038 Cstar.w[0] = P256.w[2];
2039 } else { // 22 <= ind - 1 <= 33
2041 Cstar.w[0] = P256.w[3];
2043 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
2044 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
2045 // C* = floor(C*) (logical right shift; C has p decimal digits,
2046 // correct by Property 1)
2047 // n = C* * 10^(e+x)
2049 // shift right C* by Ex-128 = __bid_shiftright128[ind]
2050 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
2051 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2053 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
2054 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
2055 } else { // 22 <= ind - 1 <= 33
2056 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
2062 } else if (exp == 0) {
2064 // res = +/-C (exact)
2069 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
2070 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
2072 res = -C1.w[0] * __bid_ten2k64[exp];
2074 res = C1.w[0] * __bid_ten2k64[exp];
2081 /*****************************************************************************
2082 * BID128_to_xint64_xint
2083 ****************************************************************************/
2085 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_xint, x)
2090 int exp; // unbiased exponent
2091 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
2092 BID_UI64DOUBLE tmp1;
2093 unsigned int x_nr_bits;
2096 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
2101 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
2102 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
2103 C1.w[1] = x.w[1] & MASK_COEFF;
2106 // check for NaN or Infinity
2107 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
2109 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
2110 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
2112 *pfpsf |= INVALID_EXCEPTION;
2113 // return Integer Indefinite
2114 res = 0x8000000000000000ull;
2115 } else { // x is QNaN
2117 *pfpsf |= INVALID_EXCEPTION;
2118 // return Integer Indefinite
2119 res = 0x8000000000000000ull;
2122 } else { // x is not a NaN, so it must be infinity
2123 if (!x_sign) { // x is +inf
2125 *pfpsf |= INVALID_EXCEPTION;
2126 // return Integer Indefinite
2127 res = 0x8000000000000000ull;
2128 } else { // x is -inf
2130 *pfpsf |= INVALID_EXCEPTION;
2131 // return Integer Indefinite
2132 res = 0x8000000000000000ull;
2137 // check for non-canonical values (after the check for special values)
2138 if ((C1.w[1] > 0x0001ed09bead87c0ull)
2139 || (C1.w[1] == 0x0001ed09bead87c0ull
2140 && (C1.w[0] > 0x378d8e63ffffffffull))
2141 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
2142 res = 0x0000000000000000ull;
2144 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
2146 res = 0x0000000000000000ull;
2148 } else { // x is not special and is not zero
2150 // q = nr. of decimal digits in x
2151 // determine first the nr. of bits in x
2153 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
2154 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2155 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
2156 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
2158 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2159 } else { // x < 2^32
2160 tmp1.d = (double) (C1.w[0]); // exact conversion
2162 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2164 } else { // if x < 2^53
2165 tmp1.d = (double) C1.w[0]; // exact conversion
2167 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2169 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
2170 tmp1.d = (double) C1.w[1]; // exact conversion
2172 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2174 q = __bid_nr_digits[x_nr_bits - 1].digits;
2176 q = __bid_nr_digits[x_nr_bits - 1].digits1;
2177 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
2178 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
2179 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
2182 exp = (x_exp >> 49) - 6176;
2183 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
2185 *pfpsf |= INVALID_EXCEPTION;
2186 // return Integer Indefinite
2187 res = 0x8000000000000000ull;
2189 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
2190 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
2191 // so x rounded to an integer may or may not fit in a signed 64-bit int
2192 // the cases that do not fit are identified here; the ones that fit
2193 // fall through and will be handled with other cases further,
2194 // under '1 <= q + exp <= 19'
2195 if (x_sign) { // if n < 0 and q + exp = 19
2196 // if n <= -2^63 - 1 then n is too large
2197 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63+1
2198 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64+2), 1<=q<=34
2199 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 0x5000000000000000a, 1<=q<=34
2200 C.w[1] = 0x0000000000000005ull;
2201 C.w[0] = 0x000000000000000aull;
2202 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
2203 // 10^(20-q) is 64-bit, and so is C1
2204 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
2205 } else if (q == 20) {
2207 } else { // if 21 <= q <= 34
2208 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2210 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2212 *pfpsf |= INVALID_EXCEPTION;
2213 // return Integer Indefinite
2214 res = 0x8000000000000000ull;
2217 // else cases that can be rounded to a 64-bit int fall through
2218 // to '1 <= q + exp <= 19'
2219 } else { // if n > 0 and q + exp = 19
2220 // if n >= 2^63 then n is too large
2221 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63
2222 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*2^64, 1<=q<=34
2223 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x50000000000000000, 1<=q<=34
2224 C.w[1] = 0x0000000000000005ull;
2225 C.w[0] = 0x0000000000000000ull;
2226 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
2227 // 10^(20-q) is 64-bit, and so is C1
2228 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
2229 } else if (q == 20) {
2231 } else { // if 21 <= q <= 34
2232 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2234 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2236 *pfpsf |= INVALID_EXCEPTION;
2237 // return Integer Indefinite
2238 res = 0x8000000000000000ull;
2241 // else cases that can be rounded to a 64-bit int fall through
2242 // to '1 <= q + exp <= 19'
2245 // n is not too large to be converted to int64: -2^63-1 < n < 2^63
2246 // Note: some of the cases tested for above fall through to this point
2247 // Restore C1 which may have been modified above
2248 C1.w[1] = x.w[1] & MASK_COEFF;
2250 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
2252 *pfpsf |= INEXACT_EXCEPTION;
2254 res = 0x0000000000000000ull;
2256 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
2257 // -2^63-1 < x <= -1 or 1 <= x < 2^63 so x can be rounded
2258 // toward zero to a 64-bit signed integer
2259 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
2260 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
2261 // chop off ind digits from the lower part of C1
2262 // C1 fits in 127 bits
2263 // calculate C* and f*
2264 // C* is actually floor(C*) in this case
2265 // C* and f* need shifting and masking, as shown by
2266 // __bid_shiftright128[] and __bid_maskhigh128[]
2268 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
2269 // C* = C1 * 10^(-x)
2270 // the approximation of 10^(-x) was rounded up to 118 bits
2271 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
2272 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2273 Cstar.w[1] = P256.w[3];
2274 Cstar.w[0] = P256.w[2];
2276 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
2277 fstar.w[1] = P256.w[1];
2278 fstar.w[0] = P256.w[0];
2279 } else { // 22 <= ind - 1 <= 33
2281 Cstar.w[0] = P256.w[3];
2282 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
2283 fstar.w[2] = P256.w[2];
2284 fstar.w[1] = P256.w[1];
2285 fstar.w[0] = P256.w[0];
2287 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
2288 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
2289 // C* = floor(C*) (logical right shift; C has p decimal digits,
2290 // correct by Property 1)
2291 // n = C* * 10^(e+x)
2293 // shift right C* by Ex-128 = __bid_shiftright128[ind]
2294 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
2295 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2297 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
2298 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
2299 } else { // 22 <= ind - 1 <= 33
2300 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
2302 // determine inexactness of the rounding of C*
2303 // if (0 < f* < 10^(-x)) then
2304 // the result is exact
2305 // else // if (f* > T*) then
2306 // the result is inexact
2308 if (fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
2309 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
2310 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
2311 // set the inexact flag
2312 *pfpsf |= INEXACT_EXCEPTION;
2313 } // else the result is exact
2314 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
2315 if (fstar.w[2] || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
2316 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
2317 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
2318 // set the inexact flag
2319 *pfpsf |= INEXACT_EXCEPTION;
2321 } else { // if 22 <= ind <= 33
2322 if (fstar.w[3] || fstar.w[2]
2323 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
2324 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
2325 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
2326 // set the inexact flag
2327 *pfpsf |= INEXACT_EXCEPTION;
2335 } else if (exp == 0) {
2337 // res = +/-C (exact)
2342 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
2343 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
2345 res = -C1.w[0] * __bid_ten2k64[exp];
2347 res = C1.w[0] * __bid_ten2k64[exp];
2354 /*****************************************************************************
2355 * BID128_to_int64_rninta
2356 ****************************************************************************/
2358 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_rninta, x)
2363 int exp; // unbiased exponent
2364 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
2366 BID_UI64DOUBLE tmp1;
2367 unsigned int x_nr_bits;
2370 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
2374 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
2375 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
2376 C1.w[1] = x.w[1] & MASK_COEFF;
2379 // check for NaN or Infinity
2380 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
2382 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
2383 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
2385 *pfpsf |= INVALID_EXCEPTION;
2386 // return Integer Indefinite
2387 res = 0x8000000000000000ull;
2388 } else { // x is QNaN
2390 *pfpsf |= INVALID_EXCEPTION;
2391 // return Integer Indefinite
2392 res = 0x8000000000000000ull;
2395 } else { // x is not a NaN, so it must be infinity
2396 if (!x_sign) { // x is +inf
2398 *pfpsf |= INVALID_EXCEPTION;
2399 // return Integer Indefinite
2400 res = 0x8000000000000000ull;
2401 } else { // x is -inf
2403 *pfpsf |= INVALID_EXCEPTION;
2404 // return Integer Indefinite
2405 res = 0x8000000000000000ull;
2410 // check for non-canonical values (after the check for special values)
2411 if ((C1.w[1] > 0x0001ed09bead87c0ull)
2412 || (C1.w[1] == 0x0001ed09bead87c0ull
2413 && (C1.w[0] > 0x378d8e63ffffffffull))
2414 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
2415 res = 0x0000000000000000ull;
2417 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
2419 res = 0x0000000000000000ull;
2421 } else { // x is not special and is not zero
2423 // q = nr. of decimal digits in x
2424 // determine first the nr. of bits in x
2426 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
2427 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2428 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
2429 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
2431 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2432 } else { // x < 2^32
2433 tmp1.d = (double) (C1.w[0]); // exact conversion
2435 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2437 } else { // if x < 2^53
2438 tmp1.d = (double) C1.w[0]; // exact conversion
2440 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2442 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
2443 tmp1.d = (double) C1.w[1]; // exact conversion
2445 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2447 q = __bid_nr_digits[x_nr_bits - 1].digits;
2449 q = __bid_nr_digits[x_nr_bits - 1].digits1;
2450 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
2451 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
2452 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
2455 exp = (x_exp >> 49) - 6176;
2456 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
2458 *pfpsf |= INVALID_EXCEPTION;
2459 // return Integer Indefinite
2460 res = 0x8000000000000000ull;
2462 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
2463 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
2464 // so x rounded to an integer may or may not fit in a signed 64-bit int
2465 // the cases that do not fit are identified here; the ones that fit
2466 // fall through and will be handled with other cases further,
2467 // under '1 <= q + exp <= 19'
2468 if (x_sign) { // if n < 0 and q + exp = 19
2469 // if n <= -2^63 - 1/2 then n is too large
2470 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63+1/2
2471 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64+1), 1<=q<=34
2472 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 0x50000000000000005, 1<=q<=34
2473 C.w[1] = 0x0000000000000005ull;
2474 C.w[0] = 0000000000000005ull;
2475 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
2476 // 10^(20-q) is 64-bit, and so is C1
2477 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
2478 } else if (q == 20) {
2480 } else { // if 21 <= q <= 34
2481 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2483 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2485 *pfpsf |= INVALID_EXCEPTION;
2486 // return Integer Indefinite
2487 res = 0x8000000000000000ull;
2490 // else cases that can be rounded to a 64-bit int fall through
2491 // to '1 <= q + exp <= 19'
2492 } else { // if n > 0 and q + exp = 19
2493 // if n >= 2^63 - 1/2 then n is too large
2494 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63-1/2
2495 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64-1), 1<=q<=34
2496 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x4fffffffffffffffb, 1<=q<=34
2497 C.w[1] = 0x0000000000000004ull;
2498 C.w[0] = 0xfffffffffffffffbull;
2499 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
2500 // 10^(20-q) is 64-bit, and so is C1
2501 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
2502 } else if (q == 20) {
2504 } else { // if 21 <= q <= 34
2505 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2507 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2509 *pfpsf |= INVALID_EXCEPTION;
2510 // return Integer Indefinite
2511 res = 0x8000000000000000ull;
2514 // else cases that can be rounded to a 64-bit int fall through
2515 // to '1 <= q + exp <= 19'
2518 // n is not too large to be converted to int64: -2^63-1/2 <= n < 2^63-1/2
2519 // Note: some of the cases tested for above fall through to this point
2520 // Restore C1 which may have been modified above
2521 C1.w[1] = x.w[1] & MASK_COEFF;
2523 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2525 res = 0x0000000000000000ull;
2527 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2528 // if 0.c(0)c(1)...c(q-1) < 0.5 <=> c(0)c(1)...c(q-1) < 5 * 10^(q-1)
2533 if (ind <= 18) { // 0 <= ind <= 18
2534 if ((C1.w[1] == 0) && (C1.w[0] < __bid_midpoint64[ind])) {
2535 res = 0x0000000000000000ull; // return 0
2536 } else if (x_sign) { // n < 0
2537 res = 0xffffffffffffffffull; // return -1
2539 res = 0x0000000000000001ull; // return +1
2541 } else { // 19 <= ind <= 33
2542 if ((C1.w[1] < __bid_midpoint128[ind - 19].w[1])
2543 || ((C1.w[1] == __bid_midpoint128[ind - 19].w[1])
2544 && (C1.w[0] < __bid_midpoint128[ind - 19].w[0]))) {
2545 res = 0x0000000000000000ull; // return 0
2546 } else if (x_sign) { // n < 0
2547 res = 0xffffffffffffffffull; // return -1
2549 res = 0x0000000000000001ull; // return +1
2552 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
2553 // -2^63-1/2 <= x <= -1 or 1 <= x < 2^63-1/2 so x can be rounded
2554 // to nearest to a 64-bit signed integer
2555 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
2556 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
2557 // chop off ind digits from the lower part of C1
2558 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 127 bits
2561 C1.w[0] = C1.w[0] + __bid_midpoint64[ind - 1];
2563 C1.w[0] = C1.w[0] + __bid_midpoint128[ind - 20].w[0];
2564 C1.w[1] = C1.w[1] + __bid_midpoint128[ind - 20].w[1];
2566 if (C1.w[0] < tmp64)
2568 // calculate C* and f*
2569 // C* is actually floor(C*) in this case
2570 // C* and f* need shifting and masking, as shown by
2571 // __bid_shiftright128[] and __bid_maskhigh128[]
2573 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
2574 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2575 // the approximation of 10^(-x) was rounded up to 118 bits
2576 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
2577 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2578 Cstar.w[1] = P256.w[3];
2579 Cstar.w[0] = P256.w[2];
2580 } else { // 22 <= ind - 1 <= 33
2582 Cstar.w[0] = P256.w[3];
2584 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
2585 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
2586 // if (0 < f* < 10^(-x)) then the result is a midpoint
2587 // if floor(C*) is even then C* = floor(C*) - logical right
2588 // shift; C* has p decimal digits, correct by Prop. 1)
2589 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
2590 // shift; C* has p decimal digits, correct by Pr. 1)
2592 // C* = floor(C*) (logical right shift; C has p decimal digits,
2593 // correct by Property 1)
2594 // n = C* * 10^(e+x)
2596 // shift right C* by Ex-128 = __bid_shiftright128[ind]
2597 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
2598 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2600 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
2601 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
2602 } else { // 22 <= ind - 1 <= 33
2603 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
2606 // if the result was a midpoint it was rounded away from zero
2611 } else if (exp == 0) {
2613 // res = +/-C (exact)
2618 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
2619 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
2621 res = -C1.w[0] * __bid_ten2k64[exp];
2623 res = C1.w[0] * __bid_ten2k64[exp];
2630 /*****************************************************************************
2631 * BID128_to_int64_xrninta
2632 ****************************************************************************/
2634 BID128_FUNCTION_ARG1_NORND_CUSTOMRESTYPE(SINT64, __bid128_to_int64_xrninta, x)
2639 int exp; // unbiased exponent
2640 // Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are UINT64)
2641 UINT64 tmp64, tmp64A;
2642 BID_UI64DOUBLE tmp1;
2643 unsigned int x_nr_bits;
2646 UINT128 Cstar; // C* represents up to 34 decimal digits ~ 113 bits
2651 x_sign = x.w[1] & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
2652 x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
2653 C1.w[1] = x.w[1] & MASK_COEFF;
2656 // check for NaN or Infinity
2657 if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
2659 if ((x.w[1] & MASK_NAN) == MASK_NAN) { // x is NAN
2660 if ((x.w[1] & MASK_SNAN) == MASK_SNAN) { // x is SNAN
2662 *pfpsf |= INVALID_EXCEPTION;
2663 // return Integer Indefinite
2664 res = 0x8000000000000000ull;
2665 } else { // x is QNaN
2667 *pfpsf |= INVALID_EXCEPTION;
2668 // return Integer Indefinite
2669 res = 0x8000000000000000ull;
2672 } else { // x is not a NaN, so it must be infinity
2673 if (!x_sign) { // x is +inf
2675 *pfpsf |= INVALID_EXCEPTION;
2676 // return Integer Indefinite
2677 res = 0x8000000000000000ull;
2678 } else { // x is -inf
2680 *pfpsf |= INVALID_EXCEPTION;
2681 // return Integer Indefinite
2682 res = 0x8000000000000000ull;
2687 // check for non-canonical values (after the check for special values)
2688 if ((C1.w[1] > 0x0001ed09bead87c0ull)
2689 || (C1.w[1] == 0x0001ed09bead87c0ull
2690 && (C1.w[0] > 0x378d8e63ffffffffull))
2691 || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
2692 res = 0x0000000000000000ull;
2694 } else if ((C1.w[1] == 0x0ull) && (C1.w[0] == 0x0ull)) {
2696 res = 0x0000000000000000ull;
2698 } else { // x is not special and is not zero
2700 // q = nr. of decimal digits in x
2701 // determine first the nr. of bits in x
2703 if (C1.w[0] >= 0x0020000000000000ull) { // x >= 2^53
2704 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2705 if (C1.w[0] >= 0x0000000100000000ull) { // x >= 2^32
2706 tmp1.d = (double) (C1.w[0] >> 32); // exact conversion
2708 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2709 } else { // x < 2^32
2710 tmp1.d = (double) (C1.w[0]); // exact conversion
2712 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2714 } else { // if x < 2^53
2715 tmp1.d = (double) C1.w[0]; // exact conversion
2717 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2719 } else { // C1.w[1] != 0 => nr. bits = 64 + nr_bits (C1.w[1])
2720 tmp1.d = (double) C1.w[1]; // exact conversion
2722 65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2724 q = __bid_nr_digits[x_nr_bits - 1].digits;
2726 q = __bid_nr_digits[x_nr_bits - 1].digits1;
2727 if (C1.w[1] > __bid_nr_digits[x_nr_bits - 1].threshold_hi
2728 || (C1.w[1] == __bid_nr_digits[x_nr_bits - 1].threshold_hi
2729 && C1.w[0] >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
2732 exp = (x_exp >> 49) - 6176;
2733 if ((q + exp) > 19) { // x >= 10^19 ~= 2^63.11... (cannot fit in SINT64)
2735 *pfpsf |= INVALID_EXCEPTION;
2736 // return Integer Indefinite
2737 res = 0x8000000000000000ull;
2739 } else if ((q + exp) == 19) { // x = c(0)c(1)...c(18).c(19)...c(q-1)
2740 // in this case 2^63.11... ~= 10^19 <= x < 10^20 ~= 2^66.43...
2741 // so x rounded to an integer may or may not fit in a signed 64-bit int
2742 // the cases that do not fit are identified here; the ones that fit
2743 // fall through and will be handled with other cases further,
2744 // under '1 <= q + exp <= 19'
2745 if (x_sign) { // if n < 0 and q + exp = 19
2746 // if n <= -2^63 - 1/2 then n is too large
2747 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63+1/2
2748 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64+1), 1<=q<=34
2749 // <=> 0.c(0)c(1)...c(q-1) * 10^20 >= 0x50000000000000005, 1<=q<=34
2750 C.w[1] = 0x0000000000000005ull;
2751 C.w[0] = 0000000000000005ull;
2752 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
2753 // 10^(20-q) is 64-bit, and so is C1
2754 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
2755 } else if (q == 20) {
2757 } else { // if 21 <= q <= 34
2758 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2760 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2762 *pfpsf |= INVALID_EXCEPTION;
2763 // return Integer Indefinite
2764 res = 0x8000000000000000ull;
2767 // else cases that can be rounded to a 64-bit int fall through
2768 // to '1 <= q + exp <= 19'
2769 } else { // if n > 0 and q + exp = 19
2770 // if n >= 2^63 - 1/2 then n is too large
2771 // too large if c(0)c(1)...c(18).c(19)...c(q-1) >= 2^63-1/2
2772 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 5*(2^64-1), 1<=q<=34
2773 // <=> if 0.c(0)c(1)...c(q-1) * 10^20 >= 0x4fffffffffffffffb, 1<=q<=34
2774 C.w[1] = 0x0000000000000004ull;
2775 C.w[0] = 0xfffffffffffffffbull;
2776 if (q <= 19) { // 1 <= q <= 19 => 1 <= 20-q <= 19 =>
2777 // 10^(20-q) is 64-bit, and so is C1
2778 __mul_64x64_to_128MACH (C1, C1.w[0], __bid_ten2k64[20 - q]);
2779 } else if (q == 20) {
2781 } else { // if 21 <= q <= 34
2782 __mul_128x64_to_128 (C, __bid_ten2k64[q - 20], C); // max 47-bit x 67-bit
2784 if (C1.w[1] > C.w[1] || (C1.w[1] == C.w[1] && C1.w[0] >= C.w[0])) {
2786 *pfpsf |= INVALID_EXCEPTION;
2787 // return Integer Indefinite
2788 res = 0x8000000000000000ull;
2791 // else cases that can be rounded to a 64-bit int fall through
2792 // to '1 <= q + exp <= 19'
2795 // n is not too large to be converted to int64: -2^63-1/2 <= n < 2^63-1/2
2796 // Note: some of the cases tested for above fall through to this point
2797 // Restore C1 which may have been modified above
2798 C1.w[1] = x.w[1] & MASK_COEFF;
2800 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2802 *pfpsf |= INEXACT_EXCEPTION;
2804 res = 0x0000000000000000ull;
2806 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2807 // if 0.c(0)c(1)...c(q-1) < 0.5 <=> c(0)c(1)...c(q-1) < 5 * 10^(q-1)
2812 if (ind <= 18) { // 0 <= ind <= 18
2813 if ((C1.w[1] == 0) && (C1.w[0] < __bid_midpoint64[ind])) {
2814 res = 0x0000000000000000ull; // return 0
2815 } else if (x_sign) { // n < 0
2816 res = 0xffffffffffffffffull; // return -1
2818 res = 0x0000000000000001ull; // return +1
2820 } else { // 19 <= ind <= 33
2821 if ((C1.w[1] < __bid_midpoint128[ind - 19].w[1])
2822 || ((C1.w[1] == __bid_midpoint128[ind - 19].w[1])
2823 && (C1.w[0] < __bid_midpoint128[ind - 19].w[0]))) {
2824 res = 0x0000000000000000ull; // return 0
2825 } else if (x_sign) { // n < 0
2826 res = 0xffffffffffffffffull; // return -1
2828 res = 0x0000000000000001ull; // return +1
2832 *pfpsf |= INEXACT_EXCEPTION;
2833 } else { // if (1 <= q + exp <= 19, 1 <= q <= 34, -33 <= exp <= 18)
2834 // -2^63-1/2 <= x <= -1 or 1 <= x < 2^63-1/2 so x can be rounded
2835 // to nearest to a 64-bit signed integer
2836 if (exp < 0) { // 2 <= q <= 34, -33 <= exp <= -1, 1 <= q + exp <= 19
2837 ind = -exp; // 1 <= ind <= 33; ind is a synonym for 'x'
2838 // chop off ind digits from the lower part of C1
2839 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 127 bits
2842 C1.w[0] = C1.w[0] + __bid_midpoint64[ind - 1];
2844 C1.w[0] = C1.w[0] + __bid_midpoint128[ind - 20].w[0];
2845 C1.w[1] = C1.w[1] + __bid_midpoint128[ind - 20].w[1];
2847 if (C1.w[0] < tmp64)
2849 // calculate C* and f*
2850 // C* is actually floor(C*) in this case
2851 // C* and f* need shifting and masking, as shown by
2852 // __bid_shiftright128[] and __bid_maskhigh128[]
2854 // kx = 10^(-x) = __bid_ten2mk128[ind - 1]
2855 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2856 // the approximation of 10^(-x) was rounded up to 118 bits
2857 __mul_128x128_to_256 (P256, C1, __bid_ten2mk128[ind - 1]);
2858 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2859 Cstar.w[1] = P256.w[3];
2860 Cstar.w[0] = P256.w[2];
2862 fstar.w[2] = P256.w[2] & __bid_maskhigh128[ind - 1];
2863 fstar.w[1] = P256.w[1];
2864 fstar.w[0] = P256.w[0];
2865 } else { // 22 <= ind - 1 <= 33
2867 Cstar.w[0] = P256.w[3];
2868 fstar.w[3] = P256.w[3] & __bid_maskhigh128[ind - 1];
2869 fstar.w[2] = P256.w[2];
2870 fstar.w[1] = P256.w[1];
2871 fstar.w[0] = P256.w[0];
2873 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind], e.g.
2874 // if x=1, T*=__bid_ten2mk128trunc[0]=0x19999999999999999999999999999999
2875 // if (0 < f* < 10^(-x)) then the result is a midpoint
2876 // if floor(C*) is even then C* = floor(C*) - logical right
2877 // shift; C* has p decimal digits, correct by Prop. 1)
2878 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
2879 // shift; C* has p decimal digits, correct by Pr. 1)
2881 // C* = floor(C*) (logical right shift; C has p decimal digits,
2882 // correct by Property 1)
2883 // n = C* * 10^(e+x)
2885 // shift right C* by Ex-128 = __bid_shiftright128[ind]
2886 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 102
2887 if (ind - 1 <= 21) { // 0 <= ind - 1 <= 21
2889 (Cstar.w[0] >> shift) | (Cstar.w[1] << (64 - shift));
2890 // redundant, it will be 0! Cstar.w[1] = (Cstar.w[1] >> shift);
2891 } else { // 22 <= ind - 1 <= 33
2892 Cstar.w[0] = (Cstar.w[0] >> (shift - 64)); // 2 <= shift - 64 <= 38
2894 // determine inexactness of the rounding of C*
2895 // if (0 < f* - 1/2 < 10^(-x)) then
2896 // the result is exact
2897 // else // if (f* - 1/2 > T*) then
2898 // the result is inexact
2900 if (fstar.w[1] > 0x8000000000000000ull ||
2901 (fstar.w[1] == 0x8000000000000000ull && fstar.w[0] > 0x0ull)) {
2902 // f* > 1/2 and the result may be exact
2903 tmp64 = fstar.w[1] - 0x8000000000000000ull; // f* - 1/2
2904 if ((tmp64 > __bid_ten2mk128trunc[ind - 1].w[1]
2905 || (tmp64 == __bid_ten2mk128trunc[ind - 1].w[1]
2906 && fstar.w[0] >= __bid_ten2mk128trunc[ind - 1].w[0]))) {
2907 // set the inexact flag
2908 *pfpsf |= INEXACT_EXCEPTION;
2909 } // else the result is exact
2910 } else { // the result is inexact; f2* <= 1/2
2911 // set the inexact flag
2912 *pfpsf |= INEXACT_EXCEPTION;
2914 } else if (ind - 1 <= 21) { // if 3 <= ind <= 21
2915 if (fstar.w[3] > 0x0 || (fstar.w[3] == 0x0
2916 && fstar.w[2] > __bid_one_half128[ind - 1]) || (fstar.w[3] == 0x0
2917 && fstar.w[2] == __bid_one_half128[ind - 1] && (fstar.w[1]
2919 // f2* > 1/2 and the result may be exact
2920 // Calculate f2* - 1/2
2921 tmp64 = fstar.w[2] - __bid_one_half128[ind - 1];
2922 tmp64A = fstar.w[3];
2923 if (tmp64 > fstar.w[2])
2926 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
2927 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
2928 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
2929 // set the inexact flag
2930 *pfpsf |= INEXACT_EXCEPTION;
2931 } // else the result is exact
2932 } else { // the result is inexact; f2* <= 1/2
2933 // set the inexact flag
2934 *pfpsf |= INEXACT_EXCEPTION;
2936 } else { // if 22 <= ind <= 33
2937 if (fstar.w[3] > __bid_one_half128[ind - 1]
2938 || (fstar.w[3] == __bid_one_half128[ind - 1] && (fstar.w[2]
2941 // f2* > 1/2 and the result may be exact
2942 // Calculate f2* - 1/2
2943 tmp64 = fstar.w[3] - __bid_one_half128[ind - 1];
2944 if (tmp64 || fstar.w[2]
2945 || fstar.w[1] > __bid_ten2mk128trunc[ind - 1].w[1]
2946 || (fstar.w[1] == __bid_ten2mk128trunc[ind - 1].w[1]
2947 && fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[0])) {
2948 // set the inexact flag
2949 *pfpsf |= INEXACT_EXCEPTION;
2950 } // else the result is exact
2951 } else { // the result is inexact; f2* <= 1/2
2952 // set the inexact flag
2953 *pfpsf |= INEXACT_EXCEPTION;
2957 // if the result was a midpoint it was rounded away from zero
2962 } else if (exp == 0) {
2964 // res = +/-C (exact)
2969 } else { // if (exp>0) => 1 <= exp <= 18, 1 <= q < 18, 2 <= q + exp <= 19
2970 // res = +/-C * 10^exp (exact) where this fits in 64-bit integer
2972 res = -C1.w[0] * __bid_ten2k64[exp];
2974 res = C1.w[0] * __bid_ten2k64[exp];