This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fix ldbl-128 roundl for exponents in [31, 47] (bug 18346) [committed]


The implementation of roundl for ldbl-128 involves undefined behavior
for arguments with exponents from 31 to 47 inclusive, from the shift:

      u_int64_t i = -1ULL >> (j0 - 48);

For example, on mips64, this means roundl (0xffffffffffff.8p0L)
wrongly returns its argument, which is not an integer.  A condition
checking for exponents < 31 should actually be checking for exponents
< 48, and this patch makes it do so.  (That condition is for whether
the bit representing 0.5 is in the high 64-bit half of the
floating-point number.  The value 31 might have arisen from an
incorrect conversion of the ldbl-96 version to handle ldbl-128.)

This was originally reported as a GCC libquadmath bug
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65757>.

Tested for mips64; also tested for x86_64 and x86 to make sure the new
tests pass there.  Committed.

2015-04-28  Joseph Myers  <joseph@codesourcery.com>

	[BZ #18346]
	* sysdeps/ieee754/ldbl-128/s_roundl.c (__roundl): Handle all
	exponents less than 48 as cases where high part of mantissa needs
	examining to determine whether argument is integral.
	* math/libm-test.inc (round_test_data): Add more tests.

diff --git a/math/libm-test.inc b/math/libm-test.inc
index 5d7f5a2..4cade14 100644
--- a/math/libm-test.inc
+++ b/math/libm-test.inc
@@ -9012,6 +9012,17 @@ static const struct test_f_f_data round_test_data[] =
     TEST_f_f (round, 2097152.5, 2097153),
     TEST_f_f (round, -2097152.5, -2097153),
 
+#ifndef TEST_FLOAT
+    TEST_f_f (round, 0xffffffffffff.0p0L, 0xffffffffffff.0p0L),
+    TEST_f_f (round, 0xffffffffffff.4p0L, 0xffffffffffff.0p0L),
+    TEST_f_f (round, 0xffffffffffff.8p0L, 0x1000000000000.0p0L),
+    TEST_f_f (round, 0xffffffffffff.cp0L, 0x1000000000000.0p0L),
+    TEST_f_f (round, -0xffffffffffff.0p0L, -0xffffffffffff.0p0L),
+    TEST_f_f (round, -0xffffffffffff.4p0L, -0xffffffffffff.0p0L),
+    TEST_f_f (round, -0xffffffffffff.8p0L, -0x1000000000000.0p0L),
+    TEST_f_f (round, -0xffffffffffff.cp0L, -0x1000000000000.0p0L),
+#endif
+
 #ifdef TEST_LDOUBLE
     /* The result can only be represented in long double.  */
     TEST_f_f (round, 4503599627370495.5L, 4503599627370496.0L),
diff --git a/sysdeps/ieee754/ldbl-128/s_roundl.c b/sysdeps/ieee754/ldbl-128/s_roundl.c
index 9df0092..3ff6ebe 100644
--- a/sysdeps/ieee754/ldbl-128/s_roundl.c
+++ b/sysdeps/ieee754/ldbl-128/s_roundl.c
@@ -34,7 +34,7 @@ __roundl (long double x)
 
   GET_LDOUBLE_WORDS64 (i0, i1, x);
   j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
-  if (j0 < 31)
+  if (j0 < 48)
     {
       if (j0 < 0)
 	{

-- 
Joseph S. Myers
joseph@codesourcery.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]