Skip to content

Commit f3aceee

Browse files
authored
[libc][math][c23] Add f16fmaf C23 math function (#95483)
Part of #93566.
1 parent bbe9119 commit f3aceee

29 files changed

+501
-300
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
503503
libc.src.math.canonicalizef16
504504
libc.src.math.ceilf16
505505
libc.src.math.copysignf16
506+
libc.src.math.f16fmaf
506507
libc.src.math.f16sqrtf
507508
libc.src.math.fabsf16
508509
libc.src.math.fdimf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
535535
libc.src.math.canonicalizef16
536536
libc.src.math.ceilf16
537537
libc.src.math.copysignf16
538+
libc.src.math.f16fmaf
538539
libc.src.math.f16sqrtf
539540
libc.src.math.fabsf16
540541
libc.src.math.fdimf16

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ Basic Operations
124124
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
125125
| dsub | N/A | N/A | | N/A | | 7.12.14.2 | F.10.11 |
126126
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
127+
| f16fma | |check| | | | N/A | | 7.12.14.5 | F.10.11 |
128+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
127129
| fabs | |check| | |check| | |check| | |check| | |check| | 7.12.7.3 | F.10.4.3 |
128130
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
129131
| fadd | N/A | | | N/A | | 7.12.14.1 | F.10.11 |

libc/spec/stdc.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,11 @@ def StdC : StandardSpec<"stdc"> {
474474

475475
FunctionSpec<"fmul", RetValSpec<FloatType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
476476

477-
478477
FunctionSpec<"fma", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
479478
FunctionSpec<"fmaf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<FloatType>]>,
480479

480+
GuardedFunctionSpec<"f16fmaf", RetValSpec<Float16Type>, [ArgSpec<FloatType>, ArgSpec<FloatType>, ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
481+
481482
FunctionSpec<"fmod", RetValSpec<DoubleType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,
482483
FunctionSpec<"fmodf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,
483484
FunctionSpec<"fmodl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>, ArgSpec<LongDoubleType>]>,

libc/src/__support/FPUtil/FMA.h

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,29 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_FMA_H
1111

1212
#include "src/__support/CPP/type_traits.h"
13+
#include "src/__support/FPUtil/generic/FMA.h"
1314
#include "src/__support/macros/properties/architectures.h"
1415
#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
1516

16-
#if defined(LIBC_TARGET_CPU_HAS_FMA)
17-
1817
namespace LIBC_NAMESPACE {
1918
namespace fputil {
2019

21-
template <typename T>
22-
LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, float>, T> fma(T x, T y, T z) {
23-
return __builtin_fmaf(x, y, z);
20+
template <typename OutType, typename InType>
21+
LIBC_INLINE OutType fma(InType x, InType y, InType z) {
22+
return generic::fma<OutType>(x, y, z);
2423
}
2524

26-
template <typename T>
27-
LIBC_INLINE cpp::enable_if_t<cpp::is_same_v<T, double>, T> fma(T x, T y, T z) {
28-
return __builtin_fma(x, y, z);
25+
#ifdef LIBC_TARGET_CPU_HAS_FMA
26+
template <> LIBC_INLINE float fma(float x, float y, float z) {
27+
return __builtin_fmaf(x, y, z);
2928
}
3029

31-
} // namespace fputil
32-
} // namespace LIBC_NAMESPACE
33-
34-
#else
35-
// FMA instructions are not available
36-
#include "generic/FMA.h"
37-
38-
namespace LIBC_NAMESPACE {
39-
namespace fputil {
40-
41-
template <typename T> LIBC_INLINE T fma(T x, T y, T z) {
42-
return generic::fma(x, y, z);
30+
template <> LIBC_INLINE double fma(double x, double y, double z) {
31+
return __builtin_fma(x, y, z);
4332
}
33+
#endif // LIBC_TARGET_CPU_HAS_FMA
4434

4535
} // namespace fputil
4636
} // namespace LIBC_NAMESPACE
4737

48-
#endif
49-
5038
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_FMA_H

libc/src/__support/FPUtil/generic/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ add_header_library(
1919
HDRS
2020
FMA.h
2121
DEPENDS
22+
libc.hdr.fenv_macros
2223
libc.src.__support.common
2324
libc.src.__support.CPP.bit
25+
libc.src.__support.CPP.limits
2426
libc.src.__support.CPP.type_traits
2527
libc.src.__support.FPUtil.fenv_impl
2628
libc.src.__support.FPUtil.fp_bits
2729
libc.src.__support.FPUtil.rounding_mode
30+
libc.src.__support.big_int
2831
libc.src.__support.macros.optimization
2932
libc.src.__support.uint128
3033
)

0 commit comments

Comments
 (0)