[v2,08/15] RISC-V: string-fz[a, i].h: Make orc.b optimization explicit

Message ID 20240527111900.1060546-9-christoph.muellner@vrull.eu
State New
Headers
Series RISC-V: Add Zbb-optimized string routines as ifuncs |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Christoph Müllner May 27, 2024, 11:18 a.m. UTC
  RISC-V Zbb has the orc.b instruction, which can be used
in find_zero_all. T-Head has a similar instruction (th.tstnbz)
in their XTheadBb extension.

Currently, we pick the optimized code, whenever the corresponding
extension test macros are enabled (e.g. __riscv_zbb for Zbb).
However, these test macros are not set in case the extensions
are enabled via function attributes.

This patch adds a mechanism to enable the optimization explicitly
to overcome this limitation.  The old behavior (enabled optimization
via __riscv_zbb) remains the same.

By evaluating the value (instead of just checking if these macros are
defined) we could also explicitly disable the optimization by defining
the macros as 0 (e.g. #define USE_ZBB_ORCB 0).

Signed-off-by: Christoph Müllner <christoph.muellner@vrull.eu>
---
 sysdeps/riscv/string-fza.h | 22 ++++++++++++++++++++--
 sysdeps/riscv/string-fzi.h | 20 +++++++++++++++++++-
 2 files changed, 39 insertions(+), 3 deletions(-)
  

Patch

diff --git a/sysdeps/riscv/string-fza.h b/sysdeps/riscv/string-fza.h
index ee5c31317f..34d6972aea 100644
--- a/sysdeps/riscv/string-fza.h
+++ b/sysdeps/riscv/string-fza.h
@@ -19,7 +19,25 @@ 
 #ifndef _RISCV_STRING_FZA_H
 #define _RISCV_STRING_FZA_H 1
 
-#if defined __riscv_zbb || defined __riscv_xtheadbb
+/* Enable use of orc.b if Zbb is available.  */
+#ifndef USE_ZBB_ORCB
+# ifdef __riscv_zbb
+#  define USE_ZBB_ORCB 1
+# else
+#  define USE_ZBB_ORCB 0
+# endif
+#endif
+
+/* Enable use of th.tstnbz if XTheadBb is available.  */
+#ifndef USE_XTHEADBB_TSTNBZ
+# ifdef __riscv_xtheadbb
+#  define USE_XTHEADBB_TSTNBZ 1
+# else
+#  define USE_XTHEADBB_TSTNBZ 0
+# endif
+#endif
+
+#if USE_ZBB_ORCB || USE_XTHEADBB_TSTNBZ
 /* With bitmap extension we can use orc.b to find all zero bytes.  */
 # include <string-misc.h>
 # include <string-optype.h>
@@ -32,7 +50,7 @@  static __always_inline find_t
 find_zero_all (op_t x)
 {
   find_t r;
-#ifdef __riscv_xtheadbb
+#if USE_XTHEADBB_TSTNBZ
   asm ("th.tstnbz %0, %1" : "=r" (r) : "r" (x));
   return r;
 #else
diff --git a/sysdeps/riscv/string-fzi.h b/sysdeps/riscv/string-fzi.h
index 66237c2f03..e3c3a96d62 100644
--- a/sysdeps/riscv/string-fzi.h
+++ b/sysdeps/riscv/string-fzi.h
@@ -19,7 +19,25 @@ 
 #ifndef _STRING_RISCV_FZI_H
 #define _STRING_RISCV_FZI_H 1
 
-#if defined __riscv_zbb || defined __riscv_xtheadbb
+/* Enable use of orc.b if Zbb is available.  */
+#ifndef USE_ZBB_ORCB
+# ifdef __riscv_zbb
+#  define USE_ZBB_ORCB 1
+# else
+#  define USE_ZBB_ORCB 0
+# endif
+#endif
+
+/* Enable use of th.tstnbz if XTheadBb is available.  */
+#ifndef USE_XTHEADBB_TSTNBZ
+# ifdef __riscv_xtheadbb
+#  define USE_XTHEADBB_TSTNBZ 1
+# else
+#  define USE_XTHEADBB_TSTNBZ 0
+# endif
+#endif
+
+#if USE_ZBB_ORCB || USE_XTHEADBB_TSTNBZ
 # include <sysdeps/generic/string-fzi.h>
 #else
 /* Without bitmap clz/ctz extensions, it is faster to direct test the bits