"Fossies" - the Fresh Open Source Software Archive

Member "mpfr-4.0.2/src/set_float128.c" (7 Jan 2019, 3742 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 "set_float128.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_set_float128 -- convert a machine __float128 number to
    2                         a multiple precision floating-point number
    3 
    4 Copyright 2012-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 #include <float.h> /* for DBL_MAX */
   25 
   26 #define MPFR_NEED_LONGLONG_H
   27 #include "mpfr-impl.h"
   28 
   29 #ifdef MPFR_WANT_FLOAT128
   30 
   31 /* The q suffix is a GNU C extension, but so is __float128. */
   32 #define MPFR_FLOAT128_MAX 0x1.ffffffffffffffffffffffffffffp+16383q
   33 
   34 int
   35 mpfr_set_float128 (mpfr_ptr r, __float128 d, mpfr_rnd_t rnd_mode)
   36 {
   37   mpfr_t t;
   38   mp_limb_t *tp;
   39   int inexact, shift_exp, neg, e, i;
   40   __float128 p[14], q[14];
   41   MPFR_SAVE_EXPO_DECL (expo);
   42 
   43   /* Check for NaN */
   44   if (MPFR_UNLIKELY (DOUBLE_ISNAN (d)))
   45     {
   46       MPFR_SET_NAN(r);
   47       MPFR_RET_NAN;
   48     }
   49 
   50   /* Check for INF */
   51   if (MPFR_UNLIKELY (d > MPFR_FLOAT128_MAX))
   52     {
   53       MPFR_SET_INF (r);
   54       MPFR_SET_POS (r);
   55       return 0;
   56     }
   57   else if (MPFR_UNLIKELY (d < -MPFR_FLOAT128_MAX))
   58     {
   59       MPFR_SET_INF (r);
   60       MPFR_SET_NEG (r);
   61       return 0;
   62     }
   63   /* Check for ZERO */
   64   else if (MPFR_UNLIKELY (d == (__float128) 0.0))
   65     return mpfr_set_d (r, (double) d, rnd_mode);
   66 
   67   shift_exp = 0; /* invariant: remainder to deal with is d*2^shift_exp */
   68   neg = d < 0;
   69   if (d < 0)
   70     d = -d;
   71 
   72   /* Scaling, avoiding (slow) divisions. Should the tables be cached? */
   73   if (d >= 1.0)
   74     {
   75       p[0] = 2.0;
   76       q[0] = 0.5;
   77       e = 1;
   78       /* p[i] = 2^(2^i), q[i] = 1/p[i] */
   79       for (i = 0; i < 13 && d >= p[i]; i++)
   80         {
   81           p[i+1] = p[i] * p[i];
   82           q[i+1] = q[i] * q[i];
   83           e <<= 1;
   84         }
   85       for (; i >= 0; e >>= 1, i--)
   86         if (d >= p[i])
   87           {
   88             d *= q[i];
   89             shift_exp += e;
   90           }
   91       d *= 0.5;
   92       shift_exp++;
   93     }
   94   else if (d < 0.5)
   95     {
   96       p[0] = 2.0;
   97       q[0] = 0.5;
   98       e = 1;
   99       /* p[i] = 2^(2^i), q[i] = 1/p[i] */
  100       for (i = 0; i < 13 && d < q[i]; i++)
  101         {
  102           p[i+1] = p[i] * p[i];
  103           q[i+1] = q[i] * q[i];
  104           e <<= 1;
  105         }
  106       /* The while() may be needed for i = 13 due to subnormals.
  107          This can probably be improved without yielding an underflow. */
  108       for (; i >= 0; e >>= 1, i--)
  109         while (d < q[i])
  110           {
  111             d *= p[i];
  112             shift_exp -= e;
  113           }
  114     }
  115 
  116   MPFR_ASSERTD (d >= 0.5 && d < 1.0);
  117 
  118   mpfr_init2 (t, IEEE_FLOAT128_MANT_DIG);
  119   tp = MPFR_MANT (t);
  120 
  121   MPFR_SAVE_EXPO_MARK (expo);
  122   MPFR_SET_EXP (t, shift_exp);
  123   MPFR_SET_SIGN (t, neg ? MPFR_SIGN_NEG : MPFR_SIGN_POS);
  124 
  125   for (i = MPFR_LAST_LIMB (t); i >= 0; i--)
  126     {
  127       d *= 2 * (__float128) MPFR_LIMB_HIGHBIT;
  128       tp[i] = (mp_limb_t) d;
  129       d -= tp[i];
  130     }
  131 
  132   inexact = mpfr_set (r, t, rnd_mode);
  133   mpfr_clear (t);
  134 
  135   MPFR_SAVE_EXPO_FREE (expo);
  136   return mpfr_check_range (r, inexact, rnd_mode);
  137 }
  138 
  139 #endif /* MPFR_WANT_FLOAT128 */