crc32.c 31 KB

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