-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.cpp
143 lines (126 loc) · 4.41 KB
/
Encoder.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
#include "Encoder.h"
#include <iostream>
#include <thread>
Encoder::Encoder(const vector<vector<uint8_t>>& message, int n, MatrixType matrixType)
: messages(message), n(n), matrixType(matrixType) {
k = message.size();
L = message[0].size();
generatorMatrix = vector<vector<int>>(n, vector<int>(k, 0));
if (matrixType == MatrixType::TWO_TONE) {
int d = n / 2;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (i < d) {
generatorMatrix[i][j] = (d - i) * (k - j - 1);
} else {
generatorMatrix[i][j] = (i - d) * j;
}
}
}
}
else if (matrixType == MatrixType::SYSTEMATIC_TWOTONE) {
// 前k行为单位矩阵(原始数据)
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
generatorMatrix[i][j] = (i == j) ? 0 : INT_MAX; // 0表示不移位,INT_MAX表示不参与异或
}
}
// 后(n-k)行为Two-tone矩阵(冗余数据)
int d = (n - k) / 2;
for (int i = k; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (i < k + d) {
generatorMatrix[i][j] = (d - (i - k)) * (k - j - 1);
} else {
generatorMatrix[i][j] = ((i - k) - d) * j;
}
}
}
}
else if (matrixType == MatrixType::SYSTEMATIC_RID) {
// 前k行为单位矩阵(原始数据)
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
generatorMatrix[i][j] = (i == j) ? 0 : INT_MAX;
}
}
// 后(n-k)行为RID矩阵(冗余数据)
for (int i = k; i < n; ++i) {
for (int j = 0; j < k; ++j) {
generatorMatrix[i][j] = (i - k + 1) * j;
}
}
}
}
// 优化的移位异或操作
void Encoder::fastShiftXOR(uint8_t* dest, const uint8_t* src, int shift, int length) {
int byteShift = shift / 8;
int bitShift = shift % 8;
if (bitShift == 0) {
// 按字节对齐的情况
for (int i = 0; i < (length - shift) / 8; ++i) {
dest[byteShift + i] ^= src[i];
}
} else {
// 非字节对齐的情况
uint16_t buffer;
for (int i = 0; i < (length - shift) / 8; ++i) {
buffer = (src[i] << bitShift);
if (i + 1 < length / 8) {
buffer |= (src[i + 1] >> (8 - bitShift));
}
dest[byteShift + i] ^= buffer & 0xFF;
}
}
}
void Encoder::encodeWorker(int startRow, int endRow, vector<vector<uint8_t>>& encodedMessage, const vector<int>& t_star) {
for (int i = startRow; i < endRow; ++i) {
for (int j = 0; j < k; ++j) {
if (generatorMatrix[i][j] != INT_MAX) {
fastShiftXOR(encodedMessage[i].data(),
messages[j].data(),
generatorMatrix[i][j],
L + t_star[i]);
}
}
}
}
vector<vector<uint8_t>> Encoder::encode() {
vector<int> t_star(n, 0);
// 计算每行的最大移位
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (generatorMatrix[i][j] != INT_MAX) {
t_star[i] = max(t_star[i], generatorMatrix[i][j]);
}
}
}
// 初始化编码结果
vector<vector<uint8_t>> encodedMessage(n);
for (int i = 0; i < n; ++i) {
encodedMessage[i].resize((L + t_star[i] + 7) / 8 * 8, 0);
}
// 使用多线程并行计算
const int numThreads = thread::hardware_concurrency();
vector<thread> threads;
int rowsPerThread = n / numThreads;
for (int i = 0; i < numThreads; ++i) {
int startRow = i * rowsPerThread;
int endRow = (i == numThreads - 1) ? n : (i + 1) * rowsPerThread;
threads.emplace_back(&Encoder::encodeWorker, this, startRow, endRow,
ref(encodedMessage), ref(t_star));
}
for (auto& t : threads) {
t.join();
}
return encodedMessage;
}
double Encoder::benchmarkEncode(int iterations) {
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
encode();
}
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> diff = end - start;
return diff.count() / iterations;
}