]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/erasure_code/ec_base.c
e690b46ab4f213e094e5b8821f5b3dba734b11ba
[ceph.git] / ceph / src / isa-l / erasure_code / ec_base.c
1 /**********************************************************************
2 Copyright(c) 2011-2015 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <limits.h>
31 #include <string.h> // for memset
32 #include "erasure_code.h"
33 #include "ec_base.h" // for GF tables
34 #include "types.h"
35
36 unsigned char gf_mul(unsigned char a, unsigned char b)
37 {
38 #ifndef GF_LARGE_TABLES
39 int i;
40
41 if ((a == 0) || (b == 0))
42 return 0;
43
44 return gff_base[(i = gflog_base[a] + gflog_base[b]) > 254 ? i - 255 : i];
45 #else
46 return gf_mul_table_base[b * 256 + a];
47 #endif
48 }
49
50 unsigned char gf_inv(unsigned char a)
51 {
52 #ifndef GF_LARGE_TABLES
53 if (a == 0)
54 return 0;
55
56 return gff_base[255 - gflog_base[a]];
57 #else
58 return gf_inv_table_base[a];
59 #endif
60 }
61
62 void gf_gen_rs_matrix(unsigned char *a, int m, int k)
63 {
64 int i, j;
65 unsigned char p, gen = 1;
66
67 memset(a, 0, k * m);
68 for (i = 0; i < k; i++)
69 a[k * i + i] = 1;
70
71 for (i = k; i < m; i++) {
72 p = 1;
73 for (j = 0; j < k; j++) {
74 a[k * i + j] = p;
75 p = gf_mul(p, gen);
76 }
77 gen = gf_mul(gen, 2);
78 }
79 }
80
81 void gf_gen_cauchy1_matrix(unsigned char *a, int m, int k)
82 {
83 int i, j;
84 unsigned char *p;
85
86 // Identity matrix in high position
87 memset(a, 0, k * m);
88 for (i = 0; i < k; i++)
89 a[k * i + i] = 1;
90
91 // For the rest choose 1/(i + j) | i != j
92 p = &a[k * k];
93 for (i = k; i < m; i++)
94 for (j = 0; j < k; j++)
95 *p++ = gf_inv(i ^ j);
96
97 }
98
99 int gf_invert_matrix(unsigned char *in_mat, unsigned char *out_mat, const int n)
100 {
101 int i, j, k;
102 unsigned char temp;
103
104 // Set out_mat[] to the identity matrix
105 for (i = 0; i < n * n; i++) // memset(out_mat, 0, n*n)
106 out_mat[i] = 0;
107
108 for (i = 0; i < n; i++)
109 out_mat[i * n + i] = 1;
110
111 // Inverse
112 for (i = 0; i < n; i++) {
113 // Check for 0 in pivot element
114 if (in_mat[i * n + i] == 0) {
115 // Find a row with non-zero in current column and swap
116 for (j = i + 1; j < n; j++)
117 if (in_mat[j * n + i])
118 break;
119
120 if (j == n) // Couldn't find means it's singular
121 return -1;
122
123 for (k = 0; k < n; k++) { // Swap rows i,j
124 temp = in_mat[i * n + k];
125 in_mat[i * n + k] = in_mat[j * n + k];
126 in_mat[j * n + k] = temp;
127
128 temp = out_mat[i * n + k];
129 out_mat[i * n + k] = out_mat[j * n + k];
130 out_mat[j * n + k] = temp;
131 }
132 }
133
134 temp = gf_inv(in_mat[i * n + i]); // 1/pivot
135 for (j = 0; j < n; j++) { // Scale row i by 1/pivot
136 in_mat[i * n + j] = gf_mul(in_mat[i * n + j], temp);
137 out_mat[i * n + j] = gf_mul(out_mat[i * n + j], temp);
138 }
139
140 for (j = 0; j < n; j++) {
141 if (j == i)
142 continue;
143
144 temp = in_mat[j * n + i];
145 for (k = 0; k < n; k++) {
146 out_mat[j * n + k] ^= gf_mul(temp, out_mat[i * n + k]);
147 in_mat[j * n + k] ^= gf_mul(temp, in_mat[i * n + k]);
148 }
149 }
150 }
151 return 0;
152 }
153
154 // Calculates const table gftbl in GF(2^8) from single input A
155 // gftbl(A) = {A{00}, A{01}, A{02}, ... , A{0f} }, {A{00}, A{10}, A{20}, ... , A{f0} }
156
157 void gf_vect_mul_init(unsigned char c, unsigned char *tbl)
158 {
159 unsigned char c2 = (c << 1) ^ ((c & 0x80) ? 0x1d : 0); //Mult by GF{2}
160 unsigned char c4 = (c2 << 1) ^ ((c2 & 0x80) ? 0x1d : 0); //Mult by GF{2}
161 unsigned char c8 = (c4 << 1) ^ ((c4 & 0x80) ? 0x1d : 0); //Mult by GF{2}
162
163 #if __WORDSIZE == 64 || _WIN64 || __x86_64__
164 unsigned long long v1, v2, v4, v8, *t;
165 unsigned long long v10, v20, v40, v80;
166 unsigned char c17, c18, c20, c24;
167
168 t = (unsigned long long *)tbl;
169
170 v1 = c * 0x0100010001000100ull;
171 v2 = c2 * 0x0101000001010000ull;
172 v4 = c4 * 0x0101010100000000ull;
173 v8 = c8 * 0x0101010101010101ull;
174
175 v4 = v1 ^ v2 ^ v4;
176 t[0] = v4;
177 t[1] = v8 ^ v4;
178
179 c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0); //Mult by GF{2}
180 c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0); //Mult by GF{2}
181 c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0); //Mult by GF{2}
182 c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0); //Mult by GF{2}
183
184 v10 = c17 * 0x0100010001000100ull;
185 v20 = c18 * 0x0101000001010000ull;
186 v40 = c20 * 0x0101010100000000ull;
187 v80 = c24 * 0x0101010101010101ull;
188
189 v40 = v10 ^ v20 ^ v40;
190 t[2] = v40;
191 t[3] = v80 ^ v40;
192
193 #else // 32-bit or other
194 unsigned char c3, c5, c6, c7, c9, c10, c11, c12, c13, c14, c15;
195 unsigned char c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30,
196 c31;
197
198 c3 = c2 ^ c;
199 c5 = c4 ^ c;
200 c6 = c4 ^ c2;
201 c7 = c4 ^ c3;
202
203 c9 = c8 ^ c;
204 c10 = c8 ^ c2;
205 c11 = c8 ^ c3;
206 c12 = c8 ^ c4;
207 c13 = c8 ^ c5;
208 c14 = c8 ^ c6;
209 c15 = c8 ^ c7;
210
211 tbl[0] = 0;
212 tbl[1] = c;
213 tbl[2] = c2;
214 tbl[3] = c3;
215 tbl[4] = c4;
216 tbl[5] = c5;
217 tbl[6] = c6;
218 tbl[7] = c7;
219 tbl[8] = c8;
220 tbl[9] = c9;
221 tbl[10] = c10;
222 tbl[11] = c11;
223 tbl[12] = c12;
224 tbl[13] = c13;
225 tbl[14] = c14;
226 tbl[15] = c15;
227
228 c17 = (c8 << 1) ^ ((c8 & 0x80) ? 0x1d : 0); //Mult by GF{2}
229 c18 = (c17 << 1) ^ ((c17 & 0x80) ? 0x1d : 0); //Mult by GF{2}
230 c19 = c18 ^ c17;
231 c20 = (c18 << 1) ^ ((c18 & 0x80) ? 0x1d : 0); //Mult by GF{2}
232 c21 = c20 ^ c17;
233 c22 = c20 ^ c18;
234 c23 = c20 ^ c19;
235 c24 = (c20 << 1) ^ ((c20 & 0x80) ? 0x1d : 0); //Mult by GF{2}
236 c25 = c24 ^ c17;
237 c26 = c24 ^ c18;
238 c27 = c24 ^ c19;
239 c28 = c24 ^ c20;
240 c29 = c24 ^ c21;
241 c30 = c24 ^ c22;
242 c31 = c24 ^ c23;
243
244 tbl[16] = 0;
245 tbl[17] = c17;
246 tbl[18] = c18;
247 tbl[19] = c19;
248 tbl[20] = c20;
249 tbl[21] = c21;
250 tbl[22] = c22;
251 tbl[23] = c23;
252 tbl[24] = c24;
253 tbl[25] = c25;
254 tbl[26] = c26;
255 tbl[27] = c27;
256 tbl[28] = c28;
257 tbl[29] = c29;
258 tbl[30] = c30;
259 tbl[31] = c31;
260
261 #endif //__WORDSIZE == 64 || _WIN64 || __x86_64__
262 }
263
264 void gf_vect_dot_prod_base(int len, int vlen, unsigned char *v,
265 unsigned char **src, unsigned char *dest)
266 {
267 int i, j;
268 unsigned char s;
269 for (i = 0; i < len; i++) {
270 s = 0;
271 for (j = 0; j < vlen; j++)
272 s ^= gf_mul(src[j][i], v[j * 32 + 1]);
273
274 dest[i] = s;
275 }
276 }
277
278 void gf_vect_mad_base(int len, int vec, int vec_i,
279 unsigned char *v, unsigned char *src, unsigned char *dest)
280 {
281 int i;
282 unsigned char s;
283 for (i = 0; i < len; i++) {
284 s = dest[i];
285 s ^= gf_mul(src[i], v[vec_i * 32 + 1]);
286 dest[i] = s;
287 }
288 }
289
290 void ec_encode_data_base(int len, int srcs, int dests, unsigned char *v,
291 unsigned char **src, unsigned char **dest)
292 {
293 int i, j, l;
294 unsigned char s;
295
296 for (l = 0; l < dests; l++) {
297 for (i = 0; i < len; i++) {
298 s = 0;
299 for (j = 0; j < srcs; j++)
300 s ^= gf_mul(src[j][i], v[j * 32 + l * srcs * 32 + 1]);
301
302 dest[l][i] = s;
303 }
304 }
305 }
306
307 void ec_encode_data_update_base(int len, int k, int rows, int vec_i, unsigned char *v,
308 unsigned char *data, unsigned char **dest)
309 {
310 int i, l;
311 unsigned char s;
312
313 for (l = 0; l < rows; l++) {
314 for (i = 0; i < len; i++) {
315 s = dest[l][i];
316 s ^= gf_mul(data[i], v[vec_i * 32 + l * k * 32 + 1]);
317
318 dest[l][i] = s;
319 }
320 }
321 }
322
323 void gf_vect_mul_base(int len, unsigned char *a, unsigned char *src, unsigned char *dest)
324 {
325 //2nd element of table array is ref value used to fill it in
326 unsigned char c = a[1];
327 while (len-- > 0)
328 *dest++ = gf_mul(c, *src++);
329 }
330
331 struct slver {
332 UINT16 snum;
333 UINT8 ver;
334 UINT8 core;
335 };
336
337 // Version info
338 struct slver gf_vect_mul_init_slver_00020035;
339 struct slver gf_vect_mul_init_slver = { 0x0035, 0x02, 0x00 };
340
341 struct slver ec_encode_data_base_slver_00010135;
342 struct slver ec_encode_data_base_slver = { 0x0135, 0x01, 0x00 };
343
344 struct slver gf_vect_mul_base_slver_00010136;
345 struct slver gf_vect_mul_base_slver = { 0x0136, 0x01, 0x00 };
346
347 struct slver gf_vect_dot_prod_base_slver_00010137;
348 struct slver gf_vect_dot_prod_base_slver = { 0x0137, 0x01, 0x00 };
349
350 struct slver gf_mul_slver_00000214;
351 struct slver gf_mul_slver = { 0x0214, 0x00, 0x00 };
352
353 struct slver gf_invert_matrix_slver_00000215;
354 struct slver gf_invert_matrix_slver = { 0x0215, 0x00, 0x00 };
355
356 struct slver gf_gen_rs_matrix_slver_00000216;
357 struct slver gf_gen_rs_matrix_slver = { 0x0216, 0x00, 0x00 };
358
359 struct slver gf_gen_cauchy1_matrix_slver_00000217;
360 struct slver gf_gen_cauchy1_matrix_slver = { 0x0217, 0x00, 0x00 };