From ac763b9fdf5b8b6cc563b30c3fe90499f3706b13 Mon Sep 17 00:00:00 2001 From: "Mikhail R. Gadelha" Date: Wed, 8 Mar 2023 16:14:40 -0300 Subject: [PATCH] [libc] Add support for fma in riscv Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D145592 --- libc/src/__support/FPUtil/FMA.h | 2 + libc/src/__support/FPUtil/riscv64/FMA.h | 49 ++++++++++++++++++++++ .../src/__support/macros/properties/cpu_features.h | 3 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 libc/src/__support/FPUtil/riscv64/FMA.h diff --git a/libc/src/__support/FPUtil/FMA.h b/libc/src/__support/FPUtil/FMA.h index 05df9cb..79a465a 100644 --- a/libc/src/__support/FPUtil/FMA.h +++ b/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 diff --git a/libc/src/__support/FPUtil/riscv64/FMA.h b/libc/src/__support/FPUtil/riscv64/FMA.h new file mode 100644 index 0000000..9b003f1 --- /dev/null +++ b/libc/src/__support/FPUtil/riscv64/FMA.h @@ -0,0 +1,49 @@ +//===-- RISCV64 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_RISCV64_FMA_H +#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_RISCV64_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 +cpp::enable_if_t, 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 +cpp::enable_if_t, 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_RISCV64_FMA_H diff --git a/libc/src/__support/macros/properties/cpu_features.h b/libc/src/__support/macros/properties/cpu_features.h index b986351..9884d56 100644 --- a/libc/src/__support/macros/properties/cpu_features.h +++ b/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 -- 2.7.4