Repository navigation
Expand file tree
/
Copy pathmatrices.cpp
More file actions
293 lines (248 loc) · 7.65 KB
/
Copy pathmatrices.cpp
File metadata and controls
293 lines (248 loc) · 7.65 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the OpenQMC Project.
// This file is used to compile a cli tool that will construct and print the
// SZ sequence generator matrices in an optimal format for the main library.
// Resulting programs are inlined into a header file include/oqmc/owen.h, and
// the matrices into the reference test in src/tests/owen.cpp.
//
// Matrices are constructed following the work by Abdalla G. M. Ahmed, Matt
// Pharr, Victor Ostromoukhov and Hui Huang in 'SZ Sequences:
// Binary-Constructed (0, 2^q)-Sequences'.
#include <oqmc/reverse.h>
#include <array>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <utility>
#include <vector>
constexpr auto numDimensions = 4;
constexpr auto size = 16;
// GF(4) elements as 2x2 GF(2) matrices, packed as [a b; c d].
struct Block
{
int a, b, c, d;
};
constexpr Block multiply(Block x, Block y)
{
return {(x.a & y.a) ^ (x.b & y.c), (x.a & y.b) ^ (x.b & y.d),
(x.c & y.a) ^ (x.d & y.c), (x.c & y.b) ^ (x.d & y.d)};
}
constexpr bool equal(Block x, Block y)
{
return x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d;
}
// A generator of the multiplicative group of GF(4), an invertible block whose
// cube is the identity but whose square is not. Two of the six invertible
// blocks qualify, either will do, so the scan takes the first.
constexpr Block findAlpha()
{
const Block zero = {0, 0, 0, 0};
const Block identity = {1, 0, 0, 1};
for(int bits = 0; bits < 16; ++bits)
{
const Block candidate = {(bits >> 3) & 1, (bits >> 2) & 1,
(bits >> 1) & 1, bits & 1};
if(equal(candidate, zero) || equal(candidate, identity))
{
continue;
}
const Block squared = multiply(candidate, candidate);
const Block cubed = multiply(squared, candidate);
if(equal(cubed, identity) && !equal(squared, identity))
{
return candidate;
}
}
assert(false);
return identity;
}
// The generator matrix of a symbol is the base-4 Pascal matrix of the GF(4)
// alphabet (Ahmed et al. 2025, eq. 24): the block at 2x2 block position
// (i, j) is symbol^(j-i) where C(j, i) is odd (Lucas: (~j & i) == 0), and
// empty otherwise. Each block is then pre-multiplied by pascal below, which
// nests a 2D Sobol sequence in the set (section 4.3): the identity symbol
// then reproduces the classic Sobol Pascal matrix, so dimensions 0 and 1 are
// unchanged. The factor is block diagonal, so the base-4 properties are
// untouched. Columns are stored as 32 bit words with the most significant bit
// as the first row.
constexpr std::array<std::uint32_t, size> buildMatrix(Block symbol)
{
constexpr auto numBlocks = size / 2;
constexpr Block pascal = {1, 1, 0, 1};
std::array<Block, numBlocks> powers = {};
powers[0] = {1, 0, 0, 1};
for(int exponent = 1; exponent < numBlocks; ++exponent)
{
powers[exponent] = multiply(powers[exponent - 1], symbol);
}
std::array<std::uint32_t, size> matrix = {};
for(int j = 0; j < numBlocks; ++j)
{
for(int i = 0; i <= j; ++i)
{
if((~j & i) != 0)
{
continue;
}
const Block block = multiply(pascal, powers[j - i]);
const std::array<std::array<int, 2>, 2> entries = {
{{block.a, block.b}, {block.c, block.d}}};
for(int row = 0; row < 2; ++row)
{
for(int column = 0; column < 2; ++column)
{
if(entries[row][column] != 0)
{
matrix[2 * j + column] |= 1u << (31 - (2 * i + row));
}
}
}
}
}
return matrix;
}
// Product of two generator matrices, column by column.
constexpr std::array<std::uint32_t, size>
multiplyMatrix(const std::array<std::uint32_t, size>& lhs,
const std::array<std::uint32_t, size>& rhs)
{
std::array<std::uint32_t, size> matrix = {};
for(int column = 0; column < size; ++column)
{
for(int row = 0; row < size; ++row)
{
if((rhs[column] >> (31 - row) & 1) != 0)
{
matrix[column] ^= lhs[row];
}
}
}
return matrix;
}
// Dimension 0 is the identity, dimensions 1 and 2 are the matrices of the
// first two alphabet symbols, and dimension 3 follows from dimension 2 by
// the pair relation of eq. (13), P * SZ[d] == SZ[d ^ 1].
constexpr std::array<std::array<std::uint32_t, size>, numDimensions>
buildMatrices()
{
std::array<std::array<std::uint32_t, size>, numDimensions> matrices = {};
for(int i = 0; i < size; ++i)
{
matrices[0][i] = 1u << (31 - i);
}
matrices[1] = buildMatrix({1, 0, 0, 1});
matrices[2] = buildMatrix(findAlpha());
matrices[3] = multiplyMatrix(matrices[1], matrices[2]);
return matrices;
}
// The SZ generator matrices, from which every table and program below derives.
constexpr auto matrices = buildMatrices();
void printReversedMatrices(int dimensionSize, int indexSize)
{
assert(dimensionSize >= 0);
assert(indexSize >= 0);
for(int i = 0; i < dimensionSize; ++i)
{
std::printf("{\n");
for(int j = 0; j < indexSize; ++j)
{
const auto indexRev = indexSize - (j + 1);
const auto valueRev = oqmc::reverseBits32(matrices[i][indexRev]);
const auto string = std::bitset<size>(valueRev).to_string();
std::printf("0b%s,\n", string.c_str());
}
std::printf("},\n\n");
}
}
// In the bit reversed basis, the generator matrix is unit lower-triangular, so
// GF(2) elimination factors it into shift-mask-xor steps (Ahmed 2024, eq. 18),
// one per sub-diagonal. Reversed, the steps form the program for that
// dimension. Programs print twice: left shifts for the device, and the
// conjugate form with reversed masks and right shifts for the host.
constexpr int wordBits = 16;
std::uint16_t reversedRow(int dimension, int row)
{
assert(dimension >= 0 && dimension < numDimensions);
assert(row >= 0 && row < wordBits);
std::uint16_t bits = 0;
for(int column = 0; column < wordBits; ++column)
{
const auto reversedColumn = wordBits - 1 - column;
const auto reversedRowIndex = wordBits - 1 - row;
const auto value =
oqmc::reverseBits32(matrices[dimension][reversedColumn]);
const auto reversed = std::bitset<wordBits>(value);
if(reversed[reversedRowIndex])
{
bits |= static_cast<std::uint16_t>(1u << column);
}
}
return bits;
}
void printPrograms(int dimensionSize, bool conjugate)
{
assert(dimensionSize >= 0);
for(int dimension = 0; dimension < dimensionSize; ++dimension)
{
std::array<std::uint16_t, wordBits> rows;
for(int row = 0; row < wordBits; ++row)
{
rows[row] = reversedRow(dimension, row);
}
std::vector<std::pair<std::uint16_t, int>> program;
for(int shift = 1; shift < wordBits; ++shift)
{
std::uint16_t mask = 0;
for(int row = 0; row + shift < wordBits; ++row)
{
if(((rows[row + shift] >> row) & 1) != 0)
{
mask |= static_cast<std::uint16_t>(1u << row);
}
}
if(mask == 0)
{
continue;
}
for(int row = 0; row + shift < wordBits; ++row)
{
if(((mask >> row) & 1) != 0)
{
rows[row + shift] ^= rows[row];
}
}
program.emplace_back(mask, shift);
}
for(int row = 0; row < wordBits; ++row)
{
assert(rows[row] == static_cast<std::uint16_t>(1u << row));
}
std::printf("// dimension %d\n", dimension);
for(auto step = program.rbegin(); step != program.rend(); ++step)
{
if(conjugate)
{
std::printf("index ^= static_cast<std::uint16_t>((index & "
"0x%04x) >> %d);\n",
oqmc::reverseBits16(step->first), step->second);
}
else
{
std::printf("index ^= static_cast<std::uint16_t>((index & "
"0x%04x) << %d);\n",
step->first, step->second);
}
}
std::printf("\n");
}
}
int main()
{
constexpr auto dimensionSize = 4;
constexpr auto indexSize = 16;
printReversedMatrices(dimensionSize, indexSize);
printPrograms(dimensionSize, false);
printPrograms(dimensionSize, true);
return 0;
}