25 # ifndef DYNAMIC_CRC_TABLE
26 # define DYNAMIC_CRC_TABLE
60 # error N must be in 1..6
82 # if defined(__x86_64__) || defined(__aarch64__)
90 # if W == 8 && defined(Z_U8)
91 typedef Z_U8 z_word_t;
95 typedef Z_U4 z_word_t;
106 #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
110 #if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
117 local z_word_t byte_swap(word)
122 (word & 0xff00000000000000) >> 56 |
123 (word & 0xff000000000000) >> 40 |
124 (word & 0xff0000000000) >> 24 |
125 (word & 0xff00000000) >> 8 |
126 (word & 0xff000000) << 8 |
127 (word & 0xff0000) << 24 |
128 (word & 0xff00) << 40 |
132 (word & 0xff000000) >> 24 |
133 (word & 0xff0000) >> 8 |
134 (word & 0xff00) << 8 |
141 #define POLY 0xedb88320
143 #ifdef DYNAMIC_CRC_TABLE
147 local void make_crc_table
OF((
void));
149 local z_word_t
FAR crc_big_table[256];
151 local z_word_t
FAR crc_braid_big_table[
W][256];
152 local void braid
OF((
z_crc_t [][256], z_word_t [][256],
int,
int));
156 local void write_table32hi
OF((FILE *,
const z_word_t
FAR *,
int));
157 local void write_table64
OF((FILE *,
const z_word_t
FAR *,
int));
169 typedef struct once_s once_t;
170 local void once
OF((once_t *,
void (*)(
void)));
173 #if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \
174 !defined(__STDC_NO_ATOMICS__)
176 #include <stdatomic.h>
183 #define ONCE_INIT {ATOMIC_FLAG_INIT, 0}
194 if (!atomic_load(&
state->done)) {
195 if (atomic_flag_test_and_set(&
state->begun))
196 while (!atomic_load(&
state->done))
200 atomic_store(&
state->done, 1);
212 #define ONCE_INIT {0, 0}
216 local int test_and_set
OF((
int volatile *));
217 local int test_and_set(flag)
233 if (test_and_set(&
state->begun))
246 local once_t made = ONCE_INIT;
273 local void make_crc_table()
279 for (
i = 0;
i < 256;
i++) {
281 for (j = 0; j < 8; j++)
282 p = p & 1 ? (p >> 1) ^
POLY : p >> 1;
285 crc_big_table[
i] = byte_swap(p);
292 for (
n = 1;
n < 32;
n++)
297 braid(crc_braid_table, crc_braid_big_table,
N,
W);
308 #if !defined(W) || W != 8
309 # error Need a 64-bit integer type in order to generate crc32.h.
314 z_word_t big[8][256];
316 out = fopen(
"crc32.h",
"w");
321 "/* crc32.h -- tables for rapid CRC calculation\n"
322 " * Generated automatically by crc32.c\n */\n"
324 "local const z_crc_t FAR crc_table[] = {\n"
337 "local const z_word_t FAR crc_big_table[] = {\n"
339 write_table64(
out, crc_big_table, 256);
346 "#else /* W == 4 */\n"
348 "local const z_word_t FAR crc_big_table[] = {\n"
350 write_table32hi(
out, crc_big_table, 256);
357 for (
n = 1;
n <= 6;
n++) {
363 braid(ltl, big,
n, 8);
370 "local const z_crc_t FAR crc_braid_table[][256] = {\n");
371 for (k = 0; k < 8; k++) {
373 write_table(
out, ltl[k], 256);
374 fprintf(
out,
"}%s", k < 7 ?
",\n" :
"");
379 "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
380 for (k = 0; k < 8; k++) {
382 write_table64(
out, big[k], 256);
383 fprintf(
out,
"}%s", k < 7 ?
",\n" :
"");
389 braid(ltl, big,
n, 4);
394 "#else /* W == 4 */\n"
396 "local const z_crc_t FAR crc_braid_table[][256] = {\n");
397 for (k = 0; k < 4; k++) {
399 write_table(
out, ltl[k], 256);
400 fprintf(
out,
"}%s", k < 3 ?
",\n" :
"");
405 "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
406 for (k = 0; k < 4; k++) {
408 write_table32hi(
out, big[k], 256);
409 fprintf(
out,
"}%s", k < 3 ?
",\n" :
"");
425 "local const z_crc_t FAR x2n_table[] = {\n"
448 for (
n = 0;
n < k;
n++)
449 fprintf(
out,
"%s0x%08lx%s",
n == 0 ||
n % 5 ?
"" :
" ",
450 (
unsigned long)(
table[
n]),
451 n == k - 1 ?
"" : (
n % 5 == 4 ?
",\n" :
", "));
465 for (
n = 0;
n < k;
n++)
466 fprintf(
out,
"%s0x%08lx%s",
n == 0 ||
n % 5 ?
"" :
" ",
467 (
unsigned long)(
table[
n] >> 32),
468 n == k - 1 ?
"" : (
n % 5 == 4 ?
",\n" :
", "));
485 for (
n = 0;
n < k;
n++)
486 fprintf(
out,
"%s0x%016llx%s",
n == 0 ||
n % 3 ?
"" :
" ",
487 (
unsigned long long)(
table[
n]),
488 n == k - 1 ?
"" : (
n % 3 == 2 ?
",\n" :
", "));
505 local void braid(ltl, big,
n, w)
513 for (k = 0; k < w; k++) {
514 p =
x2nmodp((
n * w + 3 - k) << 3, 0);
516 big[w - 1 - k][0] = 0;
517 for (
i = 1;
i < 256;
i++) {
519 big[w - 1 - k][
i] = byte_swap(q);
553 if ((
a & (m - 1)) == 0)
557 b =
b & 1 ? (
b >> 1) ^
POLY :
b >> 1;
588 #ifdef DYNAMIC_CRC_TABLE
589 once(&made, make_crc_table);
610 #define Z_BATCH_ZEROS 0xa10d3d0c
611 #define Z_BATCH_MIN 800
615 const unsigned char FAR *
buf;
620 const z_word_t *word;
621 z_word_t val0, val1, val2;
628 #ifdef DYNAMIC_CRC_TABLE
629 once(&made, make_crc_table);
639 __asm__
volatile(
"crc32b %w0, %w0, %w1" :
"+r"(crc) :
"r"(val));
643 word = (z_word_t
const *)
buf;
650 while (num >= 3 * Z_BATCH) {
653 for (
i = 0;
i < Z_BATCH;
i++) {
655 val1 = word[
i + Z_BATCH];
656 val2 = word[
i + 2 * Z_BATCH];
657 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc) :
"r"(val0));
658 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc1) :
"r"(val1));
659 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc2) :
"r"(val2));
663 crc =
multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
664 crc =
multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
670 if (
last >= Z_BATCH_MIN) {
676 val1 = word[
i +
last];
677 val2 = word[
i + last2];
678 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc) :
"r"(val0));
679 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc1) :
"r"(val1));
680 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc2) :
"r"(val2));
690 for (
i = 0;
i < num;
i++) {
692 __asm__
volatile(
"crc32x %w0, %w0, %x1" :
"+r"(crc) :
"r"(val0));
697 buf = (
const unsigned char FAR *)word;
701 __asm__
volatile(
"crc32b %w0, %w0, %w1" :
"+r"(crc) :
"r"(val));
705 return crc ^ 0xffffffff;
721 for (k = 0; k <
W; k++)
730 for (k = 0; k <
W; k++)
732 crc_big_table[(
data >> ((
W - 1) << 3)) & 0xff];
741 const unsigned char FAR *
buf;
747 #ifdef DYNAMIC_CRC_TABLE
748 once(&made, make_crc_table);
757 if (
len >=
N *
W +
W - 1) {
759 z_word_t
const *words;
770 blks =
len / (
N *
W);
772 words = (z_word_t
const *)
buf;
779 if (*(
unsigned char *)&endian) {
829 word0 = crc0 ^ words[0];
831 word1 = crc1 ^ words[1];
833 word2 = crc2 ^ words[2];
835 word3 = crc3 ^ words[3];
837 word4 = crc4 ^ words[4];
839 word5 = crc5 ^ words[5];
849 crc0 = crc_braid_table[0][word0 & 0xff];
851 crc1 = crc_braid_table[0][word1 & 0xff];
853 crc2 = crc_braid_table[0][word2 & 0xff];
855 crc3 = crc_braid_table[0][word3 & 0xff];
857 crc4 = crc_braid_table[0][word4 & 0xff];
859 crc5 = crc_braid_table[0][word5 & 0xff];
865 for (k = 1; k <
W; k++) {
866 crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
868 crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
870 crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
872 crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
874 crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
876 crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
889 crc = crc_word(crc0 ^ words[0]);
891 crc = crc_word(crc1 ^ words[1] ^ crc);
893 crc = crc_word(crc2 ^ words[2] ^ crc);
895 crc = crc_word(crc3 ^ words[3] ^ crc);
897 crc = crc_word(crc4 ^ words[4] ^ crc);
899 crc = crc_word(crc5 ^ words[5] ^ crc);
910 z_word_t crc0, word0, comb;
912 z_word_t crc1, word1;
914 z_word_t crc2, word2;
916 z_word_t crc3, word3;
918 z_word_t crc4, word4;
920 z_word_t crc5, word5;
928 crc0 = byte_swap(crc);
951 word0 = crc0 ^ words[0];
953 word1 = crc1 ^ words[1];
955 word2 = crc2 ^ words[2];
957 word3 = crc3 ^ words[3];
959 word4 = crc4 ^ words[4];
961 word5 = crc5 ^ words[5];
971 crc0 = crc_braid_big_table[0][word0 & 0xff];
973 crc1 = crc_braid_big_table[0][word1 & 0xff];
975 crc2 = crc_braid_big_table[0][word2 & 0xff];
977 crc3 = crc_braid_big_table[0][word3 & 0xff];
979 crc4 = crc_braid_big_table[0][word4 & 0xff];
981 crc5 = crc_braid_big_table[0][word5 & 0xff];
987 for (k = 1; k <
W; k++) {
988 crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
990 crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
992 crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
994 crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
996 crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
998 crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
1011 comb = crc_word_big(crc0 ^ words[0]);
1013 comb = crc_word_big(crc1 ^ words[1] ^ comb);
1015 comb = crc_word_big(crc2 ^ words[2] ^ comb);
1017 comb = crc_word_big(crc3 ^ words[3] ^ comb);
1019 comb = crc_word_big(crc4 ^ words[4] ^ comb);
1021 comb = crc_word_big(crc5 ^ words[5] ^ comb);
1028 crc = byte_swap(comb);
1034 buf = (
unsigned char const *)words;
1057 return crc ^ 0xffffffff;
1065 const unsigned char FAR *
buf;
1077 #ifdef DYNAMIC_CRC_TABLE
1078 once(&made, make_crc_table);
1096 #ifdef DYNAMIC_CRC_TABLE
1097 once(&made, make_crc_table);
int main(int argc, const char *argv[])
std::ofstream out("events_result.xml")
main entry point for tests
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
z_crc_t multmodp OF((z_crc_t a, z_crc_t b))
z_crc_t x2nmodp(z_off64_t n, unsigned k)
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, z_size_t len)
const z_crc_t FAR *ZEXPORT get_crc_table()
uLong ZEXPORT crc32_combine(uLong crc1, uLong crc2, z_off_t len2)
uLong ZEXPORT crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2)
uLong ZEXPORT crc32_combine_gen(z_off_t len2)
z_crc_t multmodp(z_crc_t a, z_crc_t b)
uLong ZEXPORT crc32_combine_gen64(z_off64_t len2)
uLong crc32_combine_op(uLong crc1, uLong crc2, uLong op)
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
const z_crc_t FAR crc_table[]
const z_crc_t FAR x2n_table[]