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]

Re: [PATCH] PowerPC - Fix PPC64 floorl


On 04/20/2012 05:54 PM, Adhemerval Zanella wrote:
> On 04/19/2012 06:26 PM, Joseph S. Myers wrote:
>> On Thu, 19 Apr 2012, Steven Munroe wrote:
>>
>>>> I don't see how that can be correct.  If the high part is small, but an 
>>>> integer, the result of this function should still depend on whether the 
>>>> low part is positive, negative or zero.  Presumably the testsuite failed 
>>>> to detect that bug in this patch, meaning you also need tests (conditioned 
>>>> on TEST_LDOUBLE - they can be written to be valid for all long double 
>>>> formats) for values that when rounded to 53 bits are an integer, but 
>>>> differ from the integer value in lower bits (covering at least the four 
>>>> cases of positive and negative value, above and below the exact integer it 
>>>> is close to).
>>> Are you concerned that xh may be "prerounded" away (from default
>>> rounding mode)? This is not what we want for floor. In which case we
>>> should "deround" xh before the __floor operation.
>>> Perhaps:
>>>
>>> +  if (__builtin_expect(__builtin_fabs (x) < two52, 1))
>>> +      return ldbl_pack (__floor (xh+xl), -0.0);
>> That won't help either.
>>
>> In a valid long double, xh is always the value of the long double, rounded 
>> to nearest, independent of the current rounding direction; that's the 
>> definition of the format.  The result of floorl doesn't depend on the 
>> current rounding direction either; it always rounds to an integer value in 
>> the direction of negative infinity.
>>
>> If xh is an integer, and xl is positive or zero with absolute value less 
>> than 1, xh is the correct result of floorl.  If xh is an integer, and xl 
>> is negative with absolute value less than 1, the correct result of floorl 
>> is xh - 1.  (If xl has absolute value at least 1, which means xh is large, 
>> then the correct result is xh + floor(xl); that formula is indeed correct 
>> whenever xh is an integer.  And if xh is not an integer, the correct 
>> result is floor (xh).)
>>
>> The sort of case where I expect this patch will get things wrong is floorl 
>> (0x1p51L - 0x1p-10L) or floorl (-0x1p51L - 0x1p-10L).
>>
> Indeed the cases you noted failed with both the old assembly and optimization
> I added based on the assembly. This patch now just use the C version and
> remove the wrong assembly version.
>
> --
>
> [PATCH] PowerPC - Fix PPC64 floorl
>
> The assembly version fails to correctly floorl some edge cases. The patch
> removes it and use the C version.
>
>
> 2012-04-17  Adhemerval Zanella  <azanella@linux.vnet.ibm.com>
>
> 	[BZ #3866]
> 	* sysdeps/powerpc/powerpc64/fpu/s_floorl.S: Delete file. Using the
> 	sysdeps/ieee754/ldbl-128ibm/s_floorl.c version.
> 	* math/libm-test.inc: (floor_test): Add more tests.
> 	* sysdeps/powerpc/fpu/libm-test-ulps: Update for pow tests.
>
> --
>
> diff --git a/math/libm-test.inc b/math/libm-test.inc
> index 94d60bf..e91aa26 100644
> --- a/math/libm-test.inc
> +++ b/math/libm-test.inc
> @@ -3442,6 +3442,9 @@ floor_test (void)
>    TEST_f_f (floor, 10141204801825835211973625643008.5L, 10141204801825835211973625643008.0L);
>    TEST_f_f (floor, 10141204801825835211973625643008.75L, 10141204801825835211973625643008.0L);
>    TEST_f_f (floor, 10141204801825835211973625643009.5L, 10141204801825835211973625643009.0L);
> +
> +  TEST_f_f (floor, 0xf.ffffffffffffff8p+47L, 0xf.fffffffffffep+47L);              
> +  TEST_f_f (floor, -0x8.000000000000004p+48L, -0x8.000000000001p+48L);
>  #endif
>
>    END (floor);
> diff --git a/sysdeps/powerpc/fpu/libm-test-ulps b/sysdeps/powerpc/fpu/libm-test-ulps
> index 1ab3d4a..c555297 100644
> --- a/sysdeps/powerpc/fpu/libm-test-ulps
> +++ b/sysdeps/powerpc/fpu/libm-test-ulps
> @@ -1488,6 +1488,17 @@ Test "log2 (e) == M_LOG2El":
>  ildouble: 1
>  ldouble: 1
>
> +# pow
> +Test "pow (0x0.ffffffp0, -0x1p24) == 2.7182819094701610539628664526874952929416":
> +float: 1
> +ifloat: 1
> +Test "pow (0x0.ffffffp0, 0x1p24) == 0.3678794302077803437135155590023422899744":
> +float: 1
> +ifloat: 1
> +Test "pow (0x1.000002p0, 0x1p24) == 7.3890552180866447284268641248075832310141":
> +float: 1
> +ifloat: 1
> +
>  # pow_downward
>  Test "pow_downward (1.0625, 1.125) == 1.070582293028761362162622578677070098674":
>  ildouble: 1
> @@ -2448,6 +2459,8 @@ ildouble: 1
>  ldouble: 1
>
>  Function: "pow":
> +float: 1
> +ifloat: 1
>  ildouble: 1
>  ldouble: 1
>
> diff --git a/sysdeps/powerpc/powerpc64/fpu/s_floorl.S b/sysdeps/powerpc/powerpc64/fpu/s_floorl.S
> deleted file mode 100644
> index 578be43..0000000
> --- a/sysdeps/powerpc/powerpc64/fpu/s_floorl.S
> +++ /dev/null
> @@ -1,133 +0,0 @@
> -/* long double floor function.
> -   IBM extended format long double version.
> -   Copyright (C) 2004, 2006 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 <sysdep.h>
> -#include <math_ldbl_opt.h>
> -
> -	.section	".toc","aw"
> -.LC0:	/* 2**52 */
> -	.tc FD_43300000_0[TC],0x4330000000000000
> -
> -	.section	".text"
> -/* long double [fp1,fp2] floorl (long double x [fp1,fp2])
> -   IEEE 1003.1 floor function.
> -
> -   PowerPC64 long double uses the IBM extended format which is
> -   represented two 64-floating point double values. The values are
> -   non-overlapping giving an effective precision of 106 bits. The first
> -   double contains the high order bits of mantisa and is always rounded
> -   to represent a normal rounding of long double to double. Since the
> -   long double value is sum of the high and low values, the low double
> -   normally has the opposite sign to compensate for the this rounding.
> -
> -   For long double there are two cases:
> -   1) |x| < 2**52, all the integer bits are in the high double.
> -      floor the high double and set the low double to -0.0.
> -   2) |x| >= 2**52, Rounding involves both doubles.
> -      See the comment before lable .L2 for details.
> -   */
> -
> -ENTRY (__floorl)
> -	mffs	fp11		/* Save current FPU rounding mode.  */
> -	lfd	fp13,.LC0@toc(2)
> -	fabs	fp0,fp1
> -	fabs	fp9,fp2
> -	fsub	fp12,fp13,fp13	/* generate 0.0  */
> -	fcmpu	cr7,fp0,fp13	/* if (fabs(x) > TWO52)  */
> -	fcmpu	cr6,fp1,fp12	/* if (x > 0.0)  */
> -	bnl-	cr7,.L2
> -	mtfsfi	7,3		/* Set rounding mode toward -inf.  */
> -	fneg	fp2,fp12	/* set low double to -0.0.  */
> -	ble-	cr6,.L0
> -	fadd	fp1,fp1,fp13	/* x+= TWO52;  */
> -	fsub	fp1,fp1,fp13	/* x-= TWO52;  */
> -	fcmpu	cr5,fp1,fp12	/* if (x > 0.0)  */
> -	mtfsf	0x01,fp11	/* restore previous rounding mode.  */
> -	bnelr+	cr5
> -	fmr	fp1,fp12	/* x must be +0.0 for the 0.0 case.  */
> -	blr
> -.L0:
> -	bge-	cr6,.L1		/* if (x < 0.0)  */
> -	fsub	fp1,fp1,fp13	/* x-= TWO52;  */
> -	fadd	fp1,fp1,fp13	/* x+= TWO52;  */
> -.L1:
> -	mtfsf	0x01,fp11	/* restore previous rounding mode.  */
> -	blr
> -	
> -
> -/* The high double is > TWO52 so we need to round the low double and
> -   perhaps the high double.  In this case we have to round the low
> -   double and handle any adjustment to the high double that may be
> -   caused by rounding (up).  This is complicated by the fact that the
> -   high double may already be rounded and the low double may have the
> -   opposite sign to compensate.This gets a bit tricky so we use the
> -   following algorithm:
> -
> -   tau = floor(x_high/TWO52);
> -   x0 = x_high - tau;
> -   x1 = x_low + tau;
> -   r1 = rint(x1);
> -   y_high = x0 + r1;
> -   y_low = x0 - y_high + r1;
> -   return y;  */
> -.L2:
> -	fcmpu	cr7,fp9,fp13	/* if (|x_low| > TWO52)  */
> -	fcmpu	cr0,fp9,fp12	/* || (|x_low| == 0.0)  */
> -	fcmpu	cr5,fp2,fp12	/* if (x_low > 0.0)  */
> -	bgelr-	cr7		/*   return x;	*/
> -	beqlr-  cr0
> -	mtfsfi	7,3		/* Set rounding mode toward -inf.  */
> -	fdiv	fp8,fp1,fp13	/* x_high/TWO52  */
> -	
> -	bng-	cr6,.L6		/* if (x > 0.0)  */
> -	fctidz	fp0,fp8
> -	fcfid	fp8,fp0		/* tau = floor(x_high/TWO52);  */
> -	bng	cr5,.L4		/* if (x_low > 0.0)  */
> -	fmr	fp3,fp1
> -	fmr	fp4,fp2
> -	b	.L5
> -.L4:				/* if (x_low < 0.0)  */
> -	fsub	fp3,fp1,fp8	/* x0 = x_high - tau;  */
> -	fadd	fp4,fp2,fp8	/* x1 = x_low + tau;  */
> -.L5:
> -	fadd	fp5,fp4,fp13	/* r1 = r1 + TWO52;  */
> -	fsub	fp5,fp5,fp13	/* r1 = r1 - TWO52;  */
> -	b	.L9
> -.L6:				/* if (x < 0.0)  */
> -	fctidz	fp0,fp8
> -	fcfid	fp8,fp0		/* tau = floor(x_high/TWO52);  */	
> -	bnl	cr5,.L7		/* if (x_low < 0.0)  */
> -	fmr	fp3,fp1
> -	fmr	fp4,fp2
> -	b	.L8
> -.L7:				/* if (x_low > 0.0)  */
> -	fsub	fp3,fp1,fp8	/* x0 = x_high - tau;  */
> -	fadd	fp4,fp2,fp8	/* x1 = x_low + tau;  */
> -.L8:
> -	fsub	fp5,fp4,fp13	/* r1-= TWO52;  */
> -	fadd	fp5,fp5,fp13	/* r1+= TWO52;  */
> -.L9:
> -	mtfsf	0x01,fp11	/* restore previous rounding mode.  */
> -	fadd	fp1,fp3,fp5	/* y_high = x0 + r1;  */
> -	fsub	fp2,fp3,fp1	/* y_low = x0 - y_high + r1;  */
> -	fadd	fp2,fp2,fp5
> -	blr
> -END (__floorl)
> -
> -long_double_symbol (libm, __floorl, floorl)
Although I found out the issue after bz#3866, there is already a bz entry reporting
this issue (bz#13886). New ChangeLog entry (the patch is the same).

---

2012-04-17  Adhemerval Zanella  <azanella@linux.vnet.ibm.com>

	[BZ #13886]
	* sysdeps/powerpc/powerpc64/fpu/s_floorl.S: Delete file. Using the
	sysdeps/ieee754/ldbl-128ibm/s_floorl.c version.
	* math/libm-test.inc: (floor_test): Add more tests.
	* sysdeps/powerpc/fpu/libm-test-ulps: Update for pow tests.



-- 
Adhemerval Zanella Netto
  Software Engineer
  Linux Technology Center Brazil
  Toolchain / GLIBC on Power Architecture
  azanella@linux.vnet.ibm.com / azanella@br.ibm.com
  +55 61 8642-9890


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