[go: up one dir, main page]

Menu

[f3b279]: / mfscommon / crc.c  Maximize  Restore  History

Download this file

235 lines (213 with data), 5.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (C) 2025 Jakub Kruszona-Zawadzki, Saglabs SA
*
* This file is part of MooseFS.
*
* MooseFS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 (only).
*
* MooseFS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MooseFS; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
* or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <inttypes.h>
#include <stdlib.h>
#include "MFSCommunication.h"
#ifndef CRC_POLY
#define CRC_POLY 0xEDB88320
#endif
static uint32_t crc_table[16][256];
static void crc_generate_main_tables(void) {
uint32_t c,poly,i;
uint32_t j;
poly = CRC_POLY;
for (i=0; i<256; i++) {
c=i;
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
c = (c&1) ? (poly^(c>>1)) : (c>>1);
crc_table[0][i] = c;
}
for (i=0; i<256; i++) {
c = crc_table[0][i];
for (j=1 ; j<16 ; j++) {
c = crc_table[0][c&0xff]^(c>>8);
crc_table[j][i] = c;
}
}
}
// based on code bublished by Stephan Brumme on his page: http://create.stephan-brumme.com/crc32/
// 16 byte slicing version by Bulat Ziganshin
#ifdef WORDS_BIGENDIAN
static inline uint32_t swap(uint32_t x) {
#if defined(__GNUC__) || defined(__clang__)
return __builtin_bswap32(x);
#else
return (x >> 24) |
((x >> 8) & 0x0000FF00) |
((x << 8) & 0x00FF0000) |
(x << 24);
#endif
}
#endif
// #define CRC_PREFETCH 1
uint32_t mycrc32(uint32_t crc,const void* data,uint32_t leng) {
const uint32_t *data4 = (const uint32_t*)data;
const uint8_t *data1;
uint32_t d0,d1,d2,d3;
#ifdef WORDS_BIGENDIAN
#define CRC_BLOCK { \
d0 = *data4++ ^ swap(crc); \
d1 = *data4++; \
d2 = *data4++; \
d3 = *data4++; \
crc = crc_table[ 0][ d3 & 0xFF] ^ \
crc_table[ 1][(d3 >> 8) & 0xFF] ^ \
crc_table[ 2][(d3 >> 16) & 0xFF] ^ \
crc_table[ 3][(d3 >> 24) & 0xFF] ^ \
crc_table[ 4][ d2 & 0xFF] ^ \
crc_table[ 5][(d2 >> 8) & 0xFF] ^ \
crc_table[ 6][(d2 >> 16) & 0xFF] ^ \
crc_table[ 7][(d2 >> 24) & 0xFF] ^ \
crc_table[ 8][ d1 & 0xFF] ^ \
crc_table[ 9][(d1 >> 8) & 0xFF] ^ \
crc_table[10][(d1 >> 16) & 0xFF] ^ \
crc_table[11][(d1 >> 24) & 0xFF] ^ \
crc_table[12][ d0 & 0xFF] ^ \
crc_table[13][(d0 >> 8) & 0xFF] ^ \
crc_table[14][(d0 >> 16) & 0xFF] ^ \
crc_table[15][(d0 >> 24) & 0xFF]; \
}
#else
#define CRC_BLOCK { \
d0 = *data4++ ^ crc; \
d1 = *data4++; \
d2 = *data4++; \
d3 = *data4++; \
crc = crc_table[ 0][(d3 >> 24) & 0xFF] ^ \
crc_table[ 1][(d3 >> 16) & 0xFF] ^ \
crc_table[ 2][(d3 >> 8) & 0xFF] ^ \
crc_table[ 3][ d3 & 0xFF] ^ \
crc_table[ 4][(d2 >> 24) & 0xFF] ^ \
crc_table[ 5][(d2 >> 16) & 0xFF] ^ \
crc_table[ 6][(d2 >> 8) & 0xFF] ^ \
crc_table[ 7][ d2 & 0xFF] ^ \
crc_table[ 8][(d1 >> 24) & 0xFF] ^ \
crc_table[ 9][(d1 >> 16) & 0xFF] ^ \
crc_table[10][(d1 >> 8) & 0xFF] ^ \
crc_table[11][ d1 & 0xFF] ^ \
crc_table[12][(d0 >> 24) & 0xFF] ^ \
crc_table[13][(d0 >> 16) & 0xFF] ^ \
crc_table[14][(d0 >> 8) & 0xFF] ^ \
crc_table[15][ d0 & 0xFF]; \
}
#endif
crc = ~crc;
#ifdef __clang__
while (leng >= 16) {
CRC_BLOCK
leng -= 16;
}
#else
while (leng >= 64) {
#if defined(CRC_PREFETCH) && defined(__GNUC__)
__builtin_prefetch(((const uint8_t*)data4)+256);
#endif
CRC_BLOCK
CRC_BLOCK
CRC_BLOCK
CRC_BLOCK
leng -= 64;
}
#endif
data1 = (const uint8_t*)data4;
while (leng-- != 0) {
crc = (crc >> 8) ^ crc_table[0][(crc & 0xFF) ^ *data1++];
}
return ~crc;
}
/* crc_combine */
static uint32_t crc_combine_table[32][4][256];
static void crc_matrix_square(uint32_t sqr[32], uint32_t m[32]) {
uint32_t i,j,s,v;
for (i=0; i<32; i++) {
for (j=0,s=0,v=m[i] ; v && j<32 ; j++, v>>=1) {
if (v&1) {
s^=m[j];
}
}
sqr[i] = s;
}
}
static void crc_generate_combine_tables(void) {
uint32_t i,j,k,l,sum;
uint32_t m1[32],m2[32],*mc,*m;
m1[0]=CRC_POLY;
j=1;
for (i=1 ; i<32 ; i++) {
m1[i]=j;
j<<=1;
}
crc_matrix_square(m2,m1); // 1 bit -> 2 bits
crc_matrix_square(m1,m2); // 2 bits -> 4 bits
for (i=0 ; i<32 ; i++) {
if (i&1) {
crc_matrix_square(m1,m2);
mc = m1;
} else {
crc_matrix_square(m2,m1);
mc = m2;
}
for (j=0 ; j<4 ; j++) {
for (k=0 ; k<256 ; k++) {
sum = 0;
l=k;
m=mc+(j*8);
while (l) {
if (l&1) {
sum ^= *m;
}
l>>=1;
m++;
}
crc_combine_table[i][j][k]=sum;
}
}
}
}
uint32_t mycrc32_combine(uint32_t crc1, uint32_t crc2, uint32_t leng2) {
uint8_t i;
/* add leng2 zeros to crc1 */
i=0;
while (leng2) {
if (leng2&1) {
crc1 = crc_combine_table[i][3][(crc1>>24)] \
^ crc_combine_table[i][2][(crc1>>16)&0xFF] \
^ crc_combine_table[i][1][(crc1>>8)&0xFF] \
^ crc_combine_table[i][0][crc1&0xFF];
}
i++;
leng2>>=1;
};
/* then combine crc1 and crc2 as output */
return crc1^crc2;
}
void mycrc32_init(void) {
crc_generate_main_tables();
crc_generate_combine_tables();
}