1 /* mpz_cmpabs_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 ? 1 : -1); \
38 #define RETURN_NONZERO(ptr, size, val) \
41 for (__i = (size)-1; __i >= 0; __i--) \
42 if ((ptr)[__i] != 0) \
49 mpz_cmpabs_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 (), return -1);
60 /* 1. Check for either operand zero. */
65 return (d != 0 ? -1 : 0);
67 /* 2. Ignore signs. */
71 /* 3. Small d, knowing abs(z) >= 1. */
75 dexp = __gmp_extract_double (darray, d);
78 /* 4. Check for different high limb positions. */
80 return (zsize >= dexp ? 1 : -1);
85 #if LIMBS_PER_DOUBLE == 2
86 RETURN_CMP (zp[zsize-1], darray[1]);
88 return (darray[0] != 0 ? -1 : 0);
90 RETURN_CMP (zp[zsize-2], darray[0]);
91 RETURN_NONZERO (zp, zsize-2, 1);
94 #if LIMBS_PER_DOUBLE == 3
95 RETURN_CMP (zp[zsize-1], darray[2]);
97 return ((darray[0] | darray[1]) != 0 ? -1 : 0);
99 RETURN_CMP (zp[zsize-2], darray[1]);
101 return (darray[0] != 0 ? -1 : 0);
103 RETURN_CMP (zp[zsize-3], darray[0]);
104 RETURN_NONZERO (zp, zsize-3, 1);
107 #if LIMBS_PER_DOUBLE >= 4
110 for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
112 RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
114 RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -1);
116 RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, 1);