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/architectures.h" 13 14 namespace __llvm_libc { 15 namespace fputil { 16 17 // Implement a simple wrapper for multiply-add operation: 18 // multiply_add(x, y, z) = x*y + z 19 // which uses FMA instructions to speed up if available. 20 21 template <typename T> static inline T multiply_add(T x, T y, T z) { 22 return x * y + z; 23 } 24 25 } // namespace fputil 26 } // namespace __llvm_libc 27 28 #if defined(LIBC_TARGET_HAS_FMA) 29 30 // FMA instructions are available. 31 #include "FMA.h" 32 33 namespace __llvm_libc { 34 namespace fputil { 35 36 template <> inline float multiply_add<float>(float x, float y, float z) { 37 return fma(x, y, z); 38 } 39 40 template <> inline double multiply_add<double>(double x, double y, double z) { 41 return fma(x, y, z); 42 } 43 44 } // namespace fputil 45 } // namespace __llvm_libc 46 47 #endif // LIBC_TARGET_HAS_FMA 48 49 #endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_MULTIPLY_ADD_H 50