-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
161 lines (139 loc) · 5.41 KB
/
test.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
#define CPPECC_IMPLEMENTATION
#include "cppecc.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
using namespace cppecc;
struct Result
{
long long encodeTime_;
long long decodeTime_;
cppecc_s32 numErrors_;
cppecc_s64 avgErrors_;
};
void print(cppecc_s32 size, std::vector<cppecc_u8>& v)
{
for(cppecc_s32 i=0; i<size; ++i){
std::cout << int(v[i]) << ", ";
}
}
#if 0
void error_check()
{
static const cppecc_s32 MessageSize = 16;
static const cppecc_s32 ECCSize = 10;
cppecc_u8 message[MessageSize] = {110, 211, 97, 221, 35, 153, 52, 124, 191, 109, 194, 65, 59, 242, 74, 22};
cppecc_u8 diff[MessageSize+ECCSize] = {0, 0, 0, 92, 0, 237, 0, 0, 0, 8, 153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 161, 0, 0, 0, 0, 0};
cppecc_u8 encoded[MessageSize + ECCSize];
cppecc_u8 decoded[MessageSize + ECCSize];
RSContext context;
gf_initialize(&context, ECCSize);
std::copy(message, message+MessageSize, encoded);
rs_encode(&context, MessageSize, &encoded[0], ECCSize);
for(cppecc_s32 j = 0; j < (MessageSize + ECCSize); ++j) {
decoded[j] = encoded[j] ^ diff[j];
}
cppecc_s32 corrected = rs_decode(&context, MessageSize, &decoded[0], ECCSize);
for(cppecc_s32 j = 0; j < MessageSize; ++j) {
if(message[j] != decoded[j]) {
assert(false);
}
}
}
#endif
Result reed_solomon(cppecc_s32 messageSize, cppecc_s32 eccSize, cppecc_s32 maxErrors, cppecc_s32 count)
{
const cppecc_s32 MaxECC = eccSize>>1;
std::vector<cppecc_u8> message;
message.resize(messageSize);
std::vector<cppecc_u8> encoded;
encoded.resize(messageSize+eccSize);
std::vector<cppecc_u8> decoded;
decoded.resize(messageSize+eccSize);
RSContext context;
Result result = {};
gf_initialize(&context, eccSize);
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_int_distribution<> randErrors(0, maxErrors);
std::uniform_int_distribution<> randPositions(0, messageSize+eccSize-1);
long long encodeTime = 0;
long long decodeTime = 0;
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point end;
for(cppecc_s32 i = 0; i < count; ++i) {
for(cppecc_s32 j=0; j<messageSize; ++j){
message[j] = static_cast<cppecc_u8>(engine()&0xFFU);
}
std::copy(message.begin(), message.end(), encoded.begin());
start = std::chrono::high_resolution_clock::now();
rs_encode(&context, messageSize, &encoded[0], eccSize);
end = std::chrono::high_resolution_clock::now();
encodeTime += std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
std::copy(encoded.begin(), encoded.end(), decoded.begin());
cppecc_s32 numErrors = randErrors(engine);
result.avgErrors_ += numErrors;
for(cppecc_s32 j=0; j<numErrors; ++j){
decoded[randPositions(engine)] ^= (engine());
}
numErrors = 0;
for(cppecc_s32 j=0; j<(messageSize+eccSize); ++j){
if(encoded[j] != decoded[j]){
++numErrors;
}
encoded[j] ^= decoded[j];
}
start = std::chrono::high_resolution_clock::now();
cppecc_s32 corrected = rs_decode(&context, messageSize, &decoded[0], eccSize);
end = std::chrono::high_resolution_clock::now();
decodeTime += std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
if(numErrors<=MaxECC){
if(corrected<0){
result.numErrors_ += 1;
}else{
for(cppecc_s32 j=0; j<messageSize; ++j){
if(message[j] != decoded[j]){
std::cout << "message size: " << messageSize << ", ecc size: " << eccSize << std::endl;
std::cout << "message: ";
print(messageSize, message);
std::cout << std::endl;
std::cout << "diff: ";
print(messageSize+eccSize, encoded);
std::cout << std::endl;
assert(false);
}
}
}
}else{
if(0<=corrected){
result.numErrors_ += 1;
}
}
}
result.encodeTime_ = encodeTime / count;
result.decodeTime_ = decodeTime / count;
result.avgErrors_ /= count;
return result;
}
int main(void)
{
static const int Patterns = 1024;
static const int Count = 4096;
int MinMessageSize = 16;
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_int_distribution<> messageSizeRange(MinMessageSize, CPPECC_GF_NW1-CPPECC_MAX_ECC_SIZE);
Result result;
for(cppecc_s32 i = 0; i < Patterns; ++i) {
cppecc_s32 messageSize = messageSizeRange(engine);
cppecc_s32 eccMaxSize = std::min(CPPECC_MAX_ECC_SIZE, messageSize-1);
std::uniform_int_distribution<> eccSizeRange(1, eccMaxSize);
cppecc_s32 eccSize = eccSizeRange(engine);
result = reed_solomon(messageSize, eccSize, eccSize, Count);
std::cout << "result: " << "message size: " << messageSize << " ecc size: " << eccSize << " encode time (micro): " << result.encodeTime_ << " decode time (micro): " << result.decodeTime_ << " errors: " << result.numErrors_ << '/' << result.avgErrors_ << std::endl;
}
return 0;
}