1 /* mpz_cmp_d -- compare absolute values of mpz and double.
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
23 #include <float.h> /* for DBL_MAX */
30 #define RETURN_CMP(zl, dl) \
35 return (zlimb >= dlimb ? ret : -ret); \
38 #define RETURN_NONZERO(ptr, size, val) \
41 for (__i = (size)-1; __i >= 0; __i--) \
42 if ((ptr)[__i] != 0) \
49 mpz_cmp_d (mpz_srcptr z
, double d
)
51 mp_limb_t darray
[LIMBS_PER_DOUBLE
], zlimb
, dlimb
;
56 /* d=NaN is an invalid operation, there's no sensible return value.
57 d=Inf or -Inf is always bigger than z. */
58 DOUBLE_NAN_INF_ACTION (d
, __gmp_invalid_operation (), goto z_zero
);
60 /* 1. Either operand zero. */
67 return (d
< 0.0 ? 1 : -1);
70 /* 2. Opposite signs. */
74 return 1; /* >=0 cmp <0 */
80 return -1; /* <0 cmp >=0 */
86 /* 3. Small d, knowing abs(z) >= 1. */
90 dexp
= __gmp_extract_double (darray
, d
);
93 /* 4. Check for different high limb positions. */
95 return (zsize
>= dexp
? ret
: -ret
);
100 #if LIMBS_PER_DOUBLE == 2
101 RETURN_CMP (zp
[zsize
-1], darray
[1]);
103 return (darray
[0] != 0 ? -ret
: 0);
105 RETURN_CMP (zp
[zsize
-2], darray
[0]);
106 RETURN_NONZERO (zp
, zsize
-2, ret
);
109 #if LIMBS_PER_DOUBLE == 3
110 RETURN_CMP (zp
[zsize
-1], darray
[2]);
112 return ((darray
[0] | darray
[1]) != 0 ? -ret
: 0);
114 RETURN_CMP (zp
[zsize
-2], darray
[1]);
116 return (darray
[0] != 0 ? -ret
: 0);
118 RETURN_CMP (zp
[zsize
-3], darray
[0]);
119 RETURN_NONZERO (zp
, zsize
-3, ret
);
122 #if LIMBS_PER_DOUBLE >= 4
125 for (i
= 1; i
<= LIMBS_PER_DOUBLE
; i
++)
127 RETURN_CMP (zp
[zsize
-i
], darray
[LIMBS_PER_DOUBLE
-i
]);
129 RETURN_NONZERO (darray
, LIMBS_PER_DOUBLE
-i
, -ret
);
131 RETURN_NONZERO (zp
, zsize
-LIMBS_PER_DOUBLE
, ret
);