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

via libc-commits libc-commits at lists.llvm.org
Mon Feb 16 07:28:21 PST 2026


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

Refactors the nexttoward math family to be header-only.

Closes https://github.com/llvm/llvm-project/issues/181684

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

---
 libc/shared/math.h                            |  5 ++
 libc/shared/math/nexttoward.h                 | 23 ++++++
 libc/shared/math/nexttowardbf16.h             | 23 ++++++
 libc/shared/math/nexttowardf.h                | 23 ++++++
 libc/shared/math/nexttowardf16.h              | 29 +++++++
 libc/shared/math/nexttowardl.h                | 23 ++++++
 libc/src/__support/math/CMakeLists.txt        | 47 ++++++++++++
 libc/src/__support/math/nexttoward.h          | 28 +++++++
 libc/src/__support/math/nexttowardbf16.h      | 28 +++++++
 libc/src/__support/math/nexttowardf.h         | 28 +++++++
 libc/src/__support/math/nexttowardf16.h       | 34 +++++++++
 libc/src/__support/math/nexttowardl.h         | 28 +++++++
 libc/src/math/generic/CMakeLists.txt          | 15 ++--
 libc/src/math/generic/nexttoward.cpp          |  8 +-
 libc/src/math/generic/nexttowardbf16.cpp      |  8 +-
 libc/src/math/generic/nexttowardf.cpp         |  8 +-
 libc/src/math/generic/nexttowardf16.cpp       |  8 +-
 libc/src/math/generic/nexttowardl.cpp         |  8 +-
 libc/test/shared/CMakeLists.txt               |  5 ++
 libc/test/shared/shared_math_test.cpp         | 12 +++
 .../llvm-project-overlay/libc/BUILD.bazel     | 75 ++++++++++++++++++-
 21 files changed, 422 insertions(+), 44 deletions(-)
 create mode 100644 libc/shared/math/nexttoward.h
 create mode 100644 libc/shared/math/nexttowardbf16.h
 create mode 100644 libc/shared/math/nexttowardf.h
 create mode 100644 libc/shared/math/nexttowardf16.h
 create mode 100644 libc/shared/math/nexttowardl.h
 create mode 100644 libc/src/__support/math/nexttoward.h
 create mode 100644 libc/src/__support/math/nexttowardbf16.h
 create mode 100644 libc/src/__support/math/nexttowardf.h
 create mode 100644 libc/src/__support/math/nexttowardf16.h
 create mode 100644 libc/src/__support/math/nexttowardl.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index 93f7d6a8156cf..8dc3ade9515b9 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -111,6 +111,11 @@
 #include "math/logbf16.h"
 #include "math/logf.h"
 #include "math/logf16.h"
+#include "math/nexttoward.h"
+#include "math/nexttowardbf16.h"
+#include "math/nexttowardf.h"
+#include "math/nexttowardf16.h"
+#include "math/nexttowardl.h"
 #include "math/pow.h"
 #include "math/powf.h"
 #include "math/rsqrtf.h"
