crc32.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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()
  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()
  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. }