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