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