iwmfw - Add version 22 firmwares for AC3168 and AC8265 chipsets.
[dragonfly.git] / contrib / gmp / mpz / cmp_d.c
blobfd635a68e1b7c06e54d9e907438f8fc90b259ba6
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/. */
20 #include "config.h"
22 #if HAVE_FLOAT_H
23 #include <float.h> /* for DBL_MAX */
24 #endif
26 #include "gmp.h"
27 #include "gmp-impl.h"
30 #define RETURN_CMP(zl, dl) \
31 do { \
32 zlimb = (zl); \
33 dlimb = (dl); \
34 if (zlimb != dlimb) \
35 return (zlimb >= dlimb ? ret : -ret); \
36 } while (0)
38 #define RETURN_NONZERO(ptr, size, val) \
39 do { \
40 mp_size_t __i; \
41 for (__i = (size)-1; __i >= 0; __i--) \
42 if ((ptr)[__i] != 0) \
43 return val; \
44 return 0; \
45 } while (0)
48 int
49 mpz_cmp_d (mpz_srcptr z, double d)
51 mp_limb_t darray[LIMBS_PER_DOUBLE], zlimb, dlimb;
52 mp_srcptr zp;
53 mp_size_t zsize;
54 int dexp, ret;
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. */
61 zsize = SIZ(z);
62 if (d == 0.0)
63 return zsize;
64 if (zsize == 0)
66 z_zero:
67 return (d < 0.0 ? 1 : -1);
70 /* 2. Opposite signs. */
71 if (zsize >= 0)
73 if (d < 0.0)
74 return 1; /* >=0 cmp <0 */
75 ret = 1;
77 else
79 if (d >= 0.0)
80 return -1; /* <0 cmp >=0 */
81 ret = -1;
82 d = -d;
83 zsize = -zsize;
86 /* 3. Small d, knowing abs(z) >= 1. */
87 if (d < 1.0)
88 return ret;
90 dexp = __gmp_extract_double (darray, d);
91 ASSERT (dexp >= 1);
93 /* 4. Check for different high limb positions. */
94 if (zsize != dexp)
95 return (zsize >= dexp ? ret : -ret);
97 /* 5. Limb data. */
98 zp = PTR(z);
100 #if LIMBS_PER_DOUBLE == 2
101 RETURN_CMP (zp[zsize-1], darray[1]);
102 if (zsize == 1)
103 return (darray[0] != 0 ? -ret : 0);
105 RETURN_CMP (zp[zsize-2], darray[0]);
106 RETURN_NONZERO (zp, zsize-2, ret);
107 #endif
109 #if LIMBS_PER_DOUBLE == 3
110 RETURN_CMP (zp[zsize-1], darray[2]);
111 if (zsize == 1)
112 return ((darray[0] | darray[1]) != 0 ? -ret : 0);
114 RETURN_CMP (zp[zsize-2], darray[1]);
115 if (zsize == 2)
116 return (darray[0] != 0 ? -ret : 0);
118 RETURN_CMP (zp[zsize-3], darray[0]);
119 RETURN_NONZERO (zp, zsize-3, ret);
120 #endif
122 #if LIMBS_PER_DOUBLE >= 4
124 int i;
125 for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
127 RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
128 if (i >= zsize)
129 RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -ret);
131 RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, ret);
133 #endif