From e9bb55f12b4f7425e7cfc0c2b86970007934ebe5 Mon Sep 17 00:00:00 2001 From: hulxv Date: Thu, 19 Feb 2026 02:24:58 +0200 Subject: [PATCH 1/4] [libc][math] Refactor floor family to header-only Refactored functions: - floor - floorbf16 - floorf - floorf128 - floorf16 - floorl --- libc/shared/math.h | 6 ++ libc/shared/math/floor.h | 22 ++++ libc/shared/math/floorbf16.h | 22 ++++ libc/shared/math/floorf.h | 22 ++++ libc/shared/math/floorf128.h | 28 +++++ libc/shared/math/floorf16.h | 28 +++++ libc/shared/math/floorl.h | 22 ++++ libc/src/__support/math/CMakeLists.txt | 52 +++++++++ libc/src/__support/math/floor.h | 29 +++++ libc/src/__support/math/floorbf16.h | 24 +++++ libc/src/__support/math/floorf.h | 29 +++++ libc/src/__support/math/floorf128.h | 29 +++++ libc/src/__support/math/floorf16.h | 38 +++++++ libc/src/__support/math/floorl.h | 23 ++++ libc/src/math/generic/CMakeLists.txt | 28 ++--- libc/src/math/generic/floor.cpp | 12 +-- libc/src/math/generic/floorbf16.cpp | 7 +- libc/src/math/generic/floorf.cpp | 12 +-- libc/src/math/generic/floorf128.cpp | 6 +- libc/src/math/generic/floorf16.cpp | 15 +-- libc/src/math/generic/floorl.cpp | 6 +- libc/test/shared/CMakeLists.txt | 6 ++ libc/test/shared/shared_math_test.cpp | 12 +++ .../llvm-project-overlay/libc/BUILD.bazel | 100 +++++++++++++++++- 24 files changed, 505 insertions(+), 73 deletions(-) create mode 100644 libc/shared/math/floor.h create mode 100644 libc/shared/math/floorbf16.h create mode 100644 libc/shared/math/floorf.h create mode 100644 libc/shared/math/floorf128.h create mode 100644 libc/shared/math/floorf16.h create mode 100644 libc/shared/math/floorl.h create mode 100644 libc/src/__support/math/floor.h create mode 100644 libc/src/__support/math/floorbf16.h create mode 100644 libc/src/__support/math/floorf.h create mode 100644 libc/src/__support/math/floorf128.h create mode 100644 libc/src/__support/math/floorf16.h create mode 100644 libc/src/__support/math/floorl.h diff --git a/libc/shared/math.h b/libc/shared/math.h index 8a5aca82c6ec3b..07b3b169812df0 100644 --- a/libc/shared/math.h +++ b/libc/shared/math.h @@ -85,6 +85,12 @@ #include "math/f16sqrtl.h" #include "math/ffma.h" #include "math/ffmal.h" +#include "math/floor.h" +#include "math/floorbf16.h" +#include "math/floorf.h" +#include "math/floorf128.h" +#include "math/floorf16.h" +#include "math/floorl.h" #include "math/frexpf.h" #include "math/frexpf128.h" #include "math/frexpf16.h" diff --git a/libc/shared/math/floor.h b/libc/shared/math/floor.h new file mode 100644 index 00000000000000..feacbf2a7a038e --- /dev/null +++ b/libc/shared/math/floor.h @@ -0,0 +1,22 @@ +//===-- Shared floor 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_FLOOR_H +#define LLVM_LIBC_SHARED_MATH_FLOOR_H + +#include "src/__support/math/floor.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::floor; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_FLOOR_H diff --git a/libc/shared/math/floorbf16.h b/libc/shared/math/floorbf16.h new file mode 100644 index 00000000000000..6153a9fa3d57f5 --- /dev/null +++ b/libc/shared/math/floorbf16.h @@ -0,0 +1,22 @@ +//===-- Shared floorbf16 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_FLOORBF16_H +#define LLVM_LIBC_SHARED_MATH_FLOORBF16_H + +#include "src/__support/math/floorbf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::floorbf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_FLOORBF16_H diff --git a/libc/shared/math/floorf.h b/libc/shared/math/floorf.h new file mode 100644 index 00000000000000..b6b9f8b21a1eac --- /dev/null +++ b/libc/shared/math/floorf.h @@ -0,0 +1,22 @@ +//===-- Shared floorf 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_FLOORF_H +#define LLVM_LIBC_SHARED_MATH_FLOORF_H + +#include "src/__support/math/floorf.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::floorf; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_FLOORF_H diff --git a/libc/shared/math/floorf128.h b/libc/shared/math/floorf128.h new file mode 100644 index 00000000000000..bdf6bf6749f720 --- /dev/null +++ b/libc/shared/math/floorf128.h @@ -0,0 +1,28 @@ +//===-- Shared floorf128 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_FLOORF128_H +#define LLVM_LIBC_SHARED_MATH_FLOORF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/math/floorf128.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::floorf128; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SHARED_MATH_FLOORF128_H diff --git a/libc/shared/math/floorf16.h b/libc/shared/math/floorf16.h new file mode 100644 index 00000000000000..fbb4650798a420 --- /dev/null +++ b/libc/shared/math/floorf16.h @@ -0,0 +1,28 @@ +//===-- Shared floorf16 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_FLOORF16_H +#define LLVM_LIBC_SHARED_MATH_FLOORF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/math/floorf16.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::floorf16; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SHARED_MATH_FLOORF16_H diff --git a/libc/shared/math/floorl.h b/libc/shared/math/floorl.h new file mode 100644 index 00000000000000..f071031a9e22c1 --- /dev/null +++ b/libc/shared/math/floorl.h @@ -0,0 +1,22 @@ +//===-- Shared floorl 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_FLOORL_H +#define LLVM_LIBC_SHARED_MATH_FLOORL_H + +#include "src/__support/math/floorl.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::floorl; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_FLOORL_H diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index e21fe8ef0ab938..aaa99a34ab2dc6 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -800,6 +800,58 @@ add_header_library( libc.src.__support.FPUtil.fma libc.src.__support.macros.config ) +add_header_library( + floor + HDRS + floor.h + DEPENDS + libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.macros.config +) +add_header_library( + floorbf16 + HDRS + floorbf16.h + DEPENDS + libc.src.__support.FPUtil.bfloat16 + libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.macros.config +) +add_header_library( + floorf + HDRS + floorf.h + DEPENDS + libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.macros.config +) +add_header_library( + floorf128 + HDRS + floorf128.h + DEPENDS + libc.include.llvm-libc-types.float128 + libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.macros.config +) +add_header_library( + floorf16 + HDRS + floorf16.h + DEPENDS + libc.include.llvm-libc-macros.float16_macros + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.macros.config +) +add_header_library( + floorl + HDRS + floorl.h + DEPENDS + libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.macros.config +) add_header_library( frexpf128 diff --git a/libc/src/__support/math/floor.h b/libc/src/__support/math/floor.h new file mode 100644 index 00000000000000..2f7ac5841087f2 --- /dev/null +++ b/libc/src/__support/math/floor.h @@ -0,0 +1,29 @@ +//===-- Implementation header for floor -------------------------*- 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_FLOOR_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOOR_H + +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE double floor(double x) { +#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC + return __builtin_floor(x); +#else + return fputil::floor(x); +#endif +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FLOOR_H diff --git a/libc/src/__support/math/floorbf16.h b/libc/src/__support/math/floorbf16.h new file mode 100644 index 00000000000000..72ed6cc3f2b978 --- /dev/null +++ b/libc/src/__support/math/floorbf16.h @@ -0,0 +1,24 @@ +//===-- Implementation header for floorbf16 ---------------------*- 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_FLOORBF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOORBF16_H + +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/FPUtil/bfloat16.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE bfloat16 floorbf16(bfloat16 x) { return fputil::floor(x); } + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FLOORBF16_H diff --git a/libc/src/__support/math/floorf.h b/libc/src/__support/math/floorf.h new file mode 100644 index 00000000000000..693b17441245b2 --- /dev/null +++ b/libc/src/__support/math/floorf.h @@ -0,0 +1,29 @@ +//===-- Implementation header for floorf ------------------------*- 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_FLOORF_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF_H + +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE float floorf(float x) { +#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC + return __builtin_floorf(x); +#else + return fputil::floor(x); +#endif +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF_H diff --git a/libc/src/__support/math/floorf128.h b/libc/src/__support/math/floorf128.h new file mode 100644 index 00000000000000..b2ef990b6d2b7b --- /dev/null +++ b/libc/src/__support/math/floorf128.h @@ -0,0 +1,29 @@ +//===-- Implementation header for floorf128 ---------------------*- 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_FLOORF128_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF128_H + +#include "include/llvm-libc-types/float128.h" + +#ifdef LIBC_TYPES_HAS_FLOAT128 + +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE float128 floorf128(float128 x) { return fputil::floor(x); } + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT128 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF128_H diff --git a/libc/src/__support/math/floorf16.h b/libc/src/__support/math/floorf16.h new file mode 100644 index 00000000000000..82015b6a9f597e --- /dev/null +++ b/libc/src/__support/math/floorf16.h @@ -0,0 +1,38 @@ +//===-- Implementation header for floorf16 ----------------------*- 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_FLOORF16_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF16_H + +#include "include/llvm-libc-macros/float16-macros.h" + +#ifdef LIBC_TYPES_HAS_FLOAT16 + +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/properties/cpu_features.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE float16 floorf16(float16 x) { +#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \ + defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) + return fputil::cast(__builtin_floorf(x)); +#else + return fputil::floor(x); +#endif +} + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LIBC_TYPES_HAS_FLOAT16 + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FLOORF16_H diff --git a/libc/src/__support/math/floorl.h b/libc/src/__support/math/floorl.h new file mode 100644 index 00000000000000..d5977b784da7f7 --- /dev/null +++ b/libc/src/__support/math/floorl.h @@ -0,0 +1,23 @@ +//===-- Implementation header for floorl ------------------------*- 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_FLOORL_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_FLOORL_H + +#include "src/__support/FPUtil/NearestIntegerOperations.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { +namespace math { + +LIBC_INLINE long double floorl(long double x) { return fputil::floor(x); } + +} // namespace math +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_FLOORL_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index 57a29665318a36..25a1768494b7c9 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -671,9 +671,7 @@ add_entrypoint_object( HDRS ../floor.h DEPENDS - libc.src.__support.FPUtil.nearest_integer_operations - FLAGS - ROUND_OPT + libc.src.__support.math.floor ) add_entrypoint_object( @@ -683,9 +681,7 @@ add_entrypoint_object( HDRS ../floorf.h DEPENDS - libc.src.__support.FPUtil.nearest_integer_operations - FLAGS - ROUND_OPT + libc.src.__support.math.floorf ) add_entrypoint_object( @@ -695,7 +691,7 @@ add_entrypoint_object( HDRS ../floorl.h DEPENDS - libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.math.floorl ) add_entrypoint_object( @@ -705,12 +701,7 @@ add_entrypoint_object( HDRS ../floorf16.h DEPENDS - libc.src.__support.FPUtil.cast - libc.src.__support.FPUtil.nearest_integer_operations - libc.src.__support.macros.properties.cpu_features - libc.src.__support.macros.properties.types - FLAGS - ROUND_OPT + libc.src.__support.math.floorf16 ) add_entrypoint_object( @@ -720,8 +711,7 @@ add_entrypoint_object( HDRS ../floorf128.h DEPENDS - libc.src.__support.macros.properties.types - libc.src.__support.FPUtil.nearest_integer_operations + libc.src.__support.math.floorf128 ) add_entrypoint_object( @@ -731,13 +721,7 @@ add_entrypoint_object( HDRS ../floorbf16.h DEPENDS - libc.src.__support.common - libc.src.__support.FPUtil.bfloat16 - libc.src.__support.FPUtil.nearest_integer_operations - libc.src.__support.macros.config - libc.src.__support.macros.properties.types - FLAGS - ROUND_OPT + libc.src.__support.math.floorbf16 ) add_entrypoint_object( diff --git a/libc/src/math/generic/floor.cpp b/libc/src/math/generic/floor.cpp index 86aed6c61a7cca..525e0f2a6ae88b 100644 --- a/libc/src/math/generic/floor.cpp +++ b/libc/src/math/generic/floor.cpp @@ -7,18 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/floor.h" -#include "src/__support/FPUtil/NearestIntegerOperations.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/floor.h" namespace LIBC_NAMESPACE_DECL { -LLVM_LIBC_FUNCTION(double, floor, (double x)) { -#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC - return __builtin_floor(x); -#else - return fputil::floor(x); -#endif -} +LLVM_LIBC_FUNCTION(double, floor, (double x)) { return math::floor(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/floorbf16.cpp b/libc/src/math/generic/floorbf16.cpp index d157096c3e62fe..88fb89d1934321 100644 --- a/libc/src/math/generic/floorbf16.cpp +++ b/libc/src/math/generic/floorbf16.cpp @@ -7,15 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/floorbf16.h" -#include "src/__support/FPUtil/NearestIntegerOperations.h" -#include "src/__support/FPUtil/bfloat16.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/floorbf16.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(bfloat16, floorbf16, (bfloat16 x)) { - return fputil::floor(x); + return math::floorbf16(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/floorf.cpp b/libc/src/math/generic/floorf.cpp index 22739eff68ec2d..c9f7e780b5eb16 100644 --- a/libc/src/math/generic/floorf.cpp +++ b/libc/src/math/generic/floorf.cpp @@ -7,18 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/floorf.h" -#include "src/__support/FPUtil/NearestIntegerOperations.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/floorf.h" namespace LIBC_NAMESPACE_DECL { -LLVM_LIBC_FUNCTION(float, floorf, (float x)) { -#ifdef __LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC - return __builtin_floorf(x); -#else - return fputil::floor(x); -#endif -} +LLVM_LIBC_FUNCTION(float, floorf, (float x)) { return math::floorf(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/floorf128.cpp b/libc/src/math/generic/floorf128.cpp index f3ad20a1dcba09..4740ec569611c4 100644 --- a/libc/src/math/generic/floorf128.cpp +++ b/libc/src/math/generic/floorf128.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/floorf128.h" -#include "src/__support/FPUtil/NearestIntegerOperations.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/floorf128.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(float128, floorf128, (float128 x)) { - return fputil::floor(x); + return math::floorf128(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/floorf16.cpp b/libc/src/math/generic/floorf16.cpp index 361b22729f642e..f79e61278cfca6 100644 --- a/libc/src/math/generic/floorf16.cpp +++ b/libc/src/math/generic/floorf16.cpp @@ -7,21 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/floorf16.h" -#include "src/__support/FPUtil/NearestIntegerOperations.h" -#include "src/__support/FPUtil/cast.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" -#include "src/__support/macros/properties/cpu_features.h" +#include "src/__support/math/floorf16.h" namespace LIBC_NAMESPACE_DECL { -LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { -#if defined(__LIBC_USE_BUILTIN_CEIL_FLOOR_RINT_TRUNC) && \ - defined(LIBC_TARGET_CPU_HAS_FAST_FLOAT16_OPS) - return fputil::cast(__builtin_floorf(x)); -#else - return fputil::floor(x); -#endif -} +LLVM_LIBC_FUNCTION(float16, floorf16, (float16 x)) { return math::floorf16(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/floorl.cpp b/libc/src/math/generic/floorl.cpp index 74fbb6893822ad..6644d57123756f 100644 --- a/libc/src/math/generic/floorl.cpp +++ b/libc/src/math/generic/floorl.cpp @@ -7,14 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/math/floorl.h" -#include "src/__support/FPUtil/NearestIntegerOperations.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/floorl.h" namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(long double, floorl, (long double x)) { - return fputil::floor(x); + return math::floorl(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index 53ad309af4f30e..5ef0fdc6bc9c63 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -81,6 +81,12 @@ add_fp_unittest( libc.src.__support.math.f16sqrtl libc.src.__support.math.ffma libc.src.__support.math.ffmal + libc.src.__support.math.floor + libc.src.__support.math.floorbf16 + libc.src.__support.math.floorf + libc.src.__support.math.floorf128 + libc.src.__support.math.floorf16 + libc.src.__support.math.floorl libc.src.__support.math.frexpf libc.src.__support.math.frexpf128 libc.src.__support.math.frexpf16 diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp index 7f2f39ac7a285a..13909c90accebd 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -88,6 +88,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::floorf16(0.0f16)); } #endif // LIBC_TYPES_HAS_FLOAT16 @@ -150,6 +152,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::floorf(0.0f)); } TEST(LlvmLibcSharedMathTest, AllDouble) { @@ -187,6 +191,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::floor(0.0)); } TEST(LlvmLibcSharedMathTest, AllLongDouble) { @@ -203,6 +209,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::floorl(0.0L)); } #ifdef LIBC_TYPES_HAS_FLOAT128 @@ -238,6 +246,8 @@ 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::floorf128(float128(0.0))); } #endif // LIBC_TYPES_HAS_FLOAT128 @@ -257,4 +267,6 @@ TEST(LlvmLibcSharedMathTest, AllBFloat16) { EXPECT_FP_EQ(bfloat16(5.0), LIBC_NAMESPACE::shared::bf16addf(2.0f, 3.0f)); EXPECT_FP_EQ(bfloat16(10.0), LIBC_NAMESPACE::shared::bf16fmaf(2.0f, 3.0f, 4.0f)); + + EXPECT_FP_EQ(bfloat16(0.0), LIBC_NAMESPACE::shared::floorbf16(bfloat16(0.0))); } diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index b90688af7a1d2d..aa22e236cc83bc 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -3106,6 +3106,64 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_math_floor", + hdrs = ["src/__support/math/floor.h"], + deps = [ + ":__support_fputil_nearest_integer_operations", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_floorbf16", + hdrs = ["src/__support/math/floorbf16.h"], + deps = [ + ":__support_fputil_bfloat16", + ":__support_fputil_nearest_integer_operations", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_floorf", + hdrs = ["src/__support/math/floorf.h"], + deps = [ + ":__support_fputil_nearest_integer_operations", + ":__support_macros_config", + ], +) + +libc_support_library( + name = "__support_math_floorf128", + hdrs = ["src/__support/math/floorf128.h"], + deps = [ + ":__support_fputil_nearest_integer_operations", + ":__support_macros_config", + ":llvm_libc_types_float128", + ], +) + +libc_support_library( + name = "__support_math_floorf16", + hdrs = ["src/__support/math/floorf16.h"], + deps = [ + ":__support_fputil_cast", + ":__support_fputil_nearest_integer_operations", + ":__support_macros_config", + ":llvm_libc_macros_float16_macros", + ], +) + +libc_support_library( + name = "__support_math_floorl", + hdrs = ["src/__support/math/floorl.h"], + deps = [ + ":__support_fputil_nearest_integer_operations", + ":__support_macros_config", + ], +) + libc_support_library( name = "__support_math_ffmal", hdrs = ["src/__support/math/ffmal.h"], @@ -5008,15 +5066,47 @@ libc_math_function( additional_deps = [":__support_fputil_fma"], ) -libc_math_function(name = "floor") +libc_math_function( + name = "floor", + additional_deps = [ + ":__support_math_floor", + ], +) -libc_math_function(name = "floorf") +libc_math_function( + name = "floorbf16", + additional_deps = [ + ":__support_math_floorbf16", + ], +) -libc_math_function(name = "floorl") +libc_math_function( + name = "floorf", + additional_deps = [ + ":__support_math_floorf", + ], +) -libc_math_function(name = "floorf128") +libc_math_function( + name = "floorl", + additional_deps = [ + ":__support_math_floorl", + ], +) -libc_math_function(name = "floorf16") +libc_math_function( + name = "floorf128", + additional_deps = [ + ":__support_math_floorf128", + ], +) + +libc_math_function( + name = "floorf16", + additional_deps = [ + ":__support_math_floorf16", + ], +) libc_math_function( name = "fma", From 8fe259bdeb87656f5b9beb2bf6d410d146718ad3 Mon Sep 17 00:00:00 2001 From: hulxv Date: Tue, 24 Feb 2026 02:35:22 +0200 Subject: [PATCH 2/4] [libc][math] Refactor floor family to header-only Refactored functions: - floor - floorbf16 - floorf - floorf128 - floorf16 - floorl --- libc/src/math/generic/CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index b8235b7401535e..a082ce67f60f23 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -655,6 +655,8 @@ add_entrypoint_object( ../floor.h DEPENDS libc.src.__support.math.floor + FLAGS + ROUND_OPT ) add_entrypoint_object( @@ -665,6 +667,8 @@ add_entrypoint_object( ../floorf.h DEPENDS libc.src.__support.math.floorf + FLAGS + ROUND_OPT ) add_entrypoint_object( @@ -685,6 +689,8 @@ add_entrypoint_object( ../floorf16.h DEPENDS libc.src.__support.math.floorf16 + FLAGS + ROUND_OPT ) add_entrypoint_object( @@ -705,6 +711,8 @@ add_entrypoint_object( ../floorbf16.h DEPENDS libc.src.__support.math.floorbf16 + FLAGS + ROUND_OPT ) add_entrypoint_object( From b4a519341f4f1553457755eca2b6a2d6e2eedfd9 Mon Sep 17 00:00:00 2001 From: hulxv Date: Tue, 24 Feb 2026 22:00:01 +0200 Subject: [PATCH 3/4] correct build deps --- libc/src/__support/math/CMakeLists.txt | 8 ++++++++ libc/src/math/generic/CMakeLists.txt | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index 72a70f81a3b56e..9f1717840d60a1 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -978,6 +978,8 @@ add_header_library( DEPENDS libc.src.__support.FPUtil.nearest_integer_operations libc.src.__support.macros.config + FLAGS + ROUND_OPT ) add_header_library( floorbf16 @@ -987,6 +989,8 @@ add_header_library( libc.src.__support.FPUtil.bfloat16 libc.src.__support.FPUtil.nearest_integer_operations libc.src.__support.macros.config + FLAGS + ROUND_OPT ) add_header_library( floorf @@ -995,6 +999,8 @@ add_header_library( DEPENDS libc.src.__support.FPUtil.nearest_integer_operations libc.src.__support.macros.config + FLAGS + ROUND_OPT ) add_header_library( floorf128 @@ -1014,6 +1020,8 @@ add_header_library( libc.src.__support.FPUtil.cast libc.src.__support.FPUtil.nearest_integer_operations libc.src.__support.macros.config + FLAGS + ROUND_OPT ) add_header_library( floorl diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index f89388b4d2b658..b23c5be251c1cb 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -655,8 +655,6 @@ add_entrypoint_object( ../floor.h DEPENDS libc.src.__support.math.floor - FLAGS - ROUND_OPT ) add_entrypoint_object( @@ -667,8 +665,6 @@ add_entrypoint_object( ../floorf.h DEPENDS libc.src.__support.math.floorf - FLAGS - ROUND_OPT ) add_entrypoint_object( @@ -689,8 +685,6 @@ add_entrypoint_object( ../floorf16.h DEPENDS libc.src.__support.math.floorf16 - FLAGS - ROUND_OPT ) add_entrypoint_object( @@ -711,8 +705,6 @@ add_entrypoint_object( ../floorbf16.h DEPENDS libc.src.__support.math.floorbf16 - FLAGS - ROUND_OPT ) add_entrypoint_object( From e9d26a15ff77443329d9b71986639c54889410f8 Mon Sep 17 00:00:00 2001 From: hulxv Date: Wed, 25 Feb 2026 22:48:14 +0200 Subject: [PATCH 4/4] fix: format --- libc/src/__support/math/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index c4da5013606f6b..f24502719d1c0a 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -407,6 +407,7 @@ add_header_library( libc.src.__support.FPUtil.fma libc.src.__support.macros.config ) + add_header_library( bf16fmaf128 HDRS @@ -1094,6 +1095,7 @@ add_header_library( libc.src.__support.FPUtil.fma libc.src.__support.macros.config ) + add_header_library( floor HDRS @@ -1104,6 +1106,7 @@ add_header_library( FLAGS ROUND_OPT ) + add_header_library( floorbf16 HDRS @@ -1115,6 +1118,7 @@ add_header_library( FLAGS ROUND_OPT ) + add_header_library( floorf HDRS @@ -1125,6 +1129,7 @@ add_header_library( FLAGS ROUND_OPT ) + add_header_library( floorf128 HDRS @@ -1134,6 +1139,7 @@ add_header_library( libc.src.__support.FPUtil.nearest_integer_operations libc.src.__support.macros.config ) + add_header_library( floorf16 HDRS @@ -1146,6 +1152,7 @@ add_header_library( FLAGS ROUND_OPT ) + add_header_library( floorl HDRS