summaryrefslogtreecommitdiff
path: root/src/__support/FPUtil/riscv/FMA.h
blob: f01962174f16f987df7fa35f66fffc489e5bccef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//===-- RISCV 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_RISCV_FMA_H
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_RISCV_FMA_H

#include "src/__support/macros/attributes.h" // LIBC_INLINE
#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_ANY_RISCV)
#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 LIBC_NAMESPACE {
namespace fputil {

#ifdef __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;
  LIBC_INLINE_ASM("fmadd.s %0, %1, %2, %3\n\t"
                  : "=f"(result)
                  : "f"(x), "f"(y), "f"(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;
  LIBC_INLINE_ASM("fmadd.d %0, %1, %2, %3\n\t"
                  : "=f"(result)
                  : "f"(x), "f"(y), "f"(z));
  return result;
}
#endif // __riscv_flen >= 64
#endif // __riscv_flen

} // namespace fputil
} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_RISCV_FMA_H