-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFecalEncoder.cpp
247 lines (195 loc) · 8.35 KB
/
FecalEncoder.cpp
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
/*
Copyright (c) 2017 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 Fecal 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.
*/
#include "FecalEncoder.h"
namespace fecal {
//------------------------------------------------------------------------------
// EncoderAppDataWindow
void EncoderAppDataWindow::AllocateOriginals()
{
OriginalData.resize(InputCount);
}
void EncoderAppDataWindow::SetEncoderInput(void* const * const input_data)
{
FECAL_DEBUG_ASSERT(InputCount > 0); // SetParameters() must be called first
for (unsigned ii = 0, count = InputCount; ii < count; ++ii)
OriginalData[ii] = reinterpret_cast<const uint8_t*>(input_data[ii]);
}
//------------------------------------------------------------------------------
// Encoder
// This optimization speeds up encoding by about 5%
#ifdef FECAL_ADD2_OPT
#define FECAL_ADD2_ENC_SETUP_OPT
#endif
FecalResult Encoder::Initialize(unsigned input_count, void* const * const input_data, uint64_t total_bytes)
{
// Validate input and set parameters
if (!Window.SetParameters(input_count, total_bytes))
{
FECAL_DEBUG_BREAK; // Invalid input
return Fecal_InvalidInput;
}
Window.AllocateOriginals();
Window.SetEncoderInput(input_data);
const unsigned symbolBytes = Window.SymbolBytes;
// Allocate lane sums
for (unsigned laneIndex = 0; laneIndex < kColumnLaneCount; ++laneIndex)
{
for (unsigned sumIndex = 0; sumIndex < kColumnSumCount; ++sumIndex)
{
if (!LaneSums[laneIndex][sumIndex].Allocate(symbolBytes))
return Fecal_OutOfMemory;
// Clear memory in each lane sum
memset(LaneSums[laneIndex][sumIndex].Data, 0, symbolBytes);
}
}
// Allocate workspace
if (!ProductWorkspace.Allocate(symbolBytes))
return Fecal_OutOfMemory;
// TBD: Unroll first set of 8 lanes to avoid the extra memset above?
// TBD: Use GetLaneSum() approach do to minimal work for small output?
#ifdef FECAL_ADD2_ENC_SETUP_OPT
for (unsigned laneIndex = 0; laneIndex < kColumnLaneCount; ++laneIndex)
{
// Sum[0] += Data
XORSummer sum;
sum.Initialize(LaneSums[laneIndex][0].Data, symbolBytes);
const unsigned columnEnd = input_count - 1;
for (unsigned column = laneIndex; column < columnEnd; column += kColumnLaneCount)
{
const uint8_t* columnData = reinterpret_cast<const uint8_t*>(input_data[column]);
sum.Add(columnData);
}
if ((columnEnd % kColumnLaneCount) == laneIndex)
{
const uint8_t* columnData = reinterpret_cast<const uint8_t*>(input_data[columnEnd]);
gf256_add_mem(LaneSums[laneIndex][0].Data, columnData, Window.FinalBytes);
}
sum.Finalize();
}
#endif
// For each input column:
for (unsigned column = 0; column < input_count; ++column)
{
const uint8_t* columnData = reinterpret_cast<const uint8_t*>(input_data[column]);
const unsigned columnBytes = Window.GetColumnBytes(column);
const unsigned laneIndex = column % kColumnLaneCount;
const uint8_t CX = GetColumnValue(column);
const uint8_t CX2 = gf256_sqr(CX);
#ifndef FECAL_ADD2_ENC_SETUP_OPT
// Sum[0] += Data
gf256_add_mem(LaneSums[laneIndex][0].Data, columnData, columnBytes);
#endif
// Sum[1] += CX * Data
gf256_muladd_mem(LaneSums[laneIndex][1].Data, CX, columnData, columnBytes);
// Sum[2] += CX^2 * Data
gf256_muladd_mem(LaneSums[laneIndex][2].Data, CX2, columnData, columnBytes);
}
return Fecal_Success;
static_assert(kColumnSumCount == 3, "Update this");
}
FecalResult Encoder::Encode(FecalSymbol& symbol)
{
// If encoder is not initialized:
if (!ProductWorkspace.Data)
return Fecal_InvalidInput;
const unsigned symbolBytes = Window.SymbolBytes;
if (symbol.Bytes != symbolBytes)
return Fecal_InvalidInput;
// Load parameters
const unsigned count = Window.InputCount;
uint8_t* outputSum = reinterpret_cast<uint8_t*>( symbol.Data );
uint8_t* outputProduct = ProductWorkspace.Data;
const unsigned row = symbol.Index;
// Initialize LDPC
PCGRandom prng;
prng.Seed(row, count);
// Accumulate original data into the two sums
const unsigned pairCount = (Window.InputCount + kPairAddRate - 1) / kPairAddRate;
// Unrolled first loop:
{
const unsigned element1 = prng.Next() % count;
const uint8_t* original1 = Window.OriginalData[element1];
const unsigned elementRX = prng.Next() % count;
const uint8_t* originalRX = Window.OriginalData[elementRX];
// Sum = Original[element1]
if (Window.IsFinalColumn(element1))
{
memcpy(outputSum, original1, Window.FinalBytes);
memset(outputSum + Window.FinalBytes, 0, symbolBytes - Window.FinalBytes);
}
else
memcpy(outputSum, original1, symbolBytes);
// Product = Original[elementRX]
if (Window.IsFinalColumn(elementRX))
{
memcpy(outputProduct, originalRX, Window.FinalBytes);
memset(outputProduct + Window.FinalBytes, 0, symbolBytes - Window.FinalBytes);
}
else
memcpy(outputProduct, originalRX, symbolBytes);
}
XORSummer sum;
sum.Initialize(outputSum, symbolBytes);
XORSummer prod;
prod.Initialize(outputProduct, symbolBytes);
for (unsigned i = 1; i < pairCount; ++i)
{
const unsigned element1 = prng.Next() % count;
const uint8_t* original1 = Window.OriginalData[element1];
const unsigned elementRX = prng.Next() % count;
const uint8_t* originalRX = Window.OriginalData[elementRX];
// Sum += Original[element1]
if (Window.IsFinalColumn(element1))
gf256_add_mem(outputSum, original1, Window.FinalBytes);
else
sum.Add(original1);
// Product += Original[elementRX]
if (Window.IsFinalColumn(elementRX))
gf256_add_mem(outputProduct, originalRX, Window.FinalBytes);
else
prod.Add(originalRX);
}
// For each lane:
for (unsigned laneIndex = 0; laneIndex < kColumnLaneCount; ++laneIndex)
{
// Compute the operations to run for this lane and row
unsigned opcode = GetRowOpcode(laneIndex, row);
// Sum += Random Lanes
unsigned mask = 1;
for (unsigned sumIndex = 0; sumIndex < kColumnSumCount; ++sumIndex, mask <<= 1)
if (opcode & mask)
sum.Add(LaneSums[laneIndex][sumIndex].Data);
// Product += Random Lanes
for (unsigned sumIndex = 0; sumIndex < kColumnSumCount; ++sumIndex, mask <<= 1)
if (opcode & mask)
prod.Add(LaneSums[laneIndex][sumIndex].Data);
}
sum.Finalize();
prod.Finalize();
// Sum += RX * Product
gf256_muladd_mem(outputSum, GetRowValue(row), outputProduct, symbolBytes);
return Fecal_Success;
}
} // namespace fecal