crc32.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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. }