OSDN Git Service

Merged with libbbid branch at revision 126349.
[pf3gnuchains/gcc-fork.git] / libgcc / config / libbid / bid64_compare.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 static const UINT64 mult_factor[16] = {
32   1ull, 10ull, 100ull, 1000ull,
33   10000ull, 100000ull, 1000000ull, 10000000ull,
34   100000000ull, 1000000000ull, 10000000000ull, 100000000000ull,
35   1000000000000ull, 10000000000000ull,
36   100000000000000ull, 1000000000000000ull
37 };
38
39 #if DECIMAL_CALL_BY_REFERENCE
40 void
41 __bid64_quiet_equal (int *pres, UINT64 * px,
42                    UINT64 *
43                    py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
44                    _EXC_INFO_PARAM) {
45   UINT64 x = *px;
46   UINT64 y = *py;
47 #else
48 int
49 __bid64_quiet_equal (UINT64 x,
50                    UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
51                    _EXC_INFO_PARAM) {
52 #endif
53   int res;
54   int exp_x, exp_y, exp_t;
55   UINT64 sig_x, sig_y, sig_t;
56   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y, lcv;
57
58   // NaN (CASE1)
59   // if either number is NAN, the comparison is unordered, 
60   // rather than equal : return 0
61   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
62     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
63       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
64     }
65     res = 0;
66     BID_RETURN (res);
67   }
68   // SIMPLE (CASE2)
69   // if all the bits are the same, these numbers are equivalent.
70   if (x == y) {
71     res = 1;
72     BID_RETURN (res);
73   }
74   // INFINITY (CASE3)
75   if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
76     res = (((x ^ y) & MASK_SIGN) != MASK_SIGN);
77     BID_RETURN (res);
78   }
79   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
80   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
81     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
82     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
83     if (sig_x > 9999999999999999ull) {
84       non_canon_x = 1;
85     } else {
86       non_canon_x = 0;
87     }
88   } else {
89     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
90     sig_x = (x & MASK_BINARY_SIG1);
91     non_canon_x = 0;
92   }
93   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
94   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
95     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
96     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
97     if (sig_y > 9999999999999999ull) {
98       non_canon_y = 1;
99     } else {
100       non_canon_y = 0;
101     }
102   } else {
103     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
104     sig_y = (y & MASK_BINARY_SIG1);
105     non_canon_y = 0;
106   }
107   // ZERO (CASE4)
108   // some properties:
109   // (+ZERO==-ZERO) => therefore ignore the sign
110   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
111   //    therefore ignore the exponent field
112   //    (Any non-canonical # is considered 0)
113   if (non_canon_x || sig_x == 0) {
114     x_is_zero = 1;
115   }
116   if (non_canon_y || sig_y == 0) {
117     y_is_zero = 1;
118   }
119   if (x_is_zero && y_is_zero) {
120     res = 1;
121     BID_RETURN (res);
122   } else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
123     res = 0;
124     BID_RETURN (res);
125   }
126   // OPPOSITE SIGN (CASE5)
127   // now, if the sign bits differ => not equal : return 0
128   if ((x ^ y) & MASK_SIGN) {
129     res = 0;
130     BID_RETURN (res);
131   }
132   // REDUNDANT REPRESENTATIONS (CASE6)
133   if (exp_x > exp_y) { // to simplify the loop below,
134     SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
135     SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
136   }
137   if (exp_y - exp_x > 15) {
138     res = 0; // difference cannot be greater than 10^15
139     BID_RETURN (res);
140   }
141   for (lcv = 0; lcv < (exp_y - exp_x); lcv++) {
142     // recalculate y's significand upwards
143     sig_y = sig_y * 10;
144     if (sig_y > 9999999999999999ull) {
145       res = 0;
146       BID_RETURN (res);
147     }
148   }
149   res = (sig_y == sig_x);
150   BID_RETURN (res);
151 }
152
153 #if DECIMAL_CALL_BY_REFERENCE
154 void
155 __bid64_quiet_greater (int *pres, UINT64 * px,
156                      UINT64 *
157                      py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
158                      _EXC_INFO_PARAM) {
159   UINT64 x = *px;
160   UINT64 y = *py;
161 #else
162 int
163 __bid64_quiet_greater (UINT64 x,
164                      UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
165                      _EXC_INFO_PARAM) {
166 #endif
167   int res;
168   int exp_x, exp_y;
169   UINT64 sig_x, sig_y;
170   UINT128 sig_n_prime;
171   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
172
173   // NaN (CASE1)
174   // if either number is NAN, the comparison is unordered, rather than equal : 
175   // return 0
176   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
177     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
178       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
179     }
180     res = 0;
181     BID_RETURN (res);
182   }
183   // SIMPLE (CASE2)
184   // if all the bits are the same, these numbers are equal (not Greater).
185   if (x == y) {
186     res = 0;
187     BID_RETURN (res);
188   }
189   // INFINITY (CASE3)
190   if ((x & MASK_INF) == MASK_INF) {
191     // if x is neg infinity, there is no way it is greater than y, return 0
192     if (((x & MASK_SIGN) == MASK_SIGN)) {
193       res = 0;
194       BID_RETURN (res);
195     } else {
196       // x is pos infinity, it is greater, unless y is positive 
197       // infinity => return y!=pos_infinity
198       res = (((y & MASK_INF) != MASK_INF)
199              || ((y & MASK_SIGN) == MASK_SIGN));
200       BID_RETURN (res);
201     }
202   } else if ((y & MASK_INF) == MASK_INF) {
203     // x is finite, so if y is positive infinity, then x is less, return 0
204     //                 if y is negative infinity, then x is greater, return 1
205     res = ((y & MASK_SIGN) == MASK_SIGN);
206     BID_RETURN (res);
207   }
208   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
209   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
210     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
211     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
212     if (sig_x > 9999999999999999ull) {
213       non_canon_x = 1;
214     } else {
215       non_canon_x = 0;
216     }
217   } else {
218     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
219     sig_x = (x & MASK_BINARY_SIG1);
220     non_canon_x = 0;
221   }
222   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
223   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
224     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
225     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
226     if (sig_y > 9999999999999999ull) {
227       non_canon_y = 1;
228     } else {
229       non_canon_y = 0;
230     }
231   } else {
232     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
233     sig_y = (y & MASK_BINARY_SIG1);
234     non_canon_y = 0;
235   }
236   // ZERO (CASE4)
237   // some properties:
238   //(+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
239   //(ZERO x 10^A == ZERO x 10^B) for any valid A, B => therefore ignore the 
240   // exponent field
241   // (Any non-canonical # is considered 0)
242   if (non_canon_x || sig_x == 0) {
243     x_is_zero = 1;
244   }
245   if (non_canon_y || sig_y == 0) {
246     y_is_zero = 1;
247   }
248   // if both numbers are zero, neither is greater => return NOTGREATERTHAN
249   if (x_is_zero && y_is_zero) {
250     res = 0;
251     BID_RETURN (res);
252   } else if (x_is_zero) {
253     // is x is zero, it is greater if Y is negative
254     res = ((y & MASK_SIGN) == MASK_SIGN);
255     BID_RETURN (res);
256   } else if (y_is_zero) {
257     // is y is zero, X is greater if it is positive
258     res = ((x & MASK_SIGN) != MASK_SIGN);
259     BID_RETURN (res);
260   }
261   // OPPOSITE SIGN (CASE5)
262   // now, if the sign bits differ, x is greater if y is negative
263   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
264     res = ((y & MASK_SIGN) == MASK_SIGN);
265     BID_RETURN (res);
266   }
267   // REDUNDANT REPRESENTATIONS (CASE6)
268   // if both components are either bigger or smaller, 
269   // it is clear what needs to be done
270   if (sig_x > sig_y && exp_x > exp_y) {
271     res = ((x & MASK_SIGN) != MASK_SIGN);
272     BID_RETURN (res);
273   }
274   if (sig_x < sig_y && exp_x < exp_y) {
275     res = ((x & MASK_SIGN) == MASK_SIGN);
276     BID_RETURN (res);
277   }
278   // if exp_x is 15 greater than exp_y, no need for compensation
279   if (exp_x - exp_y > 15) { // difference cannot be greater than 10^15
280     if (x & MASK_SIGN)  // if both are negative
281       res = 0;
282     else        // if both are positive
283       res = 1;
284     BID_RETURN (res);
285   }
286   // if exp_x is 15 less than exp_y, no need for compensation
287   if (exp_y - exp_x > 15) {
288     if (x & MASK_SIGN)  // if both are negative
289       res = 1;
290     else        // if both are positive
291       res = 0;
292     BID_RETURN (res);
293   }
294   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
295   if (exp_x > exp_y) { // to simplify the loop below,
296     // otherwise adjust the x significand upwards
297     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
298                             mult_factor[exp_x - exp_y]);
299     // if postitive, return whichever significand is larger (converse if neg.)
300     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
301       res = 0;
302       BID_RETURN (res);
303     }
304     res = (((sig_n_prime.w[1] > 0)
305             || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
306                                             MASK_SIGN));
307     BID_RETURN (res);
308   }
309   // adjust the y significand upwards
310   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
311                           mult_factor[exp_y - exp_x]);
312   // if postitive, return whichever significand is larger 
313   //     (converse if negative)
314   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
315     res = 0;
316     BID_RETURN (res);
317   }
318   res = (((sig_n_prime.w[1] == 0)
319           && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
320                                             MASK_SIGN));
321   BID_RETURN (res);
322 }
323
324 #if DECIMAL_CALL_BY_REFERENCE
325 void
326 __bid64_quiet_greater_equal (int *pres, UINT64 * px,
327                            UINT64 *
328                            py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
329                            _EXC_INFO_PARAM) {
330   UINT64 x = *px;
331   UINT64 y = *py;
332 #else
333 int
334 __bid64_quiet_greater_equal (UINT64 x,
335                            UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
336                            _EXC_INFO_PARAM) {
337 #endif
338   int res;
339   int exp_x, exp_y;
340   UINT64 sig_x, sig_y;
341   UINT128 sig_n_prime;
342   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
343
344   // NaN (CASE1)
345   // if either number is NAN, the comparison is unordered : return 1
346   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
347     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
348       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
349     }
350     res = 0;
351     BID_RETURN (res);
352   }
353   // SIMPLE (CASE2)
354   // if all the bits are the same, these numbers are equal.
355   if (x == y) {
356     res = 1;
357     BID_RETURN (res);
358   }
359   // INFINITY (CASE3)
360   if ((x & MASK_INF) == MASK_INF) {
361     // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
362     if ((x & MASK_SIGN) == MASK_SIGN) {
363       // x is -inf, so it is less than y unless y is -inf
364       res = (((y & MASK_INF) == MASK_INF)
365              && (y & MASK_SIGN) == MASK_SIGN);
366       BID_RETURN (res);
367     } else { // x is pos_inf, no way for it to be less than y
368       res = 1;
369       BID_RETURN (res);
370     }
371   } else if ((y & MASK_INF) == MASK_INF) {
372     // x is finite, so:
373     //    if y is +inf, x<y
374     //    if y is -inf, x>y
375     res = ((y & MASK_SIGN) == MASK_SIGN);
376     BID_RETURN (res);
377   }
378   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
379   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
380     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
381     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
382     if (sig_x > 9999999999999999ull) {
383       non_canon_x = 1;
384     } else {
385       non_canon_x = 0;
386     }
387   } else {
388     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
389     sig_x = (x & MASK_BINARY_SIG1);
390     non_canon_x = 0;
391   }
392   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
393   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
394     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
395     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
396     if (sig_y > 9999999999999999ull) {
397       non_canon_y = 1;
398     } else {
399       non_canon_y = 0;
400     }
401   } else {
402     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
403     sig_y = (y & MASK_BINARY_SIG1);
404     non_canon_y = 0;
405   }
406   // ZERO (CASE4)
407   // some properties:
408   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
409   // (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
410   //   therefore ignore the exponent field
411   //  (Any non-canonical # is considered 0)
412   if (non_canon_x || sig_x == 0) {
413     x_is_zero = 1;
414   }
415   if (non_canon_y || sig_y == 0) {
416     y_is_zero = 1;
417   }
418   if (x_is_zero && y_is_zero) {
419     // if both numbers are zero, they are equal
420     res = 1;
421     BID_RETURN (res);
422   } else if (x_is_zero) {
423     // if x is zero, it is lessthan if Y is positive
424     res = ((y & MASK_SIGN) == MASK_SIGN);
425     BID_RETURN (res);
426   } else if (y_is_zero) {
427     // if y is zero, X is less if it is negative
428     res = ((x & MASK_SIGN) != MASK_SIGN);
429     BID_RETURN (res);
430   }
431   // OPPOSITE SIGN (CASE5)
432   // now, if the sign bits differ, x is less than if y is positive
433   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
434     res = ((y & MASK_SIGN) == MASK_SIGN);
435     BID_RETURN (res);
436   }
437   // REDUNDANT REPRESENTATIONS (CASE6)
438   // if both components are either bigger or smaller
439   if (sig_x > sig_y && exp_x >= exp_y) {
440     res = ((x & MASK_SIGN) != MASK_SIGN);
441     BID_RETURN (res);
442   }
443   if (sig_x < sig_y && exp_x <= exp_y) {
444     res = ((x & MASK_SIGN) == MASK_SIGN);
445     BID_RETURN (res);
446   }
447   // if exp_x is 15 greater than exp_y, no need for compensation
448   if (exp_x - exp_y > 15) {
449     res = ((x & MASK_SIGN) != MASK_SIGN);
450     // difference cannot be greater than 10^15
451     BID_RETURN (res);
452   }
453   // if exp_x is 15 less than exp_y, no need for compensation
454   if (exp_y - exp_x > 15) {
455     res = ((x & MASK_SIGN) == MASK_SIGN);
456     BID_RETURN (res);
457   }
458   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
459   if (exp_x > exp_y) { // to simplify the loop below,
460     // otherwise adjust the x significand upwards
461     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
462                             mult_factor[exp_x - exp_y]);
463     // return 1 if values are equal
464     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
465       res = 1;
466       BID_RETURN (res);
467     }
468     // if postitive, return whichever significand abs is smaller 
469     // (converse if negative)
470     res = (((sig_n_prime.w[1] == 0)
471             && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
472                                             MASK_SIGN));
473     BID_RETURN (res);
474   }
475   // adjust the y significand upwards
476   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
477                           mult_factor[exp_y - exp_x]);
478   // return 0 if values are equal
479   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
480     res = 1;
481     BID_RETURN (res);
482   }
483   // if positive, return whichever significand abs is smaller 
484   // (converse if negative)
485   res = (((sig_n_prime.w[1] > 0)
486           || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
487                                             MASK_SIGN));
488   BID_RETURN (res);
489 }
490
491 #if DECIMAL_CALL_BY_REFERENCE
492 void
493 __bid64_quiet_greater_unordered (int *pres, UINT64 * px,
494                                UINT64 *
495                                py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
496                                _EXC_INFO_PARAM) {
497   UINT64 x = *px;
498   UINT64 y = *py;
499 #else
500 int
501 __bid64_quiet_greater_unordered (UINT64 x,
502                                UINT64 y _EXC_FLAGS_PARAM
503                                _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
504 #endif
505   int res;
506   int exp_x, exp_y;
507   UINT64 sig_x, sig_y;
508   UINT128 sig_n_prime;
509   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
510
511   // NaN (CASE1)
512   // if either number is NAN, the comparison is unordered, rather than equal : 
513   // return 0
514   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
515     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
516       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
517     }
518     res = 1;
519     BID_RETURN (res);
520   }
521   // SIMPLE (CASE2)
522   // if all the bits are the same, these numbers are equal (not Greater).
523   if (x == y) {
524     res = 0;
525     BID_RETURN (res);
526   }
527   // INFINITY (CASE3)
528   if ((x & MASK_INF) == MASK_INF) {
529     // if x is neg infinity, there is no way it is greater than y, return 0
530     if (((x & MASK_SIGN) == MASK_SIGN)) {
531       res = 0;
532       BID_RETURN (res);
533     } else {
534       // x is pos infinity, it is greater, unless y is positive infinity => 
535       // return y!=pos_infinity
536       res = (((y & MASK_INF) != MASK_INF)
537              || ((y & MASK_SIGN) == MASK_SIGN));
538       BID_RETURN (res);
539     }
540   } else if ((y & MASK_INF) == MASK_INF) {
541     // x is finite, so if y is positive infinity, then x is less, return 0
542     //                 if y is negative infinity, then x is greater, return 1
543     res = ((y & MASK_SIGN) == MASK_SIGN);
544     BID_RETURN (res);
545   }
546   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
547   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
548     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
549     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
550     if (sig_x > 9999999999999999ull) {
551       non_canon_x = 1;
552     } else {
553       non_canon_x = 0;
554     }
555   } else {
556     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
557     sig_x = (x & MASK_BINARY_SIG1);
558     non_canon_x = 0;
559   }
560   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
561   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
562     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
563     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
564     if (sig_y > 9999999999999999ull) {
565       non_canon_y = 1;
566     } else {
567       non_canon_y = 0;
568     }
569   } else {
570     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
571     sig_y = (y & MASK_BINARY_SIG1);
572     non_canon_y = 0;
573   }
574   // ZERO (CASE4)
575   // some properties:
576   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
577   // (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
578   // therefore ignore the exponent field
579   //    (Any non-canonical # is considered 0)
580   if (non_canon_x || sig_x == 0) {
581     x_is_zero = 1;
582   }
583   if (non_canon_y || sig_y == 0) {
584     y_is_zero = 1;
585   }
586   // if both numbers are zero, neither is greater => return NOTGREATERTHAN
587   if (x_is_zero && y_is_zero) {
588     res = 0;
589     BID_RETURN (res);
590   } else if (x_is_zero) {
591     // is x is zero, it is greater if Y is negative
592     res = ((y & MASK_SIGN) == MASK_SIGN);
593     BID_RETURN (res);
594   } else if (y_is_zero) {
595     // is y is zero, X is greater if it is positive
596     res = ((x & MASK_SIGN) != MASK_SIGN);
597     BID_RETURN (res);
598   }
599   // OPPOSITE SIGN (CASE5)
600   // now, if the sign bits differ, x is greater if y is negative
601   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
602     res = ((y & MASK_SIGN) == MASK_SIGN);
603     BID_RETURN (res);
604   }
605   // REDUNDANT REPRESENTATIONS (CASE6)
606   // if both components are either bigger or smaller
607   if (sig_x > sig_y && exp_x >= exp_y) {
608     res = ((x & MASK_SIGN) != MASK_SIGN);
609     BID_RETURN (res);
610   }
611   if (sig_x < sig_y && exp_x <= exp_y) {
612     res = ((x & MASK_SIGN) == MASK_SIGN);
613     BID_RETURN (res);
614   }
615   // if exp_x is 15 greater than exp_y, no need for compensation
616   if (exp_x - exp_y > 15) {
617     // difference cannot be greater than 10^15
618     res = ((x & MASK_SIGN) != MASK_SIGN);
619     BID_RETURN (res);
620   }
621   // if exp_x is 15 less than exp_y, no need for compensation
622   if (exp_y - exp_x > 15) {
623     res = ((x & MASK_SIGN) == MASK_SIGN);
624     BID_RETURN (res);
625   }
626   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
627   if (exp_x > exp_y) { // to simplify the loop below,
628     // otherwise adjust the x significand upwards
629     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
630                             mult_factor[exp_x - exp_y]);
631     // if postitive, return whichever significand is larger 
632     // (converse if negative)
633     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
634       res = 0;
635       BID_RETURN (res);
636     }
637     res = (((sig_n_prime.w[1] > 0)
638             || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
639                                             MASK_SIGN));
640     BID_RETURN (res);
641   }
642   // adjust the y significand upwards
643   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
644                           mult_factor[exp_y - exp_x]);
645   // if postitive, return whichever significand is larger (converse if negative)
646   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
647     res = 0;
648     BID_RETURN (res);
649   }
650   res = (((sig_n_prime.w[1] == 0)
651           && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
652                                             MASK_SIGN));
653   BID_RETURN (res);
654 }
655
656 #if DECIMAL_CALL_BY_REFERENCE
657 void
658 __bid64_quiet_less (int *pres, UINT64 * px,
659                   UINT64 *
660                   py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM _EXC_INFO_PARAM) 
661 {
662   UINT64 x = *px;
663   UINT64 y = *py;
664 #else
665 int
666 __bid64_quiet_less (UINT64 x,
667                   UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
668                   _EXC_INFO_PARAM) {
669 #endif
670   int res;
671   int exp_x, exp_y;
672   UINT64 sig_x, sig_y;
673   UINT128 sig_n_prime;
674   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
675
676   // NaN (CASE1)
677   // if either number is NAN, the comparison is unordered : return 0
678   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
679     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
680       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
681     }
682     res = 0;
683     BID_RETURN (res);
684   }
685   // SIMPLE (CASE2)
686   // if all the bits are the same, these numbers are equal.
687   if (x == y) {
688     res = 0;
689     BID_RETURN (res);
690   }
691   // INFINITY (CASE3)
692   if ((x & MASK_INF) == MASK_INF) {
693     // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
694     if ((x & MASK_SIGN) == MASK_SIGN) {
695       // x is -inf, so it is less than y unless y is -inf
696       res = (((y & MASK_INF) != MASK_INF)
697              || (y & MASK_SIGN) != MASK_SIGN);
698       BID_RETURN (res);
699     } else {
700       // x is pos_inf, no way for it to be less than y
701       res = 0;
702       BID_RETURN (res);
703     }
704   } else if ((y & MASK_INF) == MASK_INF) {
705     // x is finite, so:
706     //    if y is +inf, x<y
707     //    if y is -inf, x>y
708     res = ((y & MASK_SIGN) != MASK_SIGN);
709     BID_RETURN (res);
710   }
711   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
712   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
713     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
714     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
715     if (sig_x > 9999999999999999ull) {
716       non_canon_x = 1;
717     } else {
718       non_canon_x = 0;
719     }
720   } else {
721     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
722     sig_x = (x & MASK_BINARY_SIG1);
723     non_canon_x = 0;
724   }
725   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
726   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
727     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
728     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
729     if (sig_y > 9999999999999999ull) {
730       non_canon_y = 1;
731     } else {
732       non_canon_y = 0;
733     }
734   } else {
735     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
736     sig_y = (y & MASK_BINARY_SIG1);
737     non_canon_y = 0;
738   }
739   // ZERO (CASE4)
740   // some properties:
741   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
742   // (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
743   //  therefore ignore the exponent field
744   //    (Any non-canonical # is considered 0)
745   if (non_canon_x || sig_x == 0) {
746     x_is_zero = 1;
747   }
748   if (non_canon_y || sig_y == 0) {
749     y_is_zero = 1;
750   }
751   if (x_is_zero && y_is_zero) {
752     // if both numbers are zero, they are equal
753     res = 0;
754     BID_RETURN (res);
755   } else if (x_is_zero) {
756     // if x is zero, it is lessthan if Y is positive
757     res = ((y & MASK_SIGN) != MASK_SIGN);
758     BID_RETURN (res);
759   } else if (y_is_zero) {
760     // if y is zero, X is less if it is negative
761     res = ((x & MASK_SIGN) == MASK_SIGN);
762     BID_RETURN (res);
763   }
764   // OPPOSITE SIGN (CASE5)
765   // now, if the sign bits differ, x is less than if y is positive
766   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
767     res = ((y & MASK_SIGN) != MASK_SIGN);
768     BID_RETURN (res);
769   }
770   // REDUNDANT REPRESENTATIONS (CASE6)
771   // if both components are either bigger or smaller, 
772   // it is clear what needs to be done
773   if (sig_x > sig_y && exp_x >= exp_y) {
774     res = ((x & MASK_SIGN) == MASK_SIGN);
775     BID_RETURN (res);
776   }
777   if (sig_x < sig_y && exp_x <= exp_y) {
778     res = ((x & MASK_SIGN) != MASK_SIGN);
779     BID_RETURN (res);
780   }
781   // if exp_x is 15 greater than exp_y, no need for compensation
782   if (exp_x - exp_y > 15) {
783     res = ((x & MASK_SIGN) == MASK_SIGN);
784     // difference cannot be greater than 10^15
785     BID_RETURN (res);
786   }
787   // if exp_x is 15 less than exp_y, no need for compensation
788   if (exp_y - exp_x > 15) {
789     res = ((x & MASK_SIGN) != MASK_SIGN);
790     BID_RETURN (res);
791   }
792   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
793   if (exp_x > exp_y) { // to simplify the loop below,
794     // otherwise adjust the x significand upwards
795     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
796                             mult_factor[exp_x - exp_y]);
797     // return 0 if values are equal
798     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
799       res = 0;
800       BID_RETURN (res);
801     }
802     // if postitive, return whichever significand abs is smaller 
803     // (converse if negative)
804     res = (((sig_n_prime.w[1] == 0)
805             && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
806                                             MASK_SIGN));
807     BID_RETURN (res);
808   }
809   // adjust the y significand upwards
810   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
811                           mult_factor[exp_y - exp_x]);
812   // return 0 if values are equal
813   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
814     res = 0;
815     BID_RETURN (res);
816   }
817   // if positive, return whichever significand abs is smaller 
818   // (converse if negative)
819   res = (((sig_n_prime.w[1] > 0)
820           || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
821                                             MASK_SIGN));
822   BID_RETURN (res);
823 }
824
825 #if DECIMAL_CALL_BY_REFERENCE
826 void
827 __bid64_quiet_less_equal (int *pres, UINT64 * px,
828                         UINT64 *
829                         py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
830                         _EXC_INFO_PARAM) {
831   UINT64 x = *px;
832   UINT64 y = *py;
833 #else
834 int
835 __bid64_quiet_less_equal (UINT64 x,
836                         UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
837                         _EXC_INFO_PARAM) {
838 #endif
839   int res;
840   int exp_x, exp_y;
841   UINT64 sig_x, sig_y;
842   UINT128 sig_n_prime;
843   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
844
845   // NaN (CASE1)
846   // if either number is NAN, the comparison is unordered, rather than equal : 
847   //     return 0
848   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
849     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
850       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
851     }
852     res = 0;
853     BID_RETURN (res);
854   }
855   // SIMPLE (CASE2)
856   // if all the bits are the same, these numbers are equal (LESSEQUAL).
857   if (x == y) {
858     res = 1;
859     BID_RETURN (res);
860   }
861   // INFINITY (CASE3)
862   if ((x & MASK_INF) == MASK_INF) {
863     if (((x & MASK_SIGN) == MASK_SIGN)) {
864       // if x is neg infinity, it must be lessthan or equal to y return 1
865       res = 1;
866       BID_RETURN (res);
867     } else {
868       // x is pos infinity, it is greater, unless y is positive infinity => 
869       // return y==pos_infinity
870       res = !(((y & MASK_INF) != MASK_INF)
871               || ((y & MASK_SIGN) == MASK_SIGN));
872       BID_RETURN (res);
873     }
874   } else if ((y & MASK_INF) == MASK_INF) {
875     // x is finite, so if y is positive infinity, then x is less, return 1
876     //                 if y is negative infinity, then x is greater, return 0
877     res = ((y & MASK_SIGN) != MASK_SIGN);
878     BID_RETURN (res);
879   }
880   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
881   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
882     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
883     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
884     if (sig_x > 9999999999999999ull) {
885       non_canon_x = 1;
886     } else {
887       non_canon_x = 0;
888     }
889   } else {
890     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
891     sig_x = (x & MASK_BINARY_SIG1);
892     non_canon_x = 0;
893   }
894   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
895   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
896     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
897     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
898     if (sig_y > 9999999999999999ull) {
899       non_canon_y = 1;
900     } else {
901       non_canon_y = 0;
902     }
903   } else {
904     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
905     sig_y = (y & MASK_BINARY_SIG1);
906     non_canon_y = 0;
907   }
908   // ZERO (CASE4)
909   // some properties:
910   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
911   // (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
912   //     therefore ignore the exponent field
913   //    (Any non-canonical # is considered 0)
914   if (non_canon_x || sig_x == 0) {
915     x_is_zero = 1;
916   }
917   if (non_canon_y || sig_y == 0) {
918     y_is_zero = 1;
919   }
920   if (x_is_zero && y_is_zero) {
921     // if both numbers are zero, they are equal -> return 1
922     res = 1;
923     BID_RETURN (res);
924   } else if (x_is_zero) {
925     // if x is zero, it is lessthan if Y is positive
926     res = ((y & MASK_SIGN) != MASK_SIGN);
927     BID_RETURN (res);
928   } else if (y_is_zero) {
929     // if y is zero, X is less if it is negative
930     res = ((x & MASK_SIGN) == MASK_SIGN);
931     BID_RETURN (res);
932   }
933   // OPPOSITE SIGN (CASE5)
934   // now, if the sign bits differ, x is less than if y is positive
935   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
936     res = ((y & MASK_SIGN) != MASK_SIGN);
937     BID_RETURN (res);
938   }
939   // REDUNDANT REPRESENTATIONS (CASE6)
940   // if both components are either bigger or smaller
941   if (sig_x > sig_y && exp_x >= exp_y) {
942     res = ((x & MASK_SIGN) == MASK_SIGN);
943     BID_RETURN (res);
944   }
945   if (sig_x < sig_y && exp_x <= exp_y) {
946     res = ((x & MASK_SIGN) != MASK_SIGN);
947     BID_RETURN (res);
948   }
949   // if exp_x is 15 greater than exp_y, no need for compensation
950   if (exp_x - exp_y > 15) {
951     res = ((x & MASK_SIGN) == MASK_SIGN);
952     // difference cannot be greater than 10^15
953     BID_RETURN (res);
954   }
955   // if exp_x is 15 less than exp_y, no need for compensation
956   if (exp_y - exp_x > 15) {
957     res = ((x & MASK_SIGN) != MASK_SIGN);
958     BID_RETURN (res);
959   }
960   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
961   if (exp_x > exp_y) { // to simplify the loop below,
962     // otherwise adjust the x significand upwards
963     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
964                             mult_factor[exp_x - exp_y]);
965     // return 1 if values are equal
966     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
967       res = 1;
968       BID_RETURN (res);
969     }
970     // if postitive, return whichever significand abs is smaller 
971     //     (converse if negative)
972     res = (((sig_n_prime.w[1] == 0)
973             && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
974                                             MASK_SIGN));
975     BID_RETURN (res);
976   }
977   // adjust the y significand upwards
978   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
979                           mult_factor[exp_y - exp_x]);
980   // return 1 if values are equal
981   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
982     res = 1;
983     BID_RETURN (res);
984   }
985   // if positive, return whichever significand abs is smaller 
986   //     (converse if negative)
987   res = (((sig_n_prime.w[1] > 0)
988           || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
989                                             MASK_SIGN));
990   BID_RETURN (res);
991 }
992
993 #if DECIMAL_CALL_BY_REFERENCE
994 void
995 __bid64_quiet_less_unordered (int *pres, UINT64 * px,
996                             UINT64 *
997                             py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
998                             _EXC_INFO_PARAM) {
999   UINT64 x = *px;
1000   UINT64 y = *py;
1001 #else
1002 int
1003 __bid64_quiet_less_unordered (UINT64 x,
1004                             UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1005                             _EXC_INFO_PARAM) {
1006 #endif
1007   int res;
1008   int exp_x, exp_y;
1009   UINT64 sig_x, sig_y;
1010   UINT128 sig_n_prime;
1011   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
1012
1013   // NaN (CASE1)
1014   // if either number is NAN, the comparison is unordered : return 0
1015   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1016     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
1017       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
1018     }
1019     res = 1;
1020     BID_RETURN (res);
1021   }
1022   // SIMPLE (CASE2)
1023   // if all the bits are the same, these numbers are equal.
1024   if (x == y) {
1025     res = 0;
1026     BID_RETURN (res);
1027   }
1028   // INFINITY (CASE3)
1029   if ((x & MASK_INF) == MASK_INF) {
1030     // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
1031     if ((x & MASK_SIGN) == MASK_SIGN) {
1032       // x is -inf, so it is less than y unless y is -inf
1033       res = (((y & MASK_INF) != MASK_INF)
1034              || (y & MASK_SIGN) != MASK_SIGN);
1035       BID_RETURN (res);
1036     } else {
1037       // x is pos_inf, no way for it to be less than y
1038       res = 0;
1039       BID_RETURN (res);
1040     }
1041   } else if ((y & MASK_INF) == MASK_INF) {
1042     // x is finite, so:
1043     //    if y is +inf, x<y
1044     //    if y is -inf, x>y
1045     res = ((y & MASK_SIGN) != MASK_SIGN);
1046     BID_RETURN (res);
1047   }
1048   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1049   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1050     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
1051     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1052     if (sig_x > 9999999999999999ull) {
1053       non_canon_x = 1;
1054     } else {
1055       non_canon_x = 0;
1056     }
1057   } else {
1058     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
1059     sig_x = (x & MASK_BINARY_SIG1);
1060     non_canon_x = 0;
1061   }
1062   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1063   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1064     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
1065     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1066     if (sig_y > 9999999999999999ull) {
1067       non_canon_y = 1;
1068     } else {
1069       non_canon_y = 0;
1070     }
1071   } else {
1072     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
1073     sig_y = (y & MASK_BINARY_SIG1);
1074     non_canon_y = 0;
1075   }
1076   // ZERO (CASE4)
1077   // some properties:
1078   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
1079   // (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
1080   //     therefore ignore the exponent field
1081   //    (Any non-canonical # is considered 0)
1082   if (non_canon_x || sig_x == 0) {
1083     x_is_zero = 1;
1084   }
1085   if (non_canon_y || sig_y == 0) {
1086     y_is_zero = 1;
1087   }
1088   if (x_is_zero && y_is_zero) {
1089     // if both numbers are zero, they are equal
1090     res = 0;
1091     BID_RETURN (res);
1092   } else if (x_is_zero) {
1093     // if x is zero, it is lessthan if Y is positive
1094     res = ((y & MASK_SIGN) != MASK_SIGN);
1095     BID_RETURN (res);
1096   } else if (y_is_zero) {
1097     // if y is zero, X is less if it is negative
1098     res = ((x & MASK_SIGN) == MASK_SIGN);
1099     BID_RETURN (res);
1100   }
1101   // OPPOSITE SIGN (CASE5)
1102   // now, if the sign bits differ, x is less than if y is positive
1103   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
1104     res = ((y & MASK_SIGN) != MASK_SIGN);
1105     BID_RETURN (res);
1106   }
1107   // REDUNDANT REPRESENTATIONS (CASE6)
1108   // if both components are either bigger or smaller
1109   if (sig_x > sig_y && exp_x >= exp_y) {
1110     res = ((x & MASK_SIGN) == MASK_SIGN);
1111     BID_RETURN (res);
1112   }
1113   if (sig_x < sig_y && exp_x <= exp_y) {
1114     res = ((x & MASK_SIGN) != MASK_SIGN);
1115     BID_RETURN (res);
1116   }
1117   // if exp_x is 15 greater than exp_y, no need for compensation
1118   if (exp_x - exp_y > 15) {
1119     res = ((x & MASK_SIGN) == MASK_SIGN);
1120     // difference cannot be greater than 10^15
1121     BID_RETURN (res);
1122   }
1123   // if exp_x is 15 less than exp_y, no need for compensation
1124   if (exp_y - exp_x > 15) {
1125     res = ((x & MASK_SIGN) != MASK_SIGN);
1126     BID_RETURN (res);
1127   }
1128   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
1129   if (exp_x > exp_y) { // to simplify the loop below,
1130     // otherwise adjust the x significand upwards
1131     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
1132                             mult_factor[exp_x - exp_y]);
1133     // return 0 if values are equal
1134     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
1135       res = 0;
1136       BID_RETURN (res);
1137     }
1138     // if postitive, return whichever significand abs is smaller 
1139     //     (converse if negative)
1140     res = (((sig_n_prime.w[1] == 0)
1141             && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
1142                                             MASK_SIGN));
1143     BID_RETURN (res);
1144   }
1145   // adjust the y significand upwards
1146   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
1147                           mult_factor[exp_y - exp_x]);
1148   // return 0 if values are equal
1149   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
1150     res = 0;
1151     BID_RETURN (res);
1152   }
1153   // if positive, return whichever significand abs is smaller 
1154   //     (converse if negative)
1155   res = (((sig_n_prime.w[1] > 0)
1156           || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
1157                                             MASK_SIGN));
1158   BID_RETURN (res);
1159 }
1160
1161 #if DECIMAL_CALL_BY_REFERENCE
1162 void
1163 __bid64_quiet_not_equal (int *pres, UINT64 * px,
1164                        UINT64 *
1165                        py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1166                        _EXC_INFO_PARAM) {
1167   UINT64 x = *px;
1168   UINT64 y = *py;
1169 #else
1170 int
1171 __bid64_quiet_not_equal (UINT64 x,
1172                        UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1173                        _EXC_INFO_PARAM) {
1174 #endif
1175   int res;
1176   int exp_x, exp_y, exp_t;
1177   UINT64 sig_x, sig_y, sig_t;
1178   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y, lcv;
1179
1180   // NaN (CASE1)
1181   // if either number is NAN, the comparison is unordered, 
1182   // rather than equal : return 1
1183   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1184     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
1185       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
1186     }
1187     res = 1;
1188     BID_RETURN (res);
1189   }
1190   // SIMPLE (CASE2)
1191   // if all the bits are the same, these numbers are equivalent.
1192   if (x == y) {
1193     res = 0;
1194     BID_RETURN (res);
1195   }
1196   // INFINITY (CASE3)
1197   if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
1198     res = (((x ^ y) & MASK_SIGN) == MASK_SIGN);
1199     BID_RETURN (res);
1200   }
1201   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1202   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1203     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
1204     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1205     if (sig_x > 9999999999999999ull) {
1206       non_canon_x = 1;
1207     } else {
1208       non_canon_x = 0;
1209     }
1210   } else {
1211     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
1212     sig_x = (x & MASK_BINARY_SIG1);
1213     non_canon_x = 0;
1214   }
1215
1216   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1217   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1218     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
1219     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1220     if (sig_y > 9999999999999999ull) {
1221       non_canon_y = 1;
1222     } else {
1223       non_canon_y = 0;
1224     }
1225   } else {
1226     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
1227     sig_y = (y & MASK_BINARY_SIG1);
1228     non_canon_y = 0;
1229   }
1230
1231   // ZERO (CASE4)
1232   // some properties:
1233   // (+ZERO==-ZERO) => therefore ignore the sign
1234   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
1235   //        therefore ignore the exponent field
1236   //    (Any non-canonical # is considered 0)
1237   if (non_canon_x || sig_x == 0) {
1238     x_is_zero = 1;
1239   }
1240   if (non_canon_y || sig_y == 0) {
1241     y_is_zero = 1;
1242   }
1243
1244   if (x_is_zero && y_is_zero) {
1245     res = 0;
1246     BID_RETURN (res);
1247   } else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
1248     res = 1;
1249     BID_RETURN (res);
1250   }
1251   // OPPOSITE SIGN (CASE5)
1252   // now, if the sign bits differ => not equal : return 1
1253   if ((x ^ y) & MASK_SIGN) {
1254     res = 1;
1255     BID_RETURN (res);
1256   }
1257   // REDUNDANT REPRESENTATIONS (CASE6)
1258   if (exp_x > exp_y) { // to simplify the loop below,
1259     SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
1260     SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
1261   }
1262
1263   if (exp_y - exp_x > 15) {
1264     res = 1;
1265     BID_RETURN (res);
1266   }
1267   // difference cannot be greater than 10^16
1268
1269   for (lcv = 0; lcv < (exp_y - exp_x); lcv++) {
1270
1271     // recalculate y's significand upwards
1272     sig_y = sig_y * 10;
1273     if (sig_y > 9999999999999999ull) {
1274       res = 1;
1275       BID_RETURN (res);
1276     }
1277   }
1278
1279   {
1280     res = sig_y != sig_x;
1281     BID_RETURN (res);
1282   }
1283
1284 }
1285
1286 #if DECIMAL_CALL_BY_REFERENCE
1287 void
1288 __bid64_quiet_not_greater (int *pres, UINT64 * px,
1289                          UINT64 *
1290                          py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1291                          _EXC_INFO_PARAM) {
1292   UINT64 x = *px;
1293   UINT64 y = *py;
1294 #else
1295 int
1296 __bid64_quiet_not_greater (UINT64 x,
1297                          UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1298                          _EXC_INFO_PARAM) {
1299 #endif
1300   int res;
1301   int exp_x, exp_y;
1302   UINT64 sig_x, sig_y;
1303   UINT128 sig_n_prime;
1304   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
1305
1306   // NaN (CASE1)
1307   // if either number is NAN, the comparison is unordered, 
1308   //   rather than equal : return 0
1309   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1310     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
1311       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
1312     }
1313     res = 1;
1314     BID_RETURN (res);
1315   }
1316   // SIMPLE (CASE2)
1317   // if all the bits are the same, these numbers are equal (LESSEQUAL).
1318   if (x == y) {
1319     res = 1;
1320     BID_RETURN (res);
1321   }
1322   // INFINITY (CASE3)
1323   if ((x & MASK_INF) == MASK_INF) {
1324     // if x is neg infinity, it must be lessthan or equal to y return 1
1325     if (((x & MASK_SIGN) == MASK_SIGN)) {
1326       res = 1;
1327       BID_RETURN (res);
1328     }
1329     // x is pos infinity, it is greater, unless y is positive 
1330     // infinity => return y==pos_infinity
1331     else {
1332       res = !(((y & MASK_INF) != MASK_INF)
1333               || ((y & MASK_SIGN) == MASK_SIGN));
1334       BID_RETURN (res);
1335     }
1336   } else if ((y & MASK_INF) == MASK_INF) {
1337     // x is finite, so if y is positive infinity, then x is less, return 1
1338     //                 if y is negative infinity, then x is greater, return 0
1339     {
1340       res = ((y & MASK_SIGN) != MASK_SIGN);
1341       BID_RETURN (res);
1342     }
1343   }
1344   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1345   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1346     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
1347     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1348     if (sig_x > 9999999999999999ull) {
1349       non_canon_x = 1;
1350     } else {
1351       non_canon_x = 0;
1352     }
1353   } else {
1354     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
1355     sig_x = (x & MASK_BINARY_SIG1);
1356     non_canon_x = 0;
1357   }
1358
1359   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1360   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1361     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
1362     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1363     if (sig_y > 9999999999999999ull) {
1364       non_canon_y = 1;
1365     } else {
1366       non_canon_y = 0;
1367     }
1368   } else {
1369     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
1370     sig_y = (y & MASK_BINARY_SIG1);
1371     non_canon_y = 0;
1372   }
1373
1374   // ZERO (CASE4)
1375   // some properties:
1376   // (+ZERO==-ZERO) => therefore ignore the sign, and neither 
1377   //         number is greater
1378   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
1379   //         therefore ignore the exponent field
1380   //    (Any non-canonical # is considered 0)
1381   if (non_canon_x || sig_x == 0) {
1382     x_is_zero = 1;
1383   }
1384   if (non_canon_y || sig_y == 0) {
1385     y_is_zero = 1;
1386   }
1387   // if both numbers are zero, they are equal -> return 1
1388   if (x_is_zero && y_is_zero) {
1389     res = 1;
1390     BID_RETURN (res);
1391   }
1392   // if x is zero, it is lessthan if Y is positive
1393   else if (x_is_zero) {
1394     res = ((y & MASK_SIGN) != MASK_SIGN);
1395     BID_RETURN (res);
1396   }
1397   // if y is zero, X is less if it is negative
1398   else if (y_is_zero) {
1399     res = ((x & MASK_SIGN) == MASK_SIGN);
1400     BID_RETURN (res);
1401   }
1402   // OPPOSITE SIGN (CASE5)
1403   // now, if the sign bits differ, x is less than if y is positive
1404   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
1405     res = ((y & MASK_SIGN) != MASK_SIGN);
1406     BID_RETURN (res);
1407   }
1408   // REDUNDANT REPRESENTATIONS (CASE6)
1409   // if both components are either bigger or smaller
1410   if (sig_x > sig_y && exp_x >= exp_y) {
1411     res = ((x & MASK_SIGN) == MASK_SIGN);
1412     BID_RETURN (res);
1413   }
1414   if (sig_x < sig_y && exp_x <= exp_y) {
1415     res = ((x & MASK_SIGN) != MASK_SIGN);
1416     BID_RETURN (res);
1417   }
1418   // if exp_x is 15 greater than exp_y, no need for compensation
1419   if (exp_x - exp_y > 15) {
1420     res = ((x & MASK_SIGN) == MASK_SIGN);
1421     BID_RETURN (res);
1422   }
1423   // difference cannot be greater than 10^15
1424
1425   // if exp_x is 15 less than exp_y, no need for compensation
1426   if (exp_y - exp_x > 15) {
1427     res = ((x & MASK_SIGN) != MASK_SIGN);
1428     BID_RETURN (res);
1429   }
1430   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
1431   if (exp_x > exp_y) { // to simplify the loop below,
1432
1433     // otherwise adjust the x significand upwards
1434     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
1435                             mult_factor[exp_x - exp_y]);
1436
1437     // return 1 if values are equal
1438     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
1439       res = 1;
1440       BID_RETURN (res);
1441     }
1442     // if postitive, return whichever significand abs is smaller 
1443     //     (converse if negative)
1444     {
1445       res = (((sig_n_prime.w[1] == 0)
1446               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
1447                                               MASK_SIGN));
1448       BID_RETURN (res);
1449     }
1450   }
1451   // adjust the y significand upwards
1452   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
1453                           mult_factor[exp_y - exp_x]);
1454
1455   // return 1 if values are equal
1456   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
1457     res = 1;
1458     BID_RETURN (res);
1459   }
1460   // if positive, return whichever significand abs is smaller 
1461   //     (converse if negative)
1462   {
1463     res = (((sig_n_prime.w[1] > 0)
1464             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
1465                                               MASK_SIGN));
1466     BID_RETURN (res);
1467   }
1468 }
1469
1470 #if DECIMAL_CALL_BY_REFERENCE
1471 void
1472 __bid64_quiet_not_less (int *pres, UINT64 * px,
1473                       UINT64 *
1474                       py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1475                       _EXC_INFO_PARAM) {
1476   UINT64 x = *px;
1477   UINT64 y = *py;
1478 #else
1479 int
1480 __bid64_quiet_not_less (UINT64 x,
1481                       UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1482                       _EXC_INFO_PARAM) {
1483 #endif
1484   int res;
1485   int exp_x, exp_y;
1486   UINT64 sig_x, sig_y;
1487   UINT128 sig_n_prime;
1488   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
1489
1490   // NaN (CASE1)
1491   // if either number is NAN, the comparison is unordered : return 1
1492   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1493     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
1494       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
1495     }
1496     res = 1;
1497     BID_RETURN (res);
1498   }
1499   // SIMPLE (CASE2)
1500   // if all the bits are the same, these numbers are equal.
1501   if (x == y) {
1502     res = 1;
1503     BID_RETURN (res);
1504   }
1505   // INFINITY (CASE3)
1506   if ((x & MASK_INF) == MASK_INF) {
1507     // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
1508     if ((x & MASK_SIGN) == MASK_SIGN)
1509       // x is -inf, so it is less than y unless y is -inf
1510     {
1511       res = (((y & MASK_INF) == MASK_INF)
1512              && (y & MASK_SIGN) == MASK_SIGN);
1513       BID_RETURN (res);
1514     } else
1515       // x is pos_inf, no way for it to be less than y
1516     {
1517       res = 1;
1518       BID_RETURN (res);
1519     }
1520   } else if ((y & MASK_INF) == MASK_INF) {
1521     // x is finite, so:
1522     //    if y is +inf, x<y
1523     //    if y is -inf, x>y
1524     {
1525       res = ((y & MASK_SIGN) == MASK_SIGN);
1526       BID_RETURN (res);
1527     }
1528   }
1529   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1530   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1531     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
1532     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1533     if (sig_x > 9999999999999999ull) {
1534       non_canon_x = 1;
1535     } else {
1536       non_canon_x = 0;
1537     }
1538   } else {
1539     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
1540     sig_x = (x & MASK_BINARY_SIG1);
1541     non_canon_x = 0;
1542   }
1543
1544   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1545   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1546     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
1547     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1548     if (sig_y > 9999999999999999ull) {
1549       non_canon_y = 1;
1550     } else {
1551       non_canon_y = 0;
1552     }
1553   } else {
1554     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
1555     sig_y = (y & MASK_BINARY_SIG1);
1556     non_canon_y = 0;
1557   }
1558
1559   // ZERO (CASE4)
1560   // some properties:
1561   // (+ZERO==-ZERO) => therefore ignore the sign, and neither 
1562   //        number is greater
1563   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
1564   //        therefore ignore the exponent field
1565   //    (Any non-canonical # is considered 0)
1566   if (non_canon_x || sig_x == 0) {
1567     x_is_zero = 1;
1568   }
1569   if (non_canon_y || sig_y == 0) {
1570     y_is_zero = 1;
1571   }
1572   // if both numbers are zero, they are equal
1573   if (x_is_zero && y_is_zero) {
1574     res = 1;
1575     BID_RETURN (res);
1576   }
1577   // if x is zero, it is lessthan if Y is positive
1578   else if (x_is_zero) {
1579     res = ((y & MASK_SIGN) == MASK_SIGN);
1580     BID_RETURN (res);
1581   }
1582   // if y is zero, X is less if it is negative
1583   else if (y_is_zero) {
1584     res = ((x & MASK_SIGN) != MASK_SIGN);
1585     BID_RETURN (res);
1586   }
1587   // OPPOSITE SIGN (CASE5)
1588   // now, if the sign bits differ, x is less than if y is positive
1589   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
1590     res = ((y & MASK_SIGN) == MASK_SIGN);
1591     BID_RETURN (res);
1592   }
1593   // REDUNDANT REPRESENTATIONS (CASE6)
1594   // if both components are either bigger or smaller
1595   if (sig_x > sig_y && exp_x >= exp_y) {
1596     res = ((x & MASK_SIGN) != MASK_SIGN);
1597     BID_RETURN (res);
1598   }
1599   if (sig_x < sig_y && exp_x <= exp_y) {
1600     res = ((x & MASK_SIGN) == MASK_SIGN);
1601     BID_RETURN (res);
1602   }
1603   // if exp_x is 15 greater than exp_y, no need for compensation
1604   if (exp_x - exp_y > 15) {
1605     res = ((x & MASK_SIGN) != MASK_SIGN);
1606     BID_RETURN (res);
1607   }
1608   // difference cannot be greater than 10^15
1609
1610   // if exp_x is 15 less than exp_y, no need for compensation
1611   if (exp_y - exp_x > 15) {
1612     res = ((x & MASK_SIGN) == MASK_SIGN);
1613     BID_RETURN (res);
1614   }
1615   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
1616   if (exp_x > exp_y) { // to simplify the loop below,
1617
1618     // otherwise adjust the x significand upwards
1619     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
1620                             mult_factor[exp_x - exp_y]);
1621
1622     // return 0 if values are equal
1623     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
1624       res = 1;
1625       BID_RETURN (res);
1626     }
1627     // if postitive, return whichever significand abs is smaller 
1628     //     (converse if negative)
1629     {
1630       res = (((sig_n_prime.w[1] == 0)
1631               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
1632                                               MASK_SIGN));
1633       BID_RETURN (res);
1634     }
1635   }
1636   // adjust the y significand upwards
1637   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
1638                           mult_factor[exp_y - exp_x]);
1639
1640   // return 0 if values are equal
1641   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
1642     res = 1;
1643     BID_RETURN (res);
1644   }
1645   // if positive, return whichever significand abs is smaller 
1646   //     (converse if negative)
1647   {
1648     res = (((sig_n_prime.w[1] > 0)
1649             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
1650                                               MASK_SIGN));
1651     BID_RETURN (res);
1652   }
1653 }
1654
1655 #if DECIMAL_CALL_BY_REFERENCE
1656 void
1657 __bid64_quiet_ordered (int *pres, UINT64 * px,
1658                      UINT64 *
1659                      py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1660                      _EXC_INFO_PARAM) {
1661   UINT64 x = *px;
1662   UINT64 y = *py;
1663 #else
1664 int
1665 __bid64_quiet_ordered (UINT64 x,
1666                      UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1667                      _EXC_INFO_PARAM) {
1668 #endif
1669   int res;
1670
1671   // NaN (CASE1)
1672   // if either number is NAN, the comparison is ordered, rather than equal : return 0
1673   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1674     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
1675       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
1676     }
1677     res = 0;
1678     BID_RETURN (res);
1679   } else {
1680     res = 1;
1681     BID_RETURN (res);
1682   }
1683 }
1684
1685 #if DECIMAL_CALL_BY_REFERENCE
1686 void
1687 __bid64_quiet_unordered (int *pres, UINT64 * px,
1688                        UINT64 *
1689                        py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1690                        _EXC_INFO_PARAM) {
1691   UINT64 x = *px;
1692   UINT64 y = *py;
1693 #else
1694 int
1695 __bid64_quiet_unordered (UINT64 x,
1696                        UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1697                        _EXC_INFO_PARAM) {
1698 #endif
1699   int res;
1700
1701   // NaN (CASE1)
1702   // if either number is NAN, the comparison is unordered, 
1703   //     rather than equal : return 0
1704   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1705     if ((x & MASK_SNAN) == MASK_SNAN || (y & MASK_SNAN) == MASK_SNAN) {
1706       *pfpsf |= INVALID_EXCEPTION; // set exception if sNaN
1707     }
1708     res = 1;
1709     BID_RETURN (res);
1710   } else {
1711     res = 0;
1712     BID_RETURN (res);
1713   }
1714 }
1715
1716 #if DECIMAL_CALL_BY_REFERENCE
1717 void
1718 __bid64_signaling_equal (int *pres, UINT64 * px,
1719                        UINT64 *
1720                        py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1721                        _EXC_INFO_PARAM) {
1722   UINT64 x = *px;
1723   UINT64 y = *py;
1724 #else
1725 int
1726 __bid64_signaling_equal (UINT64 x,
1727                        UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1728                        _EXC_INFO_PARAM) {
1729 #endif
1730   int res;
1731   int exp_x, exp_y, exp_t;
1732   UINT64 sig_x, sig_y, sig_t;
1733   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y, lcv;
1734
1735   // NaN (CASE1)
1736   // if either number is NAN, the comparison is unordered, 
1737   //     rather than equal : return 0
1738   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1739     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
1740     res = 0;
1741     BID_RETURN (res);
1742   }
1743   // SIMPLE (CASE2)
1744   // if all the bits are the same, these numbers are equivalent.
1745   if (x == y) {
1746     res = 1;
1747     BID_RETURN (res);
1748   }
1749   // INFINITY (CASE3)
1750   if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
1751     res = (((x ^ y) & MASK_SIGN) != MASK_SIGN);
1752     BID_RETURN (res);
1753   }
1754   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1755   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1756     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
1757     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1758     if (sig_x > 9999999999999999ull) {
1759       non_canon_x = 1;
1760     } else {
1761       non_canon_x = 0;
1762     }
1763   } else {
1764     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
1765     sig_x = (x & MASK_BINARY_SIG1);
1766     non_canon_x = 0;
1767   }
1768
1769   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1770   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1771     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
1772     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1773     if (sig_y > 9999999999999999ull) {
1774       non_canon_y = 1;
1775     } else {
1776       non_canon_y = 0;
1777     }
1778   } else {
1779     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
1780     sig_y = (y & MASK_BINARY_SIG1);
1781     non_canon_y = 0;
1782   }
1783
1784   // ZERO (CASE4)
1785   // some properties:
1786   // (+ZERO==-ZERO) => therefore ignore the sign
1787   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
1788   //     therefore ignore the exponent field
1789   //    (Any non-canonical # is considered 0)
1790   if (non_canon_x || sig_x == 0) {
1791     x_is_zero = 1;
1792   }
1793   if (non_canon_y || sig_y == 0) {
1794     y_is_zero = 1;
1795   }
1796
1797   if (x_is_zero && y_is_zero) {
1798     res = 1;
1799     BID_RETURN (res);
1800   } else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
1801     res = 0;
1802     BID_RETURN (res);
1803   }
1804   // OPPOSITE SIGN (CASE5)
1805   // now, if the sign bits differ => not equal : return 0
1806   if ((x ^ y) & MASK_SIGN) {
1807     res = 0;
1808     BID_RETURN (res);
1809   }
1810   // REDUNDANT REPRESENTATIONS (CASE6)
1811   if (exp_x > exp_y) { // to simplify the loop below,
1812     SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
1813     SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
1814   }
1815
1816   if (exp_y - exp_x > 15) {
1817     res = 0;
1818     BID_RETURN (res);
1819   }
1820   // difference cannot be greater than 10^15
1821
1822   for (lcv = 0; lcv < (exp_y - exp_x); lcv++) {
1823
1824     // recalculate y's significand upwards
1825     sig_y = sig_y * 10;
1826     if (sig_y > 9999999999999999ull) {
1827       res = 0;
1828       BID_RETURN (res);
1829     }
1830   }
1831
1832   {
1833     res = sig_y == sig_x;
1834     BID_RETURN (res);
1835   }
1836
1837 }
1838
1839 #if DECIMAL_CALL_BY_REFERENCE
1840 void
1841 __bid64_signaling_greater (int *pres, UINT64 * px,
1842                          UINT64 *
1843                          py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1844                          _EXC_INFO_PARAM) {
1845   UINT64 x = *px;
1846   UINT64 y = *py;
1847 #else
1848 int
1849 __bid64_signaling_greater (UINT64 x,
1850                          UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
1851                          _EXC_INFO_PARAM) {
1852 #endif
1853   int res;
1854   int exp_x, exp_y;
1855   UINT64 sig_x, sig_y;
1856   UINT128 sig_n_prime;
1857   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
1858
1859   // NaN (CASE1)
1860   // if either number is NAN, the comparison is unordered, 
1861   //     rather than equal : return 0
1862   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
1863     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
1864     res = 0;
1865     BID_RETURN (res);
1866   }
1867   // SIMPLE (CASE2)
1868   // if all the bits are the same, these numbers are equal (not Greater).
1869   if (x == y) {
1870     res = 0;
1871     BID_RETURN (res);
1872   }
1873   // INFINITY (CASE3)
1874   if ((x & MASK_INF) == MASK_INF) {
1875     // if x is neg infinity, there is no way it is greater than y, return 0
1876     if (((x & MASK_SIGN) == MASK_SIGN)) {
1877       res = 0;
1878       BID_RETURN (res);
1879     }
1880     // x is pos infinity, it is greater, 
1881     // unless y is positive infinity => return y!=pos_infinity
1882     else {
1883       res = (((y & MASK_INF) != MASK_INF)
1884              || ((y & MASK_SIGN) == MASK_SIGN));
1885       BID_RETURN (res);
1886     }
1887   } else if ((y & MASK_INF) == MASK_INF) {
1888     // x is finite, so if y is positive infinity, then x is less, return 0
1889     //                 if y is negative infinity, then x is greater, return 1
1890     {
1891       res = ((y & MASK_SIGN) == MASK_SIGN);
1892       BID_RETURN (res);
1893     }
1894   }
1895   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1896   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1897     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
1898     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1899     if (sig_x > 9999999999999999ull) {
1900       non_canon_x = 1;
1901     } else {
1902       non_canon_x = 0;
1903     }
1904   } else {
1905     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
1906     sig_x = (x & MASK_BINARY_SIG1);
1907     non_canon_x = 0;
1908   }
1909
1910   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
1911   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
1912     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
1913     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
1914     if (sig_y > 9999999999999999ull) {
1915       non_canon_y = 1;
1916     } else {
1917       non_canon_y = 0;
1918     }
1919   } else {
1920     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
1921     sig_y = (y & MASK_BINARY_SIG1);
1922     non_canon_y = 0;
1923   }
1924
1925   // ZERO (CASE4)
1926   // some properties:
1927   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
1928   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
1929   //      therefore ignore the exponent field
1930   //    (Any non-canonical # is considered 0)
1931   if (non_canon_x || sig_x == 0) {
1932     x_is_zero = 1;
1933   }
1934   if (non_canon_y || sig_y == 0) {
1935     y_is_zero = 1;
1936   }
1937   // if both numbers are zero, neither is greater => return NOTGREATERTHAN
1938   if (x_is_zero && y_is_zero) {
1939     res = 0;
1940     BID_RETURN (res);
1941   }
1942   // is x is zero, it is greater if Y is negative
1943   else if (x_is_zero) {
1944     res = ((y & MASK_SIGN) == MASK_SIGN);
1945     BID_RETURN (res);
1946   }
1947   // is y is zero, X is greater if it is positive
1948   else if (y_is_zero) {
1949     res = ((x & MASK_SIGN) != MASK_SIGN);
1950     BID_RETURN (res);
1951   }
1952   // OPPOSITE SIGN (CASE5)
1953   // now, if the sign bits differ, x is greater if y is negative
1954   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
1955     res = ((y & MASK_SIGN) == MASK_SIGN);
1956     BID_RETURN (res);
1957   }
1958   // REDUNDANT REPRESENTATIONS (CASE6)
1959
1960   // if both components are either bigger or smaller
1961   if (sig_x > sig_y && exp_x >= exp_y) {
1962     res = ((x & MASK_SIGN) != MASK_SIGN);
1963     BID_RETURN (res);
1964   }
1965   if (sig_x < sig_y && exp_x <= exp_y) {
1966     res = ((x & MASK_SIGN) == MASK_SIGN);
1967     BID_RETURN (res);
1968   }
1969   // if exp_x is 15 greater than exp_y, no need for compensation
1970   if (exp_x - exp_y > 15) {
1971     res = ((x & MASK_SIGN) != MASK_SIGN);
1972     BID_RETURN (res);
1973   }
1974   // difference cannot be greater than 10^15
1975
1976   // if exp_x is 15 less than exp_y, no need for compensation
1977   if (exp_y - exp_x > 15) {
1978     res = ((x & MASK_SIGN) == MASK_SIGN);
1979     BID_RETURN (res);
1980   }
1981   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
1982   if (exp_x > exp_y) { // to simplify the loop below,
1983
1984     // otherwise adjust the x significand upwards
1985     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
1986                             mult_factor[exp_x - exp_y]);
1987
1988
1989     // if postitive, return whichever significand is larger 
1990     //     (converse if negative)
1991     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
1992       res = 0;
1993       BID_RETURN (res);
1994     }
1995
1996     {
1997       res = (((sig_n_prime.w[1] > 0)
1998               || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
1999                                               MASK_SIGN));
2000       BID_RETURN (res);
2001     }
2002   }
2003   // adjust the y significand upwards
2004   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
2005                           mult_factor[exp_y - exp_x]);
2006
2007   // if postitive, return whichever significand is larger 
2008   //     (converse if negative)
2009   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
2010     res = 0;
2011     BID_RETURN (res);
2012   }
2013   {
2014     res = (((sig_n_prime.w[1] == 0)
2015             && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
2016                                               MASK_SIGN));
2017     BID_RETURN (res);
2018   }
2019 }
2020
2021 #if DECIMAL_CALL_BY_REFERENCE
2022 void
2023 __bid64_signaling_greater_equal (int *pres, UINT64 * px,
2024                                UINT64 *
2025                                py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2026                                _EXC_INFO_PARAM) {
2027   UINT64 x = *px;
2028   UINT64 y = *py;
2029 #else
2030 int
2031 __bid64_signaling_greater_equal (UINT64 x,
2032                                UINT64 y _EXC_FLAGS_PARAM
2033                                _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
2034 #endif
2035   int res;
2036   int exp_x, exp_y;
2037   UINT64 sig_x, sig_y;
2038   UINT128 sig_n_prime;
2039   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
2040
2041   // NaN (CASE1)
2042   // if either number is NAN, the comparison is unordered : return 1
2043   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
2044     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
2045     res = 0;
2046     BID_RETURN (res);
2047   }
2048   // SIMPLE (CASE2)
2049   // if all the bits are the same, these numbers are equal.
2050   if (x == y) {
2051     res = 1;
2052     BID_RETURN (res);
2053   }
2054   // INFINITY (CASE3)
2055   if ((x & MASK_INF) == MASK_INF) {
2056     // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
2057     if ((x & MASK_SIGN) == MASK_SIGN)
2058       // x is -inf, so it is less than y unless y is -inf
2059     {
2060       res = (((y & MASK_INF) == MASK_INF)
2061              && (y & MASK_SIGN) == MASK_SIGN);
2062       BID_RETURN (res);
2063     } else
2064       // x is pos_inf, no way for it to be less than y
2065     {
2066       res = 1;
2067       BID_RETURN (res);
2068     }
2069   } else if ((y & MASK_INF) == MASK_INF) {
2070     // x is finite, so:
2071     //    if y is +inf, x<y
2072     //    if y is -inf, x>y
2073     {
2074       res = ((y & MASK_SIGN) == MASK_SIGN);
2075       BID_RETURN (res);
2076     }
2077   }
2078   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2079   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2080     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
2081     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2082     if (sig_x > 9999999999999999ull) {
2083       non_canon_x = 1;
2084     } else {
2085       non_canon_x = 0;
2086     }
2087   } else {
2088     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
2089     sig_x = (x & MASK_BINARY_SIG1);
2090     non_canon_x = 0;
2091   }
2092
2093   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2094   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2095     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
2096     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2097     if (sig_y > 9999999999999999ull) {
2098       non_canon_y = 1;
2099     } else {
2100       non_canon_y = 0;
2101     }
2102   } else {
2103     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
2104     sig_y = (y & MASK_BINARY_SIG1);
2105     non_canon_y = 0;
2106   }
2107
2108   // ZERO (CASE4)
2109   // some properties:
2110   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
2111   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
2112   //      therefore ignore the exponent field
2113   //    (Any non-canonical # is considered 0)
2114   if (non_canon_x || sig_x == 0) {
2115     x_is_zero = 1;
2116   }
2117   if (non_canon_y || sig_y == 0) {
2118     y_is_zero = 1;
2119   }
2120   // if both numbers are zero, they are equal
2121   if (x_is_zero && y_is_zero) {
2122     res = 1;
2123     BID_RETURN (res);
2124   }
2125   // if x is zero, it is lessthan if Y is positive
2126   else if (x_is_zero) {
2127     res = ((y & MASK_SIGN) == MASK_SIGN);
2128     BID_RETURN (res);
2129   }
2130   // if y is zero, X is less if it is negative
2131   else if (y_is_zero) {
2132     res = ((x & MASK_SIGN) != MASK_SIGN);
2133     BID_RETURN (res);
2134   }
2135   // OPPOSITE SIGN (CASE5)
2136   // now, if the sign bits differ, x is less than if y is positive
2137   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
2138     res = ((y & MASK_SIGN) == MASK_SIGN);
2139     BID_RETURN (res);
2140   }
2141   // REDUNDANT REPRESENTATIONS (CASE6)
2142   // if both components are either bigger or smaller
2143   if (sig_x > sig_y && exp_x >= exp_y) {
2144     res = ((x & MASK_SIGN) != MASK_SIGN);
2145     BID_RETURN (res);
2146   }
2147   if (sig_x < sig_y && exp_x <= exp_y) {
2148     res = ((x & MASK_SIGN) == MASK_SIGN);
2149     BID_RETURN (res);
2150   }
2151   // if exp_x is 15 greater than exp_y, no need for compensation
2152   if (exp_x - exp_y > 15) {
2153     res = ((x & MASK_SIGN) != MASK_SIGN);
2154     BID_RETURN (res);
2155   }
2156   // difference cannot be greater than 10^15
2157
2158   // if exp_x is 15 less than exp_y, no need for compensation
2159   if (exp_y - exp_x > 15) {
2160     res = ((x & MASK_SIGN) == MASK_SIGN);
2161     BID_RETURN (res);
2162   }
2163   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
2164   if (exp_x > exp_y) { // to simplify the loop below,
2165
2166     // otherwise adjust the x significand upwards
2167     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
2168                             mult_factor[exp_x - exp_y]);
2169
2170     // return 1 if values are equal
2171     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
2172       res = 1;
2173       BID_RETURN (res);
2174     }
2175     // if postitive, return whichever significand abs is smaller 
2176     //     (converse if negative)
2177     {
2178       res = (((sig_n_prime.w[1] == 0)
2179               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
2180                                               MASK_SIGN));
2181       BID_RETURN (res);
2182     }
2183   }
2184   // adjust the y significand upwards
2185   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
2186                           mult_factor[exp_y - exp_x]);
2187
2188   // return 0 if values are equal
2189   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
2190     res = 1;
2191     BID_RETURN (res);
2192   }
2193   // if positive, return whichever significand abs is smaller 
2194   //     (converse if negative)
2195   {
2196     res = (((sig_n_prime.w[1] > 0)
2197             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
2198                                               MASK_SIGN));
2199     BID_RETURN (res);
2200   }
2201 }
2202
2203 #if DECIMAL_CALL_BY_REFERENCE
2204 void
2205 __bid64_signaling_greater_unordered (int *pres, UINT64 * px,
2206                                    UINT64 *
2207                                    py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2208                                    _EXC_INFO_PARAM) {
2209   UINT64 x = *px;
2210   UINT64 y = *py;
2211 #else
2212 int
2213 __bid64_signaling_greater_unordered (UINT64 x,
2214                                    UINT64 y _EXC_FLAGS_PARAM
2215                                    _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
2216 #endif
2217   int res;
2218   int exp_x, exp_y;
2219   UINT64 sig_x, sig_y;
2220   UINT128 sig_n_prime;
2221   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
2222
2223   // NaN (CASE1)
2224   // if either number is NAN, the comparison is unordered, 
2225   // rather than equal : return 0
2226   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
2227     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
2228     res = 1;
2229     BID_RETURN (res);
2230   }
2231   // SIMPLE (CASE2)
2232   // if all the bits are the same, these numbers are equal (not Greater).
2233   if (x == y) {
2234     res = 0;
2235     BID_RETURN (res);
2236   }
2237   // INFINITY (CASE3)
2238   if ((x & MASK_INF) == MASK_INF) {
2239     // if x is neg infinity, there is no way it is greater than y, return 0
2240     if (((x & MASK_SIGN) == MASK_SIGN)) {
2241       res = 0;
2242       BID_RETURN (res);
2243     }
2244     // x is pos infinity, it is greater, 
2245     // unless y is positive infinity => return y!=pos_infinity
2246     else {
2247       res = (((y & MASK_INF) != MASK_INF)
2248              || ((y & MASK_SIGN) == MASK_SIGN));
2249       BID_RETURN (res);
2250     }
2251   } else if ((y & MASK_INF) == MASK_INF) {
2252     // x is finite, so if y is positive infinity, then x is less, return 0
2253     //                 if y is negative infinity, then x is greater, return 1
2254     {
2255       res = ((y & MASK_SIGN) == MASK_SIGN);
2256       BID_RETURN (res);
2257     }
2258   }
2259   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2260   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2261     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
2262     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2263     if (sig_x > 9999999999999999ull) {
2264       non_canon_x = 1;
2265     } else {
2266       non_canon_x = 0;
2267     }
2268   } else {
2269     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
2270     sig_x = (x & MASK_BINARY_SIG1);
2271     non_canon_x = 0;
2272   }
2273
2274   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2275   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2276     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
2277     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2278     if (sig_y > 9999999999999999ull) {
2279       non_canon_y = 1;
2280     } else {
2281       non_canon_y = 0;
2282     }
2283   } else {
2284     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
2285     sig_y = (y & MASK_BINARY_SIG1);
2286     non_canon_y = 0;
2287   }
2288
2289   // ZERO (CASE4)
2290   // some properties:
2291   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
2292   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
2293   //      therefore ignore the exponent field
2294   //    (Any non-canonical # is considered 0)
2295   if (non_canon_x || sig_x == 0) {
2296     x_is_zero = 1;
2297   }
2298   if (non_canon_y || sig_y == 0) {
2299     y_is_zero = 1;
2300   }
2301   // if both numbers are zero, neither is greater => return NOTGREATERTHAN
2302   if (x_is_zero && y_is_zero) {
2303     res = 0;
2304     BID_RETURN (res);
2305   }
2306   // is x is zero, it is greater if Y is negative
2307   else if (x_is_zero) {
2308     res = ((y & MASK_SIGN) == MASK_SIGN);
2309     BID_RETURN (res);
2310   }
2311   // is y is zero, X is greater if it is positive
2312   else if (y_is_zero) {
2313     res = ((x & MASK_SIGN) != MASK_SIGN);
2314     BID_RETURN (res);
2315   }
2316   // OPPOSITE SIGN (CASE5)
2317   // now, if the sign bits differ, x is greater if y is negative
2318   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
2319     res = ((y & MASK_SIGN) == MASK_SIGN);
2320     BID_RETURN (res);
2321   }
2322   // REDUNDANT REPRESENTATIONS (CASE6)
2323
2324   // if both components are either bigger or smaller
2325   if (sig_x > sig_y && exp_x >= exp_y) {
2326     res = ((x & MASK_SIGN) != MASK_SIGN);
2327     BID_RETURN (res);
2328   }
2329   if (sig_x < sig_y && exp_x <= exp_y) {
2330     res = ((x & MASK_SIGN) == MASK_SIGN);
2331     BID_RETURN (res);
2332   }
2333   // if exp_x is 15 greater than exp_y, no need for compensation
2334   if (exp_x - exp_y > 15) {
2335     res = ((x & MASK_SIGN) != MASK_SIGN);
2336     BID_RETURN (res);
2337   }
2338   // difference cannot be greater than 10^15
2339
2340   // if exp_x is 15 less than exp_y, no need for compensation
2341   if (exp_y - exp_x > 15) {
2342     res = ((x & MASK_SIGN) == MASK_SIGN);
2343     BID_RETURN (res);
2344   }
2345   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
2346   if (exp_x > exp_y) { // to simplify the loop below,
2347
2348     // otherwise adjust the x significand upwards
2349     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
2350                             mult_factor[exp_x - exp_y]);
2351
2352     // if postitive, return whichever significand is larger 
2353     //     (converse if negative)
2354     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
2355       res = 0;
2356       BID_RETURN (res);
2357     }
2358
2359     {
2360       res = (((sig_n_prime.w[1] > 0)
2361               || sig_n_prime.w[0] > sig_y) ^ ((x & MASK_SIGN) ==
2362                                               MASK_SIGN));
2363       BID_RETURN (res);
2364     }
2365   }
2366   // adjust the y significand upwards
2367   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
2368                           mult_factor[exp_y - exp_x]);
2369
2370   // if postitive, return whichever significand is larger 
2371   //     (converse if negative)
2372   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
2373     res = 0;
2374     BID_RETURN (res);
2375   }
2376   {
2377     res = (((sig_n_prime.w[1] == 0)
2378             && (sig_x > sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
2379                                               MASK_SIGN));
2380     BID_RETURN (res);
2381   }
2382 }
2383
2384 #if DECIMAL_CALL_BY_REFERENCE
2385 void
2386 __bid64_signaling_less (int *pres, UINT64 * px,
2387                       UINT64 *
2388                       py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2389                       _EXC_INFO_PARAM) {
2390   UINT64 x = *px;
2391   UINT64 y = *py;
2392 #else
2393 int
2394 __bid64_signaling_less (UINT64 x,
2395                       UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2396                       _EXC_INFO_PARAM) {
2397 #endif
2398   int res;
2399   int exp_x, exp_y;
2400   UINT64 sig_x, sig_y;
2401   UINT128 sig_n_prime;
2402   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
2403
2404   // NaN (CASE1)
2405   // if either number is NAN, the comparison is unordered : return 0
2406   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
2407     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
2408     res = 0;
2409     BID_RETURN (res);
2410   }
2411   // SIMPLE (CASE2)
2412   // if all the bits are the same, these numbers are equal.
2413   if (x == y) {
2414     res = 0;
2415     BID_RETURN (res);
2416   }
2417   // INFINITY (CASE3)
2418   if ((x & MASK_INF) == MASK_INF) {
2419     // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
2420     if ((x & MASK_SIGN) == MASK_SIGN)
2421       // x is -inf, so it is less than y unless y is -inf
2422     {
2423       res = (((y & MASK_INF) != MASK_INF)
2424              || (y & MASK_SIGN) != MASK_SIGN);
2425       BID_RETURN (res);
2426     } else
2427       // x is pos_inf, no way for it to be less than y
2428     {
2429       res = 0;
2430       BID_RETURN (res);
2431     }
2432   } else if ((y & MASK_INF) == MASK_INF) {
2433     // x is finite, so:
2434     //    if y is +inf, x<y
2435     //    if y is -inf, x>y
2436     {
2437       res = ((y & MASK_SIGN) != MASK_SIGN);
2438       BID_RETURN (res);
2439     }
2440   }
2441   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2442   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2443     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
2444     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2445     if (sig_x > 9999999999999999ull) {
2446       non_canon_x = 1;
2447     } else {
2448       non_canon_x = 0;
2449     }
2450   } else {
2451     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
2452     sig_x = (x & MASK_BINARY_SIG1);
2453     non_canon_x = 0;
2454   }
2455
2456   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2457   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2458     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
2459     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2460     if (sig_y > 9999999999999999ull) {
2461       non_canon_y = 1;
2462     } else {
2463       non_canon_y = 0;
2464     }
2465   } else {
2466     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
2467     sig_y = (y & MASK_BINARY_SIG1);
2468     non_canon_y = 0;
2469   }
2470
2471   // ZERO (CASE4)
2472   // some properties:
2473   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
2474   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
2475   //      therefore ignore the exponent field
2476   //    (Any non-canonical # is considered 0)
2477   if (non_canon_x || sig_x == 0) {
2478     x_is_zero = 1;
2479   }
2480   if (non_canon_y || sig_y == 0) {
2481     y_is_zero = 1;
2482   }
2483   // if both numbers are zero, they are equal
2484   if (x_is_zero && y_is_zero) {
2485     res = 0;
2486     BID_RETURN (res);
2487   }
2488   // if x is zero, it is lessthan if Y is positive
2489   else if (x_is_zero) {
2490     res = ((y & MASK_SIGN) != MASK_SIGN);
2491     BID_RETURN (res);
2492   }
2493   // if y is zero, X is less if it is negative
2494   else if (y_is_zero) {
2495     res = ((x & MASK_SIGN) == MASK_SIGN);
2496     BID_RETURN (res);
2497   }
2498   // OPPOSITE SIGN (CASE5)
2499   // now, if the sign bits differ, x is less than if y is positive
2500   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
2501     res = ((y & MASK_SIGN) != MASK_SIGN);
2502     BID_RETURN (res);
2503   }
2504   // REDUNDANT REPRESENTATIONS (CASE6)
2505   // if both components are either bigger or smaller
2506   if (sig_x > sig_y && exp_x >= exp_y) {
2507     res = ((x & MASK_SIGN) == MASK_SIGN);
2508     BID_RETURN (res);
2509   }
2510   if (sig_x < sig_y && exp_x <= exp_y) {
2511     res = ((x & MASK_SIGN) != MASK_SIGN);
2512     BID_RETURN (res);
2513   }
2514   // if exp_x is 15 greater than exp_y, no need for compensation
2515   if (exp_x - exp_y > 15) {
2516     res = ((x & MASK_SIGN) == MASK_SIGN);
2517     BID_RETURN (res);
2518   }
2519   // difference cannot be greater than 10^15
2520
2521   // if exp_x is 15 less than exp_y, no need for compensation
2522   if (exp_y - exp_x > 15) {
2523     res = ((x & MASK_SIGN) != MASK_SIGN);
2524     BID_RETURN (res);
2525   }
2526   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
2527   if (exp_x > exp_y) { // to simplify the loop below,
2528
2529     // otherwise adjust the x significand upwards
2530     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
2531                             mult_factor[exp_x - exp_y]);
2532
2533     // return 0 if values are equal
2534     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
2535       res = 0;
2536       BID_RETURN (res);
2537     }
2538     // if postitive, return whichever significand abs is smaller 
2539     //     (converse if negative)
2540     {
2541       res = (((sig_n_prime.w[1] == 0)
2542               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
2543                                               MASK_SIGN));
2544       BID_RETURN (res);
2545     }
2546   }
2547   // adjust the y significand upwards
2548   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
2549                           mult_factor[exp_y - exp_x]);
2550
2551   // return 0 if values are equal
2552   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
2553     res = 0;
2554     BID_RETURN (res);
2555   }
2556   // if positive, return whichever significand abs is smaller 
2557   //     (converse if negative)
2558   {
2559     res = (((sig_n_prime.w[1] > 0)
2560             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
2561                                               MASK_SIGN));
2562     BID_RETURN (res);
2563   }
2564 }
2565
2566 #if DECIMAL_CALL_BY_REFERENCE
2567 void
2568 __bid64_signaling_less_equal (int *pres, UINT64 * px,
2569                             UINT64 *
2570                             py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2571                             _EXC_INFO_PARAM) {
2572   UINT64 x = *px;
2573   UINT64 y = *py;
2574 #else
2575 int
2576 __bid64_signaling_less_equal (UINT64 x,
2577                             UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2578                             _EXC_INFO_PARAM) {
2579 #endif
2580   int res;
2581   int exp_x, exp_y;
2582   UINT64 sig_x, sig_y;
2583   UINT128 sig_n_prime;
2584   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
2585
2586   // NaN (CASE1)
2587   // if either number is NAN, the comparison is unordered, 
2588   // rather than equal : return 0
2589   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
2590     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
2591     res = 0;
2592     BID_RETURN (res);
2593   }
2594   // SIMPLE (CASE2)
2595   // if all the bits are the same, these numbers are equal (LESSEQUAL).
2596   if (x == y) {
2597     res = 1;
2598     BID_RETURN (res);
2599   }
2600   // INFINITY (CASE3)
2601   if ((x & MASK_INF) == MASK_INF) {
2602     // if x is neg infinity, it must be lessthan or equal to y return 1
2603     if (((x & MASK_SIGN) == MASK_SIGN)) {
2604       res = 1;
2605       BID_RETURN (res);
2606     }
2607     // x is pos infinity, it is greater, 
2608     // unless y is positive infinity => return y==pos_infinity
2609     else {
2610       res = !(((y & MASK_INF) != MASK_INF)
2611               || ((y & MASK_SIGN) == MASK_SIGN));
2612       BID_RETURN (res);
2613     }
2614   } else if ((y & MASK_INF) == MASK_INF) {
2615     // x is finite, so if y is positive infinity, then x is less, return 1
2616     //                 if y is negative infinity, then x is greater, return 0
2617     {
2618       res = ((y & MASK_SIGN) != MASK_SIGN);
2619       BID_RETURN (res);
2620     }
2621   }
2622   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2623   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2624     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
2625     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2626     if (sig_x > 9999999999999999ull) {
2627       non_canon_x = 1;
2628     } else {
2629       non_canon_x = 0;
2630     }
2631   } else {
2632     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
2633     sig_x = (x & MASK_BINARY_SIG1);
2634     non_canon_x = 0;
2635   }
2636
2637   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2638   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2639     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
2640     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2641     if (sig_y > 9999999999999999ull) {
2642       non_canon_y = 1;
2643     } else {
2644       non_canon_y = 0;
2645     }
2646   } else {
2647     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
2648     sig_y = (y & MASK_BINARY_SIG1);
2649     non_canon_y = 0;
2650   }
2651
2652   // ZERO (CASE4)
2653   // some properties:
2654   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
2655   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
2656   //      therefore ignore the exponent field
2657   //    (Any non-canonical # is considered 0)
2658   if (non_canon_x || sig_x == 0) {
2659     x_is_zero = 1;
2660   }
2661   if (non_canon_y || sig_y == 0) {
2662     y_is_zero = 1;
2663   }
2664   // if both numbers are zero, they are equal -> return 1
2665   if (x_is_zero && y_is_zero) {
2666     res = 1;
2667     BID_RETURN (res);
2668   }
2669   // if x is zero, it is lessthan if Y is positive
2670   else if (x_is_zero) {
2671     res = ((y & MASK_SIGN) != MASK_SIGN);
2672     BID_RETURN (res);
2673   }
2674   // if y is zero, X is less if it is negative
2675   else if (y_is_zero) {
2676     res = ((x & MASK_SIGN) == MASK_SIGN);
2677     BID_RETURN (res);
2678   }
2679   // OPPOSITE SIGN (CASE5)
2680   // now, if the sign bits differ, x is less than if y is positive
2681   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
2682     res = ((y & MASK_SIGN) != MASK_SIGN);
2683     BID_RETURN (res);
2684   }
2685   // REDUNDANT REPRESENTATIONS (CASE6)
2686   // if both components are either bigger or smaller
2687   if (sig_x > sig_y && exp_x >= exp_y) {
2688     res = ((x & MASK_SIGN) == MASK_SIGN);
2689     BID_RETURN (res);
2690   }
2691   if (sig_x < sig_y && exp_x <= exp_y) {
2692     res = ((x & MASK_SIGN) != MASK_SIGN);
2693     BID_RETURN (res);
2694   }
2695   // if exp_x is 15 greater than exp_y, no need for compensation
2696   if (exp_x - exp_y > 15) {
2697     res = ((x & MASK_SIGN) == MASK_SIGN);
2698     BID_RETURN (res);
2699   }
2700   // difference cannot be greater than 10^15
2701
2702   // if exp_x is 15 less than exp_y, no need for compensation
2703   if (exp_y - exp_x > 15) {
2704     res = ((x & MASK_SIGN) != MASK_SIGN);
2705     BID_RETURN (res);
2706   }
2707   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
2708   if (exp_x > exp_y) { // to simplify the loop below,
2709
2710     // otherwise adjust the x significand upwards
2711     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
2712                             mult_factor[exp_x - exp_y]);
2713
2714     // return 1 if values are equal
2715     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
2716       res = 1;
2717       BID_RETURN (res);
2718     }
2719     // if postitive, return whichever significand abs is smaller 
2720     //     (converse if negative)
2721     {
2722       res = (((sig_n_prime.w[1] == 0)
2723               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
2724                                               MASK_SIGN));
2725       BID_RETURN (res);
2726     }
2727   }
2728   // adjust the y significand upwards
2729   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
2730                           mult_factor[exp_y - exp_x]);
2731
2732   // return 1 if values are equal
2733   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
2734     res = 1;
2735     BID_RETURN (res);
2736   }
2737   // if positive, return whichever significand abs is smaller 
2738   //     (converse if negative)
2739   {
2740     res = (((sig_n_prime.w[1] > 0)
2741             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
2742                                               MASK_SIGN));
2743     BID_RETURN (res);
2744   }
2745 }
2746
2747 #if DECIMAL_CALL_BY_REFERENCE
2748 void
2749 __bid64_signaling_less_unordered (int *pres, UINT64 * px,
2750                                 UINT64 *
2751                                 py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2752                                 _EXC_INFO_PARAM) {
2753   UINT64 x = *px;
2754   UINT64 y = *py;
2755 #else
2756 int
2757 __bid64_signaling_less_unordered (UINT64 x,
2758                                 UINT64 y _EXC_FLAGS_PARAM
2759                                 _EXC_MASKS_PARAM _EXC_INFO_PARAM) {
2760 #endif
2761   int res;
2762   int exp_x, exp_y;
2763   UINT64 sig_x, sig_y;
2764   UINT128 sig_n_prime;
2765   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
2766
2767   // NaN (CASE1)
2768   // if either number is NAN, the comparison is unordered : return 0
2769   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
2770     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
2771     res = 1;
2772     BID_RETURN (res);
2773   }
2774   // SIMPLE (CASE2)
2775   // if all the bits are the same, these numbers are equal.
2776   if (x == y) {
2777     res = 0;
2778     BID_RETURN (res);
2779   }
2780   // INFINITY (CASE3)
2781   if ((x & MASK_INF) == MASK_INF) {
2782     // if x==neg_inf, { res = (y == neg_inf)?0:1; BID_RETURN (res) }
2783     if ((x & MASK_SIGN) == MASK_SIGN)
2784       // x is -inf, so it is less than y unless y is -inf
2785     {
2786       res = (((y & MASK_INF) != MASK_INF)
2787              || (y & MASK_SIGN) != MASK_SIGN);
2788       BID_RETURN (res);
2789     } else
2790       // x is pos_inf, no way for it to be less than y
2791     {
2792       res = 0;
2793       BID_RETURN (res);
2794     }
2795   } else if ((y & MASK_INF) == MASK_INF) {
2796     // x is finite, so:
2797     //    if y is +inf, x<y
2798     //    if y is -inf, x>y
2799     {
2800       res = ((y & MASK_SIGN) != MASK_SIGN);
2801       BID_RETURN (res);
2802     }
2803   }
2804   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2805   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2806     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
2807     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2808     if (sig_x > 9999999999999999ull) {
2809       non_canon_x = 1;
2810     } else {
2811       non_canon_x = 0;
2812     }
2813   } else {
2814     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
2815     sig_x = (x & MASK_BINARY_SIG1);
2816     non_canon_x = 0;
2817   }
2818
2819   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2820   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2821     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
2822     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2823     if (sig_y > 9999999999999999ull) {
2824       non_canon_y = 1;
2825     } else {
2826       non_canon_y = 0;
2827     }
2828   } else {
2829     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
2830     sig_y = (y & MASK_BINARY_SIG1);
2831     non_canon_y = 0;
2832   }
2833
2834   // ZERO (CASE4)
2835   // some properties:
2836   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
2837   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
2838   //      therefore ignore the exponent field
2839   //    (Any non-canonical # is considered 0)
2840   if (non_canon_x || sig_x == 0) {
2841     x_is_zero = 1;
2842   }
2843   if (non_canon_y || sig_y == 0) {
2844     y_is_zero = 1;
2845   }
2846   // if both numbers are zero, they are equal
2847   if (x_is_zero && y_is_zero) {
2848     res = 0;
2849     BID_RETURN (res);
2850   }
2851   // if x is zero, it is lessthan if Y is positive
2852   else if (x_is_zero) {
2853     res = ((y & MASK_SIGN) != MASK_SIGN);
2854     BID_RETURN (res);
2855   }
2856   // if y is zero, X is less if it is negative
2857   else if (y_is_zero) {
2858     res = ((x & MASK_SIGN) == MASK_SIGN);
2859     BID_RETURN (res);
2860   }
2861   // OPPOSITE SIGN (CASE5)
2862   // now, if the sign bits differ, x is less than if y is positive
2863   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
2864     res = ((y & MASK_SIGN) != MASK_SIGN);
2865     BID_RETURN (res);
2866   }
2867   // REDUNDANT REPRESENTATIONS (CASE6)
2868   // if both components are either bigger or smaller
2869   if (sig_x > sig_y && exp_x >= exp_y) {
2870     res = ((x & MASK_SIGN) == MASK_SIGN);
2871     BID_RETURN (res);
2872   }
2873   if (sig_x < sig_y && exp_x <= exp_y) {
2874     res = ((x & MASK_SIGN) != MASK_SIGN);
2875     BID_RETURN (res);
2876   }
2877   // if exp_x is 15 greater than exp_y, no need for compensation
2878   if (exp_x - exp_y > 15) {
2879     res = ((x & MASK_SIGN) == MASK_SIGN);
2880     BID_RETURN (res);
2881   }
2882   // difference cannot be greater than 10^15
2883
2884   // if exp_x is 15 less than exp_y, no need for compensation
2885   if (exp_y - exp_x > 15) {
2886     res = ((x & MASK_SIGN) != MASK_SIGN);
2887     BID_RETURN (res);
2888   }
2889   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
2890   if (exp_x > exp_y) { // to simplify the loop below,
2891
2892     // otherwise adjust the x significand upwards
2893     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
2894                             mult_factor[exp_x - exp_y]);
2895
2896     // return 0 if values are equal
2897     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
2898       res = 0;
2899       BID_RETURN (res);
2900     }
2901     // if postitive, return whichever significand abs is smaller 
2902     //     (converse if negative)
2903     {
2904       res = (((sig_n_prime.w[1] == 0)
2905               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
2906                                               MASK_SIGN));
2907       BID_RETURN (res);
2908     }
2909   }
2910   // adjust the y significand upwards
2911   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
2912                           mult_factor[exp_y - exp_x]);
2913
2914   // return 0 if values are equal
2915   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
2916     res = 0;
2917     BID_RETURN (res);
2918   }
2919   // if positive, return whichever significand abs is smaller 
2920   //     (converse if negative)
2921   {
2922     res = (((sig_n_prime.w[1] > 0)
2923             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
2924                                               MASK_SIGN));
2925     BID_RETURN (res);
2926   }
2927 }
2928
2929 #if DECIMAL_CALL_BY_REFERENCE
2930 void
2931 __bid64_signaling_not_equal (int *pres, UINT64 * px,
2932                            UINT64 *
2933                            py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2934                            _EXC_INFO_PARAM) {
2935   UINT64 x = *px;
2936   UINT64 y = *py;
2937 #else
2938 int
2939 __bid64_signaling_not_equal (UINT64 x,
2940                            UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
2941                            _EXC_INFO_PARAM) {
2942 #endif
2943   int res;
2944   int exp_x, exp_y, exp_t;
2945   UINT64 sig_x, sig_y, sig_t;
2946   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y, lcv;
2947
2948   // NaN (CASE1)
2949   // if either number is NAN, the comparison is unordered, 
2950   // rather than equal : return 1
2951   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
2952     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
2953     res = 1;
2954     BID_RETURN (res);
2955   }
2956   // SIMPLE (CASE2)
2957   // if all the bits are the same, these numbers are equivalent.
2958   if (x == y) {
2959     res = 0;
2960     BID_RETURN (res);
2961   }
2962   // INFINITY (CASE3)
2963   if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
2964     res = (((x ^ y) & MASK_SIGN) == MASK_SIGN);
2965     BID_RETURN (res);
2966   }
2967   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2968   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2969     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
2970     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2971     if (sig_x > 9999999999999999ull) {
2972       non_canon_x = 1;
2973     } else {
2974       non_canon_x = 0;
2975     }
2976   } else {
2977     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
2978     sig_x = (x & MASK_BINARY_SIG1);
2979     non_canon_x = 0;
2980   }
2981
2982   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
2983   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
2984     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
2985     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
2986     if (sig_y > 9999999999999999ull) {
2987       non_canon_y = 1;
2988     } else {
2989       non_canon_y = 0;
2990     }
2991   } else {
2992     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
2993     sig_y = (y & MASK_BINARY_SIG1);
2994     non_canon_y = 0;
2995   }
2996
2997   // ZERO (CASE4)
2998   // some properties:
2999   // (+ZERO==-ZERO) => therefore ignore the sign
3000   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
3001   //      therefore ignore the exponent field
3002   //    (Any non-canonical # is considered 0)
3003   if (non_canon_x || sig_x == 0) {
3004     x_is_zero = 1;
3005   }
3006   if (non_canon_y || sig_y == 0) {
3007     y_is_zero = 1;
3008   }
3009
3010   if (x_is_zero && y_is_zero) {
3011     res = 0;
3012     BID_RETURN (res);
3013   } else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
3014     res = 1;
3015     BID_RETURN (res);
3016   }
3017   // OPPOSITE SIGN (CASE5)
3018   // now, if the sign bits differ => not equal : return 1
3019   if ((x ^ y) & MASK_SIGN) {
3020     res = 1;
3021     BID_RETURN (res);
3022   }
3023   // REDUNDANT REPRESENTATIONS (CASE6)
3024   if (exp_x > exp_y) { // to simplify the loop below,
3025     SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
3026     SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
3027   }
3028
3029   if (exp_y - exp_x > 15) {
3030     res = 1;
3031     BID_RETURN (res);
3032   }
3033   // difference cannot be greater than 10^16
3034
3035   for (lcv = 0; lcv < (exp_y - exp_x); lcv++) {
3036
3037     // recalculate y's significand upwards
3038     sig_y = sig_y * 10;
3039     if (sig_y > 9999999999999999ull) {
3040       res = 1;
3041       BID_RETURN (res);
3042     }
3043   }
3044
3045   {
3046     res = sig_y != sig_x;
3047     BID_RETURN (res);
3048   }
3049
3050 }
3051
3052 #if DECIMAL_CALL_BY_REFERENCE
3053 void
3054 __bid64_signaling_not_greater (int *pres, UINT64 * px,
3055                              UINT64 *
3056                              py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3057                              _EXC_INFO_PARAM) {
3058   UINT64 x = *px;
3059   UINT64 y = *py;
3060 #else
3061 int
3062 __bid64_signaling_not_greater (UINT64 x,
3063                              UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3064                              _EXC_INFO_PARAM) {
3065 #endif
3066   int res;
3067   int exp_x, exp_y;
3068   UINT64 sig_x, sig_y;
3069   UINT128 sig_n_prime;
3070   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
3071
3072   // NaN (CASE1)
3073   // if either number is NAN, the comparison is unordered, 
3074   // rather than equal : return 0
3075   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
3076     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
3077     res = 1;
3078     BID_RETURN (res);
3079   }
3080   // SIMPLE (CASE2)
3081   // if all the bits are the same, these numbers are equal (LESSEQUAL).
3082   if (x == y) {
3083     res = 1;
3084     BID_RETURN (res);
3085   }
3086   // INFINITY (CASE3)
3087   if ((x & MASK_INF) == MASK_INF) {
3088     // if x is neg infinity, it must be lessthan or equal to y return 1
3089     if (((x & MASK_SIGN) == MASK_SIGN)) {
3090       res = 1;
3091       BID_RETURN (res);
3092     }
3093     // x is pos infinity, it is greater, 
3094     // unless y is positive infinity => return y==pos_infinity
3095     else {
3096       res = !(((y & MASK_INF) != MASK_INF)
3097               || ((y & MASK_SIGN) == MASK_SIGN));
3098       BID_RETURN (res);
3099     }
3100   } else if ((y & MASK_INF) == MASK_INF) {
3101     // x is finite, so if y is positive infinity, then x is less, return 1
3102     //                 if y is negative infinity, then x is greater, return 0
3103     {
3104       res = ((y & MASK_SIGN) != MASK_SIGN);
3105       BID_RETURN (res);
3106     }
3107   }
3108   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
3109   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
3110     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
3111     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
3112     if (sig_x > 9999999999999999ull) {
3113       non_canon_x = 1;
3114     } else {
3115       non_canon_x = 0;
3116     }
3117   } else {
3118     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
3119     sig_x = (x & MASK_BINARY_SIG1);
3120     non_canon_x = 0;
3121   }
3122
3123   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
3124   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
3125     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
3126     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
3127     if (sig_y > 9999999999999999ull) {
3128       non_canon_y = 1;
3129     } else {
3130       non_canon_y = 0;
3131     }
3132   } else {
3133     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
3134     sig_y = (y & MASK_BINARY_SIG1);
3135     non_canon_y = 0;
3136   }
3137
3138   // ZERO (CASE4)
3139   // some properties:
3140   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
3141   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
3142   //      therefore ignore the exponent field
3143   //    (Any non-canonical # is considered 0)
3144   if (non_canon_x || sig_x == 0) {
3145     x_is_zero = 1;
3146   }
3147   if (non_canon_y || sig_y == 0) {
3148     y_is_zero = 1;
3149   }
3150   // if both numbers are zero, they are equal -> return 1
3151   if (x_is_zero && y_is_zero) {
3152     res = 1;
3153     BID_RETURN (res);
3154   }
3155   // if x is zero, it is lessthan if Y is positive
3156   else if (x_is_zero) {
3157     res = ((y & MASK_SIGN) != MASK_SIGN);
3158     BID_RETURN (res);
3159   }
3160   // if y is zero, X is less if it is negative
3161   else if (y_is_zero) {
3162     res = ((x & MASK_SIGN) == MASK_SIGN);
3163     BID_RETURN (res);
3164   }
3165   // OPPOSITE SIGN (CASE5)
3166   // now, if the sign bits differ, x is less than if y is positive
3167   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
3168     res = ((y & MASK_SIGN) != MASK_SIGN);
3169     BID_RETURN (res);
3170   }
3171   // REDUNDANT REPRESENTATIONS (CASE6)
3172   // if both components are either bigger or smaller
3173   if (sig_x > sig_y && exp_x >= exp_y) {
3174     res = ((x & MASK_SIGN) == MASK_SIGN);
3175     BID_RETURN (res);
3176   }
3177   if (sig_x < sig_y && exp_x <= exp_y) {
3178     res = ((x & MASK_SIGN) != MASK_SIGN);
3179     BID_RETURN (res);
3180   }
3181   // if exp_x is 15 greater than exp_y, no need for compensation
3182   if (exp_x - exp_y > 15) {
3183     res = ((x & MASK_SIGN) == MASK_SIGN);
3184     BID_RETURN (res);
3185   }
3186   // difference cannot be greater than 10^15
3187
3188   // if exp_x is 15 less than exp_y, no need for compensation
3189   if (exp_y - exp_x > 15) {
3190     res = ((x & MASK_SIGN) != MASK_SIGN);
3191     BID_RETURN (res);
3192   }
3193   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
3194   if (exp_x > exp_y) { // to simplify the loop below,
3195
3196     // otherwise adjust the x significand upwards
3197     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
3198                             mult_factor[exp_x - exp_y]);
3199
3200     // return 1 if values are equal
3201     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
3202       res = 1;
3203       BID_RETURN (res);
3204     }
3205     // if postitive, return whichever significand abs is smaller 
3206     //     (converse if negative)
3207     {
3208       res = (((sig_n_prime.w[1] == 0)
3209               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) ==
3210                                               MASK_SIGN));
3211       BID_RETURN (res);
3212     }
3213   }
3214   // adjust the y significand upwards
3215   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
3216                           mult_factor[exp_y - exp_x]);
3217
3218   // return 1 if values are equal
3219   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
3220     res = 1;
3221     BID_RETURN (res);
3222   }
3223   // if positive, return whichever significand abs is smaller 
3224   //     (converse if negative)
3225   {
3226     res = (((sig_n_prime.w[1] > 0)
3227             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) ==
3228                                               MASK_SIGN));
3229     BID_RETURN (res);
3230   }
3231 }
3232
3233 #if DECIMAL_CALL_BY_REFERENCE
3234 void
3235 __bid64_signaling_not_less (int *pres, UINT64 * px,
3236                           UINT64 *
3237                           py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3238                           _EXC_INFO_PARAM) {
3239   UINT64 x = *px;
3240   UINT64 y = *py;
3241 #else
3242 int
3243 __bid64_signaling_not_less (UINT64 x,
3244                           UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3245                           _EXC_INFO_PARAM) {
3246 #endif
3247   int res;
3248   int exp_x, exp_y;
3249   UINT64 sig_x, sig_y;
3250   UINT128 sig_n_prime;
3251   char x_is_zero = 0, y_is_zero = 0, non_canon_x, non_canon_y;
3252
3253   // NaN (CASE1)
3254   // if either number is NAN, the comparison is unordered : return 1
3255   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
3256     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
3257     res = 1;
3258     BID_RETURN (res);
3259   }
3260   // SIMPLE (CASE2)
3261   // if all the bits are the same, these numbers are equal.
3262   if (x == y) {
3263     res = 1;
3264     BID_RETURN (res);
3265   }
3266   // INFINITY (CASE3)
3267   if ((x & MASK_INF) == MASK_INF) {
3268     // if x==neg_inf, { res = (y == neg_inf)?1:0; BID_RETURN (res) }
3269     if ((x & MASK_SIGN) == MASK_SIGN)
3270       // x is -inf, so it is less than y unless y is -inf
3271     {
3272       res = (((y & MASK_INF) == MASK_INF)
3273              && (y & MASK_SIGN) == MASK_SIGN);
3274       BID_RETURN (res);
3275     } else
3276       // x is pos_inf, no way for it to be less than y
3277     {
3278       res = 1;
3279       BID_RETURN (res);
3280     }
3281   } else if ((y & MASK_INF) == MASK_INF) {
3282     // x is finite, so:
3283     //    if y is +inf, x<y
3284     //    if y is -inf, x>y
3285     {
3286       res = ((y & MASK_SIGN) == MASK_SIGN);
3287       BID_RETURN (res);
3288     }
3289   }
3290   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
3291   if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
3292     exp_x = (x & MASK_BINARY_EXPONENT2) >> 51;
3293     sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
3294     if (sig_x > 9999999999999999ull) {
3295       non_canon_x = 1;
3296     } else {
3297       non_canon_x = 0;
3298     }
3299   } else {
3300     exp_x = (x & MASK_BINARY_EXPONENT1) >> 53;
3301     sig_x = (x & MASK_BINARY_SIG1);
3302     non_canon_x = 0;
3303   }
3304
3305   // if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
3306   if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
3307     exp_y = (y & MASK_BINARY_EXPONENT2) >> 51;
3308     sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
3309     if (sig_y > 9999999999999999ull) {
3310       non_canon_y = 1;
3311     } else {
3312       non_canon_y = 0;
3313     }
3314   } else {
3315     exp_y = (y & MASK_BINARY_EXPONENT1) >> 53;
3316     sig_y = (y & MASK_BINARY_SIG1);
3317     non_canon_y = 0;
3318   }
3319
3320   // ZERO (CASE4)
3321   // some properties:
3322   // (+ZERO==-ZERO) => therefore ignore the sign, and neither number is greater
3323   //    (ZERO x 10^A == ZERO x 10^B) for any valid A, B => 
3324   //      therefore ignore the exponent field
3325   //    (Any non-canonical # is considered 0)
3326   if (non_canon_x || sig_x == 0) {
3327     x_is_zero = 1;
3328   }
3329   if (non_canon_y || sig_y == 0) {
3330     y_is_zero = 1;
3331   }
3332   // if both numbers are zero, they are equal
3333   if (x_is_zero && y_is_zero) {
3334     res = 1;
3335     BID_RETURN (res);
3336   }
3337   // if x is zero, it is lessthan if Y is positive
3338   else if (x_is_zero) {
3339     res = ((y & MASK_SIGN) == MASK_SIGN);
3340     BID_RETURN (res);
3341   }
3342   // if y is zero, X is less if it is negative
3343   else if (y_is_zero) {
3344     res = ((x & MASK_SIGN) != MASK_SIGN);
3345     BID_RETURN (res);
3346   }
3347   // OPPOSITE SIGN (CASE5)
3348   // now, if the sign bits differ, x is less than if y is positive
3349   if (((x ^ y) & MASK_SIGN) == MASK_SIGN) {
3350     res = ((y & MASK_SIGN) == MASK_SIGN);
3351     BID_RETURN (res);
3352   }
3353   // REDUNDANT REPRESENTATIONS (CASE6)
3354   // if both components are either bigger or smaller
3355   if (sig_x > sig_y && exp_x >= exp_y) {
3356     res = ((x & MASK_SIGN) != MASK_SIGN);
3357     BID_RETURN (res);
3358   }
3359   if (sig_x < sig_y && exp_x <= exp_y) {
3360     res = ((x & MASK_SIGN) == MASK_SIGN);
3361     BID_RETURN (res);
3362   }
3363   // if exp_x is 15 greater than exp_y, no need for compensation
3364   if (exp_x - exp_y > 15) {
3365     res = ((x & MASK_SIGN) != MASK_SIGN);
3366     BID_RETURN (res);
3367   }
3368   // difference cannot be greater than 10^15
3369
3370   // if exp_x is 15 less than exp_y, no need for compensation
3371   if (exp_y - exp_x > 15) {
3372     res = ((x & MASK_SIGN) == MASK_SIGN);
3373     BID_RETURN (res);
3374   }
3375   // if |exp_x - exp_y| < 15, it comes down to the compensated significand
3376   if (exp_x > exp_y) { // to simplify the loop below,
3377
3378     // otherwise adjust the x significand upwards
3379     __mul_64x64_to_128MACH (sig_n_prime, sig_x,
3380                             mult_factor[exp_x - exp_y]);
3381
3382     // return 0 if values are equal
3383     if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_y)) {
3384       res = 1;
3385       BID_RETURN (res);
3386     }
3387     // if postitive, return whichever significand abs is smaller 
3388     //     (converse if negative)
3389     {
3390       res = (((sig_n_prime.w[1] == 0)
3391               && sig_n_prime.w[0] < sig_y) ^ ((x & MASK_SIGN) !=
3392                                               MASK_SIGN));
3393       BID_RETURN (res);
3394     }
3395   }
3396   // adjust the y significand upwards
3397   __mul_64x64_to_128MACH (sig_n_prime, sig_y,
3398                           mult_factor[exp_y - exp_x]);
3399
3400   // return 0 if values are equal
3401   if (sig_n_prime.w[1] == 0 && (sig_n_prime.w[0] == sig_x)) {
3402     res = 1;
3403     BID_RETURN (res);
3404   }
3405   // if positive, return whichever significand abs is smaller 
3406   //     (converse if negative)
3407   {
3408     res = (((sig_n_prime.w[1] > 0)
3409             || (sig_x < sig_n_prime.w[0])) ^ ((x & MASK_SIGN) !=
3410                                               MASK_SIGN));
3411     BID_RETURN (res);
3412   }
3413 }
3414
3415 #if DECIMAL_CALL_BY_REFERENCE
3416 void
3417 __bid64_signaling_ordered (int *pres, UINT64 * px,
3418                          UINT64 *
3419                          py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3420                          _EXC_INFO_PARAM) {
3421   UINT64 x = *px;
3422   UINT64 y = *py;
3423 #else
3424 int
3425 __bid64_signaling_ordered (UINT64 x,
3426                          UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3427                          _EXC_INFO_PARAM) {
3428 #endif
3429   int res;
3430
3431   // NaN (CASE1)
3432   // if either number is NAN, the comparison is ordered, 
3433   // rather than equal : return 0
3434   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
3435     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
3436     res = 0;
3437     BID_RETURN (res);
3438   } else {
3439     res = 1;
3440     BID_RETURN (res);
3441   }
3442 }
3443
3444 #if DECIMAL_CALL_BY_REFERENCE
3445 void
3446 __bid64_signaling_unordered (int *pres, UINT64 * px,
3447                            UINT64 *
3448                            py _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3449                            _EXC_INFO_PARAM) {
3450   UINT64 x = *px;
3451   UINT64 y = *py;
3452 #else
3453 int
3454 __bid64_signaling_unordered (UINT64 x,
3455                            UINT64 y _EXC_FLAGS_PARAM _EXC_MASKS_PARAM
3456                            _EXC_INFO_PARAM) {
3457 #endif
3458   int res;
3459
3460   // NaN (CASE1)
3461   // if either number is NAN, the comparison is unordered, 
3462   // rather than equal : return 0
3463   if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
3464     *pfpsf |= INVALID_EXCEPTION; // set invalid exception if NaN
3465     res = 1;
3466     BID_RETURN (res);
3467   } else {
3468     res = 0;
3469     BID_RETURN (res);
3470   }
3471 }