[libc-commits] [libc] f10ec8c - [libc][riscv] Check if we have F or D extension before using them (#79036)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 22 11:33:18 PST 2024


Author: Petr Hosek
Date: 2024-01-22T11:33:13-08:00
New Revision: f10ec8c77403fd722b3f50710cb73757da4383e4

URL: https://github.com/llvm/llvm-project/commit/f10ec8c77403fd722b3f50710cb73757da4383e4
DIFF: https://github.com/llvm/llvm-project/commit/f10ec8c77403fd722b3f50710cb73757da4383e4.diff

LOG: [libc][riscv] Check if we have F or D extension before using them (#79036)

We shouldn't be using instructions that require F or D extensions
unconditionally before checking if those instructions are available.

Added: 
    

Modified: 
    libc/src/__support/FPUtil/riscv/FMA.h
    libc/src/__support/FPUtil/riscv/sqrt.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/riscv/FMA.h b/libc/src/__support/FPUtil/riscv/FMA.h
index 32bef1ea8843e1..f01962174f16f9 100644
--- a/libc/src/__support/FPUtil/riscv/FMA.h
+++ b/libc/src/__support/FPUtil/riscv/FMA.h
@@ -26,6 +26,7 @@
 namespace LIBC_NAMESPACE {
 namespace fputil {
 
+#ifdef __riscv_flen
 template <typename T>
 LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, float>, T> fma(T x, T y, T z) {
   float result;
@@ -35,6 +36,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, float>, T> fma(T x, T y, T z) {
   return result;
 }
 
+#if __riscv_flen >= 64
 template <typename T>
 LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, double>, T> fma(T x, T y, T z) {
   double result;
@@ -43,6 +45,8 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, double>, T> fma(T x, T y, T z) {
                   : "f"(x), "f"(y), "f"(z));
   return result;
 }
+#endif // __riscv_flen >= 64
+#endif // __riscv_flen
 
 } // namespace fputil
 } // namespace LIBC_NAMESPACE

diff  --git a/libc/src/__support/FPUtil/riscv/sqrt.h b/libc/src/__support/FPUtil/riscv/sqrt.h
index a42687004639b4..a1c436d0ebb1d0 100644
--- a/libc/src/__support/FPUtil/riscv/sqrt.h
+++ b/libc/src/__support/FPUtil/riscv/sqrt.h
@@ -21,17 +21,21 @@
 namespace LIBC_NAMESPACE {
 namespace fputil {
 
+#ifdef __riscv_flen
 template <> LIBC_INLINE float sqrt<float>(float x) {
   float result;
   __asm__ __volatile__("fsqrt.s %0, %1\n\t" : "=f"(result) : "f"(x));
   return result;
 }
 
+#if __riscv_flen >= 64
 template <> LIBC_INLINE double sqrt<double>(double x) {
   double result;
   __asm__ __volatile__("fsqrt.d %0, %1\n\t" : "=f"(result) : "f"(x));
   return result;
 }
+#endif // __riscv_flen >= 64
+#endif // __riscv_flen
 
 } // namespace fputil
 } // namespace LIBC_NAMESPACE


        


More information about the libc-commits mailing list