Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 22, 2024

Conversation

petrhosek
Copy link
Member

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

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

llvmbot commented Jan 22, 2024

@llvm/pr-subscribers-libc

Author: Petr Hosek (petrhosek)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/79036.diff

2 Files Affected:

  • (modified) libc/src/__support/FPUtil/riscv/FMA.h (+4)
  • (modified) libc/src/__support/FPUtil/riscv/sqrt.h (+4)
diff --git a/libc/src/__support/FPUtil/riscv/FMA.h b/libc/src/__support/FPUtil/riscv/FMA.h
index 32bef1ea8843e15..1dd7c8c9a462d41 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 <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 // 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 a42687004639b4b..5ab027e617cc18a 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>(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 // defined(__riscv_flen)
 
 } // namespace fputil
 } // namespace LIBC_NAMESPACE

@@ -26,6 +26,7 @@
namespace LIBC_NAMESPACE {
namespace fputil {

#if defined(__riscv_flen)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless you have a more complex expression, stick with #ifdef

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@petrhosek petrhosek merged commit f10ec8c into llvm:main Jan 22, 2024
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants