[libc-commits] [libc] [llvm] Relaned "[libc][math] Refactor fmaximum_mag_num family to header-only" (PR #194194)

via libc-commits libc-commits at lists.llvm.org
Sat Apr 25 16:24:39 PDT 2026


https://github.com/hulxv created https://github.com/llvm/llvm-project/pull/194194

Reland #182169

>From fc3a00b7498d5da5c13bfbf3db578daa85114606 Mon Sep 17 00:00:00 2001
From: hulxv <hulxxv at gmail.com>
Date: Sun, 26 Apr 2026 02:18:19 +0300
Subject: [PATCH] Relaned "[libc][math] Refactor fmaximum_mag_num family to
 header-only"

Refactored functions:
  - fmaximum_mag_num
  - fmaximum_mag_numbf16
  - fmaximum_mag_numf
---
 libc/shared/math.h                            |  3 ++
 libc/shared/math/fmaximum_mag_num.h           | 23 +++++++++
 libc/shared/math/fmaximum_mag_numbf16.h       | 23 +++++++++
 libc/shared/math/fmaximum_mag_numf.h          | 23 +++++++++
 libc/src/__support/FPUtil/BasicOperations.h   | 31 +++++++-----
 libc/src/__support/math/CMakeLists.txt        | 30 ++++++++++++
 libc/src/__support/math/fmaximum_mag_num.h    | 25 ++++++++++
 .../src/__support/math/fmaximum_mag_numbf16.h | 26 ++++++++++
 libc/src/__support/math/fmaximum_mag_numf.h   | 25 ++++++++++
 libc/src/math/generic/CMakeLists.txt          | 11 ++---
 libc/src/math/generic/fmaximum_mag_num.cpp    |  6 +--
 .../src/math/generic/fmaximum_mag_numbf16.cpp |  7 +--
 libc/src/math/generic/fmaximum_mag_numf.cpp   |  6 +--
 libc/test/shared/CMakeLists.txt               |  6 +++
 .../shared/shared_math_constexpr_test.cpp     |  5 ++
 libc/test/shared/shared_math_test.cpp         |  7 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 49 ++++++++++++++++++-
 17 files changed, 272 insertions(+), 34 deletions(-)
 create mode 100644 libc/shared/math/fmaximum_mag_num.h
 create mode 100644 libc/shared/math/fmaximum_mag_numbf16.h
 create mode 100644 libc/shared/math/fmaximum_mag_numf.h
 create mode 100644 libc/src/__support/math/fmaximum_mag_num.h
 create mode 100644 libc/src/__support/math/fmaximum_mag_numbf16.h
 create mode 100644 libc/src/__support/math/fmaximum_mag_numf.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 9192831afa3ba..9ac6174a1aa8a 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -172,6 +172,9 @@
 #include "math/fmaxf.h"
 #include "math/fmaxf128.h"
 #include "math/fmaxf16.h"
+#include "math/fmaximum_mag_num.h"
+#include "math/fmaximum_mag_numbf16.h"
+#include "math/fmaximum_mag_numf.h"
 #include "math/fmaxl.h"
 #include "math/frexpf.h"
 #include "math/frexpf128.h"
diff --git a/libc/shared/math/fmaximum_mag_num.h b/libc/shared/math/fmaximum_mag_num.h
new file mode 100644
index 0000000000000..a644072cd4406
--- /dev/null
+++ b/libc/shared/math/fmaximum_mag_num.h
@@ -0,0 +1,23 @@
+//===-- Shared fmaximum_mag_num 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_FMAXIMUM_MAG_NUM_H
+#define LLVM_LIBC_SHARED_MATH_FMAXIMUM_MAG_NUM_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmaximum_mag_num.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmaximum_mag_num;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMAXIMUM_MAG_NUM_H
diff --git a/libc/shared/math/fmaximum_mag_numbf16.h b/libc/shared/math/fmaximum_mag_numbf16.h
new file mode 100644
index 0000000000000..d6b4067e59451
--- /dev/null
+++ b/libc/shared/math/fmaximum_mag_numbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared fmaximum_mag_numbf16 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_FMAXIMUM_MAG_NUMBF16_H
+#define LLVM_LIBC_SHARED_MATH_FMAXIMUM_MAG_NUMBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmaximum_mag_numbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmaximum_mag_numbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMAXIMUM_MAG_NUMBF16_H
diff --git a/libc/shared/math/fmaximum_mag_numf.h b/libc/shared/math/fmaximum_mag_numf.h
new file mode 100644
index 0000000000000..57236f4dcf1d9
--- /dev/null
+++ b/libc/shared/math/fmaximum_mag_numf.h
@@ -0,0 +1,23 @@
+//===-- Shared fmaximum_mag_numf 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_FMAXIMUM_MAG_NUMF_H
+#define LLVM_LIBC_SHARED_MATH_FMAXIMUM_MAG_NUMF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/fmaximum_mag_numf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::fmaximum_mag_numf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_FMAXIMUM_MAG_NUMF_H
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index ee5c90a8f290f..e5b7fb94c7927 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -33,25 +33,30 @@ LIBC_INLINE constexpr T abs(T x) {
 namespace internal {
 
 template <typename T>
-LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> max(T x, T y) {
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
+constexpr_max(T x, T y) {
   FPBits<T> x_bits(x);
   FPBits<T> y_bits(y);
-
-  // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and y
-  // have different signs and both are not NaNs, we return the number with
-  // positive sign.
   if (x_bits.sign() != y_bits.sign())
     return x_bits.is_pos() ? x : y;
   return x > y ? x : y;
 }
 
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_floating_point_v<T>, T>
+max(T x, T y) {
+  return constexpr_max(x, y);
+}
+
 #ifdef LIBC_TYPES_HAS_FLOAT16
 #if defined(__LIBC_USE_BUILTIN_FMAXF16_FMINF16)
-template <> LIBC_INLINE float16 max(float16 x, float16 y) {
+template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
+  if (cpp::is_constant_evaluated())
+    return constexpr_max(x, y);
   return __builtin_fmaxf16(x, y);
 }
 #elif !defined(LIBC_TARGET_ARCH_IS_AARCH64)
-template <> LIBC_INLINE float16 max(float16 x, float16 y) {
+template <> LIBC_INLINE constexpr float16 max(float16 x, float16 y) {
   FPBits<float16> x_bits(x);
   FPBits<float16> y_bits(y);
 
@@ -63,11 +68,15 @@ template <> LIBC_INLINE float16 max(float16 x, float16 y) {
 #endif // LIBC_TYPES_HAS_FLOAT16
 
 #if defined(__LIBC_USE_BUILTIN_FMAX_FMIN) && !defined(LIBC_TARGET_ARCH_IS_X86)
-template <> LIBC_INLINE float max(float x, float y) {
+template <> LIBC_INLINE constexpr float max(float x, float y) {
+  if (cpp::is_constant_evaluated())
+    return constexpr_max(x, y);
   return __builtin_fmaxf(x, y);
 }
 
-template <> LIBC_INLINE double max(double x, double y) {
+template <> LIBC_INLINE constexpr double max(double x, double y) {
+  if (cpp::is_constant_evaluated())
+    return constexpr_max(x, y);
   return __builtin_fmax(x, y);
 }
 #endif
@@ -159,7 +168,7 @@ LIBC_INLINE T fminimum(T x, T y) {
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T fmaximum_num(T x, T y) {
+LIBC_INLINE constexpr T fmaximum_num(T x, T y) {
   FPBits<T> bitx(x), bity(y);
   if (bitx.is_signaling_nan() || bity.is_signaling_nan()) {
     fputil::raise_except_if_required(FE_INVALID);
@@ -211,7 +220,7 @@ LIBC_INLINE T fminimum_mag(T x, T y) {
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
-LIBC_INLINE T fmaximum_mag_num(T x, T y) {
+LIBC_INLINE constexpr T fmaximum_mag_num(T x, T y) {
   FPBits<T> bitx(x), bity(y);
 
   if (abs(x) > abs(y))
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 095028c644d37..2fd5a437cd2eb 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1646,6 +1646,36 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  fmaximum_mag_num
+  HDRS
+    fmaximum_mag_num.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  fmaximum_mag_numbf16
+  HDRS
+    fmaximum_mag_numbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.macros.config
+  FLAGS
+    MISC_MATH_BASIC_OPS_OPT
+)
+
+add_header_library(
+  fmaximum_mag_numf
+  HDRS
+    fmaximum_mag_numf.h
+  DEPENDS
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   fmabf16
   HDRS
diff --git a/libc/src/__support/math/fmaximum_mag_num.h b/libc/src/__support/math/fmaximum_mag_num.h
new file mode 100644
index 0000000000000..bb54f5c8919d1
--- /dev/null
+++ b/libc/src/__support/math/fmaximum_mag_num.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fmaximum_mag_num --------------*- 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_FMAXIMUM_MAG_NUM_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAXIMUM_MAG_NUM_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr double fmaximum_mag_num(double x, double y) {
+  return fputil::fmaximum_mag_num(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAXIMUM_MAG_NUM_H
diff --git a/libc/src/__support/math/fmaximum_mag_numbf16.h b/libc/src/__support/math/fmaximum_mag_numbf16.h
new file mode 100644
index 0000000000000..7010371015589
--- /dev/null
+++ b/libc/src/__support/math/fmaximum_mag_numbf16.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for fmaximum_mag_numbf16 ----------*- 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_FMAXIMUM_MAG_NUMBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAXIMUM_MAG_NUMBF16_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr bfloat16 fmaximum_mag_numbf16(bfloat16 x, bfloat16 y) {
+  return fputil::fmaximum_mag_num(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAXIMUM_MAG_NUMBF16_H
diff --git a/libc/src/__support/math/fmaximum_mag_numf.h b/libc/src/__support/math/fmaximum_mag_numf.h
new file mode 100644
index 0000000000000..c2298b85b4c77
--- /dev/null
+++ b/libc/src/__support/math/fmaximum_mag_numf.h
@@ -0,0 +1,25 @@
+//===-- Implementation header for fmaximum_mag_numf -------------*- 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_FMAXIMUM_MAG_NUMF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_FMAXIMUM_MAG_NUMF_H
+
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr float fmaximum_mag_numf(float x, float y) {
+  return fputil::fmaximum_mag_num(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FMAXIMUM_MAG_NUMF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index ff02cdec1ba0f..e59306fcb970c 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -2423,7 +2423,7 @@ add_entrypoint_object(
   HDRS
     ../fmaximum_mag_num.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fmaximum_mag_num
 )
 
 add_entrypoint_object(
@@ -2433,7 +2433,7 @@ add_entrypoint_object(
   HDRS
     ../fmaximum_mag_numf.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.math.fmaximum_mag_numf
 )
 
 add_entrypoint_object(
@@ -2475,12 +2475,7 @@ add_entrypoint_object(
   HDRS
     ../fmaximum_mag_numbf16.h
   DEPENDS
-    libc.src.__support.FPUtil.basic_operations
-    libc.src.__support.FPUtil.bfloat16
-    libc.src.__support.macros.config
-    libc.src.__support.macros.properties.types
-  FLAGS
-    MISC_MATH_BASIC_OPS_OPT
+    libc.src.__support.math.fmaximum_mag_numbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/fmaximum_mag_num.cpp b/libc/src/math/generic/fmaximum_mag_num.cpp
index de250bbd32b7b..40c8f6e3755d0 100644
--- a/libc/src/math/generic/fmaximum_mag_num.cpp
+++ b/libc/src/math/generic/fmaximum_mag_num.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fmaximum_mag_num.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmaximum_mag_num.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, fmaximum_mag_num, (double x, double y)) {
-  return fputil::fmaximum_mag_num(x, y);
+  return math::fmaximum_mag_num(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fmaximum_mag_numbf16.cpp b/libc/src/math/generic/fmaximum_mag_numbf16.cpp
index 485e32959b53a..a54725b7f1eee 100644
--- a/libc/src/math/generic/fmaximum_mag_numbf16.cpp
+++ b/libc/src/math/generic/fmaximum_mag_numbf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fmaximum_mag_numbf16.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/FPUtil/bfloat16.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmaximum_mag_numbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, fmaximum_mag_numbf16, (bfloat16 x, bfloat16 y)) {
-  return fputil::fmaximum_mag_num(x, y);
+  return math::fmaximum_mag_numbf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/fmaximum_mag_numf.cpp b/libc/src/math/generic/fmaximum_mag_numf.cpp
index e372f1bd1f9d3..97039ef845aec 100644
--- a/libc/src/math/generic/fmaximum_mag_numf.cpp
+++ b/libc/src/math/generic/fmaximum_mag_numf.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/fmaximum_mag_numf.h"
-#include "src/__support/FPUtil/BasicOperations.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/fmaximum_mag_numf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, fmaximum_mag_numf, (float x, float y)) {
-  return fputil::fmaximum_mag_num(x, y);
+  return math::fmaximum_mag_numf(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 0aad310113780..a13f9daabcf99 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -169,6 +169,9 @@ add_fp_unittest(
     libc.src.__support.math.fmaxf
     libc.src.__support.math.fmaxf128
     libc.src.__support.math.fmaxf16
+    libc.src.__support.math.fmaximum_mag_num
+    libc.src.__support.math.fmaximum_mag_numbf16
+    libc.src.__support.math.fmaximum_mag_numf
     libc.src.__support.math.fmaxl
     libc.src.__support.math.frexpf
     libc.src.__support.math.frexpf128
@@ -318,6 +321,9 @@ add_fp_unittest(
     libc.src.__support.math.fabsf128
     libc.src.__support.math.fabsf16
     libc.src.__support.math.fabsl
+    libc.src.__support.math.fmaximum_mag_num
+    libc.src.__support.math.fmaximum_mag_numbf16
+    libc.src.__support.math.fmaximum_mag_numf
     libc.src.__support.math.log
     libc.src.__support.math.logbbf16
 )
diff --git a/libc/test/shared/shared_math_constexpr_test.cpp b/libc/test/shared/shared_math_constexpr_test.cpp
index e9dbdd3740846..216aef14b5c5e 100644
--- a/libc/test/shared/shared_math_constexpr_test.cpp
+++ b/libc/test/shared/shared_math_constexpr_test.cpp
@@ -18,6 +18,7 @@
 static_assert(0.0 == LIBC_NAMESPACE::shared::ceil(0.0));
 static_assert(0.0 == LIBC_NAMESPACE::shared::copysign(0.0, 0.0));
 static_assert(1.0 == LIBC_NAMESPACE::shared::fabs(-1.0));
+static_assert(2.0 == LIBC_NAMESPACE::shared::fmaximum_mag_num(1.0, 2.0));
 static_assert(1.0 == LIBC_NAMESPACE::shared::floor(1.2));
 static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
 
@@ -28,6 +29,7 @@ static_assert(0.0 == LIBC_NAMESPACE::shared::log(1.0));
 static_assert(0.0f == LIBC_NAMESPACE::shared::ceilf(0.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::copysignf(0.0f, 0.0f));
 static_assert(1.0f == LIBC_NAMESPACE::shared::fabsf(-1.0f));
+static_assert(2.0f == LIBC_NAMESPACE::shared::fmaximum_mag_numf(1.0f, 2.0f));
 static_assert(0.0f == LIBC_NAMESPACE::shared::floorf(0.0f));
 
 //===----------------------------------------------------------------------===//
@@ -88,6 +90,9 @@ static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::asinbf16(bfloat16(0.0)));
 static_assert(bfloat16(0.0) == LIBC_NAMESPACE::shared::ceilbf16(bfloat16(0.0)));
 static_assert(bfloat16(1.0) ==
               LIBC_NAMESPACE::shared::fabsbf16(bfloat16(-1.0)));
+static_assert(bfloat16(2.0) ==
+              LIBC_NAMESPACE::shared::fmaximum_mag_numbf16(bfloat16(1.0),
+                                                           bfloat16(2.0)));
 static_assert(bfloat16(0.0) ==
               LIBC_NAMESPACE::shared::copysignbf16(bfloat16(0.0),
                                                    bfloat16(0.0)));
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 7241f4fd37324..81ba675c1e71a 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -182,6 +182,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fdimf(0.0f, 0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::floorf(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fmaxf(0.0f, 0.0f));
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::fmaximum_mag_numf(0.0f, 0.0f));
+
   float getpayloadf_x = 0.0f;
   EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::shared::getpayloadf(&getpayloadf_x));
 
@@ -266,6 +268,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fdim(0.0, 0.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::floor(0.0));
   EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fmax(0.0, 0.0));
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::fmaximum_mag_num(0.0, 0.0));
+
   double getpayload_x = 0.0;
   EXPECT_FP_EQ(-1.0, LIBC_NAMESPACE::shared::getpayload(&getpayload_x));
 
@@ -492,6 +496,9 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_FP_EQ(bfloat16(0.0),
                LIBC_NAMESPACE::shared::fmaxbf16(bfloat16(0.0), bfloat16(0.0)));
 
+  EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::fmaximum_mag_numbf16(
+                         bfloat16(0.0), bfloat16(0.0)));
+
   bfloat16 getpayloadbf16_x = bfloat16(0.0);
   EXPECT_FP_EQ(bfloat16(-1.0),
                LIBC_NAMESPACE::shared::getpayloadbf16(&getpayloadbf16_x));
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 9d1f4f9e6a493..2dbde19166d83 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4794,6 +4794,34 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_fmaximum_mag_num",
+    hdrs = ["src/__support/math/fmaximum_mag_num.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fmaximum_mag_numbf16",
+    hdrs = ["src/__support/math/fmaximum_mag_numbf16.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_fputil_bfloat16",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_fmaximum_mag_numf",
+    hdrs = ["src/__support/math/fmaximum_mag_numf.h"],
+    deps = [
+        ":__support_fputil_basic_operations",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ffmal",
     hdrs = ["src/__support/math/ffmal.h"],
@@ -7884,9 +7912,26 @@ libc_math_function(
     name = "fmaximum_magf16",
 )
 
-libc_math_function(name = "fmaximum_mag_num")
+libc_math_function(
+    name = "fmaximum_mag_num",
+    additional_deps = [
+        ":__support_math_fmaximum_mag_num",
+    ],
+)
 
-libc_math_function(name = "fmaximum_mag_numf")
+libc_math_function(
+    name = "fmaximum_mag_numbf16",
+    additional_deps = [
+        ":__support_math_fmaximum_mag_numbf16",
+    ],
+)
+
+libc_math_function(
+    name = "fmaximum_mag_numf",
+    additional_deps = [
+        ":__support_math_fmaximum_mag_numf",
+    ],
+)
 
 libc_math_function(name = "fmaximum_mag_numl")
 



More information about the libc-commits mailing list