98 changes: 86 additions & 12 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -969,10 +969,10 @@ add_entrypoint_object(
ilogb.cpp
HDRS
../ilogb.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
Expand All @@ -981,10 +981,10 @@ add_entrypoint_object(
ilogbf.cpp
HDRS
../ilogbf.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
Expand All @@ -993,10 +993,72 @@ add_entrypoint_object(
ilogbl.cpp
HDRS
../ilogbl.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
ilogbf128
SRCS
ilogbf128.cpp
HDRS
../ilogbf128.h
COMPILE_OPTIONS
-O2
-O3
DEPENDS
libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
llogb
SRCS
llogb.cpp
HDRS
../llogb.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
llogbf
SRCS
llogbf.cpp
HDRS
../llogbf.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
llogbl
SRCS
llogbl.cpp
HDRS
../llogbl.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
llogbf128
SRCS
llogbf128.cpp
HDRS
../llogbf128.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
Expand Down Expand Up @@ -1044,8 +1106,8 @@ add_entrypoint_object(
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.manipulation_functions
libc.src.__support.macros.properties.float
libc.src.__support.FPUtil.manipulation_functions
)

add_object_library(
Expand Down Expand Up @@ -1229,10 +1291,10 @@ add_entrypoint_object(
logb.cpp
HDRS
../logb.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
Expand All @@ -1241,10 +1303,10 @@ add_entrypoint_object(
logbf.cpp
HDRS
../logbf.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
COMPILE_OPTIONS
-O2
)

add_entrypoint_object(
Expand All @@ -1253,10 +1315,22 @@ add_entrypoint_object(
logbl.cpp
HDRS
../logbl.h
COMPILE_OPTIONS
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
logbf128
SRCS
logbf128.cpp
HDRS
../logbf128.h
COMPILE_OPTIONS
-O2
-O3
DEPENDS
libc.src.__support.FPUtil.manipulation_functions
)

add_entrypoint_object(
Expand Down
2 changes: 1 addition & 1 deletion libc/src/math/generic/ilogb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, ilogb, (double x)) { return fputil::ilogb(x); }
LLVM_LIBC_FUNCTION(int, ilogb, (double x)) { return fputil::intlogb<int>(x); }

} // namespace LIBC_NAMESPACE
2 changes: 1 addition & 1 deletion libc/src/math/generic/ilogbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, ilogbf, (float x)) { return fputil::ilogb(x); }
LLVM_LIBC_FUNCTION(int, ilogbf, (float x)) { return fputil::intlogb<int>(x); }

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/ilogbf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of ilogbf128 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/math/ilogbf128.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, ilogbf128, (float128 x)) {
return fputil::intlogb<int>(x);
}

} // namespace LIBC_NAMESPACE
4 changes: 3 additions & 1 deletion libc/src/math/generic/ilogbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, ilogbl, (long double x)) { return fputil::ilogb(x); }
LLVM_LIBC_FUNCTION(int, ilogbl, (long double x)) {
return fputil::intlogb<int>(x);
}

} // namespace LIBC_NAMESPACE
17 changes: 17 additions & 0 deletions libc/src/math/generic/llogb.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Implementation of llogb 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/math/llogb.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(long, llogb, (double x)) { return fputil::intlogb<long>(x); }

} // namespace LIBC_NAMESPACE
17 changes: 17 additions & 0 deletions libc/src/math/generic/llogbf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Implementation of llogbf 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/math/llogbf.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(long, llogbf, (float x)) { return fputil::intlogb<long>(x); }

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/llogbf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of llogbf128 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/math/llogbf128.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(long, llogbf128, (float128 x)) {
return fputil::intlogb<long>(x);
}

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/llogbl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of llogbl 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/math/llogbl.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(long, llogbl, (long double x)) {
return fputil::intlogb<long>(x);
}

} // namespace LIBC_NAMESPACE
2 changes: 1 addition & 1 deletion libc/src/math/generic/logbf.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- Implementation of logbf function ---------------------------------===//
//===-- Implementation of logbf function ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
17 changes: 17 additions & 0 deletions libc/src/math/generic/logbf128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- Implementation of logbf128 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/math/logbf128.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float128, logbf128, (float128 x)) { return fputil::logb(x); }

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/ilogbf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for ilogbf128 ---------------------*- 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_ILOGBF128_H
#define LLVM_LIBC_SRC_MATH_ILOGBF128_H

