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 | |
| 18 | namespace LIBC_NAMESPACE_DECL { |
| 19 | namespace 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 | |
| 25 | template <typename T> |
| 26 | LIBC_INLINE cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T> |
| 27 | multiply_add(const T &x, const T &y, const T &z) { |
| 28 | return x * y + z; |
| 29 | } |
| 30 | |
| 31 | template <typename T> |
| 32 | LIBC_INLINE cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T> |
| 33 | multiply_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 | |
| 46 | namespace LIBC_NAMESPACE_DECL { |
| 47 | namespace fputil { |
| 48 | |
| 49 | #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT |
| 50 | LIBC_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 |
| 60 | LIBC_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.
