Back to index

glibc  2.9
Functions
s_llrintl.c File Reference
#include <math.h>
#include <fenv_libc.h>
#include <math_ldbl_opt.h>
#include <float.h>
#include <ieee754.h>

Go to the source code of this file.

Functions

long long __llrintl (long double x)
 long_double_symbol (libm, __llrintl, llrintl)

Function Documentation

long long __llrintl ( long double  x)

Definition at line 33 of file s_llrintl.c.

{
  double xh, xl;
  long long res, hi, lo;
  int save_round;

  ldbl_unpack (x, &xh, &xl);

  /* Limit the range of values handled by the conversion to long long.
     We do this because we aren't sure whether that conversion properly
     raises FE_INVALID.  */
  if (__builtin_expect
      ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
#if !defined (FE_INVALID)
      || 1
#endif
    )
    {
      save_round = fegetround ();

      if (__builtin_expect ((xh == -(double) (-__LONG_LONG_MAX__ - 1)), 0))
       {
         /* When XH is 9223372036854775808.0, converting to long long will
            overflow, resulting in an invalid operation.  However, XL might
            be negative and of sufficient magnitude that the overall long
            double is in fact in range.  Avoid raising an exception.  In any
            case we need to convert this value specially, because
            the converted value is not exactly represented as a double
            thus subtracting HI from XH suffers rounding error.  */
         hi = __LONG_LONG_MAX__;
         xh = 1.0;
       }
      else
       {
         hi = (long long) xh;
         xh -= hi;
       }
      ldbl_canonicalize (&xh, &xl);

      lo = (long long) xh;

      /* Peg at max/min values, assuming that the above conversions do so.
         Strictly speaking, we can return anything for values that overflow,
         but this is more useful.  */
      res = hi + lo;

      /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
      if (__builtin_expect (((~(hi ^ lo) & (res ^ hi)) < 0), 0))
       goto overflow;

      xh -= lo;
      ldbl_canonicalize (&xh, &xl);

      hi = res;
      switch (save_round)
       {
       case FE_TONEAREST:
         if (fabs (xh) < 0.5
             || (fabs (xh) == 0.5
                && ((xh > 0.0 && xl < 0.0)
                    || (xh < 0.0 && xl > 0.0)
                    || (xl == 0.0 && (res & 1) == 0))))
           return res;

         if (xh < 0.0)
           res -= 1;
         else
           res += 1;
         break;

       case FE_TOWARDZERO:
         if (res > 0 && (xh < 0.0 || (xh == 0.0 && xl < 0.0)))
           res -= 1;
         else if (res < 0 && (xh > 0.0 || (xh == 0.0 && xl > 0.0)))
           res += 1;
         return res;
         break;

       case FE_UPWARD:
         if (xh > 0.0 || (xh == 0.0 && xl > 0.0))
           res += 1;
         break;

       case FE_DOWNWARD:
         if (xh < 0.0 || (xh == 0.0 && xl < 0.0))
           res -= 1;
         break;
       }

      if (__builtin_expect (((~(hi ^ (res - hi)) & (res ^ hi)) < 0), 0))
       goto overflow;

      return res;
    }
  else
    {
      if (xh > 0.0)
       hi = __LONG_LONG_MAX__;
      else if (xh < 0.0)
       hi = -__LONG_LONG_MAX__ - 1;
      else
       /* Nan */
       hi = 0;
    }

overflow:
#ifdef FE_INVALID
  feraiseexcept (FE_INVALID);
#endif
  return hi;
}

Here is the call graph for this function:

long_double_symbol ( libm  ,
__llrintl  ,
llrintl   
)