From 419a6848d8e68a730566931910729316e840f446 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Mon, 22 Jan 2024 18:54:11 +0000 Subject: [PATCH 1/2] [libc][riscv] Check if we have F or D extension before using them We shouldn't be using instructions that require F or D extensions unconditionally before checking if those instructions are available. --- libc/src/__support/FPUtil/riscv/FMA.h | 4 ++++ libc/src/__support/FPUtil/riscv/sqrt.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/libc/src/__support/FPUtil/riscv/FMA.h b/libc/src/__support/FPUtil/riscv/FMA.h index 32bef1ea8843e..1dd7c8c9a462d 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 { +#if defined(__riscv_flen) template LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { float result; @@ -35,6 +36,7 @@ LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { return result; } +#if __riscv_flen == 64 template LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { double result; @@ -43,6 +45,8 @@ LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { : "f"(x), "f"(y), "f"(z)); return result; } +#endif // __riscv_flen == 64 +#endif // defined(__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 a42687004639b..5ab027e617cc1 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 { +#if defined(__riscv_flen) template <> LIBC_INLINE float sqrt(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 x) { double result; __asm__ __volatile__("fsqrt.d %0, %1\n\t" : "=f"(result) : "f"(x)); return result; } +#endif // __riscv_flen == 64 +#endif // defined(__riscv_flen) } // namespace fputil } // namespace LIBC_NAMESPACE From 288c4da9e8167f4ba25cbf3a36e5b683ead88f32 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Mon, 22 Jan 2024 19:04:11 +0000 Subject: [PATCH 2/2] Use ifdef and >= 64 in the condition. --- libc/src/__support/FPUtil/riscv/FMA.h | 8 ++++---- libc/src/__support/FPUtil/riscv/sqrt.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libc/src/__support/FPUtil/riscv/FMA.h b/libc/src/__support/FPUtil/riscv/FMA.h index 1dd7c8c9a462d..f01962174f16f 100644 --- a/libc/src/__support/FPUtil/riscv/FMA.h +++ b/libc/src/__support/FPUtil/riscv/FMA.h @@ -26,7 +26,7 @@ namespace LIBC_NAMESPACE { namespace fputil { -#if defined(__riscv_flen) +#ifdef __riscv_flen template LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { float result; @@ -36,7 +36,7 @@ LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { return result; } -#if __riscv_flen == 64 +#if __riscv_flen >= 64 template LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { double result; @@ -45,8 +45,8 @@ LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { : "f"(x), "f"(y), "f"(z)); return result; } -#endif // __riscv_flen == 64 -#endif // defined(__riscv_flen) +#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 5ab027e617cc1..a1c436d0ebb1d 100644 --- a/libc/src/__support/FPUtil/riscv/sqrt.h +++ b/libc/src/__support/FPUtil/riscv/sqrt.h @@ -21,21 +21,21 @@ namespace LIBC_NAMESPACE { namespace fputil { -#if defined(__riscv_flen) +#ifdef __riscv_flen template <> LIBC_INLINE float sqrt(float x) { float result; __asm__ __volatile__("fsqrt.s %0, %1\n\t" : "=f"(result) : "f"(x)); return result; } -#if __riscv_flen == 64 +#if __riscv_flen >= 64 template <> LIBC_INLINE double sqrt(double x) { double result; __asm__ __volatile__("fsqrt.d %0, %1\n\t" : "=f"(result) : "f"(x)); return result; } -#endif // __riscv_flen == 64 -#endif // defined(__riscv_flen) +#endif // __riscv_flen >= 64 +#endif // __riscv_flen } // namespace fputil } // namespace LIBC_NAMESPACE