Blame sysdeps/ieee754/ldbl-128ibm/s_lroundl.c

Packit 6c4009
/* Round to long int long double floating-point values.
Packit 6c4009
   IBM extended format long double version.
Packit 6c4009
   Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math_ldbl_opt.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <ieee754.h>
Packit 6c4009
Packit 6c4009
long
Packit 6c4009
__lroundl (long double x)
Packit 6c4009
{
Packit 6c4009
  double xh, xl;
Packit 6c4009
  long res, hi, lo;
Packit 6c4009
Packit 6c4009
  ldbl_unpack (x, &xh, &xl);
Packit 6c4009
Packit 6c4009
  /* Limit the range of values handled by the conversion to long.
Packit 6c4009
     We do this because we aren't sure whether that conversion properly
Packit 6c4009
     raises FE_INVALID.  */
Packit 6c4009
  if (
Packit 6c4009
#if __LONG_MAX__ == 2147483647
Packit 6c4009
      __builtin_expect
Packit 6c4009
      ((__builtin_fabs (xh) <= (double) __LONG_MAX__ + 2), 1)
Packit 6c4009
#else
Packit 6c4009
      __builtin_expect
Packit 6c4009
      ((__builtin_fabs (xh) <= -(double) (-__LONG_MAX__ - 1)), 1)
Packit 6c4009
#endif
Packit 6c4009
#if !defined (FE_INVALID)
Packit 6c4009
      || 1
Packit 6c4009
#endif
Packit 6c4009
    )
Packit 6c4009
    {
Packit 6c4009
#if __LONG_MAX__ == 2147483647
Packit 6c4009
      long long llhi = (long long) xh;
Packit 6c4009
      if (llhi != (long) llhi)
Packit 6c4009
	hi = llhi < 0 ? -__LONG_MAX__ - 1 : __LONG_MAX__;
Packit 6c4009
      else
Packit 6c4009
	hi = llhi;
Packit 6c4009
      xh -= hi;
Packit 6c4009
#else
Packit 6c4009
      if (__glibc_unlikely ((xh == -(double) (-__LONG_MAX__ - 1))))
Packit 6c4009
	{
Packit 6c4009
	  /* When XH is 9223372036854775808.0, converting to long long will
Packit 6c4009
	     overflow, resulting in an invalid operation.  However, XL might
Packit 6c4009
	     be negative and of sufficient magnitude that the overall long
Packit 6c4009
	     double is in fact in range.  Avoid raising an exception.  In any
Packit 6c4009
	     case we need to convert this value specially, because
Packit 6c4009
	     the converted value is not exactly represented as a double
Packit 6c4009
	     thus subtracting HI from XH suffers rounding error.  */
Packit 6c4009
	  hi = __LONG_MAX__;
Packit 6c4009
	  xh = 1.0;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  hi = (long) xh;
Packit 6c4009
	  xh -= hi;
Packit 6c4009
	}
Packit 6c4009
#endif
Packit 6c4009
      ldbl_canonicalize (&xh, &xl);
Packit 6c4009
Packit 6c4009
      lo = (long) xh;
Packit 6c4009
Packit 6c4009
      /* Peg at max/min values, assuming that the above conversions do so.
Packit 6c4009
         Strictly speaking, we can return anything for values that overflow,
Packit 6c4009
         but this is more useful.  */
Packit 6c4009
      res = (long int) ((unsigned long int) hi + (unsigned long int) lo);
Packit 6c4009
Packit 6c4009
      /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
Packit 6c4009
      if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
Packit 6c4009
	goto overflow;
Packit 6c4009
Packit 6c4009
      xh -= lo;
Packit 6c4009
      ldbl_canonicalize (&xh, &xl);
Packit 6c4009
Packit 6c4009
      hi = res;
Packit 6c4009
      if (xh > 0.5)
Packit 6c4009
	{
Packit 6c4009
	  res += 1UL;
Packit 6c4009
	}
Packit 6c4009
      else if (xh == 0.5)
Packit 6c4009
	{
Packit 6c4009
	  if (xl > 0.0 || (xl == 0.0 && res >= 0))
Packit 6c4009
	    res += 1UL;
Packit 6c4009
	}
Packit 6c4009
      else if (-xh > 0.5)
Packit 6c4009
	{
Packit 6c4009
	  res -= 1UL;
Packit 6c4009
	}
Packit 6c4009
      else if (-xh == 0.5)
Packit 6c4009
	{
Packit 6c4009
	  if (xl < 0.0 || (xl == 0.0 && res <= 0))
Packit 6c4009
	    res -= 1UL;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
Packit 6c4009
	goto overflow;
Packit 6c4009
Packit 6c4009
      return res;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (xh > 0.0)
Packit 6c4009
	hi = __LONG_MAX__;
Packit 6c4009
      else if (xh < 0.0)
Packit 6c4009
	hi = -__LONG_MAX__ - 1;
Packit 6c4009
      else
Packit 6c4009
	/* Nan */
Packit 6c4009
	hi = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
overflow:
Packit 6c4009
#ifdef FE_INVALID
Packit 6c4009
  feraiseexcept (FE_INVALID);
Packit 6c4009
#endif
Packit 6c4009
  return hi;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
long_double_symbol (libm, __lroundl, lroundl);