Import gmp-4.3.1
[dragonfly.git] / contrib / gmp / mpz / cmpabs_d.c
1 /* mpz_cmpabs_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 ? 1 : -1); \
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_cmpabs_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;
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 (), return -1);
59
60   /* 1. Check for either operand zero. */
61   zsize = SIZ(z);
62   if (d == 0.0)
63     return (zsize != 0);
64   if (zsize == 0)
65     return (d != 0 ? -1 : 0);
66
67   /* 2. Ignore signs. */
68   zsize = ABS(zsize);
69   d = ABS(d);
70
71   /* 3. Small d, knowing abs(z) >= 1. */
72   if (d < 1.0)
73     return 1;
74
75   dexp = __gmp_extract_double (darray, d);
76   ASSERT (dexp >= 1);
77
78   /* 4. Check for different high limb positions. */
79   if (zsize != dexp)
80     return (zsize >= dexp ? 1 : -1);
81
82   /* 5. Limb data. */
83   zp = PTR(z);
84
85 #if LIMBS_PER_DOUBLE == 2
86   RETURN_CMP (zp[zsize-1], darray[1]);
87   if (zsize == 1)
88     return (darray[0] != 0 ? -1 : 0);
89
90   RETURN_CMP (zp[zsize-2], darray[0]);
91   RETURN_NONZERO (zp, zsize-2, 1);
92 #endif
93
94 #if LIMBS_PER_DOUBLE == 3
95   RETURN_CMP (zp[zsize-1], darray[2]);
96   if (zsize == 1)
97     return ((darray[0] | darray[1]) != 0 ? -1 : 0);
98
99   RETURN_CMP (zp[zsize-2], darray[1]);
100   if (zsize == 2)
101     return (darray[0] != 0 ? -1 : 0);
102
103   RETURN_CMP (zp[zsize-3], darray[0]);
104   RETURN_NONZERO (zp, zsize-3, 1);
105 #endif
106
107 #if LIMBS_PER_DOUBLE >= 4
108   {
109     int i;
110     for (i = 1; i <= LIMBS_PER_DOUBLE; i++)
111       {
112         RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]);
113         if (i >= zsize)
114           RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -1);
115       }
116     RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, 1);
117   }
118 #endif
119 }