forked from catid/wirehair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WirehairTools.h
521 lines (394 loc) · 17.2 KB
/
WirehairTools.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/** \file
\brief Wirehair : Tools
\copyright Copyright (c) 2012-2018 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Wirehair nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef WIREHAIR_TOOLS_H
#define WIREHAIR_TOOLS_H
#include "wirehair.h"
#include "gf256.h"
#include <new> // std::nothrow
// Compiler-specific debug break
#if defined(_DEBUG) || defined(DEBUG)
#define CAT_DEBUG
#ifdef _WIN32
#define CAT_DEBUG_BREAK() __debugbreak()
#else
#define CAT_DEBUG_BREAK() __builtin_trap()
#endif
#define CAT_DEBUG_ASSERT(cond) { if (!(cond)) { CAT_DEBUG_BREAK(); } }
#else
#define CAT_DEBUG_BREAK() do {} while (false);
#define CAT_DEBUG_ASSERT(cond) do {} while (false);
#endif
namespace wirehair {
//------------------------------------------------------------------------------
// Configuration
// Debugging:
//#define CAT_DUMP_CODEC_DEBUG /**< Turn on debug output for decoder */
//#define CAT_DUMP_ROWOP_COUNTERS /**< Dump row operations counters to console */
//#define CAT_DUMP_PIVOT_FAIL /**< Dump pivot failure to console */
//#define CAT_DUMP_GE_MATRIX /**< Dump GE matrix to console */
// Limits:
#define CAT_REF_LIST_MAX 32 /**< Tune to be as small as possible and still succeed */
#define CAT_MAX_DENSE_ROWS 500 /**< Maximum dense row count */
#define CAT_MAX_EXTRA_ROWS 32 /**< Maximum number of extra rows to support before reusing existing rows */
#define CAT_WIREHAIR_MAX_N 64000 /**< Largest N value to allow */
#define CAT_WIREHAIR_MIN_N 2 /**< Smallest N value to allow */
// Optimization options:
#define CAT_COPY_FIRST_N /**< Copy the first N rows from the input (faster) */
#define CAT_HEAVY_WIN_MULT /**< Use 4-bit table and multiplication optimization (faster) */
#define CAT_WINDOWED_BACKSUB /**< Use window optimization for back-substitution (faster) */
#define CAT_WINDOWED_LOWERTRI /**< Use window optimization for lower triangle elimination (faster) */
#define CAT_ALL_ORIGINAL /**< Avoid doing calculations for 0 losses -- Requires CAT_COPY_FIRST_N (faster) */
/// Number of heavy rows at the bottom of the matrix
static const unsigned kHeavyRows = 6;
/// Number of heavy columns at the bottom right of the matrix
static const unsigned kHeavyCols = 18;
// If the heavy row/column count changes then we will have to regenerate a new
// heavy matrix using HeavyRowGenerator.cpp, and all the dense/peel seeds.
//------------------------------------------------------------------------------
// PCG PRNG
/// From http://www.pcg-random.org/
class PCGRandom
{
public:
void Seed(uint64_t y, uint64_t x = 0)
{
State = 0;
Inc = (y << 1u) | 1u;
Next();
State += x;
Next();
}
GF256_FORCE_INLINE uint32_t Next()
{
const uint64_t oldstate = State;
State = oldstate * UINT64_C(6364136223846793005) + Inc;
const uint32_t xorshifted = (uint32_t)(((oldstate >> 18) ^ oldstate) >> 27);
const uint32_t rot = oldstate >> 59;
return (xorshifted >> rot) | (xorshifted << ((uint32_t)(-(int32_t)rot) & 31));
}
uint64_t State = 0, Inc = 0;
};
//------------------------------------------------------------------------------
// Bit-Rotation Macros
/// These bit rotations only work for uint64_t
#define CAT_ROL64(n, r) ( ((uint64_t)(n) << (r)) | ((uint64_t)(n) >> (64 - (r))) )
#define CAT_ROR64(n, r) ( ((uint64_t)(n) >> (r)) | ((uint64_t)(n) << (64 - (r))) )
//------------------------------------------------------------------------------
// Utility: 16-bit Integer Square Root function
/**
FloorSquareRoot16()
16-bit Integer Square Root function = (uint16_t)(sqrt(x))
This is useful for prime testing, where the algorithm needs to stop testing
at or before the square root of the number to test.
*/
uint16_t FloorSquareRoot16(uint16_t x);
//------------------------------------------------------------------------------
// Utility: 16-bit Truncated Sieve of Eratosthenes Next Prime function
/**
NextPrime16()
16-bit Truncated Sieve of Eratosthenes Next Prime function
It uses trial division up to the square root of the number to test.
Uses a truncated sieve table to pick the next number to try, which
avoids small factors 2, 3, 5, 7. This can be considered a more
involved version of incrementing by 2 instead of 1. It takes about
25% less time on average than the approach that just increments by 2.
Because of the tabular increment this is a hybrid approach. The sieve
would just use a very large table, but I wanted to limit the size of
the table to something fairly small and reasonable. 210 bytes for the
sieve table. 102 bytes for the primes list.
It also calculates the integer square root faster than cmath sqrt()
and uses multiplication to update the square root instead for speed.
The largest prime number within 16 bits is 65521.
If input is 0 or 1, it returns 1.
If input is 2, it returns 2.
If input is a prime, it will return the same number.
If input is above 65521, it will return 0 and assert in debug mode.
Otherwise it will return the next larger number that is prime.
*/
uint16_t NextPrime16(uint16_t n);
//------------------------------------------------------------------------------
// Utility: GF(2) Invertible Matrix Generator function
/**
AddInvertibleGF2Matrix()
This function adds (xoring in) a binary matrix on top of the given one.
This function generates random-looking invertible NxN binary matrices for
N in the range [1, 512). For larger values of N it will just add the
identity matrix.
The input matrix is expected to be stored row-first in memory.
The offset to the first column bit to add can be specified.
Precondition: N != 0
*/
void AddInvertibleGF2Matrix(
uint64_t * GF256_RESTRICT matrix, ///< Address of upper left word
const unsigned offset, ///< Offset in bits to the first sum column
const unsigned pitchWords, ///< Pitch of the matrix in words
const unsigned n ///< Number of bits in the matrix to generate
);
//------------------------------------------------------------------------------
// Utility: Deck Shuffling function
/**
ShuffleDeck16()
Given a PRNG, generate a deck of cards in a random order.
The deck will contain elements with values between 0 and count - 1.
*/
void ShuffleDeck16(
PCGRandom &prng,
uint16_t * GF256_RESTRICT deck,
const uint32_t count);
//------------------------------------------------------------------------------
// Utility: Column Iterator function
/**
IterateNextColumn()
This implements a very light PRNG (Weyl function) to quickly generate
a set of random-looking columns without replacement.
This is Stewart Platt's excellent loop-less iterator optimization.
His common cases all require no additional modulus operation, which
makes it faster than the rare case that I designed.
*/
GF256_FORCE_INLINE void IterateNextColumn(
uint16_t &x, ///< Current column number to mutate
const uint16_t b, ///< Non-prime modulus for addition
const uint16_t p, ///< Next prime above b (or = b if prime)
const uint16_t a ///< Number to add for Weyl generator
)
{
x = (x + a) % p;
// If the result is in the range [b, p):
if (x >= b)
{
const uint16_t distanceToP = p - x;
// If adding again would wrap around:
if (a >= distanceToP) {
x = a - distanceToP;
}
else {
// Handle rare case where multiple adds are needed
x = (((uint32_t)a << 16) - distanceToP) % a;
}
}
}
/**
GeneratePeelRowWeight()
Ideal Soliton weight distribution from
"LT Codes" (2002) by Michael Luby
The weight distribution selected for use in this codec is the
Ideal Soliton distribution. The PMF for weights 2 and higher
is 1 / (k*(k - 1)). Accumulating these yields the WEIGHT_DIST
table below up to weight 64. I stuck ~0 at the end of the
table to make sure the while loop in the function terminates.
To produce a good code, the probability of weight-1 should be
added into each element of the WEIGHT_DIST table. Instead,
I add it programmatically in the function to allow it to be
easily tuned.
I played around with where to truncate this table, and found
that for higher block counts, the number of deferred rows after
greedy peeling is much lower for 64 weights than 32. And after
tuning the codec for weight 64, the performance was slightly
better than with 32.
I also tried different probabilities for weight-1 rows, and
settled on 1/128 as having the best performance in a few select
tests. Setting it too high or too low (or even to zero) tends
to reduce the performance of the codec.
However, once N gets much much larger, it is actually very
beneficial to switch over to weight-2 as a minimum.
*/
uint16_t GeneratePeelRowWeight(
uint32_t rv, ///< 32-bit random value
uint16_t block_count ///< Number of input blocks
);
//------------------------------------------------------------------------------
// Utility: Peel Matrix Row Parameter Initialization
struct PeelRowParameters
{
/// Peeling matrix: Column generator
uint16_t PeelCount, PeelFirst, PeelAdd;
/// Mixing matrix: Column generator
uint16_t MixFirst, MixAdd;
/// This function generates the PRNG parameters for randomly selecting which
/// columns to add into the given row
void Initialize(
uint32_t row_seed,
uint32_t p_seed,
uint16_t peel_column_count,
uint16_t mix_column_count);
};
//------------------------------------------------------------------------------
// Utility: Peel Row Iterator
/// Generates all of the columns referenced by a row
class PeelRowIterator
{
public:
/// Initialize iterator with parameters
GF256_FORCE_INLINE PeelRowIterator(
const PeelRowParameters& params,
uint16_t columnCount,
uint16_t columnCountNextPrime)
{
CAT_DEBUG_ASSERT(columnCountNextPrime >= columnCount);
CAT_DEBUG_ASSERT(params.PeelFirst < columnCount);
CAT_DEBUG_ASSERT(params.PeelCount > 0);
CAT_DEBUG_ASSERT(params.PeelAdd > 0);
CAT_DEBUG_ASSERT(columnCount > 0);
CAT_DEBUG_ASSERT(params.PeelCount <= CAT_MAX_DENSE_ROWS)
NextColumn = params.PeelFirst;
ColumnsRemaining = params.PeelCount - 1;
Adder = params.PeelAdd;
ColumnCount = columnCount;
ColumnCountNextPrime = columnCountNextPrime;
}
/// Get current column
GF256_FORCE_INLINE uint16_t GetColumn() const
{
return NextColumn;
}
/// Get the next column.
/// Returns true on success.
/// Returns false if no more columns remain
GF256_FORCE_INLINE bool Iterate()
{
if (ColumnsRemaining <= 0) {
return false;
}
--ColumnsRemaining;
IterateNextColumn(
NextColumn,
ColumnCount,
ColumnCountNextPrime,
Adder);
return true;
}
protected:
/// Number of column references remaining in this row
uint16_t ColumnsRemaining = 0;
/// Next column to output
uint16_t NextColumn = 0;
/// Weyl generator adder
uint16_t Adder = 0;
/// Number of columns overall
uint16_t ColumnCount = 0;
/// Next prime starting at number of columns
uint16_t ColumnCountNextPrime = 0;
};
//------------------------------------------------------------------------------
// Utility: Row Mix Iterator
/// Generates all of the columns referenced by a row
class RowMixIterator
{
public:
/// Initialize iterator with parameters
GF256_FORCE_INLINE RowMixIterator(
const PeelRowParameters& params,
uint16_t columnCount,
uint16_t columnCountNextPrime)
{
CAT_DEBUG_ASSERT(columnCountNextPrime >= columnCount);
CAT_DEBUG_ASSERT(params.MixFirst < columnCount);
CAT_DEBUG_ASSERT(params.MixAdd > 0);
CAT_DEBUG_ASSERT(columnCount > 0);
uint16_t x = params.MixFirst;
Columns[0] = x;
for (unsigned i = 1; i < kColumnCount; ++i)
{
IterateNextColumn(
x,
columnCount,
columnCountNextPrime,
params.MixAdd);
Columns[i] = x;
}
}
/// Number of output columns
static const unsigned kColumnCount = 3;
/// Output columns
uint16_t Columns[kColumnCount];
};
//------------------------------------------------------------------------------
// SIMD-Safe Aligned Memory Allocations
/// Allocate memory and return a pointer aligned to the size used for SIMD ops
uint8_t* SIMDSafeAllocate(size_t size);
/// Free an aligned pointer
void SIMDSafeFree(void* ptr);
//------------------------------------------------------------------------------
// Tables for small N
/// These tables were lovingly hand-crafted by hard-working indigenous peoples:
static const unsigned kTinyTableCount = 65; // 0..64
static const unsigned kSmallTableCount = 2048 - kTinyTableCount;
/// This table maps N -> dense row count
extern const uint8_t kTinyDenseCounts[kTinyTableCount];
/// This table maps N -> dense seed
extern const uint16_t kTinyDenseSeeds[kTinyTableCount];
/// This table maps (N - kTinyTableCount) -> dense seed
extern const uint8_t kSmallDenseSeeds[kSmallTableCount];
/// This table maps N -> peel seed
extern const uint8_t kSmallPeelSeeds[kTinyTableCount + kSmallTableCount];
//------------------------------------------------------------------------------
// Tables for larger N
static const unsigned kDenseSeedCount = 100;
/// This table maps from (N / 4) -> dense seed for N >= 2048
extern const uint8_t kDenseSeeds[kDenseSeedCount];
static const unsigned kPeelSeedSubdivisions = 2048;
/// This table maps from N % kPeelSeedSubdivisions -> peel seed
extern const uint8_t kPeelSeeds[kPeelSeedSubdivisions];
//------------------------------------------------------------------------------
// DenseCount
static const unsigned kMaxDenseCount = 400;
/**
This function returns the number of dense rows in the matrix given the
number of input blocks (N). The dense rows are supposed to make a random
binary submatrix where it is equally likely to have a 1 or a 0. If the
peel rows fail to contain a data piece it's much more likely to find that
piece in these rows. Whenever the peel row parameters are re-tuned, the
optimal dense count for each N must be re-computed.
For N < 2048, a thorough search is performed by GenerateSmallDenseSeeds.cpp
to find the best match of dense seed and dense count.
For larger values of N up to 64000, the dense count is generated by the
GenerateDenseCount.cpp program. This program was run 5 times and the
output graphed. The largest dense count from each run was used and
approximated by 64 data points that overestimate the dense count needed
for the other values of N.
The binary search used is unit-tested in TableGenerator.cpp with array
bounds checks.
For N >= 2048 the graph-based value is rounded up to the next value s.t.
D Mod 4 = 2. This is because I found that on average these counts lead
to much better results.
Preconditions: N >= 2 and N <= 64000
*/
uint16_t GetDenseCount(unsigned N);
//------------------------------------------------------------------------------
// DenseSeed
/**
This function returns the seed to use for the dense rows, given the number
of input blocks (N) and the count of dense rows provided by GetDenseCount().
*/
uint16_t GetDenseSeed(unsigned N, unsigned dense_count);
//------------------------------------------------------------------------------
// PeelSeed
/**
This function returns the seed to use for the peel rows.
*/
uint16_t GetPeelSeed(unsigned N);
} // namespace wirehair
#endif // WIREHAIR_TOOLS_H