diff --git a/libc/shared/math/nexttoward.h b/libc/shared/math/nexttoward.h
new file mode 100644
index 0000000000000..e75a5172aeaf4
--- /dev/null
+++ b/libc/shared/math/nexttoward.h
@@ -0,0 +1,23 @@
+//===-- Shared nexttoward 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_NEXTTOWARD_H
+#define LLVM_LIBC_SHARED_MATH_NEXTTOWARD_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nexttoward.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nexttoward;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTTOWARD_H
diff --git a/libc/shared/math/nexttowardbf16.h b/libc/shared/math/nexttowardbf16.h
new file mode 100644
index 0000000000000..46059754b571b
--- /dev/null
+++ b/libc/shared/math/nexttowardbf16.h
@@ -0,0 +1,23 @@
+//===-- Shared nexttowardbf16 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_NEXTTOWARDBF16_H
+#define LLVM_LIBC_SHARED_MATH_NEXTTOWARDBF16_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nexttowardbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nexttowardbf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTTOWARDBF16_H
diff --git a/libc/shared/math/nexttowardf.h b/libc/shared/math/nexttowardf.h
new file mode 100644
index 0000000000000..156e6df4fe52b
--- /dev/null
+++ b/libc/shared/math/nexttowardf.h
@@ -0,0 +1,23 @@
+//===-- Shared nexttowardf 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_NEXTTOWARDF_H
+#define LLVM_LIBC_SHARED_MATH_NEXTTOWARDF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nexttowardf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nexttowardf;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTTOWARDF_H
diff --git a/libc/shared/math/nexttowardf16.h b/libc/shared/math/nexttowardf16.h
new file mode 100644
index 0000000000000..7753943d7a349
--- /dev/null
+++ b/libc/shared/math/nexttowardf16.h
@@ -0,0 +1,29 @@
+//===-- Shared nexttowardf16 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_NEXTTOWARDF16_H
+#define LLVM_LIBC_SHARED_MATH_NEXTTOWARDF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nexttowardf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nexttowardf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTTOWARDF16_H
diff --git a/libc/shared/math/nexttowardl.h b/libc/shared/math/nexttowardl.h
new file mode 100644
index 0000000000000..ebb727f8418a7
--- /dev/null
+++ b/libc/shared/math/nexttowardl.h
@@ -0,0 +1,23 @@
+//===-- Shared nexttowardl 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_NEXTTOWARDL_H
+#define LLVM_LIBC_SHARED_MATH_NEXTTOWARDL_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/nexttowardl.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::nexttowardl;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_NEXTTOWARDL_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index aaab78c01a891..15f8c4699a470 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1255,6 +1255,53 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.macros.config
 )
