-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathtest_bit.cpp
More file actions
229 lines (193 loc) · 7.95 KB
/
test_bit.cpp
File metadata and controls
229 lines (193 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "xsimd/xsimd.hpp"
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE
#include "test_utils.hpp"
template <class T>
struct bit_test
{
using value_type = T;
using bits = std::integral_constant<int, sizeof(T) * CHAR_BIT>;
void test_popcount()
{
// Zero
CHECK_EQ(xsimd::detail::popcount(T(0)), 0);
// All bits set
CHECK_EQ(xsimd::detail::popcount(T(~T(0))), bits::value);
// Single bit patterns - all should have popcount of 1
for (int i = 0; i < bits::value; ++i)
{
T value = T(T(1) << i);
INFO("popcount(1 << " << i << ")");
CHECK_EQ(xsimd::detail::popcount(value), 1);
}
// Powers of 2 minus 1 - known popcounts
for (int i = 1; i < bits::value; ++i)
{
T value = T((T(1) << i) - 1);
INFO("popcount((1 << " << i << ") - 1)");
CHECK_EQ(xsimd::detail::popcount(value), i);
}
// Alternating patterns
if (bits::value >= 8)
{
T pattern_aa = T(0);
T pattern_55 = T(0);
for (int i = 0; i < bits::value / 8; ++i)
{
pattern_aa |= T(0xAA) << (i * 8);
pattern_55 |= T(0x55) << (i * 8);
}
INFO("popcount(0xAA...)");
CHECK_EQ(xsimd::detail::popcount(pattern_aa), bits::value / 2);
INFO("popcount(0x55...)");
CHECK_EQ(xsimd::detail::popcount(pattern_55), bits::value / 2);
}
// Specific test cases
CHECK_EQ(xsimd::detail::popcount(T(1)), 1);
CHECK_EQ(xsimd::detail::popcount(T(3)), 2);
CHECK_EQ(xsimd::detail::popcount(T(7)), 3);
CHECK_EQ(xsimd::detail::popcount(T(15)), 4);
}
void test_countl_zero()
{
// Zero should have all leading zeros
CHECK_EQ(xsimd::detail::countl_zero(T(0)), bits::value);
// All bits set should have 0 leading zeros
CHECK_EQ(xsimd::detail::countl_zero(T(~T(0))), 0);
// MSB set should have 0 leading zeros
T msb = T(1) << (bits::value - 1);
CHECK_EQ(xsimd::detail::countl_zero(msb), 0);
// Powers of 2
for (int i = 0; i < bits::value; ++i)
{
T value = T(T(1) << i);
int expected = bits::value - i - 1;
INFO("countl_zero(1 << " << i << ")");
CHECK_EQ(xsimd::detail::countl_zero(value), expected);
}
// Sequential patterns (1, 3, 7, 15, ...)
for (int i = 1; i < bits::value; ++i)
{
T value = T((T(1) << i) - 1);
int expected = bits::value - i;
INFO("countl_zero((1 << " << i << ") - 1)");
CHECK_EQ(xsimd::detail::countl_zero(value), expected);
}
// Specific values
CHECK_EQ(xsimd::detail::countl_zero(T(1)), bits::value - 1);
CHECK_EQ(xsimd::detail::countl_zero(T(2)), bits::value - 2);
CHECK_EQ(xsimd::detail::countl_zero(T(4)), bits::value - 3);
}
void test_countl_one()
{
// Zero should have 0 leading ones
CHECK_EQ(xsimd::detail::countl_one(T(0)), 0);
// All bits set should have all leading ones
CHECK_EQ(xsimd::detail::countl_one(T(~T(0))), bits::value);
// MSB clear, rest set should have 0 leading ones
T pattern = T(~(T(1) << (bits::value - 1)));
CHECK_EQ(xsimd::detail::countl_one(pattern), 0);
// Inverted powers of 2
for (int i = 0; i < bits::value; ++i)
{
T value = T(~(T(1) << i));
int expected = (i == bits::value - 1) ? 0 : bits::value - i - 1;
INFO("countl_one(~(1 << " << i << "))");
CHECK_EQ(xsimd::detail::countl_one(value), expected);
}
// Patterns with known leading ones
for (int i = 1; i <= bits::value; ++i)
{
T value = T(T(~T(0)) << (bits::value - i));
INFO("countl_one(~0 << " << (bits::value - i) << ")");
CHECK_EQ(xsimd::detail::countl_one(value), i);
}
// Specific values
CHECK_EQ(xsimd::detail::countl_one(T(~T(1))), bits::value - 1);
CHECK_EQ(xsimd::detail::countl_one(T(~T(3))), bits::value - 2);
}
void test_countr_zero()
{
// Zero should have all trailing zeros
CHECK_EQ(xsimd::detail::countr_zero(T(0)), bits::value);
// All bits set should have 0 trailing zeros
CHECK_EQ(xsimd::detail::countr_zero(T(~T(0))), 0);
// Odd numbers should have 0 trailing zeros
CHECK_EQ(xsimd::detail::countr_zero(T(1)), 0);
CHECK_EQ(xsimd::detail::countr_zero(T(3)), 0);
CHECK_EQ(xsimd::detail::countr_zero(T(5)), 0);
CHECK_EQ(xsimd::detail::countr_zero(T(7)), 0);
// Powers of 2
for (int i = 0; i < bits::value; ++i)
{
T value = T(1) << i;
INFO("countr_zero(1 << " << i << ")");
CHECK_EQ(xsimd::detail::countr_zero(value), i);
}
// Even numbers with known factors
CHECK_EQ(xsimd::detail::countr_zero(T(2)), 1);
CHECK_EQ(xsimd::detail::countr_zero(T(4)), 2);
CHECK_EQ(xsimd::detail::countr_zero(T(6)), 1);
CHECK_EQ(xsimd::detail::countr_zero(T(8)), 3);
CHECK_EQ(xsimd::detail::countr_zero(T(12)), 2);
CHECK_EQ(xsimd::detail::countr_zero(T(16)), 4);
// Specific patterns
for (int i = 1; i < bits::value; ++i)
{
T value = T(~T(0)) << i;
INFO("countr_zero(~0 << " << i << ")");
CHECK_EQ(xsimd::detail::countr_zero(value), i);
}
}
void test_countr_one()
{
// Zero should have 0 trailing ones
CHECK_EQ(xsimd::detail::countr_one(T(0)), 0);
// All bits set should have all trailing ones
CHECK_EQ(xsimd::detail::countr_one(T(~T(0))), bits::value);
// Even numbers should have 0 trailing ones
CHECK_EQ(xsimd::detail::countr_one(T(2)), 0);
CHECK_EQ(xsimd::detail::countr_one(T(4)), 0);
CHECK_EQ(xsimd::detail::countr_one(T(6)), 0);
// Powers of 2 minus 1
for (int i = 1; i < bits::value; ++i)
{
T value = T((T(1) << i) - 1);
INFO("countr_one((1 << " << i << ") - 1)");
CHECK_EQ(xsimd::detail::countr_one(value), i);
}
// Specific values
CHECK_EQ(xsimd::detail::countr_one(T(1)), 1);
CHECK_EQ(xsimd::detail::countr_one(T(3)), 2);
CHECK_EQ(xsimd::detail::countr_one(T(7)), 3);
CHECK_EQ(xsimd::detail::countr_one(T(15)), 4);
CHECK_EQ(xsimd::detail::countr_one(T(31)), 5);
// Inverted powers of 2 minus 1
for (int i = 1; i < bits::value; ++i)
{
T value = T(~((T(1) << i) - 1));
INFO("countr_one(~((1 << " << i << ") - 1))");
CHECK_EQ(xsimd::detail::countr_one(value), 0);
}
}
};
TEST_CASE_TEMPLATE("[bit operations]", T,
uint8_t, uint16_t, uint32_t, uint64_t)
{
bit_test<T> Test;
SUBCASE("popcount") { Test.test_popcount(); }
SUBCASE("countl_zero") { Test.test_countl_zero(); }
SUBCASE("countl_one") { Test.test_countl_one(); }
SUBCASE("countr_zero") { Test.test_countr_zero(); }
SUBCASE("countr_one") { Test.test_countr_one(); }
}
#endif