Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
get_msb.test.cpp
Go to the documentation of this file.
1
#include "
get_msb.hpp
"
2
#include <gtest/gtest.h>
3
4
using namespace
bb
;
5
6
TEST
(bitop, GetMsbUint640Value)
7
{
8
uint64_t
a
= 0b00000000000000000000000000000000;
9
EXPECT_EQ(
numeric::get_msb
(
a
), 0U);
10
}
11
12
TEST
(bitop, GetMsbUint320)
13
{
14
uint32_t
a
= 0b00000000000000000000000000000001;
15
EXPECT_EQ(
numeric::get_msb
(
a
), 0U);
16
}
17
18
TEST
(bitop, GetMsbUint3231)
19
{
20
uint32_t
a
= 0b10000000000000000000000000000001;
21
EXPECT_EQ(
numeric::get_msb
(
a
), 31U);
22
}
23
24
TEST
(bitop, GetMsbUint6463)
25
{
26
uint64_t
a
= 0b1000000000000000000000000000000100000000000000000000000000000000;
27
EXPECT_EQ(
numeric::get_msb
(
a
), 63U);
28
}
29
30
TEST
(bitop, GetMsbSizeT7)
31
{
32
size_t
a
= 0x80;
33
auto
r =
numeric::get_msb
(
a
);
34
EXPECT_EQ(r, 7U);
35
}
36
37
// Verify De Bruijn lookup tables by testing every bit position with multiple input patterns
38
TEST
(bitop, GetMsbUint32AllPositions)
39
{
40
for
(uint32_t i = 0; i < 32; i++) {
41
// Power of 2: exactly one bit set
42
EXPECT_EQ(
numeric::get_msb
(uint32_t(1U << i)), i);
43
// All bits set up to position i (exercises the post-smearing pattern)
44
uint32_t all_ones = (i == 31) ? 0xFFFFFFFF : ((1U << (i + 1)) - 1);
45
EXPECT_EQ(
numeric::get_msb
(all_ones), i);
46
// MSB set plus random low bit
47
if
(i > 0) {
48
EXPECT_EQ(
numeric::get_msb
(uint32_t((1U << i) | 1U)), i);
49
}
50
}
51
}
52
53
TEST
(bitop, GetMsbUint64AllPositions)
54
{
55
for
(uint64_t i = 0; i < 64; i++) {
56
// Power of 2
57
EXPECT_EQ(
numeric::get_msb
(uint64_t(1ULL << i)), i);
58
// All bits set up to position i
59
uint64_t all_ones = (i == 63) ? 0xFFFFFFFFFFFFFFFFULL : ((1ULL << (i + 1)) - 1);
60
EXPECT_EQ(
numeric::get_msb
(all_ones), i);
61
// MSB set plus low bit
62
if
(i > 0) {
63
EXPECT_EQ(
numeric::get_msb
(uint64_t((1ULL << i) | 1ULL)), i);
64
}
65
}
66
}
67
68
// get_lsb tests
69
TEST
(bitop, GetLsbZero)
70
{
71
EXPECT_EQ(
numeric::get_lsb
(uint32_t(0)), 0U);
72
EXPECT_EQ(
numeric::get_lsb
(uint64_t(0)), 0U);
73
}
74
75
TEST
(bitop, GetLsbOne)
76
{
77
EXPECT_EQ(
numeric::get_lsb
(uint32_t(1)), 0U);
78
EXPECT_EQ(
numeric::get_lsb
(uint64_t(1)), 0U);
79
}
80
81
TEST
(bitop, GetLsbPowersOfTwo)
82
{
83
for
(uint32_t i = 0; i < 32; i++) {
84
EXPECT_EQ(
numeric::get_lsb
(uint32_t(1U << i)), i);
85
}
86
for
(uint64_t i = 0; i < 64; i++) {
87
EXPECT_EQ(
numeric::get_lsb
(uint64_t(1ULL << i)), i);
88
}
89
}
90
91
TEST
(bitop, GetLsbComposite)
92
{
93
// LSB of 0b1100 is bit 2
94
EXPECT_EQ(
numeric::get_lsb
(uint32_t(0b1100)), 2U);
95
// LSB of 0xFF00 is bit 8
96
EXPECT_EQ(
numeric::get_lsb
(uint64_t(0xFF00)), 8U);
97
}
98
99
// round_up_power_2 tests
100
TEST
(bitop, RoundUpPower2Zero)
101
{
102
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t(0)), 0U);
103
EXPECT_EQ(
numeric::round_up_power_2
(uint64_t(0)), 0ULL);
104
}
105
106
TEST
(bitop, RoundUpPower2PowersOfTwo)
107
{
108
// Powers of two should be returned unchanged
109
for
(uint32_t i = 0; i < 31; i++) {
110
uint32_t val = 1U << i;
111
EXPECT_EQ(
numeric::round_up_power_2
(val), val);
112
}
113
}
114
115
TEST
(bitop, RoundUpPower2NonPowers)
116
{
117
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t(3)), 4U);
118
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t(5)), 8U);
119
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t(7)), 8U);
120
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t(9)), 16U);
121
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t(100)), 128U);
122
EXPECT_EQ(
numeric::round_up_power_2
(uint64_t(1000)), 1024ULL);
123
}
124
125
TEST
(bitop, RoundUpPower2LargestValid)
126
{
127
// Largest non-power-of-2 that doesn't overflow: 2^30 + 1 -> 2^31
128
EXPECT_EQ(
numeric::round_up_power_2
(uint32_t((1U << 30) + 1)), 1U << 31);
129
}
a
FF a
Definition
field_gt.test.cpp:52
get_msb.hpp
bb::numeric::get_msb
constexpr T get_msb(const T in)
Definition
get_msb.hpp:49
bb::numeric::round_up_power_2
constexpr T round_up_power_2(const T in)
Definition
get_msb.hpp:75
bb::numeric::get_lsb
constexpr T get_lsb(const T in)
Definition
get_msb.hpp:70
bb
Entry point for Barretenberg command-line interface.
Definition
api.hpp:5
bb::TEST
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
Definition
graph_description_megacircuitbuilder.test.cpp:22
src
barretenberg
numeric
bitop
get_msb.test.cpp
Generated by
1.9.8