Skip to content

Commit f7beac1

Browse files
committed
add: tests for some signed bitsfx
Signed-off-by: krishna2803 <[email protected]>
1 parent f6ff5d5 commit f7beac1

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

libc/test/src/stdfix/BitsFxTest.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 From, typename To>
14+
class BitsFxTest : public LIBC_NAMESPACE::testing::Test {
15+
16+
using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<From>;
17+
static constexpr From zero = FXRep::ZERO();
18+
static constexpr From max = FXRep::MAX();
19+
static constexpr From min = FXRep::MIN();
20+
static constexpr From one_half = FXRep::ONE_HALF();
21+
static constexpr From one_fourth = FXRep::ONE_FOURTH();
22+
static constexpr From eps = FXRep::EPS();
23+
// (0.42)_10 =
24+
// (0.0110101110000101000111101011100001010001111010111000010100011110)_2 =
25+
// (0.0x6b851eb851eb851e)_16
26+
static constexpr unsigned long long zero_point_forty_two =
27+
0x6b851eb851eb851eull;
28+
29+
static constexpr unsigned long long maxval = ~(1ULL << FXRep::VALUE_LEN);
30+
static constexpr unsigned long long minval = -(maxval + 1ULL);
31+
32+
public:
33+
typedef To (*BitsFxFunc)(From);
34+
35+
void testSpecialNumbers(BitsFxFunc func) {
36+
EXPECT_EQ(static_cast<To>(0), func(zero));
37+
EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 1)), func(one_half));
38+
EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 2)),
39+
func(one_fourth));
40+
EXPECT_EQ(static_cast<To>(1), func(eps));
41+
EXPECT_EQ(static_cast<To>(maxval), func(max));
42+
EXPECT_EQ(static_cast<To>(minval), func(min));
43+
44+
// (0.6875)_10 = (0.1011)_2
45+
EXPECT_EQ(static_cast<To>(11 << (FXRep::FRACTION_LEN - 4)), func(0.6875));
46+
47+
EXPECT_EQ(
48+
static_cast<To>(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)),
49+
func(0.42));
50+
51+
// EXPECT_EQ(static_cast<To>(0), func(5));
52+
53+
// if constexpr (static_cast<int>(min) <= -16)
54+
// EXPECT_EQ(static_cast<To>(0), func(-16));
55+
56+
// if constexpr (static_cast<int>(min) <= -10)
57+
// EXPECT_EQ(static_cast<To>(0), func(-10));
58+
59+
// EXPECT_EQ(static_cast<To>(), func(max));
60+
61+
// if constexpr (static_cast<int>(max) >= 16.25)
62+
// EXPECT_EQ(static_cast<To>(0), func(16.25));
63+
64+
// if constexpr (static_cast<int>(max) > -10)
65+
// EXPECT_EQ(static_cast<To>(0), func(16));
66+
}
67+
};
68+
69+
#define LIST_BITSFX_TESTS(From, To, func) \
70+
using LlvmLibcBitsFxTest = BitsFxTest<From, To>; \
71+
TEST_F(LlvmLibcBitsFxTest, SpecialNumbers) { testSpecialNumbers(&func); } \
72+
static_assert(true, "Require semicolon.")

libc/test/src/stdfix/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
8989
)
9090
endforeach()
9191

92+
foreach(suffix IN ITEMS hk k lk r lr)
93+
add_libc_test(
94+
bits${suffix}_test
95+
SUITE
96+
libc-stdfix-tests
97+
HDRS
98+
BitsFxTest.h
99+
SRCS
100+
bits${suffix}_test.cpp
101+
DEPENDS
102+
libc.src.stdfix.bits${suffix}
103+
libc.src.__support.fixed_point.fx_rep
104+
libc.src.__support.fixed_point.fx_bits
105+
)
106+
endforeach()
107+
92108
add_libc_test(
93109
uhksqrtus_test
94110
SUITE
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitshk ----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // int_hk_t
12+
#include "src/stdfix/bitshk.h"
13+
14+
LIST_BITSFX_TESTS(short accum, int_hk_t, LIBC_NAMESPACE::bitshk);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitsk -----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // int_k_t
12+
#include "src/stdfix/bitsk.h"
13+
14+
LIST_BITSFX_TESTS(accum, int_k_t, LIBC_NAMESPACE::bitsk);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitslk ----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // int_lk_t
12+
#include "src/stdfix/bitslk.h"
13+
14+
LIST_BITSFX_TESTS(long accum, int_lk_t, LIBC_NAMESPACE::bitslk);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitslr ----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // int_lr_t
12+
#include "src/stdfix/bitslr.h"
13+
14+
LIST_BITSFX_TESTS(long fract, int_lr_t, LIBC_NAMESPACE::bitslr);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===-- Unittests for bitsr -----------------------------------------------===//
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 "BitsFxTest.h"
10+
11+
#include "llvm-libc-types/stdfix-types.h" // int_r_t
12+
#include "src/stdfix/bitsr.h"
13+
14+
LIST_BITSFX_TESTS(fract, int_r_t, LIBC_NAMESPACE::bitsr);

0 commit comments

Comments
 (0)