38 #include <rs_galois.h>
39 #include <liberasurecode_rs_vand.h>
49 for (i = 0; i < rows; i++) {
50 for (j = 0; j < cols; j++) {
51 printf(
"%d ", matrix[(i * cols) + j]);
62 for (i = 0; i < n; i++) {
63 for (j = 0; j < n; j++) {
65 for (k = 0; k < n; k++) {
77 for (i = 0; i < n; i++) {
78 for (j = 0; j < n; j++) {
79 int val = matrix[(i*n) + j];
96 return &matrix[row_idx * num_cols];
99 void copy_row(
int *from_matrix,
int *to_matrix,
int from_row_idx,
int to_row_idx,
int num_cols)
101 int *from_row =
get_matrix_row(from_matrix, from_row_idx, num_cols);
104 memcpy(to_row, from_row,
sizeof(
int)*num_cols);
110 while (missing_idxs[i] > -1) {
111 if (missing_idxs[i] == index_to_check) {
124 for (i = 0, j = 0; i < n && j < k; i++) {
126 copy_row(gen_matrix, dec_matrix, i, j, k);
150 int *matrix = (
int*)malloc(
sizeof(
int)*rows*cols);
152 if (NULL == matrix)
return NULL;
156 for (i = 1; i < cols; i++) matrix[i] = 0;
160 for (i = 1; i < rows; i++) {
162 for (j = 0; j < cols; j++) {
163 matrix[i * cols + j] = acc;
177 for (i = 0; i < num_cols; i++) {
184 void col_mult(
int *matrix,
int elem,
int col_idx,
int num_rows,
int num_cols)
188 for (i = 0; i < num_rows; i++) {
194 void row_mult(
int *matrix,
int elem,
int row_idx,
int num_rows,
int num_cols)
196 int i, to_row = row_idx * num_cols;
198 for (i = 0; i < num_cols; i++) {
204 void col_mult_and_add(
int *matrix,
int elem,
int from_col,
int to_col,
int num_rows,
int num_cols)
208 for (i = 0; i < num_rows; i++) {
209 matrix[to_col] = matrix[to_col] ^
rs_galois_mult(matrix[from_col], elem);
210 from_col += num_cols;
215 void row_mult_and_add(
int *matrix,
int elem,
int from_row,
int to_row,
int num_rows,
int num_cols)
218 from_row = from_row * num_cols;
219 to_row = to_row * num_cols;
220 for (i = 0; i < num_cols; i++) {
221 matrix[to_row] = matrix[to_row] ^
rs_galois_mult(matrix[from_row], elem);
231 row_idx = (num_cols * row) + row;
232 for (i = row; i < num_rows; i++) {
233 if (matrix[row_idx] != 0) {
249 if (NULL == matrix)
return NULL;
252 for (i = 1; i < cols; i++) {
253 int diag_idx = ((cols*i) + i);
264 if (matrix[diag_idx] != 1) {
270 for (j = 0; j < cols; j++) {
271 int row_val = matrix[(i * cols) + j];
272 if (i != j && row_val != 0) {
279 for (i = 0; i < cols; i++) {
280 int row_val = matrix[(cols * cols) + i];
302 memset(inverse, 0,
sizeof(
int)*n*n);
305 for (i = 0; i < n; i++) {
306 int diag_idx = ((n*i) + i);
307 inverse[diag_idx] = 1;
310 for (i = 0; i < n; i++) {
311 int diag_idx = ((n*i) + i);
322 if (matrix[diag_idx] != 1) {
324 row_mult(matrix, leading_val_inv, i, n, n);
325 row_mult(inverse, leading_val_inv, i, n, n);
329 for (j = 0; j < n; j++) {
331 int val = matrix[(j * n) + i];
344 uint32_t *_from_buf = (uint32_t*)from_buf;
345 uint32_t *_to_buf = (uint32_t*)to_buf;
346 int adj_blocksize = blocksize / 4;
347 int trailing_bytes = blocksize % 4;
349 for (i = 0; i < adj_blocksize; i++) {
350 _to_buf[i] = _to_buf[i] ^ _from_buf[i];
353 for (i = blocksize-trailing_bytes; i < blocksize; i++) {
354 to_buf[i] = to_buf[i] ^ from_buf[i];
361 uint16_t *_from_buf = (uint16_t*)from_buf;
362 uint16_t *_to_buf = (uint16_t*)to_buf;
363 int adj_blocksize = blocksize / 2;
364 int trailing_bytes = blocksize % 2;
367 for (i = 0; i < adj_blocksize; i++) {
368 _to_buf[i] = _to_buf[i] ^ (uint16_t)
rs_galois_mult(_from_buf[i], mult);
371 if (trailing_bytes == 1) {
376 for (i = 0; i < adj_blocksize; i++) {
380 if (trailing_bytes == 1) {
387 void region_dot_product(
char **from_bufs,
char *to_buf,
int *matrix_row,
int num_entries,
int blocksize)
391 for (i = 0; i < num_entries; i++) {
392 int mult = matrix_row[i];
406 for (i = k; i < n; i++) {
407 memset(parity[i - k], 0, blocksize);
417 char **first_k_available = (
char**)malloc(
sizeof(
char*)*k);
419 for (i = 0, j = 0; j < k; i++) {
421 first_k_available[j] = i < k ? data[i] : parity[i - k];
425 return first_k_available;
430 int *decoding_matrix = NULL;
431 int *inverse_decoding_matrix = NULL;
432 char **first_k_available = NULL;
434 int *_missing = (
int*)malloc(
sizeof(
int)*n);
438 memset(_missing, 0,
sizeof(
int)*n);
440 while (missing[num_missing] > -1) {
441 _missing[missing[num_missing]] = 1;
445 if (num_missing > m) {
450 decoding_matrix = (
int*)malloc(
sizeof(
int)*k*k);
451 inverse_decoding_matrix = (
int*)malloc(
sizeof(
int)*k*k);
458 for (i = 0; i < k; i++) {
461 region_dot_product(first_k_available, data[i], &inverse_decoding_matrix[(i * k)], k, blocksize);
466 if (rebuild_parity) {
467 for (i = k; i < n; i++) {
475 free(decoding_matrix);
476 free(inverse_decoding_matrix);
477 free(first_k_available);
485 int *decoding_matrix = NULL;
486 int *inverse_decoding_matrix = NULL;
487 char **first_k_available = NULL;
488 int *parity_row = NULL;
490 int *_missing = (
int*)malloc(
sizeof(
int)*n);
494 memset(_missing, 0,
sizeof(
int)*n);
496 while (missing[num_missing] > -1) {
497 _missing[missing[num_missing]] = 1;
501 if (num_missing > m) {
506 decoding_matrix = (
int*)malloc(
sizeof(
int)*k*k);
507 inverse_decoding_matrix = (
int*)malloc(
sizeof(
int)*k*k);
515 if (destination_idx < k) {
516 region_dot_product(first_k_available, data[destination_idx], &inverse_decoding_matrix[(destination_idx * k)], k, blocksize);
523 parity_row = (
int*)malloc(
sizeof(
int)*k);
524 memset(parity_row, 0,
sizeof(
int)*k);
526 for (i = 0; i < k; i++) {
528 parity_row[j] = generator_matrix[(destination_idx * k) + i];
536 while (missing[i] > -1) {
537 if (missing[i] < k) {
538 for (j = 0; j < k; j++) {
539 parity_row[j] ^=
rs_galois_mult(generator_matrix[(destination_idx * k) + missing[i]], inverse_decoding_matrix[(missing[i] * k) + j]);
544 region_dot_product(first_k_available, parity[destination_idx - k], parity_row, k, blocksize);
547 free(decoding_matrix);
548 free(inverse_decoding_matrix);
549 free(first_k_available);