beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / cmpabs_d.c
bloba4b95d26aa3399f803f0d32ed000c4668ca892e9
1 /* mpz_cmpabs_d -- compare absolute values of mpz and double.
3 Copyright 2001-2003, 2012 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 either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "config.h"
33 #if HAVE_FLOAT_H
34 #include <float.h> /* for DBL_MAX */
35 #endif
37 #include "gmp.h"
38 #include "gmp-impl.h"
41 #define RETURN_CMP(zl, dl) \
42 do { \
43 zlimb = (zl); \
44 dlimb = (dl); \
45 if (zlimb != dlimb) \
46 return (zlimb >= dlimb ? 1 : -1); \
47 } while (0)
49 #define RETURN_NONZERO(ptr, size, val) \
50 do { \
51 mp_size_t __i; \
52 for (__i = (size)-1; __i >= 0; __i--) \
53 if ((ptr)[__i] != 0) \
54 return val; \
55 return 0; \
56 } while (0)
59 int
60 mpz_cmpabs_d (mpz_srcptr z, double d)
62 mp_limb_t darray[LIMBS_PER_DOUBLE], zlimb, dlimb;
63 mp_srcptr zp;
64 mp_size_t zsize;
65 int dexp;
67 /* d=NaN is an invalid operation, there's no sensible return value.
68 d=Inf or -Inf is always bigger than z. */
69 DOUBLE_NAN_INF_ACTION (d, __gmp_invalid_operation (), return -1);
71 /* 1. Check for either operand zero. */
72 zsize = SIZ(z);
73 if (d == 0.0)
74 return (zsize != 0);
75 if (zsize == 0)
76 return -1; /* d != 0 */
78 /* 2. Ignore signs. */
79 zsize = ABS(zsize);
80 d = ABS(d);
82 /* 3. Small d, knowing abs(z) >= 1. */
83 if (d < 1.0)
84 return 1;
86 dexp = __gmp_extract_double (darray, d);
87 ASSERT (dexp >= 1);
89 /* 4. Check for different high limb positions. */
90 if (zsize != dexp)
91 return (zsize >= dexp ? 1 : -1);
93 /* 5. Limb data. */
94 zp = PTR(z);
96 #if LIMBS_PER_DOUBLE == 2
97 RETURN_CMP (zp[zsize-1], darray[1]);
98 if (zsize == 1)
99 return (darray[0] != 0 ? -1 : 0);
101 RETURN_CMP (zp[zsize-2], darray[0]);
102 RETURN_NONZERO (zp, zsize-2, 1);
103 #endif
105 #if LIMBS_PER_DOUBLE == 3
106 RETURN_CMP (zp[zsize-1], darray[2]);
107 if (zsize == 1)
108 return ((darray[0] | darray[1]) != 0 ? -1 : 0);
110 RETURN_CMP (zp[zsize-2], darray[1]);
111 if (zsize == 2)
112 return (darray[0] != 0 ? -1 : 0);
114 RETURN_CMP (zp[zsize-3], darray[0]);
115 RETURN_NONZERO (zp, zsize-3, 1);
116 #endif
118 #if LIMBS_PER_DOUBLE >= 4
120 int i;
121 for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
123 RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
124 if (i >= zsize)
125 RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -1);
127 RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, 1);
129 #endif