Skip to content

Commit f10ec8c

Browse files
authored
[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.
1 parent bfd12f3 commit f10ec8c

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

libc/src/__support/FPUtil/riscv/FMA.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace LIBC_NAMESPACE {
2727
namespace fputil {
2828

29+
#ifdef __riscv_flen
2930
template <typename T>
3031
LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, float>, T> fma(T x, T y, T z) {
3132
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) {
3536
return result;
3637
}
3738

39+
#if __riscv_flen >= 64
3840
template <typename T>
3941
LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, double>, T> fma(T x, T y, T z) {
4042
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) {
4345
: "f"(x), "f"(y), "f"(z));
4446
return result;
4547
}
48+
#endif // __riscv_flen >= 64
49+
#endif // __riscv_flen
4650

4751
} // namespace fputil
4852
} // namespace LIBC_NAMESPACE

libc/src/__support/FPUtil/riscv/sqrt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@
2121
namespace LIBC_NAMESPACE {
2222
namespace fputil {
2323

24+
#ifdef __riscv_flen
2425
template <> LIBC_INLINE float sqrt<float>(float x) {
2526
float result;
2627
__asm__ __volatile__("fsqrt.s %0, %1\n\t" : "=f"(result) : "f"(x));
2728
return result;
2829
}
2930

31+
#if __riscv_flen >= 64
3032
template <> LIBC_INLINE double sqrt<double>(double x) {
3133
double result;
3234
__asm__ __volatile__("fsqrt.d %0, %1\n\t" : "=f"(result) : "f"(x));
3335
return result;
3436
}
37+
#endif // __riscv_flen >= 64
38+
#endif // __riscv_flen
3539

3640
} // namespace fputil
3741
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)