Merged with libbbid branch at revision 126349.
[gcc.git] / libgcc / config / libbid / bid64_to_int32.c
1 /* Copyright (C) 2007 Free Software Foundation, Inc.
2
3 This file is part of GCC.
4
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
8 version.
9
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
17 executable.)
18
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
22 for more details.
23
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
27 02110-1301, USA. */
28
29 #include "bid_internal.h"
30
31 /*****************************************************************************
32 * BID64_to_int32_rnint
33 ****************************************************************************/
34
35 #if DECIMAL_CALL_BY_REFERENCE
36 void
37 __bid64_to_int32_rnint (int *pres,
38 UINT64 *
39 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
40 _EXC_INFO_PARAM) {
41 UINT64 x = *px;
42 #else
43 int
44 __bid64_to_int32_rnint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
45 _EXC_INFO_PARAM) {
46 #endif
47 int res;
48 UINT64 x_sign;
49 UINT64 x_exp;
50 int exp; // unbiased exponent
51 // Note: C1 represents x_significand (UINT64)
52 UINT64 tmp64;
53 BID_UI64DOUBLE tmp1;
54 unsigned int x_nr_bits;
55 int q, ind, shift;
56 UINT64 C1;
57 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
58 UINT128 fstar;
59 UINT128 P128;
60
61 // check for NaN or Infinity
62 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
63 // set invalid flag
64 *pfpsf |= INVALID_EXCEPTION;
65 // return Integer Indefinite
66 res = 0x80000000;
67 BID_RETURN (res);
68 }
69 // unpack x
70 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
71 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
72 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
73 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
74 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
75 if (C1 > 9999999999999999ull) { // non-canonical
76 x_exp = 0;
77 C1 = 0;
78 }
79 } else {
80 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
81 C1 = x & MASK_BINARY_SIG1;
82 }
83
84 // check for zeros (possibly from non-canonical values)
85 if (C1 == 0x0ull) {
86 // x is 0
87 res = 0x00000000;
88 BID_RETURN (res);
89 }
90 // x is not special and is not zero
91
92 // q = nr. of decimal digits in x (1 <= q <= 54)
93 // determine first the nr. of bits in x
94 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
95 // split the 64-bit value in two 32-bit halves to avoid rounding errors
96 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
97 tmp1.d = (double) (C1 >> 32); // exact conversion
98 x_nr_bits =
99 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
100 } else { // x < 2^32
101 tmp1.d = (double) C1; // exact conversion
102 x_nr_bits =
103 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
104 }
105 } else { // if x < 2^53
106 tmp1.d = (double) C1; // exact conversion
107 x_nr_bits =
108 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
109 }
110 q = __bid_nr_digits[x_nr_bits - 1].digits;
111 if (q == 0) {
112 q = __bid_nr_digits[x_nr_bits - 1].digits1;
113 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
114 q++;
115 }
116 exp = x_exp - 398; // unbiased exponent
117
118 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
119 // set invalid flag
120 *pfpsf |= INVALID_EXCEPTION;
121 // return Integer Indefinite
122 res = 0x80000000;
123 BID_RETURN (res);
124 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
125 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
126 // so x rounded to an integer may or may not fit in a signed 32-bit int
127 // the cases that do not fit are identified here; the ones that fit
128 // fall through and will be handled with other cases further,
129 // under '1 <= q + exp <= 10'
130 if (x_sign) { // if n < 0 and q + exp = 10
131 // if n < -2^31 - 1/2 then n is too large
132 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31+1/2
133 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000005, 1<=q<=16
134 // <=> C * 10^(11-q) > 0x500000005, 1<=q<=16
135 if (q <= 11) {
136 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
137 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
138 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
139 if (tmp64 > 0x500000005ull) {
140 // set invalid flag
141 *pfpsf |= INVALID_EXCEPTION;
142 // return Integer Indefinite
143 res = 0x80000000;
144 BID_RETURN (res);
145 }
146 // else cases that can be rounded to a 32-bit int fall through
147 // to '1 <= q + exp <= 10'
148 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
149 // C * 10^(11-q) > 0x500000005 <=>
150 // C > 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
151 // (scale 2^31+1/2 up)
152 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
153 tmp64 = 0x500000005ull * __bid_ten2k64[q - 11];
154 if (C1 > tmp64) {
155 // set invalid flag
156 *pfpsf |= INVALID_EXCEPTION;
157 // return Integer Indefinite
158 res = 0x80000000;
159 BID_RETURN (res);
160 }
161 // else cases that can be rounded to a 32-bit int fall through
162 // to '1 <= q + exp <= 10'
163 }
164 } else { // if n > 0 and q + exp = 10
165 // if n >= 2^31 - 1/2 then n is too large
166 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
167 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
168 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
169 if (q <= 11) {
170 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
171 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
172 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
173 if (tmp64 >= 0x4fffffffbull) {
174 // set invalid flag
175 *pfpsf |= INVALID_EXCEPTION;
176 // return Integer Indefinite
177 res = 0x80000000;
178 BID_RETURN (res);
179 }
180 // else cases that can be rounded to a 32-bit int fall through
181 // to '1 <= q + exp <= 10'
182 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
183 // C * 10^(11-q) >= 0x4fffffffb <=>
184 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
185 // (scale 2^31-1/2 up)
186 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
187 tmp64 = 0x4fffffffbull * __bid_ten2k64[q - 11];
188 if (C1 >= tmp64) {
189 // set invalid flag
190 *pfpsf |= INVALID_EXCEPTION;
191 // return Integer Indefinite
192 res = 0x80000000;
193 BID_RETURN (res);
194 }
195 // else cases that can be rounded to a 32-bit int fall through
196 // to '1 <= q + exp <= 10'
197 }
198 }
199 }
200 // n is not too large to be converted to int32: -2^31 - 1/2 <= n < 2^31 - 1/2
201 // Note: some of the cases tested for above fall through to this point
202 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
203 // return 0
204 res = 0x00000000;
205 BID_RETURN (res);
206 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
207 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
208 // res = 0
209 // else
210 // res = +/-1
211 ind = q - 1;
212 if (C1 <= __bid_midpoint64[ind]) {
213 res = 0x00000000; // return 0
214 } else if (x_sign) { // n < 0
215 res = 0xffffffff; // return -1
216 } else { // n > 0
217 res = 0x00000001; // return +1
218 }
219 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
220 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
221 // to nearest to a 32-bit signed integer
222 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
223 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
224 // chop off ind digits from the lower part of C1
225 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
226 C1 = C1 + __bid_midpoint64[ind - 1];
227 // calculate C* and f*
228 // C* is actually floor(C*) in this case
229 // C* and f* need shifting and masking, as shown by
230 // __bid_shiftright128[] and __bid_maskhigh128[]
231 // 1 <= x <= 15
232 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
233 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
234 // the approximation of 10^(-x) was rounded up to 54 bits
235 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
236 Cstar = P128.w[1];
237 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
238 fstar.w[0] = P128.w[0];
239 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
240 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
241 // if (0 < f* < 10^(-x)) then the result is a midpoint
242 // if floor(C*) is even then C* = floor(C*) - logical right
243 // shift; C* has p decimal digits, correct by Prop. 1)
244 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
245 // shift; C* has p decimal digits, correct by Pr. 1)
246 // else
247 // C* = floor(C*) (logical right shift; C has p decimal digits,
248 // correct by Property 1)
249 // n = C* * 10^(e+x)
250
251 // shift right C* by Ex-64 = __bid_shiftright128[ind]
252 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
253 Cstar = Cstar >> shift;
254
255 // if the result was a midpoint it was rounded away from zero, so
256 // it will need a correction
257 // check for midpoints
258 if ((fstar.w[1] == 0) && fstar.w[0]
259 && (fstar.w[0] <= __bid_ten2mk128trunc[ind - 1].w[1])) {
260 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
261 // __bid_ten2mk128[ind -1].w[1]
262 // the result is a midpoint; round to nearest
263 if (Cstar & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
264 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
265 Cstar--; // Cstar is now even
266 } // else MP in [ODD, EVEN]
267 }
268 if (x_sign)
269 res = -Cstar;
270 else
271 res = Cstar;
272 } else if (exp == 0) {
273 // 1 <= q <= 10
274 // res = +/-C (exact)
275 if (x_sign)
276 res = -C1;
277 else
278 res = C1;
279 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
280 // res = +/-C * 10^exp (exact)
281 if (x_sign)
282 res = -C1 * __bid_ten2k64[exp];
283 else
284 res = C1 * __bid_ten2k64[exp];
285 }
286 }
287 BID_RETURN (res);
288 }
289
290 /*****************************************************************************
291 * BID64_to_int32_xrnint
292 ****************************************************************************/
293
294 #if DECIMAL_CALL_BY_REFERENCE
295 void
296 __bid64_to_int32_xrnint (int *pres,
297 UINT64 *
298 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
299 _EXC_INFO_PARAM) {
300 UINT64 x = *px;
301 #else
302 int
303 __bid64_to_int32_xrnint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
304 _EXC_INFO_PARAM) {
305 #endif
306 int res;
307 UINT64 x_sign;
308 UINT64 x_exp;
309 int exp; // unbiased exponent
310 // Note: C1 represents x_significand (UINT64)
311 UINT64 tmp64;
312 BID_UI64DOUBLE tmp1;
313 unsigned int x_nr_bits;
314 int q, ind, shift;
315 UINT64 C1;
316 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
317 UINT128 fstar;
318 UINT128 P128;
319
320 // check for NaN or Infinity
321 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
322 // set invalid flag
323 *pfpsf |= INVALID_EXCEPTION;
324 // return Integer Indefinite
325 res = 0x80000000;
326 BID_RETURN (res);
327 }
328 // unpack x
329 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
330 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
331 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
332 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
333 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
334 if (C1 > 9999999999999999ull) { // non-canonical
335 x_exp = 0;
336 C1 = 0;
337 }
338 } else {
339 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
340 C1 = x & MASK_BINARY_SIG1;
341 }
342
343 // check for zeros (possibly from non-canonical values)
344 if (C1 == 0x0ull) {
345 // x is 0
346 res = 0x00000000;
347 BID_RETURN (res);
348 }
349 // x is not special and is not zero
350
351 // q = nr. of decimal digits in x (1 <= q <= 54)
352 // determine first the nr. of bits in x
353 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
354 // split the 64-bit value in two 32-bit halves to avoid rounding errors
355 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
356 tmp1.d = (double) (C1 >> 32); // exact conversion
357 x_nr_bits =
358 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
359 } else { // x < 2^32
360 tmp1.d = (double) C1; // exact conversion
361 x_nr_bits =
362 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
363 }
364 } else { // if x < 2^53
365 tmp1.d = (double) C1; // exact conversion
366 x_nr_bits =
367 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
368 }
369 q = __bid_nr_digits[x_nr_bits - 1].digits;
370 if (q == 0) {
371 q = __bid_nr_digits[x_nr_bits - 1].digits1;
372 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
373 q++;
374 }
375 exp = x_exp - 398; // unbiased exponent
376
377 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
378 // set invalid flag
379 *pfpsf |= INVALID_EXCEPTION;
380 // return Integer Indefinite
381 res = 0x80000000;
382 BID_RETURN (res);
383 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
384 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
385 // so x rounded to an integer may or may not fit in a signed 32-bit int
386 // the cases that do not fit are identified here; the ones that fit
387 // fall through and will be handled with other cases further,
388 // under '1 <= q + exp <= 10'
389 if (x_sign) { // if n < 0 and q + exp = 10
390 // if n < -2^31 - 1/2 then n is too large
391 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31+1/2
392 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000005, 1<=q<=16
393 // <=> C * 10^(11-q) > 0x500000005, 1<=q<=16
394 if (q <= 11) {
395 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
396 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
397 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
398 if (tmp64 > 0x500000005ull) {
399 // set invalid flag
400 *pfpsf |= INVALID_EXCEPTION;
401 // return Integer Indefinite
402 res = 0x80000000;
403 BID_RETURN (res);
404 }
405 // else cases that can be rounded to a 32-bit int fall through
406 // to '1 <= q + exp <= 10'
407 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
408 // C * 10^(11-q) > 0x500000005 <=>
409 // C > 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
410 // (scale 2^31+1/2 up)
411 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
412 tmp64 = 0x500000005ull * __bid_ten2k64[q - 11];
413 if (C1 > tmp64) {
414 // set invalid flag
415 *pfpsf |= INVALID_EXCEPTION;
416 // return Integer Indefinite
417 res = 0x80000000;
418 BID_RETURN (res);
419 }
420 // else cases that can be rounded to a 32-bit int fall through
421 // to '1 <= q + exp <= 10'
422 }
423 } else { // if n > 0 and q + exp = 10
424 // if n >= 2^31 - 1/2 then n is too large
425 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
426 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
427 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
428 if (q <= 11) {
429 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
430 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
431 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
432 if (tmp64 >= 0x4fffffffbull) {
433 // set invalid flag
434 *pfpsf |= INVALID_EXCEPTION;
435 // return Integer Indefinite
436 res = 0x80000000;
437 BID_RETURN (res);
438 }
439 // else cases that can be rounded to a 32-bit int fall through
440 // to '1 <= q + exp <= 10'
441 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
442 // C * 10^(11-q) >= 0x4fffffffb <=>
443 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
444 // (scale 2^31-1/2 up)
445 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
446 tmp64 = 0x4fffffffbull * __bid_ten2k64[q - 11];
447 if (C1 >= tmp64) {
448 // set invalid flag
449 *pfpsf |= INVALID_EXCEPTION;
450 // return Integer Indefinite
451 res = 0x80000000;
452 BID_RETURN (res);
453 }
454 // else cases that can be rounded to a 32-bit int fall through
455 // to '1 <= q + exp <= 10'
456 }
457 }
458 }
459 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
460 // Note: some of the cases tested for above fall through to this point
461 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
462 // set inexact flag
463 *pfpsf |= INEXACT_EXCEPTION;
464 // return 0
465 res = 0x00000000;
466 BID_RETURN (res);
467 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
468 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
469 // res = 0
470 // else
471 // res = +/-1
472 ind = q - 1;
473 if (C1 <= __bid_midpoint64[ind]) {
474 res = 0x00000000; // return 0
475 } else if (x_sign) { // n < 0
476 res = 0xffffffff; // return -1
477 } else { // n > 0
478 res = 0x00000001; // return +1
479 }
480 // set inexact flag
481 *pfpsf |= INEXACT_EXCEPTION;
482 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
483 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
484 // to nearest to a 32-bit signed integer
485 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
486 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
487 // chop off ind digits from the lower part of C1
488 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
489 C1 = C1 + __bid_midpoint64[ind - 1];
490 // calculate C* and f*
491 // C* is actually floor(C*) in this case
492 // C* and f* need shifting and masking, as shown by
493 // __bid_shiftright128[] and __bid_maskhigh128[]
494 // 1 <= x <= 15
495 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
496 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
497 // the approximation of 10^(-x) was rounded up to 54 bits
498 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
499 Cstar = P128.w[1];
500 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
501 fstar.w[0] = P128.w[0];
502 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
503 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
504 // if (0 < f* < 10^(-x)) then the result is a midpoint
505 // if floor(C*) is even then C* = floor(C*) - logical right
506 // shift; C* has p decimal digits, correct by Prop. 1)
507 // else if floor(C*) is odd C* = floor(C*)-1 (logical right
508 // shift; C* has p decimal digits, correct by Pr. 1)
509 // else
510 // C* = floor(C*) (logical right shift; C has p decimal digits,
511 // correct by Property 1)
512 // n = C* * 10^(e+x)
513
514 // shift right C* by Ex-64 = __bid_shiftright128[ind]
515 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
516 Cstar = Cstar >> shift;
517 // determine inexactness of the rounding of C*
518 // if (0 < f* - 1/2 < 10^(-x)) then
519 // the result is exact
520 // else // if (f* - 1/2 > T*) then
521 // the result is inexact
522 if (ind - 1 <= 2) {
523 if (fstar.w[0] > 0x8000000000000000ull) {
524 // f* > 1/2 and the result may be exact
525 tmp64 = fstar.w[0] - 0x8000000000000000ull; // f* - 1/2
526 if ((tmp64 > __bid_ten2mk128trunc[ind - 1].w[1])) {
527 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
528 // __bid_ten2mk128[ind -1].w[1]
529 // set the inexact flag
530 *pfpsf |= INEXACT_EXCEPTION;
531 } // else the result is exact
532 } else { // the result is inexact; f2* <= 1/2
533 // set the inexact flag
534 *pfpsf |= INEXACT_EXCEPTION;
535 }
536 } else { // if 3 <= ind - 1 <= 14
537 if (fstar.w[1] > __bid_one_half128[ind - 1]
538 || (fstar.w[1] == __bid_one_half128[ind - 1]
539 && fstar.w[0])) {
540 // f2* > 1/2 and the result may be exact
541 // Calculate f2* - 1/2
542 tmp64 = fstar.w[1] - __bid_one_half128[ind - 1];
543 if (tmp64 || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
544 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
545 // __bid_ten2mk128[ind -1].w[1]
546 // set the inexact flag
547 *pfpsf |= INEXACT_EXCEPTION;
548 } // else the result is exact
549 } else { // the result is inexact; f2* <= 1/2
550 // set the inexact flag
551 *pfpsf |= INEXACT_EXCEPTION;
552 }
553 }
554
555 // if the result was a midpoint it was rounded away from zero, so
556 // it will need a correction
557 // check for midpoints
558 if ((fstar.w[1] == 0) && fstar.w[0]
559 && (fstar.w[0] <= __bid_ten2mk128trunc[ind - 1].w[1])) {
560 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
561 // __bid_ten2mk128[ind -1].w[1]
562 // the result is a midpoint; round to nearest
563 if (Cstar & 0x01) { // Cstar is odd; MP in [EVEN, ODD]
564 // if floor(C*) is odd C = floor(C*) - 1; the result >= 1
565 Cstar--; // Cstar is now even
566 } // else MP in [ODD, EVEN]
567 }
568 if (x_sign)
569 res = -Cstar;
570 else
571 res = Cstar;
572 } else if (exp == 0) {
573 // 1 <= q <= 10
574 // res = +/-C (exact)
575 if (x_sign)
576 res = -C1;
577 else
578 res = C1;
579 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
580 // res = +/-C * 10^exp (exact)
581 if (x_sign)
582 res = -C1 * __bid_ten2k64[exp];
583 else
584 res = C1 * __bid_ten2k64[exp];
585 }
586 }
587 BID_RETURN (res);
588 }
589
590 /*****************************************************************************
591 * BID64_to_int32_floor
592 ****************************************************************************/
593
594 #if DECIMAL_CALL_BY_REFERENCE
595 void
596 __bid64_to_int32_floor (int *pres,
597 UINT64 *
598 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
599 _EXC_INFO_PARAM) {
600 UINT64 x = *px;
601 #else
602 int
603 __bid64_to_int32_floor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
604 _EXC_INFO_PARAM) {
605 #endif
606 int res;
607 UINT64 x_sign;
608 UINT64 x_exp;
609 int exp; // unbiased exponent
610 // Note: C1 represents x_significand (UINT64)
611 UINT64 tmp64;
612 BID_UI64DOUBLE tmp1;
613 unsigned int x_nr_bits;
614 int q, ind, shift;
615 UINT64 C1;
616 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
617 UINT128 fstar;
618 UINT128 P128;
619
620 // check for NaN or Infinity
621 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
622 // set invalid flag
623 *pfpsf |= INVALID_EXCEPTION;
624 // return Integer Indefinite
625 res = 0x80000000;
626 BID_RETURN (res);
627 }
628 // unpack x
629 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
630 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
631 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
632 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
633 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
634 if (C1 > 9999999999999999ull) { // non-canonical
635 x_exp = 0;
636 C1 = 0;
637 }
638 } else {
639 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
640 C1 = x & MASK_BINARY_SIG1;
641 }
642
643 // check for zeros (possibly from non-canonical values)
644 if (C1 == 0x0ull) {
645 // x is 0
646 res = 0x00000000;
647 BID_RETURN (res);
648 }
649 // x is not special and is not zero
650
651 // q = nr. of decimal digits in x (1 <= q <= 54)
652 // determine first the nr. of bits in x
653 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
654 // split the 64-bit value in two 32-bit halves to avoid rounding errors
655 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
656 tmp1.d = (double) (C1 >> 32); // exact conversion
657 x_nr_bits =
658 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
659 } else { // x < 2^32
660 tmp1.d = (double) C1; // exact conversion
661 x_nr_bits =
662 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
663 }
664 } else { // if x < 2^53
665 tmp1.d = (double) C1; // exact conversion
666 x_nr_bits =
667 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
668 }
669 q = __bid_nr_digits[x_nr_bits - 1].digits;
670 if (q == 0) {
671 q = __bid_nr_digits[x_nr_bits - 1].digits1;
672 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
673 q++;
674 }
675 exp = x_exp - 398; // unbiased exponent
676
677 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
678 // set invalid flag
679 *pfpsf |= INVALID_EXCEPTION;
680 // return Integer Indefinite
681 res = 0x80000000;
682 BID_RETURN (res);
683 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
684 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
685 // so x rounded to an integer may or may not fit in a signed 32-bit int
686 // the cases that do not fit are identified here; the ones that fit
687 // fall through and will be handled with other cases further,
688 // under '1 <= q + exp <= 10'
689 if (x_sign) { // if n < 0 and q + exp = 10
690 // if n < -2^31 then n is too large
691 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
692 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
693 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
694 if (q <= 11) {
695 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
696 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
697 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
698 if (tmp64 > 0x500000000ull) {
699 // set invalid flag
700 *pfpsf |= INVALID_EXCEPTION;
701 // return Integer Indefinite
702 res = 0x80000000;
703 BID_RETURN (res);
704 }
705 // else cases that can be rounded to a 32-bit int fall through
706 // to '1 <= q + exp <= 10'
707 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
708 // C * 10^(11-q) > 0x500000000 <=>
709 // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
710 // (scale 2^31+1 up)
711 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
712 tmp64 = 0x500000000ull * __bid_ten2k64[q - 11];
713 if (C1 > tmp64) {
714 // set invalid flag
715 *pfpsf |= INVALID_EXCEPTION;
716 // return Integer Indefinite
717 res = 0x80000000;
718 BID_RETURN (res);
719 }
720 // else cases that can be rounded to a 32-bit int fall through
721 // to '1 <= q + exp <= 10'
722 }
723 } else { // if n > 0 and q + exp = 10
724 // if n >= 2^31 then n is too large
725 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
726 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
727 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
728 if (q <= 11) {
729 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
730 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
731 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
732 if (tmp64 >= 0x500000000ull) {
733 // set invalid flag
734 *pfpsf |= INVALID_EXCEPTION;
735 // return Integer Indefinite
736 res = 0x80000000;
737 BID_RETURN (res);
738 }
739 // else cases that can be rounded to a 32-bit int fall through
740 // to '1 <= q + exp <= 10'
741 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
742 // C * 10^(11-q) >= 0x500000000 <=>
743 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
744 // (scale 2^31-1 up)
745 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
746 tmp64 = 0x500000000ull * __bid_ten2k64[q - 11];
747 if (C1 >= tmp64) {
748 // set invalid flag
749 *pfpsf |= INVALID_EXCEPTION;
750 // return Integer Indefinite
751 res = 0x80000000;
752 BID_RETURN (res);
753 }
754 // else cases that can be rounded to a 32-bit int fall through
755 // to '1 <= q + exp <= 10'
756 }
757 }
758 }
759 // n is not too large to be converted to int32: -2^31 <= n < 2^31
760 // Note: some of the cases tested for above fall through to this point
761 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
762 // return -1 or 0
763 if (x_sign)
764 res = 0xffffffff;
765 else
766 res = 0x00000000;
767 BID_RETURN (res);
768 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
769 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
770 // to nearest to a 32-bit signed integer
771 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
772 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
773 // chop off ind digits from the lower part of C1
774 // C1 fits in 64 bits
775 // calculate C* and f*
776 // C* is actually floor(C*) in this case
777 // C* and f* need shifting and masking, as shown by
778 // __bid_shiftright128[] and __bid_maskhigh128[]
779 // 1 <= x <= 15
780 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
781 // C* = C1 * 10^(-x)
782 // the approximation of 10^(-x) was rounded up to 54 bits
783 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
784 Cstar = P128.w[1];
785 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
786 fstar.w[0] = P128.w[0];
787 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
788 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
789 // C* = floor(C*) (logical right shift; C has p decimal digits,
790 // correct by Property 1)
791 // n = C* * 10^(e+x)
792
793 // shift right C* by Ex-64 = __bid_shiftright128[ind]
794 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
795 Cstar = Cstar >> shift;
796 // determine inexactness of the rounding of C*
797 // if (0 < f* < 10^(-x)) then
798 // the result is exact
799 // else // if (f* > T*) then
800 // the result is inexact
801 if (ind - 1 <= 2) {
802 if (fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
803 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
804 // __bid_ten2mk128[ind -1].w[1]
805 if (x_sign) { // negative and inexact
806 Cstar++;
807 }
808 } // else the result is exact
809 } else { // if 3 <= ind - 1 <= 14
810 if (fstar.w[1] || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
811 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
812 // __bid_ten2mk128[ind -1].w[1]
813 if (x_sign) { // negative and inexact
814 Cstar++;
815 }
816 } // else the result is exact
817 }
818
819 if (x_sign)
820 res = -Cstar;
821 else
822 res = Cstar;
823 } else if (exp == 0) {
824 // 1 <= q <= 10
825 // res = +/-C (exact)
826 if (x_sign)
827 res = -C1;
828 else
829 res = C1;
830 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
831 // res = +/-C * 10^exp (exact)
832 if (x_sign)
833 res = -C1 * __bid_ten2k64[exp];
834 else
835 res = C1 * __bid_ten2k64[exp];
836 }
837 }
838 BID_RETURN (res);
839 }
840
841 /*****************************************************************************
842 * BID64_to_int32_xfloor
843 ****************************************************************************/
844
845 #if DECIMAL_CALL_BY_REFERENCE
846 void
847 __bid64_to_int32_xfloor (int *pres,
848 UINT64 *
849 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
850 _EXC_INFO_PARAM) {
851 UINT64 x = *px;
852 #else
853 int
854 __bid64_to_int32_xfloor (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
855 _EXC_INFO_PARAM) {
856 #endif
857 int res;
858 UINT64 x_sign;
859 UINT64 x_exp;
860 int exp; // unbiased exponent
861 // Note: C1 represents x_significand (UINT64)
862 UINT64 tmp64;
863 BID_UI64DOUBLE tmp1;
864 unsigned int x_nr_bits;
865 int q, ind, shift;
866 UINT64 C1;
867 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
868 UINT128 fstar;
869 UINT128 P128;
870
871 // check for NaN or Infinity
872 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
873 // set invalid flag
874 *pfpsf |= INVALID_EXCEPTION;
875 // return Integer Indefinite
876 res = 0x80000000;
877 BID_RETURN (res);
878 }
879 // unpack x
880 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
881 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
882 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
883 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
884 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
885 if (C1 > 9999999999999999ull) { // non-canonical
886 x_exp = 0;
887 C1 = 0;
888 }
889 } else {
890 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
891 C1 = x & MASK_BINARY_SIG1;
892 }
893
894 // check for zeros (possibly from non-canonical values)
895 if (C1 == 0x0ull) {
896 // x is 0
897 res = 0x00000000;
898 BID_RETURN (res);
899 }
900 // x is not special and is not zero
901
902 // q = nr. of decimal digits in x (1 <= q <= 54)
903 // determine first the nr. of bits in x
904 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
905 // split the 64-bit value in two 32-bit halves to avoid rounding errors
906 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
907 tmp1.d = (double) (C1 >> 32); // exact conversion
908 x_nr_bits =
909 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
910 } else { // x < 2^32
911 tmp1.d = (double) C1; // exact conversion
912 x_nr_bits =
913 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
914 }
915 } else { // if x < 2^53
916 tmp1.d = (double) C1; // exact conversion
917 x_nr_bits =
918 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
919 }
920 q = __bid_nr_digits[x_nr_bits - 1].digits;
921 if (q == 0) {
922 q = __bid_nr_digits[x_nr_bits - 1].digits1;
923 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
924 q++;
925 }
926 exp = x_exp - 398; // unbiased exponent
927
928 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
929 // set invalid flag
930 *pfpsf |= INVALID_EXCEPTION;
931 // return Integer Indefinite
932 res = 0x80000000;
933 BID_RETURN (res);
934 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
935 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
936 // so x rounded to an integer may or may not fit in a signed 32-bit int
937 // the cases that do not fit are identified here; the ones that fit
938 // fall through and will be handled with other cases further,
939 // under '1 <= q + exp <= 10'
940 if (x_sign) { // if n < 0 and q + exp = 10
941 // if n < -2^31 then n is too large
942 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31
943 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x500000000, 1<=q<=16
944 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
945 if (q <= 11) {
946 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
947 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
948 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
949 if (tmp64 > 0x500000000ull) {
950 // set invalid flag
951 *pfpsf |= INVALID_EXCEPTION;
952 // return Integer Indefinite
953 res = 0x80000000;
954 BID_RETURN (res);
955 }
956 // else cases that can be rounded to a 32-bit int fall through
957 // to '1 <= q + exp <= 10'
958 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
959 // C * 10^(11-q) > 0x500000000 <=>
960 // C > 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
961 // (scale 2^31+1 up)
962 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
963 tmp64 = 0x500000000ull * __bid_ten2k64[q - 11];
964 if (C1 > tmp64) {
965 // set invalid flag
966 *pfpsf |= INVALID_EXCEPTION;
967 // return Integer Indefinite
968 res = 0x80000000;
969 BID_RETURN (res);
970 }
971 // else cases that can be rounded to a 32-bit int fall through
972 // to '1 <= q + exp <= 10'
973 }
974 } else { // if n > 0 and q + exp = 10
975 // if n >= 2^31 then n is too large
976 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
977 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
978 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
979 if (q <= 11) {
980 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
981 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
982 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
983 if (tmp64 >= 0x500000000ull) {
984 // set invalid flag
985 *pfpsf |= INVALID_EXCEPTION;
986 // return Integer Indefinite
987 res = 0x80000000;
988 BID_RETURN (res);
989 }
990 // else cases that can be rounded to a 32-bit int fall through
991 // to '1 <= q + exp <= 10'
992 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
993 // C * 10^(11-q) >= 0x500000000 <=>
994 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
995 // (scale 2^31-1 up)
996 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
997 tmp64 = 0x500000000ull * __bid_ten2k64[q - 11];
998 if (C1 >= tmp64) {
999 // set invalid flag
1000 *pfpsf |= INVALID_EXCEPTION;
1001 // return Integer Indefinite
1002 res = 0x80000000;
1003 BID_RETURN (res);
1004 }
1005 // else cases that can be rounded to a 32-bit int fall through
1006 // to '1 <= q + exp <= 10'
1007 }
1008 }
1009 }
1010 // n is not too large to be converted to int32: -2^31 <= n < 2^31
1011 // Note: some of the cases tested for above fall through to this point
1012 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1013 // set inexact flag
1014 *pfpsf |= INEXACT_EXCEPTION;
1015 // return -1 or 0
1016 if (x_sign)
1017 res = 0xffffffff;
1018 else
1019 res = 0x00000000;
1020 BID_RETURN (res);
1021 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1022 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
1023 // to nearest to a 32-bit signed integer
1024 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1025 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
1026 // chop off ind digits from the lower part of C1
1027 // C1 fits in 64 bits
1028 // calculate C* and f*
1029 // C* is actually floor(C*) in this case
1030 // C* and f* need shifting and masking, as shown by
1031 // __bid_shiftright128[] and __bid_maskhigh128[]
1032 // 1 <= x <= 15
1033 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1034 // C* = C1 * 10^(-x)
1035 // the approximation of 10^(-x) was rounded up to 54 bits
1036 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
1037 Cstar = P128.w[1];
1038 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
1039 fstar.w[0] = P128.w[0];
1040 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1041 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1042 // C* = floor(C*) (logical right shift; C has p decimal digits,
1043 // correct by Property 1)
1044 // n = C* * 10^(e+x)
1045
1046 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1047 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
1048 Cstar = Cstar >> shift;
1049 // determine inexactness of the rounding of C*
1050 // if (0 < f* < 10^(-x)) then
1051 // the result is exact
1052 // else // if (f* > T*) then
1053 // the result is inexact
1054 if (ind - 1 <= 2) {
1055 if (fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
1056 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1057 // __bid_ten2mk128[ind -1].w[1]
1058 if (x_sign) { // negative and inexact
1059 Cstar++;
1060 }
1061 // set the inexact flag
1062 *pfpsf |= INEXACT_EXCEPTION;
1063 } // else the result is exact
1064 } else { // if 3 <= ind - 1 <= 14
1065 if (fstar.w[1] || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
1066 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1067 // __bid_ten2mk128[ind -1].w[1]
1068 if (x_sign) { // negative and inexact
1069 Cstar++;
1070 }
1071 // set the inexact flag
1072 *pfpsf |= INEXACT_EXCEPTION;
1073 } // else the result is exact
1074 }
1075
1076 if (x_sign)
1077 res = -Cstar;
1078 else
1079 res = Cstar;
1080 } else if (exp == 0) {
1081 // 1 <= q <= 10
1082 // res = +/-C (exact)
1083 if (x_sign)
1084 res = -C1;
1085 else
1086 res = C1;
1087 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1088 // res = +/-C * 10^exp (exact)
1089 if (x_sign)
1090 res = -C1 * __bid_ten2k64[exp];
1091 else
1092 res = C1 * __bid_ten2k64[exp];
1093 }
1094 }
1095 BID_RETURN (res);
1096 }
1097
1098 /*****************************************************************************
1099 * BID64_to_int32_ceil
1100 ****************************************************************************/
1101
1102 #if DECIMAL_CALL_BY_REFERENCE
1103 void
1104 __bid64_to_int32_ceil (int *pres,
1105 UINT64 *
1106 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1107 _EXC_INFO_PARAM) {
1108 UINT64 x = *px;
1109 #else
1110 int
1111 __bid64_to_int32_ceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1112 _EXC_INFO_PARAM) {
1113 #endif
1114 int res;
1115 UINT64 x_sign;
1116 UINT64 x_exp;
1117 int exp; // unbiased exponent
1118 // Note: C1 represents x_significand (UINT64)
1119 UINT64 tmp64;
1120 BID_UI64DOUBLE tmp1;
1121 unsigned int x_nr_bits;
1122 int q, ind, shift;
1123 UINT64 C1;
1124 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
1125 UINT128 fstar;
1126 UINT128 P128;
1127
1128 // check for NaN or Infinity
1129 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
1130 // set invalid flag
1131 *pfpsf |= INVALID_EXCEPTION;
1132 // return Integer Indefinite
1133 res = 0x80000000;
1134 BID_RETURN (res);
1135 }
1136 // unpack x
1137 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1138 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1139 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1140 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
1141 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1142 if (C1 > 9999999999999999ull) { // non-canonical
1143 x_exp = 0;
1144 C1 = 0;
1145 }
1146 } else {
1147 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
1148 C1 = x & MASK_BINARY_SIG1;
1149 }
1150
1151 // check for zeros (possibly from non-canonical values)
1152 if (C1 == 0x0ull) {
1153 // x is 0
1154 res = 0x00000000;
1155 BID_RETURN (res);
1156 }
1157 // x is not special and is not zero
1158
1159 // q = nr. of decimal digits in x (1 <= q <= 54)
1160 // determine first the nr. of bits in x
1161 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
1162 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1163 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
1164 tmp1.d = (double) (C1 >> 32); // exact conversion
1165 x_nr_bits =
1166 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1167 } else { // x < 2^32
1168 tmp1.d = (double) C1; // exact conversion
1169 x_nr_bits =
1170 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1171 }
1172 } else { // if x < 2^53
1173 tmp1.d = (double) C1; // exact conversion
1174 x_nr_bits =
1175 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1176 }
1177 q = __bid_nr_digits[x_nr_bits - 1].digits;
1178 if (q == 0) {
1179 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1180 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
1181 q++;
1182 }
1183 exp = x_exp - 398; // unbiased exponent
1184
1185 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1186 // set invalid flag
1187 *pfpsf |= INVALID_EXCEPTION;
1188 // return Integer Indefinite
1189 res = 0x80000000;
1190 BID_RETURN (res);
1191 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1192 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1193 // so x rounded to an integer may or may not fit in a signed 32-bit int
1194 // the cases that do not fit are identified here; the ones that fit
1195 // fall through and will be handled with other cases further,
1196 // under '1 <= q + exp <= 10'
1197 if (x_sign) { // if n < 0 and q + exp = 10
1198 // if n <= -2^31 - 1 then n is too large
1199 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1200 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1201 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1202 if (q <= 11) {
1203 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1204 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1205 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1206 if (tmp64 >= 0x50000000aull) {
1207 // set invalid flag
1208 *pfpsf |= INVALID_EXCEPTION;
1209 // return Integer Indefinite
1210 res = 0x80000000;
1211 BID_RETURN (res);
1212 }
1213 // else cases that can be rounded to a 32-bit int fall through
1214 // to '1 <= q + exp <= 10'
1215 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1216 // C * 10^(11-q) >= 0x50000000a <=>
1217 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1218 // (scale 2^31+1 up)
1219 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1220 tmp64 = 0x50000000aull * __bid_ten2k64[q - 11];
1221 if (C1 >= tmp64) {
1222 // set invalid flag
1223 *pfpsf |= INVALID_EXCEPTION;
1224 // return Integer Indefinite
1225 res = 0x80000000;
1226 BID_RETURN (res);
1227 }
1228 // else cases that can be rounded to a 32-bit int fall through
1229 // to '1 <= q + exp <= 10'
1230 }
1231 } else { // if n > 0 and q + exp = 10
1232 // if n > 2^31 - 1 then n is too large
1233 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
1234 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
1235 // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
1236 if (q <= 11) {
1237 // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
1238 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1239 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1240 if (tmp64 > 0x4fffffff6ull) {
1241 // set invalid flag
1242 *pfpsf |= INVALID_EXCEPTION;
1243 // return Integer Indefinite
1244 res = 0x80000000;
1245 BID_RETURN (res);
1246 }
1247 // else cases that can be rounded to a 32-bit int fall through
1248 // to '1 <= q + exp <= 10'
1249 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1250 // C * 10^(11-q) > 0x4fffffff6 <=>
1251 // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
1252 // (scale 2^31-1 up)
1253 // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
1254 tmp64 = 0x4fffffff6ull * __bid_ten2k64[q - 11];
1255 if (C1 > tmp64) {
1256 // set invalid flag
1257 *pfpsf |= INVALID_EXCEPTION;
1258 // return Integer Indefinite
1259 res = 0x80000000;
1260 BID_RETURN (res);
1261 }
1262 // else cases that can be rounded to a 32-bit int fall through
1263 // to '1 <= q + exp <= 10'
1264 }
1265 }
1266 }
1267 // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
1268 // Note: some of the cases tested for above fall through to this point
1269 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1270 // return 0 or 1
1271 if (x_sign)
1272 res = 0x00000000;
1273 else
1274 res = 0x00000001;
1275 BID_RETURN (res);
1276 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1277 // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
1278 // to nearest to a 32-bit signed integer
1279 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1280 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
1281 // chop off ind digits from the lower part of C1
1282 // C1 fits in 64 bits
1283 // calculate C* and f*
1284 // C* is actually floor(C*) in this case
1285 // C* and f* need shifting and masking, as shown by
1286 // __bid_shiftright128[] and __bid_maskhigh128[]
1287 // 1 <= x <= 15
1288 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1289 // C* = C1 * 10^(-x)
1290 // the approximation of 10^(-x) was rounded up to 54 bits
1291 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
1292 Cstar = P128.w[1];
1293 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
1294 fstar.w[0] = P128.w[0];
1295 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1296 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1297 // C* = floor(C*) (logical right shift; C has p decimal digits,
1298 // correct by Property 1)
1299 // n = C* * 10^(e+x)
1300
1301 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1302 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
1303 Cstar = Cstar >> shift;
1304 // determine inexactness of the rounding of C*
1305 // if (0 < f* < 10^(-x)) then
1306 // the result is exact
1307 // else // if (f* > T*) then
1308 // the result is inexact
1309 if (ind - 1 <= 2) {
1310 if (fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
1311 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1312 // __bid_ten2mk128[ind -1].w[1]
1313 if (!x_sign) { // positive and inexact
1314 Cstar++;
1315 }
1316 } // else the result is exact
1317 } else { // if 3 <= ind - 1 <= 14
1318 if (fstar.w[1] || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
1319 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1320 // __bid_ten2mk128[ind -1].w[1]
1321 if (!x_sign) { // positive and inexact
1322 Cstar++;
1323 }
1324 } // else the result is exact
1325 }
1326
1327 if (x_sign)
1328 res = -Cstar;
1329 else
1330 res = Cstar;
1331 } else if (exp == 0) {
1332 // 1 <= q <= 10
1333 // res = +/-C (exact)
1334 if (x_sign)
1335 res = -C1;
1336 else
1337 res = C1;
1338 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1339 // res = +/-C * 10^exp (exact)
1340 if (x_sign)
1341 res = -C1 * __bid_ten2k64[exp];
1342 else
1343 res = C1 * __bid_ten2k64[exp];
1344 }
1345 }
1346 BID_RETURN (res);
1347 }
1348
1349 /*****************************************************************************
1350 * BID64_to_int32_xceil
1351 ****************************************************************************/
1352
1353 #if DECIMAL_CALL_BY_REFERENCE
1354 void
1355 __bid64_to_int32_xceil (int *pres,
1356 UINT64 *
1357 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1358 _EXC_INFO_PARAM) {
1359 UINT64 x = *px;
1360 #else
1361 int
1362 __bid64_to_int32_xceil (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1363 _EXC_INFO_PARAM) {
1364 #endif
1365 int res;
1366 UINT64 x_sign;
1367 UINT64 x_exp;
1368 int exp; // unbiased exponent
1369 // Note: C1 represents x_significand (UINT64)
1370 UINT64 tmp64;
1371 BID_UI64DOUBLE tmp1;
1372 unsigned int x_nr_bits;
1373 int q, ind, shift;
1374 UINT64 C1;
1375 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
1376 UINT128 fstar;
1377 UINT128 P128;
1378
1379 // check for NaN or Infinity
1380 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
1381 // set invalid flag
1382 *pfpsf |= INVALID_EXCEPTION;
1383 // return Integer Indefinite
1384 res = 0x80000000;
1385 BID_RETURN (res);
1386 }
1387 // unpack x
1388 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1389 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1390 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1391 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
1392 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1393 if (C1 > 9999999999999999ull) { // non-canonical
1394 x_exp = 0;
1395 C1 = 0;
1396 }
1397 } else {
1398 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
1399 C1 = x & MASK_BINARY_SIG1;
1400 }
1401
1402 // check for zeros (possibly from non-canonical values)
1403 if (C1 == 0x0ull) {
1404 // x is 0
1405 res = 0x00000000;
1406 BID_RETURN (res);
1407 }
1408 // x is not special and is not zero
1409
1410 // q = nr. of decimal digits in x (1 <= q <= 54)
1411 // determine first the nr. of bits in x
1412 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
1413 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1414 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
1415 tmp1.d = (double) (C1 >> 32); // exact conversion
1416 x_nr_bits =
1417 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1418 } else { // x < 2^32
1419 tmp1.d = (double) C1; // exact conversion
1420 x_nr_bits =
1421 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1422 }
1423 } else { // if x < 2^53
1424 tmp1.d = (double) C1; // exact conversion
1425 x_nr_bits =
1426 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1427 }
1428 q = __bid_nr_digits[x_nr_bits - 1].digits;
1429 if (q == 0) {
1430 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1431 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
1432 q++;
1433 }
1434 exp = x_exp - 398; // unbiased exponent
1435
1436 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1437 // set invalid flag
1438 *pfpsf |= INVALID_EXCEPTION;
1439 // return Integer Indefinite
1440 res = 0x80000000;
1441 BID_RETURN (res);
1442 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1443 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1444 // so x rounded to an integer may or may not fit in a signed 32-bit int
1445 // the cases that do not fit are identified here; the ones that fit
1446 // fall through and will be handled with other cases further,
1447 // under '1 <= q + exp <= 10'
1448 if (x_sign) { // if n < 0 and q + exp = 10
1449 // if n <= -2^31 - 1 then n is too large
1450 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1451 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1452 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1453 if (q <= 11) {
1454 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1455 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1456 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1457 if (tmp64 >= 0x50000000aull) {
1458 // set invalid flag
1459 *pfpsf |= INVALID_EXCEPTION;
1460 // return Integer Indefinite
1461 res = 0x80000000;
1462 BID_RETURN (res);
1463 }
1464 // else cases that can be rounded to a 32-bit int fall through
1465 // to '1 <= q + exp <= 10'
1466 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1467 // C * 10^(11-q) >= 0x50000000a <=>
1468 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1469 // (scale 2^31+1 up)
1470 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1471 tmp64 = 0x50000000aull * __bid_ten2k64[q - 11];
1472 if (C1 >= tmp64) {
1473 // set invalid flag
1474 *pfpsf |= INVALID_EXCEPTION;
1475 // return Integer Indefinite
1476 res = 0x80000000;
1477 BID_RETURN (res);
1478 }
1479 // else cases that can be rounded to a 32-bit int fall through
1480 // to '1 <= q + exp <= 10'
1481 }
1482 } else { // if n > 0 and q + exp = 10
1483 // if n > 2^31 - 1 then n is too large
1484 // too large if c(0)c(1)...c(9).c(10)...c(q-1) > 2^31 - 1
1485 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x4fffffff6, 1<=q<=16
1486 // <=> C * 10^(11-q) > 0x4fffffff6, 1<=q<=16
1487 if (q <= 11) {
1488 // Note: C * 10^(11-q) has 10 or 11 digits; 0x4fffffff6 has 11 digits
1489 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1490 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1491 if (tmp64 > 0x4fffffff6ull) {
1492 // set invalid flag
1493 *pfpsf |= INVALID_EXCEPTION;
1494 // return Integer Indefinite
1495 res = 0x80000000;
1496 BID_RETURN (res);
1497 }
1498 // else cases that can be rounded to a 32-bit int fall through
1499 // to '1 <= q + exp <= 10'
1500 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1501 // C * 10^(11-q) > 0x4fffffff6 <=>
1502 // C > 0x4fffffff6 * 10^(q-11) where 1 <= q - 11 <= 5
1503 // (scale 2^31-1 up)
1504 // Note: 0x4fffffff6*10^(q-11) has q-1 or q digits, where q <= 16
1505 tmp64 = 0x4fffffff6ull * __bid_ten2k64[q - 11];
1506 if (C1 > tmp64) {
1507 // set invalid flag
1508 *pfpsf |= INVALID_EXCEPTION;
1509 // return Integer Indefinite
1510 res = 0x80000000;
1511 BID_RETURN (res);
1512 }
1513 // else cases that can be rounded to a 32-bit int fall through
1514 // to '1 <= q + exp <= 10'
1515 }
1516 }
1517 }
1518 // n is not too large to be converted to int32: -2^31 - 1 < n <= 2^31 - 1
1519 // Note: some of the cases tested for above fall through to this point
1520 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1521 // set inexact flag
1522 *pfpsf |= INEXACT_EXCEPTION;
1523 // return 0 or 1
1524 if (x_sign)
1525 res = 0x00000000;
1526 else
1527 res = 0x00000001;
1528 BID_RETURN (res);
1529 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1530 // -2^31-1 < x <= -1 or 1 <= x <= 2^31-1 so x can be rounded
1531 // to nearest to a 32-bit signed integer
1532 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1533 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
1534 // chop off ind digits from the lower part of C1
1535 // C1 fits in 64 bits
1536 // calculate C* and f*
1537 // C* is actually floor(C*) in this case
1538 // C* and f* need shifting and masking, as shown by
1539 // __bid_shiftright128[] and __bid_maskhigh128[]
1540 // 1 <= x <= 15
1541 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1542 // C* = C1 * 10^(-x)
1543 // the approximation of 10^(-x) was rounded up to 54 bits
1544 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
1545 Cstar = P128.w[1];
1546 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
1547 fstar.w[0] = P128.w[0];
1548 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1549 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1550 // C* = floor(C*) (logical right shift; C has p decimal digits,
1551 // correct by Property 1)
1552 // n = C* * 10^(e+x)
1553
1554 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1555 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
1556 Cstar = Cstar >> shift;
1557 // determine inexactness of the rounding of C*
1558 // if (0 < f* < 10^(-x)) then
1559 // the result is exact
1560 // else // if (f* > T*) then
1561 // the result is inexact
1562 if (ind - 1 <= 2) {
1563 if (fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
1564 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1565 // __bid_ten2mk128[ind -1].w[1]
1566 if (!x_sign) { // positive and inexact
1567 Cstar++;
1568 }
1569 // set the inexact flag
1570 *pfpsf |= INEXACT_EXCEPTION;
1571 } // else the result is exact
1572 } else { // if 3 <= ind - 1 <= 14
1573 if (fstar.w[1] || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
1574 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
1575 // __bid_ten2mk128[ind -1].w[1]
1576 if (!x_sign) { // positive and inexact
1577 Cstar++;
1578 }
1579 // set the inexact flag
1580 *pfpsf |= INEXACT_EXCEPTION;
1581 } // else the result is exact
1582 }
1583
1584 if (x_sign)
1585 res = -Cstar;
1586 else
1587 res = Cstar;
1588 } else if (exp == 0) {
1589 // 1 <= q <= 10
1590 // res = +/-C (exact)
1591 if (x_sign)
1592 res = -C1;
1593 else
1594 res = C1;
1595 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1596 // res = +/-C * 10^exp (exact)
1597 if (x_sign)
1598 res = -C1 * __bid_ten2k64[exp];
1599 else
1600 res = C1 * __bid_ten2k64[exp];
1601 }
1602 }
1603 BID_RETURN (res);
1604 }
1605
1606 /*****************************************************************************
1607 * BID64_to_int32_int
1608 ****************************************************************************/
1609
1610 #if DECIMAL_CALL_BY_REFERENCE
1611 void
1612 __bid64_to_int32_int (int *pres,
1613 UINT64 *
1614 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1615 _EXC_INFO_PARAM) {
1616 UINT64 x = *px;
1617 #else
1618 int
1619 __bid64_to_int32_int (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1620 _EXC_INFO_PARAM) {
1621 #endif
1622 int res;
1623 UINT64 x_sign;
1624 UINT64 x_exp;
1625 int exp; // unbiased exponent
1626 // Note: C1 represents x_significand (UINT64)
1627 UINT64 tmp64;
1628 BID_UI64DOUBLE tmp1;
1629 unsigned int x_nr_bits;
1630 int q, ind, shift;
1631 UINT64 C1;
1632 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
1633 UINT128 P128;
1634
1635 // check for NaN or Infinity
1636 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
1637 // set invalid flag
1638 *pfpsf |= INVALID_EXCEPTION;
1639 // return Integer Indefinite
1640 res = 0x80000000;
1641 BID_RETURN (res);
1642 }
1643 // unpack x
1644 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1645 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1646 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1647 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
1648 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1649 if (C1 > 9999999999999999ull) { // non-canonical
1650 x_exp = 0;
1651 C1 = 0;
1652 }
1653 } else {
1654 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
1655 C1 = x & MASK_BINARY_SIG1;
1656 }
1657
1658 // check for zeros (possibly from non-canonical values)
1659 if (C1 == 0x0ull) {
1660 // x is 0
1661 res = 0x00000000;
1662 BID_RETURN (res);
1663 }
1664 // x is not special and is not zero
1665
1666 // q = nr. of decimal digits in x (1 <= q <= 54)
1667 // determine first the nr. of bits in x
1668 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
1669 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1670 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
1671 tmp1.d = (double) (C1 >> 32); // exact conversion
1672 x_nr_bits =
1673 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1674 } else { // x < 2^32
1675 tmp1.d = (double) C1; // exact conversion
1676 x_nr_bits =
1677 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1678 }
1679 } else { // if x < 2^53
1680 tmp1.d = (double) C1; // exact conversion
1681 x_nr_bits =
1682 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1683 }
1684 q = __bid_nr_digits[x_nr_bits - 1].digits;
1685 if (q == 0) {
1686 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1687 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
1688 q++;
1689 }
1690 exp = x_exp - 398; // unbiased exponent
1691
1692 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1693 // set invalid flag
1694 *pfpsf |= INVALID_EXCEPTION;
1695 // return Integer Indefinite
1696 res = 0x80000000;
1697 BID_RETURN (res);
1698 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1699 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1700 // so x rounded to an integer may or may not fit in a signed 32-bit int
1701 // the cases that do not fit are identified here; the ones that fit
1702 // fall through and will be handled with other cases further,
1703 // under '1 <= q + exp <= 10'
1704 if (x_sign) { // if n < 0 and q + exp = 10
1705 // if n <= -2^31 - 1 then n is too large
1706 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1707 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1708 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1709 if (q <= 11) {
1710 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1711 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1712 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1713 if (tmp64 >= 0x50000000aull) {
1714 // set invalid flag
1715 *pfpsf |= INVALID_EXCEPTION;
1716 // return Integer Indefinite
1717 res = 0x80000000;
1718 BID_RETURN (res);
1719 }
1720 // else cases that can be rounded to a 32-bit int fall through
1721 // to '1 <= q + exp <= 10'
1722 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1723 // C * 10^(11-q) >= 0x50000000a <=>
1724 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1725 // (scale 2^31+1 up)
1726 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1727 tmp64 = 0x50000000aull * __bid_ten2k64[q - 11];
1728 if (C1 >= tmp64) {
1729 // set invalid flag
1730 *pfpsf |= INVALID_EXCEPTION;
1731 // return Integer Indefinite
1732 res = 0x80000000;
1733 BID_RETURN (res);
1734 }
1735 // else cases that can be rounded to a 32-bit int fall through
1736 // to '1 <= q + exp <= 10'
1737 }
1738 } else { // if n > 0 and q + exp = 10
1739 // if n >= 2^31 then n is too large
1740 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
1741 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
1742 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
1743 if (q <= 11) {
1744 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
1745 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1746 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1747 if (tmp64 >= 0x500000000ull) {
1748 // set invalid flag
1749 *pfpsf |= INVALID_EXCEPTION;
1750 // return Integer Indefinite
1751 res = 0x80000000;
1752 BID_RETURN (res);
1753 }
1754 // else cases that can be rounded to a 32-bit int fall through
1755 // to '1 <= q + exp <= 10'
1756 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1757 // C * 10^(11-q) >= 0x500000000 <=>
1758 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
1759 // (scale 2^31-1 up)
1760 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
1761 tmp64 = 0x500000000ull * __bid_ten2k64[q - 11];
1762 if (C1 >= tmp64) {
1763 // set invalid flag
1764 *pfpsf |= INVALID_EXCEPTION;
1765 // return Integer Indefinite
1766 res = 0x80000000;
1767 BID_RETURN (res);
1768 }
1769 // else cases that can be rounded to a 32-bit int fall through
1770 // to '1 <= q + exp <= 10'
1771 }
1772 }
1773 }
1774 // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
1775 // Note: some of the cases tested for above fall through to this point
1776 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
1777 // return 0
1778 res = 0x00000000;
1779 BID_RETURN (res);
1780 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
1781 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
1782 // to nearest to a 32-bit signed integer
1783 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
1784 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
1785 // chop off ind digits from the lower part of C1
1786 // C1 fits in 64 bits
1787 // calculate C* and f*
1788 // C* is actually floor(C*) in this case
1789 // C* and f* need shifting and masking, as shown by
1790 // __bid_shiftright128[] and __bid_maskhigh128[]
1791 // 1 <= x <= 15
1792 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
1793 // C* = C1 * 10^(-x)
1794 // the approximation of 10^(-x) was rounded up to 54 bits
1795 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
1796 Cstar = P128.w[1];
1797 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
1798 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
1799 // C* = floor(C*) (logical right shift; C has p decimal digits,
1800 // correct by Property 1)
1801 // n = C* * 10^(e+x)
1802
1803 // shift right C* by Ex-64 = __bid_shiftright128[ind]
1804 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
1805 Cstar = Cstar >> shift;
1806 if (x_sign)
1807 res = -Cstar;
1808 else
1809 res = Cstar;
1810 } else if (exp == 0) {
1811 // 1 <= q <= 10
1812 // res = +/-C (exact)
1813 if (x_sign)
1814 res = -C1;
1815 else
1816 res = C1;
1817 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
1818 // res = +/-C * 10^exp (exact)
1819 if (x_sign)
1820 res = -C1 * __bid_ten2k64[exp];
1821 else
1822 res = C1 * __bid_ten2k64[exp];
1823 }
1824 }
1825 BID_RETURN (res);
1826 }
1827
1828 /*****************************************************************************
1829 * BID64_to_int32_xint
1830 ****************************************************************************/
1831
1832 #if DECIMAL_CALL_BY_REFERENCE
1833 void
1834 __bid64_to_int32_xint (int *pres,
1835 UINT64 *
1836 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1837 _EXC_INFO_PARAM) {
1838 UINT64 x = *px;
1839 #else
1840 int
1841 __bid64_to_int32_xint (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1842 _EXC_INFO_PARAM) {
1843 #endif
1844 int res;
1845 UINT64 x_sign;
1846 UINT64 x_exp;
1847 int exp; // unbiased exponent
1848 // Note: C1 represents x_significand (UINT64)
1849 UINT64 tmp64;
1850 BID_UI64DOUBLE tmp1;
1851 unsigned int x_nr_bits;
1852 int q, ind, shift;
1853 UINT64 C1;
1854 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
1855 UINT128 fstar;
1856 UINT128 P128;
1857
1858 // check for NaN or Infinity
1859 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
1860 // set invalid flag
1861 *pfpsf |= INVALID_EXCEPTION;
1862 // return Integer Indefinite
1863 res = 0x80000000;
1864 BID_RETURN (res);
1865 }
1866 // unpack x
1867 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
1868 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1869 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1870 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
1871 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1872 if (C1 > 9999999999999999ull) { // non-canonical
1873 x_exp = 0;
1874 C1 = 0;
1875 }
1876 } else {
1877 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
1878 C1 = x & MASK_BINARY_SIG1;
1879 }
1880
1881 // check for zeros (possibly from non-canonical values)
1882 if (C1 == 0x0ull) {
1883 // x is 0
1884 res = 0x00000000;
1885 BID_RETURN (res);
1886 }
1887 // x is not special and is not zero
1888
1889 // q = nr. of decimal digits in x (1 <= q <= 54)
1890 // determine first the nr. of bits in x
1891 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
1892 // split the 64-bit value in two 32-bit halves to avoid rounding errors
1893 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
1894 tmp1.d = (double) (C1 >> 32); // exact conversion
1895 x_nr_bits =
1896 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1897 } else { // x < 2^32
1898 tmp1.d = (double) C1; // exact conversion
1899 x_nr_bits =
1900 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1901 }
1902 } else { // if x < 2^53
1903 tmp1.d = (double) C1; // exact conversion
1904 x_nr_bits =
1905 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
1906 }
1907 q = __bid_nr_digits[x_nr_bits - 1].digits;
1908 if (q == 0) {
1909 q = __bid_nr_digits[x_nr_bits - 1].digits1;
1910 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
1911 q++;
1912 }
1913 exp = x_exp - 398; // unbiased exponent
1914
1915 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
1916 // set invalid flag
1917 *pfpsf |= INVALID_EXCEPTION;
1918 // return Integer Indefinite
1919 res = 0x80000000;
1920 BID_RETURN (res);
1921 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
1922 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
1923 // so x rounded to an integer may or may not fit in a signed 32-bit int
1924 // the cases that do not fit are identified here; the ones that fit
1925 // fall through and will be handled with other cases further,
1926 // under '1 <= q + exp <= 10'
1927 if (x_sign) { // if n < 0 and q + exp = 10
1928 // if n <= -2^31 - 1 then n is too large
1929 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1
1930 // <=> 0.c(0)c(1)...c(q-1) * 10^11 > 0x50000000a, 1<=q<=16
1931 // <=> C * 10^(11-q) >= 0x50000000a, 1<=q<=16
1932 if (q <= 11) {
1933 // Note: C * 10^(11-q) has 10 or 11 digits; 0x50000000a has 11 digits
1934 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1935 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1936 if (tmp64 >= 0x50000000aull) {
1937 // set invalid flag
1938 *pfpsf |= INVALID_EXCEPTION;
1939 // return Integer Indefinite
1940 res = 0x80000000;
1941 BID_RETURN (res);
1942 }
1943 // else cases that can be rounded to a 32-bit int fall through
1944 // to '1 <= q + exp <= 10'
1945 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1946 // C * 10^(11-q) >= 0x50000000a <=>
1947 // C >= 0x50000000a * 10^(q-11) where 1 <= q - 11 <= 5
1948 // (scale 2^31+1 up)
1949 // Note: 0x50000000a*10^(q-11) has q-1 or q digits, where q <= 16
1950 tmp64 = 0x50000000aull * __bid_ten2k64[q - 11];
1951 if (C1 >= tmp64) {
1952 // set invalid flag
1953 *pfpsf |= INVALID_EXCEPTION;
1954 // return Integer Indefinite
1955 res = 0x80000000;
1956 BID_RETURN (res);
1957 }
1958 // else cases that can be rounded to a 32-bit int fall through
1959 // to '1 <= q + exp <= 10'
1960 }
1961 } else { // if n > 0 and q + exp = 10
1962 // if n >= 2^31 then n is too large
1963 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31
1964 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000000, 1<=q<=16
1965 // <=> C * 10^(11-q) >= 0x500000000, 1<=q<=16
1966 if (q <= 11) {
1967 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000000 has 11 digits
1968 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
1969 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
1970 if (tmp64 >= 0x500000000ull) {
1971 // set invalid flag
1972 *pfpsf |= INVALID_EXCEPTION;
1973 // return Integer Indefinite
1974 res = 0x80000000;
1975 BID_RETURN (res);
1976 }
1977 // else cases that can be rounded to a 32-bit int fall through
1978 // to '1 <= q + exp <= 10'
1979 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
1980 // C * 10^(11-q) >= 0x500000000 <=>
1981 // C >= 0x500000000 * 10^(q-11) where 1 <= q - 11 <= 5
1982 // (scale 2^31-1 up)
1983 // Note: 0x500000000*10^(q-11) has q-1 or q digits, where q <= 16
1984 tmp64 = 0x500000000ull * __bid_ten2k64[q - 11];
1985 if (C1 >= tmp64) {
1986 // set invalid flag
1987 *pfpsf |= INVALID_EXCEPTION;
1988 // return Integer Indefinite
1989 res = 0x80000000;
1990 BID_RETURN (res);
1991 }
1992 // else cases that can be rounded to a 32-bit int fall through
1993 // to '1 <= q + exp <= 10'
1994 }
1995 }
1996 }
1997 // n is not too large to be converted to int32: -2^31 - 1 < n < 2^31
1998 // Note: some of the cases tested for above fall through to this point
1999 if ((q + exp) <= 0) { // n = +/-0.[0...0]c(0)c(1)...c(q-1)
2000 // set inexact flag
2001 *pfpsf |= INEXACT_EXCEPTION;
2002 // return 0
2003 res = 0x00000000;
2004 BID_RETURN (res);
2005 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2006 // -2^31-1 < x <= -1 or 1 <= x < 2^31 so x can be rounded
2007 // to nearest to a 32-bit signed integer
2008 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2009 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
2010 // chop off ind digits from the lower part of C1
2011 // C1 fits in 64 bits
2012 // calculate C* and f*
2013 // C* is actually floor(C*) in this case
2014 // C* and f* need shifting and masking, as shown by
2015 // __bid_shiftright128[] and __bid_maskhigh128[]
2016 // 1 <= x <= 15
2017 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
2018 // C* = C1 * 10^(-x)
2019 // the approximation of 10^(-x) was rounded up to 54 bits
2020 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
2021 Cstar = P128.w[1];
2022 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
2023 fstar.w[0] = P128.w[0];
2024 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
2025 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
2026 // C* = floor(C*) (logical right shift; C has p decimal digits,
2027 // correct by Property 1)
2028 // n = C* * 10^(e+x)
2029
2030 // shift right C* by Ex-64 = __bid_shiftright128[ind]
2031 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
2032 Cstar = Cstar >> shift;
2033 // determine inexactness of the rounding of C*
2034 // if (0 < f* < 10^(-x)) then
2035 // the result is exact
2036 // else // if (f* > T*) then
2037 // the result is inexact
2038 if (ind - 1 <= 2) {
2039 if (fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
2040 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2041 // __bid_ten2mk128[ind -1].w[1]
2042 // set the inexact flag
2043 *pfpsf |= INEXACT_EXCEPTION;
2044 } // else the result is exact
2045 } else { // if 3 <= ind - 1 <= 14
2046 if (fstar.w[1] || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
2047 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2048 // __bid_ten2mk128[ind -1].w[1]
2049 // set the inexact flag
2050 *pfpsf |= INEXACT_EXCEPTION;
2051 } // else the result is exact
2052 }
2053
2054 if (x_sign)
2055 res = -Cstar;
2056 else
2057 res = Cstar;
2058 } else if (exp == 0) {
2059 // 1 <= q <= 10
2060 // res = +/-C (exact)
2061 if (x_sign)
2062 res = -C1;
2063 else
2064 res = C1;
2065 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2066 // res = +/-C * 10^exp (exact)
2067 if (x_sign)
2068 res = -C1 * __bid_ten2k64[exp];
2069 else
2070 res = C1 * __bid_ten2k64[exp];
2071 }
2072 }
2073 BID_RETURN (res);
2074 }
2075
2076 /*****************************************************************************
2077 * BID64_to_int32_rninta
2078 ****************************************************************************/
2079
2080 #if DECIMAL_CALL_BY_REFERENCE
2081 void
2082 __bid64_to_int32_rninta (int *pres,
2083 UINT64 *
2084 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2085 _EXC_INFO_PARAM) {
2086 UINT64 x = *px;
2087 #else
2088 int
2089 __bid64_to_int32_rninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2090 _EXC_INFO_PARAM) {
2091 #endif
2092 int res;
2093 UINT64 x_sign;
2094 UINT64 x_exp;
2095 int exp; // unbiased exponent
2096 // Note: C1 represents x_significand (UINT64)
2097 UINT64 tmp64;
2098 BID_UI64DOUBLE tmp1;
2099 unsigned int x_nr_bits;
2100 int q, ind, shift;
2101 UINT64 C1;
2102 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
2103 UINT128 P128;
2104
2105 // check for NaN or Infinity
2106 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
2107 // set invalid flag
2108 *pfpsf |= INVALID_EXCEPTION;
2109 // return Integer Indefinite
2110 res = 0x80000000;
2111 BID_RETURN (res);
2112 }
2113 // unpack x
2114 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
2115 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2116 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2117 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
2118 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2119 if (C1 > 9999999999999999ull) { // non-canonical
2120 x_exp = 0;
2121 C1 = 0;
2122 }
2123 } else {
2124 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
2125 C1 = x & MASK_BINARY_SIG1;
2126 }
2127
2128 // check for zeros (possibly from non-canonical values)
2129 if (C1 == 0x0ull) {
2130 // x is 0
2131 res = 0x00000000;
2132 BID_RETURN (res);
2133 }
2134 // x is not special and is not zero
2135
2136 // q = nr. of decimal digits in x (1 <= q <= 54)
2137 // determine first the nr. of bits in x
2138 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
2139 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2140 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
2141 tmp1.d = (double) (C1 >> 32); // exact conversion
2142 x_nr_bits =
2143 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2144 } else { // x < 2^32
2145 tmp1.d = (double) C1; // exact conversion
2146 x_nr_bits =
2147 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2148 }
2149 } else { // if x < 2^53
2150 tmp1.d = (double) C1; // exact conversion
2151 x_nr_bits =
2152 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2153 }
2154 q = __bid_nr_digits[x_nr_bits - 1].digits;
2155 if (q == 0) {
2156 q = __bid_nr_digits[x_nr_bits - 1].digits1;
2157 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
2158 q++;
2159 }
2160 exp = x_exp - 398; // unbiased exponent
2161
2162 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
2163 // set invalid flag
2164 *pfpsf |= INVALID_EXCEPTION;
2165 // return Integer Indefinite
2166 res = 0x80000000;
2167 BID_RETURN (res);
2168 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
2169 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
2170 // so x rounded to an integer may or may not fit in a signed 32-bit int
2171 // the cases that do not fit are identified here; the ones that fit
2172 // fall through and will be handled with other cases further,
2173 // under '1 <= q + exp <= 10'
2174 if (x_sign) { // if n < 0 and q + exp = 10
2175 // if n <= -2^31 - 1/2 then n is too large
2176 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
2177 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
2178 // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
2179 if (q <= 11) {
2180 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2181 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
2182 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2183 if (tmp64 >= 0x500000005ull) {
2184 // set invalid flag
2185 *pfpsf |= INVALID_EXCEPTION;
2186 // return Integer Indefinite
2187 res = 0x80000000;
2188 BID_RETURN (res);
2189 }
2190 // else cases that can be rounded to a 32-bit int fall through
2191 // to '1 <= q + exp <= 10'
2192 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2193 // C * 10^(11-q) >= 0x500000005 <=>
2194 // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
2195 // (scale 2^31+1/2 up)
2196 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
2197 tmp64 = 0x500000005ull * __bid_ten2k64[q - 11];
2198 if (C1 >= tmp64) {
2199 // set invalid flag
2200 *pfpsf |= INVALID_EXCEPTION;
2201 // return Integer Indefinite
2202 res = 0x80000000;
2203 BID_RETURN (res);
2204 }
2205 // else cases that can be rounded to a 32-bit int fall through
2206 // to '1 <= q + exp <= 10'
2207 }
2208 } else { // if n > 0 and q + exp = 10
2209 // if n >= 2^31 - 1/2 then n is too large
2210 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
2211 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
2212 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
2213 if (q <= 11) {
2214 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2215 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
2216 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2217 if (tmp64 >= 0x4fffffffbull) {
2218 // set invalid flag
2219 *pfpsf |= INVALID_EXCEPTION;
2220 // return Integer Indefinite
2221 res = 0x80000000;
2222 BID_RETURN (res);
2223 }
2224 // else cases that can be rounded to a 32-bit int fall through
2225 // to '1 <= q + exp <= 10'
2226 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2227 // C * 10^(11-q) >= 0x4fffffffb <=>
2228 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
2229 // (scale 2^31-1/2 up)
2230 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
2231 tmp64 = 0x4fffffffbull * __bid_ten2k64[q - 11];
2232 if (C1 >= tmp64) {
2233 // set invalid flag
2234 *pfpsf |= INVALID_EXCEPTION;
2235 // return Integer Indefinite
2236 res = 0x80000000;
2237 BID_RETURN (res);
2238 }
2239 // else cases that can be rounded to a 32-bit int fall through
2240 // to '1 <= q + exp <= 10'
2241 }
2242 }
2243 }
2244 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
2245 // Note: some of the cases tested for above fall through to this point
2246 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2247 // return 0
2248 res = 0x00000000;
2249 BID_RETURN (res);
2250 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2251 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
2252 // res = 0
2253 // else
2254 // res = +/-1
2255 ind = q - 1;
2256 if (C1 < __bid_midpoint64[ind]) {
2257 res = 0x00000000; // return 0
2258 } else if (x_sign) { // n < 0
2259 res = 0xffffffff; // return -1
2260 } else { // n > 0
2261 res = 0x00000001; // return +1
2262 }
2263 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2264 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
2265 // to nearest away to a 32-bit signed integer
2266 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2267 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
2268 // chop off ind digits from the lower part of C1
2269 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2270 C1 = C1 + __bid_midpoint64[ind - 1];
2271 // calculate C* and f*
2272 // C* is actually floor(C*) in this case
2273 // C* and f* need shifting and masking, as shown by
2274 // __bid_shiftright128[] and __bid_maskhigh128[]
2275 // 1 <= x <= 15
2276 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
2277 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2278 // the approximation of 10^(-x) was rounded up to 54 bits
2279 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
2280 Cstar = P128.w[1];
2281 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
2282 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
2283 // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
2284 // correct by Pr. 1)
2285 // n = C* * 10^(e+x)
2286
2287 // shift right C* by Ex-64 = __bid_shiftright128[ind]
2288 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
2289 Cstar = Cstar >> shift;
2290
2291 // if the result was a midpoint it was rounded away from zero
2292 if (x_sign)
2293 res = -Cstar;
2294 else
2295 res = Cstar;
2296 } else if (exp == 0) {
2297 // 1 <= q <= 10
2298 // res = +/-C (exact)
2299 if (x_sign)
2300 res = -C1;
2301 else
2302 res = C1;
2303 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2304 // res = +/-C * 10^exp (exact)
2305 if (x_sign)
2306 res = -C1 * __bid_ten2k64[exp];
2307 else
2308 res = C1 * __bid_ten2k64[exp];
2309 }
2310 }
2311 BID_RETURN (res);
2312 }
2313
2314 /*****************************************************************************
2315 * BID64_to_int32_xrninta
2316 ****************************************************************************/
2317
2318 #if DECIMAL_CALL_BY_REFERENCE
2319 void
2320 __bid64_to_int32_xrninta (int *pres,
2321 UINT64 *
2322 px _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2323 _EXC_INFO_PARAM) {
2324 UINT64 x = *px;
2325 #else
2326 int
2327 __bid64_to_int32_xrninta (UINT64 x _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2328 _EXC_INFO_PARAM) {
2329 #endif
2330 int res;
2331 UINT64 x_sign;
2332 UINT64 x_exp;
2333 int exp; // unbiased exponent
2334 // Note: C1 represents x_significand (UINT64)
2335 UINT64 tmp64;
2336 BID_UI64DOUBLE tmp1;
2337 unsigned int x_nr_bits;
2338 int q, ind, shift;
2339 UINT64 C1;
2340 UINT64 Cstar; // C* represents up to 16 decimal digits ~ 54 bits
2341 UINT128 fstar;
2342 UINT128 P128;
2343
2344 // check for NaN or Infinity
2345 if ((x & MASK_NAN) == MASK_NAN || (x & MASK_INF) == MASK_INF) {
2346 // set invalid flag
2347 *pfpsf |= INVALID_EXCEPTION;
2348 // return Integer Indefinite
2349 res = 0x80000000;
2350 BID_RETURN (res);
2351 }
2352 // unpack x
2353 x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
2354 // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2355 if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2356 x_exp = (x & MASK_BINARY_EXPONENT2) >> 51; // biased
2357 C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2358 if (C1 > 9999999999999999ull) { // non-canonical
2359 x_exp = 0;
2360 C1 = 0;
2361 }
2362 } else {
2363 x_exp = (x & MASK_BINARY_EXPONENT1) >> 53; // biased
2364 C1 = x & MASK_BINARY_SIG1;
2365 }
2366
2367 // check for zeros (possibly from non-canonical values)
2368 if (C1 == 0x0ull) {
2369 // x is 0
2370 res = 0x00000000;
2371 BID_RETURN (res);
2372 }
2373 // x is not special and is not zero
2374
2375 // q = nr. of decimal digits in x (1 <= q <= 54)
2376 // determine first the nr. of bits in x
2377 if (C1 >= 0x0020000000000000ull) { // x >= 2^53
2378 // split the 64-bit value in two 32-bit halves to avoid rounding errors
2379 if (C1 >= 0x0000000100000000ull) { // x >= 2^32
2380 tmp1.d = (double) (C1 >> 32); // exact conversion
2381 x_nr_bits =
2382 33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2383 } else { // x < 2^32
2384 tmp1.d = (double) C1; // exact conversion
2385 x_nr_bits =
2386 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2387 }
2388 } else { // if x < 2^53
2389 tmp1.d = (double) C1; // exact conversion
2390 x_nr_bits =
2391 1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
2392 }
2393 q = __bid_nr_digits[x_nr_bits - 1].digits;
2394 if (q == 0) {
2395 q = __bid_nr_digits[x_nr_bits - 1].digits1;
2396 if (C1 >= __bid_nr_digits[x_nr_bits - 1].threshold_lo)
2397 q++;
2398 }
2399 exp = x_exp - 398; // unbiased exponent
2400
2401 if ((q + exp) > 10) { // x >= 10^10 ~= 2^33.2... (cannot fit in 32 bits)
2402 // set invalid flag
2403 *pfpsf |= INVALID_EXCEPTION;
2404 // return Integer Indefinite
2405 res = 0x80000000;
2406 BID_RETURN (res);
2407 } else if ((q + exp) == 10) { // x = c(0)c(1)...c(9).c(10)...c(q-1)
2408 // in this case 2^29.89... ~= 10^9 <= x < 10^10 ~= 2^33.2...
2409 // so x rounded to an integer may or may not fit in a signed 32-bit int
2410 // the cases that do not fit are identified here; the ones that fit
2411 // fall through and will be handled with other cases further,
2412 // under '1 <= q + exp <= 10'
2413 if (x_sign) { // if n < 0 and q + exp = 10
2414 // if n <= -2^31 - 1/2 then n is too large
2415 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31+1/2
2416 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x500000005, 1<=q<=16
2417 // <=> C * 10^(11-q) >= 0x500000005, 1<=q<=16
2418 if (q <= 11) {
2419 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2420 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
2421 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2422 if (tmp64 >= 0x500000005ull) {
2423 // set invalid flag
2424 *pfpsf |= INVALID_EXCEPTION;
2425 // return Integer Indefinite
2426 res = 0x80000000;
2427 BID_RETURN (res);
2428 }
2429 // else cases that can be rounded to a 32-bit int fall through
2430 // to '1 <= q + exp <= 10'
2431 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2432 // C * 10^(11-q) >= 0x500000005 <=>
2433 // C >= 0x500000005 * 10^(q-11) where 1 <= q - 11 <= 5
2434 // (scale 2^31+1/2 up)
2435 // Note: 0x500000005*10^(q-11) has q-1 or q digits, where q <= 16
2436 tmp64 = 0x500000005ull * __bid_ten2k64[q - 11];
2437 if (C1 >= tmp64) {
2438 // set invalid flag
2439 *pfpsf |= INVALID_EXCEPTION;
2440 // return Integer Indefinite
2441 res = 0x80000000;
2442 BID_RETURN (res);
2443 }
2444 // else cases that can be rounded to a 32-bit int fall through
2445 // to '1 <= q + exp <= 10'
2446 }
2447 } else { // if n > 0 and q + exp = 10
2448 // if n >= 2^31 - 1/2 then n is too large
2449 // too large if c(0)c(1)...c(9).c(10)...c(q-1) >= 2^31-1/2
2450 // <=> 0.c(0)c(1)...c(q-1) * 10^11 >= 0x4fffffffb, 1<=q<=16
2451 // <=> C * 10^(11-q) >= 0x4fffffffb, 1<=q<=16
2452 if (q <= 11) {
2453 // Note: C * 10^(11-q) has 10 or 11 digits; 0x500000005 has 11 digits
2454 tmp64 = C1 * __bid_ten2k64[11 - q]; // C scaled up to 11-digit int
2455 // c(0)c(1)...c(9)c(10) or c(0)c(1)...c(q-1)0...0 (11 digits)
2456 if (tmp64 >= 0x4fffffffbull) {
2457 // set invalid flag
2458 *pfpsf |= INVALID_EXCEPTION;
2459 // return Integer Indefinite
2460 res = 0x80000000;
2461 BID_RETURN (res);
2462 }
2463 // else cases that can be rounded to a 32-bit int fall through
2464 // to '1 <= q + exp <= 10'
2465 } else { // if (q > 11), i.e. 12 <= q <= 16 and so -15 <= exp <= -2
2466 // C * 10^(11-q) >= 0x4fffffffb <=>
2467 // C >= 0x4fffffffb * 10^(q-11) where 1 <= q - 11 <= 5
2468 // (scale 2^31-1/2 up)
2469 // Note: 0x4fffffffb*10^(q-11) has q-1 or q digits, where q <= 16
2470 tmp64 = 0x4fffffffbull * __bid_ten2k64[q - 11];
2471 if (C1 >= tmp64) {
2472 // set invalid flag
2473 *pfpsf |= INVALID_EXCEPTION;
2474 // return Integer Indefinite
2475 res = 0x80000000;
2476 BID_RETURN (res);
2477 }
2478 // else cases that can be rounded to a 32-bit int fall through
2479 // to '1 <= q + exp <= 10'
2480 }
2481 }
2482 }
2483 // n is not too large to be converted to int32: -2^31 - 1/2 < n < 2^31 - 1/2
2484 // Note: some of the cases tested for above fall through to this point
2485 if ((q + exp) < 0) { // n = +/-0.0...c(0)c(1)...c(q-1)
2486 // set inexact flag
2487 *pfpsf |= INEXACT_EXCEPTION;
2488 // return 0
2489 res = 0x00000000;
2490 BID_RETURN (res);
2491 } else if ((q + exp) == 0) { // n = +/-0.c(0)c(1)...c(q-1)
2492 // if 0.c(0)c(1)...c(q-1) <= 0.5 <=> c(0)c(1)...c(q-1) <= 5 * 10^(q-1)
2493 // res = 0
2494 // else
2495 // res = +/-1
2496 ind = q - 1;
2497 if (C1 < __bid_midpoint64[ind]) {
2498 res = 0x00000000; // return 0
2499 } else if (x_sign) { // n < 0
2500 res = 0xffffffff; // return -1
2501 } else { // n > 0
2502 res = 0x00000001; // return +1
2503 }
2504 // set inexact flag
2505 *pfpsf |= INEXACT_EXCEPTION;
2506 } else { // if (1 <= q + exp <= 10, 1 <= q <= 16, -15 <= exp <= 9)
2507 // -2^31-1/2 <= x <= -1 or 1 <= x < 2^31-1/2 so x can be rounded
2508 // to nearest away to a 32-bit signed integer
2509 if (exp < 0) { // 2 <= q <= 16, -15 <= exp <= -1, 1 <= q + exp <= 10
2510 ind = -exp; // 1 <= ind <= 15; ind is a synonym for 'x'
2511 // chop off ind digits from the lower part of C1
2512 // C1 = C1 + 1/2 * 10^ind where the result C1 fits in 64 bits
2513 C1 = C1 + __bid_midpoint64[ind - 1];
2514 // calculate C* and f*
2515 // C* is actually floor(C*) in this case
2516 // C* and f* need shifting and masking, as shown by
2517 // __bid_shiftright128[] and __bid_maskhigh128[]
2518 // 1 <= x <= 15
2519 // kx = 10^(-x) = __bid_ten2mk64[ind - 1]
2520 // C* = (C1 + 1/2 * 10^x) * 10^(-x)
2521 // the approximation of 10^(-x) was rounded up to 54 bits
2522 __mul_64x64_to_128MACH (P128, C1, __bid_ten2mk64[ind - 1]);
2523 Cstar = P128.w[1];
2524 fstar.w[1] = P128.w[1] & __bid_maskhigh128[ind - 1];
2525 fstar.w[0] = P128.w[0];
2526 // the top Ex bits of 10^(-x) are T* = __bid_ten2mk128trunc[ind].w[0], e.g.
2527 // if x=1, T*=__bid_ten2mk128trunc[0].w[0]=0x1999999999999999
2528 // C* = floor(C*)-1 (logical right shift; C* has p decimal digits,
2529 // correct by Pr. 1)
2530 // n = C* * 10^(e+x)
2531
2532 // shift right C* by Ex-64 = __bid_shiftright128[ind]
2533 shift = __bid_shiftright128[ind - 1]; // 0 <= shift <= 39
2534 Cstar = Cstar >> shift;
2535 // determine inexactness of the rounding of C*
2536 // if (0 < f* - 1/2 < 10^(-x)) then
2537 // the result is exact
2538 // else // if (f* - 1/2 > T*) then
2539 // the result is inexact
2540 if (ind - 1 <= 2) {
2541 if (fstar.w[0] > 0x8000000000000000ull) {
2542 // f* > 1/2 and the result may be exact
2543 tmp64 = fstar.w[0] - 0x8000000000000000ull; // f* - 1/2
2544 if ((tmp64 > __bid_ten2mk128trunc[ind - 1].w[1])) {
2545 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2546 // __bid_ten2mk128[ind -1].w[1]
2547 // set the inexact flag
2548 *pfpsf |= INEXACT_EXCEPTION;
2549 } // else the result is exact
2550 } else { // the result is inexact; f2* <= 1/2
2551 // set the inexact flag
2552 *pfpsf |= INEXACT_EXCEPTION;
2553 }
2554 } else { // if 3 <= ind - 1 <= 14
2555 if (fstar.w[1] > __bid_one_half128[ind - 1]
2556 || (fstar.w[1] == __bid_one_half128[ind - 1]
2557 && fstar.w[0])) {
2558 // f2* > 1/2 and the result may be exact
2559 // Calculate f2* - 1/2
2560 tmp64 = fstar.w[1] - __bid_one_half128[ind - 1];
2561 if (tmp64 || fstar.w[0] > __bid_ten2mk128trunc[ind - 1].w[1]) {
2562 // __bid_ten2mk128trunc[ind -1].w[1] is identical to
2563 // __bid_ten2mk128[ind -1].w[1]
2564 // set the inexact flag
2565 *pfpsf |= INEXACT_EXCEPTION;
2566 } // else the result is exact
2567 } else { // the result is inexact; f2* <= 1/2
2568 // set the inexact flag
2569 *pfpsf |= INEXACT_EXCEPTION;
2570 }
2571 }
2572
2573 // if the result was a midpoint it was rounded away from zero
2574 if (x_sign)
2575 res = -Cstar;
2576 else
2577 res = Cstar;
2578 } else if (exp == 0) {
2579 // 1 <= q <= 10
2580 // res = +/-C (exact)
2581 if (x_sign)
2582 res = -C1;
2583 else
2584 res = C1;
2585 } else { // if (exp > 0) => 1 <= exp <= 9, 1 <= q < 9, 2 <= q + exp <= 10
2586 // res = +/-C * 10^exp (exact)
2587 if (x_sign)
2588 res = -C1 * __bid_ten2k64[exp];
2589 else
2590 res = C1 * __bid_ten2k64[exp];
2591 }
2592 }
2593 BID_RETURN (res);
2594 }