public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] RISC-V: Add a macro definition file to check whether the compiler supports extensions.
@ 2024-07-02 13:41 Julian Zhu
  2024-07-02 13:41 ` [PATCH 2/3] RISC-V: Add compiler support check in string-fza.h and string-fzi.h Julian Zhu
  2024-07-02 13:41 ` [PATCH 3/3] RISC-V: Use builtins for ffs and ffsll while supported extension available Julian Zhu
  0 siblings, 2 replies; 3+ messages in thread
From: Julian Zhu @ 2024-07-02 13:41 UTC (permalink / raw)
  To: libc-alpha; +Cc: adhemerval.zanella, xry111, Julian Zhu

Some extensions of RISC-V are only available in relatively newer compiler. These macro definition can prevent the use of extensions in unsupported compiler.

Signed-off-by: Julian Zhu <jz531210@gmail.com>
---
 sysdeps/riscv/compiler-ext-avail.h | 53 ++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 sysdeps/riscv/compiler-ext-avail.h

diff --git a/sysdeps/riscv/compiler-ext-avail.h b/sysdeps/riscv/compiler-ext-avail.h
new file mode 100644
index 0000000000..6b2d92e437
--- /dev/null
+++ b/sysdeps/riscv/compiler-ext-avail.h
@@ -0,0 +1,53 @@
+/* Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   This file is used to define whether extensions are available
+   in the compiler.
+
+   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 _COMPILER_EXT_AVAIL_H
+#define _COMPILER_EXT_AVAIL_H 1
+
+/* Zba, Zbb, Zbc, and Zbs are available after gcc 12 or llvm 14 */
+
+#if (defined __GNUC__                                                         \
+     && (__GNUC__ > 12 || (__GNUC__ >= 12 && __GNUC_MINOR__ >= 1)))           \
+    || (defined __clang__ && __clang_major__ >= 14)
+#  define COMPILER_ZBA_AVAIL 1
+#  define COMPILER_ZBB_AVAIL 1
+#  define COMPILER_ZBC_AVAIL 1
+#  define COMPILER_ZBS_AVAIL 1
+#else
+#  define COMPILER_ZBA_AVAIL 0
+#  define COMPILER_ZBB_AVAIL 0
+#  define COMPILER_ZBC_AVAIL 0
+#  define COMPILER_ZBS_AVAIL 0
+#endif
+
+/* XTHeadBa, XTHeadBb, and XTHeadBs are available after gcc 13 or llvm 17 */
+#if (defined __GNUC__                                                         \
+     && (__GNUC__ > 13 || (__GNUC__ >= 13 && __GNUC_MINOR__ >= 1)))           \
+    || (defined __clang__ && __clang_major__ >= 17)
+#  define COMPILER_XTHEADBA_AVAIL 1
+#  define COMPILER_XTHEADBB_AVAIL 1
+#  define COMPILER_XTHEADBS_AVAIL 1
+#else
+#  define COMPILER_XTHEADBA_AVAIL 0
+#  define COMPILER_XTHEADBB_AVAIL 0
+#  define COMPILER_XTHEADBS_AVAIL 0
+#endif
+
+#endif /* compiler-ext-avail.h */
-- 
2.45.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] RISC-V: Add compiler support check in string-fza.h and string-fzi.h
  2024-07-02 13:41 [PATCH 1/3] RISC-V: Add a macro definition file to check whether the compiler supports extensions Julian Zhu
@ 2024-07-02 13:41 ` Julian Zhu
  2024-07-02 13:41 ` [PATCH 3/3] RISC-V: Use builtins for ffs and ffsll while supported extension available Julian Zhu
  1 sibling, 0 replies; 3+ messages in thread
From: Julian Zhu @ 2024-07-02 13:41 UTC (permalink / raw)
  To: libc-alpha; +Cc: adhemerval.zanella, xry111, Julian Zhu

Add macros from `compiler-ext-avail.h` to avoid using Zbb or XTheadBb in unsupported compiler versions.

Signed-off-by: Julian Zhu <jz531210@gmail.com>
---
 sysdeps/riscv/string-fza.h | 5 ++++-
 sysdeps/riscv/string-fzi.h | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/sysdeps/riscv/string-fza.h b/sysdeps/riscv/string-fza.h
