brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 18dacad Raw
55 lines · c
1//===-- Utility class to test int to fixed point conversions ----*- 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/CPP/bit.h"12#include "src/__support/fixed_point/fx_bits.h"13 14template <typename T, typename XType>15class FxBitsTest : public LIBC_NAMESPACE::testing::Test {16  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;17  static constexpr T zero = FXRep::ZERO();18  static constexpr T min = FXRep::MIN();19  static constexpr T max = FXRep::MAX();20  static constexpr T half = static_cast<T>(0.5);21  static constexpr T quarter = static_cast<T>(0.25);22  static constexpr T one =23      (FXRep::INTEGRAL_LEN > 0) ? static_cast<T>(1) : FXRep::MAX();24  static constexpr T eps = FXRep::EPS();25  constexpr XType get_one_or_saturated_fraction() {26    if (FXRep::INTEGRAL_LEN > 0) {27      return static_cast<XType>(static_cast<XType>(0x1) << FXRep::FRACTION_LEN);28    } else {29      return static_cast<XType>(30          LIBC_NAMESPACE::mask_trailing_ones<typename FXRep::StorageType,31                                             FXRep::FRACTION_LEN>());32    }33  }34 35public:36  typedef T (*FxBitsFunc)(XType);37 38  void test_special_numbers(FxBitsFunc func) {39    EXPECT_EQ(zero, func(0));40    EXPECT_EQ(eps, func(0x1));41    // x.1000...42    EXPECT_EQ(half, func(static_cast<XType>(0x1) << (FXRep::FRACTION_LEN - 1)));43    // Occupy the bit to the left of the fixed point for Accum types44    // Saturate fraction portion for Fract types45    EXPECT_EQ(one, func(get_one_or_saturated_fraction()));46  }47};48 49#define LIST_FXBITS_TEST(Name, T, XType, func)                                 \50  using LlvmLibc##Name##BitsTest = FxBitsTest<T, XType>;                       \51  TEST_F(LlvmLibc##Name##BitsTest, SpecialNumbers) {                           \52    test_special_numbers(&func);                                               \53  }                                                                            \54  static_assert(true, "Require semicolon.")55