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-96 lroundl just below powers of 2 (bug 19071) [committed]


The ldbl-96 version of lroundl is incorrect for systems with 64-bit
long when the argument's absolute value is just below a power of 2,
2^32 or more, and rounds up to the next integer; in such cases, it
returns 0.  The problem is incrementing the high part of the mantissa
loses the high bit of the value (which is not an issue for any other
floating-point format, and is handled specially in lround when the bit
corresponding to 0.5 was in the high part rather than the low part).

This patch fixes this in a similar way to that used in llroundl:
storing the high part in an unsigned long variable before incrementing
it, so problems cannot occur in the case when this code is reachable.
I improved test coverage for both lround and llround by making them
use the same test inputs (appropriately conditioned on the size of
long in the lround case) - complete with the same comments, to make
comparison as easy as possible.  (This test coverage improvement was
how I found the lroundl bug.)

Tested for x86_64 and x86.  Committed.

2015-10-05  Joseph Myers  <joseph@codesourcery.com>

	[BZ #19071]
	* sysdeps/ieee754/ldbl-96/s_lroundl.c (__lroundl): Use unsigned
	long int variable to store possibly incremented high part of
	mantissa.
	* math/libm-test.inc (lround_test_data): Add tests used for
	llround.  Use [LONG_MAX > 0x7fffffff] consistently as condition
	for tests requiring 64-bit long.  Do not condition tests on
	[TEST_FLOAT] unnecessarily.
	(llround_test_data): Add tests used for lround.  Add another
	expectation for the "inexact" exception.  Do not condition tests
	on [TEST_FLOAT] unnecessarily.

diff --git a/math/libm-test.inc b/math/libm-test.inc
index ac1fe85..d3964ed 100644
--- a/math/libm-test.inc
+++ b/math/libm-test.inc
@@ -8099,25 +8099,168 @@ static const struct test_f_l_data lround_test_data[] =
     TEST_f_l (lround, 22514.5, 22515, ERRNO_UNCHANGED),
     TEST_f_l (lround, -22514.5, -22515, ERRNO_UNCHANGED),
     TEST_f_l (lround, 1071930.0008, 1071930, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 2097152.5, 2097153, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -2097152.5, -2097153, ERRNO_UNCHANGED),
 #ifndef TEST_FLOAT
     TEST_f_l (lround, 1073741824.01, 1073741824, ERRNO_UNCHANGED),
-# if LONG_MAX > 281474976710656
+# if LONG_MAX > 0x7fffffff
+    TEST_f_l (lround, 34359738368.5, 34359738369ll, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -34359738368.5, -34359738369ll, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -3.65309740835E17, -365309740835000000LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_f_l (lround, 281474976710656.025, 281474976710656, ERRNO_UNCHANGED),
     TEST_f_l (lround, 18014398509481974, 18014398509481974, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
 # endif
-    TEST_f_l (lround, 2097152.5, 2097153, ERRNO_UNCHANGED),
-    TEST_f_l (lround, -2097152.5, -2097153, ERRNO_UNCHANGED),
+#endif
+
+    /* Test boundary conditions.  */
+    /* 0x1FFFFF */
+    TEST_f_l (lround, 2097151.0, 2097151LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x800000 */
+    TEST_f_l (lround, 8388608.0, 8388608LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x1000000 */
+    TEST_f_l (lround, 16777216.0, 16777216LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+#if LONG_MAX > 0x7fffffff
+    /* 0x20000000000 */
+    TEST_f_l (lround, 2199023255552.0, 2199023255552LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x40000000000 */
+    TEST_f_l (lround, 4398046511104.0, 4398046511104LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x1000000000000 */
+    TEST_f_l (lround, 281474976710656.0, 281474976710656LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x10000000000000 */
+    TEST_f_l (lround, 4503599627370496.0, 4503599627370496LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x10000080000000 */
+    TEST_f_l (lround, 4503601774854144.0, 4503601774854144LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x20000000000000 */
+    TEST_f_l (lround, 9007199254740992.0, 9007199254740992LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x80000000000000 */
+    TEST_f_l (lround, 36028797018963968.0, 36028797018963968LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* 0x100000000000000 */
+    TEST_f_l (lround, 72057594037927936.0, 72057594037927936LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+#endif
+
+#ifndef TEST_FLOAT
+# if LONG_MAX > 0x7fffffff
+    /* 0x100000000 */
+    TEST_f_l (lround, 4294967295.5, 4294967296LL, ERRNO_UNCHANGED),
+    /* 0x200000000 */
+    TEST_f_l (lround, 8589934591.5, 8589934592LL, ERRNO_UNCHANGED),
+# endif
+
     /* nextafter(0.5,-1)  */
     TEST_f_l (lround, 0x1.fffffffffffffp-2, 0, ERRNO_UNCHANGED),
     /* nextafter(-0.5,1)  */
     TEST_f_l (lround, -0x1.fffffffffffffp-2, 0, ERRNO_UNCHANGED),
-#else
-    /* nextafter(0.5,-1)  */
+# if LONG_MAX > 0x7fffffff
+    /* On PowerPC an exponent of '52' is the largest incrementally
+     * representable sequence of whole-numbers in the 'double' range.  We test
+     * lround to make sure that a guard bit set during the lround operation
+     * hasn't forced an erroneous shift giving us an incorrect result.  The odd
+     * numbers between +-(2^52+1 and 2^53-1) are affected since they have the
+     * rightmost bit set.  */
+    /* +-(2^52+1)  */
+    TEST_f_l (lround, 0x1.0000000000001p+52,4503599627370497LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_l (lround, -0x1.0000000000001p+52,-4503599627370497LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* +-(2^53-1): Input is the last (positive and negative) incrementally
+     * representable whole-number in the 'double' range that might round
+     * erroneously.  */
+    TEST_f_l (lround, 0x1.fffffffffffffp+52, 9007199254740991LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_l (lround, -0x1.fffffffffffffp+52, -9007199254740991LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+# endif
+#endif
     TEST_f_l (lround, 0x1.fffffp-2, 0, ERRNO_UNCHANGED),
-    /* nextafter(-0.5,1)  */
+    /* nextafter(0.5,-1)  */
+    TEST_f_l (lround, 0x1.fffffep-2, 0, ERRNO_UNCHANGED),
     TEST_f_l (lround, -0x1.fffffp-2, 0, ERRNO_UNCHANGED),
+    /* nextafter(-0.5,1)  */
+    TEST_f_l (lround, -0x1.fffffep-2, 0, ERRNO_UNCHANGED),
     TEST_f_l (lround, 0x1.fffffep+23, 16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_f_l (lround, -0x1.fffffep+23, -16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    /* As above, on PowerPC an exponent of '23' is the largest incrementally
+     * representable sequence of whole-numbers in the 'float' range.
+     * Likewise, numbers between +-(2^23+1 and 2^24-1) are affected.  */
+    TEST_f_l (lround, 0x1.000002p+23,8388609, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_l (lround, -0x1.000002p+23,-8388609, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_l (lround, 0x1.fffffep+23, 16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_l (lround, -0x1.fffffep+23, -16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+
+#if LONG_MAX > 0x7fffffff
+# ifdef TEST_LDOUBLE
+    /* The input can only be represented in long double.  */
+    TEST_f_l (lround, 4503599627370495.5L, 4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370496.25L, 4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370496.5L, 4503599627370497LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370496.75L, 4503599627370497LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370497.5L, 4503599627370498LL, ERRNO_UNCHANGED),
+
+#  if LDBL_MANT_DIG > 100
+    TEST_f_l (lround, 4503599627370495.4999999999999L, 4503599627370495LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370496.4999999999999L, 4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370497.4999999999999L, 4503599627370497LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370494.5000000000001L, 4503599627370495LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370495.5000000000001L, 4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 4503599627370496.5000000000001L, 4503599627370497LL, ERRNO_UNCHANGED),
+
+    TEST_f_l (lround, -4503599627370495.4999999999999L, -4503599627370495LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370496.4999999999999L, -4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370497.4999999999999L, -4503599627370497LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370494.5000000000001L, -4503599627370495LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370495.5000000000001L, -4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370496.5000000000001L, -4503599627370497LL, ERRNO_UNCHANGED),
+#  endif
+
+    TEST_f_l (lround, -4503599627370495.5L, -4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370496.25L, -4503599627370496LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370496.5L, -4503599627370497LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370496.75L, -4503599627370497LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -4503599627370497.5L, -4503599627370498LL, ERRNO_UNCHANGED),
+
+    TEST_f_l (lround, 9007199254740991.5L, 9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740992.25L, 9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740992.5L, 9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740992.75L, 9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740993.5L, 9007199254740994LL, ERRNO_UNCHANGED),
+
+#  if LDBL_MANT_DIG > 100
+    TEST_f_l (lround, 9007199254740991.4999999999999L, 9007199254740991LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740992.4999999999999L, 9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740993.4999999999999L, 9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740991.5000000000001L, 9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740992.5000000000001L, 9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9007199254740993.5000000000001L, 9007199254740994LL, ERRNO_UNCHANGED),
+
+    TEST_f_l (lround, -9007199254740991.4999999999999L, -9007199254740991LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740992.4999999999999L, -9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740993.4999999999999L, -9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740991.5000000000001L, -9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740992.5000000000001L, -9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740993.5000000000001L, -9007199254740994LL, ERRNO_UNCHANGED),
+#  endif
+
+    TEST_f_l (lround, -9007199254740991.5L, -9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740992.25L, -9007199254740992LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740992.5L, -9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740992.75L, -9007199254740993LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9007199254740993.5L, -9007199254740994LL, ERRNO_UNCHANGED),
+
+    TEST_f_l (lround, 72057594037927935.5L, 72057594037927936LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 72057594037927936.25L, 72057594037927936LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 72057594037927936.5L, 72057594037927937LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 72057594037927936.75L, 72057594037927937LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 72057594037927937.5L, 72057594037927938LL, ERRNO_UNCHANGED),
+
+    TEST_f_l (lround, -72057594037927935.5L, -72057594037927936LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -72057594037927936.25L, -72057594037927936LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -72057594037927936.5L, -72057594037927937LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -72057594037927936.75L, -72057594037927937LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -72057594037927937.5L, -72057594037927938LL, ERRNO_UNCHANGED),
+
+    TEST_f_l (lround, 9223372036854775806.25L, 9223372036854775806LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9223372036854775806.25L, -9223372036854775806LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9223372036854775806.5L, 9223372036854775807LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9223372036854775806.5L, -9223372036854775807LL, ERRNO_UNCHANGED),
+    TEST_f_l (lround, 9223372036854775807.0L, 9223372036854775807LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_l (lround, -9223372036854775807.0L, -9223372036854775807LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+# endif
 #endif
   };
 
@@ -8147,12 +8290,15 @@ static const struct test_f_L_data llround_test_data[] =
     TEST_f_L (llround, 22514.5, 22515, ERRNO_UNCHANGED),
     TEST_f_L (llround, -22514.5, -22515, ERRNO_UNCHANGED),
     TEST_f_L (llround, 1071930.0008, 1071930, ERRNO_UNCHANGED),
-#ifndef TEST_FLOAT
     TEST_f_L (llround, 2097152.5, 2097153, ERRNO_UNCHANGED),
     TEST_f_L (llround, -2097152.5, -2097153, ERRNO_UNCHANGED),
+#ifndef TEST_FLOAT
+    TEST_f_L (llround, 1073741824.01, 1073741824, ERRNO_UNCHANGED),
     TEST_f_L (llround, 34359738368.5, 34359738369ll, ERRNO_UNCHANGED),
     TEST_f_L (llround, -34359738368.5, -34359738369ll, ERRNO_UNCHANGED),
-    TEST_f_L (llround, -3.65309740835E17, -365309740835000000LL, ERRNO_UNCHANGED),
+    TEST_f_L (llround, -3.65309740835E17, -365309740835000000LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_L (llround, 281474976710656.025, 281474976710656, ERRNO_UNCHANGED),
+    TEST_f_L (llround, 18014398509481974, 18014398509481974, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
 #endif
 
     /* Test boundary conditions.  */
@@ -8203,11 +8349,15 @@ static const struct test_f_L_data llround_test_data[] =
      * erroneously.  */
     TEST_f_L (llround, 0x1.fffffffffffffp+52, 9007199254740991LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_f_L (llround, -0x1.fffffffffffffp+52, -9007199254740991LL, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
-#else
+#endif
+    TEST_f_L (llround, 0x1.fffffp-2, 0, ERRNO_UNCHANGED),
     /* nextafter(0.5,-1)  */
     TEST_f_L (llround, 0x1.fffffep-2, 0, ERRNO_UNCHANGED),
+    TEST_f_L (llround, -0x1.fffffp-2, 0, ERRNO_UNCHANGED),
     /* nextafter(-0.5,1)  */
     TEST_f_L (llround, -0x1.fffffep-2, 0, ERRNO_UNCHANGED),
+    TEST_f_L (llround, 0x1.fffffep+23, 16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
+    TEST_f_L (llround, -0x1.fffffep+23, -16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     /* As above, on PowerPC an exponent of '23' is the largest incrementally
      * representable sequence of whole-numbers in the 'float' range.
      * Likewise, numbers between +-(2^23+1 and 2^24-1) are affected.  */
@@ -8215,8 +8365,6 @@ static const struct test_f_L_data llround_test_data[] =
     TEST_f_L (llround, -0x1.000002p+23,-8388609, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_f_L (llround, 0x1.fffffep+23, 16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
     TEST_f_L (llround, -0x1.fffffep+23, -16777215, NO_INEXACT_EXCEPTION|ERRNO_UNCHANGED),
-#endif
-
 
 #ifdef TEST_LDOUBLE
     /* The input can only be represented in long double.  */
diff --git a/sysdeps/ieee754/ldbl-96/s_lroundl.c b/sysdeps/ieee754/ldbl-96/s_lroundl.c
index e34edd3..7a59835 100644
--- a/sysdeps/ieee754/ldbl-96/s_lroundl.c
+++ b/sysdeps/ieee754/ldbl-96/s_lroundl.c
@@ -58,13 +58,15 @@ __lroundl (long double x)
       else
 	{
 	  u_int32_t j = i1 + (0x80000000 >> (j0 - 31));
+	  unsigned long int ures = i0;
+
 	  if (j < i1)
-	    ++i0;
+	    ++ures;
 
 	  if (j0 == 31)
-	    result = (long int) i0;
+	    result = ures;
 	  else
-	    result = ((long int) i0 << (j0 - 31)) | (j >> (63 - j0));
+	    result = (ures << (j0 - 31)) | (j >> (63 - j0));
 	}
     }
   else

-- 
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]