-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrank1.cpp
More file actions
148 lines (120 loc) · 3.13 KB
/
Copy pathrank1.cpp
File metadata and controls
148 lines (120 loc) · 3.13 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenQMC Project.
#include "hypothesis.h"
#include <oqmc/float.h>
#include <oqmc/pcg.h>
#include <oqmc/rank1.h>
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
namespace
{
struct SamplerV1
{
void initialise(int seed)
{
hash0 = oqmc::pcg::hash(seed * 2 + 0);
hash1 = oqmc::pcg::hash(seed * 2 + 1);
}
void sample(int index, std::uint32_t out[2]) const
{
std::uint32_t sample[2];
oqmc::shuffledRotatedLattice<1>(index, hash0, &sample[0]);
oqmc::shuffledRotatedLattice<1>(index, hash1, &sample[1]);
out[0] = sample[0];
out[1] = sample[1];
}
std::uint32_t hash0;
std::uint32_t hash1;
};
template <int X, int Y>
struct SamplerV2
{
void initialise(int seed)
{
hash = oqmc::pcg::hash(seed);
}
void sample(int index, std::uint32_t out[2]) const
{
std::uint32_t rnd[4];
oqmc::shuffledRotatedLattice<4>(index, hash, rnd);
out[0] = rnd[X];
out[1] = rnd[Y];
}
std::uint32_t hash;
};
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleIndpendent, (SamplerV1()))
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleDims01, (SamplerV2<0, 1>()))
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleDims02, (SamplerV2<0, 2>()))
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleDims03, (SamplerV2<0, 3>()))
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleDims12, (SamplerV2<1, 2>()))
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleDims13, (SamplerV2<1, 3>()))
ALL_HYPOTHESIS_TESTS(Rank1Test, SampleDims23, (SamplerV2<2, 3>()))
constexpr std::array<int, 20> primes{
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
};
constexpr auto oneEighthInt = 1 << 29;
constexpr auto oneEighthFloat = 1.f / 8;
TEST(Rank1Test, Rotate)
{
for(const auto prime : primes)
{
const auto stepUint32 = static_cast<std::uint32_t>(oneEighthInt);
const auto stepFloat = oneEighthFloat;
const auto multUint32 = stepUint32 * prime;
const auto multFloat = stepFloat * prime;
const auto valueUint32 = oqmc::uintToFloat(multUint32);
const auto valueFloat = multFloat - static_cast<int>(multFloat);
EXPECT_FLOAT_EQ(valueUint32, valueFloat);
}
}
TEST(Rank1Test, LatticeReversedIndexIndices)
{
std::uint32_t last[] = {
oqmc::latticeReversedIndex(0, 0),
oqmc::latticeReversedIndex(0, 1),
oqmc::latticeReversedIndex(0, 2),
oqmc::latticeReversedIndex(0, 3),
};
for(const auto prime : primes)
{
std::uint32_t next[] = {
oqmc::latticeReversedIndex(prime, 0),
oqmc::latticeReversedIndex(prime, 1),
oqmc::latticeReversedIndex(prime, 2),
oqmc::latticeReversedIndex(prime, 3),
};
for(int i = 0; i < 4; ++i)
{
EXPECT_NE(last[i], next[i]);
}
for(int i = 0; i < 4; ++i)
{
last[i] = next[i];
}
}
}
TEST(Rank1Test, LatticeReversedIndexDimensions)
{
for(const auto prime : primes)
{
std::uint32_t value[] = {
oqmc::latticeReversedIndex(prime, 0),
oqmc::latticeReversedIndex(prime, 1),
oqmc::latticeReversedIndex(prime, 2),
oqmc::latticeReversedIndex(prime, 3),
};
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
{
if(i == j)
{
continue;
}
EXPECT_NE(value[i], value[j]);
}
}
}
}
} // namespace