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