glibc/sysdeps/ieee754/ldbl-128ibm/s_lroundl.c
Joseph Myers 1272748886 Fix ldbl-128ibm lrintl, lroundl missing "invalid" exceptions (bug 22690).
The ldbl-128ibm implementations of lrintl and lroundl are missing
"invalid" exceptions for certain overflow cases when compiled with GCC
8.  The cause of this is after-the-fact integer overflow checks that
fail when the compiler optimizes on the basis of integer overflow
being undefined; GCC 8 must be able to detect new cases of
undefinedness here.

Failure: lrint (-0x80000001p0): Exception "Invalid operation" not set
Failure: lrint_downward (-0x80000001p0): Exception "Invalid operation" not set
Failure: lrint_towardzero (-0x80000001p0): Exception "Invalid operation" not set
Failure: lrint_upward (-0x80000001p0): Exception "Invalid operation" not set

Failure: lround (-0x80000001p0): Exception "Invalid operation" not set
Failure: lround_downward (-0x80000001p0): Exception "Invalid operation" not set
Failure: lround_towardzero (-0x80000001p0): Exception "Invalid operation" not set
Failure: lround_upward (-0x80000001p0): Exception "Invalid operation" not set

(Tested that these failures occur before the patch for powerpc
soft-float, but the issue applies in principle for hard-float as well,
whether or not the particular optimizations in fact occur there at
present.)

This patch fixes the bug by ensuring the additions / subtractions in
question cast arguments to unsigned long int, or use 1UL as a constant
argument, so that the arithmetic occurs in an unsigned type with the
result then converted back to a signed type.

Tested for powerpc (soft-float).

	[BZ #22690]
	* sysdeps/ieee754/ldbl-128ibm/s_lrintl.c (__lrintl): Use unsigned
	long int for arguments of possibly overflowing addition or
	subtraction.
	* sysdeps/ieee754/ldbl-128ibm/s_lroundl.c (__lroundl): Likewise.
2018-01-10 00:02:35 +00:00

136 lines
3.5 KiB
C

/* Round to long int long double floating-point values.
IBM extended format long double version.
Copyright (C) 2006-2018 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <math.h>
#include <fenv.h>
#include <math_ldbl_opt.h>
#include <float.h>
#include <ieee754.h>
long
__lroundl (long double x)
{
double xh, xl;
long res, hi, lo;
ldbl_unpack (x, &xh, &xl);
/* Limit the range of values handled by the conversion to long.
We do this because we aren't sure whether that conversion properly
raises FE_INVALID. */
if (
#if __LONG_MAX__ == 2147483647
__builtin_expect
((__builtin_fabs (xh) <= (double) __LONG_MAX__ + 2), 1)
#else
__builtin_expect
((__builtin_fabs (xh) <= -(double) (-__LONG_MAX__ - 1)), 1)
#endif
#if !defined (FE_INVALID)
|| 1
#endif
)
{
#if __LONG_MAX__ == 2147483647
long long llhi = (long long) xh;
if (llhi != (long) llhi)
hi = llhi < 0 ? -__LONG_MAX__ - 1 : __LONG_MAX__;
else
hi = llhi;
xh -= hi;
#else
if (__glibc_unlikely ((xh == -(double) (-__LONG_MAX__ - 1))))
{
/* 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_MAX__;
xh = 1.0;
}
else
{
hi = (long) xh;
xh -= hi;
}
#endif
ldbl_canonicalize (&xh, &xl);
lo = (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 = (long int) ((unsigned long int) hi + (unsigned long int) lo);
/* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
goto overflow;
xh -= lo;
ldbl_canonicalize (&xh, &xl);
hi = res;
if (xh > 0.5)
{
res += 1UL;
}
else if (xh == 0.5)
{
if (xl > 0.0 || (xl == 0.0 && res >= 0))
res += 1UL;
}
else if (-xh > 0.5)
{
res -= 1UL;
}
else if (-xh == 0.5)
{
if (xl < 0.0 || (xl == 0.0 && res <= 0))
res -= 1UL;
}
if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
goto overflow;
return res;
}
else
{
if (xh > 0.0)
hi = __LONG_MAX__;
else if (xh < 0.0)
hi = -__LONG_MAX__ - 1;
else
/* Nan */
hi = 0;
}
overflow:
#ifdef FE_INVALID
feraiseexcept (FE_INVALID);
#endif
return hi;
}
long_double_symbol (libm, __lroundl, lroundl);