75 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H10#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H11 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_FMA17 18namespace LIBC_NAMESPACE_DECL {19namespace fputil {20 21// Implement a simple wrapper for multiply-add operation:22// multiply_add(x, y, z) = x*y + z23// 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 static constexpr 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 fputil38} // namespace LIBC_NAMESPACE_DECL39 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 circular44// 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_FLOAT50LIBC_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#else54 return __builtin_fmaf(x, y, z);55#endif56}57#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT58 59#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE60LIBC_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#else64 return __builtin_fma(x, y, z);65#endif66}67#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE68 69} // namespace fputil70} // namespace LIBC_NAMESPACE_DECL71 72#endif // LIBC_TARGET_CPU_HAS_FMA73 74#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H75