crc32.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. /* crc32.c -- compute the CRC-32 of a data stream
  2. * Copyright (C) 1995-2022 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. *
  5. * This interleaved implementation of a CRC makes use of pipelined multiple
  6. * arithmetic-logic units, commonly found in modern CPU cores. It is due to
  7. * Kadatch and Jenkins (2010). See doc/crc-doc.1.0.pdf in this distribution.
  8. */
  9. /* @(#) $Id$ */
  10. /*
  11. Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore
  12. protection on the static variables used to control the first-use generation
  13. of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should
  14. first call get_crc_table() to initialize the tables before allowing more than
  15. one thread to use crc32().
  16. MAKECRCH can be #defined to write out crc32.h. A main() routine is also
  17. produced, so that this one source file can be compiled to an executable.
  18. */
  19. #ifdef MAKECRCH
  20. # include <stdio.h>
  21. # ifndef DYNAMIC_CRC_TABLE
  22. # define DYNAMIC_CRC_TABLE
  23. # endif /* !DYNAMIC_CRC_TABLE */
  24. #endif /* MAKECRCH */
  25. #include "zutil.h" /* for Z_U4, Z_U8, z_crc_t, and FAR definitions */
  26. /*
  27. A CRC of a message is computed on N braids of words in the message, where
  28. each word consists of W bytes (4 or 8). If N is 3, for example, then three
  29. running sparse CRCs are calculated respectively on each braid, at these
  30. indices in the array of words: 0, 3, 6, ..., 1, 4, 7, ..., and 2, 5, 8, ...
  31. This is done starting at a word boundary, and continues until as many blocks
  32. of N * W bytes as are available have been processed. The results are combined
  33. into a single CRC at the end. For this code, N must be in the range 1..6 and
  34. W must be 4 or 8. The upper limit on N can be increased if desired by adding
  35. more #if blocks, extending the patterns apparent in the code. In addition,
  36. crc32.h would need to be regenerated, if the maximum N value is increased.
  37. N and W are chosen empirically by benchmarking the execution time on a given
  38. processor. The choices for N and W below were based on testing on Intel Kaby
  39. Lake i7, AMD Ryzen 7, ARM Cortex-A57, Sparc64-VII, PowerPC POWER9, and MIPS64
  40. Octeon II processors. The Intel, AMD, and ARM processors were all fastest
  41. with N=5, W=8. The Sparc, PowerPC, and MIPS64 were all fastest at N=5, W=4.
  42. They were all tested with either gcc or clang, all using the -O3 optimization
  43. level. Your mileage may vary.
  44. */
  45. /* Define N */
  46. #ifdef Z_TESTN
  47. # define N Z_TESTN
  48. #else
  49. # define N 5
  50. #endif
  51. #if N < 1 || N > 6
  52. # error N must be in 1..6
  53. #endif
  54. /*
  55. z_crc_t must be at least 32 bits. z_word_t must be at least as long as
  56. z_crc_t. It is assumed here that z_word_t is either 32 bits or 64 bits, and
  57. that bytes are eight bits.
  58. */
  59. /*
  60. Define W and the associated z_word_t type. If W is not defined, then a
  61. braided calculation is not used, and the associated tables and code are not
  62. compiled.
  63. */
  64. #ifdef Z_TESTW
  65. # if Z_TESTW-1 != -1
  66. # define W Z_TESTW
  67. # endif
  68. #else
  69. # ifdef MAKECRCH
  70. # define W 8 /* required for MAKECRCH */
  71. # else
  72. # if defined(__x86_64__) || defined(__aarch64__)
  73. # define W 8
  74. # else
  75. # define W 4
  76. # endif
  77. # endif
  78. #endif
  79. #ifdef W
  80. # if W == 8 && defined(Z_U8)
  81. typedef Z_U8 z_word_t;
  82. # elif defined(Z_U4)
  83. # undef W
  84. # define W 4
  85. typedef Z_U4 z_word_t;
  86. # else
  87. # undef W
  88. # endif
  89. #endif
  90. /* Local functions. */
  91. local z_crc_t multmodp OF((z_crc_t a, z_crc_t b));
  92. local z_crc_t x2nmodp OF((z_off64_t n, unsigned k));
  93. /* If available, use the ARM processor CRC32 instruction. */
  94. #if defined(__aarch64__) && defined(__ARM_FEATURE_CRC32) && W == 8
  95. # define ARMCRC32
  96. #endif
  97. #if defined(W) && (!defined(ARMCRC32) || defined(DYNAMIC_CRC_TABLE))
  98. /*
  99. Swap the bytes in a z_word_t to convert between little and big endian. Any
  100. self-respecting compiler will optimize this to a single machine byte-swap
  101. instruction, if one is available. This assumes that word_t is either 32 bits
  102. or 64 bits.
  103. */
  104. local z_word_t byte_swap(word)
  105. z_word_t word;
  106. {
  107. # if W == 8
  108. return
  109. (word & 0xff00000000000000) >> 56 |
  110. (word & 0xff000000000000) >> 40 |
  111. (word & 0xff0000000000) >> 24 |
  112. (word & 0xff00000000) >> 8 |
  113. (word & 0xff000000) << 8 |
  114. (word & 0xff0000) << 24 |
  115. (word & 0xff00) << 40 |
  116. (word & 0xff) << 56;
  117. # else /* W == 4 */
  118. return
  119. (word & 0xff000000) >> 24 |
  120. (word & 0xff0000) >> 8 |
  121. (word & 0xff00) << 8 |
  122. (word & 0xff) << 24;
  123. # endif
  124. }
  125. #endif
  126. /* CRC polynomial. */
  127. #define POLY 0xedb88320 /* p(x) reflected, with x^32 implied */
  128. #ifdef DYNAMIC_CRC_TABLE
  129. local z_crc_t FAR crc_table[256];
  130. local z_crc_t FAR x2n_table[32];
  131. local void make_crc_table OF((void));
  132. #ifdef W
  133. local z_word_t FAR crc_big_table[256];
  134. local z_crc_t FAR crc_braid_table[W][256];
  135. local z_word_t FAR crc_braid_big_table[W][256];
  136. local void braid OF((z_crc_t [][256], z_word_t [][256], int, int));
  137. #endif
  138. #ifdef MAKECRCH
  139. local void write_table OF((FILE *, const z_crc_t FAR *, int));
  140. local void write_table32hi OF((FILE *, const z_word_t FAR *, int));
  141. local void write_table64 OF((FILE *, const z_word_t FAR *, int));
  142. #endif /* MAKECRCH */
  143. /*
  144. Define a once() function depending on the availability of atomics. If this is
  145. compiled with DYNAMIC_CRC_TABLE defined, and if CRCs will be computed in
  146. multiple threads, and if atomics are not available, then get_crc_table() must
  147. be called to initialize the tables and must return before any threads are
  148. allowed to compute or combine CRCs.
  149. */
  150. /* Definition of once functionality. */
  151. typedef struct once_s once_t;
  152. local void once OF((once_t *, void (*)(void)));
  153. /* Check for the availability of atomics. */
  154. #if defined(__STDC__) && __STDC_VERSION__ >= 201112L && \
  155. !defined(__STDC_NO_ATOMICS__)
  156. #include <stdatomic.h>
  157. /* Structure for once(), which must be initialized with ONCE_INIT. */
  158. struct once_s {
  159. atomic_flag begun;
  160. atomic_int done;
  161. };
  162. #define ONCE_INIT {ATOMIC_FLAG_INIT, 0}
  163. /*
  164. Run the provided init() function exactly once, even if multiple threads
  165. invoke once() at the same time. The state must be a once_t initialized with
  166. ONCE_INIT.
  167. */
  168. local void once(state, init)
  169. once_t *state;
  170. void (*init)(void);
  171. {
  172. if (!atomic_load(&state->done)) {
  173. if (atomic_flag_test_and_set(&state->begun))
  174. while (!atomic_load(&state->done))
  175. ;
  176. else {
  177. init();
  178. atomic_store(&state->done, 1);
  179. }
  180. }
  181. }
  182. #else /* no atomics */
  183. /* Structure for once(), which must be initialized with ONCE_INIT. */
  184. struct once_s {
  185. volatile int begun;
  186. volatile int done;
  187. };
  188. #define ONCE_INIT {0, 0}
  189. /* Test and set. Alas, not atomic, but tries to minimize the period of
  190. vulnerability. */
  191. local int test_and_set OF((int volatile *));
  192. local int test_and_set(flag)
  193. int volatile *flag;
  194. {
  195. int was;
  196. was = *flag;
  197. *flag = 1;
  198. return was;
  199. }
  200. /* Run the provided init() function once. This is not thread-safe. */
  201. local void once(state, init)
  202. once_t *state;
  203. void (*init)(void);
  204. {
  205. if (!state->done) {
  206. if (test_and_set(&state->begun))
  207. while (!state->done)
  208. ;
  209. else {
  210. init();
  211. state->done = 1;
  212. }
  213. }
  214. }
  215. #endif
  216. /* State for once(). */
  217. local once_t made = ONCE_INIT;
  218. /*
  219. Generate tables for a byte-wise 32-bit CRC calculation on the polynomial:
  220. x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
  221. Polynomials over GF(2) are represented in binary, one bit per coefficient,
  222. with the lowest powers in the most significant bit. Then adding polynomials
  223. is just exclusive-or, and multiplying a polynomial by x is a right shift by
  224. one. If we call the above polynomial p, and represent a byte as the
  225. polynomial q, also with the lowest power in the most significant bit (so the
  226. byte 0xb1 is the polynomial x^7+x^3+x^2+1), then the CRC is (q*x^32) mod p,
  227. where a mod b means the remainder after dividing a by b.
  228. This calculation is done using the shift-register method of multiplying and
  229. taking the remainder. The register is initialized to zero, and for each
  230. incoming bit, x^32 is added mod p to the register if the bit is a one (where
  231. x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x
  232. (which is shifting right by one and adding x^32 mod p if the bit shifted out
  233. is a one). We start with the highest power (least significant bit) of q and
  234. repeat for all eight bits of q.
  235. The table is simply the CRC of all possible eight bit values. This is all the
  236. information needed to generate CRCs on data a byte at a time for all
  237. combinations of CRC register values and incoming bytes.
  238. */
  239. local void make_crc_table()
  240. {
  241. unsigned i, j, n;
  242. z_crc_t p;
  243. /* initialize the CRC of bytes tables */
  244. for (i = 0; i < 256; i++) {
  245. p = i;
  246. for (j = 0; j < 8; j++)
  247. p = p & 1 ? (p >> 1) ^ POLY : p >> 1;
  248. crc_table[i] = p;
  249. #ifdef W
  250. crc_big_table[i] = byte_swap(p);
  251. #endif
  252. }
  253. /* initialize the x^2^n mod p(x) table */
  254. p = (z_crc_t)1 << 30; /* x^1 */
  255. x2n_table[0] = p;
  256. for (n = 1; n < 32; n++)
  257. x2n_table[n] = p = multmodp(p, p);
  258. #ifdef W
  259. /* initialize the braiding tables -- needs x2n_table[] */
  260. braid(crc_braid_table, crc_braid_big_table, N, W);
  261. #endif
  262. #ifdef MAKECRCH
  263. {
  264. /*
  265. The crc32.h header file contains tables for both 32-bit and 64-bit
  266. z_word_t's, and so requires a 64-bit type be available. In that case,
  267. z_word_t must be defined to be 64-bits. This code then also generates
  268. and writes out the tables for the case that z_word_t is 32 bits.
  269. */
  270. #if !defined(W) || W != 8
  271. # error Need a 64-bit integer type in order to generate crc32.h.
  272. #endif
  273. FILE *out;
  274. int k, n;
  275. z_crc_t ltl[8][256];
  276. z_word_t big[8][256];
  277. out = fopen("crc32.h", "w");
  278. if (out == NULL) return;
  279. /* write out little-endian CRC table to crc32.h */
  280. fprintf(out,
  281. "/* crc32.h -- tables for rapid CRC calculation\n"
  282. " * Generated automatically by crc32.c\n */\n"
  283. "\n"
  284. "local const z_crc_t FAR crc_table[] = {\n"
  285. " ");
  286. write_table(out, crc_table, 256);
  287. fprintf(out,
  288. "};\n");
  289. /* write out big-endian CRC table for 64-bit z_word_t to crc32.h */
  290. fprintf(out,
  291. "\n"
  292. "#ifdef W\n"
  293. "\n"
  294. "#if W == 8\n"
  295. "\n"
  296. "local const z_word_t FAR crc_big_table[] = {\n"
  297. " ");
  298. write_table64(out, crc_big_table, 256);
  299. fprintf(out,
  300. "};\n");
  301. /* write out big-endian CRC table for 32-bit z_word_t to crc32.h */
  302. fprintf(out,
  303. "\n"
  304. "#else /* W == 4 */\n"
  305. "\n"
  306. "local const z_word_t FAR crc_big_table[] = {\n"
  307. " ");
  308. write_table32hi(out, crc_big_table, 256);
  309. fprintf(out,
  310. "};\n"
  311. "\n"
  312. "#endif\n");
  313. /* write out braid tables for each value of N */
  314. for (n = 1; n <= 6; n++) {
  315. fprintf(out,
  316. "\n"
  317. "#if N == %d\n", n);
  318. /* compute braid tables for this N and 64-bit word_t */
  319. braid(ltl, big, n, 8);
  320. /* write out braid tables for 64-bit z_word_t to crc32.h */
  321. fprintf(out,
  322. "\n"
  323. "#if W == 8\n"
  324. "\n"
  325. "local const z_crc_t FAR crc_braid_table[][256] = {\n");
  326. for (k = 0; k < 8; k++) {
  327. fprintf(out, " {");
  328. write_table(out, ltl[k], 256);
  329. fprintf(out, "}%s", k < 7 ? ",\n" : "");
  330. }
  331. fprintf(out,
  332. "};\n"
  333. "\n"
  334. "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
  335. for (k = 0; k < 8; k++) {
  336. fprintf(out, " {");
  337. write_table64(out, big[k], 256);
  338. fprintf(out, "}%s", k < 7 ? ",\n" : "");
  339. }
  340. fprintf(out,
  341. "};\n");
  342. /* compute braid tables for this N and 32-bit word_t */
  343. braid(ltl, big, n, 4);
  344. /* write out braid tables for 32-bit z_word_t to crc32.h */
  345. fprintf(out,
  346. "\n"
  347. "#else /* W == 4 */\n"
  348. "\n"
  349. "local const z_crc_t FAR crc_braid_table[][256] = {\n");
  350. for (k = 0; k < 4; k++) {
  351. fprintf(out, " {");
  352. write_table(out, ltl[k], 256);
  353. fprintf(out, "}%s", k < 3 ? ",\n" : "");
  354. }
  355. fprintf(out,
  356. "};\n"
  357. "\n"
  358. "local const z_word_t FAR crc_braid_big_table[][256] = {\n");
  359. for (k = 0; k < 4; k++) {
  360. fprintf(out, " {");
  361. write_table32hi(out, big[k], 256);
  362. fprintf(out, "}%s", k < 3 ? ",\n" : "");
  363. }
  364. fprintf(out,
  365. "};\n"
  366. "\n"
  367. "#endif\n"
  368. "\n"
  369. "#endif\n");
  370. }
  371. fprintf(out,
  372. "\n"
  373. "#endif\n");
  374. /* write out zeros operator table to crc32.h */
  375. fprintf(out,
  376. "\n"
  377. "local const z_crc_t FAR x2n_table[] = {\n"
  378. " ");
  379. write_table(out, x2n_table, 32);
  380. fprintf(out,
  381. "};\n");
  382. fclose(out);
  383. }
  384. #endif /* MAKECRCH */
  385. }
  386. #ifdef MAKECRCH
  387. /*
  388. Write the 32-bit values in table[0..k-1] to out, five per line in
  389. hexadecimal separated by commas.
  390. */
  391. local void write_table(out, table, k)
  392. FILE *out;
  393. const z_crc_t FAR *table;
  394. int k;
  395. {
  396. int n;
  397. for (n = 0; n < k; n++)
  398. fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ",
  399. (unsigned long)(table[n]),
  400. n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
  401. }
  402. /*
  403. Write the high 32-bits of each value in table[0..k-1] to out, five per line
  404. in hexadecimal separated by commas.
  405. */
  406. local void write_table32hi(out, table, k)
  407. FILE *out;
  408. const z_word_t FAR *table;
  409. int k;
  410. {
  411. int n;
  412. for (n = 0; n < k; n++)
  413. fprintf(out, "%s0x%08lx%s", n == 0 || n % 5 ? "" : " ",
  414. (unsigned long)(table[n] >> 32),
  415. n == k - 1 ? "" : (n % 5 == 4 ? ",\n" : ", "));
  416. }
  417. /*
  418. Write the 64-bit values in table[0..k-1] to out, three per line in
  419. hexadecimal separated by commas. This assumes that if there is a 64-bit
  420. type, then there is also a long long integer type, and it is at least 64
  421. bits. If not, then the type cast and format string can be adjusted
  422. accordingly.
  423. */
  424. local void write_table64(out, table, k)
  425. FILE *out;
  426. const z_word_t FAR *table;
  427. int k;
  428. {
  429. int n;
  430. for (n = 0; n < k; n++)
  431. fprintf(out, "%s0x%016llx%s", n == 0 || n % 3 ? "" : " ",
  432. (unsigned long long)(table[n]),
  433. n == k - 1 ? "" : (n % 3 == 2 ? ",\n" : ", "));
  434. }
  435. /* Actually do the deed. */
  436. int main()
  437. {
  438. make_crc_table();
  439. return 0;
  440. }
  441. #endif /* MAKECRCH */
  442. #ifdef W
  443. /*
  444. Generate the little and big-endian braid tables for the given n and z_word_t
  445. size w. Each array must have room for w blocks of 256 elements.
  446. */
  447. local void braid(ltl, big, n, w)
  448. z_crc_t ltl[][256];
  449. z_word_t big[][256];
  450. int n;
  451. int w;
  452. {
  453. int k;
  454. z_crc_t i, p, q;
  455. for (k = 0; k < w; k++) {
  456. p = x2nmodp((n * w + 3 - k) << 3, 0);
  457. ltl[k][0] = 0;
  458. big[w - 1 - k][0] = 0;
  459. for (i = 1; i < 256; i++) {
  460. ltl[k][i] = q = multmodp(i << 24, p);
  461. big[w - 1 - k][i] = byte_swap(q);
  462. }
  463. }
  464. }
  465. #endif
  466. #else /* !DYNAMIC_CRC_TABLE */
  467. /* ========================================================================
  468. * Tables for byte-wise and braided CRC-32 calculations, and a table of powers
  469. * of x for combining CRC-32s, all made by make_crc_table().
  470. */
  471. #include "crc32.h"
  472. #endif /* DYNAMIC_CRC_TABLE */
  473. /* ========================================================================
  474. * Routines used for CRC calculation. Some are also required for the table
  475. * generation above.
  476. */
  477. /*
  478. Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC polynomial,
  479. reflected. For speed, this requires that a not be zero.
  480. */
  481. local z_crc_t multmodp(a, b)
  482. z_crc_t a;
  483. z_crc_t b;
  484. {
  485. z_crc_t m, p;
  486. m = (z_crc_t)1 << 31;
  487. p = 0;
  488. for (;;) {
  489. if (a & m) {
  490. p ^= b;
  491. if ((a & (m - 1)) == 0)
  492. break;
  493. }
  494. m >>= 1;
  495. b = b & 1 ? (b >> 1) ^ POLY : b >> 1;
  496. }
  497. return p;
  498. }
  499. /*
  500. Return x^(n * 2^k) modulo p(x). Requires that x2n_table[] has been
  501. initialized.
  502. */
  503. local z_crc_t x2nmodp(n, k)
  504. z_off64_t n;
  505. unsigned k;
  506. {
  507. z_crc_t p;
  508. p = (z_crc_t)1 << 31; /* x^0 == 1 */
  509. while (n) {
  510. if (n & 1)
  511. p = multmodp(x2n_table[k & 31], p);
  512. n >>= 1;
  513. k++;
  514. }
  515. return p;
  516. }
  517. /* =========================================================================
  518. * This function can be used by asm versions of crc32(), and to force the
  519. * generation of the CRC tables in a threaded application.
  520. */
  521. const z_crc_t FAR * ZEXPORT get_crc_table()
  522. {
  523. #ifdef DYNAMIC_CRC_TABLE
  524. once(&made, make_crc_table);
  525. #endif /* DYNAMIC_CRC_TABLE */
  526. return (const z_crc_t FAR *)crc_table;
  527. }
  528. /* =========================================================================
  529. * Use ARM machine instructions if available. This will compute the CRC about
  530. * ten times faster than the braided calculation. This code does not check for
  531. * the presence of the CRC instruction at run time. __ARM_FEATURE_CRC32 will
  532. * only be defined if the compilation specifies an ARM processor architecture
  533. * that has the instructions. For example, compiling with -march=armv8.1-a or
  534. * -march=armv8-a+crc, or -march=native if the compile machine has the crc32
  535. * instructions.
  536. */
  537. #ifdef ARMCRC32
  538. /*
  539. Constants empirically determined to maximize speed. These values are from
  540. measurements on a Cortex-A57. Your mileage may vary.
  541. */
  542. #define Z_BATCH 3990 /* number of words in a batch */
  543. #define Z_BATCH_ZEROS 0xa10d3d0c /* computed from Z_BATCH = 3990 */
  544. #define Z_BATCH_MIN 800 /* fewest words in a final batch */
  545. unsigned long ZEXPORT crc32_z(crc, buf, len)
  546. unsigned long crc;
  547. const unsigned char FAR *buf;
  548. z_size_t len;
  549. {
  550. z_crc_t val;
  551. z_word_t crc1, crc2;
  552. const z_word_t *word;
  553. z_word_t val0, val1, val2;
  554. z_size_t last, last2, i;
  555. z_size_t num;
  556. /* Return initial CRC, if requested. */
  557. if (buf == Z_NULL) return 0;
  558. #ifdef DYNAMIC_CRC_TABLE
  559. once(&made, make_crc_table);
  560. #endif /* DYNAMIC_CRC_TABLE */
  561. /* Pre-condition the CRC */
  562. crc ^= 0xffffffff;
  563. /* Compute the CRC up to a word boundary. */
  564. while (len && ((z_size_t)buf & 7) != 0) {
  565. len--;
  566. val = *buf++;
  567. __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
  568. }
  569. /* Prepare to compute the CRC on full 64-bit words word[0..num-1]. */
  570. word = (z_word_t const *)buf;
  571. num = len >> 3;
  572. len &= 7;
  573. /* Do three interleaved CRCs to realize the throughput of one crc32x
  574. instruction per cycle. Each CRC is calcuated on Z_BATCH words. The three
  575. CRCs are combined into a single CRC after each set of batches. */
  576. while (num >= 3 * Z_BATCH) {
  577. crc1 = 0;
  578. crc2 = 0;
  579. for (i = 0; i < Z_BATCH; i++) {
  580. val0 = word[i];
  581. val1 = word[i + Z_BATCH];
  582. val2 = word[i + 2 * Z_BATCH];
  583. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
  584. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
  585. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
  586. }
  587. word += 3 * Z_BATCH;
  588. num -= 3 * Z_BATCH;
  589. crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
  590. crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
  591. }
  592. /* Do one last smaller batch with the remaining words, if there are enough
  593. to pay for the combination of CRCs. */
  594. last = num / 3;
  595. if (last >= Z_BATCH_MIN) {
  596. last2 = last << 1;
  597. crc1 = 0;
  598. crc2 = 0;
  599. for (i = 0; i < last; i++) {
  600. val0 = word[i];
  601. val1 = word[i + last];
  602. val2 = word[i + last2];
  603. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
  604. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc1) : "r"(val1));
  605. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc2) : "r"(val2));
  606. }
  607. word += 3 * last;
  608. num -= 3 * last;
  609. val = x2nmodp(last, 6);
  610. crc = multmodp(val, crc) ^ crc1;
  611. crc = multmodp(val, crc) ^ crc2;
  612. }
  613. /* Compute the CRC on any remaining words. */
  614. for (i = 0; i < num; i++) {
  615. val0 = word[i];
  616. __asm__ volatile("crc32x %w0, %w0, %x1" : "+r"(crc) : "r"(val0));
  617. }
  618. word += num;
  619. /* Complete the CRC on any remaining bytes. */
  620. buf = (const unsigned char FAR *)word;
  621. while (len) {
  622. len--;
  623. val = *buf++;
  624. __asm__ volatile("crc32b %w0, %w0, %w1" : "+r"(crc) : "r"(val));
  625. }
  626. /* Return the CRC, post-conditioned. */
  627. return crc ^ 0xffffffff;
  628. }
  629. #else
  630. #ifdef W
  631. /*
  632. Return the CRC of the W bytes in the word_t data, taking the
  633. least-significant byte of the word as the first byte of data, without any pre
  634. or post conditioning. This is used to combine the CRCs of each braid.
  635. */
  636. local z_crc_t crc_word(data)
  637. z_word_t data;
  638. {
  639. int k;
  640. for (k = 0; k < W; k++)
  641. data = (data >> 8) ^ crc_table[data & 0xff];
  642. return (z_crc_t)data;
  643. }
  644. local z_word_t crc_word_big(data)
  645. z_word_t data;
  646. {
  647. int k;
  648. for (k = 0; k < W; k++)
  649. data = (data << 8) ^
  650. crc_big_table[(data >> ((W - 1) << 3)) & 0xff];
  651. return data;
  652. }
  653. #endif
  654. /* ========================================================================= */
  655. unsigned long ZEXPORT crc32_z(crc, buf, len)
  656. unsigned long crc;
  657. const unsigned char FAR *buf;
  658. z_size_t len;
  659. {
  660. /* Return initial CRC, if requested. */
  661. if (buf == Z_NULL) return 0;
  662. #ifdef DYNAMIC_CRC_TABLE
  663. once(&made, make_crc_table);
  664. #endif /* DYNAMIC_CRC_TABLE */
  665. /* Pre-condition the CRC */
  666. crc ^= 0xffffffff;
  667. #ifdef W
  668. /* If provided enough bytes, do a braided CRC calculation. */
  669. if (len >= N * W + W - 1) {
  670. z_size_t blks;
  671. z_word_t const *words;
  672. unsigned endian;
  673. int k;
  674. /* Compute the CRC up to a z_word_t boundary. */
  675. while (len && ((z_size_t)buf & (W - 1)) != 0) {
  676. len--;
  677. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  678. }
  679. /* Compute the CRC on as many N z_word_t blocks as are available. */
  680. blks = len / (N * W);
  681. len -= blks * N * W;
  682. words = (z_word_t const *)buf;
  683. /* Do endian check at execution time instead of compile time, since ARM
  684. processors can change the endianess at execution time. If the
  685. compiler knows what the endianess will be, it can optimize out the
  686. check and the unused branch. */
  687. endian = 1;
  688. if (*(unsigned char *)&endian) {
  689. /* Little endian. */
  690. z_crc_t crc0;
  691. z_word_t word0;
  692. #if N > 1
  693. z_crc_t crc1;
  694. z_word_t word1;
  695. #if N > 2
  696. z_crc_t crc2;
  697. z_word_t word2;
  698. #if N > 3
  699. z_crc_t crc3;
  700. z_word_t word3;
  701. #if N > 4
  702. z_crc_t crc4;
  703. z_word_t word4;
  704. #if N > 5
  705. z_crc_t crc5;
  706. z_word_t word5;
  707. #endif
  708. #endif
  709. #endif
  710. #endif
  711. #endif
  712. /* Initialize the CRC for each braid. */
  713. crc0 = crc;
  714. #if N > 1
  715. crc1 = 0;
  716. #if N > 2
  717. crc2 = 0;
  718. #if N > 3
  719. crc3 = 0;
  720. #if N > 4
  721. crc4 = 0;
  722. #if N > 5
  723. crc5 = 0;
  724. #endif
  725. #endif
  726. #endif
  727. #endif
  728. #endif
  729. /*
  730. Process the first blks-1 blocks, computing the CRCs on each braid
  731. independently.
  732. */
  733. while (--blks) {
  734. /* Load the word for each braid into registers. */
  735. word0 = crc0 ^ words[0];
  736. #if N > 1
  737. word1 = crc1 ^ words[1];
  738. #if N > 2
  739. word2 = crc2 ^ words[2];
  740. #if N > 3
  741. word3 = crc3 ^ words[3];
  742. #if N > 4
  743. word4 = crc4 ^ words[4];
  744. #if N > 5
  745. word5 = crc5 ^ words[5];
  746. #endif
  747. #endif
  748. #endif
  749. #endif
  750. #endif
  751. words += N;
  752. /* Compute and update the CRC for each word. The loop should
  753. get unrolled. */
  754. crc0 = crc_braid_table[0][word0 & 0xff];
  755. #if N > 1
  756. crc1 = crc_braid_table[0][word1 & 0xff];
  757. #if N > 2
  758. crc2 = crc_braid_table[0][word2 & 0xff];
  759. #if N > 3
  760. crc3 = crc_braid_table[0][word3 & 0xff];
  761. #if N > 4
  762. crc4 = crc_braid_table[0][word4 & 0xff];
  763. #if N > 5
  764. crc5 = crc_braid_table[0][word5 & 0xff];
  765. #endif
  766. #endif
  767. #endif
  768. #endif
  769. #endif
  770. for (k = 1; k < W; k++) {
  771. crc0 ^= crc_braid_table[k][(word0 >> (k << 3)) & 0xff];
  772. #if N > 1
  773. crc1 ^= crc_braid_table[k][(word1 >> (k << 3)) & 0xff];
  774. #if N > 2
  775. crc2 ^= crc_braid_table[k][(word2 >> (k << 3)) & 0xff];
  776. #if N > 3
  777. crc3 ^= crc_braid_table[k][(word3 >> (k << 3)) & 0xff];
  778. #if N > 4
  779. crc4 ^= crc_braid_table[k][(word4 >> (k << 3)) & 0xff];
  780. #if N > 5
  781. crc5 ^= crc_braid_table[k][(word5 >> (k << 3)) & 0xff];
  782. #endif
  783. #endif
  784. #endif
  785. #endif
  786. #endif
  787. }
  788. }
  789. /*
  790. Process the last block, combining the CRCs of the N braids at the
  791. same time.
  792. */
  793. crc = crc_word(crc0 ^ words[0]);
  794. #if N > 1
  795. crc = crc_word(crc1 ^ words[1] ^ crc);
  796. #if N > 2
  797. crc = crc_word(crc2 ^ words[2] ^ crc);
  798. #if N > 3
  799. crc = crc_word(crc3 ^ words[3] ^ crc);
  800. #if N > 4
  801. crc = crc_word(crc4 ^ words[4] ^ crc);
  802. #if N > 5
  803. crc = crc_word(crc5 ^ words[5] ^ crc);
  804. #endif
  805. #endif
  806. #endif
  807. #endif
  808. #endif
  809. words += N;
  810. }
  811. else {
  812. /* Big endian. */
  813. z_word_t crc0, word0, comb;
  814. #if N > 1
  815. z_word_t crc1, word1;
  816. #if N > 2
  817. z_word_t crc2, word2;
  818. #if N > 3
  819. z_word_t crc3, word3;
  820. #if N > 4
  821. z_word_t crc4, word4;
  822. #if N > 5
  823. z_word_t crc5, word5;
  824. #endif
  825. #endif
  826. #endif
  827. #endif
  828. #endif
  829. /* Initialize the CRC for each braid. */
  830. crc0 = byte_swap(crc);
  831. #if N > 1
  832. crc1 = 0;
  833. #if N > 2
  834. crc2 = 0;
  835. #if N > 3
  836. crc3 = 0;
  837. #if N > 4
  838. crc4 = 0;
  839. #if N > 5
  840. crc5 = 0;
  841. #endif
  842. #endif
  843. #endif
  844. #endif
  845. #endif
  846. /*
  847. Process the first blks-1 blocks, computing the CRCs on each braid
  848. independently.
  849. */
  850. while (--blks) {
  851. /* Load the word for each braid into registers. */
  852. word0 = crc0 ^ words[0];
  853. #if N > 1
  854. word1 = crc1 ^ words[1];
  855. #if N > 2
  856. word2 = crc2 ^ words[2];
  857. #if N > 3
  858. word3 = crc3 ^ words[3];
  859. #if N > 4
  860. word4 = crc4 ^ words[4];
  861. #if N > 5
  862. word5 = crc5 ^ words[5];
  863. #endif
  864. #endif
  865. #endif
  866. #endif
  867. #endif
  868. words += N;
  869. /* Compute and update the CRC for each word. The loop should
  870. get unrolled. */
  871. crc0 = crc_braid_big_table[0][word0 & 0xff];
  872. #if N > 1
  873. crc1 = crc_braid_big_table[0][word1 & 0xff];
  874. #if N > 2
  875. crc2 = crc_braid_big_table[0][word2 & 0xff];
  876. #if N > 3
  877. crc3 = crc_braid_big_table[0][word3 & 0xff];
  878. #if N > 4
  879. crc4 = crc_braid_big_table[0][word4 & 0xff];
  880. #if N > 5
  881. crc5 = crc_braid_big_table[0][word5 & 0xff];
  882. #endif
  883. #endif
  884. #endif
  885. #endif
  886. #endif
  887. for (k = 1; k < W; k++) {
  888. crc0 ^= crc_braid_big_table[k][(word0 >> (k << 3)) & 0xff];
  889. #if N > 1
  890. crc1 ^= crc_braid_big_table[k][(word1 >> (k << 3)) & 0xff];
  891. #if N > 2
  892. crc2 ^= crc_braid_big_table[k][(word2 >> (k << 3)) & 0xff];
  893. #if N > 3
  894. crc3 ^= crc_braid_big_table[k][(word3 >> (k << 3)) & 0xff];
  895. #if N > 4
  896. crc4 ^= crc_braid_big_table[k][(word4 >> (k << 3)) & 0xff];
  897. #if N > 5
  898. crc5 ^= crc_braid_big_table[k][(word5 >> (k << 3)) & 0xff];
  899. #endif
  900. #endif
  901. #endif
  902. #endif
  903. #endif
  904. }
  905. }
  906. /*
  907. Process the last block, combining the CRCs of the N braids at the
  908. same time.
  909. */
  910. comb = crc_word_big(crc0 ^ words[0]);
  911. #if N > 1
  912. comb = crc_word_big(crc1 ^ words[1] ^ comb);
  913. #if N > 2
  914. comb = crc_word_big(crc2 ^ words[2] ^ comb);
  915. #if N > 3
  916. comb = crc_word_big(crc3 ^ words[3] ^ comb);
  917. #if N > 4
  918. comb = crc_word_big(crc4 ^ words[4] ^ comb);
  919. #if N > 5
  920. comb = crc_word_big(crc5 ^ words[5] ^ comb);
  921. #endif
  922. #endif
  923. #endif
  924. #endif
  925. #endif
  926. words += N;
  927. crc = byte_swap(comb);
  928. }
  929. /*
  930. Update the pointer to the remaining bytes to process.
  931. */
  932. buf = (unsigned char const *)words;
  933. }
  934. #endif /* W */
  935. /* Complete the computation of the CRC on any remaining bytes. */
  936. while (len >= 8) {
  937. len -= 8;
  938. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  939. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  940. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  941. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  942. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  943. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  944. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  945. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  946. }
  947. while (len) {
  948. len--;
  949. crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
  950. }
  951. /* Return the CRC, post-conditioned. */
  952. return crc ^ 0xffffffff;
  953. }
  954. #endif
  955. /* ========================================================================= */
  956. unsigned long ZEXPORT crc32(crc, buf, len)
  957. unsigned long crc;
  958. const unsigned char FAR *buf;
  959. uInt len;
  960. {
  961. return crc32_z(crc, buf, len);
  962. }
  963. /* ========================================================================= */
  964. uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
  965. uLong crc1;
  966. uLong crc2;
  967. z_off64_t len2;
  968. {
  969. #ifdef DYNAMIC_CRC_TABLE
  970. once(&made, make_crc_table);
  971. #endif /* DYNAMIC_CRC_TABLE */
  972. return multmodp(x2nmodp(len2, 3), crc1) ^ crc2;
  973. }
  974. /* ========================================================================= */
  975. uLong ZEXPORT crc32_combine(crc1, crc2, len2)
  976. uLong crc1;
  977. uLong crc2;
  978. z_off_t len2;
  979. {
  980. return crc32_combine64(crc1, crc2, len2);
  981. }
  982. /* ========================================================================= */
  983. uLong ZEXPORT crc32_combine_gen64(len2)
  984. z_off64_t len2;
  985. {
  986. #ifdef DYNAMIC_CRC_TABLE
  987. once(&made, make_crc_table);
  988. #endif /* DYNAMIC_CRC_TABLE */
  989. return x2nmodp(len2, 3);
  990. }
  991. /* ========================================================================= */
  992. uLong ZEXPORT crc32_combine_gen(len2)
  993. z_off_t len2;
  994. {
  995. return crc32_combine_gen64(len2);
  996. }
  997. /* ========================================================================= */
  998. uLong crc32_combine_op(crc1, crc2, op)
  999. uLong crc1;
  1000. uLong crc2;
  1001. uLong op;
  1002. {
  1003. return multmodp(op, crc1) ^ crc2;
  1004. }