[libc-commits] [libc] [llvm] [libc][math] Refactor fmul-fsub-frexp family to header-only (PR #195431)

via libc-commits libc-commits at lists.llvm.org
Sat May 2 01:43:38 PDT 2026


https://github.com/AnonMiraj updated https://github.com/llvm/llvm-project/pull/195431

>From 60d81fc084ed8f14a51c06c1c654a321e2aad2db Mon Sep 17 00:00:00 2001
From: Anonmiraj <ezzibrahimx at gmail.com>
Date: Sat, 2 May 2026 11:26:43 +0300
Subject: [PATCH] [libc][math] Refactor fmul-fsub-frexp family to header-only

Refactored functions:
  - fmul
  - fmulf128
  - fmull
  - fsub
  - fsubf128
  - fsubl
  - frexp
  - frexpbf16
  - frexpl
---
 libc/shared/math.h                            |   9 ++
 libc/shared/math/fmul.h                       |  23 +++
 libc/shared/math/fmulf128.h                   |  29 ++++
 libc/shared/math/fmull.h                      |  23 +++
 libc/shared/math/frexp.h                      |  23 +++
 libc/shared/math/frexpbf16.h                  |  23 +++
 libc/shared/math/frexpl.h                     |  23 +++
 libc/shared/math/fsub.h                       |  23 +++
 libc/shared/math/fsubf128.h                   |  29 ++++
 libc/shared/math/fsubl.h                      |  23 +++
 libc/src/__support/FPUtil/double_double.h     |   5 +-
 libc/src/__support/math/CMakeLists.txt        |  87 +++++++++++
 libc/src/__support/math/fmul.h                | 126 ++++++++++++++++
 libc/src/__support/math/fmulf128.h            |  31 ++++
 libc/src/__support/math/fmull.h               |  25 ++++
 libc/src/__support/math/frexp.h               |  25 ++++
 libc/src/__support/math/frexpbf16.h           |  26 ++++
 libc/src/__support/math/frexpl.h              |  25 ++++
 libc/src/__support/math/fsub.h                |  25 ++++
 libc/src/__support/math/fsubf128.h            |  31 ++++
 libc/src/__support/math/fsubl.h               |  25 ++++
 libc/src/math/generic/CMakeLists.txt          |  26 ++--
 libc/src/math/generic/fmul.cpp                | 109 +-------------
 libc/src/math/generic/fmulf128.cpp            |   6 +-
 libc/src/math/generic/fmull.cpp               |   6 +-
 libc/src/math/generic/frexp.cpp               |   6 +-
 libc/src/math/generic/frexpbf16.cpp           |   7 +-
 libc/src/math/generic/frexpl.cpp              |   6 +-
 libc/src/math/generic/fsub.cpp                |   6 +-
 libc/src/math/generic/fsubf128.cpp            |   6 +-
 libc/src/math/generic/fsubl.cpp               |   6 +-
 libc/test/shared/CMakeLists.txt               |  18 +++
 .../shared/shared_math_constexpr_test.cpp     |  20 +++
 libc/test/shared/shared_math_test.cpp         |  18 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 138 +++++++++++++++++-
 35 files changed, 872 insertions(+), 165 deletions(-)
 create mode 100644 libc/shared/math/fmul.h
 create mode 100644 libc/shared/math/fmulf128.h
 create mode 100644 libc/shared/math/fmull.h
 create mode 100644 libc/shared/math/frexp.h
 create mode 100644 libc/shared/math/frexpbf16.h
 create mode 100644 libc/shared/math/frexpl.h
 create mode 100644 libc/shared/math/fsub.h
 create mode 100644 libc/shared/math/fsubf128.h
 create mode 100644 libc/shared/math/fsubl.h
 create mode 100644 libc/src/__support/math/fmul.h
 create mode 100644 libc/src/__support/math/fmulf128.h
 create mode 100644 libc/src/__support/math/fmull.h
 create mode 100644 libc/src/__support/math/frexp.h
 create mode 100644 libc/src/__support/math/frexpbf16.h
 create mode 100644 libc/src/__support/math/frexpl.h
 create mode 100644 libc/src/__support/math/fsub.h
 create mode 100644 libc/src/__support/math/fsubf128.h
 create mode 100644 libc/src/__support/math/fsubl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 2641e0e71676f..4cd943a2435f2 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -238,9 +238,15 @@
 #include "math/fmodf128.h"
 #include "math/fmodf16.h"
 #include "math/fmodl.h"
+#include "math/fmul.h"
+#include "math/fmulf128.h"
+#include "math/fmull.h"
+#include "math/frexp.h"
+#include "math/frexpbf16.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
 #include "math/frexpf16.h"
+#include "math/frexpl.h"
 #include "math/fromfp.h"
 #include "math/fromfpbf16.h"
 #include "math/fromfpf.h"
@@ -256,6 +262,9 @@
 #include "math/fsqrt.h"
 #include "math/fsqrtf128.h"
 #include "math/fsqrtl.h"
+#include "math/fsub.h"
+#include "math/fsubf128.h"
+#include "math/fsubl.h"
 #include "math/getpayload.h"
 #include "math/getpayloadbf16.h"
 #include "math/getpayloadf.h"
diff --git a/libc/shared/math/fmul.h b/libc/shared/math/fmul.h
new file mode 100644
index 0000000000000..c5d759feaedb0
--- /dev/null
+++ b/libc/shared/math/fmul.h
@@ -0,0 +1,23 @@
+//===-- Shared fmul 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_SHARED_MATH_FMUL_H
+#define LLVM_LIBC_SHARED_MATH_FMUL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmul.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmul;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMUL_H
diff --git a/libc/shared/math/fmulf128.h b/libc/shared/math/fmulf128.h
new file mode 100644
index 0000000000000..279d9a84d12a6
--- /dev/null
+++ b/libc/shared/math/fmulf128.h
@@ -0,0 +1,29 @@
+//===-- Shared fmulf128 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_SHARED_MATH_FMULF128_H
+#define LLVM_LIBC_SHARED_MATH_FMULF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmulf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmulf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_FMULF128_H
diff --git a/libc/shared/math/fmull.h b/libc/shared/math/fmull.h
new file mode 100644
index 0000000000000..6e88f5df1c97f
--- /dev/null
+++ b/libc/shared/math/fmull.h
@@ -0,0 +1,23 @@
+//===-- Shared fmull 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_SHARED_MATH_FMULL_H
+#define LLVM_LIBC_SHARED_MATH_FMULL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmull.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmull;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMULL_H
diff --git a/libc/shared/math/frexp.h b/libc/shared/math/frexp.h
new file mode 100644
index 0000000000000..7dcf87eea85fc
--- /dev/null
+++ b/libc/shared/math/frexp.h
@@ -0,0 +1,23 @@
+//===-- Shared frexp 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_SHARED_MATH_FREXP_H
+#define LLVM_LIBC_SHARED_MATH_FREXP_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/frexp.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::frexp;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FREXP_H
diff --git a/libc/shared/math/frexpbf16.h b/libc/shared/math/frexpbf16.h
new file mode 100644
index 0000000000000..33ddb25c3cf67
--- /dev/null
+++ b/libc/shared/math/frexpbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared frexpbf16 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_SHARED_MATH_FREXPBF16_H
+#define LLVM_LIBC_SHARED_MATH_FREXPBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/frexpbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::frexpbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FREXPBF16_H
diff --git a/libc/shared/math/frexpl.h b/libc/shared/math/frexpl.h
new file mode 100644
index 0000000000000..68f0fd3a342d5
--- /dev/null
+++ b/libc/shared/math/frexpl.h
@@ -0,0 +1,23 @@
+//===-- Shared frexpl 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_SHARED_MATH_FREXPL_H
+#define LLVM_LIBC_SHARED_MATH_FREXPL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/frexpl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::frexpl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FREXPL_H
diff --git a/libc/shared/math/fsub.h b/libc/shared/math/fsub.h
new file mode 100644
index 0000000000000..38396a26b1c72
--- /dev/null
+++ b/libc/shared/math/fsub.h
@@ -0,0 +1,23 @@
+//===-- Shared fsub 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_SHARED_MATH_FSUB_H
+#define LLVM_LIBC_SHARED_MATH_FSUB_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fsub.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fsub;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FSUB_H
diff --git a/libc/shared/math/fsubf128.h b/libc/shared/math/fsubf128.h
new file mode 100644
index 0000000000000..b8cf101704dc0
--- /dev/null
+++ b/libc/shared/math/fsubf128.h
@@ -0,0 +1,29 @@
+//===-- Shared fsubf128 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_SHARED_MATH_FSUBF128_H
+#define LLVM_LIBC_SHARED_MATH_FSUBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fsubf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fsubf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_FSUBF128_H
diff --git a/libc/shared/math/fsubl.h b/libc/shared/math/fsubl.h
new file mode 100644
index 0000000000000..658de2fdc98cc
--- /dev/null
+++ b/libc/shared/math/fsubl.h
@@ -0,0 +1,23 @@
+//===-- Shared fsubl 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_SHARED_MATH_FSUBL_H
+#define LLVM_LIBC_SHARED_MATH_FSUBL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fsubl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fsubl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FSUBL_H
diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h
index 3913f7acb7f8c..31049ced5a934 100644
--- a/libc/src/__support/FPUtil/double_double.h
+++ b/libc/src/__support/FPUtil/double_double.h
@@ -87,7 +87,8 @@ LIBC_INLINE constexpr NumberPair<T> split(T a) {
 
 // Helper for non-fma exact mult where the first number is already split.
 template <typename T = double, size_t SPLIT_B = DefaultSplit<T>::VALUE>
-LIBC_INLINE NumberPair<T> exact_mult(const NumberPair<T> &as, T a, T b) {
+LIBC_INLINE constexpr NumberPair<T> exact_mult(const NumberPair<T> &as, T a,
+                                               T b) {
   NumberPair<T> bs = split<T, SPLIT_B>(b);
   NumberPair<T> r{0.0, 0.0};
 
@@ -128,7 +129,7 @@ template <> struct TargetHasFmaInstruction<double> {
 // the generated constants to precision <= 51, and splitting it by 2^28 + 1,
 // then a * b = r.hi + r.lo is exact for all rounding modes.
 template <typename T = double, size_t SPLIT_B = DefaultSplit<T>::VALUE>
-LIBC_INLINE NumberPair<T> exact_mult(T a, T b) {
+LIBC_INLINE LIBC_CONSTEXPR NumberPair<T> exact_mult(T a, T b) {
   NumberPair<T> r{0.0, 0.0};
 
   if constexpr (TargetHasFmaInstruction<T>::VALUE) {
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 3100dca01d68e..db689026d404f 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1420,6 +1420,64 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  fmul
+  HDRS
+    fmul.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.double_double
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  fmulf128
+  HDRS
+    fmulf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  fmull
+  HDRS
+    fmull.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  frexp
+  HDRS
+    frexp.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  frexpbf16
+  HDRS
+    frexpbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  frexpl
+  HDRS
+    frexpl.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   fromfp
   HDRS
@@ -1534,6 +1592,35 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  fsub
+  HDRS
+    fsub.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  fsubf128
+  HDRS
+    fsubf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  fsubl
+  HDRS
+    fsubl.h
+  DEPENDS
+    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.macros.config
+)
+
+
 add_header_library(
   ldexp
   HDRS
diff --git a/libc/src/__support/math/fmul.h b/libc/src/__support/math/fmul.h
new file mode 100644
index 0000000000000..52c6c80232ba4
--- /dev/null
+++ b/libc/src/__support/math/fmul.h
@@ -0,0 +1,126 @@
+//===-- Implementation header for fmul --------------------------*- 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_MATH_FMUL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMUL_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/double_double.h"
+#include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fmul(double x, double y) {
+
+  // Without FMA instructions, fputil::exact_mult is not
+  // correctly rounded for all rounding modes, so we fall
+  // back to the generic `fmul` implementation
+
+#ifndef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
+  return fputil::generic::mul<float>(x, y);
+#else
+  fputil::DoubleDouble prod = fputil::exact_mult(x, y);
+  using DoubleBits = fputil::FPBits<double>;
+  using DoubleStorageType = typename DoubleBits::StorageType;
+  using FloatBits = fputil::FPBits<float>;
+  using FloatStorageType = typename FloatBits::StorageType;
+  DoubleBits x_bits(x);
+  DoubleBits y_bits(y);
+
+  Sign result_sign = x_bits.sign() == y_bits.sign() ? Sign::POS : Sign::NEG;
+  double result = prod.hi;
+  DoubleBits hi_bits(prod.hi), lo_bits(prod.lo);
+  // Check for cases where we need to propagate the sticky bits:
+  constexpr uint64_t STICKY_MASK = 0xFFF'FFF; // Lower (52 - 23 - 1 = 28 bits)
+  uint64_t sticky_bits = (hi_bits.uintval() & STICKY_MASK);
+  if (LIBC_UNLIKELY(sticky_bits == 0)) {
+    // Might need to propagate sticky bits:
+    if (!(lo_bits.is_inf_or_nan() || lo_bits.is_zero())) {
+      // Now prod.lo is nonzero and finite, we need to propagate sticky bits.
+      if (lo_bits.sign() != hi_bits.sign())
+        result = DoubleBits(hi_bits.uintval() - 1).get_val();
+      else
+        result = DoubleBits(hi_bits.uintval() | 1).get_val();
+    }
+  }
+
+  float result_f = static_cast<float>(result);
+  FloatBits rf_bits(result_f);
+  uint32_t rf_exp = rf_bits.get_biased_exponent();
+  if (LIBC_LIKELY(rf_exp > 0 && rf_exp < 2 * FloatBits::EXP_BIAS + 1)) {
+    return result_f;
+  }
+
+  // Now result_f is either inf/nan/zero/denormal.
+  if (x_bits.is_nan() || y_bits.is_nan()) {
+    if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
+      fputil::raise_except_if_required(FE_INVALID);
+
+    if (x_bits.is_quiet_nan()) {
+      DoubleStorageType x_payload = x_bits.get_mantissa();
+      x_payload >>= DoubleBits::FRACTION_LEN - FloatBits::FRACTION_LEN;
+      return FloatBits::quiet_nan(x_bits.sign(),
+                                  static_cast<FloatStorageType>(x_payload))
+          .get_val();
+    }
+
+    if (y_bits.is_quiet_nan()) {
+      DoubleStorageType y_payload = y_bits.get_mantissa();
+      y_payload >>= DoubleBits::FRACTION_LEN - FloatBits::FRACTION_LEN;
+      return FloatBits::quiet_nan(y_bits.sign(),
+                                  static_cast<FloatStorageType>(y_payload))
+          .get_val();
+    }
+
+    return FloatBits::quiet_nan().get_val();
+  }
+
+  if (x_bits.is_inf()) {
+    if (y_bits.is_zero()) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+
+      return FloatBits::quiet_nan().get_val();
+    }
+
+    return FloatBits::inf(result_sign).get_val();
+  }
+
+  if (y_bits.is_inf()) {
+    if (x_bits.is_zero()) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+      return FloatBits::quiet_nan().get_val();
+    }
+
+    return FloatBits::inf(result_sign).get_val();
+  }
+
+  // Now either x or y is zero, and the other one is finite.
+  if (rf_bits.is_inf()) {
+    fputil::set_errno_if_required(ERANGE);
+    return FloatBits::inf(result_sign).get_val();
+  }
+
+  if (x_bits.is_zero() || y_bits.is_zero())
+    return FloatBits::zero(result_sign).get_val();
+
+  fputil::set_errno_if_required(ERANGE);
+  fputil::raise_except_if_required(FE_UNDERFLOW);
+  return result_f;
+
+#endif
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMUL_H
diff --git a/libc/src/__support/math/fmulf128.h b/libc/src/__support/math/fmulf128.h
new file mode 100644
index 0000000000000..858befc0b54b4
--- /dev/null
+++ b/libc/src/__support/math/fmulf128.h
@@ -0,0 +1,31 @@
+//===-- Implementation header for fmulf128 ----------------------*- 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_MATH_FMULF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMULF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fmulf128(float128 x, float128 y) {
+  return fputil::generic::mul<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMULF128_H
diff --git a/libc/src/__support/math/fmull.h b/libc/src/__support/math/fmull.h
new file mode 100644
index 0000000000000..68893b579a427
--- /dev/null
+++ b/libc/src/__support/math/fmull.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fmull -------------------------*- 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_MATH_FMULL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMULL_H
+
+#include "src/__support/FPUtil/generic/mul.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fmull(long double x, long double y) {
+  return fputil::generic::mul<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMULL_H
diff --git a/libc/src/__support/math/frexp.h b/libc/src/__support/math/frexp.h
new file mode 100644
index 0000000000000..f143111c16c4b
--- /dev/null
+++ b/libc/src/__support/math/frexp.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for frexp -------------------------*- 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_MATH_FREXP_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXP_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr double frexp(double x, int *exp) {
+  return fputil::frexp(x, *exp);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FREXP_H
diff --git a/libc/src/__support/math/frexpbf16.h b/libc/src/__support/math/frexpbf16.h
new file mode 100644
index 0000000000000..cb31ba47560a8
--- /dev/null
+++ b/libc/src/__support/math/frexpbf16.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for frexpbf16 ---------------------*- 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_MATH_FREXPBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXPBF16_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr bfloat16 frexpbf16(bfloat16 x, int *exp) {
+  return fputil::frexp(x, *exp);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FREXPBF16_H
diff --git a/libc/src/__support/math/frexpl.h b/libc/src/__support/math/frexpl.h
new file mode 100644
index 0000000000000..deedcbd8ce094
--- /dev/null
+++ b/libc/src/__support/math/frexpl.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for frexpl ------------------------*- 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_MATH_FREXPL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FREXPL_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr long double frexpl(long double x, int *exp) {
+  return fputil::frexp(x, *exp);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FREXPL_H
diff --git a/libc/src/__support/math/fsub.h b/libc/src/__support/math/fsub.h
new file mode 100644
index 0000000000000..803f035e39870
--- /dev/null
+++ b/libc/src/__support/math/fsub.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fsub --------------------------*- 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_MATH_FSUB_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSUB_H
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fsub(double x, double y) {
+  return fputil::generic::sub<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSUB_H
diff --git a/libc/src/__support/math/fsubf128.h b/libc/src/__support/math/fsubf128.h
new file mode 100644
index 0000000000000..74bcb87acc6ed
--- /dev/null
+++ b/libc/src/__support/math/fsubf128.h
@@ -0,0 +1,31 @@
+//===-- Implementation header for fsubf128 ----------------------*- 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_MATH_FSUBF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSUBF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fsubf128(float128 x, float128 y) {
+  return fputil::generic::sub<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSUBF128_H
diff --git a/libc/src/__support/math/fsubl.h b/libc/src/__support/math/fsubl.h
new file mode 100644
index 0000000000000..e31d89b749fb5
--- /dev/null
+++ b/libc/src/__support/math/fsubl.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fsubl -------------------------*- 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_MATH_FSUBL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FSUBL_H
+
+#include "src/__support/FPUtil/generic/add_sub.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fsubl(long double x, long double y) {
+  return fputil::generic::sub<float>(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FSUBL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b750e273091f6..3e799a037f340 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1543,7 +1543,7 @@ add_entrypoint_object(
   HDRS
     ../frexp.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.frexp
 )
 
 add_entrypoint_object(
@@ -1563,7 +1563,7 @@ add_entrypoint_object(
   HDRS
     ../frexpl.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.frexpl
 )
 
 add_entrypoint_object(
@@ -1593,11 +1593,7 @@ add_entrypoint_object(
   HDRS
     ../frexpbf16.h
   DEPENDS
-    libc.src.__support.common
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.FPUtil.manipulation_functions
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
+    libc.src.__support.math.frexpbf16
 )
 
 add_entrypoint_object(
@@ -2657,9 +2653,7 @@ add_entrypoint_object(
   HDRS
     ../fmul.h
   DEPENDS
-    libc.hdr.errno_macros
-    libc.hdr.fenv_macros
-    libc.src.__support.FPUtil.double_double
+    libc.src.__support.math.fmul
 )
 
 add_entrypoint_object(
@@ -2669,7 +2663,7 @@ add_entrypoint_object(
   HDRS
     ../fmull.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.math.fmull
 )
 
 add_entrypoint_object(
@@ -2679,8 +2673,7 @@ add_entrypoint_object(
   HDRS
     ../fmulf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.generic.mul
+    libc.src.__support.math.fmulf128
 )
 
 add_entrypoint_object(
@@ -2690,7 +2683,7 @@ add_entrypoint_object(
   HDRS
     ../fsub.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.fsub
 )
 
 add_entrypoint_object(
@@ -2700,7 +2693,7 @@ add_entrypoint_object(
   HDRS
     ../fsubl.h
   DEPENDS
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.fsubl
 )
 
 add_entrypoint_object(
@@ -2710,8 +2703,7 @@ add_entrypoint_object(
   HDRS
     ../fsubf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.generic.add_sub
+    libc.src.__support.math.fsubf128
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fmul.cpp b/libc/src/math/generic/fmul.cpp
index daad64873f27a..dc9116e9c3f06 100644
--- a/libc/src/math/generic/fmul.cpp
+++ b/libc/src/math/generic/fmul.cpp
@@ -5,115 +5,14 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+
 #include "src/math/fmul.h"
-#include "hdr/errno_macros.h"
-#include "hdr/fenv_macros.h"
-#include "src/__support/FPUtil/double_double.h"
-#include "src/__support/FPUtil/generic/mul.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmul.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fmul, (double x, double y)) {
-
-  // Without FMA instructions, fputil::exact_mult is not
-  // correctly rounded for all rounding modes, so we fall
-  // back to the generic `fmul` implementation
-
-#ifndef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
-  return fputil::generic::mul<float>(x, y);
-#else
-  fputil::DoubleDouble prod = fputil::exact_mult(x, y);
-  using DoubleBits = fputil::FPBits<double>;
-  using DoubleStorageType = typename DoubleBits::StorageType;
-  using FloatBits = fputil::FPBits<float>;
-  using FloatStorageType = typename FloatBits::StorageType;
-  DoubleBits x_bits(x);
-  DoubleBits y_bits(y);
-
-  Sign result_sign = x_bits.sign() == y_bits.sign() ? Sign::POS : Sign::NEG;
-  double result = prod.hi;
-  DoubleBits hi_bits(prod.hi), lo_bits(prod.lo);
-  // Check for cases where we need to propagate the sticky bits:
-  constexpr uint64_t STICKY_MASK = 0xFFF'FFF; // Lower (52 - 23 - 1 = 28 bits)
-  uint64_t sticky_bits = (hi_bits.uintval() & STICKY_MASK);
-  if (LIBC_UNLIKELY(sticky_bits == 0)) {
-    // Might need to propagate sticky bits:
-    if (!(lo_bits.is_inf_or_nan() || lo_bits.is_zero())) {
-      // Now prod.lo is nonzero and finite, we need to propagate sticky bits.
-      if (lo_bits.sign() != hi_bits.sign())
-        result = DoubleBits(hi_bits.uintval() - 1).get_val();
-      else
-        result = DoubleBits(hi_bits.uintval() | 1).get_val();
-    }
-  }
-
-  float result_f = static_cast<float>(result);
-  FloatBits rf_bits(result_f);
-  uint32_t rf_exp = rf_bits.get_biased_exponent();
-  if (LIBC_LIKELY(rf_exp > 0 && rf_exp < 2 * FloatBits::EXP_BIAS + 1)) {
-    return result_f;
-  }
-
-  // Now result_f is either inf/nan/zero/denormal.
-  if (x_bits.is_nan() || y_bits.is_nan()) {
-    if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan())
-      fputil::raise_except_if_required(FE_INVALID);
-
-    if (x_bits.is_quiet_nan()) {
-      DoubleStorageType x_payload = x_bits.get_mantissa();
-      x_payload >>= DoubleBits::FRACTION_LEN - FloatBits::FRACTION_LEN;
-      return FloatBits::quiet_nan(x_bits.sign(),
-                                  static_cast<FloatStorageType>(x_payload))
-          .get_val();
-    }
-
-    if (y_bits.is_quiet_nan()) {
-      DoubleStorageType y_payload = y_bits.get_mantissa();
-      y_payload >>= DoubleBits::FRACTION_LEN - FloatBits::FRACTION_LEN;
-      return FloatBits::quiet_nan(y_bits.sign(),
-                                  static_cast<FloatStorageType>(y_payload))
-          .get_val();
-    }
-
-    return FloatBits::quiet_nan().get_val();
-  }
-
-  if (x_bits.is_inf()) {
-    if (y_bits.is_zero()) {
-      fputil::set_errno_if_required(EDOM);
-      fputil::raise_except_if_required(FE_INVALID);
-
-      return FloatBits::quiet_nan().get_val();
-    }
-
-    return FloatBits::inf(result_sign).get_val();
-  }
-
-  if (y_bits.is_inf()) {
-    if (x_bits.is_zero()) {
-      fputil::set_errno_if_required(EDOM);
-      fputil::raise_except_if_required(FE_INVALID);
-      return FloatBits::quiet_nan().get_val();
-    }
-
-    return FloatBits::inf(result_sign).get_val();
-  }
-
-  // Now either x or y is zero, and the other one is finite.
-  if (rf_bits.is_inf()) {
-    fputil::set_errno_if_required(ERANGE);
-    return FloatBits::inf(result_sign).get_val();
-  }
-
-  if (x_bits.is_zero() || y_bits.is_zero())
-    return FloatBits::zero(result_sign).get_val();
-
-  fputil::set_errno_if_required(ERANGE);
-  fputil::raise_except_if_required(FE_UNDERFLOW);
-  return result_f;
-
-#endif
+  return math::fmul(x, y);
 }
+
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fmulf128.cpp b/libc/src/math/generic/fmulf128.cpp
index c0c55ace641b8..b0cfb43dc5d91 100644
--- a/libc/src/math/generic/fmulf128.cpp
+++ b/libc/src/math/generic/fmulf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fmulf128.h"
-#include "src/__support/FPUtil/generic/mul.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmulf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fmulf128, (float128 x, float128 y)) {
-  return fputil::generic::mul<float>(x, y);
+  return math::fmulf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fmull.cpp b/libc/src/math/generic/fmull.cpp
index 41ab165e7d09d..9d1255260f62f 100644
--- a/libc/src/math/generic/fmull.cpp
+++ b/libc/src/math/generic/fmull.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fmull.h"
-#include "src/__support/FPUtil/generic/mul.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmull.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fmull, (long double x, long double y)) {
-  return fputil::generic::mul<float>(x, y);
+  return math::fmull(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/frexp.cpp b/libc/src/math/generic/frexp.cpp
index 0efc0d48e588d..4c4ef030d3f23 100644
--- a/libc/src/math/generic/frexp.cpp
+++ b/libc/src/math/generic/frexp.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/frexp.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/frexp.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, frexp, (double x, int *exp)) {
-  return fputil::frexp(x, *exp);
+  return math::frexp(x, exp);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/frexpbf16.cpp b/libc/src/math/generic/frexpbf16.cpp
index 004f64f282f7d..73abde35e5327 100644
--- a/libc/src/math/generic/frexpbf16.cpp
+++ b/libc/src/math/generic/frexpbf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/frexpbf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/frexpbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, frexpbf16, (bfloat16 x, int *exp)) {
-  return fputil::frexp(x, *exp);
+  return math::frexpbf16(x, exp);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/frexpl.cpp b/libc/src/math/generic/frexpl.cpp
index 6f2b456b5944e..c1e0362f6cb08 100644
--- a/libc/src/math/generic/frexpl.cpp
+++ b/libc/src/math/generic/frexpl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/frexpl.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/frexpl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, frexpl, (long double x, int *exp)) {
-  return fputil::frexp(x, *exp);
+  return math::frexpl(x, exp);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fsub.cpp b/libc/src/math/generic/fsub.cpp
index 97e28015c0487..5c841f9443fed 100644
--- a/libc/src/math/generic/fsub.cpp
+++ b/libc/src/math/generic/fsub.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fsub.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fsub.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fsub, (double x, double y)) {
-  return fputil::generic::sub<float>(x, y);
+  return math::fsub(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fsubf128.cpp b/libc/src/math/generic/fsubf128.cpp
index 3efb34992b748..7d971ec7a7150 100644
--- a/libc/src/math/generic/fsubf128.cpp
+++ b/libc/src/math/generic/fsubf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fsubf128.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fsubf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fsubf128, (float128 x, float128 y)) {
-  return fputil::generic::sub<float>(x, y);
+  return math::fsubf128(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fsubl.cpp b/libc/src/math/generic/fsubl.cpp
index cad5a2d5d452a..fd6a3364098f9 100644
--- a/libc/src/math/generic/fsubl.cpp
+++ b/libc/src/math/generic/fsubl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fsubl.h"
-#include "src/__support/FPUtil/generic/add_sub.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fsubl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fsubl, (long double x, long double y)) {
-  return fputil::generic::sub<float>(x, y);
+  return math::fsubl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index bf92e4cbdbcf2..27701bf1bbeee 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -235,9 +235,15 @@ add_fp_unittest(
     libc.src.__support.math.fmodf128
     libc.src.__support.math.fmodf16
     libc.src.__support.math.fmodl
+    libc.src.__support.math.fmul
+    libc.src.__support.math.fmulf128
+    libc.src.__support.math.fmull
+    libc.src.__support.math.frexp
+    libc.src.__support.math.frexpbf16
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
     libc.src.__support.math.frexpf16
+    libc.src.__support.math.frexpl
     libc.src.__support.math.fromfp
     libc.src.__support.math.fromfpbf16
     libc.src.__support.math.fromfpf
@@ -253,6 +259,9 @@ add_fp_unittest(
     libc.src.__support.math.fsqrt
     libc.src.__support.math.fsqrtf128
     libc.src.__support.math.fsqrtl
+    libc.src.__support.math.fsub
+    libc.src.__support.math.fsubf128
+    libc.src.__support.math.fsubl
     libc.src.__support.math.getpayload
     libc.src.__support.math.getpayloadbf16
     libc.src.__support.math.getpayloadf
@@ -529,6 +538,12 @@ add_fp_unittest(
     libc.src.__support.math.fmodf128
     libc.src.__support.math.fmodf16
     libc.src.__support.math.fmodl
+    libc.src.__support.math.fmul
+    libc.src.__support.math.fmulf128
+    libc.src.__support.math.fmull
+    libc.src.__support.math.frexp
+    libc.src.__support.math.frexpbf16
+    libc.src.__support.math.frexpl
     libc.src.__support.math.fromfp
     libc.src.__support.math.fromfpbf16
     libc.src.__support.math.fromfpf
@@ -541,6 +556,9 @@ add_fp_unittest(
     libc.src.__support.math.fromfpxf128
     libc.src.__support.math.fromfpxf16
     libc.src.__support.math.fromfpxl
+    libc.src.__support.math.fsub
+    libc.src.__support.math.fsubf128
+    libc.src.__support.math.fsubl
     libc.src.__support.math.ldexp
     libc.src.__support.math.ldexpbf16
     libc.src.__support.math.ldexpl
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index d53287c978e76..4158338078718 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -58,6 +58,10 @@ static_assert(0.0 == [] {
 static_assert(0.0 == LIBC_NAMESPACE::shared::ldexp(0.0, 0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::scalbln(0.0, 0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::scalbn(0.0, 0.0));
+static_assert(0.0 == [] {
+  int exp{};
+  return LIBC_NAMESPACE::shared::frexp(0.0, &exp);
+}());
 
 //===----------------------------------------------------------------------===//
 //                       Float Tests
@@ -104,6 +108,8 @@ static_assert(0.0f == [] {
 }());
 static_assert(0.0f == LIBC_NAMESPACE::shared::scalblnf(0.0f, 0.0));
 static_assert(0.0f == LIBC_NAMESPACE::shared::scalbnf(0.0f, 0.0));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fmul(0.0, 0.0));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fsub(0.0, 0.0));
 //===----------------------------------------------------------------------===//
 //                       Float16 Tests
 //===----------------------------------------------------------------------===//
@@ -215,6 +221,12 @@ static_assert(0.0L == [] {
 static_assert(0.0L == LIBC_NAMESPACE::shared::ldexpl(0.0L, 0.0));
 static_assert(0.0L == LIBC_NAMESPACE::shared::scalblnl(0.0L, 0.0));
 static_assert(0.0L == LIBC_NAMESPACE::shared::scalbnl(0.0L, 0.0));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fmull(0.0L, 0.0L));
+static_assert(0.0f == LIBC_NAMESPACE::shared::fsubl(0.0L, 0.0L));
+static_assert(0.0L == [] {
+  int exp{};
+  return LIBC_NAMESPACE::shared::frexpl(0.0L, &exp);
+}());
 
 #endif
 
@@ -307,6 +319,10 @@ static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::scalblnf128(float128(0.0), 0.0));
 static_assert(float128(0.0) ==
               LIBC_NAMESPACE::shared::scalbnf128(float128(0.0), 0.0));
+static_assert(0.0f ==
+              LIBC_NAMESPACE::shared::fmulf128(float128(0.0), float128(0.0)));
+static_assert(0.0f ==
+              LIBC_NAMESPACE::shared::fsubf128(float128(0.0), float128(0.0)));
 
 #endif // LIBC_TYPES_HAS_FLOAT128
 
@@ -394,5 +410,9 @@ static_assert(bfloat16(0.0) ==
               LIBC_NAMESPACE::shared::scalblnbf16(bfloat16(0.0), 0.0));
 static_assert(bfloat16(0.0) ==
               LIBC_NAMESPACE::shared::scalbnbf16(bfloat16(0.0), 0.0));
+static_assert(bfloat16(0.0) == [] {
+  int exp{};
+  return LIBC_NAMESPACE::shared::frexpbf16(bfloat16(0.0), &exp);
+}());
 
 TEST(LlvmLibcSharedMathTest, ConstantEvaluation) {}
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 2f2893c407766..c177adf8b46bc 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -284,6 +284,10 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_EQ(1, remquof_exp);
   EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::scalblnf(0.0f, 0L));
   EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::scalbnf(0.0f, 0));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fmul(0.0L, 0.0L));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fmull(0.0L, 0.0L));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fsub(0.0, 0.0));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fsubl(0.0L, 0.0L));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -402,6 +406,9 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::ldexp(0.0, 0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::scalbln(0.0, 0L));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::scalbn(0.0, 0));
+  int frexp_exp = 0;
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::frexp(0.0, &frexp_exp));
+  EXPECT_EQ(0, frexp_exp);
 }
 
 // TODO: Enable the tests when double-double type is supported.
@@ -503,6 +510,9 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::ldexpl(0.0L, 0));
   EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::scalblnl(0.0L, 0L));
   EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::scalbnl(0.0L, 0));
+  int frexpl_exp = 0;
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::frexpl(0.0L, &frexpl_exp));
+  EXPECT_EQ(0, frexpl_exp);
 }
 
 #endif // LIBC_TYPES_LONG_DOUBLE_IS_DOUBLE_DOUBLE
@@ -661,6 +671,10 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
                LIBC_NAMESPACE::shared::scalblnf128(float128(0.0), 0L));
   EXPECT_FP_EQ(float128(0.0),
                LIBC_NAMESPACE::shared::scalbnf128(float128(0.0), 0));
+  EXPECT_FP_EQ(0x0p+0f,
+               LIBC_NAMESPACE::shared::fsubf128(float128(0.0), float128(0.0)));
+  EXPECT_FP_EQ(0x0p+0f,
+               LIBC_NAMESPACE::shared::fmulf128(float128(0.0), float128(0.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
@@ -782,4 +796,8 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
                LIBC_NAMESPACE::shared::scalblnbf16(bfloat16(0.0), 0L));
   EXPECT_FP_EQ(bfloat16(0.0),
                LIBC_NAMESPACE::shared::scalbnbf16(bfloat16(0.0), 0));
+  int frexpbf16_exp = 0;
+  EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::frexpbf16(
+                                  bfloat16(0.0), &frexpbf16_exp));
+  EXPECT_EQ(0, frexpbf16_exp);
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c9f7dbc6a113e..6ea23584c62a0 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4619,6 +4619,64 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fmul",
+    hdrs = ["src/__support/math/fmul.h"],
+    deps = [
+        ":__support_fputil_double_double",
+        ":__support_macros_config",
+        ":hdr_errno_macros",
+        ":hdr_fenv_macros",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fmulf128",
+    hdrs = ["src/__support/math/fmulf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fmull",
+    hdrs = ["src/__support/math/fmull.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_frexp",
+    hdrs = ["src/__support/math/frexp.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_frexpbf16",
+    hdrs = ["src/__support/math/frexpbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_frexpl",
+    hdrs = ["src/__support/math/frexpl.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_fromfp",
     hdrs = ["src/__support/math/fromfp.h"],
@@ -4733,6 +4791,34 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fsub",
+    hdrs = ["src/__support/math/fsub.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fsubf128",
+    hdrs = ["src/__support/math/fsubf128.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fsubl",
+    hdrs = ["src/__support/math/fsubl.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ldexp",
     hdrs = ["src/__support/math/ldexp.h"],
@@ -8619,6 +8705,13 @@ libc_math_function(
     ],
 )
 
+libc_math_function(
+    name = "frexpbf16",
+    additional_deps = [
+        ":__support_math_frexpbf16",
+    ],
+)
+
 libc_math_function(
     name = "fromfpbf16",
     additional_deps = [
@@ -9799,26 +9892,42 @@ libc_math_function(
 libc_math_function(
     name = "fmul",
     additional_deps = [
-        ":__support_fputil_double_double",
-        ":hdr_errno_macros",
-        ":hdr_fenv_macros",
+        ":__support_math_fmul",
     ],
 )
 
-libc_math_function(name = "fmull")
+libc_math_function(
+    name = "fmull",
+    additional_deps = [
+        ":__support_math_fmull",
+    ],
+)
 
 libc_math_function(
     name = "fmulf128",
+    additional_deps = [
+        ":__support_math_fmulf128",
+    ],
 )
 
-libc_math_function(name = "frexp")
+libc_math_function(
+    name = "frexp",
+    additional_deps = [
+        ":__support_math_frexp",
+    ],
+)
 
 libc_math_function(
     name = "frexpf",
     additional_deps = [":__support_math_frexpf"],
 )
 
-libc_math_function(name = "frexpl")
+libc_math_function(
+    name = "frexpl",
+    additional_deps = [
+        ":__support_math_frexpl",
+    ],
+)
 
 libc_math_function(
     name = "frexpf128",
@@ -9922,12 +10031,25 @@ libc_math_function(
     ],
 )
 
-libc_math_function(name = "fsub")
+libc_math_function(
+    name = "fsub",
+    additional_deps = [
+        ":__support_math_fsub",
+    ],
+)
 
-libc_math_function(name = "fsubl")
+libc_math_function(
+    name = "fsubl",
+    additional_deps = [
+        ":__support_math_fsubl",
+    ],
+)
 
 libc_math_function(
     name = "fsubf128",
+    additional_deps = [
+        ":__support_math_fsubf128",
+    ],
 )
 
 libc_math_function(



More information about the libc-commits mailing list