1 /* mpf_cmp_si -- Compare a float with a signed integer. 2 3 Copyright 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2004 Free Software 4 Foundation, Inc. 5 6 This file is part of the GNU MP Library. 7 8 The GNU MP Library is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or (at your 11 option) any later version. 12 13 The GNU MP Library is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16 License for more details. 17 18 You should have received a copy of the GNU Lesser General Public License 19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 20 21 #include "gmp.h" 22 #include "gmp-impl.h" 23 24 int 25 mpf_cmp_si (mpf_srcptr u, long int vval) __GMP_NOTHROW 26 { 27 mp_srcptr up; 28 mp_size_t usize; 29 mp_exp_t uexp; 30 mp_limb_t ulimb; 31 int usign; 32 unsigned long abs_vval; 33 34 uexp = u->_mp_exp; 35 usize = u->_mp_size; 36 37 /* 1. Are the signs different? */ 38 if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */ 39 { 40 /* U and V are both non-negative or both negative. */ 41 if (usize == 0) 42 /* vval >= 0 */ 43 return -(vval != 0); 44 if (vval == 0) 45 /* usize >= 0 */ 46 return usize != 0; 47 /* Fall out. */ 48 } 49 else 50 { 51 /* Either U or V is negative, but not both. */ 52 return usize >= 0 ? 1 : -1; 53 } 54 55 /* U and V have the same sign and are both non-zero. */ 56 57 usign = usize >= 0 ? 1 : -1; 58 usize = ABS (usize); 59 abs_vval = ABS_CAST (unsigned long, vval); 60 61 /* 2. Are the exponents different (V's exponent == 1)? */ 62 #if GMP_NAIL_BITS != 0 63 if (uexp > 1 + (abs_vval > GMP_NUMB_MAX)) 64 return usign; 65 if (uexp < 1 + (abs_vval > GMP_NUMB_MAX)) 66 return -usign; 67 #else 68 if (uexp > 1) 69 return usign; 70 if (uexp < 1) 71 return -usign; 72 #endif 73 74 up = u->_mp_d; 75 76 ulimb = up[usize - 1]; 77 #if GMP_NAIL_BITS != 0 78 if (usize >= 2 && uexp == 2) 79 { 80 if ((ulimb >> GMP_NAIL_BITS) != 0) 81 return usign; 82 ulimb = (ulimb << GMP_NUMB_BITS) | up[usize - 2]; 83 usize--; 84 } 85 #endif 86 usize--; 87 88 /* 3. Compare the most significant mantissa limb with V. */ 89 if (ulimb > abs_vval) 90 return usign; 91 else if (ulimb < abs_vval) 92 return -usign; 93 94 /* Ignore zeroes at the low end of U. */ 95 while (*up == 0) 96 { 97 up++; 98 usize--; 99 } 100 101 /* 4. Now, if the number of limbs are different, we have a difference 102 since we have made sure the trailing limbs are not zero. */ 103 if (usize > 0) 104 return usign; 105 106 /* Wow, we got zero even if we tried hard to avoid it. */ 107 return 0; 108 } 109