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