1 /* mpz_cmp_d -- compare absolute values of mpz and double.
2
3 Copyright 2001-2003 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
9
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.
13
14 or
15
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.
19
20 or both in parallel, as here.
21
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.
26
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/. */
30
31 #include "config.h"
32
33 #if HAVE_FLOAT_H
34 #include <float.h> /* for DBL_MAX */
35 #endif
36
37 #include "gmp-impl.h"
38
39
40 #define RETURN_CMP(zl, dl) \
41 do { \
42 zlimb = (zl); \
43 dlimb = (dl); \
44 if (zlimb != dlimb) \
45 return (zlimb >= dlimb ? ret : -ret); \
46 } while (0)
47
48 #define RETURN_NONZERO(ptr, size, val) \
49 do { \
50 mp_size_t __i; \
51 for (__i = (size)-1; __i >= 0; __i--) \
52 if ((ptr)[__i] != 0) \
53 return val; \
54 return 0; \
55 } while (0)
56
57
58 int
mpz_cmp_d(mpz_srcptr z,double d)59 mpz_cmp_d (mpz_srcptr z, double d)
60 {
61 mp_limb_t darray[LIMBS_PER_DOUBLE], zlimb, dlimb;
62 mp_srcptr zp;
63 mp_size_t zsize;
64 int dexp, ret;
65
66 /* d=NaN is an invalid operation, there's no sensible return value.
67 d=Inf or -Inf is always bigger than z. */
68 DOUBLE_NAN_INF_ACTION (d, __gmp_invalid_operation (), goto z_zero);
69
70 /* 1. Either operand zero. */
71 zsize = SIZ(z);
72 if (d == 0.0)
73 return zsize;
74 if (zsize == 0)
75 {
76 z_zero:
77 return (d < 0.0 ? 1 : -1);
78 }
79
80 /* 2. Opposite signs. */
81 if (zsize >= 0)
82 {
83 if (d < 0.0)
84 return 1; /* >=0 cmp <0 */
85 ret = 1;
86 }
87 else
88 {
89 if (d >= 0.0)
90 return -1; /* <0 cmp >=0 */
91 ret = -1;
92 d = -d;
93 zsize = -zsize;
94 }
95
96 /* 3. Small d, knowing abs(z) >= 1. */
97 if (d < 1.0)
98 return ret;
99
100 dexp = __gmp_extract_double (darray, d);
101 ASSERT (dexp >= 1);
102
103 /* 4. Check for different high limb positions. */
104 if (zsize != dexp)
105 return (zsize >= dexp ? ret : -ret);
106
107 /* 5. Limb data. */
108 zp = PTR(z);
109
110 #if LIMBS_PER_DOUBLE == 2
111 RETURN_CMP (zp[zsize-1], darray[1]);
112 if (zsize == 1)
113 return (darray[0] != 0 ? -ret : 0);
114
115 RETURN_CMP (zp[zsize-2], darray[0]);
116 RETURN_NONZERO (zp, zsize-2, ret);
117 #endif
118
119 #if LIMBS_PER_DOUBLE == 3
120 RETURN_CMP (zp[zsize-1], darray[2]);
121 if (zsize == 1)
122 return ((darray[0] | darray[1]) != 0 ? -ret : 0);
123
124 RETURN_CMP (zp[zsize-2], darray[1]);
125 if (zsize == 2)
126 return (darray[0] != 0 ? -ret : 0);
127
128 RETURN_CMP (zp[zsize-3], darray[0]);
129 RETURN_NONZERO (zp, zsize-3, ret);
130 #endif
131
132 #if LIMBS_PER_DOUBLE >= 4
133 {
134 int i;
135 for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
136 {
137 RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
138 if (i >= zsize)
139 RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -ret);
140 }
141 RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, ret);
142 }
143 #endif
144 }
145