Patchwork [v4,1/3] Cleanup __ieee754_sqrt(f/l)

login
register
mail settings
Submitter Wilco Dijkstra
Date March 15, 2018, 3:53 p.m.
Message ID <DB6PR0801MB2053657FD24E0CE67014E89883D00@DB6PR0801MB2053.eurprd08.prod.outlook.com>
Download mbox | patch
Permalink /patch/26321/
State New
Headers show

Comments

Wilco Dijkstra - March 15, 2018, 3:53 p.m.
This patch series cleans up the many uses of  __ieee754_sqrt(f/l) in GLIBC.
The goal is to enable GCC to do the inlining, and if this fails call the
__ieee754_sqrt function.  This is done by internally declaring sqrt with asm
redirects.  The compat symbols and sqrt wrappers need to disable the redirect.
The redirect is also disabled if there are already redirects defined when
using -ffinite-math-only.

All math functions (but not math tests, non-library code and libnldbl) are
built with -fno-math-errno which means GCC will typically inline sqrt as a
single instruction.  This means targets are no longer forced to add a special
inline for sqrt.

Regress tested on AArch64 ad x64 and a subset of buildmanyglibc.

ChangeLog:
2018-03-15  Wilco Dijkstra  <wdijkstr@arm.com>

        * include/math.h (sqrt): Declare with asm redirect.
        (sqrtf): Likewise.
        (sqrtl): Likewise.
        (sqrtf128): Likewise.
        * Makeconfig: Add -fno-math-errno for libc/libm, but build testsuite,
        nonlib and libnldbl with -fmath-errno.
        * math/w_sqrt_compat.c: Define NO_MATH_REDIRECT.
        * math/w_sqrt_template.c: Likewise.
        * math/w_sqrtf_compat.c: Likewise.
        * math/w_sqrtl_compat.c: Likewise.
        * sysdeps/generic/math-type-macros-float128.h: Remove math.h and complex.h.
        * sysdeps/i386/fpu/w_sqrt.c: Likewise.
        * sysdeps/i386/fpu/w_sqrt_compat.c: Likewise.

--
Joseph Myers - March 15, 2018, 5:04 p.m.
On Thu, 15 Mar 2018, Wilco Dijkstra wrote:

> 2018-03-15  Wilco Dijkstra  <wdijkstr@arm.com>
> 
>         * include/math.h (sqrt): Declare with asm redirect.
>         (sqrtf): Likewise.
>         (sqrtl): Likewise.
>         (sqrtf128): Likewise.
>         * Makeconfig: Add -fno-math-errno for libc/libm, but build testsuite,
>         nonlib and libnldbl with -fmath-errno.
>         * math/w_sqrt_compat.c: Define NO_MATH_REDIRECT.
>         * math/w_sqrt_template.c: Likewise.
>         * math/w_sqrtf_compat.c: Likewise.
>         * math/w_sqrtl_compat.c: Likewise.
>         * sysdeps/generic/math-type-macros-float128.h: Remove math.h and complex.h.
>         * sysdeps/i386/fpu/w_sqrt.c: Likewise.
>         * sysdeps/i386/fpu/w_sqrt_compat.c: Likewise.

The sysdeps/generic/math-type-macros-float128.h entry should come at the 
end, otherwise the "Likewise" for the subsequent two files is incorrect, 
since it's not math.h and complex.h includes being removed from them, it's 
NO_MATH_REDIRECT defines being added.

> +# if !(defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
> +#  ifndef NO_MATH_REDIRECT
> +/* Declare sqrt for use within GLIBC.  Sqrt will typically inline into a

sqrt not Sqrt at the start of a sentence (see the GNU Coding Standards: 
"If a lower-case identifier comes at the beginning of a sentence, don't 
capitalize it!  Changing the spelling makes it a different identifier.  
If you don't like starting a sentence with a lower case letter, write the 
sentence differently (e.g., ``The identifier lower-case is @dots{}'').").

This patch is OK with those changes.

Patch

diff --git a/Makeconfig b/Makeconfig
index 86a71e580213f6e5de6e619d9f3370f4365b4ae2..1afe86475c10ea208ed88b9b137cc7863510929f 100644
--- a/Makeconfig
+++ b/Makeconfig
@@ -831,6 +831,9 @@  endif
 # disable any optimization that assume default rounding mode.
 +math-flags = -frounding-math
 
+# Build libc/libm using -fno-math-errno, but run testsuite with -fmath-errno.
++extra-math-flags = $(if $(filter libnldbl nonlib testsuite,$(in-module)),-fmath-errno,-fno-math-errno)
+
 # We might want to compile with some stack-protection flag.
 ifneq ($(stack-protector),)
 +stack-protector=$(stack-protector)
@@ -966,6 +969,7 @@  endif
 
 override CFLAGS	= -std=gnu11 -fgnu89-inline $(config-extra-cflags) \
 		  $(filter-out %frame-pointer,$(+cflags)) $(+gccwarn-c) \
+		  $(+extra-math-flags) \
 		  $(sysdep-CFLAGS) $(CFLAGS-$(suffix $@)) $(CFLAGS-$(<F)) \
 		  $(CFLAGS-$(@F)) $(tls-model) \
 		  $(foreach lib,$(libof-$(basename $(@F))) \
diff --git a/include/math.h b/include/math.h
index 7ee291fc972088a1409949326f7024e8e3fe5415..c25033fcd5add2f4067fdbb51fb345056d6a1400 100644
--- a/include/math.h
+++ b/include/math.h
@@ -54,5 +54,20 @@  libm_hidden_proto (__expf128)
 libm_hidden_proto (__expm1f128)
 # endif
 
+# if !(defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0)
+#  ifndef NO_MATH_REDIRECT
+/* Declare sqrt for use within GLIBC.  Sqrt will typically inline into a
+   single instruction.  Use an asm to avoid use of PLTs if it doesn't.  */
+float (sqrtf) (float) asm ("__ieee754_sqrtf");
+double (sqrt) (double) asm ("__ieee754_sqrt");
+#   ifndef __NO_LONG_DOUBLE_MATH
+long double (sqrtl) (long double) asm ("__ieee754_sqrtl");
+#   endif
+#   if __HAVE_DISTINCT_FLOAT128 > 0
+_Float128 (sqrtf128) (_Float128) asm ("__ieee754_sqrtf128");
+#   endif
+#  endif
+# endif
+
 #endif
 #endif
