Example #1
0
double
mpfr_get_d (mpfr_srcptr src, mpfr_rnd_t rnd_mode)
{
  double d;
  int negative;
  mpfr_exp_t e;

  if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (src)))
    {
      if (MPFR_IS_NAN (src))
        return MPFR_DBL_NAN;

      negative = MPFR_IS_NEG (src);

      if (MPFR_IS_INF (src))
        return negative ? MPFR_DBL_INFM : MPFR_DBL_INFP;

      MPFR_ASSERTD (MPFR_IS_ZERO(src));
      return negative ? DBL_NEG_ZERO : 0.0;
    }

  e = MPFR_GET_EXP (src);
  negative = MPFR_IS_NEG (src);

  if (MPFR_UNLIKELY(rnd_mode == MPFR_RNDA))
    rnd_mode = negative ? MPFR_RNDD : MPFR_RNDU;

  /* the smallest normalized number is 2^(-1022)=0.1e-1021, and the smallest
     subnormal is 2^(-1074)=0.1e-1073 */
  if (MPFR_UNLIKELY (e < -1073))
    {
      /* Note: Avoid using a constant expression DBL_MIN * DBL_EPSILON
         as this gives 0 instead of the correct result with gcc on some
         Alpha machines. */
      d = negative ?
        (rnd_mode == MPFR_RNDD ||
         (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp(src, -1, -1075) < 0)
         ? -DBL_MIN : DBL_NEG_ZERO) :
        (rnd_mode == MPFR_RNDU ||
         (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp(src, 1, -1075) > 0)
         ? DBL_MIN : 0.0);
      if (d != 0.0) /* we multiply DBL_MIN = 2^(-1022) by DBL_EPSILON = 2^(-52)
                       to get +-2^(-1074) */
        d *= DBL_EPSILON;
    }
  /* the largest normalized number is 2^1024*(1-2^(-53))=0.111...111e1024 */
  else if (MPFR_UNLIKELY (e > 1024))
    {
      d = negative ?
        (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDU ?
         -DBL_MAX : MPFR_DBL_INFM) :
        (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDD ?
         DBL_MAX : MPFR_DBL_INFP);
    }
  else
    {
      int nbits;
      mp_size_t np, i;
      mp_limb_t tp[ MPFR_LIMBS_PER_DOUBLE ];
      int carry;

      nbits = IEEE_DBL_MANT_DIG; /* 53 */
      if (MPFR_UNLIKELY (e < -1021))
        /*In the subnormal case, compute the exact number of significant bits*/
        {
          nbits += (1021 + e);
          MPFR_ASSERTD (nbits >= 1);
        }
      np = MPFR_PREC2LIMBS (nbits);
      MPFR_ASSERTD ( np <= MPFR_LIMBS_PER_DOUBLE );
      carry = mpfr_round_raw_4 (tp, MPFR_MANT(src), MPFR_PREC(src), negative,
                                nbits, rnd_mode);
      if (MPFR_UNLIKELY(carry))
        d = 1.0;
      else
        {
          /* The following computations are exact thanks to the previous
             mpfr_round_raw. */
          d = (double) tp[0] / MP_BASE_AS_DOUBLE;
          for (i = 1 ; i < np ; i++)
            d = (d + tp[i]) / MP_BASE_AS_DOUBLE;
          /* d is the mantissa (between 1/2 and 1) of the argument rounded
             to 53 bits */
        }
      d = mpfr_scale2 (d, e);
      if (negative)
        d = -d;
    }

  return d;
}
Example #2
0
float
mpfr_get_flt (mpfr_srcptr src, mpfr_rnd_t rnd_mode)
{
    int negative;
    mpfr_exp_t e;
    float d;

    /* in case of NaN, +Inf, -Inf, +0, -0, the conversion from double to float
       is exact */
    if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (src)))
        return (float) mpfr_get_d (src, rnd_mode);

    e = MPFR_GET_EXP (src);
    negative = MPFR_IS_NEG (src);

    if (MPFR_UNLIKELY(rnd_mode == MPFR_RNDA))
        rnd_mode = negative ? MPFR_RNDD : MPFR_RNDU;

    /* the smallest positive normal float number is 2^(-126) = 0.5*2^(-125),
       and the smallest positive subnormal number is 2^(-149) = 0.5*2^(-148) */
    if (MPFR_UNLIKELY (e < -148))
    {
        /* |src| < 2^(-149), i.e., |src| is smaller than the smallest positive
           subnormal number.
           In round-to-nearest mode, 2^(-150) is rounded to zero.
        */
        d = negative ?
            (rnd_mode == MPFR_RNDD ||
             (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp (src, -1, -150) < 0)
             ? -FLT_MIN : FLT_NEG_ZERO) :
            (rnd_mode == MPFR_RNDU ||
             (rnd_mode == MPFR_RNDN && mpfr_cmp_si_2exp (src, 1, -150) > 0)
             ? FLT_MIN : 0.0);
        if (d != 0.0) /* we multiply FLT_MIN = 2^(-126) by FLT_EPSILON = 2^(-23)
                       to get +-2^(-149) */
            d *= FLT_EPSILON;
    }
    /* the largest normal number is 2^128*(1-2^(-24)) = 0.111...111e128 */
    else if (MPFR_UNLIKELY (e > 128))
    {
        d = negative ?
            (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDU ?
             -FLT_MAX : MPFR_FLT_INFM) :
            (rnd_mode == MPFR_RNDZ || rnd_mode == MPFR_RNDD ?
             FLT_MAX : MPFR_FLT_INFP);
    }
    else /* -148 <= e <= 127 */
    {
        int nbits;
        mp_size_t np, i;
        mp_limb_t tp[MPFR_LIMBS_PER_FLT];
        int carry;
        double dd;

        nbits = IEEE_FLT_MANT_DIG; /* 24 */
        if (MPFR_UNLIKELY (e < -125))
            /*In the subnormal case, compute the exact number of significant bits*/
        {
            nbits += (125 + e);
            MPFR_ASSERTD (nbits >= 1);
        }
        np = MPFR_PREC2LIMBS (nbits);
        MPFR_ASSERTD(np <= MPFR_LIMBS_PER_FLT);
        carry = mpfr_round_raw_4 (tp, MPFR_MANT(src), MPFR_PREC(src), negative,
                                  nbits, rnd_mode);
        /* we perform the reconstruction using the 'double' type here,
           knowing the result is exactly representable as 'float' */
        if (MPFR_UNLIKELY(carry))
            dd = 1.0;
        else
        {
            /* The following computations are exact thanks to the previous
               mpfr_round_raw. */
            dd = (double) tp[0] / MP_BASE_AS_DOUBLE;
            for (i = 1 ; i < np ; i++)
                dd = (dd + tp[i]) / MP_BASE_AS_DOUBLE;
            /* dd is the mantissa (between 1/2 and 1) of the argument rounded
               to 24 bits */
        }
        dd = mpfr_scale2 (dd, e);
        if (negative)
            dd = -dd;

        /* convert (exacly) to float */
        d = (float) dd;
    }

    return d;
}