Simplified distributed block storage with strong consistency, like in Ceph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

375 lines
11 KiB

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