crc32_hw1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* Compile with gcc -O3 -msse4.2 ... */
  2. /* crc32c.c -- compute CRC-32C using the Intel crc32 instruction
  3. * Copyright (C) 2013 Mark Adler
  4. * Version 1.1 1 Aug 2013 Mark Adler
  5. */
  6. /*
  7. This software is provided 'as-is', without any express or implied
  8. warranty. In no event will the author be held liable for any damages
  9. arising from the use of this software.
  10. Permission is granted to anyone to use this software for any purpose,
  11. including commercial applications, and to alter it and redistribute it
  12. freely, subject to the following restrictions:
  13. 1. The origin of this software must not be misrepresented; you must not
  14. claim that you wrote the original software. If you use this software
  15. in a product, an acknowledgment in the product documentation would be
  16. appreciated but is not required.
  17. 2. Altered source versions must be plainly marked as such, and must not be
  18. misrepresented as being the original software.
  19. 3. This notice may not be removed or altered from any source distribution.
  20. Mark Adler
  21. madler@alumni.caltech.edu
  22. */
  23. /* Use hardware CRC instruction on Intel SSE 4.2 processors. This computes a
  24. CRC-32C, *not* the CRC-32 used by Ethernet and zip, gzip, etc.
  25. */
  26. /* Version history:
  27. 1.0 10 Feb 2013 First version
  28. 1.1 1 Aug 2013 Correct comments on why three crc instructions in parallel
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <stdint.h>
  33. #include <unistd.h>
  34. #include <pthread.h>
  35. /* CRC-32C (iSCSI) polynomial in reversed bit order. */
  36. #define POLY 0x82f63b78
  37. /* Table for a quadword-at-a-time software crc. */
  38. static pthread_once_t crc32c_once_sw = PTHREAD_ONCE_INIT;
  39. static uint32_t crc32c_table[8][256];
  40. /* Construct table for software CRC-32C calculation. */
  41. static void crc32c_init_sw(void)
  42. {
  43. uint32_t n, crc, k;
  44. for (n = 0; n < 256; n++) {
  45. crc = n;
  46. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  47. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  48. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  49. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  50. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  51. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  52. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  53. crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
  54. crc32c_table[0][n] = crc;
  55. }
  56. for (n = 0; n < 256; n++) {
  57. crc = crc32c_table[0][n];
  58. for (k = 1; k < 8; k++) {
  59. crc = crc32c_table[0][crc & 0xff] ^ (crc >> 8);
  60. crc32c_table[k][n] = crc;
  61. }
  62. }
  63. }
  64. /* Table-driven software version as a fall-back. This is about 15 times slower
  65. than using the hardware instructions. This assumes little-endian integers,
  66. as is the case on Intel processors that the assembler code here is for. */
  67. static uint32_t crc32c_sw(uint32_t crci, const void *buf, size_t len)
  68. {
  69. const unsigned char *next = buf;
  70. uint64_t crc;
  71. pthread_once(&crc32c_once_sw, crc32c_init_sw);
  72. crc = crci ^ 0xffffffff;
  73. while (len && ((uintptr_t)next & 7) != 0) {
  74. crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
  75. len--;
  76. }
  77. while (len >= 8) {
  78. crc ^= *(uint64_t *)next;
  79. crc = crc32c_table[7][crc & 0xff] ^
  80. crc32c_table[6][(crc >> 8) & 0xff] ^
  81. crc32c_table[5][(crc >> 16) & 0xff] ^
  82. crc32c_table[4][(crc >> 24) & 0xff] ^
  83. crc32c_table[3][(crc >> 32) & 0xff] ^
  84. crc32c_table[2][(crc >> 40) & 0xff] ^
  85. crc32c_table[1][(crc >> 48) & 0xff] ^
  86. crc32c_table[0][crc >> 56];
  87. next += 8;
  88. len -= 8;
  89. }
  90. while (len) {
  91. crc = crc32c_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
  92. len--;
  93. }
  94. return (uint32_t)crc ^ 0xffffffff;
  95. }
  96. /* Block sizes for three-way parallel crc computation. LONG and SHORT must
  97. both be powers of two. The associated string constants must be set
  98. accordingly, for use in constructing the assembler instructions. */
  99. #define LONG 8192
  100. #define LONGx1 "8192"
  101. #define LONGx2 "16384"
  102. #define SHORT 256
  103. #define SHORTx1 "256"
  104. #define SHORTx2 "512"
  105. /* Tables for hardware crc that shift a crc by LONG and SHORT zeros. */
  106. static pthread_once_t crc32c_once_hw = PTHREAD_ONCE_INIT;
  107. static uint32_t crc32c_long[4][256];
  108. static uint32_t crc32c_short[4][256];
  109. /* Multiply a matrix times a vector over the Galois field of two elements,
  110. GF(2). Each element is a bit in an unsigned integer. mat must have at
  111. least as many entries as the power of two for most significant one bit in
  112. vec. */
  113. static inline uint32_t gf2_matrix_times(uint32_t *mat, uint32_t vec)
  114. {
  115. uint32_t sum;
  116. sum = 0;
  117. while (vec) {
  118. if (vec & 1)
  119. sum ^= *mat;
  120. vec >>= 1;
  121. mat++;
  122. }
  123. return sum;
  124. }
  125. /* Multiply a matrix by itself over GF(2). Both mat and square must have 32
  126. rows. */
  127. static inline void gf2_matrix_square(uint32_t *square, uint32_t *mat)
  128. {
  129. int n;
  130. for (n = 0; n < 32; n++)
  131. square[n] = gf2_matrix_times(mat, mat[n]);
  132. }
  133. /* Construct an operator to apply len zeros to a crc. len must be a power of
  134. two. If len is not a power of two, then the result is the same as for the
  135. largest power of two less than len. The result for len == 0 is the same as
  136. for len == 1. A version of this routine could be easily written for any
  137. len, but that is not needed for this application. */
  138. static void crc32c_zeros_op(uint32_t *even, size_t len)
  139. {
  140. int n;
  141. uint32_t row;
  142. uint32_t odd[32]; /* odd-power-of-two zeros operator */
  143. /* put operator for one zero bit in odd */
  144. odd[0] = POLY; /* CRC-32C polynomial */
  145. row = 1;
  146. for (n = 1; n < 32; n++) {
  147. odd[n] = row;
  148. row <<= 1;
  149. }
  150. /* put operator for two zero bits in even */
  151. gf2_matrix_square(even, odd);
  152. /* put operator for four zero bits in odd */
  153. gf2_matrix_square(odd, even);
  154. /* first square will put the operator for one zero byte (eight zero bits),
  155. in even -- next square puts operator for two zero bytes in odd, and so
  156. on, until len has been rotated down to zero */
  157. do {
  158. gf2_matrix_square(even, odd);
  159. len >>= 1;
  160. if (len == 0)
  161. return;
  162. gf2_matrix_square(odd, even);
  163. len >>= 1;
  164. } while (len);
  165. /* answer ended up in odd -- copy to even */
  166. for (n = 0; n < 32; n++)
  167. even[n] = odd[n];
  168. }
  169. /* Take a length and build four lookup tables for applying the zeros operator
  170. for that length, byte-by-byte on the operand. */
  171. static void crc32c_zeros(uint32_t zeros[][256], size_t len)
  172. {
  173. uint32_t n;
  174. uint32_t op[32];
  175. crc32c_zeros_op(op, len);
  176. for (n = 0; n < 256; n++) {
  177. zeros[0][n] = gf2_matrix_times(op, n);
  178. zeros[1][n] = gf2_matrix_times(op, n << 8);
  179. zeros[2][n] = gf2_matrix_times(op, n << 16);
  180. zeros[3][n] = gf2_matrix_times(op, n << 24);
  181. }
  182. }
  183. /* Apply the zeros operator table to crc. */
  184. static inline uint32_t crc32c_shift(uint32_t zeros[][256], uint32_t crc)
  185. {
  186. return zeros[0][crc & 0xff] ^ zeros[1][(crc >> 8) & 0xff] ^
  187. zeros[2][(crc >> 16) & 0xff] ^ zeros[3][crc >> 24];
  188. }
  189. /* Initialize tables for shifting crcs. */
  190. static void crc32c_init_hw(void)
  191. {
  192. crc32c_zeros(crc32c_long, LONG);
  193. crc32c_zeros(crc32c_short, SHORT);
  194. }
  195. /* Compute CRC-32C using the Intel hardware instruction. */
  196. uint32_t crc32c(const void *buf, size_t len, uint32_t crc)
  197. {
  198. const unsigned char *next = buf;
  199. const unsigned char *end;
  200. uint64_t crc0, crc1, crc2; /* need to be 64 bits for crc32q */
  201. /* populate shift tables the first time through */
  202. pthread_once(&crc32c_once_hw, crc32c_init_hw);
  203. /* pre-process the crc */
  204. crc0 = crc ^ 0xffffffff;
  205. /* compute the crc for up to seven leading bytes to bring the data pointer
  206. to an eight-byte boundary */
  207. while (len && ((uintptr_t)next & 7) != 0) {
  208. __asm__("crc32b\t" "(%1), %0"
  209. : "=r"(crc0)
  210. : "r"(next), "0"(crc0));
  211. next++;
  212. len--;
  213. }
  214. /* compute the crc on sets of LONG*3 bytes, executing three independent crc
  215. instructions, each on LONG bytes -- this is optimized for the Nehalem,
  216. Westmere, Sandy Bridge, and Ivy Bridge architectures, which have a
  217. throughput of one crc per cycle, but a latency of three cycles */
  218. while (len >= LONG*3) {
  219. crc1 = 0;
  220. crc2 = 0;
  221. end = next + LONG;
  222. do {
  223. __asm__("crc32q\t" "(%3), %0\n\t"
  224. "crc32q\t" LONGx1 "(%3), %1\n\t"
  225. "crc32q\t" LONGx2 "(%3), %2"
  226. : "=r"(crc0), "=r"(crc1), "=r"(crc2)
  227. : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2));
  228. next += 8;
  229. } while (next < end);
  230. crc0 = crc32c_shift(crc32c_long, crc0) ^ crc1;
  231. crc0 = crc32c_shift(crc32c_long, crc0) ^ crc2;
  232. next += LONG*2;
  233. len -= LONG*3;
  234. }
  235. /* do the same thing, but now on SHORT*3 blocks for the remaining data less
  236. than a LONG*3 block */
  237. while (len >= SHORT*3) {
  238. crc1 = 0;
  239. crc2 = 0;
  240. end = next + SHORT;
  241. do {
  242. __asm__("crc32q\t" "(%3), %0\n\t"
  243. "crc32q\t" SHORTx1 "(%3), %1\n\t"
  244. "crc32q\t" SHORTx2 "(%3), %2"
  245. : "=r"(crc0), "=r"(crc1), "=r"(crc2)
  246. : "r"(next), "0"(crc0), "1"(crc1), "2"(crc2));
  247. next += 8;
  248. } while (next < end);
  249. crc0 = crc32c_shift(crc32c_short, crc0) ^ crc1;
  250. crc0 = crc32c_shift(crc32c_short, crc0) ^ crc2;
  251. next += SHORT*2;
  252. len -= SHORT*3;
  253. }
  254. /* compute the crc on the remaining eight-byte units less than a SHORT*3
  255. block */
  256. end = next + (len - (len & 7));
  257. while (next < end) {
  258. __asm__("crc32q\t" "(%1), %0"
  259. : "=r"(crc0)
  260. : "r"(next), "0"(crc0));
  261. next += 8;
  262. }
  263. len &= 7;
  264. /* compute the crc for up to seven trailing bytes */
  265. while (len) {
  266. __asm__("crc32b\t" "(%1), %0"
  267. : "=r"(crc0)
  268. : "r"(next), "0"(crc0));
  269. next++;
  270. len--;
  271. }
  272. /* return a post-processed crc */
  273. return (uint32_t)(crc0 ^ 0xffffffff);
  274. }