github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpf/cmp_si.c (about)

     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.h"
    33  #include "gmp-impl.h"
    34  
    35  int
    36  mpf_cmp_si (mpf_srcptr u, long int vval) __GMP_NOTHROW
    37  {
    38    mp_srcptr up;
    39    mp_size_t usize;
    40    mp_exp_t uexp;
    41    mp_limb_t ulimb;
    42    int usign;
    43    unsigned long abs_vval;
    44  
    45    usize = SIZ (u);
    46  
    47    /* 1. Are the signs different?  */
    48    if ((usize < 0) == (vval < 0)) /* don't use xor, type size may differ */
    49      {
    50        /* U and V are both non-negative or both negative.  */
    51        if (usize == 0)
    52  	/* vval >= 0 */
    53  	return -(vval != 0);
    54        if (vval == 0)
    55  	/* usize >= 0 */
    56  	return usize != 0;
    57        /* Fall out.  */
    58      }
    59    else
    60      {
    61        /* Either U or V is negative, but not both.  */
    62        return usize >= 0 ? 1 : -1;
    63      }
    64  
    65    /* U and V have the same sign and are both non-zero.  */
    66  
    67    /* 2. Are the exponents different (V's exponent == 1)?  */
    68    uexp = EXP (u);
    69    usign = usize >= 0 ? 1 : -1;
    70    usize = ABS (usize);
    71    abs_vval = ABS_CAST (unsigned long, vval);
    72  
    73  #if GMP_NAIL_BITS != 0
    74    if (uexp != 1 + (abs_vval > GMP_NUMB_MAX))
    75      return (uexp < 1 + (abs_vval > GMP_NUMB_MAX)) ? -usign : usign;
    76  #else
    77    if (uexp != 1)
    78      return (uexp < 1) ? -usign : usign;
    79  #endif
    80  
    81    up = PTR (u);
    82  
    83    ASSERT (usize > 0);
    84    ulimb = up[--usize];
    85  #if GMP_NAIL_BITS != 0
    86    if (uexp == 2)
    87      {
    88        if ((ulimb >> GMP_NAIL_BITS) != 0)
    89  	return usign;
    90        ulimb = (ulimb << GMP_NUMB_BITS);
    91        if (usize != 0) ulimb |= up[--usize];
    92      }
    93  #endif
    94  
    95    /* 3. Compare the most significant mantissa limb with V.  */
    96    if (ulimb != abs_vval)
    97      return (ulimb < abs_vval) ? -usign : usign;
    98  
    99    /* Ignore zeroes at the low end of U.  */
   100    for (; *up == 0; ++up)
   101      --usize;
   102  
   103    /* 4. Now, if the number of limbs are different, we have a difference
   104       since we have made sure the trailing limbs are not zero.  */
   105    if (usize > 0)
   106      return usign;
   107  
   108    /* Wow, we got zero even if we tried hard to avoid it.  */
   109    return 0;
   110  }