[libc-commits] [libc] [llvm] [libc][math] Add multiply_sub and sub_multiply to multiply_add.h. (PR #243269)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 16:11:37 PST 2026
https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/243269
>From 454a2a5bb58e90926e9de2266cc30d0cd85c0ef7 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Mon, 16 Nov 2026 22:59:41 +0000
Subject: [PATCH] [libc][math] Add multiply_sub and sub_multiply to
multiply_add.h.
- Add fputil::multiply_sub (x * y - z) and fputil::sub_multiply (x - y * z)
alongside fputil::multiply_add (x * y + z).
- Use template-based hardware FMA dispatch and size-aware inline overloads so
explicit multiply_add<T> calls also use hardware FMA when available.
- Add NumberPair<T> add/sub overloads in double_double.h, and matching
multiply_sub and sub_multiply overloads for DoubleDouble and DyadicFloat.
- Add unit tests in libc/test/src/__support/FPUtil/multiply_add_test.cpp.
---
libc/src/__support/FPUtil/CMakeLists.txt | 5 +
libc/src/__support/FPUtil/double_double.h | 50 +++++
libc/src/__support/FPUtil/dyadic_float.h | 14 ++
libc/src/__support/FPUtil/multiply_add.h | 198 ++++++++++++++----
libc/test/src/__support/FPUtil/CMakeLists.txt | 13 ++
.../__support/FPUtil/multiply_add_test.cpp | 169 +++++++++++++++
.../libc/src/__support/FPUtil/BUILD.bazel | 1 +
.../test/src/__support/FPUtil/BUILD.bazel | 12 ++
8 files changed, 421 insertions(+), 41 deletions(-)
create mode 100644 libc/test/src/__support/FPUtil/multiply_add_test.cpp
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index fc0a01670338a9..6286a78c5cc455 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -158,6 +158,11 @@ add_header_library(
multiply_add.h
DEPENDS
libc.src.__support.common
+ libc.src.__support.CPP.type_traits
+ libc.src.__support.macros.config
+ libc.src.__support.macros.optimization
+ libc.src.__support.macros.properties.architectures
+ libc.src.__support.macros.properties.compiler
libc.src.__support.macros.properties.cpu_features
FLAGS
FMA_OPT
diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h
index a0ca626771263c..3c1c052352a0a9 100644
--- a/libc/src/__support/FPUtil/double_double.h
+++ b/libc/src/__support/FPUtil/double_double.h
@@ -107,6 +107,40 @@ LIBC_INLINE constexpr NumberPair<T> add(const NumberPair<T> &a, T b) {
return exact_add<FAST2SUM>(r.hi, r_lo);
}
+// Assumption: when FAST2SUM = true, |a| >= |b.hi|.
+template <bool FAST2SUM = true, typename T>
+LIBC_INLINE constexpr NumberPair<T> add(T a, const NumberPair<T> &b) {
+ NumberPair<T> r = exact_add<FAST2SUM>(a, b.hi);
+ T r_lo = r.lo + b.lo;
+ return exact_add<FAST2SUM>(r.hi, r_lo);
+}
+
+// Assumption: when FAST2SUM = true, |a.hi| >= |b.hi|.
+template <bool FAST2SUM = true, typename T>
+LIBC_INLINE constexpr NumberPair<T> sub(const NumberPair<T> &a,
+ const NumberPair<T> &b) {
+ NumberPair<T> r = exact_sub<FAST2SUM>(a.hi, b.hi);
+ T lo = a.lo - b.lo;
+ T r_lo = r.lo + lo;
+ return exact_add<FAST2SUM>(r.hi, r_lo);
+}
+
+// Assumption: when FAST2SUM = true, |a.hi| >= |b|.
+template <bool FAST2SUM = true, typename T>
+LIBC_INLINE constexpr NumberPair<T> sub(const NumberPair<T> &a, T b) {
+ NumberPair<T> r = exact_sub<FAST2SUM>(a.hi, b);
+ T r_lo = r.lo + a.lo;
+ return exact_add<FAST2SUM>(r.hi, r_lo);
+}
+
+// Assumption: when FAST2SUM = true, |a| >= |b.hi|.
+template <bool FAST2SUM = true, typename T>
+LIBC_INLINE constexpr NumberPair<T> sub(T a, const NumberPair<T> &b) {
+ NumberPair<T> r = exact_sub<FAST2SUM>(a, b.hi);
+ T r_lo = r.lo - b.lo;
+ return exact_add<FAST2SUM>(r.hi, r_lo);
+}
+
// Veltkamp's Splitting for double precision.
// Note: This is proved to be correct for all rounding modes:
// Zimmermann, P., "Note on the Veltkamp/Dekker Algorithms with Directed
@@ -214,6 +248,22 @@ LIBC_INLINE DoubleDouble multiply_add<DoubleDouble>(const DoubleDouble &a,
return add(c, quick_mult(a, b));
}
+// Assuming |a * b| >= |c|.
+template <>
+LIBC_INLINE DoubleDouble multiply_sub<DoubleDouble>(const DoubleDouble &a,
+ const DoubleDouble &b,
+ const DoubleDouble &c) {
+ return sub(quick_mult(a, b), c);
+}
+
+// Assuming |a| >= |b * c|.
+template <>
+LIBC_INLINE DoubleDouble sub_multiply<DoubleDouble>(const DoubleDouble &a,
+ const DoubleDouble &b,
+ const DoubleDouble &c) {
+ return sub(a, quick_mult(b, c));
+}
+
// Accurate double-double division, following Karp-Markstein's trick for
// division, implemented in the CORE-MATH project at:
// https://gitlab.inria.fr/core-math/core-math/-/blob/master/src/binary64/tan/tan.c#L1855
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index 130e85d0a4f5f0..2dcd5746f5083b 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -756,6 +756,20 @@ multiply_add(const DyadicFloat<Bits> &a, const DyadicFloat<Bits> &b,
return quick_add(c, quick_mul(a, b));
}
+template <size_t Bits>
+LIBC_INLINE constexpr DyadicFloat<Bits>
+multiply_sub(const DyadicFloat<Bits> &a, const DyadicFloat<Bits> &b,
+ const DyadicFloat<Bits> &c) {
+ return quick_sub(quick_mul(a, b), c);
+}
+
+template <size_t Bits>
+LIBC_INLINE constexpr DyadicFloat<Bits>
+sub_multiply(const DyadicFloat<Bits> &a, const DyadicFloat<Bits> &b,
+ const DyadicFloat<Bits> &c) {
+ return quick_sub(a, quick_mul(b, c));
+}
+
// Simple exponentiation implementation for printf. Only handles positive
// exponents, since division isn't implemented.
template <size_t Bits>
diff --git a/libc/src/__support/FPUtil/multiply_add.h b/libc/src/__support/FPUtil/multiply_add.h
index f7f30035c7739c..c534bd74e8d645 100644
--- a/libc/src/__support/FPUtil/multiply_add.h
+++ b/libc/src/__support/FPUtil/multiply_add.h
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
///
/// \file
-/// Common header for multiply-add implementations.
+/// Common header for multiply-add and multiply-sub implementations.
///
//===----------------------------------------------------------------------===//
@@ -17,36 +17,12 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
#include "src/__support/macros/properties/architectures.h"
#include "src/__support/macros/properties/compiler.h"
#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
-namespace LIBC_NAMESPACE_DECL {
-namespace fputil {
-
-// Implement a simple wrapper for multiply-add operation:
-// multiply_add(x, y, z) = x*y + z
-// which uses FMA instructions to speed up if available.
-
-template <typename T>
-LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) > sizeof(void *)), T>
-multiply_add(const T &x, const T &y, const T &z) {
- T prod = x * y;
- return prod + z;
-}
-
-template <typename T>
-LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(void *)), T>
-multiply_add(T x, T y, T z) {
- T prod = x * y;
- return prod + z;
-}
-
-} // namespace fputil
-} // namespace LIBC_NAMESPACE_DECL
-
-#if defined(LIBC_TARGET_CPU_HAS_FMA) && !defined(LIBC_USE_CONSTEXPR)
-
+#if defined(LIBC_TARGET_CPU_HAS_FMA)
#if !__has_builtin(__builtin_elementwise_fma) && defined(LIBC_COMPILER_IS_MSVC)
#if defined(LIBC_TARGET_ARCH_IS_X86)
#include <immintrin.h>
@@ -54,22 +30,28 @@ multiply_add(T x, T y, T z) {
#include <arm64_neon.h>
#endif
#endif
-
-// FMA instructions are available.
-// We use builtins directly instead of including FMA.h to avoid a circular
-// dependency: multiply_add.h -> FMA.h -> generic/FMA.h -> dyadic_float.h.
-//
-// TODO: for constexpr evaluation of multiply_add using FMA, we will need to
-// 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.
+#endif // LIBC_TARGET_CPU_HAS_FMA
namespace LIBC_NAMESPACE_DECL {
namespace fputil {
+namespace internal {
+
+template <typename T> struct HasHardwareFma : cpp::false_type {};
+
+template <typename T> T fma_impl(T, T, T) = delete;
+template <typename T> T fms_impl(T, T, T) = delete;
+template <typename T> T fnma_impl(T, T, T) = delete;
+
+// Hardware FMA helpers use builtins directly instead of including FMA.h to
+// avoid a circular dependency:
+// multiply_add.h -> FMA.h -> generic/FMA.h -> dyadic_float.h.
+#if defined(LIBC_TARGET_CPU_HAS_FMA)
+
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
-LIBC_INLINE float multiply_add(float x, float y, float z) {
+template <> struct HasHardwareFma<float> : cpp::true_type {};
+
+LIBC_ALWAYS_INLINE float fma_impl(float x, float y, float z) {
#if __has_builtin(__builtin_elementwise_fma)
return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
@@ -82,10 +64,40 @@ LIBC_INLINE float multiply_add(float x, float y, float z) {
return __builtin_fmaf(x, y, z);
#endif
}
+
+LIBC_ALWAYS_INLINE float fms_impl(float x, float y, float z) {
+#if __has_builtin(__builtin_elementwise_fma)
+ return __builtin_elementwise_fma(x, y, -z);
+#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
+ return _mm_cvtss_f32(
+ _mm_fmsub_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);
+#else
+ return __builtin_fmaf(x, y, -z);
+#endif
+}
+
+LIBC_ALWAYS_INLINE float fnma_impl(float x, float y, float z) {
+#if __has_builtin(__builtin_elementwise_fma)
+ return __builtin_elementwise_fma(-x, y, z);
+#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
+ return _mm_cvtss_f32(
+ _mm_fnmadd_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(vfms_f32(vdup_n_f32(z), vdup_n_f32(x), vdup_n_f32(y)),
+ 0);
+#else
+ 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) {
+template <> struct HasHardwareFma<double> : cpp::true_type {};
+
+LIBC_ALWAYS_INLINE double fma_impl(double x, double y, double z) {
#if __has_builtin(__builtin_elementwise_fma)
return __builtin_elementwise_fma(x, y, z);
#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
@@ -98,11 +110,115 @@ LIBC_INLINE double multiply_add(double x, double y, double z) {
return __builtin_fma(x, y, z);
#endif
}
+
+LIBC_ALWAYS_INLINE double fms_impl(double x, double y, double z) {
+#if __has_builtin(__builtin_elementwise_fma)
+ return __builtin_elementwise_fma(x, y, -z);
+#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
+ return _mm_cvtsd_f64(
+ _mm_fmsub_sd(_mm_set_sd(x), _mm_set_sd(y), _mm_set_sd(z)));
+#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
+ return vget_lane_f64(vfma_f64(vdup_n_f64(-z), vdup_n_f64(x), vdup_n_f64(y)),
+ 0);
+#else
+ return __builtin_fma(x, y, -z);
+#endif
+}
+
+LIBC_ALWAYS_INLINE double fnma_impl(double x, double y, double z) {
+#if __has_builtin(__builtin_elementwise_fma)
+ return __builtin_elementwise_fma(-x, y, z);
+#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_X86)
+ return _mm_cvtsd_f64(
+ _mm_fnmadd_sd(_mm_set_sd(x), _mm_set_sd(y), _mm_set_sd(z)));
+#elif defined(LIBC_COMPILER_IS_MSVC) && defined(LIBC_TARGET_ARCH_IS_AARCH64)
+ return vget_lane_f64(vfms_f64(vdup_n_f64(z), vdup_n_f64(x), vdup_n_f64(y)),
+ 0);
+#else
+ return __builtin_fma(-x, y, z);
+#endif
+}
#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+#endif // LIBC_TARGET_CPU_HAS_FMA
+
+} // namespace internal
+
+// Compute x * y + z, using hardware FMA when available.
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) > sizeof(double)), T>
+multiply_add(const T &x, const T &y, const T &z) {
+ T prod = x * y;
+ return prod + z;
+}
+
+template <typename T>
+LIBC_ALWAYS_INLINE constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(double)), T>
+multiply_add(T x, T y, T z) {
+ if constexpr (internal::HasHardwareFma<T>::value) {
+ if (!cpp::is_constant_evaluated())
+ return internal::fma_impl(x, y, z);
+ }
+ T prod = x * y;
+ return prod + z;
+}
+
+// Compute x * y - z, using hardware FMA when available.
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) > sizeof(double)), T>
+multiply_sub(const T &x, const T &y, const T &z) {
+ T prod = x * y;
+ return prod - z;
+}
+
+template <typename T>
+LIBC_ALWAYS_INLINE constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(double)), T>
+multiply_sub(T x, T y, T z) {
+ if constexpr (internal::HasHardwareFma<T>::value) {
+ if (!cpp::is_constant_evaluated())
+ return internal::fms_impl(x, y, z);
+ }
+ T prod = x * y;
+ return prod - z;
+}
+
+// Compute x - y * z, using hardware FMA when available.
+// Without FMA, when directed rounding modes (FE_UPWARD / FE_DOWNWARD) may be
+// active, evaluate x + ((-y) * z) instead of x - (y * z) for floating-point
+// types so that (-y) * z is rounded in the same direction as the outer sum
+// when y or z is constant (e.g., for x = min_denormal and 0 < z < 1 in
+// FE_UPWARD, RU(x + RU((-x) * z)) = x, whereas RU(x - RU(x * z)) = 0).
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<(sizeof(T) > sizeof(double)), T>
+sub_multiply(const T &x, const T &y, const T &z) {
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if constexpr (cpp::is_floating_point_v<T>) {
+ T prod = (-y) * z;
+ return x + prod;
+ }
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ T prod = y * z;
+ return x - prod;
+}
+
+template <typename T>
+LIBC_ALWAYS_INLINE constexpr cpp::enable_if_t<(sizeof(T) <= sizeof(double)), T>
+sub_multiply(T x, T y, T z) {
+ if constexpr (internal::HasHardwareFma<T>::value) {
+ if (!cpp::is_constant_evaluated())
+ return internal::fnma_impl(y, z, x);
+ }
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if constexpr (cpp::is_floating_point_v<T>) {
+ T prod = (-y) * z;
+ return x + prod;
+ }
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ T prod = y * z;
+ return x - prod;
+}
+
} // namespace fputil
} // namespace LIBC_NAMESPACE_DECL
-#endif // LIBC_TARGET_CPU_HAS_FMA
-
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_MULTIPLY_ADD_H
diff --git a/libc/test/src/__support/FPUtil/CMakeLists.txt b/libc/test/src/__support/FPUtil/CMakeLists.txt
index 964a37cecc9a3d..99c09bc116a585 100644
--- a/libc/test/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/test/src/__support/FPUtil/CMakeLists.txt
@@ -63,6 +63,19 @@ add_fp_unittest(
libc.src.__support.macros.properties.types
)
+add_fp_unittest(
+ multiply_add_test
+ SUITE
+ libc-fputil-tests
+ SRCS
+ multiply_add_test.cpp
+ DEPENDS
+ libc.src.__support.FPUtil.double_double
+ libc.src.__support.FPUtil.dyadic_float
+ libc.src.__support.FPUtil.multiply_add
+ libc.src.__support.frac128
+)
+
add_fp_unittest(
rounding_mode_test
SUITE
diff --git a/libc/test/src/__support/FPUtil/multiply_add_test.cpp b/libc/test/src/__support/FPUtil/multiply_add_test.cpp
new file mode 100644
index 00000000000000..6137a9784af9f0
--- /dev/null
+++ b/libc/test/src/__support/FPUtil/multiply_add_test.cpp
@@ -0,0 +1,169 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for multiply_add, multiply_sub, and sub_multiply.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/dyadic_float.h"
+#include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/frac128.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::Frac128;
+using LIBC_NAMESPACE::fputil::DoubleDouble;
+using LIBC_NAMESPACE::fputil::DyadicFloat;
+using LIBC_NAMESPACE::fputil::multiply_add;
+using LIBC_NAMESPACE::fputil::multiply_sub;
+using LIBC_NAMESPACE::fputil::sub_multiply;
+
+template <typename T>
+class LlvmLibcMultiplyAddTest : public LIBC_NAMESPACE::testing::Test {
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ void test_identities() {
+ constexpr T XS[5] = {T(0.0), T(-0.0), T(1.5), T(-2.25), T(0x1.3p-5)};
+ constexpr T YS[4] = {T(0.0), T(0.75), T(-1.25), T(0x1.7p-4)};
+ constexpr T ZS[4] = {T(0.0), T(-0.0), T(2.5), T(-0.625)};
+
+ for (T x : XS) {
+ for (T y : YS) {
+ for (T z : ZS) {
+ EXPECT_FP_EQ_ALL_ROUNDING(multiply_add(x, y, -z),
+ multiply_sub(x, y, z));
+ EXPECT_FP_EQ_ALL_ROUNDING(multiply_add(-y, z, x),
+ sub_multiply(x, y, z));
+ EXPECT_FP_EQ_ALL_ROUNDING(multiply_add(y, -z, x),
+ sub_multiply(x, y, z));
+ // Explicit template parameter calls:
+ EXPECT_FP_EQ_ALL_ROUNDING(multiply_add<T>(x, y, z),
+ multiply_add(x, y, z));
+ EXPECT_FP_EQ_ALL_ROUNDING(multiply_sub<T>(x, y, z),
+ multiply_sub(x, y, z));
+ EXPECT_FP_EQ_ALL_ROUNDING(sub_multiply<T>(x, y, z),
+ sub_multiply(x, y, z));
+ }
+ }
+ }
+
+ // Directed rounding on subnormals: x - x * 2^-25 vs x + x * (-2^-25).
+ const T eps = T(0x1.0p-25);
+ EXPECT_FP_EQ_ALL_ROUNDING(multiply_add(min_denormal, -eps, min_denormal),
+ sub_multiply(min_denormal, min_denormal, eps));
+ EXPECT_FP_EQ_ALL_ROUNDING(
+ multiply_add(neg_min_denormal, -eps, neg_min_denormal),
+ sub_multiply(neg_min_denormal, neg_min_denormal, eps));
+ }
+
+ void test_fma_precision() {
+ if constexpr (LIBC_NAMESPACE::fputil::internal::HasHardwareFma<T>::value) {
+ // With a = 1 + 2^-12, a * a = 1 + 2^-11 + 2^-24:
+ // single-rounded FMA preserves the 2^-24 low bit when 1 is cancelled.
+ const T a = T(1.0) + T(0x1.0p-12);
+ const T expected = T(0x1.0p-11) + T(0x1.0p-24);
+ EXPECT_FP_EQ(expected, multiply_add(a, a, T(-1.0)));
+ EXPECT_FP_EQ(expected, multiply_sub(a, a, T(1.0)));
+ EXPECT_FP_EQ(-expected, sub_multiply(T(1.0), a, a));
+ }
+ }
+
+ void test_constexpr() {
+#if LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
+ static_assert(multiply_add(T(1.5), T(2.0), T(0.25)) == T(3.25));
+ static_assert(multiply_sub(T(1.5), T(2.0), T(0.25)) == T(2.75));
+ static_assert(sub_multiply(T(3.5), T(1.5), T(2.0)) == T(0.5));
+#endif
+ }
+};
+
+using MultiplyAddFPTypes = LIBC_NAMESPACE::testing::Types<float, double>;
+
+TYPED_TEST_SUITE(LlvmLibcMultiplyAddTest, MultiplyAddFPTypes);
+
+TYPED_TEST(LlvmLibcMultiplyAddTest, Identities) { this->test_identities(); }
+
+TYPED_TEST(LlvmLibcMultiplyAddTest, FmaPrecision) {
+ this->test_fma_precision();
+}
+
+TYPED_TEST(LlvmLibcMultiplyAddTest, ConstexprEval) { this->test_constexpr(); }
+
+TEST(LlvmLibcMultiplyAddStructTest, DoubleDoubleOps) {
+ const DoubleDouble a{0x1.0p-60, 2.0};
+ const DoubleDouble b{0.0, 0.5};
+ const DoubleDouble c{0x1.0p-62, 0.25};
+
+ // sub overloads:
+ DoubleDouble s_dd = LIBC_NAMESPACE::fputil::sub(a, c);
+ EXPECT_FP_EQ(1.75, s_dd.hi);
+ EXPECT_FP_EQ(0x1.0p-60 - 0x1.0p-62, s_dd.lo);
+
+ DoubleDouble s_ds = LIBC_NAMESPACE::fputil::sub(a, 0.25);
+ EXPECT_FP_EQ(1.75, s_ds.hi);
+ EXPECT_FP_EQ(0x1.0p-60, s_ds.lo);
+
+ DoubleDouble s_sd = LIBC_NAMESPACE::fputil::sub(2.0, c);
+ EXPECT_FP_EQ(1.75, s_sd.hi);
+ EXPECT_FP_EQ(-0x1.0p-62, s_sd.lo);
+
+ // multiply_add(0.5, 0.25 + 2^-62, 2.0 + 2^-60) = 2.125 + 2^-60 + 2^-63.
+ DoubleDouble ma = multiply_add(b, c, a);
+ EXPECT_FP_EQ(2.125, ma.hi);
+ EXPECT_FP_EQ(0x1.0p-60 + 0x1.0p-63, ma.lo);
+
+ // multiply_sub(2.0 + 2^-60, 0.5, 0.25 + 2^-62) = 0.75 + 2^-61 - 2^-62.
+ DoubleDouble ms = multiply_sub(a, b, c);
+ EXPECT_FP_EQ(0.75, ms.hi);
+ EXPECT_FP_EQ(0x1.0p-61 - 0x1.0p-62, ms.lo);
+
+ // sub_multiply(2.0 + 2^-60, 0.5, 0.25 + 2^-62) = 1.875 + 2^-60 - 2^-63.
+ DoubleDouble sm = sub_multiply(a, b, c);
+ EXPECT_FP_EQ(1.875, sm.hi);
+ EXPECT_FP_EQ(0x1.0p-60 - 0x1.0p-63, sm.lo);
+}
+
+TEST(LlvmLibcMultiplyAddStructTest, DyadicFloatOps) {
+ using DFloat128 = DyadicFloat<128>;
+ constexpr DFloat128 A(2.0);
+ constexpr DFloat128 B(0.5);
+ constexpr DFloat128 C(0.25);
+
+ constexpr DFloat128 MA = multiply_add(B, C, A);
+ constexpr DFloat128 MS = multiply_sub(B, C, A);
+ constexpr DFloat128 SM = sub_multiply(A, B, C);
+ static_assert(MS.mantissa == SM.mantissa && MS.exponent == SM.exponent &&
+ MS.sign != SM.sign);
+
+ EXPECT_FP_EQ(2.125, static_cast<double>(MA));
+ EXPECT_FP_EQ(-1.875, static_cast<double>(MS));
+ EXPECT_FP_EQ(1.875, static_cast<double>(SM));
+}
+
+TEST(LlvmLibcMultiplyAddStructTest, Frac128Ops) {
+ constexpr Frac128 A({0, 0x8000'0000'0000'0000ULL}); // 0.5
+ constexpr Frac128 B({0, 0x8000'0000'0000'0000ULL}); // 0.5
+ constexpr Frac128 C({0, 0x2000'0000'0000'0000ULL}); // 0.125
+
+ // 0.5 * 0.5 + 0.125 = 0.375; 0.5 * 0.5 - 0.125 = 0.125.
+ constexpr Frac128 EXP_SUM({0, 0x6000'0000'0000'0000ULL}); // 0.375
+ constexpr Frac128 EXP_DIFF({0, 0x2000'0000'0000'0000ULL}); // 0.125
+ // 0.5 - 0.5 * 0.125 = 0.4375.
+ constexpr Frac128 EXP_SM({0, 0x7000'0000'0000'0000ULL}); // 0.4375
+
+ static_assert(EXP_SUM == multiply_add(A, B, C));
+ static_assert(EXP_DIFF == multiply_sub(A, B, C));
+ static_assert(EXP_SM == sub_multiply(A, B, C));
+
+ EXPECT_TRUE(EXP_SUM == multiply_add(A, B, C));
+ EXPECT_TRUE(EXP_DIFF == multiply_sub(A, B, C));
+ EXPECT_TRUE(EXP_SM == sub_multiply(A, B, C));
+}
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel
index 45d4b344c6ad4d..013e61d54b698e 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/FPUtil/BUILD.bazel
@@ -386,6 +386,7 @@ libc_support_library(
"//libc/src/__support:common",
"//libc/src/__support/CPP:type_traits",
"//libc/src/__support/macros:config",
+ "//libc/src/__support/macros:optimization",
"//libc/src/__support/macros/properties:architectures",
"//libc/src/__support/macros/properties:compiler",
"//libc/src/__support/macros/properties:cpu_features",
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel
index 921acaea2dd1dd..67bad4ebfaa220 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/__support/FPUtil/BUILD.bazel
@@ -63,6 +63,18 @@ libc_test(
],
)
+libc_test(
+ name = "multiply_add_test",
+ srcs = ["multiply_add_test.cpp"],
+ deps = [
+ "//libc/src/__support:frac128",
+ "//libc/src/__support/FPUtil:double_double",
+ "//libc/src/__support/FPUtil:dyadic_float",
+ "//libc/src/__support/FPUtil:multiply_add",
+ "//libc/test/UnitTest:fp_test_helpers",
+ ],
+)
+
libc_test(
name = "bfloat16_test",
srcs = ["bfloat16_test.cpp"],
More information about the libc-commits
mailing list