1 //===-- Utility class to test countls -------------------*- 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> class CountlsTest : public LIBC_NAMESPACE::testing::Test { 14 15 using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>; 16 static constexpr T zero = FXRep::ZERO(); 17 static constexpr T max = FXRep::MAX(); 18 static constexpr T min = FXRep::MIN(); 19 static constexpr T one_half = FXRep::ONE_HALF(); 20 static constexpr T one_fourth = FXRep::ONE_FOURTH(); 21 static constexpr T eps = FXRep::EPS(); 22 23 public: 24 typedef int (*CountlsFunc)(T); 25 testSpecialNumbers(CountlsFunc func)26 void testSpecialNumbers(CountlsFunc func) { 27 constexpr bool is_signed = (FXRep::SIGN_LEN > 0); 28 29 EXPECT_EQ(FXRep::INTEGRAL_LEN, func(one_half)); 30 EXPECT_EQ(FXRep::INTEGRAL_LEN + 1, func(one_fourth)); 31 EXPECT_EQ(FXRep::VALUE_LEN, func(zero)); 32 EXPECT_EQ(FXRep::VALUE_LEN - 1, func(eps)); 33 EXPECT_EQ(0, func(max)); 34 // If signed, left shifting the minimum value will overflow, so countls = 0. 35 // If unsigned, the minimum value is zero, so countls is the number of value 36 // bits according to ISO/IEC TR 18037. 37 EXPECT_EQ(is_signed ? 0 : FXRep::VALUE_LEN, func(min)); 38 39 if (10 <= static_cast<int>(max)) 40 EXPECT_EQ(FXRep::INTEGRAL_LEN - 4, func(10)); 41 42 if (static_cast<int>(min) <= -10) 43 EXPECT_EQ(FXRep::INTEGRAL_LEN - 4, func(-10)); 44 45 if constexpr (is_signed) { 46 EXPECT_EQ(FXRep::VALUE_LEN, func(-zero)); 47 EXPECT_EQ(FXRep::VALUE_LEN, func(-eps)); 48 EXPECT_EQ(FXRep::INTEGRAL_LEN + 1, func(-one_half)); 49 if (FXRep::FRACTION_LEN >= 2) 50 EXPECT_EQ(FXRep::INTEGRAL_LEN + 2, func(-one_fourth)); 51 } 52 } 53 }; 54 55 #define LIST_COUNTLS_TESTS(T, func) \ 56 using LlvmLibcCountlsTest = CountlsTest<T>; \ 57 TEST_F(LlvmLibcCountlsTest, SpecialNumbers) { testSpecialNumbers(&func); } \ 58 static_assert(true, "Require semicolon.") 59