xref: /aosp_15_r20/external/llvm-libc/src/__support/FPUtil/multiply_add.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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>
multiply_add(const T & x,const T & y,const T & z)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>
multiply_add(T x,T y,T z)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 
multiply_add(float x,float y,float z)49 LIBC_INLINE float multiply_add(float x, float y, float z) {
50   return __builtin_fmaf(x, y, z);
51 }
52 
multiply_add(double x,double y,double z)53 LIBC_INLINE double multiply_add(double x, double y, double z) {
54   return __builtin_fma(x, y, z);
55 }
56 
57 } // namespace fputil
58 } // namespace LIBC_NAMESPACE_DECL
59 
60 #endif // LIBC_TARGET_CPU_HAS_FMA
61 
62 #endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H
63