00db10
Backport of this upstream commit (with libm-test.inc adjustments):
00db10
00db10
commit 98fb27a373f37554232e0060eef1a5bb00a07eb0
00db10
Author: Adhemerval Zanella <azanella@linux.vnet.ibm.com>
00db10
Date:   Fri Mar 14 12:27:52 2014 -0500
00db10
00db10
    PowerPC: remove wrong nearbyintl implementation for PPC64
00db10
    
00db10
    The nearbyintl assembly implementation
00db10
    (sysdeps/powerpc/powerpc64/fpu/s_nearbyintl.S)
00db10
    returns wrong results for some inputs where first double is a exact
00db10
    integer and the precision is determined by second long double.
00db10
    
00db10
    Checking on implementation comments and history, I am very confident the
00db10
    assembly implementation was based on a version before commit
00db10
    5c68d401698a58cf7da150d9cce769fa6679ba5f that fixes BZ#2423 (Errors in
00db10
    long double (ldbl-128ibm) rounding functions in glibc-2.4).
00db10
    
00db10
    By just removing the implementation and make the build select
00db10
    sysdeps/ieee754/ldbl-128ibm/s_nearbyintl.c instead fixes the failing
00db10
    math.
00db10
    
00db10
    Fixes BZ#16706.
00db10
00db10
Index: b/math/libm-test.inc
00db10
===================================================================
00db10
--- a/math/libm-test.inc
00db10
+++ b/math/libm-test.inc
00db10
@@ -7619,6 +7619,16 @@ nearbyint_test (void)
00db10
   TEST_f_f (nearbyint, -562949953421312.75, -562949953421313.0);
00db10
   TEST_f_f (nearbyint, -1125899906842624.75, -1125899906842625.0);
00db10
 #endif
00db10
+#ifdef TEST_LDOUBLE
00db10
+    /* Check cases where first double is a exact integer higher than 2^52 and
00db10
+       the precision is determined by second long double for IBM long double.  */
00db10
+    TEST_f_f (nearbyint,  34503599627370498.515625L, 34503599627370499.0L);
00db10
+    TEST_f_f (nearbyint, -34503599627370498.515625L, -34503599627370499.0L);
00db10
+# if LDBL_MANT_DIG >= 106
00db10
+    TEST_f_f (nearbyint,  1192568192774434123539907640624.484375L, 1192568192774434123539907640624.0L);
00db10
+    TEST_f_f (nearbyint, -1192568192774434123539907640624.484375L, -1192568192774434123539907640624.0L);
00db10
+# endif
00db10
+#endif
00db10
 
00db10
   END (nearbyint);
00db10
 }
