examples: reformat using new code style

Signed-off-by: Marcel Cornu <marcel.d.cornu@intel.com>
This commit is contained in:
Marcel Cornu 2024-04-19 17:09:13 +01:00 committed by Pablo de Lara
parent 300260a4d9
commit 9d99f8215d
3 changed files with 866 additions and 858 deletions

View File

@ -47,396 +47,412 @@
#include <immintrin.h> #include <immintrin.h>
#include <isa-l.h> #include <isa-l.h>
int verbose; // Global for tests int verbose; // Global for tests
#if defined (_MSC_VER) #if defined(_MSC_VER)
# define __builtin_parity(x) (__popcnt64(x) & 1) #define __builtin_parity(x) (__popcnt64(x) & 1)
#endif #endif
#if defined (__GNUC__) || defined (__clang__) #if defined(__GNUC__) || defined(__clang__)
# define ATTRIBUTE_TARGET(x) __attribute__((target(x))) #define ATTRIBUTE_TARGET(x) __attribute__((target(x)))
#else #else
# define ATTRIBUTE_TARGET(x) #define ATTRIBUTE_TARGET(x)
#endif #endif
struct crc64_desc { struct crc64_desc {
uint64_t poly; uint64_t poly;
uint64_t k5; uint64_t k5;
uint64_t k7; uint64_t k7;
uint64_t k8; uint64_t k8;
}; };
void gen_crc64_refl_consts(uint64_t poly, struct crc64_desc *c) void
gen_crc64_refl_consts(uint64_t poly, struct crc64_desc *c)
{ {
uint64_t quotienth = 0; uint64_t quotienth = 0;
uint64_t div; uint64_t div;
uint64_t rem = 1ull; uint64_t rem = 1ull;
int i; int i;
for (i = 0; i < 64; i++) { for (i = 0; i < 64; i++) {
div = (rem & 1ull) != 0; div = (rem & 1ull) != 0;
quotienth = (quotienth >> 1) | (div ? 0x8000000000000000ull : 0); quotienth = (quotienth >> 1) | (div ? 0x8000000000000000ull : 0);
rem = (div ? poly : 0) ^ (rem >> 1); rem = (div ? poly : 0) ^ (rem >> 1);
} }
c->k5 = rem; c->k5 = rem;
c->poly = poly; c->poly = poly;
c->k7 = quotienth; c->k7 = quotienth;
c->k8 = poly << 1; c->k8 = poly << 1;
} }
void gen_crc64_norm_consts(uint64_t poly, struct crc64_desc *c) void
gen_crc64_norm_consts(uint64_t poly, struct crc64_desc *c)
{ {
uint64_t quotientl = 0; uint64_t quotientl = 0;
uint64_t div; uint64_t div;
uint64_t rem = 1ull << 63; uint64_t rem = 1ull << 63;
int i; int i;
for (i = 0; i < 65; i++) { for (i = 0; i < 65; i++) {
div = (rem & 0x8000000000000000ull) != 0; div = (rem & 0x8000000000000000ull) != 0;
quotientl = (quotientl << 1) | div; quotientl = (quotientl << 1) | div;
rem = (div ? poly : 0) ^ (rem << 1); rem = (div ? poly : 0) ^ (rem << 1);
} }
c->poly = poly; c->poly = poly;
c->k5 = rem; c->k5 = rem;
c->k7 = quotientl; c->k7 = quotientl;
c->k8 = poly; c->k8 = poly;
} }
uint32_t calc_xi_mod(int n) uint32_t
calc_xi_mod(int n)
{ {
uint32_t rem = 0x1ul; uint32_t rem = 0x1ul;
int i, j; int i, j;
const uint32_t poly = 0x82f63b78; const uint32_t poly = 0x82f63b78;
if (n < 16) if (n < 16)
return 0; return 0;
for (i = 0; i < n - 8; i++) for (i = 0; i < n - 8; i++)
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
rem = (rem & 0x1ul) ? (rem >> 1) ^ poly : (rem >> 1); rem = (rem & 0x1ul) ? (rem >> 1) ^ poly : (rem >> 1);
return rem; return rem;
} }
uint64_t calc64_refl_xi_mod(int n, struct crc64_desc *c) uint64_t
calc64_refl_xi_mod(int n, struct crc64_desc *c)
{ {
uint64_t rem = 1ull; uint64_t rem = 1ull;
int i, j; int i, j;
const uint64_t poly = c->poly; const uint64_t poly = c->poly;
if (n < 32) if (n < 32)
return 0; return 0;
for (i = 0; i < n - 16; i++) for (i = 0; i < n - 16; i++)
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
rem = (rem & 0x1ull) ? (rem >> 1) ^ poly : (rem >> 1); rem = (rem & 0x1ull) ? (rem >> 1) ^ poly : (rem >> 1);
return rem; return rem;
} }
uint64_t calc64_norm_xi_mod(int n, struct crc64_desc *c) uint64_t
calc64_norm_xi_mod(int n, struct crc64_desc *c)
{ {
uint64_t rem = 1ull; uint64_t rem = 1ull;
int i, j; int i, j;
const uint64_t poly = c->poly; const uint64_t poly = c->poly;
if (n < 32) if (n < 32)
return 0; return 0;
for (i = 0; i < n - 8; i++) for (i = 0; i < n - 8; i++)
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
rem = (rem & 0x8000000000000000ull ? poly : 0) ^ (rem << 1); rem = (rem & 0x8000000000000000ull ? poly : 0) ^ (rem << 1);
return rem; return rem;
} }
// Base function for crc32_iscsi_shiftx() if c++ multi-function versioning // Base function for crc32_iscsi_shiftx() if c++ multi-function versioning
#ifdef __cplusplus #ifdef __cplusplus
static inline uint32_t bit_reverse32(uint32_t x) static inline uint32_t
bit_reverse32(uint32_t x)
{ {
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return ((x >> 16) | (x << 16)); return ((x >> 16) | (x << 16));
} }
// Base function for crc32_iscsi_shiftx without clmul optimizations // Base function for crc32_iscsi_shiftx without clmul optimizations
ATTRIBUTE_TARGET("default") ATTRIBUTE_TARGET("default")
uint32_t crc32_iscsi_shiftx(uint32_t crc1, uint32_t x) uint32_t
crc32_iscsi_shiftx(uint32_t crc1, uint32_t x)
{ {
int i; int i;
uint64_t xrev, q = 0; uint64_t xrev, q = 0;
union { union {
uint8_t a[8]; uint8_t a[8];
uint64_t q; uint64_t q;
} qu; } qu;
xrev = bit_reverse32(x); xrev = bit_reverse32(x);
xrev <<= 32; xrev <<= 32;
for (i = 0; i < 64; i++, xrev >>= 1) for (i = 0; i < 64; i++, xrev >>= 1)
q = (q << 1) | __builtin_parity(crc1 & xrev); q = (q << 1) | __builtin_parity(crc1 & xrev);
qu.q = q; qu.q = q;
return crc32_iscsi(qu.a, 8, 0); return crc32_iscsi(qu.a, 8, 0);
} }
#endif // cplusplus #endif // cplusplus
ATTRIBUTE_TARGET("pclmul,sse4.2") ATTRIBUTE_TARGET("pclmul,sse4.2")
uint32_t crc32_iscsi_shiftx(uint32_t crc1, uint32_t x) uint32_t
crc32_iscsi_shiftx(uint32_t crc1, uint32_t x)
{ {
__m128i crc1x, constx; __m128i crc1x, constx;
uint64_t crc64; uint64_t crc64;
crc1x = _mm_setr_epi32(crc1, 0, 0, 0); crc1x = _mm_setr_epi32(crc1, 0, 0, 0);
constx = _mm_setr_epi32(x, 0, 0, 0); constx = _mm_setr_epi32(x, 0, 0, 0);
crc1x = _mm_clmulepi64_si128(crc1x, constx, 0); crc1x = _mm_clmulepi64_si128(crc1x, constx, 0);
crc64 = _mm_cvtsi128_si64(crc1x); crc64 = _mm_cvtsi128_si64(crc1x);
crc64 = _mm_crc32_u64(0, crc64); crc64 = _mm_crc32_u64(0, crc64);
return crc64 & 0xffffffff; return crc64 & 0xffffffff;
} }
ATTRIBUTE_TARGET("pclmul,sse4.2") ATTRIBUTE_TARGET("pclmul,sse4.2")
uint64_t crc64_refl_shiftx(uint64_t crc1, uint64_t x, struct crc64_desc *c) uint64_t
crc64_refl_shiftx(uint64_t crc1, uint64_t x, struct crc64_desc *c)
{ {
__m128i crc1x, crc2x, crc3x, constx; __m128i crc1x, crc2x, crc3x, constx;
const __m128i rk5 = _mm_loadu_si64(&c->k5); const __m128i rk5 = _mm_loadu_si64(&c->k5);
const __m128i rk7 = _mm_loadu_si128((__m128i *) & c->k7); const __m128i rk7 = _mm_loadu_si128((__m128i *) &c->k7);
crc1x = _mm_cvtsi64_si128(crc1); crc1x = _mm_cvtsi64_si128(crc1);
constx = _mm_cvtsi64_si128(x); constx = _mm_cvtsi64_si128(x);
crc1x = _mm_clmulepi64_si128(crc1x, constx, 0x00); crc1x = _mm_clmulepi64_si128(crc1x, constx, 0x00);
// Fold to 64b // Fold to 64b
crc2x = _mm_clmulepi64_si128(crc1x, rk5, 0x00); crc2x = _mm_clmulepi64_si128(crc1x, rk5, 0x00);
crc3x = _mm_bsrli_si128(crc1x, 8); crc3x = _mm_bsrli_si128(crc1x, 8);
crc1x = _mm_xor_si128(crc2x, crc3x); crc1x = _mm_xor_si128(crc2x, crc3x);
// Reduce // Reduce
crc2x = _mm_clmulepi64_si128(crc1x, rk7, 0x00); crc2x = _mm_clmulepi64_si128(crc1x, rk7, 0x00);
crc3x = _mm_clmulepi64_si128(crc2x, rk7, 0x10); crc3x = _mm_clmulepi64_si128(crc2x, rk7, 0x10);
crc2x = _mm_bslli_si128(crc2x, 8); crc2x = _mm_bslli_si128(crc2x, 8);
crc1x = _mm_xor_si128(crc1x, crc2x); crc1x = _mm_xor_si128(crc1x, crc2x);
crc1x = _mm_xor_si128(crc1x, crc3x); crc1x = _mm_xor_si128(crc1x, crc3x);
return _mm_extract_epi64(crc1x, 1); return _mm_extract_epi64(crc1x, 1);
} }
ATTRIBUTE_TARGET("pclmul,sse4.2") ATTRIBUTE_TARGET("pclmul,sse4.2")
uint64_t crc64_norm_shiftx(uint64_t crc1, uint64_t x, struct crc64_desc *c) uint64_t
crc64_norm_shiftx(uint64_t crc1, uint64_t x, struct crc64_desc *c)
{ {
__m128i crc1x, crc2x, crc3x, constx; __m128i crc1x, crc2x, crc3x, constx;
const __m128i rk5 = _mm_loadu_si64(&c->k5); const __m128i rk5 = _mm_loadu_si64(&c->k5);
const __m128i rk7 = _mm_loadu_si128((__m128i *) & c->k7); const __m128i rk7 = _mm_loadu_si128((__m128i *) &c->k7);
crc1x = _mm_cvtsi64_si128(crc1); crc1x = _mm_cvtsi64_si128(crc1);
constx = _mm_cvtsi64_si128(x); constx = _mm_cvtsi64_si128(x);
crc1x = _mm_clmulepi64_si128(crc1x, constx, 0x00); crc1x = _mm_clmulepi64_si128(crc1x, constx, 0x00);
// Fold to 64b // Fold to 64b
crc2x = _mm_clmulepi64_si128(crc1x, rk5, 0x01); crc2x = _mm_clmulepi64_si128(crc1x, rk5, 0x01);
crc3x = _mm_bslli_si128(crc1x, 8); crc3x = _mm_bslli_si128(crc1x, 8);
crc1x = _mm_xor_si128(crc2x, crc3x); crc1x = _mm_xor_si128(crc2x, crc3x);
// Reduce // Reduce
crc2x = _mm_clmulepi64_si128(crc1x, rk7, 0x01); crc2x = _mm_clmulepi64_si128(crc1x, rk7, 0x01);
crc2x = _mm_xor_si128(crc1x, crc2x); crc2x = _mm_xor_si128(crc1x, crc2x);
crc3x = _mm_clmulepi64_si128(crc2x, rk7, 0x11); crc3x = _mm_clmulepi64_si128(crc2x, rk7, 0x11);
crc1x = _mm_xor_si128(crc1x, crc3x); crc1x = _mm_xor_si128(crc1x, crc3x);
return _mm_extract_epi64(crc1x, 0); return _mm_extract_epi64(crc1x, 0);
} }
uint32_t crc32_iscsi_combine_4k(uint32_t * crc_array, int n) uint32_t
crc32_iscsi_combine_4k(uint32_t *crc_array, int n)
{ {
const uint32_t cn4k = 0x82f89c77; //calc_xi_mod(4*1024); const uint32_t cn4k = 0x82f89c77; // calc_xi_mod(4*1024);
int i; int i;
if (n < 1) if (n < 1)
return 0; return 0;
uint32_t crc = crc_array[0]; uint32_t crc = crc_array[0];
for (i = 1; i < n; i++) for (i = 1; i < n; i++)
crc = crc32_iscsi_shiftx(crc, cn4k) ^ crc_array[i]; crc = crc32_iscsi_shiftx(crc, cn4k) ^ crc_array[i];
return crc; return crc;
} }
// Tests // Tests
#define printv(...) {if (verbose) printf(__VA_ARGS__); else printf(".");} #define printv(...) \
{ \
if (verbose) \
printf(__VA_ARGS__); \
else \
printf("."); \
}
uint64_t test_combine64(uint8_t * inp, size_t len, uint64_t poly, int reflected, uint64_t
uint64_t(*func) (uint64_t, const uint8_t *, uint64_t)) test_combine64(uint8_t *inp, size_t len, uint64_t poly, int reflected,
uint64_t (*func)(uint64_t, const uint8_t *, uint64_t))
{ {
uint64_t crc64_init, crc64, crc64a, crc64b; uint64_t crc64_init, crc64, crc64a, crc64b;
uint64_t crc64_1, crc64_2, crc64_3, crc64_n, err = 0; uint64_t crc64_1, crc64_2, crc64_3, crc64_n, err = 0;
uint64_t xi_mod; uint64_t xi_mod;
struct crc64_desc crc64_c; struct crc64_desc crc64_c;
size_t l1, l2, l3; size_t l1, l2, l3;
l1 = len / 2; l1 = len / 2;
l2 = len - l1; l2 = len - l1;
crc64_init = rand(); crc64_init = rand();
crc64 = func(crc64_init, inp, len); crc64 = func(crc64_init, inp, len);
printv("\ncrc64 all = 0x%" PRIx64 "\n", crc64); printv("\ncrc64 all = 0x%" PRIx64 "\n", crc64);
// Do a sequential crc update // Do a sequential crc update
crc64a = func(crc64_init, &inp[0], l1); crc64a = func(crc64_init, &inp[0], l1);
crc64b = func(crc64a, &inp[l1], l2); crc64b = func(crc64a, &inp[l1], l2);
printv("crc64 seq = 0x%" PRIx64 "\n", crc64b); printv("crc64 seq = 0x%" PRIx64 "\n", crc64b);
// Split into 2 independent crc calc and combine // Split into 2 independent crc calc and combine
crc64_1 = func(crc64_init, &inp[0], l1); crc64_1 = func(crc64_init, &inp[0], l1);
crc64_2 = func(0, &inp[l1], l2); crc64_2 = func(0, &inp[l1], l2);
if (reflected) { if (reflected) {
gen_crc64_refl_consts(poly, &crc64_c); gen_crc64_refl_consts(poly, &crc64_c);
xi_mod = calc64_refl_xi_mod(l1, &crc64_c); xi_mod = calc64_refl_xi_mod(l1, &crc64_c);
crc64_1 = crc64_refl_shiftx(crc64_1, xi_mod, &crc64_c); crc64_1 = crc64_refl_shiftx(crc64_1, xi_mod, &crc64_c);
} else { } else {
gen_crc64_norm_consts(poly, &crc64_c); gen_crc64_norm_consts(poly, &crc64_c);
xi_mod = calc64_norm_xi_mod(l1, &crc64_c); xi_mod = calc64_norm_xi_mod(l1, &crc64_c);
crc64_1 = crc64_norm_shiftx(crc64_1, xi_mod, &crc64_c); crc64_1 = crc64_norm_shiftx(crc64_1, xi_mod, &crc64_c);
} }
crc64_n = crc64_1 ^ crc64_2; crc64_n = crc64_1 ^ crc64_2;
printv("crc64 combined2 = 0x%" PRIx64 "\n", crc64_n); printv("crc64 combined2 = 0x%" PRIx64 "\n", crc64_n);
err |= crc64_n ^ crc64; err |= crc64_n ^ crc64;
if (err) if (err)
return err; return err;
// Split into 3 uneven segments and combine // Split into 3 uneven segments and combine
l1 = len / 3; l1 = len / 3;
l2 = (len / 3) - 3; l2 = (len / 3) - 3;
l3 = len - l2 - l1; l3 = len - l2 - l1;
crc64_1 = func(crc64_init, &inp[0], l1); crc64_1 = func(crc64_init, &inp[0], l1);
crc64_2 = func(0, &inp[l1], l2); crc64_2 = func(0, &inp[l1], l2);
crc64_3 = func(0, &inp[l1 + l2], l3); crc64_3 = func(0, &inp[l1 + l2], l3);
if (reflected) { if (reflected) {
xi_mod = calc64_refl_xi_mod(l3, &crc64_c); xi_mod = calc64_refl_xi_mod(l3, &crc64_c);
crc64_2 = crc64_refl_shiftx(crc64_2, xi_mod, &crc64_c); crc64_2 = crc64_refl_shiftx(crc64_2, xi_mod, &crc64_c);
xi_mod = calc64_refl_xi_mod(len - l1, &crc64_c); xi_mod = calc64_refl_xi_mod(len - l1, &crc64_c);
crc64_1 = crc64_refl_shiftx(crc64_1, xi_mod, &crc64_c); crc64_1 = crc64_refl_shiftx(crc64_1, xi_mod, &crc64_c);
} else { } else {
xi_mod = calc64_norm_xi_mod(l3, &crc64_c); xi_mod = calc64_norm_xi_mod(l3, &crc64_c);
crc64_2 = crc64_norm_shiftx(crc64_2, xi_mod, &crc64_c); crc64_2 = crc64_norm_shiftx(crc64_2, xi_mod, &crc64_c);
xi_mod = calc64_norm_xi_mod(len - l1, &crc64_c); xi_mod = calc64_norm_xi_mod(len - l1, &crc64_c);
crc64_1 = crc64_norm_shiftx(crc64_1, xi_mod, &crc64_c); crc64_1 = crc64_norm_shiftx(crc64_1, xi_mod, &crc64_c);
} }
crc64_n = crc64_1 ^ crc64_2 ^ crc64_3; crc64_n = crc64_1 ^ crc64_2 ^ crc64_3;
printv("crc64 combined3 = 0x%" PRIx64 "\n", crc64_n); printv("crc64 combined3 = 0x%" PRIx64 "\n", crc64_n);
err |= crc64_n ^ crc64; err |= crc64_n ^ crc64;
return err; return err;
} }
#define N (1024) #define N (1024)
#define B (2*N) #define B (2 * N)
#define T (3*N) #define T (3 * N)
#define N4k (4*1024) #define N4k (4 * 1024)
#define NMAX 32 #define NMAX 32
#define NMAX_SIZE (NMAX * N4k) #define NMAX_SIZE (NMAX * N4k)
int main(int argc, char *argv[]) int
main(int argc, char *argv[])
{ {
int i; int i;
uint32_t crc, crca, crcb, crc1, crc2, crc3, crcn; uint32_t crc, crca, crcb, crc1, crc2, crc3, crcn;
uint32_t crc_init = rand(); uint32_t crc_init = rand();
uint32_t err = 0; uint32_t err = 0;
uint8_t *inp = (uint8_t *) malloc(NMAX_SIZE); uint8_t *inp = (uint8_t *) malloc(NMAX_SIZE);
verbose = argc - 1; verbose = argc - 1;
if (NULL == inp) if (NULL == inp)
return -1; return -1;
for (int i = 0; i < NMAX_SIZE; i++) for (int i = 0; i < NMAX_SIZE; i++)
inp[i] = rand(); inp[i] = rand();
printf("crc_combine_test:"); printf("crc_combine_test:");
// Calc crc all at once // Calc crc all at once
crc = crc32_iscsi(inp, B, crc_init); crc = crc32_iscsi(inp, B, crc_init);
printv("\ncrcB all = 0x%" PRIx32 "\n", crc); printv("\ncrcB all = 0x%" PRIx32 "\n", crc);
// Do a sequential crc update // Do a sequential crc update
crca = crc32_iscsi(&inp[0], N, crc_init); crca = crc32_iscsi(&inp[0], N, crc_init);
crcb = crc32_iscsi(&inp[N], N, crca); crcb = crc32_iscsi(&inp[N], N, crca);
printv("crcB seq = 0x%" PRIx32 "\n", crcb); printv("crcB seq = 0x%" PRIx32 "\n", crcb);
// Split into 2 independent crc calc and combine // Split into 2 independent crc calc and combine
crc1 = crc32_iscsi(&inp[0], N, crc_init); crc1 = crc32_iscsi(&inp[0], N, crc_init);
crc2 = crc32_iscsi(&inp[N], N, 0); crc2 = crc32_iscsi(&inp[N], N, 0);
crcn = crc32_iscsi_shiftx(crc1, calc_xi_mod(N)) ^ crc2; crcn = crc32_iscsi_shiftx(crc1, calc_xi_mod(N)) ^ crc2;
printv("crcB combined2 = 0x%" PRIx32 "\n", crcn); printv("crcB combined2 = 0x%" PRIx32 "\n", crcn);
err |= crcn ^ crc; err |= crcn ^ crc;
// Split into 3 uneven segments and combine // Split into 3 uneven segments and combine
crc1 = crc32_iscsi(&inp[0], 100, crc_init); crc1 = crc32_iscsi(&inp[0], 100, crc_init);
crc2 = crc32_iscsi(&inp[100], 100, 0); crc2 = crc32_iscsi(&inp[100], 100, 0);
crc3 = crc32_iscsi(&inp[200], B - 200, 0); crc3 = crc32_iscsi(&inp[200], B - 200, 0);
crcn = crc3 ^ crcn = crc3 ^ crc32_iscsi_shiftx(crc2, calc_xi_mod(B - 200)) ^
crc32_iscsi_shiftx(crc2, calc_xi_mod(B - 200)) ^ crc32_iscsi_shiftx(crc1, calc_xi_mod(B - 100));
crc32_iscsi_shiftx(crc1, calc_xi_mod(B - 100)); printv("crcB combined3 = 0x%" PRIx32 "\n\n", crcn);
printv("crcB combined3 = 0x%" PRIx32 "\n\n", crcn); err |= crcn ^ crc;
err |= crcn ^ crc;
// Call all size T at once // Call all size T at once
crc = crc32_iscsi(inp, T, crc_init); crc = crc32_iscsi(inp, T, crc_init);
printv("crcT all = 0x%" PRIx32 "\n", crc); printv("crcT all = 0x%" PRIx32 "\n", crc);
// Split into 3 segments and combine with 2 consts // Split into 3 segments and combine with 2 consts
crc1 = crc32_iscsi(&inp[0], N, crc_init); crc1 = crc32_iscsi(&inp[0], N, crc_init);
crc2 = crc32_iscsi(&inp[N], N, 0); crc2 = crc32_iscsi(&inp[N], N, 0);
crc3 = crc32_iscsi(&inp[2 * N], N, 0); crc3 = crc32_iscsi(&inp[2 * N], N, 0);
crcn = crc3 ^ crcn = crc3 ^ crc32_iscsi_shiftx(crc2, calc_xi_mod(N)) ^
crc32_iscsi_shiftx(crc2, calc_xi_mod(N)) ^ crc32_iscsi_shiftx(crc1, calc_xi_mod(2 * N));
crc32_iscsi_shiftx(crc1, calc_xi_mod(2 * N)); printv("crcT combined3 = 0x%" PRIx32 "\n", crcn);
printv("crcT combined3 = 0x%" PRIx32 "\n", crcn); err |= crcn ^ crc;
err |= crcn ^ crc;
// Combine 3 segments with one const by sequential shift // Combine 3 segments with one const by sequential shift
uint32_t xi_mod_n = calc_xi_mod(N); uint32_t xi_mod_n = calc_xi_mod(N);
crcn = crc3 ^ crc32_iscsi_shiftx(crc32_iscsi_shiftx(crc1, xi_mod_n) crcn = crc3 ^ crc32_iscsi_shiftx(crc32_iscsi_shiftx(crc1, xi_mod_n) ^ crc2, xi_mod_n);
^ crc2, xi_mod_n); printv("crcT comb3 seq = 0x%" PRIx32 "\n\n", crcn);
printv("crcT comb3 seq = 0x%" PRIx32 "\n\n", crcn); err |= crcn ^ crc;
err |= crcn ^ crc;
// Test 4k array function // Test 4k array function
crc = crc32_iscsi(inp, NMAX_SIZE, crc_init); crc = crc32_iscsi(inp, NMAX_SIZE, crc_init);
printv("crc 4k x n all = 0x%" PRIx32 "\n", crc); printv("crc 4k x n all = 0x%" PRIx32 "\n", crc);
// Test crc 4k array combine function // Test crc 4k array combine function
uint32_t crcs[NMAX]; uint32_t crcs[NMAX];
crcs[0] = crc32_iscsi(&inp[0], N4k, crc_init); crcs[0] = crc32_iscsi(&inp[0], N4k, crc_init);
for (i = 1; i < NMAX; i++) for (i = 1; i < NMAX; i++)
crcs[i] = crc32_iscsi(&inp[i * N4k], N4k, 0); crcs[i] = crc32_iscsi(&inp[i * N4k], N4k, 0);
crcn = crc32_iscsi_combine_4k(crcs, NMAX); crcn = crc32_iscsi_combine_4k(crcs, NMAX);
printv("crc4k_array = 0x%" PRIx32 "\n", crcn); printv("crc4k_array = 0x%" PRIx32 "\n", crcn);
err |= crcn ^ crc; err |= crcn ^ crc;
// CRC64 generic poly tests - reflected // CRC64 generic poly tests - reflected
uint64_t len = NMAX_SIZE; uint64_t len = NMAX_SIZE;
err |= test_combine64(inp, len, 0xc96c5795d7870f42ull, 1, crc64_ecma_refl); err |= test_combine64(inp, len, 0xc96c5795d7870f42ull, 1, crc64_ecma_refl);
err |= test_combine64(inp, len, 0xd800000000000000ull, 1, crc64_iso_refl); err |= test_combine64(inp, len, 0xd800000000000000ull, 1, crc64_iso_refl);
err |= test_combine64(inp, len, 0x95ac9329ac4bc9b5ull, 1, crc64_jones_refl); err |= test_combine64(inp, len, 0x95ac9329ac4bc9b5ull, 1, crc64_jones_refl);
// CRC64 non-reflected polynomial tests // CRC64 non-reflected polynomial tests
err |= test_combine64(inp, len, 0x42f0e1eba9ea3693ull, 0, crc64_ecma_norm); err |= test_combine64(inp, len, 0x42f0e1eba9ea3693ull, 0, crc64_ecma_norm);
err |= test_combine64(inp, len, 0x000000000000001bull, 0, crc64_iso_norm); err |= test_combine64(inp, len, 0x000000000000001bull, 0, crc64_iso_norm);
err |= test_combine64(inp, len, 0xad93d23594c935a9ull, 0, crc64_jones_norm); err |= test_combine64(inp, len, 0xad93d23594c935a9ull, 0, crc64_jones_norm);
printf(err == 0 ? "pass\n" : "fail\n"); printf(err == 0 ? "pass\n" : "fail\n");
free(inp); free(inp);
return err; return err;
} }

