brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · cdb363f Raw
84 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "test/UnitTest/Test.h"10 11#include "src/__support/fixed_point/fx_rep.h"12 13template <typename T, typename XType>14class 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 number29  static constexpr T special_num_t = 10.71875;30 31  static constexpr T negative_special_num_t = -10.71875;32 33public: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)_245    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)_257        constexpr int64_t 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)_266          constexpr int64_t kExpected =67              static_cast<int64_t>(static_cast<uint64_t>(-1372)68                                   << (FXRep::FRACTION_LEN - kMinFbits));69 70          EXPECT_EQ(static_cast<XType>(kExpected),71                    func(negative_special_num_t));72        }73      }74    }75  }76};77 78#define LIST_BITSFX_TESTS(Name, T, XType, func)                                \79  using LlvmLibcBits##Name##Test = BitsFxTest<T, XType>;                       \80  TEST_F(LlvmLibcBits##Name##Test, SpecialNumbers) {                           \81    testSpecialNumbers(&func);                                                 \82  }                                                                            \83  static_assert(true, "Require semicolon.")84