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

     1  /* mpf_cmp -- Compare two floats.
     2  
     3  Copyright 1993, 1994, 1996, 2001, 2015 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 "gmp.h"
    32  #include "gmp-impl.h"
    33  
    34  int
    35  mpf_cmp (mpf_srcptr u, mpf_srcptr v) __GMP_NOTHROW
    36  {
    37    mp_srcptr up, vp;
    38    mp_size_t usize, vsize;
    39    mp_exp_t uexp, vexp;
    40    int cmp;
    41    int usign;
    42  
    43    usize = SIZ(u);
    44    vsize = SIZ(v);
    45    usign = usize >= 0 ? 1 : -1;
    46  
    47    /* 1. Are the signs different?  */
    48    if ((usize ^ vsize) >= 0)
    49      {
    50        /* U and V are both non-negative or both negative.  */
    51        if (usize == 0)
    52  	/* vsize >= 0 */
    53  	return -(vsize != 0);
    54        if (vsize == 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 usign;
    63      }
    64  
    65    /* U and V have the same sign and are both non-zero.  */
    66  
    67    uexp = EXP(u);
    68    vexp = EXP(v);
    69  
    70    /* 2. Are the exponents different?  */
    71    if (uexp > vexp)
    72      return usign;
    73    if (uexp < vexp)
    74      return -usign;
    75  
    76    usize = ABS (usize);
    77    vsize = ABS (vsize);
    78  
    79    up = PTR (u);
    80    vp = PTR (v);
    81  
    82  #define STRICT_MPF_NORMALIZATION 0
    83  #if ! STRICT_MPF_NORMALIZATION
    84    /* Ignore zeroes at the low end of U and V.  */
    85    do {
    86      mp_limb_t tl;
    87      tl = up[0];
    88      MPN_STRIP_LOW_ZEROS_NOT_ZERO (up, usize, tl);
    89      tl = vp[0];
    90      MPN_STRIP_LOW_ZEROS_NOT_ZERO (vp, vsize, tl);
    91    } while (0);
    92  #endif
    93  
    94    if (usize > vsize)
    95      {
    96        cmp = mpn_cmp (up + usize - vsize, vp, vsize);
    97        /* if (cmp == 0) */
    98        /*	return usign; */
    99        ++cmp;
   100      }
   101    else if (vsize > usize)
   102      {
   103        cmp = mpn_cmp (up, vp + vsize - usize, usize);
   104        /* if (cmp == 0) */
   105        /*	return -usign; */
   106      }
   107    else
   108      {
   109        cmp = mpn_cmp (up, vp, usize);
   110        if (cmp == 0)
   111  	return 0;
   112      }
   113    return cmp > 0 ? usign : -usign;
   114  }