github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/cmp_d.c (about) 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.h" 38 #include "gmp-impl.h" 39 40 41 #define RETURN_CMP(zl, dl) \ 42 do { \ 43 zlimb = (zl); \ 44 dlimb = (dl); \ 45 if (zlimb != dlimb) \ 46 return (zlimb >= dlimb ? ret : -ret); \ 47 } while (0) 48 49 #define RETURN_NONZERO(ptr, size, val) \ 50 do { \ 51 mp_size_t __i; \ 52 for (__i = (size)-1; __i >= 0; __i--) \ 53 if ((ptr)[__i] != 0) \ 54 return val; \ 55 return 0; \ 56 } while (0) 57 58 59 int 60 mpz_cmp_d (mpz_srcptr z, double d) 61 { 62 mp_limb_t darray[LIMBS_PER_DOUBLE], zlimb, dlimb; 63 mp_srcptr zp; 64 mp_size_t zsize; 65 int dexp, ret; 66 67 /* d=NaN is an invalid operation, there's no sensible return value. 68 d=Inf or -Inf is always bigger than z. */ 69 DOUBLE_NAN_INF_ACTION (d, __gmp_invalid_operation (), goto z_zero); 70 71 /* 1. Either operand zero. */ 72 zsize = SIZ(z); 73 if (d == 0.0) 74 return zsize; 75 if (zsize == 0) 76 { 77 z_zero: 78 return (d < 0.0 ? 1 : -1); 79 } 80 81 /* 2. Opposite signs. */ 82 if (zsize >= 0) 83 { 84 if (d < 0.0) 85 return 1; /* >=0 cmp <0 */ 86 ret = 1; 87 } 88 else 89 { 90 if (d >= 0.0) 91 return -1; /* <0 cmp >=0 */ 92 ret = -1; 93 d = -d; 94 zsize = -zsize; 95 } 96 97 /* 3. Small d, knowing abs(z) >= 1. */ 98 if (d < 1.0) 99 return ret; 100 101 dexp = __gmp_extract_double (darray, d); 102 ASSERT (dexp >= 1); 103 104 /* 4. Check for different high limb positions. */ 105 if (zsize != dexp) 106 return (zsize >= dexp ? ret : -ret); 107 108 /* 5. Limb data. */ 109 zp = PTR(z); 110 111 #if LIMBS_PER_DOUBLE == 2 112 RETURN_CMP (zp[zsize-1], darray[1]); 113 if (zsize == 1) 114 return (darray[0] != 0 ? -ret : 0); 115 116 RETURN_CMP (zp[zsize-2], darray[0]); 117 RETURN_NONZERO (zp, zsize-2, ret); 118 #endif 119 120 #if LIMBS_PER_DOUBLE == 3 121 RETURN_CMP (zp[zsize-1], darray[2]); 122 if (zsize == 1) 123 return ((darray[0] | darray[1]) != 0 ? -ret : 0); 124 125 RETURN_CMP (zp[zsize-2], darray[1]); 126 if (zsize == 2) 127 return (darray[0] != 0 ? -ret : 0); 128 129 RETURN_CMP (zp[zsize-3], darray[0]); 130 RETURN_NONZERO (zp, zsize-3, ret); 131 #endif 132 133 #if LIMBS_PER_DOUBLE >= 4 134 { 135 int i; 136 for (i = 1; i <= LIMBS_PER_DOUBLE; i++) 137 { 138 RETURN_CMP (zp[zsize-i], darray[LIMBS_PER_DOUBLE-i]); 139 if (i >= zsize) 140 RETURN_NONZERO (darray, LIMBS_PER_DOUBLE-i, -ret); 141 } 142 RETURN_NONZERO (zp, zsize-LIMBS_PER_DOUBLE, ret); 143 } 144 #endif 145 }