x86-64: Don't inline atof without SSE2 [BZ #27600]

Message ID 20210318015414.902129-1-hjl.tools@gmail.com
State Dropped
Headers
Series x86-64: Don't inline atof without SSE2 [BZ #27600] |

Commit Message

H.J. Lu March 18, 2021, 1:54 a.m. UTC
  Since without SSE2, inlined atof fails with

/usr/include/bits/stdlib-float.h: In function ‘atof’:
/usr/include/bits/stdlib-float.h:26:1: error: SSE register return with SSE disabled
   26 | {
      | ^

don't inline atof without SSE2 for x86-64.
---
 include/bits/stdlib-float.h     |  4 ++--
 sysdeps/x86/Makefile            |  7 +++++++
 sysdeps/x86/bits/stdlib-float.h | 29 +++++++++++++++++++++++++++++
 sysdeps/x86/tst-stdlib.c        | 27 +++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 sysdeps/x86/bits/stdlib-float.h
 create mode 100644 sysdeps/x86/tst-stdlib.c
  

Comments

Florian Weimer March 18, 2021, 5:03 a.m. UTC | #1
* H. J. Lu via Libc-alpha:

> Since without SSE2, inlined atof fails with
>
> /usr/include/bits/stdlib-float.h: In function ‘atof’:
> /usr/include/bits/stdlib-float.h:26:1: error: SSE register return with SSE disabled
>    26 | {
>       | ^
>
> don't inline atof without SSE2 for x86-64.

Could you say publicly why you want to make this change, given that SSE2
is a mandatory part of x86-64?

> +#if defined __USE_EXTERN_INLINES && (!defined __x86_64__ || defined __SSE2__)
> +__extern_inline double
> +__NTH (atof (const char *__nptr))
> +{
> +  return strtod (__nptr, (char **) NULL);
> +}

This is just an inline function that is using the double type, not
something that uses an SSE2 type.  It's not even always_inline.  Many
other headers will have the same problem, especially for C++.  I think
you should teach GCC not to issue a diagnostic for inline functions
whose definitions are not used in a compilation.  This is how
-mgeneral-regs-only behaves on AArch64 already, if I'm not mistaken.

Thanks,
Florian
  

Patch

diff --git a/include/bits/stdlib-float.h b/include/bits/stdlib-float.h
index 54ab571981..4474d1d2dc 100644
--- a/include/bits/stdlib-float.h
+++ b/include/bits/stdlib-float.h
@@ -1,8 +1,8 @@ 
 /* No floating-point inline functions in rtld and for the conform tests.  */
 #ifdef _ISOMAC
-# include <stdlib/bits/stdlib-float.h>
+# include_next <bits/stdlib-float.h>
 #else
 # if !IS_IN (rtld)
-#  include <stdlib/bits/stdlib-float.h>
+#  include_next <bits/stdlib-float.h>
 # endif
 #endif
diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
index 3ca7bfefe4..d6381e73af 100644
--- a/sysdeps/x86/Makefile
+++ b/sysdeps/x86/Makefile
@@ -221,3 +221,10 @@  tests += \
 tests-static += \
   tst-sysconf-cache-linesize-static
 endif
+
+ifeq ($(subdir),stdlib)
+tests += \
+  tst-stdlib
+
+CFLAGS-tst-stdlib.c += -mno-sse
+endif
diff --git a/sysdeps/x86/bits/stdlib-float.h b/sysdeps/x86/bits/stdlib-float.h
new file mode 100644
index 0000000000..17be04b927
--- /dev/null
+++ b/sysdeps/x86/bits/stdlib-float.h
@@ -0,0 +1,29 @@ 
+/* Floating-point inline functions for stdlib.h.
+   Copyright (C) 2012-2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _STDLIB_H
+# error "Never use <bits/stdlib-float.h> directly; include <stdlib.h> instead."
+#endif
+
+#if defined __USE_EXTERN_INLINES && (!defined __x86_64__ || defined __SSE2__)
+__extern_inline double
+__NTH (atof (const char *__nptr))
+{
+  return strtod (__nptr, (char **) NULL);
+}
+#endif /* Optimizing and Inlining.  */
diff --git a/sysdeps/x86/tst-stdlib.c b/sysdeps/x86/tst-stdlib.c
new file mode 100644
index 0000000000..226eb35a5d
--- /dev/null
+++ b/sysdeps/x86/tst-stdlib.c
@@ -0,0 +1,27 @@ 
+/* Test <stdlib.h> with -mno-sse.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdlib.h>
+
+static int
+do_test (void)
+{
+  return 0;
+}
+
+#include <support/test-driver.c>