diff --git a/math/w_sqrt_compat.c b/math/w_sqrt_compat.c
index 7884d146331d7c43c79e1957e3ba4f2dab608ead..e76a8079aa0a2dd4dc09aba48b6a82aaddd1a584 100644
--- a/math/w_sqrt_compat.c
+++ b/math/w_sqrt_compat.c
@@ -16,6 +16,7 @@ 
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#define NO_MATH_REDIRECT
 #include <math.h>
 #include <math_private.h>
 #include <math-svid-compat.h>
diff --git a/math/w_sqrt_template.c b/math/w_sqrt_template.c
index 52a21c0bf1cd7bdab164356635ffa5f10973b633..9c6ac75956ca8abf9448d631fd286b5e2d1deb73 100644
--- a/math/w_sqrt_template.c
+++ b/math/w_sqrt_template.c
@@ -21,6 +21,7 @@ 
    for each floating-point type.  */
 #if __USE_WRAPPER_TEMPLATE
 
+# define NO_MATH_REDIRECT
 # include <errno.h>
 # include <fenv.h>
 # include <math.h>
diff --git a/math/w_sqrtf_compat.c b/math/w_sqrtf_compat.c
index 824ce0ed4658210b75e38953ee39d3dcd0bbb3f7..cad9b4ad77f1707af6c1df5ada82b07537d79dfa 100644
--- a/math/w_sqrtf_compat.c
+++ b/math/w_sqrtf_compat.c
@@ -16,6 +16,7 @@ 
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#define NO_MATH_REDIRECT
 #include <math.h>
 #include <math_private.h>
 #include <math-svid-compat.h>
diff --git a/math/w_sqrtl_compat.c b/math/w_sqrtl_compat.c
index 56627a615dce25aae893f9d84292922a6e452b3f..16dda403b77144667e3f76b633f6f9165c8a38d3 100644
--- a/math/w_sqrtl_compat.c
+++ b/math/w_sqrtl_compat.c
@@ -16,6 +16,7 @@ 
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
 
+#define NO_MATH_REDIRECT
 #include <math.h>
 #include <math_private.h>
 #include <math-svid-compat.h>
diff --git a/sysdeps/generic/math-type-macros-float128.h b/sysdeps/generic/math-type-macros-float128.h
index 605996e0ba73f3ad1e421f516396442b2c2b1e25..485c13bb88a1995c69d62e11ea819d1323dd938b 100644
--- a/sysdeps/generic/math-type-macros-float128.h
+++ b/sysdeps/generic/math-type-macros-float128.h
@@ -19,9 +19,6 @@ 
 #ifndef _MATH_TYPE_MACROS_FLOAT128
 #define _MATH_TYPE_MACROS_FLOAT128
 
-#include <math.h>
-#include <complex.h>
-
 #define M_LIT(c) __f128 (c)
 #define M_PFX FLT128
 #define M_SUF(c) c ## f128
diff --git a/sysdeps/i386/fpu/w_sqrt.c b/sysdeps/i386/fpu/w_sqrt.c
index d37a5d55bf439cebdff05f05dd66f910d6d48c18..8bef04e68a7be3d21d89e6eb41dcfa2561f3f6a5 100644
--- a/sysdeps/i386/fpu/w_sqrt.c
+++ b/sysdeps/i386/fpu/w_sqrt.c
@@ -1,5 +1,6 @@ 
 /* The inline __ieee754_sqrt is not correctly rounding; it's OK for
    most internal uses in glibc, but not for sqrt itself.  */
+#define NO_MATH_REDIRECT
 #define __ieee754_sqrt __avoid_ieee754_sqrt
 #include <math.h>
 #include <math_private.h>
diff --git a/sysdeps/i386/fpu/w_sqrt_compat.c b/sysdeps/i386/fpu/w_sqrt_compat.c
index ddd36d0964c2945579bc59b02f127af88384acca..dd485f4b88c7f5348abaab81a1308aeadc9594ec 100644
--- a/sysdeps/i386/fpu/w_sqrt_compat.c
+++ b/sysdeps/i386/fpu/w_sqrt_compat.c
@@ -1,5 +1,6 @@ 
 /* The inline __ieee754_sqrt is not correctly rounding; it's OK for
    most internal uses in glibc, but not for sqrt itself.  */
+#define NO_MATH_REDIRECT
 #define __ieee754_sqrt __avoid_ieee754_sqrt
 #include <math.h>
 #include <math_private.h>