diff mbox series

[19/30] x86_64: Add glibc-hwcaps support

Message ID 6b56704d61b2fddf9e03eb5d0781d0fff57db96a.1592836143.git.fweimer@redhat.com
State Superseded
Headers show
Series RFC: elf: glibc-hwcaps support | expand

Commit Message

Florian Weimer June 22, 2020, 3:14 p.m. UTC
Details of the supported CPU flags and the names are still subject to
changes.
---
 sysdeps/x86_64/dl-hwcaps-subdirs.c | 73 ++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 sysdeps/x86_64/dl-hwcaps-subdirs.c

Comments

Florian Weimer June 25, 2020, 10:13 a.m. UTC | #1
* Florian Weimer via Libc-alpha:

> Details of the supported CPU flags and the names are still subject to
> changes.
> +int32_t
> +_dl_hwcaps_subdirs_active (void)
> +{
> +  const struct cpu_features *cpu_features = __get_cpu_features ();
> +  int32_t result = 0;
> +  int32_t bit = 1 << 3;
> +
> +  /* Test in reverse preference order.  */
> +
> +  /* x86-100.  */
> +  if (!(CPU_FEATURES_ARCH_P (cpu_features, CMPXCHG16B_Usable)
> +        && CPU_FEATURES_ARCH_P (cpu_features, POPCNT_Usable)
> +        && CPU_FEATURES_ARCH_P (cpu_features, SSE3_Usable)
> +        && CPU_FEATURES_ARCH_P (cpu_features, SSE4_1_Usable)
> +        && CPU_FEATURES_ARCH_P (cpu_features, SSE4_2_Usable)
> +        && CPU_FEATURES_ARCH_P (cpu_features, SSSE3_Usable)))
> +    return result;

This misuses CPU_FEATURES_ARCH_P, and the recent <cpu-features.h> update
on the master branch makes it finally fail to compile.  The version
below should fix the build and also correct the feature detection logic.

Thanks,
Florian

diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c
new file mode 100644
index 0000000000..1a90854b69
--- /dev/null
+++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c
@@ -0,0 +1,73 @@
+/* Architecture-specific glibc-hwcaps subdirectories.  x86 version.
+   Copyright (C) 2020 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 <dl-hwcaps.h>
+#include <cpu-features.h>
+
+const char _dl_hwcaps_subdirs[] = "x86-103:x86-102:x86-101:x86-100";
+
+int32_t
+_dl_hwcaps_subdirs_active (void)
+{
+  const struct cpu_features *cpu_features = __get_cpu_features ();
+  int32_t result = 0;
+  int32_t bit = 1 << 3;
+
+  /* Test in reverse preference order.  */
+
+  /* x86-100.  */
+  if (!(CPU_FEATURES_CPU_P (cpu_features, CMPXCHG16B)
+        && CPU_FEATURES_CPU_P (cpu_features, POPCNT)
+        && CPU_FEATURES_CPU_P (cpu_features, SSE3)
+        && CPU_FEATURES_CPU_P (cpu_features, SSE4_1)
+        && CPU_FEATURES_CPU_P (cpu_features, SSE4_2)
+        && CPU_FEATURES_CPU_P (cpu_features, SSSE3)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+  /* x86-101.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, AVX_Usable)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+  /* x86-102.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, AVX2_Usable)
+        && CPU_FEATURES_CPU_P (cpu_features, BMI1)
+        && CPU_FEATURES_CPU_P (cpu_features, BMI2)
+        && CPU_FEATURES_ARCH_P (cpu_features, F16C_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, FMA_Usable)
+        && CPU_FEATURES_CPU_P (cpu_features, LZCNT)
+        && CPU_FEATURES_CPU_P (cpu_features, MOVBE)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+ /* x86-103.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, AVX512F_Usable)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512BW)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512CD)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512DQ)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512VL)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+  return result;
+}
H.J. Lu June 25, 2020, 1:50 p.m. UTC | #2
On Thu, Jun 25, 2020 at 3:14 AM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> * Florian Weimer via Libc-alpha:
>
> > Details of the supported CPU flags and the names are still subject to
> > changes.
> > +int32_t
> > +_dl_hwcaps_subdirs_active (void)
> > +{
> > +  const struct cpu_features *cpu_features = __get_cpu_features ();
> > +  int32_t result = 0;
> > +  int32_t bit = 1 << 3;
> > +
> > +  /* Test in reverse preference order.  */
> > +
> > +  /* x86-100.  */
> > +  if (!(CPU_FEATURES_ARCH_P (cpu_features, CMPXCHG16B_Usable)
> > +        && CPU_FEATURES_ARCH_P (cpu_features, POPCNT_Usable)
> > +        && CPU_FEATURES_ARCH_P (cpu_features, SSE3_Usable)
> > +        && CPU_FEATURES_ARCH_P (cpu_features, SSE4_1_Usable)
> > +        && CPU_FEATURES_ARCH_P (cpu_features, SSE4_2_Usable)
> > +        && CPU_FEATURES_ARCH_P (cpu_features, SSSE3_Usable)))
> > +    return result;
>
> This misuses CPU_FEATURES_ARCH_P, and the recent <cpu-features.h> update
> on the master branch makes it finally fail to compile.  The version
> below should fix the build and also correct the feature detection logic.
>
> Thanks,
> Florian
>
> diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c
> new file mode 100644
> index 0000000000..1a90854b69
> --- /dev/null
> +++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c
> @@ -0,0 +1,73 @@
> +/* Architecture-specific glibc-hwcaps subdirectories.  x86 version.
> +   Copyright (C) 2020 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 <dl-hwcaps.h>
> +#include <cpu-features.h>
> +
> +const char _dl_hwcaps_subdirs[] = "x86-103:x86-102:x86-101:x86-100";
>

I understand why you use digits.  But it is very cryptic.  Since it is
user visible,
how can one tell what 103 means without looking at glibc source?
diff mbox series

Patch

diff --git a/sysdeps/x86_64/dl-hwcaps-subdirs.c b/sysdeps/x86_64/dl-hwcaps-subdirs.c
new file mode 100644
index 0000000000..4a8fae976e
--- /dev/null
+++ b/sysdeps/x86_64/dl-hwcaps-subdirs.c
@@ -0,0 +1,73 @@ 
+/* Architecture-specific glibc-hwcaps subdirectories.  x86 version.
+   Copyright (C) 2020 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 <dl-hwcaps.h>
+#include <cpu-features.h>
+
+const char _dl_hwcaps_subdirs[] = "x86-103:x86-102:x86-101:x86-100";
+
+int32_t
+_dl_hwcaps_subdirs_active (void)
+{
+  const struct cpu_features *cpu_features = __get_cpu_features ();
+  int32_t result = 0;
+  int32_t bit = 1 << 3;
+
+  /* Test in reverse preference order.  */
+
+  /* x86-100.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, CMPXCHG16B_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, POPCNT_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, SSE3_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, SSE4_1_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, SSE4_2_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, SSSE3_Usable)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+  /* x86-101.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, AVX_Usable)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+  /* x86-102.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, AVX2_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, BMI1_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, BMI2_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, F16C_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, FMA_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, LZCNT_Usable)
+        && CPU_FEATURES_ARCH_P (cpu_features, MOVBE_Usable)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+ /* x86-103.  */
+  if (!(CPU_FEATURES_ARCH_P (cpu_features, AVX512F_Usable)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512BW)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512CD)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512DQ)
+        && CPU_FEATURES_CPU_P (cpu_features, AVX512VL)))
+    return result;
+  result |= bit;
+  bit >>= 1;
+
+  return result;
+}