diff mbox

Fix log (1) in round-downward mode (bug 16731)

Message ID Pine.LNX.4.64.1403211746350.4557@digraph.polyomino.org.uk
State New
Headers show

Commit Message

Joseph Myers March 21, 2014, 5:47 p.m. UTC
According to ISO C Annex F, log (1) should be +0 in all rounding
modes, but some implementations in glibc wrongly return -0 in
round-downward mode (mapping to log1p (x - 1) is problematic because 1
- 1 is -0 in round-downward mode, and log1p (-0) is -0).  This patch
fixes this.  (It helps with some implementations of other functions
such as acosh, log2 and log10 that call out to log, but not enough to
enable all-rounding-modes testing for those functions without further
fixes to other implementations of them.)

Tested x86_64 and x86 and ulps updated accordingly, and did spot tests
for mips64 for the ldbl-128 fix, and i586 for the sysdeps/i386/fpu
implementations shadowed by those in sysdeps/i386/i686/fpu.

2014-03-21  Joseph Myers  <joseph@codesourcery.com>

	[BZ #16731]
	* sysdeps/i386/fpu/e_log.S (__ieee754_log): Take absolute value
	when x - 1 is zero.
	* sysdeps/i386/fpu/e_logf.S (__ieee754_logf): Likewise.
	* sysdeps/i386/fpu/e_logl.S (__ieee754_logl): Likewise.
	* sysdeps/i386/i686/fpu/e_logl.S (__ieee754_logl): Likewise.
	* sysdeps/ieee754/dbl-64/e_log.c (__ieee754_log): Return +0 when
	argument is 1.
	* sysdeps/ieee754/ldbl-128/e_logl.c (__ieee754_logl): Likewise.
	* sysdeps/x86_64/fpu/e_logl.S: Take absolute value when x - 1 is
	zero.
	* math/libm-test.inc (log_test): Use ALL_RM_TEST.
	* sysdeps/i386/fpu/libm-test-ulps: Update.
	* sysdeps/x86_64/fpu/libm-test-ulps: Likewise.

Comments

Carlos O'Donell March 21, 2014, 5:58 p.m. UTC | #1
On 03/21/2014 01:47 PM, Joseph S. Myers wrote:
> According to ISO C Annex F, log (1) should be +0 in all rounding
> modes, but some implementations in glibc wrongly return -0 in
> round-downward mode (mapping to log1p (x - 1) is problematic because 1
> - 1 is -0 in round-downward mode, and log1p (-0) is -0).  This patch
> fixes this.  (It helps with some implementations of other functions
> such as acosh, log2 and log10 that call out to log, but not enough to
> enable all-rounding-modes testing for those functions without further
> fixes to other implementations of them.)
> 
> Tested x86_64 and x86 and ulps updated accordingly, and did spot tests
> for mips64 for the ldbl-128 fix, and i586 for the sysdeps/i386/fpu
> implementations shadowed by those in sysdeps/i386/i686/fpu.
> 
> 2014-03-21  Joseph Myers  <joseph@codesourcery.com>
> 
> 	[BZ #16731]
> 	* sysdeps/i386/fpu/e_log.S (__ieee754_log): Take absolute value
> 	when x - 1 is zero.
> 	* sysdeps/i386/fpu/e_logf.S (__ieee754_logf): Likewise.
> 	* sysdeps/i386/fpu/e_logl.S (__ieee754_logl): Likewise.
> 	* sysdeps/i386/i686/fpu/e_logl.S (__ieee754_logl): Likewise.
> 	* sysdeps/ieee754/dbl-64/e_log.c (__ieee754_log): Return +0 when
> 	argument is 1.
> 	* sysdeps/ieee754/ldbl-128/e_logl.c (__ieee754_logl): Likewise.
> 	* sysdeps/x86_64/fpu/e_logl.S: Take absolute value when x - 1 is
> 	zero.
> 	* math/libm-test.inc (log_test): Use ALL_RM_TEST.
> 	* sysdeps/i386/fpu/libm-test-ulps: Update.
> 	* sysdeps/x86_64/fpu/libm-test-ulps: Likewise.

Looks good to me.

> diff --git a/math/libm-test.inc b/math/libm-test.inc
> index 7abc7c1..5e50f0e 100644
> --- a/math/libm-test.inc
> +++ b/math/libm-test.inc
> @@ -7786,9 +7786,7 @@ static const struct test_f_f_data log_test_data[] =
>  static void
>  log_test (void)
>  {
> -  START (log, 0);
> -  RUN_TEST_LOOP_f_f (log, log_test_data, );
> -  END;
> +  ALL_RM_TEST (log, 0, log_test_data, RUN_TEST_LOOP_f_f, END);

OK. Nice to see another function using ALL_RM_TEST.

>  }
>  
>  
> diff --git a/sysdeps/i386/fpu/e_log.S b/sysdeps/i386/fpu/e_log.S
> index 0877924..3fa32aa 100644
> --- a/sysdeps/i386/fpu/e_log.S
> +++ b/sysdeps/i386/fpu/e_log.S
> @@ -46,7 +46,13 @@ ENTRY(__ieee754_log)
>  	fnstsw			// x-1 : x : log(2)
>  	andb	$0x45, %ah
>  	jz	2f
> -	fstp	%st(1)		// x-1 : log(2)
> +	fxam
> +	fnstsw
> +	andb	$0x45, %ah
> +	cmpb	$0x40, %ah
> +	jne	5f
> +	fabs			// log(1) is +0 in all rounding modes.
> +5:	fstp	%st(1)		// x-1 : log(2)

OK.

>  	fyl2xp1			// log(x)
>  	ret
>  
> diff --git a/sysdeps/i386/fpu/e_logf.S b/sysdeps/i386/fpu/e_logf.S
> index 485180e..ca83d39 100644
> --- a/sysdeps/i386/fpu/e_logf.S
> +++ b/sysdeps/i386/fpu/e_logf.S
> @@ -47,7 +47,13 @@ ENTRY(__ieee754_logf)
>  	fnstsw			// x-1 : x : log(2)
>  	andb	$0x45, %ah
>  	jz	2f
> -	fstp	%st(1)		// x-1 : log(2)
> +	fxam
> +	fnstsw
> +	andb	$0x45, %ah
> +	cmpb	$0x40, %ah
> +	jne	5f
> +	fabs			// log(1) is +0 in all rounding modes.
> +5:	fstp	%st(1)		// x-1 : log(2)

OK.

>  	fyl2xp1			// log(x)
>  	ret
>  
> diff --git a/sysdeps/i386/fpu/e_logl.S b/sysdeps/i386/fpu/e_logl.S
> index d7a459a..edae1d7 100644
> --- a/sysdeps/i386/fpu/e_logl.S
> +++ b/sysdeps/i386/fpu/e_logl.S
> @@ -47,7 +47,13 @@ ENTRY(__ieee754_logl)
>  	fnstsw			// x-1 : x : log(2)
>  	andb	$0x45, %ah
>  	jz	2f
> -	fstp	%st(1)		// x-1 : log(2)
> +	fxam
> +	fnstsw
> +	andb	$0x45, %ah
> +	cmpb	$0x40, %ah
> +	jne	5f
> +	fabs			// log(1) is +0 in all rounding modes.
> +5:	fstp	%st(1)		// x-1 : log(2)

OK.

>  	fyl2xp1			// log(x)
>  	ret
>  
> diff --git a/sysdeps/i386/fpu/libm-test-ulps b/sysdeps/i386/fpu/libm-test-ulps
> index 3be1806..b7b2e12 100644
> --- a/sysdeps/i386/fpu/libm-test-ulps
> +++ b/sysdeps/i386/fpu/libm-test-ulps
> @@ -1096,6 +1096,18 @@ Function: "log1p":
>  ildouble: 1
>  ldouble: 1
>  
> +Function: "log_downward":
> +ildouble: 1
> +ldouble: 1
> +
> +Function: "log_towardzero":
> +ildouble: 1
> +ldouble: 1
> +
> +Function: "log_upward":
> +ildouble: 1
> +ldouble: 1
> +

OK.

>  Function: "pow":
>  ildouble: 1
>  ldouble: 1
> diff --git a/sysdeps/i386/i686/fpu/e_logl.S b/sysdeps/i386/i686/fpu/e_logl.S
> index 8a86222..53a3d10 100644
> --- a/sysdeps/i386/i686/fpu/e_logl.S
> +++ b/sysdeps/i386/i686/fpu/e_logl.S
> @@ -46,7 +46,13 @@ ENTRY(__ieee754_logl)
>  	fcomip	%st(1)		// |x-1| : x-1 : x : log(2)
>  	fstp	%st(0)		// x-1 : x : log(2)
>  	jc	2f
> -	fstp	%st(1)		// x-1 : log(2)
> +	fxam
> +	fnstsw
> +	andb	$0x45, %ah
> +	cmpb	$0x40, %ah
> +	jne	4f
> +	fabs			// // log(1) is +0 in all rounding modes.
> +4:	fstp	%st(1)		// x-1 : log(2)

OK.

>  	fyl2xp1			// log(x)
>  	ret
>  
> diff --git a/sysdeps/ieee754/dbl-64/e_log.c b/sysdeps/ieee754/dbl-64/e_log.c
> index 05d318b..4ecd372 100644
> --- a/sysdeps/ieee754/dbl-64/e_log.c
> +++ b/sysdeps/ieee754/dbl-64/e_log.c
> @@ -96,6 +96,10 @@ __ieee754_log (double x)
>    if (__glibc_likely (ABS (w) > U03))
>      goto case_03;
>  
> +  /* log (1) is +0 in all rounding modes.  */
> +  if (w == 0.0)
> +    return 0.0;

OK.

> +
>    /*--- Stage I, the case abs(x-1) < 0.03 */
>  
>    t8 = MHALF * w;
> diff --git a/sysdeps/ieee754/ldbl-128/e_logl.c b/sysdeps/ieee754/ldbl-128/e_logl.c
> index 3d1034d..cb43816 100644
> --- a/sysdeps/ieee754/ldbl-128/e_logl.c
> +++ b/sysdeps/ieee754/ldbl-128/e_logl.c
> @@ -240,6 +240,8 @@ __ieee754_logl(long double x)
>    /* On this interval the table is not used due to cancellation error.  */
>    if ((x <= 1.0078125L) && (x >= 0.9921875L))
>      {
> +      if (x == 1.0L)
> +	return 0.0L;

OK.

>        z = x - 1.0L;
>        k = 64;
>        t.value  = 1.0L;
> diff --git a/sysdeps/x86_64/fpu/e_logl.S b/sysdeps/x86_64/fpu/e_logl.S
> index a8e3108..315afc0 100644
> --- a/sysdeps/x86_64/fpu/e_logl.S
> +++ b/sysdeps/x86_64/fpu/e_logl.S
> @@ -45,7 +45,13 @@ ENTRY(__ieee754_logl)
>  	fnstsw			// x-1 : x : log(2)
>  	andb	$0x45, %ah
>  	jz	2f
> -	fstp	%st(1)		// x-1 : log(2)
> +	fxam
> +	fnstsw
> +	andb	$0x45, %ah
> +	cmpb	$0x40, %ah
> +	jne	5f
> +	fabs			// log(1) is +0 in all rounding modes.
> +5:	fstp	%st(1)		// x-1 : log(2)

OK.

>  	fyl2xp1			// log(x)
>  	ret
>  
> diff --git a/sysdeps/x86_64/fpu/libm-test-ulps b/sysdeps/x86_64/fpu/libm-test-ulps
> index 5f4ab06..301eaa6 100644
> --- a/sysdeps/x86_64/fpu/libm-test-ulps
> +++ b/sysdeps/x86_64/fpu/libm-test-ulps
> @@ -1170,6 +1170,22 @@ ifloat: 1
>  ildouble: 1
>  ldouble: 1
>  
> +Function: "log_downward":
> +float: 1
> +ifloat: 1
> +ildouble: 1
> +ldouble: 1
> +
> +Function: "log_towardzero":
> +ildouble: 1
> +ldouble: 1
> +
> +Function: "log_upward":
> +float: 1
> +ifloat: 1
> +ildouble: 1
> +ldouble: 1
> +
>  Function: "pow":
>  float: 1
>  ifloat: 1
> 

OK.

Cheers,
Carlos.
Adam Conrad March 21, 2014, 8:11 p.m. UTC | #2
On Fri, Mar 21, 2014 at 05:47:45PM +0000, Joseph S. Myers wrote:
> According to ISO C Annex F, log (1) should be +0 in all rounding
> modes, but some implementations in glibc wrongly return -0 in
> round-downward mode (mapping to log1p (x - 1) is problematic because 1
> - 1 is -0 in round-downward mode, and log1p (-0) is -0).  This patch
> fixes this.  (It helps with some implementations of other functions
> such as acosh, log2 and log10 that call out to log, but not enough to
> enable all-rounding-modes testing for those functions without further
> fixes to other implementations of them.)

This reminds me (in context) of this patch we're carrying in Debian:

http://anonscm.debian.org/viewvc/pkg-glibc/glibc-package/trunk/debian/patches/powerpc/local-math-logb.diff?revision=5951&view=markup

If I recall, Aurelien said we're carrying it locally because it was
rejected upstream as not a bug, due to it only affecting older HW
that no one cares about.

Should we be cleaning this up and re-submitting, to go along with
your batch of fixes here?

... Adam
Joseph Myers March 21, 2014, 10:31 p.m. UTC | #3
On Fri, 21 Mar 2014, Adam Conrad wrote:

> On Fri, Mar 21, 2014 at 05:47:45PM +0000, Joseph S. Myers wrote:
> > According to ISO C Annex F, log (1) should be +0 in all rounding
> > modes, but some implementations in glibc wrongly return -0 in
> > round-downward mode (mapping to log1p (x - 1) is problematic because 1
> > - 1 is -0 in round-downward mode, and log1p (-0) is -0).  This patch
> > fixes this.  (It helps with some implementations of other functions
> > such as acosh, log2 and log10 that call out to log, but not enough to
> > enable all-rounding-modes testing for those functions without further
> > fixes to other implementations of them.)
> 
> This reminds me (in context) of this patch we're carrying in Debian:
> 
> http://anonscm.debian.org/viewvc/pkg-glibc/glibc-package/trunk/debian/patches/powerpc/local-math-logb.diff?revision=5951&view=markup
> 
> If I recall, Aurelien said we're carrying it locally because it was
> rejected upstream as not a bug, due to it only affecting older HW
> that no one cares about.
> 
> Should we be cleaning this up and re-submitting, to go along with
> your batch of fixes here?

What I think should be done here is:

* Submit a bug to GCC Bugzilla about conversion from integer to floating 
point being incorrect for older hard-float powerpc with -frounding-math.

* Reopen bug 887 as not in fact fixed, and remove it from the list of bugs 
fixed in 2.16 in NEWS.

* Close bug 16423 as a duplicate of the newly reopened bug 887.

* Work around the GCC bug in glibc conditional on a configure test for 
whether (building for powerpc and) $CC $CFLAGS $CPPFLAGS -frounding-math 
generates the problem code sequence (i.e. the one involving certain magic 
constants and floating-point addition / subtraction), not using fcfid, not 
using any instructions to save / restore the rounding mode (the last bit 
may be more difficult to test for in a future-proof way, since you don't 
know what any fix for the GCC bug might look like).  Make sure comments 
reference the GCC bug in question.

* Having got such a workaround in glibc, bug 887 can then be closed as 
fixed.
diff mbox

Patch

diff --git a/math/libm-test.inc b/math/libm-test.inc
index 7abc7c1..5e50f0e 100644
--- a/math/libm-test.inc
+++ b/math/libm-test.inc
@@ -7786,9 +7786,7 @@  static const struct test_f_f_data log_test_data[] =
 static void
 log_test (void)
 {
-  START (log, 0);
-  RUN_TEST_LOOP_f_f (log, log_test_data, );
-  END;
+  ALL_RM_TEST (log, 0, log_test_data, RUN_TEST_LOOP_f_f, END);
 }
 
 
diff --git a/sysdeps/i386/fpu/e_log.S b/sysdeps/i386/fpu/e_log.S
index 0877924..3fa32aa 100644
--- a/sysdeps/i386/fpu/e_log.S
+++ b/sysdeps/i386/fpu/e_log.S
@@ -46,7 +46,13 @@  ENTRY(__ieee754_log)
 	fnstsw			// x-1 : x : log(2)
 	andb	$0x45, %ah
 	jz	2f
-	fstp	%st(1)		// x-1 : log(2)
+	fxam
+	fnstsw
+	andb	$0x45, %ah
+	cmpb	$0x40, %ah
+	jne	5f
+	fabs			// log(1) is +0 in all rounding modes.
+5:	fstp	%st(1)		// x-1 : log(2)
 	fyl2xp1			// log(x)
 	ret
 
diff --git a/sysdeps/i386/fpu/e_logf.S b/sysdeps/i386/fpu/e_logf.S
index 485180e..ca83d39 100644
--- a/sysdeps/i386/fpu/e_logf.S
+++ b/sysdeps/i386/fpu/e_logf.S
@@ -47,7 +47,13 @@  ENTRY(__ieee754_logf)
 	fnstsw			// x-1 : x : log(2)
 	andb	$0x45, %ah
 	jz	2f
-	fstp	%st(1)		// x-1 : log(2)
+	fxam
+	fnstsw
+	andb	$0x45, %ah
+	cmpb	$0x40, %ah
+	jne	5f
+	fabs			// log(1) is +0 in all rounding modes.
+5:	fstp	%st(1)		// x-1 : log(2)
 	fyl2xp1			// log(x)
 	ret
 
diff --git a/sysdeps/i386/fpu/e_logl.S b/sysdeps/i386/fpu/e_logl.S
index d7a459a..edae1d7 100644
--- a/sysdeps/i386/fpu/e_logl.S
+++ b/sysdeps/i386/fpu/e_logl.S
@@ -47,7 +47,13 @@  ENTRY(__ieee754_logl)
 	fnstsw			// x-1 : x : log(2)
 	andb	$0x45, %ah
 	jz	2f
-	fstp	%st(1)		// x-1 : log(2)
+	fxam
+	fnstsw
+	andb	$0x45, %ah
+	cmpb	$0x40, %ah
+	jne	5f
+	fabs			// log(1) is +0 in all rounding modes.
+5:	fstp	%st(1)		// x-1 : log(2)
 	fyl2xp1			// log(x)
 	ret
 
diff --git a/sysdeps/i386/fpu/libm-test-ulps b/sysdeps/i386/fpu/libm-test-ulps
index 3be1806..b7b2e12 100644
--- a/sysdeps/i386/fpu/libm-test-ulps
+++ b/sysdeps/i386/fpu/libm-test-ulps
@@ -1096,6 +1096,18 @@  Function: "log1p":
 ildouble: 1
 ldouble: 1
 
+Function: "log_downward":
+ildouble: 1
+ldouble: 1
+
+Function: "log_towardzero":
+ildouble: 1
+ldouble: 1
+
+Function: "log_upward":
+ildouble: 1
+ldouble: 1
+
 Function: "pow":
 ildouble: 1
 ldouble: 1
diff --git a/sysdeps/i386/i686/fpu/e_logl.S b/sysdeps/i386/i686/fpu/e_logl.S
index 8a86222..53a3d10 100644
--- a/sysdeps/i386/i686/fpu/e_logl.S
+++ b/sysdeps/i386/i686/fpu/e_logl.S
@@ -46,7 +46,13 @@  ENTRY(__ieee754_logl)
 	fcomip	%st(1)		// |x-1| : x-1 : x : log(2)
 	fstp	%st(0)		// x-1 : x : log(2)
 	jc	2f
-	fstp	%st(1)		// x-1 : log(2)
+	fxam
+	fnstsw
+	andb	$0x45, %ah
+	cmpb	$0x40, %ah
+	jne	4f
+	fabs			// // log(1) is +0 in all rounding modes.
+4:	fstp	%st(1)		// x-1 : log(2)
 	fyl2xp1			// log(x)
 	ret
 
diff --git a/sysdeps/ieee754/dbl-64/e_log.c b/sysdeps/ieee754/dbl-64/e_log.c
index 05d318b..4ecd372 100644
--- a/sysdeps/ieee754/dbl-64/e_log.c
+++ b/sysdeps/ieee754/dbl-64/e_log.c
@@ -96,6 +96,10 @@  __ieee754_log (double x)
   if (__glibc_likely (ABS (w) > U03))
     goto case_03;
 
+  /* log (1) is +0 in all rounding modes.  */
+  if (w == 0.0)
+    return 0.0;
+
   /*--- Stage I, the case abs(x-1) < 0.03 */
 
   t8 = MHALF * w;
diff --git a/sysdeps/ieee754/ldbl-128/e_logl.c b/sysdeps/ieee754/ldbl-128/e_logl.c
index 3d1034d..cb43816 100644
--- a/sysdeps/ieee754/ldbl-128/e_logl.c
+++ b/sysdeps/ieee754/ldbl-128/e_logl.c
@@ -240,6 +240,8 @@  __ieee754_logl(long double x)
   /* On this interval the table is not used due to cancellation error.  */
   if ((x <= 1.0078125L) && (x >= 0.9921875L))
     {
+      if (x == 1.0L)
+	return 0.0L;
       z = x - 1.0L;
       k = 64;
       t.value  = 1.0L;
diff --git a/sysdeps/x86_64/fpu/e_logl.S b/sysdeps/x86_64/fpu/e_logl.S
index a8e3108..315afc0 100644
--- a/sysdeps/x86_64/fpu/e_logl.S
+++ b/sysdeps/x86_64/fpu/e_logl.S
@@ -45,7 +45,13 @@  ENTRY(__ieee754_logl)
 	fnstsw			// x-1 : x : log(2)
 	andb	$0x45, %ah
 	jz	2f
-	fstp	%st(1)		// x-1 : log(2)
+	fxam
+	fnstsw
+	andb	$0x45, %ah
+	cmpb	$0x40, %ah
+	jne	5f
+	fabs			// log(1) is +0 in all rounding modes.
+5:	fstp	%st(1)		// x-1 : log(2)
 	fyl2xp1			// log(x)
 	ret
 
diff --git a/sysdeps/x86_64/fpu/libm-test-ulps b/sysdeps/x86_64/fpu/libm-test-ulps
index 5f4ab06..301eaa6 100644
--- a/sysdeps/x86_64/fpu/libm-test-ulps
+++ b/sysdeps/x86_64/fpu/libm-test-ulps
@@ -1170,6 +1170,22 @@  ifloat: 1
 ildouble: 1
 ldouble: 1
 
+Function: "log_downward":
+float: 1
+ifloat: 1
+ildouble: 1
+ldouble: 1
+
+Function: "log_towardzero":
+ildouble: 1
+ldouble: 1
+
+Function: "log_upward":
+float: 1
+ifloat: 1
+ildouble: 1
+ldouble: 1
+
 Function: "pow":
 float: 1
 ifloat: 1