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/properties/architectures.h" |
15 | #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
16 | |
17 | namespace LIBC_NAMESPACE { |
18 | namespace fputil { |
19 | |
20 | // Implement a simple wrapper for multiply-add operation: |
21 | // multiply_add(x, y, z) = x*y + z |
22 | // which uses FMA instructions to speed up if available. |
23 | |
24 | template <typename T> |
25 | LIBC_INLINE cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T> |
26 | multiply_add(const T &x, const T &y, const T &z) { |
27 | return x * y + z; |
28 | } |
29 | |
30 | template <typename T> |
31 | LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T> |
32 | multiply_add(T x, T y, T z) { |
33 | return x * y + z; |
34 | } |
35 | |
36 | } // namespace fputil |
37 | } // namespace LIBC_NAMESPACE |
38 | |
39 | #if defined(LIBC_TARGET_CPU_HAS_FMA) |
40 | |
41 | // FMA instructions are available. |
42 | #include "FMA.h" |
43 | |
44 | namespace LIBC_NAMESPACE { |
45 | namespace fputil { |
46 | |
47 | LIBC_INLINE float multiply_add(float x, float y, float z) { |
48 | return fma(x, y, z); |
49 | } |
50 | |
51 | LIBC_INLINE double multiply_add(double x, double y, double z) { |
52 | return fma(x, y, z); |
53 | } |
54 | |
55 | } // namespace fputil |
56 | } // namespace LIBC_NAMESPACE |
57 | |
58 | #endif // LIBC_TARGET_CPU_HAS_FMA |
59 | |
60 | #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H |
61 | |