Warning: This file is not a C or C++ file. It does not have highlighting.

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 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 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)
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
46namespace LIBC_NAMESPACE_DECL {
47namespace fputil {
48
49#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
50LIBC_INLINE float multiply_add(float x, float y, float z) {
51#if __has_builtin(__builtin_elementwise_fma)
52 return __builtin_elementwise_fma(x, y, z);
53#else
54 return __builtin_fmaf(x, y, z);
55#endif
56}
57#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
58
59#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
60LIBC_INLINE double multiply_add(double x, double y, double z) {
61#if __has_builtin(__builtin_elementwise_fma)
62 return __builtin_elementwise_fma(x, y, z);
63#else
64 return __builtin_fma(x, y, z);
65#endif
66}
67#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
68
69} // namespace fputil
70} // namespace LIBC_NAMESPACE_DECL
71
72#endif // LIBC_TARGET_CPU_HAS_FMA
73
74#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H
75

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libc/src/__support/FPUtil/multiply_add.h