blob: e993eca35e0792856103a8148263ebbce1efa4a1 [file] [log] [blame]
//===-- Utility class to test logb[f|l] -------------------------*- 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
//
//===----------------------------------------------------------------------===//
#include "utils/FPUtil/ManipulationFunctions.h"
#include "utils/FPUtil/TestHelpers.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"
#include <math.h>
namespace mpfr = __llvm_libc::testing::mpfr;
template <typename T> class LogbTest : public __llvm_libc::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
static constexpr UIntType HiddenBit =
UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::value;
public:
typedef T (*LogbFunc)(T);
void testSpecialNumbers(LogbFunc func) {
ASSERT_FP_EQ(aNaN, func(aNaN));
ASSERT_FP_EQ(inf, func(inf));
ASSERT_FP_EQ(inf, func(negInf));
ASSERT_FP_EQ(negInf, func(0.0));
ASSERT_FP_EQ(negInf, func(-0.0));
}
void testPowersOfTwo(LogbFunc func) {
EXPECT_FP_EQ(T(0.0), func(T(1.0)));
EXPECT_FP_EQ(T(0.0), func(T(-1.0)));
EXPECT_FP_EQ(T(1.0), func(T(2.0)));
EXPECT_FP_EQ(T(1.0), func(T(-2.0)));
EXPECT_FP_EQ(T(2.0), func(T(4.0)));
EXPECT_FP_EQ(T(2.0), func(T(-4.0)));
EXPECT_FP_EQ(T(3.0), func(T(8.0)));
EXPECT_FP_EQ(T(3.0), func(T(-8.0)));
EXPECT_FP_EQ(T(4.0), func(T(16.0)));
EXPECT_FP_EQ(T(4.0), func(T(-16.0)));
EXPECT_FP_EQ(T(5.0), func(T(32.0)));
EXPECT_FP_EQ(T(5.0), func(T(-32.0)));
}
void testSomeIntegers(LogbFunc func) {
EXPECT_FP_EQ(T(1.0), func(T(3.0)));
EXPECT_FP_EQ(T(1.0), func(T(-3.0)));
EXPECT_FP_EQ(T(2.0), func(T(7.0)));
EXPECT_FP_EQ(T(2.0), func(T(-7.0)));
EXPECT_FP_EQ(T(3.0), func(T(10.0)));
EXPECT_FP_EQ(T(3.0), func(T(-10.0)));
EXPECT_FP_EQ(T(4.0), func(T(31.0)));
EXPECT_FP_EQ(T(4.0), func(T(-31.0)));
EXPECT_FP_EQ(T(5.0), func(T(55.0)));
EXPECT_FP_EQ(T(5.0), func(T(-55.0)));
}
void testRange(LogbFunc func) {
using UIntType = typename FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
T x = static_cast<T>(FPBits(v));
if (isnan(x) || isinf(x) || x == 0.0l)
continue;
int exponent;
__llvm_libc::fputil::frexp(x, exponent);
ASSERT_FP_EQ(T(exponent), func(x) + T(1.0));
}
}
};
#define LIST_LOGB_TESTS(T, func) \
using LlvmLibcLogbTest = LogbTest<T>; \
TEST_F(LlvmLibcLogbTest, SpecialNumbers) { testSpecialNumbers(&func); } \
TEST_F(LlvmLibcLogbTest, PowersOfTwo) { testPowersOfTwo(&func); } \
TEST_F(LlvmLibcLogbTest, SomeIntegers) { testSomeIntegers(&func); } \
TEST_F(LlvmLibcLogbTest, InRange) { testRange(&func); }