diff --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt --- a/libc/config/gpu/entrypoints.txt +++ b/libc/config/gpu/entrypoints.txt @@ -83,6 +83,8 @@ set(TARGET_LIBM_ENTRYPOINTS # math.h entrypoints + libc.src.math.fma + libc.src.math.fmaf libc.src.math.sin libc.src.math.round libc.src.math.roundf diff --git a/libc/src/__support/FPUtil/FMA.h b/libc/src/__support/FPUtil/FMA.h --- a/libc/src/__support/FPUtil/FMA.h +++ b/libc/src/__support/FPUtil/FMA.h @@ -20,6 +20,8 @@ #include "aarch64/FMA.h" #elif defined(LIBC_TARGET_ARCH_IS_RISCV64) #include "riscv64/FMA.h" +#elif defined(LIBC_TARGET_ARCH_IS_GPU) +#include "gpu/FMA.h" #endif #else diff --git a/libc/src/__support/FPUtil/gpu/FMA.h b/libc/src/__support/FPUtil/gpu/FMA.h new file mode 100644 --- /dev/null +++ b/libc/src/__support/FPUtil/gpu/FMA.h @@ -0,0 +1,33 @@ +//===-- GPU implementations of the fma function -----------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_SUPPORT_FPUTIL_GPU_FMA_H +#define LLVM_LIBC_SRC_SUPPORT_FPUTIL_GPU_FMA_H + +// These intrinsics map to the FMA instrunctions in the target ISA for the GPU. +// The default rounding mode generated from these will be to the nearest even. +static_assert(__has_builtin(__builtin_fma), "FMA builtins must be defined"); +static_assert(__has_builtin(__builtin_fmaf), "FMA builtins must be defined"); + +namespace __llvm_libc { +namespace fputil { + +template +LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { + __builtin_fmaf(x, y, z); +} + +template +LIBC_INLINE cpp::enable_if_t, T> fma(T x, T y, T z) { + __builtin_fma(x, y, z); +} + +} // namespace fputil +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SUPPORT_FPUTIL_GPU_FMA_H diff --git a/libc/src/__support/macros/properties/cpu_features.h b/libc/src/__support/macros/properties/cpu_features.h --- a/libc/src/__support/macros/properties/cpu_features.h +++ b/libc/src/__support/macros/properties/cpu_features.h @@ -37,7 +37,7 @@ #endif #if defined(__ARM_FEATURE_FMA) || (defined(__AVX2__) && defined(__FMA__)) || \ - defined(__LIBC_RISCV_USE_FMA) + defined(__NVPTX__) || defined(__AMDGPU__) || defined(__LIBC_RISCV_USE_FMA) #define LIBC_TARGET_CPU_HAS_FMA #endif