mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-12 14:20:13 +00:00
8afdb7ac1e
The dbl-64, ldbl-96 and ldbl-128 implementations of lround and llround fail to produce "invalid" exceptions in cases where the rounded result overflows the target type, but truncating the floating-point argument to the next integer towards zero does not overflow it (so in particular casts do not produce such exceptions). (This issue cannot arise for float, or for double with 64-bit target type, or for ldbl-96 with 64-bit target type and negative arguments, because of insufficient precision in the floating-point type for arguments with the relevant property to exist.) This patch fixes these problems by inserting checks for the special cases that can occur in each implementation, and explicitly raising FE_INVALID (and avoiding the cast if it might raise spurious FE_INEXACT). Tested for x86_64, x86 and mips64. [BZ #19088] * sysdeps/ieee754/dbl-64/s_lround.c: Include <fenv.h> and <limits.h>. (__lround) [FE_INVALID]: Force FE_INVALID exception when result overflows but exception would not result from cast. * sysdeps/ieee754/dbl-64/wordsize-64/s_lround.c: Include <fenv.h> and <limits.h>. (__lround) [FE_INVALID]: Force FE_INVALID exception when result overflows but exception would not result from cast. * sysdeps/ieee754/ldbl-128/s_llroundl.c: Include <fenv.h> and <limits.h>. (__llroundl) [FE_INVALID]: Force FE_INVALID exception when result overflows but exception would not result from cast. * sysdeps/ieee754/ldbl-128/s_lroundl.c: Include <fenv.h> and <limits.h>. (__lroundl) [FE_INVALID]: Force FE_INVALID exception when result overflows but exception would not result from cast. * sysdeps/ieee754/ldbl-96/s_llroundl.c: Include <fenv.h> and <limits.h>. (__llroundl) [FE_INVALID]: Force FE_INVALID exception when result overflows but exception would not result from cast. * sysdeps/ieee754/ldbl-96/s_lroundl.c: Include <fenv.h> and <limits.h>. (__lroundl) [FE_INVALID]: Force FE_INVALID exception when result overflows but exception would not result from cast. * math/libm-test.inc (lround_test_data): Add more tests. (llround_test_data): Likewise.
112 lines
2.6 KiB
C
112 lines
2.6 KiB
C
/* Round long double value to long int.
|
|
Copyright (C) 1997-2015 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
|
|
|
|
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 <fenv.h>
|
|
#include <limits.h>
|
|
#include <math.h>
|
|
|
|
#include <math_private.h>
|
|
|
|
|
|
long int
|
|
__lroundl (long double x)
|
|
{
|
|
int32_t j0;
|
|
u_int32_t se, i1, i0;
|
|
long int result;
|
|
int sign;
|
|
|
|
GET_LDOUBLE_WORDS (se, i0, i1, x);
|
|
j0 = (se & 0x7fff) - 0x3fff;
|
|
sign = (se & 0x8000) != 0 ? -1 : 1;
|
|
|
|
if (j0 < 31)
|
|
{
|
|
if (j0 < 0)
|
|
return j0 < -1 ? 0 : sign;
|
|
else
|
|
{
|
|
u_int32_t j = i0 + (0x40000000 >> j0);
|
|
if (j < i0)
|
|
{
|
|
j >>= 1;
|
|
j |= 0x80000000;
|
|
++j0;
|
|
}
|
|
|
|
result = j >> (31 - j0);
|
|
#ifdef FE_INVALID
|
|
if (sizeof (long int) == 4
|
|
&& sign == 1
|
|
&& result == LONG_MIN)
|
|
/* Rounding brought the value out of range. */
|
|
feraiseexcept (FE_INVALID);
|
|
#endif
|
|
}
|
|
}
|
|
else if (j0 < (int32_t) (8 * sizeof (long int)) - 1)
|
|
{
|
|
if (j0 >= 63)
|
|
result = ((long int) i0 << (j0 - 31)) | (i1 << (j0 - 63));
|
|
else
|
|
{
|
|
u_int32_t j = i1 + (0x80000000 >> (j0 - 31));
|
|
unsigned long int ures = i0;
|
|
|
|
if (j < i1)
|
|
++ures;
|
|
|
|
if (j0 == 31)
|
|
result = ures;
|
|
else
|
|
{
|
|
result = (ures << (j0 - 31)) | (j >> (63 - j0));
|
|
#ifdef FE_INVALID
|
|
if (sizeof (long int) == 8
|
|
&& sign == 1
|
|
&& result == LONG_MIN)
|
|
/* Rounding brought the value out of range. */
|
|
feraiseexcept (FE_INVALID);
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* The number is too large. Unless it rounds to LONG_MIN,
|
|
FE_INVALID must be raised and the return value is
|
|
unspecified. */
|
|
#ifdef FE_INVALID
|
|
if (sizeof (long int) == 4
|
|
&& x <= (long double) LONG_MIN - 0.5L)
|
|
{
|
|
/* If truncation produces LONG_MIN, the cast will not raise
|
|
the exception, but may raise "inexact". */
|
|
feraiseexcept (FE_INVALID);
|
|
return LONG_MIN;
|
|
}
|
|
#endif
|
|
return (long int) x;
|
|
}
|
|
|
|
return sign * result;
|
|
}
|
|
|
|
weak_alias (__lroundl, lroundl)
|