View File

@ -31,7 +31,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "erasure_code.h" // use <isa-l.h> instead when linking against installed #include "erasure_code.h" // use <isa-l.h> instead when linking against installed
#include "test.h" #include "test.h"
#define MMAX 255 #define MMAX 255
@ -40,467 +40,463 @@
typedef unsigned char u8; typedef unsigned char u8;
int verbose = 0; int verbose = 0;
int usage(void) int
usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"Usage: ec_piggyback_example [options]\n" "Usage: ec_piggyback_example [options]\n"
" -h Help\n" " -h Help\n"
" -k <val> Number of source fragments\n" " -k <val> Number of source fragments\n"
" -p <val> Number of parity fragments\n" " -p <val> Number of parity fragments\n"
" -l <val> Length of fragments\n" " -l <val> Length of fragments\n"
" -e <val> Simulate erasure on frag index val. Zero based. Can be repeated.\n" " -e <val> Simulate erasure on frag index val. Zero based. Can be repeated.\n"
" -v Verbose\n" " -v Verbose\n"
" -b Run timed benchmark\n" " -b Run timed benchmark\n"
" -s Toggle use of sparse matrix opt\n" " -s Toggle use of sparse matrix opt\n"
" -r <seed> Pick random (k, p) with seed\n"); " -r <seed> Pick random (k, p) with seed\n");
exit(0); exit(0);
} }
// Cauchy-based matrix // Cauchy-based matrix
void gf_gen_full_pb_cauchy_matrix(u8 * a, int m, int k) void
gf_gen_full_pb_cauchy_matrix(u8 *a, int m, int k)
{ {
int i, j, p = m - k; int i, j, p = m - k;
// Identity matrix in top k x k to indicate a symmetric code // Identity matrix in top k x k to indicate a symmetric code
memset(a, 0, k * m); memset(a, 0, k * m);
for (i = 0; i < k; i++) for (i = 0; i < k; i++)
a[k * i + i] = 1; a[k * i + i] = 1;
for (i = k; i < (k + p / 2); i++) { for (i = k; i < (k + p / 2); i++) {
for (j = 0; j < k / 2; j++) for (j = 0; j < k / 2; j++)
a[k * i + j] = gf_inv(i ^ j); a[k * i + j] = gf_inv(i ^ j);
for (; j < k; j++) for (; j < k; j++)
a[k * i + j] = 0; a[k * i + j] = 0;
} }
for (; i < m; i++) { for (; i < m; i++) {
for (j = 0; j < k / 2; j++) for (j = 0; j < k / 2; j++)
a[k * i + j] = 0; a[k * i + j] = 0;
for (; j < k; j++) for (; j < k; j++)
a[k * i + j] = gf_inv((i - p / 2) ^ (j - k / 2)); a[k * i + j] = gf_inv((i - p / 2) ^ (j - k / 2));
} }
// Fill in mixture of B parity depending on a few localized A sources // Fill in mixture of B parity depending on a few localized A sources
int r = 0, c = 0; int r = 0, c = 0;
int repeat_len = k / (p - 2); int repeat_len = k / (p - 2);
int parity_rows = p / 2; int parity_rows = p / 2;
for (i = 1 + k + parity_rows; i < m; i++, r++) { for (i = 1 + k + parity_rows; i < m; i++, r++) {
if (r == (parity_rows - 1) - ((k / 2 % (parity_rows - 1)))) if (r == (parity_rows - 1) - ((k / 2 % (parity_rows - 1))))
repeat_len++; repeat_len++;
for (j = 0; j < repeat_len; j++, c++) for (j = 0; j < repeat_len; j++, c++)
a[k * i + c] = gf_inv((k + 1) ^ c); a[k * i + c] = gf_inv((k + 1) ^ c);
} }
} }
// Vandermonde based matrix - not recommended due to limits when invertable // Vandermonde based matrix - not recommended due to limits when invertable
void gf_gen_full_pb_vand_matrix(u8 * a, int m, int k) void
gf_gen_full_pb_vand_matrix(u8 *a, int m, int k)
{ {
int i, j, p = m - k; int i, j, p = m - k;
unsigned char q, gen = 1; unsigned char q, gen = 1;
// Identity matrix in top k x k to indicate a symmetric code // Identity matrix in top k x k to indicate a symmetric code
memset(a, 0, k * m); memset(a, 0, k * m);
for (i = 0; i < k; i++) for (i = 0; i < k; i++)
a[k * i + i] = 1; a[k * i + i] = 1;
for (i = k; i < (k + (p / 2)); i++) { for (i = k; i < (k + (p / 2)); i++) {
q = 1; q = 1;
for (j = 0; j < k / 2; j++) { for (j = 0; j < k / 2; j++) {
a[k * i + j] = q; a[k * i + j] = q;
q = gf_mul(q, gen); q = gf_mul(q, gen);
} }
for (; j < k; j++) for (; j < k; j++)
a[k * i + j] = 0; a[k * i + j] = 0;
gen = gf_mul(gen, 2); gen = gf_mul(gen, 2);
} }
gen = 1; gen = 1;
for (; i < m; i++) { for (; i < m; i++) {
q = 1; q = 1;
for (j = 0; j < k / 2; j++) { for (j = 0; j < k / 2; j++) {
a[k * i + j] = 0; a[k * i + j] = 0;
} }
for (; j < k; j++) { for (; j < k; j++) {
a[k * i + j] = q; a[k * i + j] = q;
q = gf_mul(q, gen); q = gf_mul(q, gen);
} }
gen = gf_mul(gen, 2); gen = gf_mul(gen, 2);
} }
// Fill in mixture of B parity depending on a few localized A sources // Fill in mixture of B parity depending on a few localized A sources
int r = 0, c = 0; int r = 0, c = 0;
int repeat_len = k / (p - 2); int repeat_len = k / (p - 2);
int parity_rows = p / 2; int parity_rows = p / 2;
for (i = 1 + k + parity_rows; i < m; i++, r++) { for (i = 1 + k + parity_rows; i < m; i++, r++) {
if (r == (parity_rows - 1) - ((k / 2 % (parity_rows - 1)))) if (r == (parity_rows - 1) - ((k / 2 % (parity_rows - 1))))
repeat_len++; repeat_len++;
for (j = 0; j < repeat_len; j++) for (j = 0; j < repeat_len; j++)
a[k * i + c++] = 1; a[k * i + c++] = 1;
} }
} }
void print_matrix(int m, int k, unsigned char *s, const char *msg) void
print_matrix(int m, int k, unsigned char *s, const char *msg)
{ {
int i, j; int i, j;
printf("%s:\n", msg); printf("%s:\n", msg);
for (i = 0; i < m; i++) { for (i = 0; i < m; i++) {
printf("%3d- ", i); printf("%3d- ", i);
for (j = 0; j < k; j++) { for (j = 0; j < k; j++) {
printf(" %2x", 0xff & s[j + (i * k)]); printf(" %2x", 0xff & s[j + (i * k)]);
} }
printf("\n"); printf("\n");
} }
printf("\n"); printf("\n");
} }
void print_list(int n, unsigned char *s, const char *msg) void
print_list(int n, unsigned char *s, const char *msg)
{ {
int i; int i;
if (!verbose) if (!verbose)
return; return;
printf("%s: ", msg); printf("%s: ", msg);
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
printf(" %d", s[i]); printf(" %d", s[i]);
printf("\n"); printf("\n");
} }
static int gf_gen_decode_matrix(u8 * encode_matrix, static int
u8 * decode_matrix, gf_gen_decode_matrix(u8 *encode_matrix, u8 *decode_matrix, u8 *invert_matrix, u8 *temp_matrix,
u8 * invert_matrix, u8 *decode_index, u8 *frag_err_list, int nerrs, int k, int m);
u8 * temp_matrix,
u8 * decode_index,
u8 * frag_err_list, int nerrs, int k, int m);
int main(int argc, char *argv[]) int
main(int argc, char *argv[])
{ {
int i, j, m, c, e, ret; int i, j, m, c, e, ret;
int k = 10, p = 4, len = 8 * 1024; // Default params int k = 10, p = 4, len = 8 * 1024; // Default params
int nerrs = 0; int nerrs = 0;
int benchmark = 0; int benchmark = 0;
int sparse_matrix_opt = 1; int sparse_matrix_opt = 1;
// Fragment buffer pointers // Fragment buffer pointers
u8 *frag_ptrs[MMAX]; u8 *frag_ptrs[MMAX];
u8 *parity_ptrs[KMAX]; u8 *parity_ptrs[KMAX];
u8 *recover_srcs[KMAX]; u8 *recover_srcs[KMAX];
u8 *recover_outp[KMAX]; u8 *recover_outp[KMAX];
u8 frag_err_list[MMAX]; u8 frag_err_list[MMAX];
// Coefficient matrices // Coefficient matrices
u8 *encode_matrix, *decode_matrix; u8 *encode_matrix, *decode_matrix;
u8 *invert_matrix, *temp_matrix; u8 *invert_matrix, *temp_matrix;
u8 *g_tbls; u8 *g_tbls;
u8 decode_index[MMAX]; u8 decode_index[MMAX];
if (argc == 1) if (argc == 1)
for (i = 0; i < p; i++) for (i = 0; i < p; i++)
frag_err_list[nerrs++] = rand() % (k + p); frag_err_list[nerrs++] = rand() % (k + p);
while ((c = getopt(argc, argv, "k:p:l:e:r:hvbs")) != -1) { while ((c = getopt(argc, argv, "k:p:l:e:r:hvbs")) != -1) {
switch (c) { switch (c) {
case 'k': case 'k':
k = atoi(optarg); k = atoi(optarg);
break; break;
case 'p': case 'p':
p = atoi(optarg); p = atoi(optarg);
break; break;
case 'l': case 'l':
len = atoi(optarg); len = atoi(optarg);
if (len < 0) if (len < 0)
usage(); usage();
break; break;
case 'e': case 'e':
e = atoi(optarg); e = atoi(optarg);
frag_err_list[nerrs++] = e; frag_err_list[nerrs++] = e;
break; break;
case 'r': case 'r':
srand(atoi(optarg)); srand(atoi(optarg));
k = (rand() % MMAX) / 4; k = (rand() % MMAX) / 4;
k = (k < 2) ? 2 : k; k = (k < 2) ? 2 : k;
p = (rand() % (MMAX - k)) / 4; p = (rand() % (MMAX - k)) / 4;
p = (p < 2) ? 2 : p; p = (p < 2) ? 2 : p;
for (i = 0; i < k && nerrs < p; i++) for (i = 0; i < k && nerrs < p; i++)
if (rand() & 1) if (rand() & 1)
frag_err_list[nerrs++] = i; frag_err_list[nerrs++] = i;
break; break;
case 'v': case 'v':
verbose++; verbose++;
break; break;
case 'b': case 'b':
benchmark = 1; benchmark = 1;
break; break;
case 's': case 's':
sparse_matrix_opt = !sparse_matrix_opt; sparse_matrix_opt = !sparse_matrix_opt;
break; break;
case 'h': case 'h':
default: default:
usage(); usage();
break; break;
} }
} }
m = k + p; m = k + p;
// Check for valid parameters // Check for valid parameters
if (m > (MMAX / 2) || k > (KMAX / 2) || m < 0 || p < 2 || k < 1) { if (m > (MMAX / 2) || k > (KMAX / 2) || m < 0 || p < 2 || k < 1) {
printf(" Input test parameter error m=%d, k=%d, p=%d, erasures=%d\n", printf(" Input test parameter error m=%d, k=%d, p=%d, erasures=%d\n", m, k, p,
m, k, p, nerrs); nerrs);
usage(); usage();
} }
if (nerrs > p) { if (nerrs > p) {
printf(" Number of erasures chosen exceeds power of code erasures=%d p=%d\n", printf(" Number of erasures chosen exceeds power of code erasures=%d p=%d\n", nerrs,
nerrs, p); p);
} }
for (i = 0; i < nerrs; i++) { for (i = 0; i < nerrs; i++) {
if (frag_err_list[i] >= m) if (frag_err_list[i] >= m)
printf(" fragment %d not in range\n", frag_err_list[i]); printf(" fragment %d not in range\n", frag_err_list[i]);
} }
printf("ec_piggyback_example:\n"); printf("ec_piggyback_example:\n");
/* /*
* One simple way to implement piggyback codes is to keep a 2x wide matrix * One simple way to implement piggyback codes is to keep a 2x wide matrix
* that covers the how each parity is related to both A and B sources. This * that covers the how each parity is related to both A and B sources. This
* keeps it easy to generalize in parameters m,k and the resulting sparse * keeps it easy to generalize in parameters m,k and the resulting sparse
* matrix multiplication can be optimized by pre-removal of zero items. * matrix multiplication can be optimized by pre-removal of zero items.
*/ */
int k2 = 2 * k; int k2 = 2 * k;
int p2 = 2 * p; int p2 = 2 * p;
int m2 = k2 + p2; int m2 = k2 + p2;
int nerrs2 = nerrs; int nerrs2 = nerrs;
encode_matrix = malloc(m2 * k2); encode_matrix = malloc(m2 * k2);
decode_matrix = malloc(m2 * k2); decode_matrix = malloc(m2 * k2);
invert_matrix = malloc(m2 * k2); invert_matrix = malloc(m2 * k2);
temp_matrix = malloc(m2 * k2); temp_matrix = malloc(m2 * k2);
g_tbls = malloc(k2 * p2 * 32); g_tbls = malloc(k2 * p2 * 32);
if (encode_matrix == NULL || decode_matrix == NULL if (encode_matrix == NULL || decode_matrix == NULL || invert_matrix == NULL ||
|| invert_matrix == NULL || temp_matrix == NULL || g_tbls == NULL) { temp_matrix == NULL || g_tbls == NULL) {
printf("Test failure! Error with malloc\n"); printf("Test failure! Error with malloc\n");
return -1; return -1;
} }
// Allocate the src fragments // Allocate the src fragments
for (i = 0; i < k; i++) { for (i = 0; i < k; i++) {
if (NULL == (frag_ptrs[i] = malloc(len))) { if (NULL == (frag_ptrs[i] = malloc(len))) {
printf("alloc error: Fail\n"); printf("alloc error: Fail\n");
return -1; return -1;
} }
} }
// Allocate the parity fragments // Allocate the parity fragments
for (i = 0; i < p2; i++) { for (i = 0; i < p2; i++) {
if (NULL == (parity_ptrs[i] = malloc(len / 2))) { if (NULL == (parity_ptrs[i] = malloc(len / 2))) {
printf("alloc error: Fail\n"); printf("alloc error: Fail\n");
return -1; return -1;
} }
} }
// Allocate buffers for recovered data // Allocate buffers for recovered data
for (i = 0; i < p2; i++) { for (i = 0; i < p2; i++) {
if (NULL == (recover_outp[i] = malloc(len / 2))) { if (NULL == (recover_outp[i] = malloc(len / 2))) {
printf("alloc error: Fail\n"); printf("alloc error: Fail\n");
return -1; return -1;
} }
} }
// Fill sources with random data // Fill sources with random data
for (i = 0; i < k; i++) for (i = 0; i < k; i++)
for (j = 0; j < len; j++) for (j = 0; j < len; j++)
frag_ptrs[i][j] = rand(); frag_ptrs[i][j] = rand();
printf(" encode (m,k,p)=(%d,%d,%d) len=%d\n", m, k, p, len); printf(" encode (m,k,p)=(%d,%d,%d) len=%d\n", m, k, p, len);
// Pick an encode matrix. // Pick an encode matrix.
gf_gen_full_pb_cauchy_matrix(encode_matrix, m2, k2); gf_gen_full_pb_cauchy_matrix(encode_matrix, m2, k2);
if (verbose) if (verbose)
print_matrix(m2, k2, encode_matrix, "encode matrix"); print_matrix(m2, k2, encode_matrix, "encode matrix");
// Initialize g_tbls from encode matrix // Initialize g_tbls from encode matrix
ec_init_tables(k2, p2, &encode_matrix[k2 * k2], g_tbls); ec_init_tables(k2, p2, &encode_matrix[k2 * k2], g_tbls);
// Fold A and B into single list of fragments // Fold A and B into single list of fragments
for (i = 0; i < k; i++) for (i = 0; i < k; i++)
frag_ptrs[i + k] = &frag_ptrs[i][len / 2]; frag_ptrs[i + k] = &frag_ptrs[i][len / 2];
if (!sparse_matrix_opt) { if (!sparse_matrix_opt) {
// Standard encode using no assumptions on the encode matrix // Standard encode using no assumptions on the encode matrix
// Generate EC parity blocks from sources // Generate EC parity blocks from sources
ec_encode_data(len / 2, k2, p2, g_tbls, frag_ptrs, parity_ptrs); ec_encode_data(len / 2, k2, p2, g_tbls, frag_ptrs, parity_ptrs);
if (benchmark) { if (benchmark) {
struct perf start; struct perf start;
BENCHMARK(&start, BENCHMARK_TIME, BENCHMARK(&start, BENCHMARK_TIME,
ec_encode_data(len / 2, k2, p2, g_tbls, frag_ptrs, ec_encode_data(len / 2, k2, p2, g_tbls, frag_ptrs, parity_ptrs));
parity_ptrs)); printf("ec_piggyback_encode_std: ");
printf("ec_piggyback_encode_std: "); perf_print(start, m2 * len / 2);
perf_print(start, m2 * len / 2); }
} } else {
} else { // Sparse matrix optimization - use fact that input matrix is sparse
// Sparse matrix optimization - use fact that input matrix is sparse
// Keep an encode matrix with some zero elements removed // Keep an encode matrix with some zero elements removed
u8 *encode_matrix_faster, *g_tbls_faster; u8 *encode_matrix_faster, *g_tbls_faster;
encode_matrix_faster = malloc(m * k); encode_matrix_faster = malloc(m * k);
g_tbls_faster = malloc(k * p * 32); g_tbls_faster = malloc(k * p * 32);
if (encode_matrix_faster == NULL || g_tbls_faster == NULL) { if (encode_matrix_faster == NULL || g_tbls_faster == NULL) {
printf("Test failure! Error with malloc\n"); printf("Test failure! Error with malloc\n");
return -1; return -1;
} }
/* /*
* Pack with only the part that we know are non-zero. Alternatively * Pack with only the part that we know are non-zero. Alternatively
* we could search and keep track of non-zero elements but for * we could search and keep track of non-zero elements but for
* simplicity we just skip the lower quadrant. * simplicity we just skip the lower quadrant.
*/ */
for (i = k, j = k2; i < m; i++, j++) for (i = k, j = k2; i < m; i++, j++)
memcpy(&encode_matrix_faster[k * i], &encode_matrix[k2 * j], k); memcpy(&encode_matrix_faster[k * i], &encode_matrix[k2 * j], k);
if (verbose) { if (verbose) {
print_matrix(p, k, &encode_matrix_faster[k * k], print_matrix(p, k, &encode_matrix_faster[k * k], "encode via sparse-opt");
"encode via sparse-opt"); print_matrix(p2 / 2, k2, &encode_matrix[(k2 + p2 / 2) * k2],
print_matrix(p2 / 2, k2, &encode_matrix[(k2 + p2 / 2) * k2], "encode via sparse-opt");
"encode via sparse-opt"); }
} // Initialize g_tbls from encode matrix
// Initialize g_tbls from encode matrix ec_init_tables(k, p, &encode_matrix_faster[k * k], g_tbls_faster);
ec_init_tables(k, p, &encode_matrix_faster[k * k], g_tbls_faster);
// Generate EC parity blocks from sources // Generate EC parity blocks from sources
ec_encode_data(len / 2, k, p, g_tbls_faster, frag_ptrs, parity_ptrs); ec_encode_data(len / 2, k, p, g_tbls_faster, frag_ptrs, parity_ptrs);
ec_encode_data(len / 2, k2, p, &g_tbls[k2 * p * 32], frag_ptrs, ec_encode_data(len / 2, k2, p, &g_tbls[k2 * p * 32], frag_ptrs, &parity_ptrs[p]);
&parity_ptrs[p]);
if (benchmark) { if (benchmark) {
struct perf start; struct perf start;
BENCHMARK(&start, BENCHMARK_TIME, BENCHMARK(&start, BENCHMARK_TIME,
ec_encode_data(len / 2, k, p, g_tbls_faster, frag_ptrs, ec_encode_data(len / 2, k, p, g_tbls_faster, frag_ptrs,
parity_ptrs); parity_ptrs);
ec_encode_data(len / 2, k2, p, &g_tbls[k2 * p * 32], ec_encode_data(len / 2, k2, p, &g_tbls[k2 * p * 32], frag_ptrs,
frag_ptrs, &parity_ptrs[p])); &parity_ptrs[p]));
printf("ec_piggyback_encode_sparse: "); printf("ec_piggyback_encode_sparse: ");
perf_print(start, m2 * len / 2); perf_print(start, m2 * len / 2);
} }
} }
if (nerrs <= 0) if (nerrs <= 0)
return 0; return 0;
printf(" recover %d fragments\n", nerrs); printf(" recover %d fragments\n", nerrs);
// Set frag pointers to correspond to parity // Set frag pointers to correspond to parity
for (i = k2; i < m2; i++) for (i = k2; i < m2; i++)
frag_ptrs[i] = parity_ptrs[i - k2]; frag_ptrs[i] = parity_ptrs[i - k2];
print_list(nerrs2, frag_err_list, " frag err list"); print_list(nerrs2, frag_err_list, " frag err list");
// Find a decode matrix to regenerate all erasures from remaining frags // Find a decode matrix to regenerate all erasures from remaining frags
ret = gf_gen_decode_matrix(encode_matrix, decode_matrix, ret = gf_gen_decode_matrix(encode_matrix, decode_matrix, invert_matrix, temp_matrix,
invert_matrix, temp_matrix, decode_index, frag_err_list, decode_index, frag_err_list, nerrs2, k2, m2);
nerrs2, k2, m2);
if (ret != 0) { if (ret != 0) {
printf("Fail on generate decode matrix\n"); printf("Fail on generate decode matrix\n");
return -1; return -1;
} }
// Pack recovery array pointers as list of valid fragments // Pack recovery array pointers as list of valid fragments
for (i = 0; i < k2; i++) for (i = 0; i < k2; i++)
if (decode_index[i] < k2) if (decode_index[i] < k2)
recover_srcs[i] = frag_ptrs[decode_index[i]]; recover_srcs[i] = frag_ptrs[decode_index[i]];
else else
recover_srcs[i] = parity_ptrs[decode_index[i] - k2]; recover_srcs[i] = parity_ptrs[decode_index[i] - k2];
print_list(k2, decode_index, " decode index"); print_list(k2, decode_index, " decode index");
// Recover data // Recover data
ec_init_tables(k2, nerrs2, decode_matrix, g_tbls); ec_init_tables(k2, nerrs2, decode_matrix, g_tbls);
ec_encode_data(len / 2, k2, nerrs2, g_tbls, recover_srcs, recover_outp); ec_encode_data(len / 2, k2, nerrs2, g_tbls, recover_srcs, recover_outp);
if (benchmark) { if (benchmark) {
struct perf start; struct perf start;
BENCHMARK(&start, BENCHMARK_TIME, BENCHMARK(&start, BENCHMARK_TIME,
ec_encode_data(len / 2, k2, nerrs2, g_tbls, recover_srcs, ec_encode_data(len / 2, k2, nerrs2, g_tbls, recover_srcs, recover_outp));
recover_outp)); printf("ec_piggyback_decode: ");
printf("ec_piggyback_decode: "); perf_print(start, (k2 + nerrs2) * len / 2);
perf_print(start, (k2 + nerrs2) * len / 2); }
} // Check that recovered buffers are the same as original
// Check that recovered buffers are the same as original printf(" check recovery of block {");
printf(" check recovery of block {"); for (i = 0; i < nerrs2; i++) {
for (i = 0; i < nerrs2; i++) { printf(" %d", frag_err_list[i]);
printf(" %d", frag_err_list[i]); if (memcmp(recover_outp[i], frag_ptrs[frag_err_list[i]], len / 2)) {
if (memcmp(recover_outp[i], frag_ptrs[frag_err_list[i]], len / 2)) { printf(" Fail erasure recovery %d, frag %d\n", i, frag_err_list[i]);
printf(" Fail erasure recovery %d, frag %d\n", i, frag_err_list[i]); return -1;
return -1; }
} }
} printf(" } done all: Pass\n");
printf(" } done all: Pass\n");
return 0; return 0;
} }
// Generate decode matrix from encode matrix and erasure list // Generate decode matrix from encode matrix and erasure list
static int gf_gen_decode_matrix(u8 * encode_matrix, static int
u8 * decode_matrix, gf_gen_decode_matrix(u8 *encode_matrix, u8 *decode_matrix, u8 *invert_matrix, u8 *temp_matrix,
u8 * invert_matrix, u8 *decode_index, u8 *frag_err_list, int nerrs, int k, int m)
u8 * temp_matrix,
u8 * decode_index, u8 * frag_err_list, int nerrs, int k, int m)
{ {
int i, j, p, r; int i, j, p, r;
int nsrcerrs = 0; int nsrcerrs = 0;
u8 s, *b = temp_matrix; u8 s, *b = temp_matrix;
u8 frag_in_err[MMAX]; u8 frag_in_err[MMAX];
memset(frag_in_err, 0, sizeof(frag_in_err)); memset(frag_in_err, 0, sizeof(frag_in_err));
// Order the fragments in erasure for easier sorting // Order the fragments in erasure for easier sorting
for (i = 0; i < nerrs; i++) { for (i = 0; i < nerrs; i++) {
if (frag_err_list[i] < k) if (frag_err_list[i] < k)
nsrcerrs++; nsrcerrs++;
frag_in_err[frag_err_list[i]] = 1; frag_in_err[frag_err_list[i]] = 1;
} }
// Construct b (matrix that encoded remaining frags) by removing erased rows // Construct b (matrix that encoded remaining frags) by removing erased rows
for (i = 0, r = 0; i < k; i++, r++) { for (i = 0, r = 0; i < k; i++, r++) {
while (frag_in_err[r]) while (frag_in_err[r])
r++; r++;
for (j = 0; j < k; j++) for (j = 0; j < k; j++)
b[k * i + j] = encode_matrix[k * r + j]; b[k * i + j] = encode_matrix[k * r + j];
decode_index[i] = r; decode_index[i] = r;
} }
if (verbose > 1) if (verbose > 1)
print_matrix(k, k, b, "matrix to invert"); print_matrix(k, k, b, "matrix to invert");
// Invert matrix to get recovery matrix // Invert matrix to get recovery matrix
if (gf_invert_matrix(b, invert_matrix, k) < 0) if (gf_invert_matrix(b, invert_matrix, k) < 0)
return -1; return -1;
if (verbose > 2) if (verbose > 2)
print_matrix(k, k, invert_matrix, "matrix inverted"); print_matrix(k, k, invert_matrix, "matrix inverted");
// Get decode matrix with only wanted recovery rows // Get decode matrix with only wanted recovery rows
for (i = 0; i < nsrcerrs; i++) { for (i = 0; i < nsrcerrs; i++) {
for (j = 0; j < k; j++) { for (j = 0; j < k; j++) {
decode_matrix[k * i + j] = invert_matrix[k * frag_err_list[i] + j]; decode_matrix[k * i + j] = invert_matrix[k * frag_err_list[i] + j];
} }
} }
// For non-src (parity) erasures need to multiply encode matrix * invert // For non-src (parity) erasures need to multiply encode matrix * invert
for (p = nsrcerrs; p < nerrs; p++) { for (p = nsrcerrs; p < nerrs; p++) {
for (i = 0; i < k; i++) { for (i = 0; i < k; i++) {
s = 0; s = 0;
for (j = 0; j < k; j++) for (j = 0; j < k; j++)
s ^= gf_mul(invert_matrix[j * k + i], s ^= gf_mul(invert_matrix[j * k + i],
encode_matrix[k * frag_err_list[p] + j]); encode_matrix[k * frag_err_list[p] + j]);
decode_matrix[k * p + i] = s; decode_matrix[k * p + i] = s;
} }
} }
if (verbose > 1) if (verbose > 1)
print_matrix(nerrs, k, decode_matrix, "decode matrix"); print_matrix(nerrs, k, decode_matrix, "decode matrix");
return 0; return 0;
} }

