| 1 | //===-- Utility class to test bitsfx functions ------------------*- 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "test/UnitTest/Test.h" |
| 10 | |
| 11 | #include "src/__support/fixed_point/fx_rep.h" |
| 12 | |
| 13 | template <typename T, typename XType> |
| 14 | class BitsFxTest : public LIBC_NAMESPACE::testing::Test { |
| 15 | |
| 16 | using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>; |
| 17 | static constexpr T zero = FXRep::ZERO(); |
| 18 | static constexpr T max = FXRep::MAX(); |
| 19 | static constexpr T min = FXRep::MIN(); |
| 20 | static constexpr T one_half = FXRep::ONE_HALF(); |
| 21 | static constexpr T one_fourth = FXRep::ONE_FOURTH(); |
| 22 | static constexpr T eps = FXRep::EPS(); |
| 23 | |
| 24 | static constexpr T zero_point_six_eight_seven_five_t = 0.6875; |
| 25 | |
| 26 | static constexpr T negative_zero_point_six_eight_seven_five_t = -0.6875; |
| 27 | |
| 28 | // an arbitrarily chosen special number |
| 29 | static constexpr T special_num_t = 10.71875; |
| 30 | |
| 31 | static constexpr T negative_special_num_t = -10.71875; |
| 32 | |
| 33 | public: |
| 34 | typedef XType (*BitsFxFunc)(T); |
| 35 | |
| 36 | void testSpecialNumbers(BitsFxFunc func) { |
| 37 | EXPECT_EQ(static_cast<XType>(0), func(zero)); |
| 38 | EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 1)), |
| 39 | func(one_half)); |
| 40 | EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 2)), |
| 41 | func(one_fourth)); |
| 42 | EXPECT_EQ(static_cast<XType>(1), func(eps)); |
| 43 | |
| 44 | // (0.6875)_10 = (0.1011)_2 |
| 45 | EXPECT_EQ(static_cast<XType>(11ULL << (FXRep::FRACTION_LEN - 4)), |
| 46 | func(zero_point_six_eight_seven_five_t)); |
| 47 | |
| 48 | if constexpr (FXRep::SIGN_LEN > 0) |
| 49 | EXPECT_EQ(static_cast<XType>(-(11ULL << (FXRep::FRACTION_LEN - 4))), |
| 50 | func(negative_zero_point_six_eight_seven_five_t)); |
| 51 | |
| 52 | if constexpr (FXRep::INTEGRAL_LEN > 0) { |
| 53 | constexpr size_t kMinFbits = 7; |
| 54 | |
| 55 | if (max >= 11 && FXRep::FRACTION_LEN >= kMinFbits) { |
| 56 | // (10.71875)_10 = (1010.1011100)_2 |
| 57 | constexpr long long kExpected = 1372; |
| 58 | EXPECT_EQ( |
| 59 | static_cast<XType>(kExpected << (FXRep::FRACTION_LEN - kMinFbits)), |
| 60 | func(special_num_t)); |
| 61 | } |
| 62 | |
| 63 | if constexpr (FXRep::SIGN_LEN > 0) { |
| 64 | if (min <= -11 && FXRep::FRACTION_LEN >= kMinFbits) { |
| 65 | // (-10.71875)_10 = (-1010.1011100)_2 |
| 66 | constexpr long long kExpected = -1372; |
| 67 | EXPECT_EQ(static_cast<XType>(kExpected |
| 68 | << (FXRep::FRACTION_LEN - kMinFbits)), |
| 69 | func(negative_special_num_t)); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | #define LIST_BITSFX_TESTS(Name, T, XType, func) \ |
| 77 | using LlvmLibcBits##Name##Test = BitsFxTest<T, XType>; \ |
| 78 | TEST_F(LlvmLibcBits##Name##Test, SpecialNumbers) { \ |
| 79 | testSpecialNumbers(&func); \ |
| 80 | } \ |
| 81 | static_assert(true, "Require semicolon.") |
| 82 | |