author | Claus Gittinger <cg@exept.de> |
Sun, 17 Mar 2019 17:09:26 +0100 | |
changeset 4871 | 32729bea327d |
parent 4869 | d90f0348f3ef |
child 4873 | 86aab8ca867c |
permissions | -rw-r--r-- |
4845 | 1 |
"{ Encoding: utf8 }" |
2 |
||
2416 | 3 |
" |
4 |
COPYRIGHT (c) 2003 by eXept Software AG |
|
2963 | 5 |
All Rights Reserved |
2416 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1975 | 14 |
"{ Package: 'stx:libbasic2' }" |
1192 | 15 |
|
4238 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
4845 | 18 |
CRCStream subclass:#CRC32Stream |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
1192 | 21 |
poolDictionaries:'' |
2324 | 22 |
category:'System-Crypt-Hashing' |
1192 | 23 |
! |
24 |
||
4871 | 25 |
CRC32Stream subclass:#CRC32Stream_Castagnoli |
26 |
instanceVariableNames:'' |
|
27 |
classVariableNames:'' |
|
28 |
poolDictionaries:'' |
|
29 |
privateIn:CRC32Stream |
|
30 |
! |
|
31 |
||
32 |
!CRC32Stream primitiveDefinitions! |
|
33 |
%{ |
|
34 |
||
35 |
/* |
|
36 |
* includes, defines, structure definitions |
|
37 |
* and typedefs come here. |
|
38 |
*/ |
|
39 |
#ifdef __LP64__ |
|
40 |
inline uint64_t _mm_crc32_u64(uint64_t crc, uint64_t value) { |
|
41 |
asm("crc32q %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value)); |
|
42 |
return crc; |
|
43 |
} |
|
44 |
#endif |
|
45 |
||
46 |
inline uint32_t _mm_crc32_u32(uint32_t crc, uint32_t value) { |
|
47 |
asm("crc32l %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value)); |
|
48 |
return crc; |
|
49 |
} |
|
50 |
||
51 |
inline uint32_t _mm_crc32_u16(uint32_t crc, uint16_t value) { |
|
52 |
asm("crc32w %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value)); |
|
53 |
return crc; |
|
54 |
} |
|
55 |
||
56 |
inline uint32_t _mm_crc32_u8(uint32_t crc, uint8_t value) { |
|
57 |
asm("crc32b %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value)); |
|
58 |
return crc; |
|
59 |
} |
|
60 |
%} |
|
61 |
! ! |
|
62 |
||
1192 | 63 |
!CRC32Stream class methodsFor:'documentation'! |
64 |
||
2416 | 65 |
copyright |
66 |
" |
|
67 |
COPYRIGHT (c) 2003 by eXept Software AG |
|
2963 | 68 |
All Rights Reserved |
2416 | 69 |
|
70 |
This software is furnished under a license and may be used |
|
71 |
only in accordance with the terms of that license and with the |
|
72 |
inclusion of the above copyright notice. This software may not |
|
73 |
be provided or otherwise made available to, or used by, any |
|
74 |
other person. No title to or ownership of the software is |
|
75 |
hereby transferred. |
|
76 |
" |
|
77 |
! |
|
78 |
||
1192 | 79 |
documentation |
80 |
" |
|
2963 | 81 |
Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42]. |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
82 |
The default CRC polynomial employed is |
1192 | 83 |
|
4238 | 84 |
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 |
85 |
(or 16r04C11DB7) |
|
1192 | 86 |
|
4871 | 87 |
You can also create an instace performing the Castagnoli CRC-32C |
4845 | 88 |
(used in iSCSI & SCTP [RFC3720], G.hn payload, SSE4.2): |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
89 |
|
4238 | 90 |
self newCrc32c |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
91 |
|
4845 | 92 |
poly: 16r1edc6f41 |
93 |
= x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
94 |
|
2746 | 95 |
Only use CRC to protect against communication errors; |
2310 | 96 |
do NOT use CRC for cryptography - use SHA1Stream or MD5Stream instead. |
97 |
||
4869 | 98 |
Notice that this CRC is also used with PNG images; |
99 |
therefore, its performance directly affects png image processing (png write speed). |
|
1192 | 100 |
|
2306 | 101 |
throughput: |
4869 | 102 |
235 Mb/s on MacBook Pro (2.6Ghz I7) |
4871 | 103 |
157 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
104 |
150 Mb/s on 2Ghz Duo |
|
105 |
new: |
|
106 |
500 Mb/s for castagnoli on MacBook Pro (2.6Ghz I7) |
|
1192 | 107 |
|
108 |
[author:] |
|
4238 | 109 |
Stefan Vogel (stefan@zwerg) |
1192 | 110 |
|
111 |
[instance variables:] |
|
112 |
||
113 |
[class variables:] |
|
114 |
||
115 |
[see also:] |
|
4238 | 116 |
SHA1Stream |
117 |
MD5Stream |
|
1192 | 118 |
|
119 |
" |
|
120 |
! |
|
121 |
||
122 |
examples |
|
123 |
" |
|
124 |
||
2306 | 125 |
expect 60C1D0A0 |
4238 | 126 |
[exBegin] |
1977 | 127 |
self information:(CRC32Stream hashValueOf:'resume') hexPrintString |
4238 | 128 |
[exEnd] |
1192 | 129 |
|
2306 | 130 |
expect 16r60C1D0A0 |
4238 | 131 |
[exBegin] |
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
132 |
self information:(CRC32Stream new |
4238 | 133 |
nextPut:$r; |
134 |
nextPut:$e; |
|
135 |
nextPut:$s; |
|
136 |
nextPut:$u; |
|
137 |
nextPut:$m; |
|
138 |
nextPut:$e; |
|
139 |
hashValue) hexPrintString |
|
140 |
[exEnd] |
|
1978
f7330488b12c
changed #nextPut: - multibyte caharcters
Stefan Vogel <sv@exept.de>
parents:
1977
diff
changeset
|
141 |
|
2306 | 142 |
expect 16r70E46888: |
4238 | 143 |
[exBegin] |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
144 |
self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString |
4238 | 145 |
[exEnd] |
1192 | 146 |
|
2306 | 147 |
expect 16r8CD04C73: |
4238 | 148 |
[exBegin] |
2963 | 149 |
self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
4238 | 150 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
151 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF |
|
152 |
16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString) |
|
153 |
[exEnd] |
|
2306 | 154 |
|
155 |
timing throughput: |
|
4871 | 156 |
230Mb/s (on MacBook Pro 2012 / 2.6Ghz I7) |
4238 | 157 |
[exBegin] |
2306 | 158 |
|hashStream n t| |
159 |
||
160 |
hashStream := CRC32Stream new. |
|
4869 | 161 |
n := 2000000. |
2306 | 162 |
t := Time millisecondsToRun:[ |
4238 | 163 |
n timesRepeat:[ |
164 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
165 |
]. |
|
166 |
]. |
|
2306 | 167 |
t := (t / 1000) asFloat. |
2702
bbfd7a7031a6
only one basic encoder: nextPutBytes:from:startingAt:
Claus Gittinger <cg@exept.de>
parents:
2572
diff
changeset
|
168 |
Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString. |
2306 | 169 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
170 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
4238 | 171 |
[exEnd] |
2306 | 172 |
|
4871 | 173 |
500Mb/s (on MacBook Pro 2012 / 2.6Ghz I7) |
174 |
[exBegin] |
|
175 |
|hashStream n t| |
|
176 |
||
177 |
hashStream := CRC32Stream newCastagnoli. |
|
178 |
n := 2000000. |
|
179 |
t := Time millisecondsToRun:[ |
|
180 |
n timesRepeat:[ |
|
181 |
hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
|
182 |
]. |
|
183 |
]. |
|
184 |
t := (t / 1000) asFloat. |
|
185 |
Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString. |
|
186 |
Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
|
187 |
Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
|
188 |
[exEnd] |
|
189 |
||
4845 | 190 |
test vectors from https://tools.ietf.org/html/rfc3720#page-217: |
191 |
||
192 |
expect 0 |
|
193 |
[exBegin] |
|
194 |
self information:(CRC32Stream newCrc32c hashValueOf:'') hexPrintString |
|
195 |
[exEnd] |
|
196 |
expect C1D04330 |
|
197 |
[exBegin] |
|
198 |
self information:(CRC32Stream newCrc32c hashValueOf:'a') hexPrintString |
|
199 |
[exEnd] |
|
200 |
expect E3069283 |
|
201 |
[exBegin] |
|
202 |
self information:(CRC32Stream newCrc32c hashValueOf:'123456789') hexPrintString |
|
203 |
[exEnd] |
|
204 |
expect 8A9136AA |
|
205 |
[exBegin] |
|
206 |
self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:0)) hexPrintString |
|
207 |
[exEnd] |
|
208 |
expect 62a8ab43 |
|
209 |
[exBegin] |
|
210 |
self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:16rFF)) hexPrintString |
|
211 |
[exEnd] |
|
212 |
expect 46dd794e |
|
213 |
[exBegin] |
|
214 |
self information:(CRC32Stream newCrc32c hashValueOf:(0 to:31) asByteArray) hexPrintString |
|
215 |
[exEnd] |
|
1192 | 216 |
|
4845 | 217 |
" |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
218 |
! ! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
219 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
220 |
!CRC32Stream class methodsFor:'instance creation'! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
221 |
|
4871 | 222 |
castagnoli_nextPutBytes:count from:anObject startingAt:start |
223 |
"add the hash of anObject to the computed hash so far. |
|
224 |
This uses an x86 crc instruction, |
|
225 |
and works only with the castagnoli polynom" |
|
226 |
||
227 |
%{ |
|
228 |
#if defined(__x86__) || defined(__x86_64__) |
|
229 |
# if defined(__clang__) || defined(__GNUC__) |
|
230 |
# pragma GCC push_options |
|
231 |
# pragma GCC target ("arch=core-avx2") |
|
232 |
||
233 |
if (__bothSmallInteger(count, start)) { |
|
234 |
int len, offs; |
|
235 |
int objSize; |
|
236 |
unsigned char *extPtr; |
|
237 |
||
238 |
len = __intVal(count); |
|
239 |
offs = __intVal(start) - 1; |
|
240 |
||
241 |
if (__isExternalBytesLike(anObject)) { |
|
242 |
OBJ sz = __externalBytesSize(anObject); |
|
243 |
||
244 |
extPtr = (unsigned char *)__externalBytesAddress(anObject); |
|
245 |
if (__isSmallInteger(sz)) { |
|
246 |
objSize = __intVal(sz); |
|
247 |
} else { |
|
248 |
objSize = 0; /* unknown */ |
|
249 |
} |
|
250 |
} else { |
|
251 |
int nInstVars, nInstBytes; |
|
252 |
OBJ oClass = __Class(anObject); |
|
253 |
||
254 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
255 |
case BYTEARRAY: |
|
256 |
case WORDARRAY: |
|
257 |
case LONGARRAY: |
|
258 |
case SWORDARRAY: |
|
259 |
case SLONGARRAY: |
|
260 |
case FLOATARRAY: |
|
261 |
case DOUBLEARRAY: |
|
262 |
break; |
|
263 |
default: |
|
264 |
goto bad; |
|
265 |
} |
|
266 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
267 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
268 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
269 |
// subtract from size and add to byte-pointer |
|
270 |
objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes; |
|
271 |
extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes; |
|
272 |
} |
|
273 |
||
274 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
275 |
unsigned int _crc; |
|
276 |
unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) ); |
|
277 |
unsigned char *cp = extPtr+offs; |
|
278 |
unsigned int n = len; |
|
279 |
||
280 |
if (__isSmallInteger(__INST(crc))) { |
|
281 |
_crc = (unsigned int) (__intVal( __INST(crc) )); |
|
282 |
} else { |
|
283 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
|
284 |
} |
|
285 |
||
286 |
#if 0 |
|
287 |
# if __POINTER_SIZE__ == 8 |
|
288 |
if (((unsigned INT)cp & 7) == 0) { |
|
289 |
// longword aligned |
|
290 |
for ( ; n >= 8 ; n -= 8, cp += 8) { |
|
291 |
unsigned INT lWord; |
|
292 |
unsigned char _idx; |
|
293 |
||
294 |
lWord = ((unsigned INT *)cp)[0]; |
|
295 |
_idx = (_crc ^ lWord) & 0xFF; |
|
296 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
297 |
_idx = (_crc ^ (lWord>>8)) & 0xFF; |
|
298 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
299 |
_idx = (_crc ^ (lWord>>16)) & 0xFF; |
|
300 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
301 |
_idx = (_crc ^ (lWord>>24)) & 0xFF; |
|
302 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
303 |
||
304 |
_idx = (_crc ^ (lWord>>32)) & 0xFF; |
|
305 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
306 |
_idx = (_crc ^ (lWord>>40)) & 0xFF; |
|
307 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
308 |
_idx = (_crc ^ (lWord>>48)) & 0xFF; |
|
309 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
310 |
_idx = (_crc ^ (lWord>>56)) & 0xFF; |
|
311 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
312 |
} |
|
313 |
} |
|
314 |
# endif // __POINTER_SIZE__ == 8 |
|
315 |
if (((unsigned INT)cp & 3) == 0) { |
|
316 |
// word aligned |
|
317 |
for ( ; n >= 4 ; n -= 4, cp += 4) { |
|
318 |
unsigned int word; |
|
319 |
unsigned char _idx; |
|
320 |
||
321 |
word = ((unsigned int *)cp)[0]; |
|
322 |
_idx = (_crc ^ word) & 0xFF; |
|
323 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
324 |
_idx = (_crc ^ (word>>8)) & 0xFF; |
|
325 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
326 |
_idx = (_crc ^ (word>>16)) & 0xFF; |
|
327 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
328 |
_idx = (_crc ^ (word>>24)) & 0xFF; |
|
329 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
330 |
} |
|
331 |
} |
|
332 |
#endif // 0 |
|
333 |
for ( ; n >= 4 ; n -= 4, cp += 4) { |
|
334 |
unsigned char _idx; |
|
335 |
||
336 |
_idx = (_crc ^ cp[0]) & 0xFF; |
|
337 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
338 |
_idx = (_crc ^ cp[1]) & 0xFF; |
|
339 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
340 |
_idx = (_crc ^ cp[2]) & 0xFF; |
|
341 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
342 |
_idx = (_crc ^ cp[3]) & 0xFF; |
|
343 |
_crc = _crcTable[_idx] ^ (_crc >> 8); |
|
344 |
} |
|
345 |
while (n-- > 0) { |
|
346 |
unsigned char ch = *cp++; |
|
347 |
||
348 |
_crc = __builtin_ia32_crc32qi(_crc, ch); |
|
349 |
} |
|
350 |
||
351 |
if ((__POINTER_SIZE__==8) || (_crc <= _MAX_INT)) { |
|
352 |
__INST(crc) = __MKSMALLINT(_crc); |
|
353 |
} else { |
|
354 |
// this code fails with gcc 4.7.2: |
|
355 |
// __INST(crc) = __MKUINT(_crc); __STORESELF(crc); |
|
356 |
OBJ temp = __MKUINT(_crc); |
|
357 |
__INST(crc) = temp; __STORESELF(crc); |
|
358 |
} |
|
359 |
||
360 |
RETURN (count); |
|
361 |
} |
|
362 |
} |
|
363 |
bad: ; |
|
364 |
# endif |
|
365 |
#endif |
|
366 |
%}. |
|
367 |
self primitiveFailed |
|
368 |
||
369 |
"Created: / 17-03-2019 / 15:36:45 / Claus Gittinger" |
|
370 |
! |
|
371 |
||
4845 | 372 |
generatorPolynomMSB:anInteger |
373 |
"notice, in literature, the generator polynom is usually specified as an MSB number" |
|
374 |
||
375 |
^ self generatorPolynom:(anInteger bitReversed32) |
|
1192 | 376 |
|
377 |
" |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
378 |
self assert:((self generatorPolynom:16r82F63B78) |
4845 | 379 |
nextPut:'123456789'; |
380 |
hashValue) = 16rE3069283 |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
381 |
" |
4845 | 382 |
|
383 |
"Created: / 16-03-2019 / 20:55:46 / Claus Gittinger" |
|
384 |
! |
|
385 |
||
386 |
generatorPolynomMSB:anInteger initValue:initValueArg |
|
387 |
"notice, in literature, the generator polynom is usually specified as an MSB number" |
|
388 |
||
389 |
^ self generatorPolynom:(anInteger bitReversed32) initValue:initValueArg |
|
390 |
||
391 |
"Created: / 16-03-2019 / 21:11:31 / Claus Gittinger" |
|
392 |
! |
|
393 |
||
394 |
generatorPolynomMSB:anInteger initValue:initValueArg xorOut:xorOut |
|
395 |
"notice, in literature, the generator polynom is usually specified as an MSB number" |
|
396 |
||
397 |
^ self generatorPolynom:(anInteger bitReversed32) initValue:initValueArg xorOut:xorOut |
|
398 |
||
399 |
"Created: / 16-03-2019 / 21:21:27 / Claus Gittinger" |
|
400 |
! |
|
401 |
||
402 |
new |
|
4869 | 403 |
"return an instance of the ITU-T CRC-32" |
4845 | 404 |
|
4857 | 405 |
^ self newCCITT |
406 |
||
407 |
"Created: / 16-03-2019 / 21:09:19 / Claus Gittinger" |
|
408 |
"Modified: / 16-03-2019 / 23:29:45 / Claus Gittinger" |
|
4869 | 409 |
"Modified (comment): / 17-03-2019 / 14:34:47 / Claus Gittinger" |
4857 | 410 |
! |
411 |
||
412 |
newCCITT |
|
413 |
"return an instance of the ITU-T CRC-32 |
|
414 |
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" |
|
415 |
||
416 |
"/ 16r4C11DB7 bitReversed32 -> 16rEDB88320 |
|
4869 | 417 |
^ self generatorPolynomMSB:16r4C11DB7 |
418 |
initValue:16rFFFFFFFF xorOut:16rFFFFFFFF |
|
4845 | 419 |
|
4857 | 420 |
"Created: / 16-03-2019 / 23:29:39 / Claus Gittinger" |
4869 | 421 |
"Modified: / 17-03-2019 / 14:07:36 / Claus Gittinger" |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
422 |
! |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
423 |
|
4869 | 424 |
newCastagnoli |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
425 |
"return an instance of the Castagnoli CRC-32 |
4845 | 426 |
x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1 |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
427 |
(used in iSCSI & SCTP, G.hn payload, SSE4.2)" |
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
428 |
|
4871 | 429 |
|impl| |
430 |
||
431 |
impl := (self canUseFastCRC) ifTrue:[CRC32Stream_Castagnoli] ifFalse:[self]. |
|
432 |
||
4845 | 433 |
"/ 16r1edc6f41 bitReversed32 -> 16r82F63B78 |
4871 | 434 |
^ impl generatorPolynomMSB:16r1edc6f41 |
4869 | 435 |
initValue:16rFFFFFFFF xorOut:16rFFFFFFFF |
436 |
||
437 |
" |
|
438 |
Castagnoli crc: |
|
439 |
self assert:((self newCrc32c) |
|
440 |
nextPut:'123456789'; |
|
441 |
hashValue) = 3808858755. '16rE3069283' |
|
442 |
||
443 |
default crc: |
|
444 |
self assert:((self new) |
|
445 |
nextPut:'123456789'; |
|
446 |
hashValue) = 3421780262. '16rCBF43926' |
|
447 |
" |
|
448 |
||
449 |
"Created: / 17-03-2019 / 14:33:54 / Claus Gittinger" |
|
4871 | 450 |
"Modified: / 17-03-2019 / 17:08:32 / Claus Gittinger" |
4869 | 451 |
! |
452 |
||
453 |
newCrc32c |
|
454 |
"return an instance of the Castagnoli CRC-32" |
|
455 |
||
456 |
^ self newCastagnoli |
|
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
457 |
|
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
458 |
" |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
459 |
Castagnoli crc: |
2572 | 460 |
self assert:((self newCrc32c) |
4845 | 461 |
nextPut:'123456789'; |
462 |
hashValue) = 3808858755. '16rE3069283' |
|
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
463 |
|
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
464 |
default crc: |
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
465 |
self assert:((self new) |
4845 | 466 |
nextPut:'123456789'; |
467 |
hashValue) = 3421780262. '16rCBF43926' |
|
1192 | 468 |
" |
2747
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
469 |
|
c6e4b8d6d9e7
comment/format in: #newCrc32c
Claus Gittinger <cg@exept.de>
parents:
2746
diff
changeset
|
470 |
"Modified (comment): / 17-05-2012 / 12:48:53 / cg" |
4869 | 471 |
"Modified: / 17-03-2019 / 14:34:26 / Claus Gittinger" |
1192 | 472 |
! ! |
473 |
||
4871 | 474 |
!CRC32Stream class methodsFor:'private'! |
475 |
||
476 |
canUseFastCRC |
|
477 |
%{ /* NOCONTEXT */ |
|
478 |
#if defined(__x86__) || defined(__x86_64__) |
|
479 |
# if defined(__clang__) || defined(__GNUC__) |
|
480 |
extern unsigned char __cpu_hasSSE4_2; |
|
481 |
||
482 |
RETURN (__cpu_hasSSE4_2 ? true : false); |
|
483 |
# endif |
|
484 |
#endif |
|
485 |
%}. |
|
486 |
^ false |
|
487 |
||
488 |
" |
|
489 |
self canUseFastCRC |
|
490 |
" |
|
491 |
||
492 |
"Created: / 17-03-2019 / 16:45:42 / Claus Gittinger" |
|
493 |
! ! |
|
494 |
||
4845 | 495 |
!CRC32Stream methodsFor:'initialization'! |
2570
55ce3e8bd7dc
Support additional generator polynoms (e.g. CRC-32C)
Stefan Vogel <sv@exept.de>
parents:
2569
diff
changeset
|
496 |
|
4864 | 497 |
generatorPolynom:anLSBInteger |
2571
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
498 |
"set the generator polynom for this instance. |
4864 | 499 |
set start and xorOut to 16rFFFFFFFF. |
2571
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
500 |
Note: you have to set the bit-reversed value, so the LSB must be first" |
12d5e280eea1
comment/format in: #generatorPolynom:
Stefan Vogel <sv@exept.de>
parents:
2570
diff
changeset
|
501 |
|
4864 | 502 |
self generatorPolynom:anLSBInteger |
503 |
initValue:16rFFFFFFFF xorOut:16rFFFFFFFF. |
|
4845 | 504 |
|
505 |
"Modified: / 16-03-2019 / 21:23:25 / Claus Gittinger" |
|
4864 | 506 |
"Modified (format): / 17-03-2019 / 14:02:38 / Claus Gittinger" |
2705 | 507 |
! |
508 |
||
4864 | 509 |
generatorPolynom:anLSBInteger initValue:initValueArg |
4845 | 510 |
"set the generator polynom for this instance. |
4864 | 511 |
set start to initValueArg and xorOut to 16rFFFFFFFF. |
4845 | 512 |
Note: you have to set the bit-reversed value, so the LSB must be first" |
1192 | 513 |
|
4864 | 514 |
self generatorPolynom:anLSBInteger |
515 |
initValue:initValueArg xorOut:16rFFFFFFFF |
|
1192 | 516 |
|
4845 | 517 |
"Created: / 16-03-2019 / 21:06:16 / Claus Gittinger" |
4864 | 518 |
"Modified (format): / 17-03-2019 / 14:02:31 / Claus Gittinger" |
1192 | 519 |
! ! |
520 |
||
4871 | 521 |
!CRC32Stream::CRC32Stream_Castagnoli class methodsFor:'documentation'! |
522 |
||
523 |
documentation |
|
524 |
" |
|
525 |
redefined to use the CPU's CRC instruction |
|
526 |
||
527 |
[author:] |
|
528 |
Claus Gittinger |
|
529 |
" |
|
530 |
! ! |
|
531 |
||
532 |
!CRC32Stream::CRC32Stream_Castagnoli methodsFor:'writing'! |
|
533 |
||
534 |
nextPutBytes:count from:anObject startingAt:start |
|
535 |
"add the hash of anObject to the computed hash so far. |
|
536 |
This uses the x86/x86_64 crc instruction, |
|
537 |
and works only for the castagnoli polynom" |
|
538 |
||
539 |
%{ /* NOCONTEXT */ |
|
540 |
#if defined(__x86__) || defined(__x86_64__) |
|
541 |
# if defined(__clang__) || defined(__GNUC__) |
|
542 |
||
543 |
if (__bothSmallInteger(count, start)) { |
|
544 |
int len, offs; |
|
545 |
int objSize; |
|
546 |
unsigned char *extPtr; |
|
547 |
||
548 |
len = __intVal(count); |
|
549 |
offs = __intVal(start) - 1; |
|
550 |
||
551 |
if (__isExternalBytesLike(anObject)) { |
|
552 |
OBJ sz = __externalBytesSize(anObject); |
|
553 |
||
554 |
extPtr = (unsigned char *)__externalBytesAddress(anObject); |
|
555 |
if (__isSmallInteger(sz)) { |
|
556 |
objSize = __intVal(sz); |
|
557 |
} else { |
|
558 |
objSize = 0; /* unknown */ |
|
559 |
} |
|
560 |
} else { |
|
561 |
int nInstVars, nInstBytes; |
|
562 |
OBJ oClass = __Class(anObject); |
|
563 |
||
564 |
switch (__intVal(__ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
565 |
case BYTEARRAY: |
|
566 |
case WORDARRAY: |
|
567 |
case LONGARRAY: |
|
568 |
case SWORDARRAY: |
|
569 |
case SLONGARRAY: |
|
570 |
case FLOATARRAY: |
|
571 |
case DOUBLEARRAY: |
|
572 |
break; |
|
573 |
default: |
|
574 |
goto bad; |
|
575 |
} |
|
576 |
nInstVars = __intVal(__ClassInstPtr(oClass)->c_ninstvars); |
|
577 |
nInstBytes = __OBJS2BYTES__(nInstVars); |
|
578 |
// nInstBytes is the number of bytes occupied by pointer instance variables |
|
579 |
// subtract from size and add to byte-pointer |
|
580 |
objSize = __qSize(anObject) - OHDR_SIZE - nInstBytes; |
|
581 |
extPtr = (unsigned char *)__byteArrayVal(anObject)+nInstBytes; |
|
582 |
} |
|
583 |
||
584 |
if ((offs >= 0) && (len >= 0) && (objSize >= (len + offs))) { |
|
585 |
unsigned int _crc; |
|
586 |
unsigned int *_crcTable = __integerArrayVal( __INST(crcTable) ); |
|
587 |
unsigned char *cp = extPtr+offs; |
|
588 |
unsigned int n = len; |
|
589 |
||
590 |
if (__isSmallInteger(__INST(crc))) { |
|
591 |
_crc = (unsigned int) (__intVal( __INST(crc) )); |
|
592 |
} else { |
|
593 |
_crc = __unsignedLongIntVal( __INST(crc) ); |
|
594 |
} |
|
595 |
||
596 |
# if __POINTER_SIZE__ == 8 |
|
597 |
if (((unsigned INT)cp & 7) == 0) { |
|
598 |
// longword aligned |
|
599 |
for ( ; n >= 32 ; n -= 32, cp += 32) { |
|
600 |
unsigned INT w0 = ((unsigned INT *)cp)[0]; |
|
601 |
unsigned INT w1, w2, w3; |
|
602 |
||
603 |
_crc = _mm_crc32_u64(_crc, w0); |
|
604 |
w1 = ((unsigned INT *)cp)[1]; |
|
605 |
_crc = _mm_crc32_u64(_crc, w1); |
|
606 |
w2 = ((unsigned INT *)cp)[2]; |
|
607 |
_crc = _mm_crc32_u64(_crc, w2); |
|
608 |
w3 = ((unsigned INT *)cp)[3]; |
|
609 |
_crc = _mm_crc32_u64(_crc, w3); |
|
610 |
} |
|
611 |
for ( ; n >= 8 ; n -= 8, cp += 8) { |
|
612 |
_crc = _mm_crc32_u64(_crc, ((unsigned INT *)cp)[0]); |
|
613 |
} |
|
614 |
} |
|
615 |
# endif // __POINTER_SIZE__ == 8 |
|
616 |
if (((unsigned INT)cp & 3) == 0) { |
|
617 |
// word aligned |
|
618 |
for ( ; n >= 4 ; n -= 4, cp += 4) { |
|
619 |
_crc = _mm_crc32_u32(_crc, ((unsigned int *)cp)[0]); |
|
620 |
} |
|
621 |
} |
|
622 |
for ( ; n >= 4 ; n -= 4, cp += 4) { |
|
623 |
_crc = _mm_crc32_u8(_crc, cp[0]); |
|
624 |
_crc = _mm_crc32_u8(_crc, cp[1]); |
|
625 |
_crc = _mm_crc32_u8(_crc, cp[2]); |
|
626 |
_crc = _mm_crc32_u8(_crc, cp[3]); |
|
627 |
} |
|
628 |
while (n-- > 0) { |
|
629 |
unsigned char ch = *cp++; |
|
630 |
||
631 |
_crc = _mm_crc32_u8(_crc, ch); |
|
632 |
} |
|
633 |
||
634 |
if ((__POINTER_SIZE__==8) || (_crc <= _MAX_INT)) { |
|
635 |
__INST(crc) = __MKSMALLINT(_crc); |
|
636 |
} else { |
|
637 |
// this code fails with gcc 4.7.2: |
|
638 |
// __INST(crc) = __MKUINT(_crc); __STORESELF(crc); |
|
639 |
OBJ temp = __MKUINT(_crc); |
|
640 |
__INST(crc) = temp; __STORESELF(crc); |
|
641 |
} |
|
642 |
||
643 |
RETURN (count); |
|
644 |
} |
|
645 |
} |
|
646 |
bad: ; |
|
647 |
# endif |
|
648 |
#endif |
|
649 |
%}. |
|
650 |
self primitiveFailed |
|
651 |
||
652 |
"Created: / 17-03-2019 / 16:44:27 / Claus Gittinger" |
|
653 |
! ! |
|
654 |
||
1192 | 655 |
!CRC32Stream class methodsFor:'documentation'! |
656 |
||
657 |
version |
|
4238 | 658 |
^ '$Header$' |
2306 | 659 |
! |
660 |
||
661 |
version_CVS |
|
4238 | 662 |
^ '$Header$' |
1192 | 663 |
! ! |
664 |