brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.8 KiB · 605698e Raw
129 lines · c
1//===-- Utility class to test different flavors of ilogb --------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H10#define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H11 12#include "src/__support/CPP/algorithm.h"13#include "src/__support/FPUtil/FPBits.h"14#include "src/__support/FPUtil/ManipulationFunctions.h"15#include "test/UnitTest/FEnvSafeTest.h"16#include "test/UnitTest/Test.h"17 18using LIBC_NAMESPACE::Sign;19 20template <typename OutType, typename InType>21class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {22  using FPBits = LIBC_NAMESPACE::fputil::FPBits<InType>;23  using StorageType = typename FPBits::StorageType;24 25public:26  typedef OutType (*Func)(InType);27 28  void test_special_numbers(Func func) {29    EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::FP_LOGB0,30              func(FPBits::zero(Sign::POS).get_val()));31    EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::FP_LOGB0,32              func(FPBits::zero(Sign::NEG).get_val()));33    EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::FP_LOGBNAN,34              func(FPBits::quiet_nan().get_val()));35    EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::T_MAX,36              func(FPBits::inf(Sign::POS).get_val()));37    EXPECT_EQ(LIBC_NAMESPACE::fputil::IntLogbConstants<OutType>::T_MAX,38              func(FPBits::inf(Sign::NEG).get_val()));39  }40 41  void test_powers_of_two(Func func) {42    EXPECT_EQ(OutType(0), func(InType(1.0)));43    EXPECT_EQ(OutType(0), func(InType(-1.0)));44 45    EXPECT_EQ(OutType(1), func(InType(2.0)));46    EXPECT_EQ(OutType(1), func(InType(-2.0)));47 48    EXPECT_EQ(OutType(2), func(InType(4.0)));49    EXPECT_EQ(OutType(2), func(InType(-4.0)));50 51    EXPECT_EQ(OutType(3), func(InType(8.0)));52    EXPECT_EQ(OutType(3), func(InType(-8.0)));53 54    EXPECT_EQ(OutType(4), func(InType(16.0)));55    EXPECT_EQ(OutType(4), func(InType(-16.0)));56 57    EXPECT_EQ(OutType(5), func(InType(32.0)));58    EXPECT_EQ(OutType(5), func(InType(-32.0)));59  }60 61  void test_some_integers(Func func) {62    EXPECT_EQ(OutType(1), func(InType(3.0)));63    EXPECT_EQ(OutType(1), func(InType(-3.0)));64 65    EXPECT_EQ(OutType(2), func(InType(7.0)));66    EXPECT_EQ(OutType(2), func(InType(-7.0)));67 68    EXPECT_EQ(OutType(3), func(InType(10.0)));69    EXPECT_EQ(OutType(3), func(InType(-10.0)));70 71    EXPECT_EQ(OutType(4), func(InType(31.0)));72    EXPECT_EQ(OutType(4), func(InType(-31.0)));73 74    EXPECT_EQ(OutType(5), func(InType(55.0)));75    EXPECT_EQ(OutType(5), func(InType(-55.0)));76  }77 78  void test_subnormal_range(Func func) {79    constexpr StorageType MIN_SUBNORMAL = FPBits::min_subnormal().uintval();80    constexpr StorageType MAX_SUBNORMAL = FPBits::max_subnormal().uintval();81    constexpr int COUNT = 10'001;82    constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(83        static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),84        StorageType(1));85    for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) {86      FPBits x_bits(v);87      if (x_bits.is_zero() || x_bits.is_inf_or_nan())88        continue;89 90      InType x = x_bits.get_val();91 92      int exponent;93      LIBC_NAMESPACE::fputil::frexp(x, exponent);94      ASSERT_EQ(static_cast<OutType>(exponent), func(x) + OutType(1));95    }96  }97 98  void test_normal_range(Func func) {99    constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();100    constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();101    constexpr int COUNT = 10'001;102    constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(103        static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),104        StorageType(1));105    for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) {106      FPBits x_bits(v);107      if (x_bits.is_zero() || x_bits.is_inf_or_nan())108        continue;109 110      InType x = x_bits.get_val();111 112      int exponent;113      LIBC_NAMESPACE::fputil::frexp(x, exponent);114      ASSERT_EQ(static_cast<OutType>(exponent), func(x) + OutType(1));115    }116  }117};118 119#define LIST_INTLOGB_TESTS(OutType, InType, Func)                              \120  using LlvmLibcIntLogbTest = LlvmLibcILogbTest<OutType, InType>;              \121  TEST_F(LlvmLibcIntLogbTest, SpecialNumbers) { test_special_numbers(&Func); } \122  TEST_F(LlvmLibcIntLogbTest, PowersOfTwo) { test_powers_of_two(&Func); }      \123  TEST_F(LlvmLibcIntLogbTest, SomeIntegers) { test_some_integers(&Func); }     \124  TEST_F(LlvmLibcIntLogbTest, SubnormalRange) { test_subnormal_range(&Func); } \125  TEST_F(LlvmLibcIntLogbTest, NormalRange) { test_normal_range(&Func); }       \126  static_assert(true)127 128#endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H129