#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE {

int ilogbf128(float128 x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_ILOGBF128_H
20 changes: 20 additions & 0 deletions libc/src/math/llogb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for llogb -------------------------*- 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_LLOGB_H
#define LLVM_LIBC_SRC_MATH_LLOGB_H

#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE {

long llogb(double x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_LLOGB_H
20 changes: 20 additions & 0 deletions libc/src/math/llogbf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for llogbf ------------------------*- 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_LLOGBF_H
#define LLVM_LIBC_SRC_MATH_LLOGBF_H

#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE {

long llogbf(float x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_LLOGBF_H
20 changes: 20 additions & 0 deletions libc/src/math/llogbf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for llogbf128 ---------------------*- 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_LLOGBF128_H
#define LLVM_LIBC_SRC_MATH_LLOGBF128_H

#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE {

long llogbf128(float128 x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_LLOGBF128_H
20 changes: 20 additions & 0 deletions libc/src/math/llogbl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for llogbl ------------------------*- 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_LLOGBL_H
#define LLVM_LIBC_SRC_MATH_LLOGBL_H

#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE {

long llogbl(long double x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_LLOGBL_H
20 changes: 20 additions & 0 deletions libc/src/math/logbf128.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for logbf128 ---------------------*- 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_LOGBF128_H
#define LLVM_LIBC_SRC_MATH_LOGBF128_H

#include "src/__support/macros/properties/float.h"

namespace LIBC_NAMESPACE {

float128 logbf128(float128 x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_LOGBF128_H
92 changes: 86 additions & 6 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
HDRS
ILogbTest.h
DEPENDS
libc.include.math
libc.src.math.ilogb
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
Expand All @@ -785,7 +784,6 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
HDRS
ILogbTest.h
DEPENDS
libc.include.math
libc.src.math.ilogbf
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
Expand All @@ -802,13 +800,87 @@ add_fp_unittest(
HDRS
ILogbTest.h
DEPENDS
libc.include.math
libc.src.math.ilogbl
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
ilogbf128_test
SUITE
libc-math-smoke-tests
SRCS
ilogbf128_test.cpp
HDRS
ILogbTest.h
DEPENDS
libc.src.math.ilogbf128
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
llogb_test
SUITE
libc-math-smoke-tests
SRCS
llogb_test.cpp
HDRS
ILogbTest.h
DEPENDS
libc.src.math.llogb
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
llogbf_test
SUITE
libc-math-smoke-tests
SRCS
llogbf_test.cpp
HDRS
ILogbTest.h
DEPENDS
libc.src.math.llogbf
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
llogbl_test
SUITE
libc-math-smoke-tests
SRCS
llogbl_test.cpp
HDRS
ILogbTest.h
DEPENDS
libc.src.math.llogbl
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
llogbf128_test
SUITE
libc-math-smoke-tests
SRCS
llogbf128_test.cpp
HDRS
ILogbTest.h
DEPENDS
libc.src.math.llogbf128
libc.src.__support.CPP.limits
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
ldexp_test
SUITE
Expand Down Expand Up @@ -876,7 +948,6 @@ add_fp_unittest(
SRCS
logb_test.cpp
DEPENDS
libc.include.math
libc.src.math.logb
libc.src.__support.FPUtil.manipulation_functions
)
Expand All @@ -888,7 +959,6 @@ add_fp_unittest(
SRCS
logbf_test.cpp
DEPENDS
libc.include.math
libc.src.math.logbf
libc.src.__support.FPUtil.manipulation_functions
)
Expand All @@ -902,11 +972,21 @@ add_fp_unittest(
HDRS
LogbTest.h
DEPENDS
libc.include.math
libc.src.math.logbl
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
logbf128_test
SUITE
libc-math-smoke-tests
SRCS
logbf128_test.cpp
DEPENDS
libc.src.math.logbf128
libc.src.__support.FPUtil.manipulation_functions
)

add_fp_unittest(
modf_test
SUITE
Expand Down
117 changes: 63 additions & 54 deletions libc/test/src/math/smoke/ILogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,101 +13,110 @@
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "test/UnitTest/Test.h"
#include <math.h>

template <typename OutType, typename InType>
class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::Test {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<InType>;
using StorageType = typename FPBits::StorageType;
using Sign = LIBC_NAMESPACE::fputil::Sign;

public:
template <typename T> struct ILogbFunc {
typedef int (*Func)(T);
};

template <typename T>
void test_special_numbers(typename ILogbFunc<T>::Func func) {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using Sign = LIBC_NAMESPACE::fputil::Sign;
EXPECT_EQ(FP_ILOGB0, func(FPBits::zero(Sign::POS).get_val()));
EXPECT_EQ(FP_ILOGB0, func(FPBits::zero(Sign::NEG).get_val()));
EXPECT_EQ(FP_ILOGBNAN, func(FPBits::quiet_nan().get_val()));
EXPECT_EQ(INT_MAX, func(FPBits::inf(Sign::POS).get_val()));
EXPECT_EQ(INT_MAX, func(FPBits::inf(Sign::NEG).get_val()));
typedef OutType (*Func)(InType);

void test_special_numbers(Func func) {
EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::FP_LOGB0,
func(FPBits::zero(Sign::POS).get_val()));
EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::FP_LOGB0,
func(FPBits::zero(Sign::NEG).get_val()));
EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::FP_LOGBNAN,
func(FPBits::quiet_nan().get_val()));
EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::T_MAX,
func(FPBits::inf(Sign::POS).get_val()));
EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::T_MAX,
func(FPBits::inf(Sign::NEG).get_val()));
}

template <typename T>
void test_powers_of_two(typename ILogbFunc<T>::Func func) {
EXPECT_EQ(0, func(T(1.0)));
EXPECT_EQ(0, func(T(-1.0)));
void test_powers_of_two(Func func) {
EXPECT_EQ(OutType(0), func(InType(1.0)));
EXPECT_EQ(OutType(0), func(InType(-1.0)));

EXPECT_EQ(1, func(T(2.0)));
EXPECT_EQ(1, func(T(-2.0)));
EXPECT_EQ(OutType(1), func(InType(2.0)));
EXPECT_EQ(OutType(1), func(InType(-2.0)));

EXPECT_EQ(2, func(T(4.0)));
EXPECT_EQ(2, func(T(-4.0)));
EXPECT_EQ(OutType(2), func(InType(4.0)));
EXPECT_EQ(OutType(2), func(InType(-4.0)));

EXPECT_EQ(3, func(T(8.0)));
EXPECT_EQ(3, func(-8.0));
EXPECT_EQ(OutType(3), func(InType(8.0)));
EXPECT_EQ(OutType(3), func(-8.0));

EXPECT_EQ(4, func(16.0));
EXPECT_EQ(4, func(-16.0));
EXPECT_EQ(OutType(4), func(16.0));
EXPECT_EQ(OutType(4), func(-16.0));

EXPECT_EQ(5, func(32.0));
EXPECT_EQ(5, func(-32.0));
EXPECT_EQ(OutType(5), func(32.0));
EXPECT_EQ(OutType(5), func(-32.0));
}

template <typename T>
void test_some_integers(typename ILogbFunc<T>::Func func) {
EXPECT_EQ(1, func(T(3.0)));
EXPECT_EQ(1, func(T(-3.0)));
void test_some_integers(Func func) {
EXPECT_EQ(OutType(1), func(InType(3.0)));
EXPECT_EQ(OutType(1), func(InType(-3.0)));

EXPECT_EQ(2, func(T(7.0)));
EXPECT_EQ(2, func(T(-7.0)));
EXPECT_EQ(OutType(2), func(InType(7.0)));
EXPECT_EQ(OutType(2), func(InType(-7.0)));

EXPECT_EQ(3, func(T(10.0)));
EXPECT_EQ(3, func(T(-10.0)));
EXPECT_EQ(OutType(3), func(InType(10.0)));
EXPECT_EQ(OutType(3), func(InType(-10.0)));

EXPECT_EQ(4, func(T(31.0)));
EXPECT_EQ(4, func(-31.0));
EXPECT_EQ(OutType(4), func(InType(31.0)));
EXPECT_EQ(OutType(4), func(-31.0));

EXPECT_EQ(5, func(55.0));
EXPECT_EQ(5, func(-55.0));
EXPECT_EQ(OutType(5), func(55.0));
EXPECT_EQ(OutType(5), func(-55.0));
}

template <typename T>
void test_subnormal_range(typename ILogbFunc<T>::Func func) {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
void test_subnormal_range(Func func) {
constexpr StorageType MIN_SUBNORMAL = FPBits::min_subnormal().uintval();
constexpr StorageType MAX_SUBNORMAL = FPBits::max_subnormal().uintval();
constexpr StorageType COUNT = 10'001;
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0)
FPBits x_bits = FPBits(v);
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
continue;

InType x = x_bits.get_val();

int exponent;
LIBC_NAMESPACE::fputil::frexp(x, exponent);
ASSERT_EQ(exponent, func(x) + 1);
ASSERT_EQ(static_cast<OutType>(exponent), func(x) + OutType(1));
}
}

template <typename T>
void test_normal_range(typename ILogbFunc<T>::Func func) {
using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
using StorageType = typename FPBits::StorageType;
void test_normal_range(Func func) {
constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
constexpr StorageType COUNT = 10'001;
constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0)
FPBits x_bits = FPBits(v);
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
continue;

InType x = x_bits.get_val();

int exponent;
LIBC_NAMESPACE::fputil::frexp(x, exponent);
ASSERT_EQ(exponent, func(x) + 1);
ASSERT_EQ(static_cast<OutType>(exponent), func(x) + OutType(1));
}
}
};

#define LIST_INTLOGB_TESTS(OutType, InType, Func) \
using LlvmLibcIntLogbTest = LlvmLibcILogbTest<OutType, InType>; \
TEST_F(LlvmLibcIntLogbTest, SpecialNumbers) { test_special_numbers(&Func); } \
TEST_F(LlvmLibcIntLogbTest, PowersOfTwo) { test_powers_of_two(&Func); } \
TEST_F(LlvmLibcIntLogbTest, SomeIntegers) { test_some_integers(&Func); } \
TEST_F(LlvmLibcIntLogbTest, SubnormalRange) { test_subnormal_range(&Func); } \
TEST_F(LlvmLibcIntLogbTest, NormalRange) { test_normal_range(&Func); } \
static_assert(true)

#endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
8 changes: 4 additions & 4 deletions libc/test/src/math/smoke/LogbTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

#include <math.h>

template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {

DECLARE_SPECIAL_CONSTANTS(T)
Expand Down Expand Up @@ -72,10 +70,12 @@ template <typename T> class LogbTest : public LIBC_NAMESPACE::testing::Test {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x) || x == 0.0l)
FPBits x_bits = FPBits(v);
if (x_bits.is_zero() || x_bits.is_inf_or_nan())
continue;

T x = x_bits.get_val();

int exponent;
LIBC_NAMESPACE::fputil::frexp(x, exponent);
ASSERT_FP_EQ(T(exponent), func(x) + T(1.0));
Expand Down
25 changes: 1 addition & 24 deletions libc/test/src/math/smoke/ilogb_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@

#include "ILogbTest.h"

#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/math/ilogb.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include <math.h>

TEST_F(LlvmLibcILogbTest, SpecialNumbers_ilogb) {
test_special_numbers<double>(&LIBC_NAMESPACE::ilogb);
}

TEST_F(LlvmLibcILogbTest, PowersOfTwo_ilogb) {
test_powers_of_two<double>(&LIBC_NAMESPACE::ilogb);
}

TEST_F(LlvmLibcILogbTest, SomeIntegers_ilogb) {
test_some_integers<double>(&LIBC_NAMESPACE::ilogb);
}

TEST_F(LlvmLibcILogbTest, SubnormalRange_ilogb) {
test_subnormal_range<double>(&LIBC_NAMESPACE::ilogb);
}

TEST_F(LlvmLibcILogbTest, NormalRange_ilogb) {
test_normal_range<double>(&LIBC_NAMESPACE::ilogb);
}
LIST_INTLOGB_TESTS(int, double, LIBC_NAMESPACE::ilogb);
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/ilogbf128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for ilogbf128 -------------------------------------------===//
//
// 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 "src/math/ilogbf128.h"

LIST_INTLOGB_TESTS(int, float128, LIBC_NAMESPACE::ilogbf128);
25 changes: 1 addition & 24 deletions libc/test/src/math/smoke/ilogbf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@

#include "ILogbTest.h"

#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/math/ilogbf.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include <math.h>

TEST_F(LlvmLibcILogbTest, SpecialNumbers_ilogbf) {
test_special_numbers<float>(&LIBC_NAMESPACE::ilogbf);
}

TEST_F(LlvmLibcILogbTest, PowersOfTwo_ilogbf) {
test_powers_of_two<float>(&LIBC_NAMESPACE::ilogbf);
}

TEST_F(LlvmLibcILogbTest, SomeIntegers_ilogbf) {
test_some_integers<float>(&LIBC_NAMESPACE::ilogbf);
}

TEST_F(LlvmLibcILogbTest, SubnormalRange_ilogbf) {
test_subnormal_range<float>(&LIBC_NAMESPACE::ilogbf);
}

TEST_F(LlvmLibcILogbTest, NormalRange_ilogbf) {
test_normal_range<float>(&LIBC_NAMESPACE::ilogbf);
}
LIST_INTLOGB_TESTS(int, float, LIBC_NAMESPACE::ilogbf);
25 changes: 1 addition & 24 deletions libc/test/src/math/smoke/ilogbl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@

#include "ILogbTest.h"

#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/ManipulationFunctions.h"
#include "src/math/ilogbl.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include <math.h>

TEST_F(LlvmLibcILogbTest, SpecialNumbers_ilogbl) {
test_special_numbers<long double>(&LIBC_NAMESPACE::ilogbl);
}

TEST_F(LlvmLibcILogbTest, PowersOfTwo_ilogbl) {
test_powers_of_two<long double>(&LIBC_NAMESPACE::ilogbl);
}

TEST_F(LlvmLibcILogbTest, SomeIntegers_ilogbl) {
test_some_integers<long double>(&LIBC_NAMESPACE::ilogbl);
}

TEST_F(LlvmLibcILogbTest, SubnormalRange_ilogbl) {
test_subnormal_range<long double>(&LIBC_NAMESPACE::ilogbl);
}

TEST_F(LlvmLibcILogbTest, NormalRange_ilogbl) {
test_normal_range<long double>(&LIBC_NAMESPACE::ilogbl);
}
LIST_INTLOGB_TESTS(int, long double, LIBC_NAMESPACE::ilogbl);
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/llogb_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for llogb -----------------------------------------------===//
//
// 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 "src/math/llogb.h"

LIST_INTLOGB_TESTS(long, double, LIBC_NAMESPACE::llogb);
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/llogbf128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for llogbf128 -------------------------------------------===//
//
// 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 "src/math/llogbf128.h"

LIST_INTLOGB_TESTS(long, float128, LIBC_NAMESPACE::llogbf128);
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/llogbf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for llogbf ----------------------------------------------===//
//
// 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 "src/math/llogbf.h"

LIST_INTLOGB_TESTS(long, float, LIBC_NAMESPACE::llogbf);
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/llogbl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for llogbl ----------------------------------------------===//
//
// 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 "src/math/llogbl.h"

LIST_INTLOGB_TESTS(long, long double, LIBC_NAMESPACE::llogbl);
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/logbf128_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for logbf128 --------------------------------------------===//
//
// 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 "LogbTest.h"

#include "src/math/logbf128.h"

LIST_LOGB_TESTS(float128, LIBC_NAMESPACE::logbf128)