[libc-commits] [libc] [llvm] [libc][math] Refactor nextdown family to header-only (PR #181679)

via libc-commits libc-commits at lists.llvm.org
Mon Feb 16 06:53:18 PST 2026


https://github.com/AnonMiraj created https://github.com/llvm/llvm-project/pull/181679

closes #181678 

>From fd070b0e717a963695345f012a10a718a5d443ab Mon Sep 17 00:00:00 2001
From: Ezzeldin Ibrahim <ezzibrahimx at gmail.com>
Date: Mon, 16 Feb 2026 16:44:09 +0200
Subject: [PATCH] [libc][math] Refactor nextdown family to header-only

---
 libc/shared/math.h                            |  6 ++
 libc/shared/math/nextdown.h                   | 23 +++++
 libc/shared/math/nextdownbf16.h               | 23 +++++
 libc/shared/math/nextdownf.h                  | 23 +++++
 libc/shared/math/nextdownf128.h               | 29 ++++++
 libc/shared/math/nextdownf16.h                | 29 ++++++
 libc/shared/math/nextdownl.h                  | 23 +++++
 libc/src/__support/math/CMakeLists.txt        | 57 ++++++++++++
 libc/src/__support/math/nextdown.h            | 26 ++++++
 libc/src/__support/math/nextdownbf16.h        | 27 ++++++
 libc/src/__support/math/nextdownf.h           | 26 ++++++
 libc/src/__support/math/nextdownf128.h        | 32 +++++++
 libc/src/__support/math/nextdownf16.h         | 32 +++++++
 libc/src/__support/math/nextdownl.h           | 26 ++++++
 libc/src/math/generic/CMakeLists.txt          | 18 ++--
 libc/src/math/generic/nextdown.cpp            |  8 +-
 libc/src/math/generic/nextdownbf16.cpp        |  7 +-
 libc/src/math/generic/nextdownf.cpp           |  8 +-
 libc/src/math/generic/nextdownf128.cpp        |  6 +-
 libc/src/math/generic/nextdownf16.cpp         |  6 +-
 libc/src/math/generic/nextdownl.cpp           |  6 +-
 libc/test/shared/CMakeLists.txt               |  6 ++
 libc/test/shared/shared_math_test.cpp         | 14 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 92 ++++++++++++++++++-
 24 files changed, 507 insertions(+), 46 deletions(-)
 create mode 100644 libc/shared/math/nextdown.h
 create mode 100644 libc/shared/math/nextdownbf16.h
 create mode 100644 libc/shared/math/nextdownf.h
 create mode 100644 libc/shared/math/nextdownf128.h
 create mode 100644 libc/shared/math/nextdownf16.h
 create mode 100644 libc/shared/math/nextdownl.h
 create mode 100644 libc/src/__support/math/nextdown.h
 create mode 100644 libc/src/__support/math/nextdownbf16.h
 create mode 100644 libc/src/__support/math/nextdownf.h
 create mode 100644 libc/src/__support/math/nextdownf128.h
 create mode 100644 libc/src/__support/math/nextdownf16.h
 create mode 100644 libc/src/__support/math/nextdownl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 93f7d6a8156cf..f0e5a6155e9dc 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -111,6 +111,12 @@
 #include "math/logbf16.h"
 #include "math/logf.h"
 #include "math/logf16.h"
+#include "math/nextdown.h"
+#include "math/nextdownbf16.h"
+#include "math/nextdownf.h"
+#include "math/nextdownf128.h"
+#include "math/nextdownf16.h"
+#include "math/nextdownl.h"
 #include "math/pow.h"
 #include "math/powf.h"
 #include "math/rsqrtf.h"
diff --git a/libc/shared/math/nextdown.h b/libc/shared/math/nextdown.h
new file mode 100644
index 0000000000000..5ea6c0ee5b364
--- /dev/null
+++ b/libc/shared/math/nextdown.h
@@ -0,0 +1,23 @@
+//===-- Shared nextdown 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_NEXTDOWN_H
+#define LLVM_LIBC_SHARED_MATH_NEXTDOWN_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nextdown.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nextdown;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTDOWN_H
diff --git a/libc/shared/math/nextdownbf16.h b/libc/shared/math/nextdownbf16.h
new file mode 100644
index 0000000000000..859e8b2ee9a30
--- /dev/null
+++ b/libc/shared/math/nextdownbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared nextdownbf16 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_NEXTDOWNBF16_H
+#define LLVM_LIBC_SHARED_MATH_NEXTDOWNBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nextdownbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nextdownbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTDOWNBF16_H
diff --git a/libc/shared/math/nextdownf.h b/libc/shared/math/nextdownf.h
new file mode 100644
index 0000000000000..1b5a118a7af4b
--- /dev/null
+++ b/libc/shared/math/nextdownf.h
@@ -0,0 +1,23 @@
+//===-- Shared nextdownf 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_NEXTDOWNF_H
+#define LLVM_LIBC_SHARED_MATH_NEXTDOWNF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nextdownf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nextdownf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTDOWNF_H
diff --git a/libc/shared/math/nextdownf128.h b/libc/shared/math/nextdownf128.h
new file mode 100644
index 0000000000000..1632717defc1a
--- /dev/null
+++ b/libc/shared/math/nextdownf128.h
@@ -0,0 +1,29 @@
+//===-- Shared nextdownf128 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_NEXTDOWNF128_H
+#define LLVM_LIBC_SHARED_MATH_NEXTDOWNF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nextdownf128.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nextdownf128;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTDOWNF128_H
diff --git a/libc/shared/math/nextdownf16.h b/libc/shared/math/nextdownf16.h
new file mode 100644
index 0000000000000..f472ec8bd091f
--- /dev/null
+++ b/libc/shared/math/nextdownf16.h
@@ -0,0 +1,29 @@
+//===-- Shared nextdownf16 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_NEXTDOWNF16_H
+#define LLVM_LIBC_SHARED_MATH_NEXTDOWNF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nextdownf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nextdownf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTDOWNF16_H
diff --git a/libc/shared/math/nextdownl.h b/libc/shared/math/nextdownl.h
new file mode 100644
index 0000000000000..f8d0a98e8e944
--- /dev/null
+++ b/libc/shared/math/nextdownl.h
@@ -0,0 +1,23 @@
+//===-- Shared nextdownl 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_NEXTDOWNL_H
+#define LLVM_LIBC_SHARED_MATH_NEXTDOWNL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nextdownl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nextdownl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTDOWNL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index aaab78c01a891..ec66c61ab1177 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1255,6 +1255,63 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.macros.config
 )
