about summary refs log tree commit diff
path: root/sysdeps/x86_64/fpu
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2012-03-28 14:57:58 +0000
committerJoseph Myers <joseph@codesourcery.com>2012-03-28 14:57:58 +0000
commitd6270972f79fe89a96fa7a3909991dad2e317033 (patch)
tree18ed157e89359cf8711d641916028dab73e37449 /sysdeps/x86_64/fpu
parent414fca039edfece0dc9bfc266730e390124687de (diff)
downloadglibc-d6270972f79fe89a96fa7a3909991dad2e317033.tar.gz
glibc-d6270972f79fe89a96fa7a3909991dad2e317033.tar.xz
glibc-d6270972f79fe89a96fa7a3909991dad2e317033.zip
Fix pow of negative numbers to integer exponents (bugs 369, 2678, 3866).
Diffstat (limited to 'sysdeps/x86_64/fpu')
-rw-r--r--sysdeps/x86_64/fpu/e_powl.S47
1 files changed, 43 insertions, 4 deletions
diff --git a/sysdeps/x86_64/fpu/e_powl.S b/sysdeps/x86_64/fpu/e_powl.S
index 562791d302..0626ce4172 100644
--- a/sysdeps/x86_64/fpu/e_powl.S
+++ b/sysdeps/x86_64/fpu/e_powl.S
@@ -107,7 +107,7 @@ ENTRY(__ieee754_powl)
 	fistpll	-8(%rsp)	// y : x
 	fildll	-8(%rsp)	// int(y) : y : x
 	fucomip	%st(1),%st	// y : x
-	jne	2f
+	jne	3f
 
 	/* OK, we have an integer value for y.  */
 	mov	-8(%rsp),%eax
@@ -145,7 +145,14 @@ ENTRY(__ieee754_powl)
 	ret
 
 	.align ALIGNARG(4)
-2:	/* y is a real number.  */
+2:	// y is a large integer (absolute value at least 1L<<63), but
+	// may be odd unless at least 1L<<64.  So it may be necessary
+	// to adjust the sign of a negative result afterwards.
+	fxch			// x : y
+	fabs			// |x| : y
+	fxch			// y : |x|
+	.align ALIGNARG(4)
+3:	/* y is a real number.  */
 	fxch			// x : y
 	fldl	MO(one)		// 1.0 : x : y
 	fldl	MO(limit)	// 0.29 : 1.0 : x : y
@@ -176,13 +183,45 @@ ENTRY(__ieee754_powl)
 	faddl	MO(one)		// 2^fract(y*log2(x)) : int(y*log2(x))
 	fscale			// 2^fract(y*log2(x))*2^int(y*log2(x)) : int(y*log2(x))
 	fstp	%st(1)		// 2^fract(y*log2(x))*2^int(y*log2(x))
-	ret
+	jmp	29f
 
 28:	fstp	%st(1)		// y*log2(x)
 	fldl	MO(one)		// 1 : y*log2(x)
 	fscale			// 2^(y*log2(x)) : y*log2(x)
 	fstp	%st(1)		// 2^(y*log2(x))
-	ret
+29:	testb	$2, %dh
+	jz	292f
+	// x is negative.  If y is an odd integer, negate the result.
+	fldt	24(%rsp)	// y : abs(result)
+	fldl	MO(p64)		// 1L<<64 : y : abs(result)
+	fld	%st(1)		// y : 1L<<64 : y : abs(result)
+	fabs			// |y| : 1L<<64 : y : abs(result)
+	fcomip	%st(1), %st	// 1L<<64 : y : abs(result)
+	fstp	%st(0)		// y : abs(result)
+	jnc	291f
+	fldl	MO(p63)		// p63 : y : abs(result)
+	fxch			// y : p63 : abs(result)
+	fprem			// y%p63 : p63 : abs(result)
+	fstp	%st(1)		// y%p63 : abs(result)
+
+	// We must find out whether y is an odd integer.
+	fld	%st		// y : y : abs(result)
+	fistpll	-8(%rsp)	// y : abs(result)
+	fildll	-8(%rsp)	// int(y) : y : abs(result)
+	fucomip	%st(1),%st	// y : abs(result)
+	ffreep	%st		// abs(result)
+	jne	292f
+
+	// OK, the value is an integer, but is it odd?
+	mov	-8(%rsp), %eax
+	mov	-4(%rsp), %edx
+	andb	$1, %al
+	jz	290f		// jump if not odd
+	// It's an odd integer.
+	fchs
+290:	ret
+291:	fstp	%st(0)		// abs(result)
+292:	ret
 
 	// pow(x,±0) = 1
 	.align ALIGNARG(4)