diff mbox

[1/3] Cleanup __ieee754_sqrt(f/l)

Message ID DB6PR0801MB20539115E7D34D60B40EB024837B0@DB6PR0801MB2053.eurprd08.prod.outlook.com
State New, archived
Headers show

Commit Message

Wilco Dijkstra Sept. 26, 2017, 1:22 p.m. UTC
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.

This means targets are no longer forced to add a special inline for sqrt.

All math functions (but not math tests) are built with -fno-math-errno which
means GCC will inline sqrt as a single instruction.


ChangeLog:
2017-09-25  Wilco Dijkstra  <wdijkstr@arm.com>

        * include/math.h (sqrt): Declare with asm redirect.
        (sqrtf): Likewise.
        (sqrtl): Likewise.
        * math/Makefile: Add -fno-math-errno, but build tests
        with -fmath-errno.
        * math/w_sqrt_compat.c: Define NO_SQRT_REDIRECT.
        * math/w_sqrt_template.c: Likewise.
        * math/w_sqrtf_compat.c: Likewise.
        * math/w_sqrtl_compat.c: Likewise.
--

Comments

Joseph Myers Sept. 26, 2017, 5:25 p.m. UTC | #1
On Tue, 26 Sep 2017, Wilco Dijkstra wrote:

> 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.
> 
> This means targets are no longer forced to add a special inline for sqrt.

I don't think these patches are reviewable without each patch including a 
description of how it was tested.  Appropriate testing might be a full 
build-many-glibcs.py run for each patch in the series (but at least, you 
need to include testing for at least one configuration where these 
functions are not inlined, and at least one configuration with float128 
support).

>         * math/Makefile: Add -fno-math-errno, but build tests
>         with -fmath-errno.

This seems extremely fragile.  It looks like it's relying on options added 
with one variable coming after options added with another, which is a 
matter of implementation details of how the compilation commands are built 
up from different variables.

I think there should be a single variable variable that adds the required 
option in every case - always at most one of -fmath-errno and 
-fno-math-errno, with no file ever getting both - probably based on 
$(in-module), and that should then be used at top level alongside 
-frounding-math, rather than only in the math/ directory.
diff mbox

Patch

diff --git a/include/math.h b/include/math.h
index fe3ed1378d19faf572b48347b41501d9ec0925b5..e176b090caa9f07c226aff0f9c6c5bf34740f2ec 100644
--- a/include/math.h
+++ b/include/math.h
@@ -55,5 +55,15 @@  libm_hidden_proto (__expf128)
 libm_hidden_proto (__expm1f128)
 # endif
 
+# ifndef NO_SQRT_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
+# endif
+
 #endif
 #endif
diff --git a/math/Makefile b/math/Makefile
index 04586156f8050222b2aedcebc30dab7153a1b6b1..ad65dd7677dd3c6a91fb2f393e5ca43769f3ddec 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -500,6 +500,12 @@  endef
 object-suffixes-left := $(types)
 include $(o-iterator)
 
+define o-iterator-doit
+$(objpfx)$(o).o: CFLAGS += -fmath-errno
+endef
+object-suffixes-left := $(tests) $(tests-internal)
+include $(o-iterator)
+
 # Run the math programs to automatically generate ULPs files.
 .PHONY: regen-ulps
 
@@ -547,6 +553,8 @@  include $(o-iterator)
 # only the fdlibm code.
 math-CPPFLAGS += -D__NO_MATH_INLINES -D__LIBC_INTERNAL_MATH_INLINES
 
+config-extra-cflags += -fno-math-errno
+
 ifneq ($(long-double-fcts),yes)
 # The `double' and `long double' types are the same on this machine.
 # We won't compile the `long double' code at all.  Tell the `double' code
diff --git a/math/w_sqrt_compat.c b/math/w_sqrt_compat.c
index 3280d2fbb86af2aeaf6e686fd38579b209c901aa..fe068af9597ffb0f15b32cd454b4c34ba8bc060e 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_SQRT_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 5fae302382d10e9b05df2665c9cb05126cd62f02..235c263e60c85422e41bca7c817148b66030438f 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_SQRT_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 6c8c7e3857c7dacf52de6b575a2386095688b5dc..880be0936d9a20f9aa75cf2d66000f0c621f090f 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_SQRT_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 0590f6d155fee27d41bfd42c1dbdf19c381ba1c8..bff77fd31bf2c62394e0d54e6b2e7bc9296226d6 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_SQRT_REDIRECT
 #include <math.h>
 #include <math_private.h>
 #include <math-svid-compat.h>