+add_header_library(
+  nextdown
+  HDRS
+    nextdown.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nextdownbf16
+  HDRS
+    nextdownbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nextdownf
+  HDRS
+    nextdownf.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nextdownf128
+  HDRS
+    nextdownf128.h
+  DEPENDS
+    libc.include.llvm-libc-types.float128
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nextdownf16
+  HDRS
+    nextdownf16.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float16_macros
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nextdownl
+  HDRS
+    nextdownl.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
 
 add_header_library(
   range_reduction_double
diff --git a/libc/src/__support/math/nextdown.h b/libc/src/__support/math/nextdown.h
new file mode 100644
index 0000000000000..67deeb715536f
--- /dev/null
+++ b/libc/src/__support/math/nextdown.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for nextdown ----------------------*- 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_NEXTDOWN_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWN_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr double nextdown(double x) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWN_H
diff --git a/libc/src/__support/math/nextdownbf16.h b/libc/src/__support/math/nextdownbf16.h
new file mode 100644
index 0000000000000..e09dce316adbf
--- /dev/null
+++ b/libc/src/__support/math/nextdownbf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for nextdownbf16 ------------------*- 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_NEXTDOWNBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNBF16_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 nextdownbf16(bfloat16 x) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNBF16_H
diff --git a/libc/src/__support/math/nextdownf.h b/libc/src/__support/math/nextdownf.h
new file mode 100644
index 0000000000000..580eca4e05881
--- /dev/null
+++ b/libc/src/__support/math/nextdownf.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for nextdownf ---------------------*- 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_NEXTDOWNF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNF_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float nextdownf(float x) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNF_H
diff --git a/libc/src/__support/math/nextdownf128.h b/libc/src/__support/math/nextdownf128.h
new file mode 100644
index 0000000000000..8f5e5c015efe4
--- /dev/null
+++ b/libc/src/__support/math/nextdownf128.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for nextdownf128 ------------------*- 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_NEXTDOWNF128_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNF128_H
+
+#include "include/llvm-libc-types/float128.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT128
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float128 nextdownf128(float128 x) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNF128_H
diff --git a/libc/src/__support/math/nextdownf16.h b/libc/src/__support/math/nextdownf16.h
new file mode 100644
index 0000000000000..6ce4b77947ac0
--- /dev/null
+++ b/libc/src/__support/math/nextdownf16.h
@@ -0,0 +1,32 @@
+//===-- Implementation header for nextdownf16 -------------------*- 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_NEXTDOWNF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr float16 nextdownf16(float16 x) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNF16_H
diff --git a/libc/src/__support/math/nextdownl.h b/libc/src/__support/math/nextdownl.h
new file mode 100644
index 0000000000000..235186d0b2d43
--- /dev/null
+++ b/libc/src/__support/math/nextdownl.h
@@ -0,0 +1,26 @@
+//===-- Implementation header for nextdownl ---------------------*- 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_NEXTDOWNL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNL_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE constexpr long double nextdownl(long double x) {
+  return fputil::nextupdown</*IsDown=*/true>(x);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTDOWNL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 75b51b4587bea..07d74abc66f12 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3534,7 +3534,7 @@ add_entrypoint_object(
   HDRS
     ../nextdown.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nextdown
 )
 
 add_entrypoint_object(
@@ -3544,7 +3544,7 @@ add_entrypoint_object(
   HDRS
     ../nextdownl.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nextdownl
 )
 
 add_entrypoint_object(
@@ -3554,7 +3554,7 @@ add_entrypoint_object(
   HDRS
     ../nextdownf.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nextdownf
 )
 
 add_entrypoint_object(
@@ -3564,8 +3564,7 @@ add_entrypoint_object(
   HDRS
     ../nextdownf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nextdownf16
 )
 
 add_entrypoint_object(
@@ -3575,8 +3574,7 @@ add_entrypoint_object(
   HDRS
     ../nextdownf128.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nextdownf128
 )
 
 add_entrypoint_object(
@@ -3586,11 +3584,7 @@ add_entrypoint_object(
   HDRS
     ../nextdownbf16.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.nextdownbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/nextdown.cpp b/libc/src/math/generic/nextdown.cpp
index 90a91435f6129..5c328dae0b2e4 100644
--- a/libc/src/math/generic/nextdown.cpp
+++ b/libc/src/math/generic/nextdown.cpp
@@ -7,14 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nextdown.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nextdown.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(double, nextdown, (double x)) {
-  return fputil::nextupdown</*IsDown=*/true>(x);
-}
+LLVM_LIBC_FUNCTION(double, nextdown, (double x)) { return math::nextdown(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextdownbf16.cpp b/libc/src/math/generic/nextdownbf16.cpp
index 2115df95ff072..e2e11005c1cf1 100644
--- a/libc/src/math/generic/nextdownbf16.cpp
+++ b/libc/src/math/generic/nextdownbf16.cpp
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nextdownbf16.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/nextdownbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, nextdownbf16, (bfloat16 x)) {
-  return fputil::nextupdown</*IsDown=*/true>(x);
+  return math::nextdownbf16(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextdownf.cpp b/libc/src/math/generic/nextdownf.cpp
index 282ebfd139c67..86db31161cf8d 100644
--- a/libc/src/math/generic/nextdownf.cpp
+++ b/libc/src/math/generic/nextdownf.cpp
@@ -7,14 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nextdownf.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nextdownf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(float, nextdownf, (float x)) {
-  return fputil::nextupdown</*IsDown=*/true>(x);
-}
+LLVM_LIBC_FUNCTION(float, nextdownf, (float x)) { return math::nextdownf(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextdownf128.cpp b/libc/src/math/generic/nextdownf128.cpp
index 1557e3315be9b..3a9a60fe7130a 100644
--- a/libc/src/math/generic/nextdownf128.cpp
+++ b/libc/src/math/generic/nextdownf128.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nextdownf128.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nextdownf128.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float128, nextdownf128, (float128 x)) {
-  return fputil::nextupdown</*IsDown=*/true>(x);
+  return math::nextdownf128(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextdownf16.cpp b/libc/src/math/generic/nextdownf16.cpp
index 2573fbf7267a6..0506b1b14757b 100644
--- a/libc/src/math/generic/nextdownf16.cpp
+++ b/libc/src/math/generic/nextdownf16.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nextdownf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nextdownf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, nextdownf16, (float16 x)) {
-  return fputil::nextupdown</*IsDown=*/true>(x);
+  return math::nextdownf16(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nextdownl.cpp b/libc/src/math/generic/nextdownl.cpp
index 476c5f30e66f9..35648aa291f0f 100644
--- a/libc/src/math/generic/nextdownl.cpp
+++ b/libc/src/math/generic/nextdownl.cpp
@@ -7,14 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nextdownl.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nextdownl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, nextdownl, (long double x)) {
-  return fputil::nextupdown</*IsDown=*/true>(x);
+  return math::nextdownl(x);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9dfbedee66a33..53f14a73faa45 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -108,6 +108,12 @@ add_fp_unittest(
     libc.src.__support.math.llogbf16
     libc.src.__support.math.logf16    
     libc.src.__support.math.llogbl
+    libc.src.__support.math.nextdown
+    libc.src.__support.math.nextdownbf16
+    libc.src.__support.math.nextdownf
+    libc.src.__support.math.nextdownf128
+    libc.src.__support.math.nextdownf16
+    libc.src.__support.math.nextdownl
     libc.src.__support.math.pow
     libc.src.__support.math.powf
     libc.src.__support.math.rsqrtf
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index 2d7ee388f754d..8599c20edff09 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -81,6 +81,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef16(&canonicalizef16_cx,
                                                        &canonicalizef16_x));
   EXPECT_FP_EQ(0x0p+0f16, canonicalizef16_cx);
+
+  EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::nextdownf16(0.0f16));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
@@ -142,6 +144,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef(&canonicalizef_cx,
                                                      &canonicalizef_x));
   EXPECT_FP_EQ(0x0p+0f, canonicalizef_cx);
+
+  EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::nextdownf(0.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -174,6 +178,8 @@ TEST(LlvmLibcSharedMathTest, AllDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalize(&canonicalize_cx,
                                                     &canonicalize_x));
   EXPECT_FP_EQ(0.0, canonicalize_cx);
+
+  EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::nextdown(0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -189,6 +195,8 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizel(&canonicalizel_cx,
                                                      &canonicalizel_x));
   EXPECT_FP_EQ(0x0p+0L, canonicalizel_cx);
+
+  EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::nextdownl(0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -224,6 +232,9 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizef128(&canonicalizef128_cx,
                                                         &canonicalizef128_x));
   EXPECT_FP_EQ(float128(0.0), canonicalizef128_cx);
+
+  EXPECT_FP_EQ(float128(0.0),
+               LIBC_NAMESPACE::shared::nextdownf128(float128(0.0)));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT128
@@ -236,4 +247,7 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) {
   EXPECT_EQ(0, LIBC_NAMESPACE::shared::canonicalizebf16(&canonicalizebf16_cx,
                                                         &canonicalizebf16_x));
   EXPECT_FP_EQ(bfloat16(0.0), canonicalizebf16_cx);
+
+  EXPECT_FP_EQ(bfloat16(0.0),
+               LIBC_NAMESPACE::shared::nextdownbf16(bfloat16(0.0)));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 608180b90e87b..24b66ec1649f8 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3138,6 +3138,63 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_nextdown",
+    hdrs = ["src/__support/math/nextdown.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nextdownbf16",
+    hdrs = ["src/__support/math/nextdownbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nextdownf",
+    hdrs = ["src/__support/math/nextdownf.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nextdownf128",
+    hdrs = ["src/__support/math/nextdownf128.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_properties_types",
+        ":llvm_libc_types_float128",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nextdownf16",
+    hdrs = ["src/__support/math/nextdownf16.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nextdownl",
+    hdrs = ["src/__support/math/nextdownl.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ldexpf128",
     hdrs = ["src/__support/math/ldexpf128.h"],
@@ -5334,15 +5391,40 @@ libc_math_function(name = "nextafterf128")
 
 libc_math_function(name = "nextafterf16")
 
-libc_math_function(name = "nextdown")
+libc_math_function(
+    name = "nextdown",
+    additional_deps = [
+        ":__support_math_nextdown"
+    ],
+)
 
-libc_math_function(name = "nextdownf")
+libc_math_function(
+    name = "nextdownf",
+    additional_deps = [
+        ":__support_math_nextdownf"
+    ],
+)
 
-libc_math_function(name = "nextdownl")
+libc_math_function(
+    name = "nextdownl",
+    additional_deps = [
+        ":__support_math_nextdownl"
+    ],
+)
 
-libc_math_function(name = "nextdownf128")
+libc_math_function(
+    name = "nextdownf128",
+    additional_deps = [
+        ":__support_math_nextdownf128"
+    ],
+)
 
-libc_math_function(name = "nextdownf16")
+libc_math_function(
+    name = "nextdownf16",
+    additional_deps = [
+        ":__support_math_nextdownf16"
+    ],
+)
 
 libc_math_function(name = "nexttoward")
 



More information about the libc-commits mailing list