aos_crc64.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* aos_crc64.c -- compute CRC-64
  2. * Copyright (C) 2013 Mark Adler
  3. * Version 1.4 16 Dec 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. /* Compute CRC-64 in the manner of xz, using the ECMA-182 polynomial,
  23. bit-reversed, with one's complement pre and post processing. Provide a
  24. means to combine separately computed CRC-64's. */
  25. /* Version history:
  26. 1.0 13 Dec 2013 First version
  27. 1.1 13 Dec 2013 Fix comments in test code
  28. 1.2 14 Dec 2013 Determine endianess at run time
  29. 1.3 15 Dec 2013 Add eight-byte processing for big endian as well
  30. Make use of the pthread library optional
  31. 1.4 16 Dec 2013 Make once variable volatile for limited thread protection
  32. */
  33. #include "aos_crc64.h"
  34. #include <pthread.h>
  35. /* 64-bit CRC polynomial with these coefficients, but reversed:
  36. 64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37, 35, 33, 32,
  37. 31, 29, 27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7, 4, 1, 0 */
  38. #define POLY 0xc96c5795d7870f42
  39. /* Tables for CRC calculation -- filled in by initialization functions that are
  40. called once. These could be replaced by constant tables generated in the
  41. same way. There are two tables, one for each endianess. Since these are
  42. static, i.e. local, one should be compiled out of existence if the compiler
  43. can evaluate the endianess check in crc64() at compile time. */
  44. static uint64_t crc64_little_table[8][256];
  45. static uint64_t crc64_big_table[8][256];
  46. /* Fill in the CRC-64 constants table. */
  47. static void crc64_init(uint64_t table[][256])
  48. {
  49. unsigned n, k;
  50. uint64_t crc;
  51. /* generate CRC-64's for all single byte sequences */
  52. for (n = 0; n < 256; n++) {
  53. crc = n;
  54. for (k = 0; k < 8; k++)
  55. crc = crc & 1 ? POLY ^ (crc >> 1) : crc >> 1;
  56. table[0][n] = crc;
  57. }
  58. /* generate CRC-64's for those followed by 1 to 7 zeros */
  59. for (n = 0; n < 256; n++) {
  60. crc = table[0][n];
  61. for (k = 1; k < 8; k++) {
  62. crc = table[0][crc & 0xff] ^ (crc >> 8);
  63. table[k][n] = crc;
  64. }
  65. }
  66. }
  67. /* This function is called once to initialize the CRC-64 table for use on a
  68. little-endian architecture. */
  69. static void crc64_little_init(void)
  70. {
  71. crc64_init(crc64_little_table);
  72. }
  73. /* Reverse the bytes in a 64-bit word. */
  74. static inline uint64_t rev8(uint64_t a)
  75. {
  76. uint64_t m;
  77. m = 0xff00ff00ff00ff;
  78. a = ((a >> 8) & m) | (a & m) << 8;
  79. m = 0xffff0000ffff;
  80. a = ((a >> 16) & m) | (a & m) << 16;
  81. return a >> 32 | a << 32;
  82. }
  83. /* This function is called once to initialize the CRC-64 table for use on a
  84. big-endian architecture. */
  85. static void crc64_big_init(void)
  86. {
  87. unsigned k, n;
  88. crc64_init(crc64_big_table);
  89. for (k = 0; k < 8; k++)
  90. for (n = 0; n < 256; n++)
  91. crc64_big_table[k][n] = rev8(crc64_big_table[k][n]);
  92. }
  93. /* Run the init() function exactly once. If pthread.h is not included, then
  94. this macro will use a simple static state variable for the purpose, which is
  95. not thread-safe. The init function must be of the type void init(void). */
  96. #ifdef PTHREAD_ONCE_INIT
  97. # define ONCE(init) \
  98. do { \
  99. static pthread_once_t once = PTHREAD_ONCE_INIT; \
  100. pthread_once(&once, init); \
  101. } while (0)
  102. #else
  103. # define ONCE(init) \
  104. do { \
  105. static volatile int once = 1; \
  106. if (once) { \
  107. if (once++ == 1) { \
  108. init(); \
  109. once = 0; \
  110. } \
  111. else \
  112. while (once) \
  113. ; \
  114. } \
  115. } while (0)
  116. #endif
  117. /* Calculate a CRC-64 eight bytes at a time on a little-endian architecture. */
  118. static inline uint64_t crc64_little(uint64_t crc, void *buf, size_t len)
  119. {
  120. unsigned char *next = buf;
  121. ONCE(crc64_little_init);
  122. crc = ~crc;
  123. while (len && ((uintptr_t)next & 7) != 0) {
  124. crc = crc64_little_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
  125. len--;
  126. }
  127. while (len >= 8) {
  128. crc ^= *(uint64_t *)next;
  129. crc = crc64_little_table[7][crc & 0xff] ^
  130. crc64_little_table[6][(crc >> 8) & 0xff] ^
  131. crc64_little_table[5][(crc >> 16) & 0xff] ^
  132. crc64_little_table[4][(crc >> 24) & 0xff] ^
  133. crc64_little_table[3][(crc >> 32) & 0xff] ^
  134. crc64_little_table[2][(crc >> 40) & 0xff] ^
  135. crc64_little_table[1][(crc >> 48) & 0xff] ^
  136. crc64_little_table[0][crc >> 56];
  137. next += 8;
  138. len -= 8;
  139. }
  140. while (len) {
  141. crc = crc64_little_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);
  142. len--;
  143. }
  144. return ~crc;
  145. }
  146. /* Calculate a CRC-64 eight bytes at a time on a big-endian architecture. */
  147. static inline uint64_t crc64_big(uint64_t crc, void *buf, size_t len)
  148. {
  149. unsigned char *next = buf;
  150. ONCE(crc64_big_init);
  151. crc = ~rev8(crc);
  152. while (len && ((uintptr_t)next & 7) != 0) {
  153. crc = crc64_big_table[0][(crc >> 56) ^ *next++] ^ (crc << 8);
  154. len--;
  155. }
  156. while (len >= 8) {
  157. crc ^= *(uint64_t *)next;
  158. crc = crc64_big_table[0][crc & 0xff] ^
  159. crc64_big_table[1][(crc >> 8) & 0xff] ^
  160. crc64_big_table[2][(crc >> 16) & 0xff] ^
  161. crc64_big_table[3][(crc >> 24) & 0xff] ^
  162. crc64_big_table[4][(crc >> 32) & 0xff] ^
  163. crc64_big_table[5][(crc >> 40) & 0xff] ^
  164. crc64_big_table[6][(crc >> 48) & 0xff] ^
  165. crc64_big_table[7][crc >> 56];
  166. next += 8;
  167. len -= 8;
  168. }
  169. while (len) {
  170. crc = crc64_big_table[0][(crc >> 56) ^ *next++] ^ (crc << 8);
  171. len--;
  172. }
  173. return ~rev8(crc);
  174. }
  175. /* Return the CRC-64 of buf[0..len-1] with initial crc, processing eight bytes
  176. at a time. This selects one of two routines depending on the endianess of
  177. the architecture. A good optimizing compiler will determine the endianess
  178. at compile time if it can, and get rid of the unused code and table. If the
  179. endianess can be changed at run time, then this code will handle that as
  180. well, initializing and using two tables, if called upon to do so. */
  181. uint64_t aos_crc64(uint64_t crc, void *buf, size_t len)
  182. {
  183. uint64_t n = 1;
  184. return *(char *)&n ? crc64_little(crc, buf, len) :
  185. crc64_big(crc, buf, len);
  186. }
  187. #define GF2_DIM 64 /* dimension of GF(2) vectors (length of CRC) */
  188. static uint64_t gf2_matrix_times(uint64_t *mat, uint64_t vec)
  189. {
  190. uint64_t sum;
  191. sum = 0;
  192. while (vec) {
  193. if (vec & 1)
  194. sum ^= *mat;
  195. vec >>= 1;
  196. mat++;
  197. }
  198. return sum;
  199. }
  200. static void gf2_matrix_square(uint64_t *square, uint64_t *mat)
  201. {
  202. unsigned n;
  203. for (n = 0; n < GF2_DIM; n++)
  204. square[n] = gf2_matrix_times(mat, mat[n]);
  205. }
  206. /* Return the CRC-64 of two sequential blocks, where crc1 is the CRC-64 of the
  207. first block, crc2 is the CRC-64 of the second block, and len2 is the length
  208. of the second block. */
  209. uint64_t aos_crc64_combine(uint64_t crc1, uint64_t crc2, uintmax_t len2)
  210. {
  211. unsigned n;
  212. uint64_t row;
  213. uint64_t even[GF2_DIM]; /* even-power-of-two zeros operator */
  214. uint64_t odd[GF2_DIM]; /* odd-power-of-two zeros operator */
  215. /* degenerate case */
  216. if (len2 == 0)
  217. return crc1;
  218. /* put operator for one zero bit in odd */
  219. odd[0] = POLY; /* CRC-64 polynomial */
  220. row = 1;
  221. for (n = 1; n < GF2_DIM; n++) {
  222. odd[n] = row;
  223. row <<= 1;
  224. }
  225. /* put operator for two zero bits in even */
  226. gf2_matrix_square(even, odd);
  227. /* put operator for four zero bits in odd */
  228. gf2_matrix_square(odd, even);
  229. /* apply len2 zeros to crc1 (first square will put the operator for one
  230. zero byte, eight zero bits, in even) */
  231. do {
  232. /* apply zeros operator for this bit of len2 */
  233. gf2_matrix_square(even, odd);
  234. if (len2 & 1)
  235. crc1 = gf2_matrix_times(even, crc1);
  236. len2 >>= 1;
  237. /* if no more bits set, then done */
  238. if (len2 == 0)
  239. break;
  240. /* another iteration of the loop with odd and even swapped */
  241. gf2_matrix_square(odd, even);
  242. if (len2 & 1)
  243. crc1 = gf2_matrix_times(odd, crc1);
  244. len2 >>= 1;
  245. /* if no more bits set, then done */
  246. } while (len2 != 0);
  247. /* return combined crc */
  248. crc1 ^= crc2;
  249. return crc1;
  250. }