+add_header_library(
+  nexttoward
+  HDRS
+    nexttoward.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nexttowardbf16
+  HDRS
+    nexttowardbf16.h
+  DEPENDS
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nexttowardf
+  HDRS
+    nexttowardf.h
+  DEPENDS
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nexttowardf16
+  HDRS
+    nexttowardf16.h
+  DEPENDS
+    libc.include.llvm-libc-macros.float16_macros
+    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.macros.config
+)
+
+add_header_library(
+  nexttowardl
+  HDRS
+    nexttowardl.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/nexttoward.h b/libc/src/__support/math/nexttoward.h
new file mode 100644
index 0000000000000..7f9ba9df3a816
--- /dev/null
+++ b/libc/src/__support/math/nexttoward.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for nexttoward --------------------*- 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_NEXTTOWARD_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARD_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE double nexttoward(double x, long double y) {
+  // We can reuse the nextafter implementation because the internal nextafter is
+  // templated on the types of the arguments.
+  return fputil::nextafter(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARD_H
diff --git a/libc/src/__support/math/nexttowardbf16.h b/libc/src/__support/math/nexttowardbf16.h
new file mode 100644
index 0000000000000..3d0d278f6801f
--- /dev/null
+++ b/libc/src/__support/math/nexttowardbf16.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for nexttowardbf16 ----------------*- 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_NEXTTOWARDBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDBF16_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 bfloat16 nexttowardbf16(bfloat16 x, long double y) {
+  // nextafter<T, U> where T != U is nexttoward
+  return fputil::nextafter(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDBF16_H
diff --git a/libc/src/__support/math/nexttowardf.h b/libc/src/__support/math/nexttowardf.h
new file mode 100644
index 0000000000000..575b50744cd2f
--- /dev/null
+++ b/libc/src/__support/math/nexttowardf.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for nexttowardf -------------------*- 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_NEXTTOWARDF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDF_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE float nexttowardf(float x, long double y) {
+  // We can reuse the nextafter implementation because the internal nextafter is
+  // templated on the types of the arguments.
+  return fputil::nextafter(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDF_H
diff --git a/libc/src/__support/math/nexttowardf16.h b/libc/src/__support/math/nexttowardf16.h
new file mode 100644
index 0000000000000..9e5b3ef049b2d
--- /dev/null
+++ b/libc/src/__support/math/nexttowardf16.h
@@ -0,0 +1,34 @@
+//===-- Implementation header for nexttowardf16 -----------------*- 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_NEXTTOWARDF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDF16_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 float16 nexttowardf16(float16 x, long double y) {
+  // We can reuse the nextafter implementation because the internal nextafter is
+  // templated on the types of the arguments.
+  return fputil::nextafter(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDF16_H
diff --git a/libc/src/__support/math/nexttowardl.h b/libc/src/__support/math/nexttowardl.h
new file mode 100644
index 0000000000000..b1a7a66f69886
--- /dev/null
+++ b/libc/src/__support/math/nexttowardl.h
@@ -0,0 +1,28 @@
+//===-- Implementation header for nexttowardl -------------------*- 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_NEXTTOWARDL_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDL_H
+
+#include "src/__support/FPUtil/ManipulationFunctions.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE long double nexttowardl(long double x, long double y) {
+  // We can reuse the nextafter implementation because the internal nextafter is
+  // templated on the types of the arguments.
+  return fputil::nextafter(x, y);
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTTOWARDL_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 75b51b4587bea..2018be0a99de6 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -3479,7 +3479,7 @@ add_entrypoint_object(
   HDRS
     ../nexttoward.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nexttoward
 )
 
 add_entrypoint_object(
@@ -3489,7 +3489,7 @@ add_entrypoint_object(
   HDRS
     ../nexttowardf.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nexttowardf
 )
 
 add_entrypoint_object(
@@ -3499,7 +3499,7 @@ add_entrypoint_object(
   HDRS
     ../nexttowardl.h
   DEPENDS
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nexttowardl
 )
 
 add_entrypoint_object(
@@ -3509,8 +3509,7 @@ add_entrypoint_object(
   HDRS
     ../nexttowardf16.h
   DEPENDS
-    libc.src.__support.macros.properties.types
-    libc.src.__support.FPUtil.manipulation_functions
+    libc.src.__support.math.nexttowardf16
 )
 
 add_entrypoint_object(
@@ -3520,11 +3519,7 @@ add_entrypoint_object(
   HDRS
     ../nexttowardbf16.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.nexttowardbf16
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/nexttoward.cpp b/libc/src/math/generic/nexttoward.cpp
index 7102a0afe38f3..a5536270bccc7 100644
--- a/libc/src/math/generic/nexttoward.cpp
+++ b/libc/src/math/generic/nexttoward.cpp
@@ -7,16 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nexttoward.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nexttoward.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, nexttoward, (double x, long double y)) {
-  // We can reuse the nextafter implementation because the internal nextafter is
-  // templated on the types of the arguments.
-  return fputil::nextafter(x, y);
+  return math::nexttoward(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nexttowardbf16.cpp b/libc/src/math/generic/nexttowardbf16.cpp
index 3deab87575939..90078219b722c 100644
--- a/libc/src/math/generic/nexttowardbf16.cpp
+++ b/libc/src/math/generic/nexttowardbf16.cpp
@@ -7,16 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nexttowardbf16.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/nexttowardbf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(bfloat16, nexttowardbf16, (bfloat16 x, long double y)) {
-  // nextafter<T, U> where T != U is nexttoward
-  return fputil::nextafter(x, y);
+  return math::nexttowardbf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nexttowardf.cpp b/libc/src/math/generic/nexttowardf.cpp
index 095a7af8c2611..5a4e1d618a1d3 100644
--- a/libc/src/math/generic/nexttowardf.cpp
+++ b/libc/src/math/generic/nexttowardf.cpp
@@ -7,16 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nexttowardf.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nexttowardf.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float, nexttowardf, (float x, long double y)) {
-  // We can reuse the nextafter implementation because the internal nextafter is
-  // templated on the types of the arguments.
-  return fputil::nextafter(x, y);
+  return math::nexttowardf(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nexttowardf16.cpp b/libc/src/math/generic/nexttowardf16.cpp
index 75218b1dcf812..4cd95852fc255 100644
--- a/libc/src/math/generic/nexttowardf16.cpp
+++ b/libc/src/math/generic/nexttowardf16.cpp
@@ -7,16 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nexttowardf16.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nexttowardf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(float16, nexttowardf16, (float16 x, long double y)) {
-  // We can reuse the nextafter implementation because the internal nextafter is
-  // templated on the types of the arguments.
-  return fputil::nextafter(x, y);
+  return math::nexttowardf16(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/nexttowardl.cpp b/libc/src/math/generic/nexttowardl.cpp
index b20868956dbc8..24ca7b5af3a63 100644
--- a/libc/src/math/generic/nexttowardl.cpp
+++ b/libc/src/math/generic/nexttowardl.cpp
@@ -7,16 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/nexttowardl.h"
-#include "src/__support/FPUtil/ManipulationFunctions.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
+#include "src/__support/math/nexttowardl.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long double, nexttowardl, (long double x, long double y)) {
-  // We can reuse the nextafter implementation because the internal nextafter is
-  // templated on the types of the arguments.
-  return fputil::nextafter(x, y);
+  return math::nexttowardl(x, y);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 9dfbedee66a33..a76e1d5e6bd14 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -108,6 +108,11 @@ add_fp_unittest(
     libc.src.__support.math.llogbf16
     libc.src.__support.math.logf16    
     libc.src.__support.math.llogbl
+    libc.src.__support.math.nexttoward
+    libc.src.__support.math.nexttowardbf16
+    libc.src.__support.math.nexttowardf
+    libc.src.__support.math.nexttowardf16
+    libc.src.__support.math.nexttowardl
     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..5211fda2ca5e7 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -81,6 +81,9 @@ 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::nexttowardf16(0.0f16, 0.0f16));
 }
 
 #endif // LIBC_TYPES_HAS_FLOAT16
@@ -142,6 +145,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::nexttowardf(0.0f, 0.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
@@ -174,6 +179,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::nexttoward(0.0, 0.0));
 }
 
 TEST(LlvmLibcSharedMathTest, AllLongDouble) {
@@ -189,6 +196,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::nexttowardl(0.0L, 0.0L));
 }
 
 #ifdef LIBC_TYPES_HAS_FLOAT128
@@ -236,4 +245,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::nexttowardbf16(bfloat16(0.0), 0.0L));
 }
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 608180b90e87b..994cd646ff6da 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3138,6 +3138,53 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_nexttoward",
+    hdrs = ["src/__support/math/nexttoward.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nexttowardbf16",
+    hdrs = ["src/__support/math/nexttowardbf16.h"],
+    deps = [
+        ":__support_fputil_bfloat16",
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nexttowardf",
+    hdrs = ["src/__support/math/nexttowardf.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nexttowardf16",
+    hdrs = ["src/__support/math/nexttowardf16.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+        ":llvm_libc_macros_float16_macros",
+    ],
+)
+
+libc_support_library(
+    name = "__support_math_nexttowardl",
+    hdrs = ["src/__support/math/nexttowardl.h"],
+    deps = [
+        ":__support_fputil_manipulation_functions",
+        ":__support_macros_config",
+    ],
+)
+
 libc_support_library(
     name = "__support_math_ldexpf128",
     hdrs = ["src/__support/math/ldexpf128.h"],
@@ -5344,13 +5391,33 @@ libc_math_function(name = "nextdownf128")
 
 libc_math_function(name = "nextdownf16")
 
-libc_math_function(name = "nexttoward")
+libc_math_function(
+    name = "nexttoward",
+    additional_deps = [
+        ":__support_math_nexttoward",
+    ],
+)
 
-libc_math_function(name = "nexttowardf")
+libc_math_function(
+    name = "nexttowardf",
+    additional_deps = [
+        ":__support_math_nexttowardf",
+    ],
+)
 
-libc_math_function(name = "nexttowardf16")
+libc_math_function(
+    name = "nexttowardf16",
+    additional_deps = [
+        ":__support_math_nexttowardf16",
+    ],
+)
 
-libc_math_function(name = "nexttowardl")
+libc_math_function(
+    name = "nexttowardl",
+    additional_deps = [
+        ":__support_math_nexttowardl",
+    ],
+)
 
 libc_math_function(name = "nextup")
 



More information about the libc-commits mailing list