View File

@ -31,187 +31,186 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "erasure_code.h" // use <isa-l.h> instead when linking against installed #include "erasure_code.h" // use <isa-l.h> instead when linking against installed
#define MMAX 255 #define MMAX 255
#define KMAX 255 #define KMAX 255
typedef unsigned char u8; typedef unsigned char u8;
int usage(void) int
usage(void)
{ {
fprintf(stderr, fprintf(stderr,
"Usage: ec_simple_example [options]\n" "Usage: ec_simple_example [options]\n"
" -h Help\n" " -h Help\n"
" -k <val> Number of source fragments\n" " -k <val> Number of source fragments\n"
" -p <val> Number of parity fragments\n" " -p <val> Number of parity fragments\n"
" -l <val> Length of fragments\n" " -l <val> Length of fragments\n"
" -e <val> Simulate erasure on frag index val. Zero based. Can be repeated.\n" " -e <val> Simulate erasure on frag index val. Zero based. Can be repeated.\n"
" -r <seed> Pick random (k, p) with seed\n"); " -r <seed> Pick random (k, p) with seed\n");
exit(0); exit(0);
} }
static int gf_gen_decode_matrix_simple(u8 * encode_matrix, static int
u8 * decode_matrix, gf_gen_decode_matrix_simple(u8 *encode_matrix, u8 *decode_matrix, u8 *invert_matrix,
u8 * invert_matrix, u8 *temp_matrix, u8 *decode_index, u8 *frag_err_list, int nerrs, int k,
u8 * temp_matrix, int m);
u8 * decode_index,
u8 * frag_err_list, int nerrs, int k, int m);
int main(int argc, char *argv[]) int
main(int argc, char *argv[])
{ {
int i, j, m, c, e, ret; int i, j, m, c, e, ret;
int k = 10, p = 4, len = 8 * 1024; // Default params int k = 10, p = 4, len = 8 * 1024; // Default params
int nerrs = 0; int nerrs = 0;
// Fragment buffer pointers // Fragment buffer pointers
u8 *frag_ptrs[MMAX]; u8 *frag_ptrs[MMAX];
u8 *recover_srcs[KMAX]; u8 *recover_srcs[KMAX];
u8 *recover_outp[KMAX]; u8 *recover_outp[KMAX];
u8 frag_err_list[MMAX]; u8 frag_err_list[MMAX];
// Coefficient matrices // Coefficient matrices
u8 *encode_matrix, *decode_matrix; u8 *encode_matrix, *decode_matrix;
u8 *invert_matrix, *temp_matrix; u8 *invert_matrix, *temp_matrix;
u8 *g_tbls; u8 *g_tbls;
u8 decode_index[MMAX]; u8 decode_index[MMAX];
if (argc == 1) if (argc == 1)
for (i = 0; i < p; i++) for (i = 0; i < p; i++)
frag_err_list[nerrs++] = rand() % (k + p); frag_err_list[nerrs++] = rand() % (k + p);
while ((c = getopt(argc, argv, "k:p:l:e:r:h")) != -1) { while ((c = getopt(argc, argv, "k:p:l:e:r:h")) != -1) {
switch (c) { switch (c) {
case 'k': case 'k':
k = atoi(optarg); k = atoi(optarg);
break; break;
case 'p': case 'p':
p = atoi(optarg); p = atoi(optarg);
break; break;
case 'l': case 'l':
len = atoi(optarg); len = atoi(optarg);
if (len < 0) if (len < 0)
usage(); usage();
break; break;
case 'e': case 'e':
e = atoi(optarg); e = atoi(optarg);
frag_err_list[nerrs++] = e; frag_err_list[nerrs++] = e;
break; break;
case 'r': case 'r':
srand(atoi(optarg)); srand(atoi(optarg));
k = (rand() % (MMAX - 1)) + 1; // Pick k {1 to MMAX - 1} k = (rand() % (MMAX - 1)) + 1; // Pick k {1 to MMAX - 1}
p = (rand() % (MMAX - k)) + 1; // Pick p {1 to MMAX - k} p = (rand() % (MMAX - k)) + 1; // Pick p {1 to MMAX - k}
for (i = 0; i < k + p && nerrs < p; i++) for (i = 0; i < k + p && nerrs < p; i++)
if (rand() & 1) if (rand() & 1)
frag_err_list[nerrs++] = i; frag_err_list[nerrs++] = i;
break; break;
case 'h': case 'h':
default: default:
usage(); usage();
break; break;
} }
} }
m = k + p; m = k + p;
// Check for valid parameters // Check for valid parameters
if (m > MMAX || k > KMAX || m < 0 || p < 1 || k < 1) { if (m > MMAX || k > KMAX || m < 0 || p < 1 || k < 1) {
printf(" Input test parameter error m=%d, k=%d, p=%d, erasures=%d\n", printf(" Input test parameter error m=%d, k=%d, p=%d, erasures=%d\n", m, k, p,
m, k, p, nerrs); nerrs);
usage(); usage();
} }
if (nerrs > p) { if (nerrs > p) {
printf(" Number of erasures chosen exceeds power of code erasures=%d p=%d\n", printf(" Number of erasures chosen exceeds power of code erasures=%d p=%d\n", nerrs,
nerrs, p); p);
usage(); usage();
} }
for (i = 0; i < nerrs; i++) { for (i = 0; i < nerrs; i++) {
if (frag_err_list[i] >= m) { if (frag_err_list[i] >= m) {
printf(" fragment %d not in range\n", frag_err_list[i]); printf(" fragment %d not in range\n", frag_err_list[i]);
usage(); usage();
} }
} }
printf("ec_simple_example:\n"); printf("ec_simple_example:\n");
// Allocate coding matrices // Allocate coding matrices
encode_matrix = malloc(m * k); encode_matrix = malloc(m * k);
decode_matrix = malloc(m * k); decode_matrix = malloc(m * k);
invert_matrix = malloc(m * k); invert_matrix = malloc(m * k);
temp_matrix = malloc(m * k); temp_matrix = malloc(m * k);
g_tbls = malloc(k * p * 32); g_tbls = malloc(k * p * 32);
if (encode_matrix == NULL || decode_matrix == NULL if (encode_matrix == NULL || decode_matrix == NULL || invert_matrix == NULL ||
|| invert_matrix == NULL || temp_matrix == NULL || g_tbls == NULL) { temp_matrix == NULL || g_tbls == NULL) {
printf("Test failure! Error with malloc\n"); printf("Test failure! Error with malloc\n");
return -1; return -1;
} }
// Allocate the src & parity buffers // Allocate the src & parity buffers
for (i = 0; i < m; i++) { for (i = 0; i < m; i++) {
if (NULL == (frag_ptrs[i] = malloc(len))) { if (NULL == (frag_ptrs[i] = malloc(len))) {
printf("alloc error: Fail\n"); printf("alloc error: Fail\n");
return -1; return -1;
} }
} }
// Allocate buffers for recovered data // Allocate buffers for recovered data
for (i = 0; i < p; i++) { for (i = 0; i < p; i++) {
if (NULL == (recover_outp[i] = malloc(len))) { if (NULL == (recover_outp[i] = malloc(len))) {
printf("alloc error: Fail\n"); printf("alloc error: Fail\n");
return -1; return -1;
} }
} }
// Fill sources with random data // Fill sources with random data
for (i = 0; i < k; i++) for (i = 0; i < k; i++)
for (j = 0; j < len; j++) for (j = 0; j < len; j++)
frag_ptrs[i][j] = rand(); frag_ptrs[i][j] = rand();
printf(" encode (m,k,p)=(%d,%d,%d) len=%d\n", m, k, p, len); printf(" encode (m,k,p)=(%d,%d,%d) len=%d\n", m, k, p, len);
// Pick an encode matrix. A Cauchy matrix is a good choice as even // Pick an encode matrix. A Cauchy matrix is a good choice as even
// large k are always invertable keeping the recovery rule simple. // large k are always invertable keeping the recovery rule simple.
gf_gen_cauchy1_matrix(encode_matrix, m, k); gf_gen_cauchy1_matrix(encode_matrix, m, k);
// Initialize g_tbls from encode matrix // Initialize g_tbls from encode matrix
ec_init_tables(k, p, &encode_matrix[k * k], g_tbls); ec_init_tables(k, p, &encode_matrix[k * k], g_tbls);
// Generate EC parity blocks from sources // Generate EC parity blocks from sources
ec_encode_data(len, k, p, g_tbls, frag_ptrs, &frag_ptrs[k]); ec_encode_data(len, k, p, g_tbls, frag_ptrs, &frag_ptrs[k]);
if (nerrs <= 0) if (nerrs <= 0)
return 0; return 0;
printf(" recover %d fragments\n", nerrs); printf(" recover %d fragments\n", nerrs);
// Find a decode matrix to regenerate all erasures from remaining frags // Find a decode matrix to regenerate all erasures from remaining frags
ret = gf_gen_decode_matrix_simple(encode_matrix, decode_matrix, ret = gf_gen_decode_matrix_simple(encode_matrix, decode_matrix, invert_matrix, temp_matrix,
invert_matrix, temp_matrix, decode_index, decode_index, frag_err_list, nerrs, k, m);
frag_err_list, nerrs, k, m); if (ret != 0) {
if (ret != 0) { printf("Fail on generate decode matrix\n");
printf("Fail on generate decode matrix\n"); return -1;
return -1; }
} // Pack recovery array pointers as list of valid fragments
// Pack recovery array pointers as list of valid fragments for (i = 0; i < k; i++)
for (i = 0; i < k; i++) recover_srcs[i] = frag_ptrs[decode_index[i]];
recover_srcs[i] = frag_ptrs[decode_index[i]];
// Recover data // Recover data
ec_init_tables(k, nerrs, decode_matrix, g_tbls); ec_init_tables(k, nerrs, decode_matrix, g_tbls);
ec_encode_data(len, k, nerrs, g_tbls, recover_srcs, recover_outp); ec_encode_data(len, k, nerrs, g_tbls, recover_srcs, recover_outp);
// Check that recovered buffers are the same as original // Check that recovered buffers are the same as original
printf(" check recovery of block {"); printf(" check recovery of block {");
for (i = 0; i < nerrs; i++) { for (i = 0; i < nerrs; i++) {
printf(" %d", frag_err_list[i]); printf(" %d", frag_err_list[i]);
if (memcmp(recover_outp[i], frag_ptrs[frag_err_list[i]], len)) { if (memcmp(recover_outp[i], frag_ptrs[frag_err_list[i]], len)) {
printf(" Fail erasure recovery %d, frag %d\n", i, frag_err_list[i]); printf(" Fail erasure recovery %d, frag %d\n", i, frag_err_list[i]);
return -1; return -1;
} }
} }
printf(" } done all: Pass\n"); printf(" } done all: Pass\n");
return 0; return 0;
} }
/* /*
@ -219,59 +218,56 @@ int main(int argc, char *argv[])
* *
*/ */
static int gf_gen_decode_matrix_simple(u8 * encode_matrix, static int
u8 * decode_matrix, gf_gen_decode_matrix_simple(u8 *encode_matrix, u8 *decode_matrix, u8 *invert_matrix,
u8 * invert_matrix, u8 *temp_matrix, u8 *decode_index, u8 *frag_err_list, int nerrs, int k,
u8 * temp_matrix, int m)
u8 * decode_index, u8 * frag_err_list, int nerrs, int k,
int m)
{ {
int i, j, p, r; int i, j, p, r;
int nsrcerrs = 0; int nsrcerrs = 0;
u8 s, *b = temp_matrix; u8 s, *b = temp_matrix;
u8 frag_in_err[MMAX]; u8 frag_in_err[MMAX];
memset(frag_in_err, 0, sizeof(frag_in_err)); memset(frag_in_err, 0, sizeof(frag_in_err));
// Order the fragments in erasure for easier sorting // Order the fragments in erasure for easier sorting
for (i = 0; i < nerrs; i++) { for (i = 0; i < nerrs; i++) {
if (frag_err_list[i] < k) if (frag_err_list[i] < k)
nsrcerrs++; nsrcerrs++;
frag_in_err[frag_err_list[i]] = 1; frag_in_err[frag_err_list[i]] = 1;
} }
// Construct b (matrix that encoded remaining frags) by removing erased rows // Construct b (matrix that encoded remaining frags) by removing erased rows
for (i = 0, r = 0; i < k; i++, r++) { for (i = 0, r = 0; i < k; i++, r++) {
while (frag_in_err[r]) while (frag_in_err[r])
r++; r++;
for (j = 0; j < k; j++) for (j = 0; j < k; j++)
b[k * i + j] = encode_matrix[k * r + j]; b[k * i + j] = encode_matrix[k * r + j];
decode_index[i] = r; decode_index[i] = r;
} }
// Invert matrix to get recovery matrix // Invert matrix to get recovery matrix
if (gf_invert_matrix(b, invert_matrix, k) < 0) if (gf_invert_matrix(b, invert_matrix, k) < 0)
return -1; return -1;
// Get decode matrix with only wanted recovery rows // Get decode matrix with only wanted recovery rows
for (i = 0; i < nerrs; i++) { for (i = 0; i < nerrs; i++) {
if (frag_err_list[i] < k) // A src err if (frag_err_list[i] < k) // A src err
for (j = 0; j < k; j++) for (j = 0; j < k; j++)
decode_matrix[k * i + j] = decode_matrix[k * i + j] = invert_matrix[k * frag_err_list[i] + j];
invert_matrix[k * frag_err_list[i] + j]; }
}
// For non-src (parity) erasures need to multiply encode matrix * invert // For non-src (parity) erasures need to multiply encode matrix * invert
for (p = 0; p < nerrs; p++) { for (p = 0; p < nerrs; p++) {
if (frag_err_list[p] >= k) { // A parity err if (frag_err_list[p] >= k) { // A parity err
for (i = 0; i < k; i++) { for (i = 0; i < k; i++) {
s = 0; s = 0;
for (j = 0; j < k; j++) for (j = 0; j < k; j++)
s ^= gf_mul(invert_matrix[j * k + i], s ^= gf_mul(invert_matrix[j * k + i],
encode_matrix[k * frag_err_list[p] + j]); encode_matrix[k * frag_err_list[p] + j]);
decode_matrix[k * p + i] = s; decode_matrix[k * p + i] = s;
} }
} }
} }
return 0; return 0;
} }