00db10
Index: b/sysdeps/powerpc/powerpc64/fpu/s_nearbyintl.S
00db10
===================================================================
00db10
--- a/sysdeps/powerpc/powerpc64/fpu/s_nearbyintl.S
00db10
+++ /dev/null
00db10
@@ -1,113 +0,0 @@
00db10
-/* nearbyint long double.
00db10
-   IBM extended format long double version.
00db10
-   Copyright (C) 2004, 2006 Free Software Foundation, Inc.
00db10
-   This file is part of the GNU C Library.
00db10
-
00db10
-   The GNU C Library is free software; you can redistribute it and/or
00db10
-   modify it under the terms of the GNU Lesser General Public
00db10
-   License as published by the Free Software Foundation; either
00db10
-   version 2.1 of the License, or (at your option) any later version.
00db10
-
00db10
-   The GNU C Library is distributed in the hope that it will be useful,
00db10
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
-   Lesser General Public License for more details.
00db10
-
00db10
-   You should have received a copy of the GNU Lesser General Public
00db10
-   License along with the GNU C Library; if not, see
00db10
-   <http://www.gnu.org/licenses/>.  */
00db10
-
00db10
-#include <sysdep.h>
00db10
-#include <math_ldbl_opt.h>
00db10
-
00db10
-	.section	".toc","aw"
00db10
-.LC0:	/* 2**52 */
00db10
-	.tc FD_43300000_0[TC],0x4330000000000000
00db10
-	.section	".text"
00db10
-
00db10
-/* long double [fp1,fp2] nearbyintl (long double x [fp1,fp2])
00db10
-   IEEE 1003.1 nearbyintl function.  nearbyintl is simular to the rintl
00db10
-   but does raise the "inexact" exception.  This implementation is
00db10
-   based on rintl but explicitly maskes the inexact exception on entry
00db10
-   and clears any pending inexact before restoring the exception mask
00db10
-   on exit.
00db10
-
00db10
-   PowerPC64 long double uses the IBM extended format which is
00db10
-   represented two 64-floating point double values. The values are
00db10
-   non-overlapping giving an effective precision of 106 bits. The first
00db10
-   double contains the high order bits of mantisa and is always rounded
00db10
-   to represent a normal rounding of long double to double. Since the
00db10
-   long double value is sum of the high and low values, the low double
00db10
-   normally has the opposite sign to compensate for the this rounding.
00db10
-
00db10
-   For long double there are two cases:
00db10
-   1) |x| < 2**52, all the integer bits are in the high double.
00db10
-      floor the high double and set the low double to -0.0.
00db10
-   2) |x| >= 2**52, Rounding involves both doubles.
00db10
-      See the comment before lable .L2 for details.
00db10
-   */
00db10
-ENTRY (__nearbyintl)
00db10
-	mffs	fp11		/* Save current FPSCR.  */
00db10
-	lfd	fp13,.LC0@toc(2)
00db10
-	fabs	fp0,fp1
00db10
-	mtfsb0  28		/* Disable "inexact" exceptions.  */
00db10
-	fsub	fp12,fp13,fp13	/* generate 0.0  */
00db10
-	fabs	fp9,fp2
00db10
-	fcmpu	cr7,fp0,fp13	/* if (fabs(x) > TWO52)  */
00db10
-	fcmpu	cr6,fp1,fp12	/* if (x > 0.0)  */
00db10
-	bnl-	cr7,.L2
00db10
-	fmr	fp2,fp12
00db10
-	bng-	cr6,.L4
00db10
-	fadd	fp1,fp1,fp13	/* x+= TWO52;  */
00db10
-	fsub	fp1,fp1,fp13	/* x-= TWO52;  */
00db10
-	b	.L9
00db10
-.L4:
00db10
-	bnl-	cr6,.L9		/* if (x < 0.0)  */
00db10
-	fsub	fp1,fp13,fp1	/* x = TWO52 - x;  */
00db10
-	fsub	fp0,fp1,fp13	/* x = - (x - TWO52);  */
00db10
-	fneg	fp1,fp0
00db10
-.L9:
00db10
-	mtfsb0	6		/* Clear any pending "inexact" exceptions.  */
00db10
-	mtfsf	0x01,fp11	/* restore exception mask.  */
00db10
-	blr
00db10
-
00db10
-/* The high double is > TWO52 so we need to round the low double and
00db10
-   perhaps the high double.  This gets a bit tricky so we use the
00db10
-   following algorithm:
00db10
-
00db10
-   tau = floor(x_high/TWO52);
00db10
-   x0 = x_high - tau;
00db10
-   x1 = x_low + tau;
00db10
-   r1 = nearbyint(x1);
00db10
-   y_high = x0 + r1;
00db10
-   y_low = r1 - tau;
00db10
-   return y;  */
00db10
-.L2:
00db10
-	fcmpu	cr7,fp9,fp13	/* if (|x_low| > TWO52)  */
00db10
-	fcmpu	cr0,fp9,fp12	/* || (|x_low| == 0.0)  */
00db10
-	bge-	cr7,.L9		/*   return x;	*/
00db10
-	beq-  cr0,.L9
00db10
-	fdiv	fp8,fp1,fp13	/* x_high/TWO52  */
00db10
-	fctidz	fp0,fp8
00db10
-	fcfid	fp8,fp0		/* tau = floor(x_high/TWO52);  */
00db10
-	fsub	fp3,fp1,fp8	/* x0 = x_high - tau;  */
00db10
-	fadd	fp4,fp2,fp8	/* x1 = x_low + tau;  */
00db10
-
00db10
-	fcmpu	cr6,fp4,fp12	/* if (x1 > 0.0)  */
00db10
-	bng-	cr6,.L8
00db10
-	fadd	fp5,fp4,fp13	/* r1 = x1 + TWO52;  */
00db10
-	fsub	fp5,fp5,fp13	/* r1 = r1 - TWO52;  */
00db10
-	b	.L6
00db10
-.L8:
00db10
-	fmr	fp5,fp4
00db10
-	bge-	cr6,.L6		/* if (x1 < 0.0)  */
00db10
-	fsub	fp5,fp13,fp4	/* r1 = TWO52 - x1;  */
00db10
-	fsub	fp0,fp5,fp13	/* r1 = - (r1 - TWO52);  */
00db10
-	fneg	fp5,fp0
00db10
-.L6:
00db10
-	fadd	fp1,fp3,fp5	/* y_high = x0 + r1;  */
00db10
-	fsub	fp2,fp5,fp8	/* y_low = r1 - tau;  */
00db10
-	b	.L9
00db10
-END (__nearbyintl)
00db10
-
00db10
-long_double_symbol (libm, __nearbyintl, nearbyintl)