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