From 7e39b280e8607e4bd09b87fb3271963803b4d02f Mon Sep 17 00:00:00 2001 From: Anonmiraj Date: Fri, 27 Feb 2026 01:52:57 +0200 Subject: [PATCH] [libc][math] Refactor nextafter family to header-only (#181673) closes #181672 --- libc/shared/math.h | 6 ++ libc/shared/math/nextafter.h | 23 +++++ libc/shared/math/nextafterbf16.h | 23 +++++ libc/shared/math/nextafterf.h | 23 +++++ libc/shared/math/nextafterf128.h | 29 ++++++ libc/shared/math/nextafterf16.h | 29 ++++++ libc/shared/math/nextafterl.h | 23 +++++ libc/src/__support/math/CMakeLists.txt | 57 ++++++++++++ libc/src/__support/math/nextafter.h | 26 ++++++ libc/src/__support/math/nextafterbf16.h | 27 ++++++ libc/src/__support/math/nextafterf.h | 26 ++++++ libc/src/__support/math/nextafterf128.h | 32 +++++++ libc/src/__support/math/nextafterf16.h | 32 +++++++ libc/src/__support/math/nextafterl.h | 26 ++++++ libc/src/math/generic/CMakeLists.txt | 18 ++-- libc/src/math/generic/nextafter.cpp | 6 +- libc/src/math/generic/nextafterbf16.cpp | 7 +- libc/src/math/generic/nextafterf.cpp | 6 +- libc/src/math/generic/nextafterf128.cpp | 6 +- libc/src/math/generic/nextafterf16.cpp | 6 +- libc/src/math/generic/nextafterl.cpp | 6 +- libc/test/shared/CMakeLists.txt | 6 ++ libc/test/shared/shared_math_test.cpp | 8 ++ .../llvm-project-overlay/libc/BUILD.bazel | 91 ++++++++++++++++++- 24 files changed, 502 insertions(+), 40 deletions(-) create mode 100644 libc/shared/math/nextafter.h create mode 100644 libc/shared/math/nextafterbf16.h create mode 100644 libc/shared/math/nextafterf.h create mode 100644 libc/shared/math/nextafterf128.h create mode 100644 libc/shared/math/nextafterf16.h create mode 100644 libc/shared/math/nextafterl.h create mode 100644 libc/src/__support/math/nextafter.h create mode 100644 libc/src/__support/math/nextafterbf16.h create mode 100644 libc/src/__support/math/nextafterf.h create mode 100644 libc/src/__support/math/nextafterf128.h create mode 100644 libc/src/__support/math/nextafterf16.h create mode 100644 libc/src/__support/math/nextafterl.h diff --git a/libc/shared/math.h b/libc/shared/math.h index c1cf20ac25f9..c1243e1937bc 100644 --- a/libc/shared/math.h +++ b/libc/shared/math.h @@ -170,6 +170,12 @@ #include "math/logbl.h" #include "math/logf.h" #include "math/logf16.h" +#include "math/nextafter.h" +#include "math/nextafterbf16.h" +#include "math/nextafterf.h" +#include "math/nextafterf128.h" +#include "math/nextafterf16.h" +#include "math/nextafterl.h" #include "math/nextdown.h" #include "math/nextdownbf16.h" #include "math/nextdownf.h" diff --git a/libc/shared/math/nextafter.h b/libc/shared/math/nextafter.h new file mode 100644 index 000000000000..07addbba337e --- /dev/null +++ b/libc/shared/math/nextafter.h @@ -0,0 +1,23 @@ +//===-- Shared nextafter 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_NEXTAFTER_H +#define LLVM_LIBC_SHARED_MATH_NEXTAFTER_H + +#include "shared/libc_common.h" +#include "src/__support/math/nextafter.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::nextafter; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_NEXTAFTER_H diff --git a/libc/shared/math/nextafterbf16.h b/libc/shared/math/nextafterbf16.h new file mode 100644 index 000000000000..52073b1fde40 --- /dev/null +++ b/libc/shared/math/nextafterbf16.h @@ -0,0 +1,23 @@ +//===-- Shared nextafterbf16 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_NEXTAFTERBF16_H +#define LLVM_LIBC_SHARED_MATH_NEXTAFTERBF16_H + +#include "shared/libc_common.h" +#include "src/__support/math/nextafterbf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::nextafterbf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_NEXTAFTERBF16_H diff --git a/libc/shared/math/nextafterf.h b/libc/shared/math/nextafterf.h new file mode 100644 index 000000000000..0933cd30679e --- /dev/null +++ b/libc/shared/math/nextafterf.h @@ -0,0 +1,23 @@ +//===-- Shared nextafterf 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_NEXTAFTERF_H +#define LLVM_LIBC_SHARED_MATH_NEXTAFTERF_H + +#include "shared/libc_common.h" +#include "src/__support/math/nextafterf.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::nextafterf; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_NEXTAFTERF_H diff --git a/libc/shared/math/nextafterf128.h b/libc/shared/math/nextafterf128.h new file mode 100644 index 000000000000..e25c2ea1fee5 --- /dev/null +++ b/libc/shared/math/nextafterf128.h @@ -0,0 +1,29 @@ +//===-- Shared nextafterf128 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_NEXTAFTERF128_H +#define LLVM_LIBC_SHARED_MATH_NEXTAFTERF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "shared/libc_common.h" +#include "src/__support/math/nextafterf128.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::nextafterf128; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_MATH_NEXTAFTERF128_H diff --git a/libc/shared/math/nextafterf16.h b/libc/shared/math/nextafterf16.h new file mode 100644 index 000000000000..c90ac3b9ae69 --- /dev/null +++ b/libc/shared/math/nextafterf16.h @@ -0,0 +1,29 @@ +//===-- Shared nextafterf16 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_NEXTAFTERF16_H +#define LLVM_LIBC_SHARED_MATH_NEXTAFTERF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "shared/libc_common.h" +#include "src/__support/math/nextafterf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::nextafterf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SHARED_MATH_NEXTAFTERF16_H diff --git a/libc/shared/math/nextafterl.h b/libc/shared/math/nextafterl.h new file mode 100644 index 000000000000..4de0f1c25c02 --- /dev/null +++ b/libc/shared/math/nextafterl.h @@ -0,0 +1,23 @@ +//===-- Shared nextafterl 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_NEXTAFTERL_H +#define LLVM_LIBC_SHARED_MATH_NEXTAFTERL_H + +#include "shared/libc_common.h" +#include "src/__support/math/nextafterl.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::nextafterl; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_NEXTAFTERL_H diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index 75a55c2939e1..44b6a1035253 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -1848,6 +1848,63 @@ add_header_library( libc.src.__support.macros.config ) +add_header_library( + nextafter + HDRS + nextafter.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + +add_header_library( + nextafterbf16 + HDRS + nextafterbf16.h + DEPENDS + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + +add_header_library( + nextafterf + HDRS + nextafterf.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + +add_header_library( + nextafterf128 + HDRS + nextafterf128.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + +add_header_library( + nextafterf16 + HDRS + nextafterf16.h + DEPENDS + libc.include.llvm-libc-macros.float16_macros + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + +add_header_library( + nextafterl + HDRS + nextafterl.h + DEPENDS + libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.macros.config +) + add_header_library( nexttoward HDRS diff --git a/libc/src/__support/math/nextafter.h b/libc/src/__support/math/nextafter.h new file mode 100644 index 000000000000..beb2fc9da057 --- /dev/null +++ b/libc/src/__support/math/nextafter.h @@ -0,0 +1,26 @@ +//===-- Implementation header for nextafter ---------------------*- 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_NEXTAFTER_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTER_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +LIBC_INLINE double nextafter(double x, double y) { + return fputil::nextafter(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTER_H diff --git a/libc/src/__support/math/nextafterbf16.h b/libc/src/__support/math/nextafterbf16.h new file mode 100644 index 000000000000..26679e207904 --- /dev/null +++ b/libc/src/__support/math/nextafterbf16.h @@ -0,0 +1,27 @@ +//===-- Implementation header for nextafterbf16 -----------------*- 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_NEXTAFTERBF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERBF16_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 nextafterbf16(bfloat16 x, bfloat16 y) { + return fputil::nextafter(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERBF16_H diff --git a/libc/src/__support/math/nextafterf.h b/libc/src/__support/math/nextafterf.h new file mode 100644 index 000000000000..7d8a0f203879 --- /dev/null +++ b/libc/src/__support/math/nextafterf.h @@ -0,0 +1,26 @@ +//===-- Implementation header for nextafterf --------------------*- 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_NEXTAFTERF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERF_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +LIBC_INLINE float nextafterf(float x, float y) { + return fputil::nextafter(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERF_H diff --git a/libc/src/__support/math/nextafterf128.h b/libc/src/__support/math/nextafterf128.h new file mode 100644 index 000000000000..b13b913c506f --- /dev/null +++ b/libc/src/__support/math/nextafterf128.h @@ -0,0 +1,32 @@ +//===-- Implementation header for nextafterf128 -----------------*- 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_NEXTAFTERF128_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERF128_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 float128 nextafterf128(float128 x, float128 y) { + return fputil::nextafter(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERF128_H diff --git a/libc/src/__support/math/nextafterf16.h b/libc/src/__support/math/nextafterf16.h new file mode 100644 index 000000000000..631790b44cb7 --- /dev/null +++ b/libc/src/__support/math/nextafterf16.h @@ -0,0 +1,32 @@ +//===-- Implementation header for nextafterf16 ------------------*- 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_NEXTAFTERF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERF16_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 nextafterf16(float16 x, float16 y) { + return fputil::nextafter(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERF16_H diff --git a/libc/src/__support/math/nextafterl.h b/libc/src/__support/math/nextafterl.h new file mode 100644 index 000000000000..cf2c2e6da5fb --- /dev/null +++ b/libc/src/__support/math/nextafterl.h @@ -0,0 +1,26 @@ +//===-- Implementation header for nextafterl --------------------*- 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_NEXTAFTERL_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERL_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +LIBC_INLINE long double nextafterl(long double x, long double y) { + return fputil::nextafter(x, y); +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_NEXTAFTERL_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index f8834c5c1270..516817be16cf 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -3345,7 +3345,7 @@ add_entrypoint_object( HDRS ../nextafter.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.nextafter ) add_entrypoint_object( @@ -3355,7 +3355,7 @@ add_entrypoint_object( HDRS ../nextafterf.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.nextafterf ) add_entrypoint_object( @@ -3365,7 +3365,7 @@ add_entrypoint_object( HDRS ../nextafterl.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.nextafterl ) add_entrypoint_object( @@ -3375,8 +3375,7 @@ add_entrypoint_object( HDRS ../nextafterf16.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.nextafterf16 ) add_entrypoint_object( @@ -3386,8 +3385,7 @@ add_entrypoint_object( HDRS ../nextafterf128.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.nextafterf128 ) add_entrypoint_object( @@ -3397,11 +3395,7 @@ add_entrypoint_object( HDRS ../nextafterbf16.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.nextafterbf16 ) add_entrypoint_object( diff --git a/libc/src/math/generic/nextafter.cpp b/libc/src/math/generic/nextafter.cpp index ff35c86930ea..bbb98576e0b4 100644 --- a/libc/src/math/generic/nextafter.cpp +++ b/libc/src/math/generic/nextafter.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/nextafter.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/nextafter.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(double, nextafter, (double x, double y)) { - return fputil::nextafter(x, y); + return math::nextafter(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/nextafterbf16.cpp b/libc/src/math/generic/nextafterbf16.cpp index e21a2dcb3d66..42887c9eba27 100644 --- a/libc/src/math/generic/nextafterbf16.cpp +++ b/libc/src/math/generic/nextafterbf16.cpp @@ -7,15 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/nextafterbf16.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/nextafterbf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(bfloat16, nextafterbf16, (bfloat16 x, bfloat16 y)) { - return fputil::nextafter(x, y); + return math::nextafterbf16(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/nextafterf.cpp b/libc/src/math/generic/nextafterf.cpp index fd8ca02895b6..eca649704b75 100644 --- a/libc/src/math/generic/nextafterf.cpp +++ b/libc/src/math/generic/nextafterf.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/nextafterf.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/nextafterf.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float, nextafterf, (float x, float y)) { - return fputil::nextafter(x, y); + return math::nextafterf(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/nextafterf128.cpp b/libc/src/math/generic/nextafterf128.cpp index c774f10b88cc..3363db898fe0 100644 --- a/libc/src/math/generic/nextafterf128.cpp +++ b/libc/src/math/generic/nextafterf128.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/nextafterf128.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/nextafterf128.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float128, nextafterf128, (float128 x, float128 y)) { - return fputil::nextafter(x, y); + return math::nextafterf128(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/nextafterf16.cpp b/libc/src/math/generic/nextafterf16.cpp index 70adc9c0800d..69fe584eae1a 100644 --- a/libc/src/math/generic/nextafterf16.cpp +++ b/libc/src/math/generic/nextafterf16.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/nextafterf16.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/nextafterf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float16, nextafterf16, (float16 x, float16 y)) { - return fputil::nextafter(x, y); + return math::nextafterf16(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/nextafterl.cpp b/libc/src/math/generic/nextafterl.cpp index 5dafd63b7756..e888f88c36a0 100644 --- a/libc/src/math/generic/nextafterl.cpp +++ b/libc/src/math/generic/nextafterl.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/nextafterl.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/nextafterl.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(long double, nextafterl, (long double x, long double y)) { - return fputil::nextafter(x, y); + return math::nextafterl(x, y); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index 490e498aaf9e..e23b477d0300 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -185,6 +185,12 @@ add_fp_unittest( libc.src.__support.math.nexttowardf libc.src.__support.math.nexttowardf16 libc.src.__support.math.nexttowardl + libc.src.__support.math.nextafter + libc.src.__support.math.nextafterbf16 + libc.src.__support.math.nextafterf + libc.src.__support.math.nextafterf16 + libc.src.__support.math.nextafterl + libc.src.__support.math.nextafterf128 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 43ff7610e384..978418714cdf 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -135,6 +135,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) { EXPECT_FP_EQ(min_denormal, LIBC_NAMESPACE::shared::nextupf16(0.0f16)); EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::nexttowardf16(0.0f16, 0.0f16)); + EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::nextafterf16(0.0f16, 0.0f16)); } #endif // LIBC_TYPES_HAS_FLOAT16 @@ -219,6 +220,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) { float min_denormal = FPBits::min_subnormal(Sign ::POS).get_val(); EXPECT_FP_EQ(min_denormal, LIBC_NAMESPACE::shared::nextupf(0.0f)); EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::nexttowardf(0.0f, 0.0f)); + EXPECT_FP_EQ(0x0p+0f, LIBC_NAMESPACE::shared::nextafterf(0.0f, 0.0f)); } TEST(LlvmLibcSharedMathTest, AllDouble) { @@ -279,6 +281,7 @@ TEST(LlvmLibcSharedMathTest, AllDouble) { double min_denormal = FPBits::min_subnormal(Sign ::POS).get_val(); EXPECT_FP_EQ(min_denormal, LIBC_NAMESPACE::shared::nextup(0.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::nexttoward(0.0, 0.0)); + EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::nextafter(0.0, 0.0)); } TEST(LlvmLibcSharedMathTest, AllLongDouble) { @@ -318,6 +321,7 @@ TEST(LlvmLibcSharedMathTest, AllLongDouble) { long double min_denormal = FPBits::min_subnormal(Sign ::POS).get_val(); EXPECT_FP_EQ(min_denormal, LIBC_NAMESPACE::shared::nextupl(0.0L)); EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::nexttowardl(0.0L, 0.0L)); + EXPECT_FP_EQ(0x0p+0L, LIBC_NAMESPACE::shared::nextafterl(0.0L, 0.0L)); } #ifdef LIBC_TYPES_HAS_FLOAT128 @@ -384,6 +388,8 @@ TEST(LlvmLibcSharedMathTest, AllFloat128) { LIBC_NAMESPACE::shared::nextdownf128(float128(0.0))); float128 min_denormal = FPBits::min_subnormal(Sign ::POS).get_val(); EXPECT_FP_EQ(min_denormal, LIBC_NAMESPACE::shared::nextupf128(float128(0.0))); + EXPECT_FP_EQ(float128(0.0), LIBC_NAMESPACE::shared::nextafterf128( + float128(0.0), float128(0.0))); } #endif // LIBC_TYPES_HAS_FLOAT128 @@ -431,4 +437,6 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) { EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nexttowardbf16(bfloat16(0.0), 0.0L)); + EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::nextafterbf16( + bfloat16(0.0), bfloat16(0.0))); } diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index c31c1e18f1de..b700e0fe9946 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -4107,6 +4107,63 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_math_nextafter", + hdrs = ["src/__support/math/nextafter.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_nextafterbf16", + hdrs = ["src/__support/math/nextafterbf16.h"], + deps = [ + ":__support_fputil_bfloat16", + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_nextafterf", + hdrs = ["src/__support/math/nextafterf.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_nextafterf128", + hdrs = ["src/__support/math/nextafterf128.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ":llvm_libc_types_float128", + ], +) + +libc_support_library( + name = "__support_math_nextafterf16", + hdrs = ["src/__support/math/nextafterf16.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ":llvm_libc_macros_float16_macros", + ], +) + +libc_support_library( + name = "__support_math_nextafterl", + hdrs = ["src/__support/math/nextafterl.h"], + deps = [ + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + libc_support_library( name = "__support_math_nextdown", hdrs = ["src/__support/math/nextdown.h"], @@ -7196,18 +7253,46 @@ libc_math_function( name = "nearbyintf16", ) -libc_math_function(name = "nextafter") +libc_math_function( + name = "nextafter", + additional_deps = [ + ":__support_math_nextafter", + ], +) -libc_math_function(name = "nextafterf") +libc_math_function( + name = "nextafterbf16", + additional_deps = [ + ":__support_math_nextafterbf16", + ], +) -libc_math_function(name = "nextafterl") +libc_math_function( + name = "nextafterf", + additional_deps = [ + ":__support_math_nextafterf", + ], +) + +libc_math_function( + name = "nextafterl", + additional_deps = [ + ":__support_math_nextafterl", + ], +) libc_math_function( name = "nextafterf128", + additional_deps = [ + ":__support_math_nextafterf128", + ], ) libc_math_function( name = "nextafterf16", + additional_deps = [ + ":__support_math_nextafterf16", + ], ) libc_math_function(