[libc-commits] [libc] [libc][FPUtil] Make hardware helpers usable in constant expressions (PR #242283)
Muhammad Bassiouni via libc-commits
libc-commits at lists.llvm.org
Sat Nov 14 10:22:52 PST 2026
================
@@ -63,40 +63,51 @@ multiply_add(T x, T y, T z) {
// use the generic fma implementation from generic/FMA.h. But currently that
// implementation will use dyadic_float.h , which in turns including this
// multiply_add.h . We will need to break the dependency to enable constexpr
-// for other math functions.
+// for other math functions. Until then, constant evaluation falls back to the
+// non-fused `x * y + z`, exactly like the generic implementation above.
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-LIBC_INLINE float multiply_add(float x, float y, float z) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT float multiply_add(float x, float y,
+ float z) {
+ if (cpp::is_constant_evaluated()) {
+ return x * y + z;
+ } else {
#if __has_builtin(__builtin_elementwise_fma)
- return __builtin_elementwise_fma(x, y, z);
+ return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
- return _mm_cvtss_f32(
- _mm_fmadd_ss(_mm_set_ss(x), _mm_set_ss(y), _mm_set_ss(z)));
+ return _mm_cvtss_f32(
+ _mm_fmadd_ss(_mm_set_ss(x), _mm_set_ss(y), _mm_set_ss(z)));
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
- return vget_lane_f32(vfma_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
- 0);
+ return vget_lane_f32(vfma_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
+ 0);
#else
- return __builtin_fmaf(x, y, z);
+ return __builtin_fmaf(x, y, z);
#endif
+ }
}
#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-LIBC_INLINE double multiply_add(double x, double y, double z) {
+LIBC_INLINE LIBC_CONSTEXPR_DEFAULT double multiply_add(double x, double y,
+ double z) {
+ if (cpp::is_constant_evaluated()) {
+ return x * y + z;
----------------
bassiounix wrote:
SAO
https://github.com/llvm/llvm-project/pull/242283
More information about the libc-commits
mailing list