crc32.c 32 KB

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