From 69e855bb1bcad5cf2a684d424d5094ff61075da8 Mon Sep 17 00:00:00 2001 From: Dasha Buka Date: Tue, 27 Jan 2026 21:45:47 -0800 Subject: [PATCH] [libc][math] Refactor ilogb implementation to header-only in src/__support/math folder. (#175504) closes #175348 --- libc/shared/math.h | 1 + libc/shared/math/ilogb.h | 23 ++++++++++++++++ libc/src/__support/math/CMakeLists.txt | 10 +++++++ libc/src/__support/math/ilogb.h | 27 +++++++++++++++++++ libc/src/math/generic/CMakeLists.txt | 2 +- libc/src/math/generic/ilogb.cpp | 6 ++--- libc/test/shared/CMakeLists.txt | 1 + libc/test/shared/shared_math_test.cpp | 3 ++- .../llvm-project-overlay/libc/BUILD.bazel | 17 +++++++++++- 9 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 libc/shared/math/ilogb.h create mode 100644 libc/src/__support/math/ilogb.h diff --git a/libc/shared/math.h b/libc/shared/math.h index 7db77fc02329..452fe3fddc91 100644 --- a/libc/shared/math.h +++ b/libc/shared/math.h @@ -68,6 +68,7 @@ #include "math/fsqrt.h" #include "math/fsqrtf128.h" #include "math/hypotf.h" +#include "math/ilogb.h" #include "math/ilogbf.h" #include "math/ilogbf128.h" #include "math/ilogbf16.h" diff --git a/libc/shared/math/ilogb.h b/libc/shared/math/ilogb.h new file mode 100644 index 000000000000..54944c20b8e4 --- /dev/null +++ b/libc/shared/math/ilogb.h @@ -0,0 +1,23 @@ +//===-- Shared ilogb 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_ILOGB_H +#define LLVM_LIBC_SHARED_MATH_ILOGB_H + +#include "shared/libc_common.h" +#include "src/__support/math/ilogb.h" + +namespace LIBC_NAMESPACE_DECL { +namespace shared { + +using math::ilogb; + +} // namespace shared +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SHARED_MATH_ILOGB_H diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt index 2b73914d5462..0fa48661e58e 100644 --- a/libc/src/__support/math/CMakeLists.txt +++ b/libc/src/__support/math/CMakeLists.txt @@ -667,6 +667,16 @@ add_header_library( libc.include.llvm-libc-macros.float16_macros ) +add_header_library( + ilogb + HDRS + ilogb.h + DEPENDS + libc.src.__support.common + libc.src.__support.macros.config + libc.src.__support.FPUtil.manipulation_functions +) + add_header_library( ilogbf16 HDRS diff --git a/libc/src/__support/math/ilogb.h b/libc/src/__support/math/ilogb.h new file mode 100644 index 000000000000..021531489b43 --- /dev/null +++ b/libc/src/__support/math/ilogb.h @@ -0,0 +1,27 @@ +//===-- Implementation header for ilogb -------------------------*- 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_ILOGB_H +#define LLVM_LIBC_SRC___SUPPORT_MATH_ILOGB_H + +#include "src/__support/FPUtil/ManipulationFunctions.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +namespace math { + +LIBC_INLINE static constexpr int ilogb(double x) { + return fputil::intlogb(x); +} +} // namespace math + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ILOGB_H diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index afb54194bda1..2475aa52c3c0 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1720,7 +1720,7 @@ add_entrypoint_object( HDRS ../ilogb.h DEPENDS - libc.src.__support.FPUtil.manipulation_functions + libc.src.__support.math.ilogb ) add_entrypoint_object( diff --git a/libc/src/math/generic/ilogb.cpp b/libc/src/math/generic/ilogb.cpp index 60f2af24dc22..74e6166a7339 100644 --- a/libc/src/math/generic/ilogb.cpp +++ b/libc/src/math/generic/ilogb.cpp @@ -7,12 +7,10 @@ //===----------------------------------------------------------------------===// #include "src/math/ilogb.h" -#include "src/__support/FPUtil/ManipulationFunctions.h" -#include "src/__support/common.h" -#include "src/__support/macros/config.h" +#include "src/__support/math/ilogb.h" namespace LIBC_NAMESPACE_DECL { -LLVM_LIBC_FUNCTION(int, ilogb, (double x)) { return fputil::intlogb(x); } +LLVM_LIBC_FUNCTION(int, ilogb, (double x)) { return math::ilogb(x); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt index ce713748a4b7..fb9874ab3ec0 100644 --- a/libc/test/shared/CMakeLists.txt +++ b/libc/test/shared/CMakeLists.txt @@ -64,6 +64,7 @@ add_fp_unittest( libc.src.__support.math.fsqrt libc.src.__support.math.fsqrtf128 libc.src.__support.math.hypotf + libc.src.__support.math.ilogb libc.src.__support.math.ilogbf libc.src.__support.math.ilogbf16 libc.src.__support.math.ilogbf128 diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp index 75c3bdc29257..92511cc55267 100644 --- a/libc/test/shared/shared_math_test.cpp +++ b/libc/test/shared/shared_math_test.cpp @@ -119,8 +119,9 @@ TEST(LlvmLibcSharedMathTest, AllDouble) { EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log1p(0.0)); EXPECT_FP_EQ(0x0p+0, LIBC_NAMESPACE::shared::log2(1.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::sin(0.0)); - EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogb(1.0)); EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::shared::tan(0.0)); + EXPECT_EQ(0, LIBC_NAMESPACE::shared::ilogb(1.0)); + EXPECT_EQ(0L, LIBC_NAMESPACE::shared::llogb(1.0)); } TEST(LlvmLibcSharedMathTest, AllLongDouble) { diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index f3bd226d010f..87b2a392287a 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -2974,6 +2974,16 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_math_ilogb", + hdrs = ["src/__support/math/ilogb.h"], + deps = [ + ":__support_common", + ":__support_fputil_manipulation_functions", + ":__support_macros_config", + ], +) + libc_support_library( name = "__support_math_ilogbf16", hdrs = ["src/__support/math/ilogbf16.h"], @@ -4745,7 +4755,12 @@ libc_math_function( ], ) -libc_math_function(name = "ilogb") +libc_math_function( + name = "ilogb", + additional_deps = [ + ":__support_math_ilogb", + ], +) libc_math_function( name = "ilogbf",