OSDN Git Service

Merged with libbbid branch at revision 126349.
[pf3gnuchains/gcc-fork.git] / libgcc / config / libbid / bid128_noncomp.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  *
33  *    BID128 non-computational functions:
34  *         - __bid128_isSigned
35  *         - __bid128_isNormal
36  *         - __bid128_isSubnormal
37  *         - __bid128_isFinite
38  *         - __bid128_isZero
39  *         - __bid128_isInf
40  *         - __bid128_isSignaling
41  *         - __bid128_isCanonical
42  *         - __bid128_isNaN
43  *         - __bid128_copy
44  *         - __bid128_negate
45  *         - __bid128_abs
46  *         - __bid128_copySign
47  *         - __bid128_class
48  *         - __bid128_totalOrder
49  *         - __bid128_totalOrderMag
50  *         - __bid128_sameQuantum
51  *         - __bid128_radix
52  ****************************************************************************/
53
54 #if DECIMAL_CALL_BY_REFERENCE
55 void
56 __bid128_isSigned (int *pres,
57                  UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
58   UINT128 x = *px;
59 #else
60 int
61 __bid128_isSigned (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
62 #endif
63   int res;
64
65   res = ((x.w[HIGH_128W] & MASK_SIGN) == MASK_SIGN);
66   BID_RETURN (res);
67 }
68
69 // return 1 iff x is not zero, nor NaN nor subnormal nor infinity
70 #if DECIMAL_CALL_BY_REFERENCE
71 void
72 __bid128_isNormal (int *pres,
73                  UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
74   UINT128 x = *px;
75 #else
76 int
77 __bid128_isNormal (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
78 #endif
79   int res;
80   UINT64 x_exp, C1_hi, C1_lo;
81   BID_UI64DOUBLE tmp1;
82   int exp, q, x_nr_bits;
83
84   BID_SWAP128(x);
85   // test for special values - infinity or NaN
86   if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
87     // x is special
88     res = 0;
89     BID_RETURN (res);
90   }
91   // unpack x 
92   x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
93   C1_hi = x.w[1] & MASK_COEFF;
94   C1_lo = x.w[0];
95   // test for zero
96   if (C1_hi == 0 && C1_lo == 0) {
97     res = 0;
98     BID_RETURN (res);
99   }
100   // test for non-canonical values of the argument x
101   if ((((C1_hi > 0x0001ed09bead87c0ull)
102        || ((C1_hi == 0x0001ed09bead87c0ull)
103            && (C1_lo > 0x378d8e63ffffffffull)))
104       && ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull))
105       || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
106     res = 0;
107     BID_RETURN (res);
108   }
109   // x is subnormal or normal
110   // determine the number of digits q in the significand
111   // q = nr. of decimal digits in x
112   // determine first the nr. of bits in x
113   if (C1_hi == 0) {
114     if (C1_lo >= 0x0020000000000000ull) { // x >= 2^53
115       // split the 64-bit value in two 32-bit halves to avoid rounding errors
116       if (C1_lo >= 0x0000000100000000ull) { // x >= 2^32
117         tmp1.d = (double) (C1_lo >> 32); // exact conversion
118         x_nr_bits =
119           33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
120       } else { // x < 2^32
121         tmp1.d = (double) (C1_lo); // exact conversion
122         x_nr_bits =
123           1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
124       }
125     } else { // if x < 2^53
126       tmp1.d = (double) C1_lo; // exact conversion
127       x_nr_bits =
128         1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
129     }
130   } else { // C1_hi != 0 => nr. bits = 64 + nr_bits (C1_hi)
131     tmp1.d = (double) C1_hi; // exact conversion
132     x_nr_bits =
133       65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
134   }
135   q = __bid_nr_digits[x_nr_bits - 1].digits;
136   if (q == 0) {
137     q = __bid_nr_digits[x_nr_bits - 1].digits1;
138     if (C1_hi > __bid_nr_digits[x_nr_bits - 1].threshold_hi
139         || (C1_hi == __bid_nr_digits[x_nr_bits - 1].threshold_hi
140         && C1_lo >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
141       q++;
142   }
143   exp = (int) (x_exp >> 49) - 6176;
144   // test for subnormal values of x
145   if (exp + q <= -6143) {
146     res = 0;
147     BID_RETURN (res);
148   } else {
149     res = 1;
150     BID_RETURN (res);
151   }
152 }
153
154 // return 1 iff x is not zero, nor NaN nor normal nor infinity
155 #if DECIMAL_CALL_BY_REFERENCE
156 void
157 __bid128_isSubnormal (int *pres,
158                     UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
159   UINT128 x = *px;
160 #else
161 int
162 __bid128_isSubnormal (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
163 #endif
164   int res;
165   UINT64 x_exp, C1_hi, C1_lo;
166   BID_UI64DOUBLE tmp1;
167   int exp, q, x_nr_bits;
168
169   BID_SWAP128(x);
170   // test for special values - infinity or NaN
171   if ((x.w[1] & MASK_SPECIAL) == MASK_SPECIAL) {
172     // x is special
173     res = 0;
174     BID_RETURN (res);
175   }
176   // unpack x 
177   x_exp = x.w[1] & MASK_EXP; // biased and shifted left 49 bit positions
178   C1_hi = x.w[1] & MASK_COEFF;
179   C1_lo = x.w[0];
180   // test for zero
181   if (C1_hi == 0 && C1_lo == 0) {
182     res = 0;
183     BID_RETURN (res);
184   }
185   // test for non-canonical values of the argument x
186   if ((((C1_hi > 0x0001ed09bead87c0ull)
187        || ((C1_hi == 0x0001ed09bead87c0ull)
188            && (C1_lo > 0x378d8e63ffffffffull)))
189       && ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull))
190       || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
191     res = 0;
192     BID_RETURN (res);
193   }
194   // x is subnormal or normal
195   // determine the number of digits q in the significand
196   // q = nr. of decimal digits in x
197   // determine first the nr. of bits in x
198   if (C1_hi == 0) {
199     if (C1_lo >= 0x0020000000000000ull) { // x >= 2^53
200       // split the 64-bit value in two 32-bit halves to avoid rounding errors
201       if (C1_lo >= 0x0000000100000000ull) { // x >= 2^32
202         tmp1.d = (double) (C1_lo >> 32); // exact conversion
203         x_nr_bits =
204           33 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
205       } else { // x < 2^32
206         tmp1.d = (double) (C1_lo); // exact conversion
207         x_nr_bits =
208           1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
209       }
210     } else { // if x < 2^53
211       tmp1.d = (double) C1_lo; // exact conversion
212       x_nr_bits =
213         1 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
214     }
215   } else { // C1_hi != 0 => nr. bits = 64 + nr_bits (C1_hi)
216     tmp1.d = (double) C1_hi; // exact conversion
217     x_nr_bits =
218       65 + ((((unsigned int) (tmp1.ui64 >> 52)) & 0x7ff) - 0x3ff);
219   }
220   q = __bid_nr_digits[x_nr_bits - 1].digits;
221   if (q == 0) {
222     q = __bid_nr_digits[x_nr_bits - 1].digits1;
223     if (C1_hi > __bid_nr_digits[x_nr_bits - 1].threshold_hi
224         || (C1_hi == __bid_nr_digits[x_nr_bits - 1].threshold_hi
225         && C1_lo >= __bid_nr_digits[x_nr_bits - 1].threshold_lo))
226       q++;
227   }
228   exp = (int) (x_exp >> 49) - 6176;
229   // test for subnormal values of x
230   if (exp + q <= -6143) {
231     res = 1;
232   } else {
233     res = 0;
234   }
235   BID_RETURN (res);
236 }
237
238 #if DECIMAL_CALL_BY_REFERENCE
239 void
240 __bid128_isFinite (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
241   UINT128 x = *px;
242 #else
243 int
244 __bid128_isFinite (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
245 #endif
246   int res;
247   res = ((x.w[HIGH_128W] & MASK_INF) != MASK_INF);
248   BID_RETURN (res);
249 }
250
251 #if DECIMAL_CALL_BY_REFERENCE
252 void
253 __bid128_isZero (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
254   UINT128 x = *px;
255 #else
256 int
257 __bid128_isZero (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
258 #endif
259   int res;
260   UINT128 sig_x;
261
262   BID_SWAP128(x);
263   if ((x.w[1] & MASK_INF) == MASK_INF) {
264     res = 0;
265     BID_RETURN (res);
266   }
267   sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
268   sig_x.w[0] = x.w[0];
269   if ((sig_x.w[1] > 0x0001ed09bead87c0ull) || // significand is non-canonical
270       ((sig_x.w[1] == 0x0001ed09bead87c0ull) && 
271       (sig_x.w[0] > 0x378d8e63ffffffffull)) || // significand is non-canonical
272       ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull && 
273       (x.w[1] & MASK_INF) != MASK_INF) || // significand is non-canonical
274       (sig_x.w[1] == 0 && sig_x.w[0] == 0)) { // significand is 0
275     res = 1;
276     BID_RETURN (res);
277   }
278   res = 0;
279   BID_RETURN (res);
280 }
281
282 #if DECIMAL_CALL_BY_REFERENCE
283 void
284 __bid128_isInf (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
285   UINT128 x = *px;
286 #else
287 int
288 __bid128_isInf (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
289 #endif
290   int res;
291   res = ((x.w[HIGH_128W] & MASK_INF) == MASK_INF)
292     && ((x.w[HIGH_128W] & MASK_NAN) != MASK_NAN);
293   BID_RETURN (res);
294 }
295
296 #if DECIMAL_CALL_BY_REFERENCE
297 void
298 __bid128_isSignaling (int *pres,
299                     UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
300   UINT128 x = *px;
301 #else
302 int
303 __bid128_isSignaling (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
304 #endif
305   int res;
306
307   res = ((x.w[HIGH_128W] & MASK_SNAN) == MASK_SNAN);
308   BID_RETURN (res);
309 }
310
311 // return 1 iff x is a canonical number ,infinity, or NaN.
312 #if DECIMAL_CALL_BY_REFERENCE
313 void
314 __bid128_isCanonical (int *pres,
315                     UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
316   UINT128 x = *px;
317 #else
318 int
319 __bid128_isCanonical (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
320 #endif
321   int res;
322   UINT128 sig_x;
323
324   BID_SWAP128(x);
325   if ((x.w[1] & MASK_NAN) == MASK_NAN) { // NaN
326     if (x.w[1] & 0x01ffc00000000000ull) {
327       res = 0;
328       BID_RETURN (res);
329     }
330     sig_x.w[1] = x.w[1] & 0x00003fffffffffffull; // 46 bits
331     sig_x.w[0] = x.w[0]; // 64 bits
332     // payload must be < 10^33 = 0x0000314dc6448d93_38c15b0a00000000
333     if (sig_x.w[1] < 0x0000314dc6448d93ull
334         || (sig_x.w[1] == 0x0000314dc6448d93ull
335         && sig_x.w[0] < 0x38c15b0a00000000ull)) {
336       res = 1;
337     } else {
338       res = 0;
339     }
340     BID_RETURN (res);
341   } else if ((x.w[1] & MASK_INF) == MASK_INF) { // infinity
342     if ((x.w[1] & 0x03ffffffffffffffull) || x.w[0]) {
343       res = 0;
344     } else {
345       res = 1;
346     }
347     BID_RETURN (res);
348   }
349   // not NaN or infinity; extract significand to ensure it is canonical
350   sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
351   sig_x.w[0] = x.w[0];
352   // a canonical number has a coefficient < 10^34 
353   //    (0x0001ed09_bead87c0_378d8e64_00000000)
354   if ((sig_x.w[1] > 0x0001ed09bead87c0ull) || // significand is non-canonical
355       ((sig_x.w[1] == 0x0001ed09bead87c0ull) && (sig_x.w[0] > 0x378d8e63ffffffffull)) ||        // significand is non-canonical
356       ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)) {
357     res = 0;
358   } else {
359     res = 1;
360   }
361   BID_RETURN (res);
362 }
363
364 #if DECIMAL_CALL_BY_REFERENCE
365 void
366 __bid128_isNaN (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
367   UINT128 x = *px;
368 #else
369 int
370 __bid128_isNaN (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
371 #endif
372   int res;
373
374   res = ((x.w[HIGH_128W] & MASK_NAN) == MASK_NAN);
375   BID_RETURN (res);
376 }
377
378 // copies a floating-point operand x to destination y, with no change
379 #if DECIMAL_CALL_BY_REFERENCE
380 void
381 __bid128_copy (UINT128 * pres,
382              UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
383   UINT128 x = *px;
384 #else
385 UINT128
386 __bid128_copy (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
387 #endif
388   UINT128 res;
389
390   res = x;
391   BID_RETURN (res);
392 }
393
394 // copies a floating-point operand x to destination y, reversing the sign
395 #if DECIMAL_CALL_BY_REFERENCE
396 void
397 __bid128_negate (UINT128 * pres,
398                UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
399   UINT128 x = *px;
400 #else
401 UINT128
402 __bid128_negate (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
403 #endif
404   UINT128 res;
405
406   x.w[HIGH_128W] ^= MASK_SIGN;
407   res = x;
408   BID_RETURN (res);
409 }
410
411 // copies a floating-point operand x to destination y, changing the sign to positive
412 #if DECIMAL_CALL_BY_REFERENCE
413 void
414 __bid128_abs (UINT128 * pres,
415             UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
416   UINT128 x = *px;
417 #else
418 UINT128
419 __bid128_abs (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
420 #endif
421   UINT128 res;
422
423   x.w[HIGH_128W] &= ~MASK_SIGN;
424   res = x;
425   BID_RETURN (res);
426 }
427
428 // copies operand x to destination in the same format as x, but with the sign of y
429 #if DECIMAL_CALL_BY_REFERENCE
430 void
431 __bid128_copySign (UINT128 * pres, UINT128 * px,
432                  UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
433   UINT128 x = *px;
434   UINT128 y = *py;
435 #else
436 UINT128
437 __bid128_copySign (UINT128 x, UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
438 #endif
439   UINT128 res;
440
441   x.w[HIGH_128W] = (x.w[HIGH_128W] & ~MASK_SIGN) | (y.w[HIGH_128W] & MASK_SIGN);
442   res = x;
443   BID_RETURN (res);
444 }
445
446 #if DECIMAL_CALL_BY_REFERENCE
447 void
448 __bid128_class (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
449   UINT128 x = *px;
450 #else
451 int
452 __bid128_class (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
453 #endif
454   int res;
455   UINT256 sig_x_prime256;
456   UINT192 sig_x_prime192;
457   UINT128 sig_x;
458   int exp_x;
459
460   BID_SWAP128(x);
461   if ((x.w[1] & MASK_NAN) == MASK_NAN) {
462     if ((x.w[1] & MASK_SNAN) == MASK_SNAN) {
463       res = signalingNaN;
464     } else {
465       res = quietNaN;
466     }
467     BID_RETURN (res);
468   }
469   if ((x.w[1] & MASK_INF) == MASK_INF) {
470     if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
471       res = negativeInfinity;
472     } else {
473       res = positiveInfinity;
474     }
475     BID_RETURN (res);
476   }
477   // decode number into exponent and significand
478   sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
479   sig_x.w[0] = x.w[0];
480   // check for zero or non-canonical
481   if ((sig_x.w[1] > 0x0001ed09bead87c0ull)
482       || ((sig_x.w[1] == 0x0001ed09bead87c0ull)
483           && (sig_x.w[0] > 0x378d8e63ffffffffull))
484       || ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull)
485       || ((sig_x.w[1] == 0) && (sig_x.w[0] == 0))) {
486     if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
487       res = negativeZero;
488     } else {
489       res = positiveZero;
490     }
491     BID_RETURN (res);
492   }
493   exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
494   // if exponent is less than -6176, the number may be subnormal 
495   // (less than the smallest normal value)
496   //  the smallest normal value is 1 x 10^-6143 = 10^33 x 10^-6176
497   //  if (exp_x - 6176 < -6143)
498   if (exp_x < 33) { // sig_x * 10^exp_x
499     if (exp_x > 19) {
500       __mul_128x128_to_256 (sig_x_prime256, sig_x,
501                             __bid_ten2k128[exp_x - 20]);
502       // 10^33 = 0x0000314dc6448d93_38c15b0a00000000
503       if ((sig_x_prime256.w[3] == 0) && (sig_x_prime256.w[2] == 0)
504           && ((sig_x_prime256.w[1] < 0x0000314dc6448d93ull)
505               || ((sig_x_prime256.w[1] == 0x0000314dc6448d93ull)
506               && (sig_x_prime256.w[0] < 0x38c15b0a00000000ull)))) {
507         res =
508           ((x.w[1] & MASK_SIGN) ==
509            MASK_SIGN) ? negativeSubnormal : positiveSubnormal;
510         BID_RETURN (res);
511       }
512     } else {
513       __mul_64x128_to_192 (sig_x_prime192, __bid_ten2k64[exp_x], sig_x);
514       // 10^33 = 0x0000314dc6448d93_38c15b0a00000000
515       if ((sig_x_prime192.w[2] == 0)
516           && ((sig_x_prime192.w[1] < 0x0000314dc6448d93ull)
517               || ((sig_x_prime192.w[1] == 0x0000314dc6448d93ull)
518               && (sig_x_prime192.w[0] < 0x38c15b0a00000000ull)))) {
519         res =
520           ((x.w[1] & MASK_SIGN) ==
521            MASK_SIGN) ? negativeSubnormal : positiveSubnormal;
522         BID_RETURN (res);
523       }
524     }
525   }
526   // otherwise, normal number, determine the sign
527   res =
528     ((x.w[1] & MASK_SIGN) ==
529      MASK_SIGN) ? negativeNormal : positiveNormal;
530   BID_RETURN (res);
531 }
532
533 // true if the exponents of x and y are the same, false otherwise.
534 // The special cases of sameQuantum(NaN, NaN) and sameQuantum(Inf, Inf) are true
535 // If exactly one operand is infinite or exactly one operand is NaN, then false
536 #if DECIMAL_CALL_BY_REFERENCE
537 void
538 __bid128_sameQuantum (int *pres, UINT128 * px,
539                     UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
540   UINT128 x = *px;
541   UINT128 y = *py;
542 #else
543 int
544 __bid128_sameQuantum (UINT128 x,
545                     UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
546 #endif
547   int res;
548
549   BID_SWAP128(x);
550   BID_SWAP128(y);
551   // if both operands are NaN, return true
552   if ((x.w[1] & MASK_NAN) == MASK_NAN
553       || ((y.w[1] & MASK_NAN) == MASK_NAN)) {
554     res = ((x.w[1] & MASK_NAN) == MASK_NAN
555            && (y.w[1] & MASK_NAN) == MASK_NAN);
556     BID_RETURN (res);
557   }
558   // if both operands are INF, return true
559   if ((x.w[1] & MASK_INF) == MASK_INF
560       || (y.w[1] & MASK_INF) == MASK_INF) {
561     res = ((x.w[1] & MASK_INF) == MASK_INF)
562       && ((y.w[1] & MASK_INF) == MASK_INF);
563     BID_RETURN (res);
564   }
565   // decode exponents for both numbers, and return true if they match
566   res =
567     ((x.w[1] >> 49) & 0x0000000000003fffull) ==
568     ((y.w[1] >> 49) & 0x0000000000003fffull);
569   BID_RETURN (res);
570 }
571
572 #if DECIMAL_CALL_BY_REFERENCE
573 void
574 __bid128_totalOrder (int *pres, UINT128 * px,
575                    UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
576   UINT128 x = *px;
577   UINT128 y = *py;
578 #else
579 int
580 __bid128_totalOrder (UINT128 x,
581                    UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
582 #endif
583   int res;
584   int exp_x, exp_y;
585   UINT128 sig_x, sig_y, pyld_y, pyld_x;
586   UINT192 sig_n_prime192;
587   UINT256 sig_n_prime256;
588   char x_is_zero = 0, y_is_zero = 0;
589
590   BID_SWAP128(x);
591   BID_SWAP128(y);
592   // NaN (CASE 1)
593   // if x and y are unordered numerically because either operand is NaN
594   //    (1) totalOrder(-NaN, number) is true
595   //    (2) totalOrder(number, +NaN) is true
596   //    (3) if x and y are both NaN:
597   //       i) negative sign bit < positive sign bit
598   //       ii) signaling < quiet fir +NaN, reverse for -NaN
599   //       iii) lesser payload < greater payload for +NaN (reverse for -NaN)
600   if ((x.w[1] & MASK_NAN) == MASK_NAN) {
601     // if x is -NaN
602     if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
603       // return true, unless y is -NaN also
604       if ((y.w[1] & MASK_NAN) != MASK_NAN
605           || (y.w[1] & MASK_SIGN) != MASK_SIGN) {
606         res = 1; // y is a number, return 1
607         BID_RETURN (res);
608       } else { // if y and x are both -NaN
609         // if x and y are both -SNaN or both -QNaN, we have to compare payloads
610         // this statement evaluates to true if both are SNaN or QNaN
611         if (!
612             (((y.w[1] & MASK_SNAN) ==
613               MASK_SNAN) ^ ((x.w[1] & MASK_SNAN) == MASK_SNAN))) {
614           // it comes down to the payload.  we want to return true if x has a
615           // larger payload, but first we must calculate the payload.
616           pyld_y.w[1] = y.w[1] & 0x00003fffffffffffull;
617           pyld_y.w[0] = y.w[0];
618           pyld_x.w[1] = x.w[1] & 0x00003fffffffffffull;
619           pyld_x.w[0] = x.w[0];
620           if ((pyld_y.w[1] > 0x0000314dc6448d93ull)
621               || ((pyld_y.w[1] == 0x0000314dc6448d93ull)
622                   && (pyld_y.w[0] > 0x38c15b09ffffffffull))
623               || (pyld_y.w[1] == 0 && pyld_y.w[0] == 0)) {
624             // if y is zero, x must be less than or numerically equal
625             // y's payload is 0
626             res = 1;
627             BID_RETURN (res);
628           }
629           // if x is zero and y isn't, x has the smaller payload
630           // definitely (since we know y isn't 0 at this point)
631           if ((pyld_x.w[1] > 0x0000314dc6448d93ull)
632               || ((pyld_x.w[1] == 0x0000314dc6448d93ull)
633                   && (pyld_x.w[0] > 0x38c15b09ffffffffull))
634               || (pyld_x.w[0] == 0 && pyld_x.w[1] == 0)) {
635             // x's payload is 0
636             res = 0;
637             BID_RETURN (res);
638           }
639           res = ((pyld_x.w[1] > pyld_y.w[1])
640                  || (pyld_x.w[1] == pyld_y.w[1]
641                      && pyld_x.w[0] >= pyld_y.w[0]));
642           BID_RETURN (res);
643         } else {
644           // either x = -SNaN and y = -QNaN or x = -QNaN and y = -SNaN
645           res = (y.w[1] & MASK_SNAN) == MASK_SNAN;
646           // totalOrder (-QNaN, -SNaN) == 1
647           BID_RETURN (res);
648         }
649       }
650     } else { // x is +NaN
651       // return false, unless y is +NaN also
652       if ((y.w[1] & MASK_NAN) != MASK_NAN
653           || (y.w[1] & MASK_SIGN) == MASK_SIGN) {
654         res = 0; // y is a number, return 1
655         BID_RETURN (res);
656       } else {
657         // x and y are both +NaN; 
658         // must investigate payload if both quiet or both signaling
659         // this xnor statement will be true if both x and y are +QNaN or +SNaN
660         if (!
661             (((y.w[1] & MASK_SNAN) ==
662               MASK_SNAN) ^ ((x.w[1] & MASK_SNAN) == MASK_SNAN))) {
663           // it comes down to the payload.  we want to return true if x has a
664           // smaller payload, but first we must calculate the payload.
665           pyld_y.w[1] = y.w[1] & 0x00003fffffffffffull;
666           pyld_y.w[0] = y.w[0];
667           pyld_x.w[1] = x.w[1] & 0x00003fffffffffffull;
668           pyld_x.w[0] = x.w[0];
669           // if x is zero and y isn't, x has the smaller payload definitely 
670           // (since we know y isn't 0 at this point)
671           if ((pyld_x.w[1] > 0x0000314dc6448d93ull)
672               || ((pyld_x.w[1] == 0x0000314dc6448d93ull)
673                   && (pyld_x.w[0] > 0x38c15b09ffffffffull))
674               || (pyld_x.w[1] == 0 && pyld_x.w[0] == 0)) {
675             res = 1;
676             BID_RETURN (res);
677           }
678           if ((pyld_y.w[1] > 0x0000314dc6448d93ull)
679               || ((pyld_y.w[1] == 0x0000314dc6448d93ull)
680                   && (pyld_y.w[0] > 0x38c15b09ffffffffull))
681               || (pyld_y.w[1] == 0 && pyld_y.w[0] == 0)) {
682             // if y is zero, x must be less than or numerically equal
683             res = 0;
684             BID_RETURN (res);
685           }
686           res = ((pyld_x.w[1] < pyld_y.w[1])
687                  || (pyld_x.w[1] == pyld_y.w[1]
688                      && pyld_x.w[0] <= pyld_y.w[0]));
689           BID_RETURN (res);
690         } else {
691           // return true if y is +QNaN and x is +SNaN 
692           // (we know they're different bc of xor if_stmt above)
693           res = ((x.w[1] & MASK_SNAN) == MASK_SNAN);
694           BID_RETURN (res);
695         }
696       }
697     }
698   } else if ((y.w[1] & MASK_NAN) == MASK_NAN) {
699     // x is certainly not NAN in this case.
700     // return true if y is positive
701     res = ((y.w[1] & MASK_SIGN) != MASK_SIGN);
702     BID_RETURN (res);
703   }
704   // SIMPLE (CASE 2)
705   // if all the bits are the same, the numbers are equal.
706   if ((x.w[1] == y.w[1]) && (x.w[0] == y.w[0])) {
707     res = 1;
708     BID_RETURN (res);
709   }
710   // OPPOSITE SIGNS (CASE 3)
711   // if signs are opposite, return 1 if x is negative 
712   // (if x < y, totalOrder is true)
713   if (((x.w[1] & MASK_SIGN) == MASK_SIGN) ^ 
714       ((y.w[1] & MASK_SIGN) == MASK_SIGN)) {
715     res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
716     BID_RETURN (res);
717   }
718   // INFINITY (CASE 4)
719   if ((x.w[1] & MASK_INF) == MASK_INF) {
720     // if x == neg_inf, return (y == neg_inf);
721     if ((x.w[1] & MASK_SIGN) == MASK_SIGN) {
722       res = 1;
723       BID_RETURN (res);
724     } else {
725       // x is positive infinity, only return1 if y is positive infinity as well
726       res = ((y.w[1] & MASK_INF) == MASK_INF);
727       BID_RETURN (res);
728       // && (y & MASK_SIGN) != MASK_SIGN); (we know y has same sign as x)
729     }
730   } else if ((y.w[1] & MASK_INF) == MASK_INF) {
731     // x is finite, so:
732     //    if y is +inf, x<y
733     //    if y is -inf, x>y
734     res = ((y.w[1] & MASK_SIGN) != MASK_SIGN);
735     BID_RETURN (res);
736   }
737   // CONVERT x
738   sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
739   sig_x.w[0] = x.w[0];
740   exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
741
742   // CHECK IF x IS CANONICAL
743   // 9999999999999999999999999999999999 (decimal) = 
744   //     1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
745   // [0, 10^34) is the 754r supported canonical range.  
746   // If the value exceeds that, it is interpreted as 0.
747   if ((((sig_x.w[1] > 0x0001ed09bead87c0ull) || 
748       ((sig_x.w[1] == 0x0001ed09bead87c0ull) && 
749       (sig_x.w[0] > 0x378d8e63ffffffffull))) && 
750       ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) || 
751       ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) || 
752       ((sig_x.w[1] == 0) && (sig_x.w[0] == 0))) {
753     x_is_zero = 1;
754     // check for the case where the exponent is shifted right by 2 bits!
755     if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
756       exp_x = (x.w[1] >> 47) & 0x000000000003fffull;
757     }
758   }
759   // CONVERT y
760   exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
761   sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
762   sig_y.w[0] = y.w[0];
763
764   // CHECK IF y IS CANONICAL
765   // 9999999999999999999999999999999999(decimal) = 
766   //     1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
767   // [0, 10^34) is the 754r supported canonical range.  
768   // If the value exceeds that, it is interpreted as 0.
769   if ((((sig_y.w[1] > 0x0001ed09bead87c0ull) || 
770       ((sig_y.w[1] == 0x0001ed09bead87c0ull) && 
771       (sig_y.w[0] > 0x378d8e63ffffffffull))) && 
772       ((y.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) || 
773       ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) || 
774       ((sig_y.w[1] == 0) && (sig_y.w[0] == 0))) {
775     y_is_zero = 1;
776     // check for the case where the exponent is shifted right by 2 bits!
777     if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
778       exp_y = (y.w[1] >> 47) & 0x000000000003fffull;
779     }
780   }
781   // ZERO (CASE 5)
782   // if x and y represent the same entities, and both are negative 
783   // return true iff exp_x <= exp_y
784   if (x_is_zero && y_is_zero) {
785     // we know that signs must be the same because we would have caught it 
786     // in case3 if signs were different
787     // totalOrder(x,y) iff exp_x >= exp_y for negative numbers
788     // totalOrder(x,y) iff exp_x <= exp_y for positive numbers
789     if (exp_x == exp_y) {
790       res = 1;
791       BID_RETURN (res);
792     }
793     res = ((exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
794     BID_RETURN (res);
795   }
796   // if x is zero and y isn't, clearly x has the smaller payload
797   if (x_is_zero) {
798     res = ((y.w[1] & MASK_SIGN) != MASK_SIGN);
799     BID_RETURN (res);
800   }
801   // if y is zero, and x isn't, clearly y has the smaller payload
802   if (y_is_zero) {
803     res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
804     BID_RETURN (res);
805   }
806   // REDUNDANT REPRESENTATIONS (CASE 6)
807   // if both components are either bigger or smaller
808   if (((sig_x.w[1] > sig_y.w[1])
809        || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] > sig_y.w[0]))
810       && exp_x >= exp_y) {
811     res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
812     BID_RETURN (res);
813   }
814   if (((sig_x.w[1] < sig_y.w[1])
815        || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] < sig_y.w[0]))
816       && exp_x <= exp_y) {
817     res = ((x.w[1] & MASK_SIGN) != MASK_SIGN);
818     BID_RETURN (res);
819   }
820   // if |exp_x - exp_y| < 33, it comes down to the compensated significand
821   if (exp_x > exp_y) {
822     // if exp_x is 33 greater than exp_y, it is definitely larger, 
823     // so no need for compensation
824     if (exp_x - exp_y > 33) {
825       res = ((x.w[1] & MASK_SIGN) == MASK_SIGN);
826       BID_RETURN (res);
827       // difference cannot be greater than 10^33
828     }
829     // otherwise adjust the x significand upwards
830     if (exp_x - exp_y > 19) {
831       __mul_128x128_to_256 (sig_n_prime256, sig_x,
832                             __bid_ten2k128[exp_x - exp_y - 20]);
833       // the compensated significands are equal (ie "x and y represent the same
834       // entities") return 1 if (negative && expx > expy) || 
835       // (positive && expx < expy)
836       if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
837           && (sig_n_prime256.w[1] == sig_y.w[1])
838           && (sig_n_prime256.w[0] == sig_y.w[0])) {
839         // the case exp_x == exp_y  cannot occur, because all bits must be 
840         // the same - would have been caught if (x == y)
841         res = ((exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
842         BID_RETURN (res);
843       }
844       // if positive, return 1 if adjusted x is smaller than y
845       res = (((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
846               && ((sig_n_prime256.w[1] < sig_y.w[1])
847                   || (sig_n_prime256.w[1] == sig_y.w[1]
848                       && sig_n_prime256.w[0] <
849                       sig_y.w[0]))) ^ ((x.w[1] & MASK_SIGN) ==
850                                        MASK_SIGN));
851       BID_RETURN (res);
852     }
853     __mul_64x128_to_192 (sig_n_prime192, __bid_ten2k64[exp_x - exp_y], sig_x);
854     // if positive, return whichever significand is larger 
855     // (converse if negative)
856     if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_y.w[1]
857         && (sig_n_prime192.w[0] == sig_y.w[0])) {
858       res = ((exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
859       BID_RETURN (res);
860     }
861     res = (((sig_n_prime192.w[2] == 0)
862             && ((sig_n_prime192.w[1] < sig_y.w[1])
863                 || (sig_n_prime192.w[1] == sig_y.w[1]
864                     && sig_n_prime192.w[0] <
865                     sig_y.w[0]))) ^ ((x.w[1] & MASK_SIGN) ==
866                                      MASK_SIGN));
867     BID_RETURN (res);
868   }
869   // if exp_x is 33 less than exp_y, it is definitely smaller, 
870   // no need for compensation
871   if (exp_y - exp_x > 33) {
872     res = ((x.w[1] & MASK_SIGN) != MASK_SIGN);
873     BID_RETURN (res);
874   }
875   if (exp_y - exp_x > 19) {
876     // adjust the y significand upwards
877     __mul_128x128_to_256 (sig_n_prime256, sig_y,
878                           __bid_ten2k128[exp_y - exp_x - 20]);
879     // if x and y represent the same entities and both are negative
880     // return true iff exp_x <= exp_y
881     if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
882         && (sig_n_prime256.w[1] == sig_x.w[1])
883         && (sig_n_prime256.w[0] == sig_x.w[0])) {
884       res = (exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN);
885       BID_RETURN (res);
886     }
887     // values are not equal, for positive numbers return 1 if x is less than y
888     // and 0 otherwise
889     res = (((sig_n_prime256.w[3] != 0) ||
890             // if upper128 bits of compensated y are non-zero, y is bigger
891             (sig_n_prime256.w[2] != 0) ||
892             // if upper128 bits of compensated y are non-zero, y is bigger
893             (sig_n_prime256.w[1] > sig_x.w[1]) ||
894             // if compensated y is bigger, y is bigger
895             (sig_n_prime256.w[1] == sig_x.w[1]
896              && sig_n_prime256.w[0] >
897              sig_x.w[0])) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
898     BID_RETURN (res);
899   }
900   __mul_64x128_to_192 (sig_n_prime192, __bid_ten2k64[exp_y - exp_x], sig_y);
901   if ((sig_n_prime192.w[2] == 0) && (sig_n_prime192.w[1] == sig_x.w[1])
902       && (sig_n_prime192.w[0] == sig_x.w[0])) {
903     res = (exp_x <= exp_y) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN);
904     BID_RETURN (res);
905   }
906   res = (((sig_n_prime192.w[2] != 0) ||
907           // if upper128 bits of compensated y are non-zero, y is bigger
908           (sig_n_prime192.w[1] > sig_x.w[1]) ||
909           // if compensated y is bigger, y is bigger
910           (sig_n_prime192.w[1] == sig_x.w[1]
911            && sig_n_prime192.w[0] >
912            sig_x.w[0])) ^ ((x.w[1] & MASK_SIGN) == MASK_SIGN));
913   BID_RETURN (res);
914 }
915
916 #if DECIMAL_CALL_BY_REFERENCE
917 void
918 __bid128_totalOrderMag (int *pres, UINT128 * px,
919                       UINT128 * py _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
920   UINT128 x = *px;
921   UINT128 y = *py;
922 #else
923 int
924 __bid128_totalOrderMag (UINT128 x,
925                       UINT128 y _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
926 #endif
927   int res;
928   int exp_x, exp_y;
929   UINT128 sig_x, sig_y, pyld_y, pyld_x;
930   UINT192 sig_n_prime192;
931   UINT256 sig_n_prime256;
932   char x_is_zero = 0, y_is_zero = 0;
933
934   BID_SWAP128(x);
935   BID_SWAP128(y);
936   x.w[1] = x.w[1] & 0x7fffffffffffffffull;
937   y.w[1] = y.w[1] & 0x7fffffffffffffffull;
938
939   // NaN (CASE 1)
940   // if x and y are unordered numerically because either operand is NaN
941   //    (1) totalOrder(number, +NaN) is true
942   //    (2) if x and y are both NaN:
943   //       i) signaling < quiet fir +NaN
944   //       ii) lesser payload < greater payload for +NaN
945   if ((x.w[1] & MASK_NAN) == MASK_NAN) {
946     // x is +NaN
947     // return false, unless y is +NaN also
948     if ((y.w[1] & MASK_NAN) != MASK_NAN) {
949       res = 0; // y is a number, return 0
950       BID_RETURN (res);
951     } else {
952       // x and y are both +NaN; 
953       // must investigate payload if both quiet or both signaling
954       // this xnor statement will be true if both x and y are +QNaN or +SNaN
955       if (!
956           (((y.w[1] & MASK_SNAN) ==
957             MASK_SNAN) ^ ((x.w[1] & MASK_SNAN) == MASK_SNAN))) {
958         // it comes down to the payload.  we want to return true if x has a
959         // smaller payload, but first we must calculate the payload.
960         pyld_y.w[1] = y.w[1] & 0x00003fffffffffffull;
961         pyld_y.w[0] = y.w[0];
962         pyld_x.w[1] = x.w[1] & 0x00003fffffffffffull;
963         pyld_x.w[0] = x.w[0];
964         // if x is zero and y isn't, x has the smaller payload definitely 
965         // (since we know y isn't 0 at this point)
966         if ((pyld_x.w[1] > 0x0000314dc6448d93ull)
967             || ((pyld_x.w[1] == 0x0000314dc6448d93ull)
968                 && (pyld_x.w[0] > 0x38c15b09ffffffffull))
969             || (pyld_x.w[1] == 0 && pyld_x.w[0] == 0)) {
970           res = 1;
971           BID_RETURN (res);
972         }
973         if ((pyld_y.w[1] > 0x0000314dc6448d93ull)
974             || ((pyld_y.w[1] == 0x0000314dc6448d93ull)
975                 && (pyld_y.w[0] > 0x38c15b09ffffffffull))
976             || (pyld_y.w[1] == 0 && pyld_y.w[0] == 0)) {
977           // if y is zero, x must be less than or numerically equal
978           res = 0;
979           BID_RETURN (res);
980         }
981         res = ((pyld_x.w[1] < pyld_y.w[1])
982                || (pyld_x.w[1] == pyld_y.w[1]
983                    && pyld_x.w[0] <= pyld_y.w[0]));
984         BID_RETURN (res);
985       } else {
986         // return true if y is +QNaN and x is +SNaN 
987         // (we know they're different bc of xor if_stmt above)
988         res = ((x.w[1] & MASK_SNAN) == MASK_SNAN);
989         BID_RETURN (res);
990       }
991     }
992   } else if ((y.w[1] & MASK_NAN) == MASK_NAN) {
993     // x is certainly not NAN in this case.
994     // return true because y is positive
995     res = 1;
996     BID_RETURN (res);
997   }
998   // SIMPLE (CASE 2)
999   // if all the bits are the same, the numbers are equal.
1000   if ((x.w[1] == y.w[1]) && (x.w[0] == y.w[0])) {
1001     res = 1;
1002     BID_RETURN (res);
1003   }
1004   // INFINITY (CASE 3)
1005   if ((x.w[1] & MASK_INF) == MASK_INF) {
1006     // x is positive infinity, only return 1 if y is positive infinity as well
1007     res = ((y.w[1] & MASK_INF) == MASK_INF);
1008     BID_RETURN (res);
1009     // (we know y has same sign as x)
1010   } else if ((y.w[1] & MASK_INF) == MASK_INF) {
1011     // x is finite, so:
1012     //    since y is +inf, x<y
1013     res = 1;
1014     BID_RETURN (res);
1015   } else {
1016     ; // continue
1017   }
1018
1019   // CONVERT x
1020   sig_x.w[1] = x.w[1] & 0x0001ffffffffffffull;
1021   sig_x.w[0] = x.w[0];
1022   exp_x = (x.w[1] >> 49) & 0x000000000003fffull;
1023
1024   // CHECK IF x IS CANONICAL
1025   // 9999999999999999999999999999999999 (decimal) = 
1026   //     1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
1027   // [0, 10^34) is the 754r supported canonical range.  
1028   // If the value exceeds that, it is interpreted as 0.
1029   if ((((sig_x.w[1] > 0x0001ed09bead87c0ull) || 
1030       ((sig_x.w[1] == 0x0001ed09bead87c0ull) && 
1031       (sig_x.w[0] > 0x378d8e63ffffffffull))) && 
1032       ((x.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) || 
1033       ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) || 
1034       ((sig_x.w[1] == 0) && (sig_x.w[0] == 0))) {
1035     x_is_zero = 1;
1036     // check for the case where the exponent is shifted right by 2 bits!
1037     if ((x.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
1038       exp_x = (x.w[1] >> 47) & 0x000000000003fffull;
1039     }
1040   }
1041   // CONVERT y
1042   exp_y = (y.w[1] >> 49) & 0x0000000000003fffull;
1043   sig_y.w[1] = y.w[1] & 0x0001ffffffffffffull;
1044   sig_y.w[0] = y.w[0];
1045
1046   // CHECK IF y IS CANONICAL
1047   // 9999999999999999999999999999999999(decimal) = 
1048   //     1ed09_bead87c0_378d8e63_ffffffff(hexadecimal)
1049   // [0, 10^34) is the 754r supported canonical range.  
1050   // If the value exceeds that, it is interpreted as 0.
1051   if ((((sig_y.w[1] > 0x0001ed09bead87c0ull) || 
1052       ((sig_y.w[1] == 0x0001ed09bead87c0ull) && 
1053       (sig_y.w[0] > 0x378d8e63ffffffffull))) && 
1054       ((y.w[1] & 0x6000000000000000ull) != 0x6000000000000000ull)) || 
1055       ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) || 
1056       ((sig_y.w[1] == 0) && (sig_y.w[0] == 0))) {
1057     y_is_zero = 1;
1058     // check for the case where the exponent is shifted right by 2 bits!
1059     if ((y.w[1] & 0x6000000000000000ull) == 0x6000000000000000ull) {
1060       exp_y = (y.w[1] >> 47) & 0x000000000003fffull;
1061     }
1062   }
1063   // ZERO (CASE 4)
1064   if (x_is_zero && y_is_zero) {
1065     // we know that signs must be the same because we would have caught it 
1066     // in case3 if signs were different
1067     // totalOrder(x,y) iff exp_x <= exp_y for positive numbers
1068     if (exp_x == exp_y) {
1069       res = 1;
1070       BID_RETURN (res);
1071     }
1072     res = (exp_x <= exp_y);
1073     BID_RETURN (res);
1074   }
1075   // if x is zero and y isn't, clearly x has the smaller payload
1076   if (x_is_zero) {
1077     res = 1;
1078     BID_RETURN (res);
1079   }
1080   // if y is zero, and x isn't, clearly y has the smaller payload
1081   if (y_is_zero) {
1082     res = 0;
1083     BID_RETURN (res);
1084   }
1085   // REDUNDANT REPRESENTATIONS (CASE 5)
1086   // if both components are either bigger or smaller
1087   if (((sig_x.w[1] > sig_y.w[1])
1088        || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] > sig_y.w[0]))
1089       && exp_x >= exp_y) {
1090     res = 0;
1091     BID_RETURN (res);
1092   }
1093   if (((sig_x.w[1] < sig_y.w[1])
1094        || (sig_x.w[1] == sig_y.w[1] && sig_x.w[0] < sig_y.w[0]))
1095       && exp_x <= exp_y) {
1096     res = 1;
1097     BID_RETURN (res);
1098   }
1099   // if |exp_x - exp_y| < 33, it comes down to the compensated significand
1100   if (exp_x > exp_y) {
1101     // if exp_x is 33 greater than exp_y, it is definitely larger, 
1102     // so no need for compensation
1103     if (exp_x - exp_y > 33) {
1104       res = 0; // difference cannot be greater than 10^33
1105       BID_RETURN (res);
1106     }
1107     // otherwise adjust the x significand upwards
1108     if (exp_x - exp_y > 19) {
1109       __mul_128x128_to_256 (sig_n_prime256, sig_x,
1110                             __bid_ten2k128[exp_x - exp_y - 20]);
1111       // the compensated significands are equal (ie "x and y represent the same
1112       // entities") return 1 if (negative && expx > expy) || 
1113       // (positive && expx < expy)
1114       if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
1115           && (sig_n_prime256.w[1] == sig_y.w[1])
1116           && (sig_n_prime256.w[0] == sig_y.w[0])) {
1117         // the case (exp_x == exp_y) cannot occur, because all bits must be 
1118         // the same - would have been caught if (x == y)
1119         res = (exp_x <= exp_y);
1120         BID_RETURN (res);
1121       }
1122       // since positive, return 1 if adjusted x is smaller than y
1123       res = ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
1124              && ((sig_n_prime256.w[1] < sig_y.w[1])
1125                  || (sig_n_prime256.w[1] == sig_y.w[1]
1126                      && sig_n_prime256.w[0] < sig_y.w[0])));
1127       BID_RETURN (res);
1128     }
1129     __mul_64x128_to_192 (sig_n_prime192, __bid_ten2k64[exp_x - exp_y], sig_x);
1130     // if positive, return whichever significand is larger 
1131     // (converse if negative)
1132     if ((sig_n_prime192.w[2] == 0) && sig_n_prime192.w[1] == sig_y.w[1]
1133         && (sig_n_prime192.w[0] == sig_y.w[0])) {
1134       res = (exp_x <= exp_y);
1135       BID_RETURN (res);
1136     }
1137     res = ((sig_n_prime192.w[2] == 0)
1138            && ((sig_n_prime192.w[1] < sig_y.w[1])
1139                || (sig_n_prime192.w[1] == sig_y.w[1]
1140                    && sig_n_prime192.w[0] < sig_y.w[0])));
1141     BID_RETURN (res);
1142   }
1143   // if exp_x is 33 less than exp_y, it is definitely smaller, 
1144   // no need for compensation
1145   if (exp_y - exp_x > 33) {
1146     res = 1;
1147     BID_RETURN (res);
1148   }
1149   if (exp_y - exp_x > 19) {
1150     // adjust the y significand upwards
1151     __mul_128x128_to_256 (sig_n_prime256, sig_y,
1152                           __bid_ten2k128[exp_y - exp_x - 20]);
1153     if ((sig_n_prime256.w[3] == 0) && (sig_n_prime256.w[2] == 0)
1154         && (sig_n_prime256.w[1] == sig_x.w[1])
1155         && (sig_n_prime256.w[0] == sig_x.w[0])) {
1156       res = (exp_x <= exp_y);
1157       BID_RETURN (res);
1158     }
1159     // values are not equal, for positive numbers return 1 if x is less than y
1160     // and 0 otherwise
1161     res = ((sig_n_prime256.w[3] != 0) ||
1162            // if upper128 bits of compensated y are non-zero, y is bigger
1163            (sig_n_prime256.w[2] != 0) ||
1164            // if upper128 bits of compensated y are non-zero, y is bigger
1165            (sig_n_prime256.w[1] > sig_x.w[1]) ||
1166            // if compensated y is bigger, y is bigger
1167            (sig_n_prime256.w[1] == sig_x.w[1]
1168             && sig_n_prime256.w[0] > sig_x.w[0]));
1169     BID_RETURN (res);
1170   }
1171   __mul_64x128_to_192 (sig_n_prime192, __bid_ten2k64[exp_y - exp_x], sig_y);
1172   if ((sig_n_prime192.w[2] == 0) && (sig_n_prime192.w[1] == sig_x.w[1])
1173       && (sig_n_prime192.w[0] == sig_x.w[0])) {
1174     res = (exp_x <= exp_y);
1175     BID_RETURN (res);
1176   }
1177   res = ((sig_n_prime192.w[2] != 0) ||
1178          // if upper128 bits of compensated y are non-zero, y is bigger
1179          (sig_n_prime192.w[1] > sig_x.w[1]) ||
1180          // if compensated y is bigger, y is bigger
1181          (sig_n_prime192.w[1] == sig_x.w[1]
1182           && sig_n_prime192.w[0] > sig_x.w[0]));
1183   BID_RETURN (res);
1184 }
1185
1186 #if DECIMAL_CALL_BY_REFERENCE
1187 void
1188 __bid128_radix (int *pres, UINT128 * px _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
1189   UINT128 x = *px;
1190 #else
1191 int
1192 __bid128_radix (UINT128 x _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
1193 #endif
1194   int res;
1195   if (x.w[LOW_128W])        // dummy test
1196     res = 10;
1197   else
1198     res = 10;
1199   BID_RETURN (res);
1200 }