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