Import gmp-4.3.1
[dragonfly.git] / contrib / gmp / mpz / cmp_d.c
1 /* mpz_cmp_d -- compare absolute values of mpz and double.
2
3 Copyright 2001, 2002, 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 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.
11
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.
16
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/.  */
19
20 #include "config.h"
21
22 #if HAVE_FLOAT_H
23 #include <float.h>  /* for DBL_MAX */
24 #endif
25
26 #include "gmp.h"
27 #include "gmp-impl.h"
28
29
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)
37
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)
46
47
48 int
49 mpz_cmp_d (mpz_srcptr z, double d)
50 {
51   mp_limb_t  darray[LIMBS_PER_DOUBLE], zlimb, dlimb;
52   mp_srcptr  zp;
53   mp_size_t  zsize;
54   int        dexp, ret;
55
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);
59
60   /* 1. Either operand zero. */
61   zsize = SIZ(z);
62   if (d == 0.0)
63     return zsize;
64   if (zsize == 0)
65     {
66     z_zero:
67       return (d < 0.0 ? 1 : -1);
68     }
69
70   /* 2. Opposite signs. */
71   if (zsize >= 0)
72     {
73       if (d < 0.0)
74         return 1;    /* >=0 cmp <0 */
75       ret = 1;
76     }
77   else
78     {
79       if (d >= 0.0)
80         return -1;   /* <0 cmp >=0 */
81       ret = -1;
82       d = -d;
83       zsize = -zsize;
84     }
85
86   /* 3. Small d, knowing abs(z) >= 1. */
87   if (d < 1.0)
88     return ret;
89
90   dexp = __gmp_extract_double (darray, d);
91   ASSERT (dexp >= 1);
92
93   /* 4. Check for different high limb positions. */
94   if (zsize != dexp)
95     return (zsize >= dexp ? ret : -ret);
96
97   /* 5. Limb data. */
98   zp = PTR(z);
99
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);
104
105   RETURN_CMP (zp[zsize-2], darray[0]);
106   RETURN_NONZERO (zp, zsize-2, ret);
107 #endif
108
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);
113
114   RETURN_CMP (zp[zsize-2], darray[1]);
115   if (zsize == 2)
116     return (darray[0] != 0 ? -ret : 0);
117
118   RETURN_CMP (zp[zsize-3], darray[0]);
119   RETURN_NONZERO (zp, zsize-3, ret);
120 #endif
121
122 #if LIMBS_PER_DOUBLE >= 4
123   {
124     int i;
125     for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
126       {
127         RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
128         if (i >= zsize)
129           RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -ret);
130       }
131     RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, ret);
132   }
133 #endif
134 }