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