about summary refs log tree commit diff
path: root/sysdeps
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2016-06-27 17:26:52 +0000
committerJoseph Myers <joseph@codesourcery.com>2016-06-27 17:26:52 +0000
commit30dcf959d2ab76f0bc8b5bc147c35319a6a2ba08 (patch)
tree99f9e66d62c04966c5919f9499f9c372498eb013 /sysdeps
parent623629de066dc2f404470e76ff074fc5ba643c6c (diff)
downloadglibc-30dcf959d2ab76f0bc8b5bc147c35319a6a2ba08.tar.gz
glibc-30dcf959d2ab76f0bc8b5bc147c35319a6a2ba08.tar.xz
glibc-30dcf959d2ab76f0bc8b5bc147c35319a6a2ba08.zip
Avoid "inexact" exceptions in i386/x86_64 trunc functions (bug 15479).
As discussed in
<https://sourceware.org/ml/libc-alpha/2016-05/msg00577.html>, TS
18661-1 disallows ceil, floor, round and trunc functions from raising
the "inexact" exception, in accordance with general IEEE 754 semantics
for when that exception is raised.  Fixing this for x87 floating point
is more complicated than for the other versions of these functions,
because they use the frndint instruction that raises "inexact" and
this can only be avoided by saving and restoring the whole
floating-point environment.

As I noted in
<https://sourceware.org/ml/libc-alpha/2016-06/msg00128.html>, I have
now implemented a GCC option -fno-fp-int-builtin-inexact for GCC 7,
such that GCC will inline these functions on x86, without caring about
"inexact", when the default -ffp-int-builtin-inexact is in effect.
This allows users to get optimized code depending on the options they
pass to the compiler, while making the out-of-line functions follow TS
18661-1 semantics and avoid "inexact".

This patch duly fixes the out-of-line trunc function implementations
to avoid "inexact", in the same way as the nearbyint implementations.

I do not know how the performance of implementations such as these
based on saving the environment and changing the rounding mode
temporarily compares to that of the C versions or SSE 4.1 versions (of
course, for 32-bit x86 SSE implementations still need to get the
return value in an x87 register); it's entirely possible other
implementations could be faster in some cases.

