1 | /* mpz_cmpabs_d -- compare absolute values of mpz and double. |
---|
2 | |
---|
3 | Copyright 2001, 2002 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 2.1 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; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "gmp.h" |
---|
24 | #include "gmp-impl.h" |
---|
25 | |
---|
26 | |
---|
27 | #define RETURN_CMP(zl, dl) \ |
---|
28 | do { \ |
---|
29 | zlimb = (zl); \ |
---|
30 | dlimb = (dl); \ |
---|
31 | if (zlimb != dlimb) \ |
---|
32 | return (zlimb >= dlimb ? ret : -ret); \ |
---|
33 | } while (0) |
---|
34 | |
---|
35 | #define RETURN_NONZERO(ptr, size, val) \ |
---|
36 | do { \ |
---|
37 | mp_size_t __i; \ |
---|
38 | for (__i = (size)-1; __i >= 0; __i--) \ |
---|
39 | if ((ptr)[__i] != 0) \ |
---|
40 | return val; \ |
---|
41 | return 0; \ |
---|
42 | } while (0) |
---|
43 | |
---|
44 | |
---|
45 | int |
---|
46 | mpz_cmp_d (mpz_srcptr z, double d) |
---|
47 | { |
---|
48 | mp_limb_t darray[LIMBS_PER_DOUBLE], zlimb, dlimb; |
---|
49 | mp_srcptr zp; |
---|
50 | mp_size_t zsize; |
---|
51 | int dexp, ret; |
---|
52 | |
---|
53 | /* 1. Either operand zero. */ |
---|
54 | zsize = SIZ(z); |
---|
55 | if (d == 0.0) |
---|
56 | return zsize; |
---|
57 | if (zsize == 0) |
---|
58 | return (d < 0.0 ? 1 : -1); |
---|
59 | |
---|
60 | /* 2. Opposite signs. */ |
---|
61 | if (zsize >= 0) |
---|
62 | { |
---|
63 | if (d < 0.0) |
---|
64 | return 1; /* >=0 cmp <0 */ |
---|
65 | ret = 1; |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | if (d >= 0.0) |
---|
70 | return -1; /* <0 cmp >=0 */ |
---|
71 | ret = -1; |
---|
72 | d = -d; |
---|
73 | zsize = -zsize; |
---|
74 | } |
---|
75 | |
---|
76 | /* 3. Small d, knowing abs(z) >= 1. */ |
---|
77 | if (d < 1.0) |
---|
78 | return ret; |
---|
79 | |
---|
80 | dexp = __gmp_extract_double (darray, d); |
---|
81 | ASSERT (dexp >= 1); |
---|
82 | |
---|
83 | /* 4. Different high limb positions. */ |
---|
84 | if (zsize != dexp) |
---|
85 | return (zsize >= dexp ? ret : -ret); |
---|
86 | |
---|
87 | /* 5. Limb data. */ |
---|
88 | zp = PTR(z); |
---|
89 | |
---|
90 | #if LIMBS_PER_DOUBLE == 2 |
---|
91 | RETURN_CMP (zp[zsize-1], darray[1]); |
---|
92 | if (zsize == 1) |
---|
93 | return (darray[0] != 0 ? -ret : 0); |
---|
94 | |
---|
95 | RETURN_CMP (zp[zsize-2], darray[0]); |
---|
96 | RETURN_NONZERO (zp, zsize-2, ret); |
---|
97 | |
---|
98 | #else |
---|
99 | #if LIMBS_PER_DOUBLE == 3 |
---|
100 | RETURN_CMP (zp[zsize-1], darray[2]); |
---|
101 | if (zsize == 1) |
---|
102 | return ((darray[0] | darray[1]) != 0 ? -ret : 0); |
---|
103 | |
---|
104 | RETURN_CMP (zp[zsize-2], darray[1]); |
---|
105 | if (zsize == 2) |
---|
106 | return (darray[0] != 0 ? -ret : 0); |
---|
107 | |
---|
108 | RETURN_CMP (zp[zsize-3], darray[0]); |
---|
109 | RETURN_NONZERO (zp, zsize-3, ret); |
---|
110 | |
---|
111 | #else |
---|
112 | for (i = 1; i <= LIMBS_PER_DOUBLE; i++) |
---|
113 | { |
---|
114 | RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]); |
---|
115 | if (i >= zsize) |
---|
116 | RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -ret); |
---|
117 | } |
---|
118 | RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, ret); |
---|
119 | #endif |
---|
120 | #endif |
---|
121 | } |
---|