index ee5c31317f..57fe39e66a 100644
--- a/sysdeps/riscv/string-fza.h
+++ b/sysdeps/riscv/string-fza.h
@@ -19,7 +19,10 @@
 #ifndef _RISCV_STRING_FZA_H
 #define _RISCV_STRING_FZA_H 1
 
-#if defined __riscv_zbb || defined __riscv_xtheadbb
+#include "compiler-ext-avail.h"
+
+#if (defined COMPILER_ZBB_AVAIL && defined __riscv_zbb)                       \
+    || (defined COMPILER_XTHEADBB_AVAIL && defined __riscv_xtheadbb)
 /* With bitmap extension we can use orc.b to find all zero bytes.  */
 # include <string-misc.h>
 # include <string-optype.h>
diff --git a/sysdeps/riscv/string-fzi.h b/sysdeps/riscv/string-fzi.h
index 66237c2f03..dccd759dca 100644
--- a/sysdeps/riscv/string-fzi.h
+++ b/sysdeps/riscv/string-fzi.h
@@ -19,7 +19,10 @@
 #ifndef _STRING_RISCV_FZI_H
 #define _STRING_RISCV_FZI_H 1
 
-#if defined __riscv_zbb || defined __riscv_xtheadbb
+#include "compiler-ext-avail.h"
+
+#if (defined COMPILER_ZBB_AVAIL && defined __riscv_zbb)                       \
+    || (defined COMPILER_XTHEADBB_AVAIL && defined __riscv_xtheadbb)
 # include <sysdeps/generic/string-fzi.h>
 #else
 /* Without bitmap clz/ctz extensions, it is faster to direct test the bits
-- 
2.45.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] RISC-V: Use builtins for ffs and ffsll while supported extension available
  2024-07-02 13:41 [PATCH 1/3] RISC-V: Add a macro definition file to check whether the compiler supports extensions Julian Zhu
  2024-07-02 13:41 ` [PATCH 2/3] RISC-V: Add compiler support check in string-fza.h and string-fzi.h Julian Zhu
@ 2024-07-02 13:41 ` Julian Zhu
  1 sibling, 0 replies; 3+ messages in thread
From: Julian Zhu @ 2024-07-02 13:41 UTC (permalink / raw)
  To: libc-alpha; +Cc: adhemerval.zanella, xry111, Julian Zhu

Hardware ctz/ctzw instructions are available in the RISC-V Zbb extension, and the similar ctz instruction th.ff1 is available in the RISC-V XTheadBb extension. We can get more simplified code compared to the generic implement of ffs/ffsll.

Signed-off-by: Julian Zhu <jz531210@gmail.com>
---
 sysdeps/riscv/math-use-builtins-ffs.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
 create mode 100644 sysdeps/riscv/math-use-builtins-ffs.h

diff --git a/sysdeps/riscv/math-use-builtins-ffs.h b/sysdeps/riscv/math-use-builtins-ffs.h
new file mode 100644
index 0000000000..e1ba6f76c7
--- /dev/null
+++ b/sysdeps/riscv/math-use-builtins-ffs.h
@@ -0,0 +1,10 @@
+#ifdef COMPILER_ZBB_AVAIL && defined __riscv_zbb
+#  define USE_FFS_BUILTIN 1
+#  define USE_FFSLL_BUILTIN 1
+#elif COMPILER_XTHEADBB_AVAIL && defined __riscv_xtheadbb
+#  define USE_FFS_BUILTIN 0
+#  define USE_FFSLL_BUILTIN 1
+#else
+#  define USE_FFS_BUILTIN 0
+#  define USE_FFSLL_BUILTIN 0
+#endif
-- 
2.45.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-07-02 13:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-02 13:41 [PATCH 1/3] RISC-V: Add a macro definition file to check whether the compiler supports extensions Julian Zhu
2024-07-02 13:41 ` [PATCH 2/3] RISC-V: Add compiler support check in string-fza.h and string-fzi.h Julian Zhu
2024-07-02 13:41 ` [PATCH 3/3] RISC-V: Use builtins for ffs and ffsll while supported extension available Julian Zhu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).