1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Common header for multiply-add implementations.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H
15#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H
16
17#include "src/__support/CPP/type_traits.h"
18#include "src/__support/common.h"
19#include "src/__support/macros/config.h"
20#include "src/__support/macros/properties/architectures.h"
21#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
22
23namespace LIBC_NAMESPACE_DECL {
24namespace fputil {
25
26// Implement a simple wrapper for multiply-add operation:
27// multiply_add(x, y, z) = x*y + z
28// which uses FMA instructions to speed up if available.
29
30template <typename T>
31LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T>
32multiply_add(const T &x, const T &y, const T &z) {
33 T prod = x * y;
34 return prod + z;
35}
36
37template <typename T>
38LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
39multiply_add(T x, T y, T z) {
40 T prod = x * y;
41 return prod + z;
42}
43
44} // namespace fputil
45} // namespace LIBC_NAMESPACE_DECL
46
47#if defined(LIBC_TARGET_CPU_HAS_FMA) && !defined(LIBC_USE_CONSTEXPR)
48
49// FMA instructions are available.
50// We use builtins directly instead of including FMA.h to avoid a circular
51// dependency: multiply_add.h -> FMA.h -> generic/FMA.h -> dyadic_float.h.
52//
53// TODO: for constexpr evaluation of multiply_add using FMA, we will need to
54// use the generic fma implementation from generic/FMA.h. But currently that
55// implementation will use dyadic_float.h , which in turns including this
56// multiply_add.h . We will need to break the dependency to enable constexpr
57// for other math functions.
58
59namespace LIBC_NAMESPACE_DECL {
60namespace fputil {
61
62#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
63LIBC_INLINE float multiply_add(float x, float y, float z) {
64#if __has_builtin(__builtin_elementwise_fma)
65 return __builtin_elementwise_fma(x, y, z);
66#else
67 return __builtin_fmaf(x, y, z);
68#endif
69}
70#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
71
72#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
73LIBC_INLINE double multiply_add(double x, double y, double z) {
74#if __has_builtin(__builtin_elementwise_fma)
75 return __builtin_elementwise_fma(x, y, z);
76#else
77 return __builtin_fma(x, y, z);
78#endif
79}
80#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
81
82} // namespace fputil
83} // namespace LIBC_NAMESPACE_DECL
84
85#endif // LIBC_TARGET_CPU_HAS_FMA
86
87#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H
88