[libc-commits] [PATCH] D145592: [libc] Add support for fma in riscv
Mikhail Ramalho via Phabricator via libc-commits
libc-commits at lists.llvm.org
Wed Mar 8 09:24:06 PST 2023
mikhail.ramalho created this revision.
mikhail.ramalho added a reviewer: sivachandra.
Herald added subscribers: libc-commits, luke, VincentWu, vkmr, frasercrmck, ecnelises, evandro, luismarques, apazos, sameer.abuasal, tschuett, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, arichardson.
Herald added projects: libc-project, All.
mikhail.ramalho requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D145592
Files:
libc/src/__support/FPUtil/FMA.h
libc/src/__support/FPUtil/riscv64/FMA.h
libc/src/__support/macros/properties/cpu_features.h
Index: libc/src/__support/macros/properties/cpu_features.h
===================================================================
--- libc/src/__support/macros/properties/cpu_features.h
+++ libc/src/__support/macros/properties/cpu_features.h
@@ -36,7 +36,8 @@
#define LIBC_TARGET_CPU_HAS_AVX512BW
#endif
-#if defined(__ARM_FEATURE_FMA) || defined(__AVX2__) || defined(__FMA__)
+#if defined(__ARM_FEATURE_FMA) || defined(__AVX2__) || defined(__FMA__) || \
+ defined(__riscv)
#define LIBC_TARGET_CPU_HAS_FMA
#endif
Index: libc/src/__support/FPUtil/riscv64/FMA.h
===================================================================
--- /dev/null
+++ libc/src/__support/FPUtil/riscv64/FMA.h
@@ -0,0 +1,49 @@
+//===-- Aarch64 implementations of the fma function -------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_AARCH64_FMA_H
+#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_AARCH64_FMA_H
+
+#include "src/__support/macros/properties/architectures.h"
+#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
+
+#if !defined(LIBC_TARGET_ARCH_IS_RISCV64)
+#error "Invalid include"
+#endif
+
+#if !defined(LIBC_TARGET_CPU_HAS_FMA)
+#error "FMA instructions are not supported"
+#endif
+
+#include "src/__support/CPP/type_traits.h"
+
+namespace __llvm_libc {
+namespace fputil {
+
+template <typename T>
+cpp::enable_if_t<cpp::is_same_v<T, float>, T> fma(T x, T y, T z) {
+ float result;
+ __asm__ __volatile__("fmadd.s %0, %1, %2, %3\n\t"
+ : "=f"(result)
+ : "f"(x), "f"(y), "f"(z));
+ return result;
+}
+
+template <typename T>
+cpp::enable_if_t<cpp::is_same_v<T, double>, T> fma(T x, T y, T z) {
+ double result;
+ __asm__ __volatile__("fmadd.d %0, %1, %2, %3\n\t"
+ : "=f"(result)
+ : "f"(x), "f"(y), "f"(z));
+ return result;
+}
+
+} // namespace fputil
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_AARCH64_FMA_H
Index: libc/src/__support/FPUtil/FMA.h
===================================================================
--- libc/src/__support/FPUtil/FMA.h
+++ libc/src/__support/FPUtil/FMA.h
@@ -18,6 +18,8 @@
#include "x86_64/FMA.h"
#elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
#include "aarch64/FMA.h"
+#elif defined(LIBC_TARGET_ARCH_IS_RISCV64)
+#include "riscv64/FMA.h"
#endif
#else
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145592.503408.patch
Type: text/x-patch
Size: 2649 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230308/de045b53/attachment.bin>
More information about the libc-commits
mailing list