"Fossies" - the Fresh Open Source Software Archive

Member "mpfr-4.0.2/src/cmp_si.c" (7 Jan 2019, 2875 Bytes) of package /linux/misc/mpfr-4.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cmp_si.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.0.1_vs_4.0.2.

    1 /* mpfr_cmp_si_2exp -- compare a floating-point number with a signed
    2 machine integer multiplied by a power of 2
    3 
    4 Copyright 1999, 2001-2019 Free Software Foundation, Inc.
    5 Contributed by the AriC and Caramba projects, INRIA.
    6 
    7 This file is part of the GNU MPFR Library.
    8 
    9 The GNU MPFR Library is free software; you can redistribute it and/or modify
   10 it under the terms of the GNU Lesser General Public License as published by
   11 the Free Software Foundation; either version 3 of the License, or (at your
   12 option) any later version.
   13 
   14 The GNU MPFR Library is distributed in the hope that it will be useful, but
   15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   17 License for more details.
   18 
   19 You should have received a copy of the GNU Lesser General Public License
   20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
   21 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
   22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
   23 
   24 #define MPFR_NEED_LONGLONG_H
   25 #include "mpfr-impl.h"
   26 
   27 /* returns a positive value if b > i*2^f,
   28            a negative value if b < i*2^f,
   29            zero if b = i*2^f.
   30    b must not be NaN.
   31 */
   32 
   33 int
   34 mpfr_cmp_si_2exp (mpfr_srcptr b, long int i, mpfr_exp_t f)
   35 {
   36   int si;
   37 
   38   si = i < 0 ? -1 : 1; /* sign of i */
   39   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (b)))
   40     {
   41       if (MPFR_IS_INF(b))
   42         return MPFR_INT_SIGN(b);
   43       else if (MPFR_IS_ZERO(b))
   44         return i != 0 ? -si : 0;
   45       /* NAN */
   46       MPFR_SET_ERANGEFLAG ();
   47       return 0;
   48     }
   49   else if (MPFR_SIGN(b) != si || i == 0)
   50     return MPFR_INT_SIGN (b);
   51   else /* b and i are of same sign si */
   52     {
   53       mpfr_exp_t e;
   54       unsigned long ai;
   55       int k;
   56       mp_size_t bn;
   57       mp_limb_t c, *bp;
   58 
   59       ai = SAFE_ABS(unsigned long, i);
   60 
   61       /* ai must be representable in a mp_limb_t */
   62       MPFR_ASSERTN(ai == (mp_limb_t) ai);
   63 
   64       e = MPFR_GET_EXP (b); /* 2^(e-1) <= b < 2^e */
   65       if (e <= f)
   66         return -si;
   67       if (f < MPFR_EMAX_MAX - GMP_NUMB_BITS &&
   68           e > f + GMP_NUMB_BITS)
   69         return si;
   70 
   71       /* now f < e <= f + GMP_NUMB_BITS */
   72       c = (mp_limb_t) ai;
   73       count_leading_zeros(k, c);
   74       if ((int) (e - f) > GMP_NUMB_BITS - k)
   75         return si;
   76       if ((int) (e - f) < GMP_NUMB_BITS - k)
   77         return -si;
   78 
   79       /* now b and i*2^f have the same exponent */
   80       c <<= k;
   81       bn = (MPFR_PREC(b) - 1) / GMP_NUMB_BITS;
   82       bp = MPFR_MANT(b);
   83       if (bp[bn] > c)
   84         return si;
   85       if (bp[bn] < c)
   86         return -si;
   87 
   88       /* most significant limbs agree, check remaining limbs from b */
   89       while (bn > 0)
   90         if (bp[--bn])
   91           return si;
   92       return 0;
   93     }
   94 }
   95 
   96 #undef mpfr_cmp_si
   97 int
   98 mpfr_cmp_si (mpfr_srcptr b, long int i)
   99 {
  100   return mpfr_cmp_si_2exp (b, i, 0);
  101 }