1/* mpf_cmp -- Compare two floats. 2 3Copyright 1993, 1994, 1996, 2001, 2015 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library. 6 7The GNU MP Library is free software; you can redistribute it and/or modify 8it 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 14or 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 20or both in parallel, as here. 21 22The GNU MP Library is distributed in the hope that it will be useful, but 23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 24or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 25for more details. 26 27You should have received copies of the GNU General Public License and the 28GNU Lesser General Public License along with the GNU MP Library. If not, 29see https://www.gnu.org/licenses/. */ 30 31#include "gmp-impl.h" 32 33int 34mpf_cmp (mpf_srcptr u, mpf_srcptr v) __GMP_NOTHROW 35{ 36 mp_srcptr up, vp; 37 mp_size_t usize, vsize; 38 mp_exp_t uexp, vexp; 39 int cmp; 40 int usign; 41 42 usize = SIZ(u); 43 vsize = SIZ(v); 44 usign = usize >= 0 ? 1 : -1; 45 46 /* 1. Are the signs different? */ 47 if ((usize ^ vsize) >= 0) 48 { 49 /* U and V are both non-negative or both negative. */ 50 if (usize == 0) 51 /* vsize >= 0 */ 52 return -(vsize != 0); 53 if (vsize == 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 usign; 62 } 63 64 /* U and V have the same sign and are both non-zero. */ 65 66 uexp = EXP(u); 67 vexp = EXP(v); 68 69 /* 2. Are the exponents different? */ 70 if (uexp > vexp) 71 return usign; 72 if (uexp < vexp) 73 return -usign; 74 75 usize = ABS (usize); 76 vsize = ABS (vsize); 77 78 up = PTR (u); 79 vp = PTR (v); 80 81#define STRICT_MPF_NORMALIZATION 0 82#if ! STRICT_MPF_NORMALIZATION 83 /* Ignore zeroes at the low end of U and V. */ 84 do { 85 mp_limb_t tl; 86 tl = up[0]; 87 MPN_STRIP_LOW_ZEROS_NOT_ZERO (up, usize, tl); 88 tl = vp[0]; 89 MPN_STRIP_LOW_ZEROS_NOT_ZERO (vp, vsize, tl); 90 } while (0); 91#endif 92 93 if (usize > vsize) 94 { 95 cmp = mpn_cmp (up + usize - vsize, vp, vsize); 96 /* if (cmp == 0) */ 97 /* return usign; */ 98 ++cmp; 99 } 100 else if (vsize > usize) 101 { 102 cmp = mpn_cmp (up, vp + vsize - usize, usize); 103 /* if (cmp == 0) */ 104 /* return -usign; */ 105 } 106 else 107 { 108 cmp = mpn_cmp (up, vp, usize); 109 if (cmp == 0) 110 return 0; 111 } 112 return cmp > 0 ? usign : -usign; 113} 114