From 930cf1cb9fdb77138dedf5ac4afc2ebfc46d63a3 Mon Sep 17 00:00:00 2001 From: Siva Chandra Reddy Date: Tue, 3 Nov 2020 16:30:16 -0800 Subject: [PATCH] [libc] Add implementations of ilogb[f|l]. Depends on D90805. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D90806 --- libc/config/linux/aarch64/entrypoints.txt | 3 + libc/config/linux/api.td | 3 + libc/config/linux/x86_64/entrypoints.txt | 3 + libc/spec/stdc.td | 7 ++ libc/src/math/CMakeLists.txt | 36 ++++++++++ libc/src/math/ilogb.cpp | 16 +++++ libc/src/math/ilogb.h | 18 +++++ libc/src/math/ilogbf.cpp | 16 +++++ libc/src/math/ilogbf.h | 18 +++++ libc/src/math/ilogbl.cpp | 16 +++++ libc/src/math/ilogbl.h | 18 +++++ libc/test/src/math/CMakeLists.txt | 42 ++++++++++++ libc/test/src/math/ILogbTest.h | 108 ++++++++++++++++++++++++++++++ libc/test/src/math/ilogb_test.cpp | 41 ++++++++++++ libc/test/src/math/ilogbf_test.cpp | 41 ++++++++++++ libc/test/src/math/ilogbl_test.cpp | 41 ++++++++++++ libc/utils/FPUtil/CMakeLists.txt | 1 + libc/utils/FPUtil/ManipulationFunctions.h | 32 +++++++++ 18 files changed, 460 insertions(+) create mode 100644 libc/src/math/ilogb.cpp create mode 100644 libc/src/math/ilogb.h create mode 100644 libc/src/math/ilogbf.cpp create mode 100644 libc/src/math/ilogbf.h create mode 100644 libc/src/math/ilogbl.cpp create mode 100644 libc/src/math/ilogbl.h create mode 100644 libc/test/src/math/ILogbTest.h create mode 100644 libc/test/src/math/ilogb_test.cpp create mode 100644 libc/test/src/math/ilogbf_test.cpp create mode 100644 libc/test/src/math/ilogbl_test.cpp diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index e654d59..a8aeb6d 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -65,6 +65,9 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.frexpf libc.src.math.frexpl libc.src.math.hypotf + libc.src.math.ilogb + libc.src.math.ilogbf + libc.src.math.ilogbl libc.src.math.logb libc.src.math.logbf libc.src.math.logbl diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td index fbeb040..8415e80 100644 --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -144,6 +144,9 @@ def MathAPI : PublicAPI<"math.h"> { SimpleMacroDef<"INFINITY", "__builtin_inff()">, SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">, + SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN + SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">, + IsFiniteMacro, IsInfMacro, IsNanMacro, diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index a67e408..f72fb77 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -98,6 +98,9 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.frexpf libc.src.math.frexpl libc.src.math.hypotf + libc.src.math.ilogb + libc.src.math.ilogbf + libc.src.math.ilogbl libc.src.math.logb libc.src.math.logbf libc.src.math.logbl diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 7d082d2..de2cbea 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -233,6 +233,9 @@ def StdC : StandardSpec<"stdc"> { Macro<"INFINITY">, Macro<"NAN">, + Macro<"FP_ILOGB0">, + Macro<"FP_ILOGBNAN">, + Macro<"isfinite">, Macro<"isinf">, Macro<"isnan">, @@ -273,6 +276,10 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"hypotf", RetValSpec, [ArgSpec, ArgSpec]>, + FunctionSpec<"ilogb", RetValSpec, [ArgSpec]>, + FunctionSpec<"ilogbf", RetValSpec, [ArgSpec]>, + FunctionSpec<"ilogbl", RetValSpec, [ArgSpec]>, + FunctionSpec<"logb", RetValSpec, [ArgSpec]>, FunctionSpec<"logbf", RetValSpec, [ArgSpec]>, FunctionSpec<"logbl", RetValSpec, [ArgSpec]>, diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 633a1cd..6c2ff2c 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -343,6 +343,42 @@ add_entrypoint_object( ) add_entrypoint_object( + ilogb + SRCS + ilogb.cpp + HDRS + ilogb.h + DEPENDS + libc.utils.FPUtil.fputil + COMPILE_OPTIONS + -O2 +) + +add_entrypoint_object( + ilogbf + SRCS + ilogbf.cpp + HDRS + ilogbf.h + DEPENDS + libc.utils.FPUtil.fputil + COMPILE_OPTIONS + -O2 +) + +add_entrypoint_object( + ilogbl + SRCS + ilogbl.cpp + HDRS + ilogbl.h + DEPENDS + libc.utils.FPUtil.fputil + COMPILE_OPTIONS + -O2 +) + +add_entrypoint_object( logb SRCS logb.cpp diff --git a/libc/src/math/ilogb.cpp b/libc/src/math/ilogb.cpp new file mode 100644 index 0000000..001925b --- /dev/null +++ b/libc/src/math/ilogb.cpp @@ -0,0 +1,16 @@ +//===-- Implementation of ilogb function ----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/__support/common.h" +#include "utils/FPUtil/ManipulationFunctions.h" + +namespace __llvm_libc { + +int LLVM_LIBC_ENTRYPOINT(ilogb)(double x) { return fputil::ilogb(x); } + +} // namespace __llvm_libc diff --git a/libc/src/math/ilogb.h b/libc/src/math/ilogb.h new file mode 100644 index 0000000..9667207 --- /dev/null +++ b/libc/src/math/ilogb.h @@ -0,0 +1,18 @@ +//===-- 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_MATH_ILOGB_H +#define LLVM_LIBC_SRC_MATH_ILOGB_H + +namespace __llvm_libc { + +int ilogb(double x); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_MATH_ILOGB_H diff --git a/libc/src/math/ilogbf.cpp b/libc/src/math/ilogbf.cpp new file mode 100644 index 0000000..d62b22b --- /dev/null +++ b/libc/src/math/ilogbf.cpp @@ -0,0 +1,16 @@ +//===-- Implementation of ilogbf function ---------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/__support/common.h" +#include "utils/FPUtil/ManipulationFunctions.h" + +namespace __llvm_libc { + +int LLVM_LIBC_ENTRYPOINT(ilogbf)(float x) { return fputil::ilogb(x); } + +} // namespace __llvm_libc diff --git a/libc/src/math/ilogbf.h b/libc/src/math/ilogbf.h new file mode 100644 index 0000000..1afb76a --- /dev/null +++ b/libc/src/math/ilogbf.h @@ -0,0 +1,18 @@ +//===-- Implementation header for ilogbf ------------------------*- 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_MATH_ILOGBF_H +#define LLVM_LIBC_SRC_MATH_ILOGBF_H + +namespace __llvm_libc { + +int ilogbf(float x); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_MATH_ILOGBF_H diff --git a/libc/src/math/ilogbl.cpp b/libc/src/math/ilogbl.cpp new file mode 100644 index 0000000..0ad791f --- /dev/null +++ b/libc/src/math/ilogbl.cpp @@ -0,0 +1,16 @@ +//===-- Implementation of ilogbl function ---------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "src/__support/common.h" +#include "utils/FPUtil/ManipulationFunctions.h" + +namespace __llvm_libc { + +int LLVM_LIBC_ENTRYPOINT(ilogbl)(long double x) { return fputil::ilogb(x); } + +} // namespace __llvm_libc diff --git a/libc/src/math/ilogbl.h b/libc/src/math/ilogbl.h new file mode 100644 index 0000000..4d1cc19 --- /dev/null +++ b/libc/src/math/ilogbl.h @@ -0,0 +1,18 @@ +//===-- Implementation header for ilogbl ------------------------*- 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_MATH_ILOGBL_H +#define LLVM_LIBC_SRC_MATH_ILOGBL_H + +namespace __llvm_libc { + +int ilogbl(long double x); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_MATH_ILOGBL_H diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index a907369..4f8f77e 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -371,6 +371,48 @@ add_fp_unittest( ) add_fp_unittest( + ilogb_test + SUITE + libc_math_unittests + SRCS + ilogb_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.include.math + libc.src.math.ilogb + libc.utils.FPUtil.fputil +) + +add_fp_unittest( + ilogbf_test + SUITE + libc_math_unittests + SRCS + ilogbf_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.include.math + libc.src.math.ilogbf + libc.utils.FPUtil.fputil +) + +add_fp_unittest( + ilogbl_test + SUITE + libc_math_unittests + SRCS + ilogbl_test.cpp + HDRS + ILogbTest.h + DEPENDS + libc.include.math + libc.src.math.ilogbl + libc.utils.FPUtil.fputil +) + +add_fp_unittest( logb_test SUITE libc_math_unittests diff --git a/libc/test/src/math/ILogbTest.h b/libc/test/src/math/ILogbTest.h new file mode 100644 index 0000000..001b419 --- /dev/null +++ b/libc/test/src/math/ILogbTest.h @@ -0,0 +1,108 @@ +//===-- Utility class to test different flavors of 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_TEST_SRC_MATH_ILOGBTEST_H +#define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H + +#include "include/math.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/UnitTest/Test.h" + +#include + +class ILogbTest : public __llvm_libc::testing::Test { +public: + template struct ILogbFunc { typedef int (*Func)(T); }; + + template + void testSpecialNumbers(typename ILogbFunc::Func func) { + EXPECT_EQ(FP_ILOGB0, func(__llvm_libc::fputil::FPBits::zero())); + EXPECT_EQ(FP_ILOGB0, func(__llvm_libc::fputil::FPBits::negZero())); + + EXPECT_EQ(FP_ILOGBNAN, func(__llvm_libc::fputil::FPBits::buildNaN(1))); + + EXPECT_EQ(INT_MAX, func(__llvm_libc::fputil::FPBits::inf())); + EXPECT_EQ(INT_MAX, func(__llvm_libc::fputil::FPBits::negInf())); + } + + template void testPowersOfTwo(typename ILogbFunc::Func func) { + EXPECT_EQ(0, func(T(1.0))); + EXPECT_EQ(0, func(T(-1.0))); + + EXPECT_EQ(1, func(T(2.0))); + EXPECT_EQ(1, func(T(-2.0))); + + EXPECT_EQ(2, func(T(4.0))); + EXPECT_EQ(2, func(T(-4.0))); + + EXPECT_EQ(3, func(T(8.0))); + EXPECT_EQ(3, func(-8.0)); + + EXPECT_EQ(4, func(16.0)); + EXPECT_EQ(4, func(-16.0)); + + EXPECT_EQ(5, func(32.0)); + EXPECT_EQ(5, func(-32.0)); + } + + template + void testSomeIntegers(typename ILogbFunc::Func func) { + EXPECT_EQ(1, func(T(3.0))); + EXPECT_EQ(1, func(T(-3.0))); + + EXPECT_EQ(2, func(T(7.0))); + EXPECT_EQ(2, func(T(-7.0))); + + EXPECT_EQ(3, func(T(10.0))); + EXPECT_EQ(3, func(T(-10.0))); + + EXPECT_EQ(4, func(T(31.0))); + EXPECT_EQ(4, func(-31.0)); + + EXPECT_EQ(5, func(55.0)); + EXPECT_EQ(5, func(-55.0)); + } + + template + void testSubnormalRange(typename ILogbFunc::Func func) { + using FPBits = __llvm_libc::fputil::FPBits; + using UIntType = typename FPBits::UIntType; + constexpr UIntType count = 1000001; + constexpr UIntType step = + (FPBits::maxSubnormal - FPBits::minSubnormal) / count; + for (UIntType v = FPBits::minSubnormal; v <= FPBits::maxSubnormal; + v += step) { + T x = FPBits(v); + if (isnan(x) || isinf(x) || x == 0.0) + continue; + + int exponent; + __llvm_libc::fputil::frexp(x, exponent); + ASSERT_EQ(exponent, func(x) + 1); + } + } + + template void testNormalRange(typename ILogbFunc::Func func) { + using FPBits = __llvm_libc::fputil::FPBits; + using UIntType = typename FPBits::UIntType; + constexpr UIntType count = 1000001; + constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; + for (UIntType v = FPBits::minNormal; v <= FPBits::maxNormal; v += step) { + T x = FPBits(v); + if (isnan(x) || isinf(x) || x == 0.0) + continue; + + int exponent; + __llvm_libc::fputil::frexp(x, exponent); + ASSERT_EQ(exponent, func(x) + 1); + } + } +}; + +#endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H diff --git a/libc/test/src/math/ilogb_test.cpp b/libc/test/src/math/ilogb_test.cpp new file mode 100644 index 0000000..cf71ac0 --- /dev/null +++ b/libc/test/src/math/ilogb_test.cpp @@ -0,0 +1,41 @@ +//===-- Unittests for ilogb -----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "ILogbTest.h" + +#include "include/math.h" +#include "src/math/ilogb.h" +#include "utils/CPP/Functional.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/UnitTest/Test.h" + +#include + +using RunContext = __llvm_libc::testing::RunContext; + +TEST_F(ILogbTest, SpecialNumbers_ilogb) { + testSpecialNumbers(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, PowersOfTwo_ilogb) { + testPowersOfTwo(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, SomeIntegers_ilogb) { + testSomeIntegers(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, SubnormalRange_ilogb) { + testSubnormalRange(&__llvm_libc::ilogb); +} + +TEST_F(ILogbTest, NormalRange_ilogb) { + testNormalRange(&__llvm_libc::ilogb); +} diff --git a/libc/test/src/math/ilogbf_test.cpp b/libc/test/src/math/ilogbf_test.cpp new file mode 100644 index 0000000..109ddde --- /dev/null +++ b/libc/test/src/math/ilogbf_test.cpp @@ -0,0 +1,41 @@ +//===-- Unittests for ilogbf ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "ILogbTest.h" + +#include "include/math.h" +#include "src/math/ilogbf.h" +#include "utils/CPP/Functional.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/UnitTest/Test.h" + +#include + +using RunContext = __llvm_libc::testing::RunContext; + +TEST_F(ILogbTest, SpecialNumbers_ilogbf) { + testSpecialNumbers(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, PowersOfTwo_ilogbf) { + testPowersOfTwo(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, SomeIntegers_ilogbf) { + testSomeIntegers(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, SubnormalRange_ilogbf) { + testSubnormalRange(&__llvm_libc::ilogbf); +} + +TEST_F(ILogbTest, NormalRange_ilogbf) { + testNormalRange(&__llvm_libc::ilogbf); +} diff --git a/libc/test/src/math/ilogbl_test.cpp b/libc/test/src/math/ilogbl_test.cpp new file mode 100644 index 0000000..6404c6b --- /dev/null +++ b/libc/test/src/math/ilogbl_test.cpp @@ -0,0 +1,41 @@ +//===-- Unittests for ilogbl ----------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "ILogbTest.h" + +#include "include/math.h" +#include "src/math/ilogbl.h" +#include "utils/CPP/Functional.h" +#include "utils/FPUtil/FPBits.h" +#include "utils/FPUtil/ManipulationFunctions.h" +#include "utils/FPUtil/TestHelpers.h" +#include "utils/UnitTest/Test.h" + +#include + +using RunContext = __llvm_libc::testing::RunContext; + +TEST_F(ILogbTest, SpecialNumbers_ilogbl) { + testSpecialNumbers(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, PowersOfTwo_ilogbl) { + testPowersOfTwo(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, SomeIntegers_ilogbl) { + testSomeIntegers(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, SubnormalRange_ilogbl) { + testSubnormalRange(&__llvm_libc::ilogbl); +} + +TEST_F(ILogbTest, NormalRange_ilogbl) { + testNormalRange(&__llvm_libc::ilogbl); +} diff --git a/libc/utils/FPUtil/CMakeLists.txt b/libc/utils/FPUtil/CMakeLists.txt index 8a6cc36..1c90e01 100644 --- a/libc/utils/FPUtil/CMakeLists.txt +++ b/libc/utils/FPUtil/CMakeLists.txt @@ -20,6 +20,7 @@ add_header_library( NearestIntegerOperations.h NormalFloat.h DEPENDS + libc.include.math libc.utils.CPP.standalone_cpp ) diff --git a/libc/utils/FPUtil/ManipulationFunctions.h b/libc/utils/FPUtil/ManipulationFunctions.h index f233fcd..bfd24c2 100644 --- a/libc/utils/FPUtil/ManipulationFunctions.h +++ b/libc/utils/FPUtil/ManipulationFunctions.h @@ -13,8 +13,11 @@ #include "NearestIntegerOperations.h" #include "NormalFloat.h" +#include "include/math.h" #include "utils/CPP/TypeTraits.h" +#include + namespace __llvm_libc { namespace fputil { @@ -67,6 +70,35 @@ static inline T copysign(T x, T y) { template ::Value, int> = 0> +static inline int ilogb(T x) { + // TODO: Raise appropriate floating point exceptions and set errno to the + // an appropriate error value wherever relevant. + FPBits bits(x); + if (bits.isZero()) { + return FP_ILOGB0; + } else if (bits.isNaN()) { + return FP_ILOGBNAN; + } else if (bits.isInf()) { + return INT_MAX; + } + + NormalFloat normal(bits); + // The C standard does not specify the return value when an exponent is + // out of int range. However, XSI conformance required that INT_MAX or + // INT_MIN are returned. + // NOTE: It is highly unlikely that exponent will be out of int range as + // the exponent is only 15 bits wide even for the 128-bit floating point + // format. + if (normal.exponent > INT_MAX) + return INT_MAX; + else if (normal.exponent < INT_MIN) + return INT_MIN; + else + return normal.exponent; +} + +template ::Value, int> = 0> static inline T logb(T x) { FPBits bits(x); if (bits.isZero()) { -- 2.7.4