Tested for x86_64 and x86.

	[BZ #15479]
	* sysdeps/i386/fpu/s_trunc.S (__trunc): Save and restore
	floating-point environment rather than just control word.
	* sysdeps/i386/fpu/s_truncf.S (__truncf): Likewise.
	* sysdeps/i386/fpu/s_truncl.S (__truncl): Save and restore
	floating-point environment, with "invalid" exceptions merged in,
	rather than just control word.
	* sysdeps/x86_64/fpu/s_truncl.S (__truncl): Likewise.
	* math/libm-test.inc (trunc_test_data): Do not allow spurious
	"inexact" exceptions.
Diffstat (limited to 'sysdeps')
-rw-r--r--sysdeps/i386/fpu/s_trunc.S12
-rw-r--r--sysdeps/i386/fpu/s_truncf.S12
-rw-r--r--sysdeps/i386/fpu/s_truncl.S15
-rw-r--r--sysdeps/x86_64/fpu/s_truncl.S13
4 files changed, 29 insertions, 23 deletions
diff --git a/sysdeps/i386/fpu/s_trunc.S b/sysdeps/i386/fpu/s_trunc.S
index a15e5e5f16..8d9a6df028 100644
--- a/sysdeps/i386/fpu/s_trunc.S
+++ b/sysdeps/i386/fpu/s_trunc.S
@@ -21,17 +21,17 @@
 
 ENTRY(__trunc)
 	fldl	4(%esp)
-	subl	$8, %esp
-	cfi_adjust_cfa_offset (8)
-	fstcw	4(%esp)
+	subl	$32, %esp
+	cfi_adjust_cfa_offset (32)
+	fnstenv	4(%esp)
 	movl	$0xc00, %edx
 	orl	4(%esp), %edx
 	movl	%edx, (%esp)
 	fldcw	(%esp)
 	frndint
-	fldcw	4(%esp)
-	addl	$8, %esp
-	cfi_adjust_cfa_offset (-8)
+	fldenv	4(%esp)
+	addl	$32, %esp
+	cfi_adjust_cfa_offset (-32)
 	ret
 END(__trunc)
 weak_alias (__trunc, trunc)
diff --git a/sysdeps/i386/fpu/s_truncf.S b/sysdeps/i386/fpu/s_truncf.S
index cbf257a949..d55a11d393 100644
--- a/sysdeps/i386/fpu/s_truncf.S
+++ b/sysdeps/i386/fpu/s_truncf.S
@@ -21,17 +21,17 @@
 
 ENTRY(__truncf)
 	flds	4(%esp)
-	subl	$8, %esp
-	cfi_adjust_cfa_offset (8)
-	fstcw	4(%esp)
+	subl	$32, %esp
+	cfi_adjust_cfa_offset (32)
+	fnstenv	4(%esp)
 	movl	$0xc00, %edx
 	orl	4(%esp), %edx
 	movl	%edx, (%esp)
 	fldcw	(%esp)
 	frndint
-	fldcw	4(%esp)
-	addl	$8, %esp
-	cfi_adjust_cfa_offset (-8)
+	fldenv	4(%esp)
+	addl	$32, %esp
+	cfi_adjust_cfa_offset (-32)
 	ret
 END(__truncf)
 weak_alias (__truncf, truncf)
diff --git a/sysdeps/i386/fpu/s_truncl.S b/sysdeps/i386/fpu/s_truncl.S
index f92b474d49..f5ba372976 100644
--- a/sysdeps/i386/fpu/s_truncl.S
+++ b/sysdeps/i386/fpu/s_truncl.S
@@ -21,17 +21,20 @@
 
 ENTRY(__truncl)
 	fldt	4(%esp)
-	subl	$8, %esp
-	cfi_adjust_cfa_offset (8)
-	fstcw	4(%esp)
+	subl	$32, %esp
+	cfi_adjust_cfa_offset (32)
+	fnstenv	4(%esp)
 	movl	$0xc00, %edx
 	orl	4(%esp), %edx
 	movl	%edx, (%esp)
 	fldcw	(%esp)
 	frndint
-	fldcw	4(%esp)
-	addl	$8, %esp
-	cfi_adjust_cfa_offset (-8)
+	fnstsw
+	andl	$0x1, %eax
+	orl	%eax, 8(%esp)
+	fldenv	4(%esp)
+	addl	$32, %esp
+	cfi_adjust_cfa_offset (-32)
 	ret
 END(__truncl)
 weak_alias (__truncl, truncl)
diff --git a/sysdeps/x86_64/fpu/s_truncl.S b/sysdeps/x86_64/fpu/s_truncl.S
index c37cf00241..0b46efec53 100644
--- a/sysdeps/x86_64/fpu/s_truncl.S
+++ b/sysdeps/x86_64/fpu/s_truncl.S
@@ -21,13 +21,16 @@
 
 ENTRY(__truncl)
 	fldt	8(%rsp)
-	fstcw	-4(%rsp)
+	fnstenv	-28(%rsp)
 	movl	$0xc00, %edx
-	orl	-4(%rsp), %edx
-	movl	%edx, -8(%rsp)
-	fldcw	-8(%rsp)
+	orl	-28(%rsp), %edx
+	movl	%edx, -32(%rsp)
+	fldcw	-32(%rsp)
 	frndint
-	fldcw	-4(%rsp)
+	fnstsw
+	andl	$0x1, %eax
+	orl	%eax, -24(%rsp)
+	fldenv	-28(%rsp)
 	ret
 END(__truncl)
 weak_alias (__truncl, truncl)