CRC32Stream.st
changeset 4884 e5f679d1e592
parent 4883 823874567cd3
child 4885 f23f8cf58b0c
equal deleted inserted replaced
4883:823874567cd3 4884:e5f679d1e592
    42 # if defined(__clang__)
    42 # if defined(__clang__)
    43 #  define HAS_CRC
    43 #  define HAS_CRC
    44 # endif
    44 # endif
    45 
    45 
    46 # if defined(__MINGW__)
    46 # if defined(__MINGW__)
    47 #  ifdef __LP64__
       
    48 #   define uint64_t       __uint64__
       
    49 #  endif
       
    50    typedef unsigned int   uint32_t;
    47    typedef unsigned int   uint32_t;
    51    typedef unsigned short uint16_t;
    48    typedef unsigned short uint16_t;
    52    typedef unsigned char  uint8_t;
    49    typedef unsigned char  uint8_t;
    53 # endif
    50 # endif
    54 
    51 
    55 #ifdef HAS_CRC
    52 #ifdef HAS_CRC
    56 
    53 
    57 # ifdef __LP64__
    54 # ifdef __LP64__
    58 static inline uint64_t __crc32_u64(uint64_t crc, uint64_t value) {
    55 static inline __uint64__
       
    56 __crc32_u64(__uint64__ crc, __uint64__ value) {
    59   asm("crc32q %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value));
    57   asm("crc32q %[value], %[crc]\n" : [crc] "+r" (crc) : [value] "rm" (value));
    60   return crc;
    58   return crc;
    61 }
    59 }
    62 # endif
    60 # endif
    63 
    61 
   103     - use secure hashes for those instead.
   101     - use secure hashes for those instead.
   104 
   102 
   105     Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42].
   103     Standard CRC method as defined by ISO 3309 [ISO-3309] or ITU-T V.42 [ITU-T-V42].
   106     The default CRC polynomial employed is
   104     The default CRC polynomial employed is
   107 
   105 
   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
   106 	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)
   107 	(or 16r04C11DB7)
   110 
   108 
   111     You can also create an instace performing the Castagnoli CRC-32C
   109     You can also create an instace performing the Castagnoli CRC-32C
   112     (used in iSCSI & SCTP [RFC3720], G.hn payload, SSE4.2):
   110     (used in iSCSI & SCTP [RFC3720], G.hn payload, SSE4.2):
   113 
   111 
   114         self newCrc32c
   112 	self newCrc32c
   115 
   113 
   116         poly: 16r1edc6f41
   114 	poly: 16r1edc6f41
   117         = x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1
   115 	= x32 + x28 + x27 + x26 + x25 + x23 + x22 + x20 + x19 + x18 + x14 + x13 + x11 + x10 + x9 + x8 + x6 + 1
   118 
   116 
   119     Notice that this CRC is also used with PNG images;
   117     Notice that this CRC is also used with PNG images;
   120     therefore, its performance directly affects png image processing (png write speed).
   118     therefore, its performance directly affects png image processing (png write speed).
   121 
   119 
   122     throughput:
   120     throughput:
   123         235 Mb/s on MacBook Pro (2.6Ghz I7) (360 Mb/s for big chunks)
   121 	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)
   122 	157 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit)
   125         150 Mb/s on 2Ghz Duo
   123 	150 Mb/s on 2Ghz Duo
   126      new:
   124      new:
   127         500 Mb/s for castagnoli on MacBook Pro (2.6Ghz I7) (5Gb/s for big chunks)
   125 	500 Mb/s for castagnoli on MacBook Pro (2.6Ghz I7) (5Gb/s for big chunks)
   128 
   126 
   129     [author:]
   127     [author:]
   130         Stefan Vogel (stefan@zwerg)
   128 	Stefan Vogel (stefan@zwerg)
   131 
   129 
   132     [instance variables:]
   130     [instance variables:]
   133 
   131 
   134     [class variables:]
   132     [class variables:]
   135 
   133 
   136     [see also:]
   134     [see also:]
   137         SHA1Stream
   135 	SHA1Stream
   138         MD5Stream
   136 	MD5Stream
   139 
   137 
   140 "
   138 "
   141 !
   139 !
   142 
   140 
   143 examples
   141 examples
   144 "
   142 "
   145 
   143 
   146   expect 60C1D0A0
   144   expect 60C1D0A0
   147                                                                 [exBegin]
   145 								[exBegin]
   148     self information:(CRC32Stream hashValueOf:'resume') hexPrintString
   146     self information:(CRC32Stream hashValueOf:'resume') hexPrintString
   149                                                                 [exEnd]
   147 								[exEnd]
   150 
   148 
   151   expect 16r60C1D0A0
   149   expect 16r60C1D0A0
   152                                                                 [exBegin]
   150 								[exBegin]
   153     self information:(CRC32Stream new
   151     self information:(CRC32Stream new
   154                             nextPut:$r;
   152 			    nextPut:$r;
   155                             nextPut:$e;
   153 			    nextPut:$e;
   156                             nextPut:$s;
   154 			    nextPut:$s;
   157                             nextPut:$u;
   155 			    nextPut:$u;
   158                             nextPut:$m;
   156 			    nextPut:$m;
   159                             nextPut:$e;
   157 			    nextPut:$e;
   160                             hashValue) hexPrintString
   158 			    hashValue) hexPrintString
   161                                                                 [exEnd]
   159 								[exEnd]
   162 
   160 
   163   expect 16r70E46888:
   161   expect 16r70E46888:
   164                                                                 [exBegin]
   162 								[exBegin]
   165     self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
   163     self information:(CRC32Stream hashValueOf:#[1 2 3 4 5 6 7]) hexPrintString
   166                                                                 [exEnd]
   164 								[exEnd]
   167 
   165 
   168   expect 16r8CD04C73:
   166   expect 16r8CD04C73:
   169                                                                 [exBegin]
   167 								[exBegin]
   170     self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
   168     self information:((CRC32Stream hashValueOf:#[16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
   171              16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
   169 	     16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
   172              16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
   170 	     16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF
   173              16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
   171 	     16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF 16rFF]) hexPrintString)
   174                                                                 [exEnd]
   172 								[exEnd]
   175 
   173 
   176   timing throughput:
   174   timing throughput:
   177   230Mb/s (on MacBook Pro 2012 / 2.6Ghz I7)
   175   230Mb/s (on MacBook Pro 2012 / 2.6Ghz I7)
   178                                                                 [exBegin]
   176 								[exBegin]
   179     |hashStream n t|
   177     |hashStream n t|
   180 
   178 
   181     hashStream := CRC32Stream new.
   179     hashStream := CRC32Stream new.
   182     n := 2000000.
   180     n := 2000000.
   183     t := Time millisecondsToRun:[
   181     t := Time millisecondsToRun:[
   184             n timesRepeat:[
   182 	    n timesRepeat:[
   185                 hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
   183 		hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
   186             ].
   184 	    ].
   187          ].
   185 	 ].
   188     t := (t / 1000) asFloat.
   186     t := (t / 1000) asFloat.
   189     Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
   187     Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
   190     Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
   188     Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
   191     Transcript show:(n*50/1024 / t); showCR:' Kb/s'
   189     Transcript show:(n*50/1024 / t); showCR:' Kb/s'
   192                                                                 [exEnd]
   190 								[exEnd]
   193 
   191 
   194   500Mb/s (on MacBook Pro 2012 / 2.6Ghz I7)
   192   500Mb/s (on MacBook Pro 2012 / 2.6Ghz I7)
   195                                                                 [exBegin]
   193 								[exBegin]
   196     |hashStream n t|
   194     |hashStream n t|
   197 
   195 
   198     hashStream := CRC32Stream newCastagnoli.
   196     hashStream := CRC32Stream newCastagnoli.
   199     n := 2000000.
   197     n := 2000000.
   200     t := Time millisecondsToRun:[
   198     t := Time millisecondsToRun:[
   201             n timesRepeat:[
   199 	    n timesRepeat:[
   202                 hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
   200 		hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'.
   203             ].
   201 	    ].
   204          ].
   202 	 ].
   205     t := (t / 1000) asFloat.
   203     t := (t / 1000) asFloat.
   206     Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
   204     Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
   207     Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
   205     Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'.
   208     Transcript show:(n*50/1024 / t); showCR:' Kb/s'
   206     Transcript show:(n*50/1024 / t); showCR:' Kb/s'
   209                                                                 [exEnd]
   207 								[exEnd]
   210   the real speed is shown with longer inputs...
   208   the real speed is shown with longer inputs...
   211                                                                 [exBegin]
   209 								[exBegin]
   212     |hashStream n t l s|
   210     |hashStream n t l s|
   213 
   211 
   214     hashStream := CRC32Stream newCastagnoli.
   212     hashStream := CRC32Stream newCastagnoli.
   215     n := 20000.
   213     n := 20000.
   216     s := '1234567890' ,* 10000.
   214     s := '1234567890' ,* 10000.
   217     l := s size.
   215     l := s size.
   218     t := Time millisecondsToRun:[
   216     t := Time millisecondsToRun:[
   219             n timesRepeat:[
   217 	    n timesRepeat:[
   220                 hashStream nextPutAll:s
   218 		hashStream nextPutAll:s
   221             ].
   219 	    ].
   222          ].
   220 	 ].
   223     t := (t / 1000) asFloat.
   221     t := (t / 1000) asFloat.
   224     Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
   222     Transcript show:'crc32:'; showCR: hashStream hashValue hexPrintString.
   225     Transcript show:t; show:' seconds for '; show:(l*n/1024) asFloat; showCR:' Kb'.
   223     Transcript show:t; show:' seconds for '; show:(l*n/1024) asFloat; showCR:' Kb'.
   226     Transcript show:(n*l/1024/1024 / t); showCR:' Mb/s'
   224     Transcript show:(n*l/1024/1024 / t); showCR:' Mb/s'
   227                                                                 [exEnd]
   225 								[exEnd]
   228 
   226 
   229   test vectors from https://tools.ietf.org/html/rfc3720#page-217:
   227   test vectors from https://tools.ietf.org/html/rfc3720#page-217:
   230 
   228 
   231   expect 0
   229   expect 0
   232                                                                 [exBegin]
   230 								[exBegin]
   233     self information:(CRC32Stream newCrc32c hashValueOf:'') hexPrintString
   231     self information:(CRC32Stream newCrc32c hashValueOf:'') hexPrintString
   234                                                                 [exEnd]
   232 								[exEnd]
   235   expect C1D04330
   233   expect C1D04330
   236                                                                 [exBegin]
   234 								[exBegin]
   237     self information:(CRC32Stream newCrc32c hashValueOf:'a') hexPrintString
   235     self information:(CRC32Stream newCrc32c hashValueOf:'a') hexPrintString
   238                                                                 [exEnd]
   236 								[exEnd]
   239   expect E3069283
   237   expect E3069283
   240                                                                 [exBegin]
   238 								[exBegin]
   241     self information:(CRC32Stream newCrc32c hashValueOf:'123456789') hexPrintString
   239     self information:(CRC32Stream newCrc32c hashValueOf:'123456789') hexPrintString
   242                                                                 [exEnd]
   240 								[exEnd]
   243   expect 8A9136AA
   241   expect 8A9136AA
   244                                                                 [exBegin]
   242 								[exBegin]
   245     self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:0)) hexPrintString
   243     self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:0)) hexPrintString
   246                                                                 [exEnd]
   244 								[exEnd]
   247   expect 62a8ab43
   245   expect 62a8ab43
   248                                                                 [exBegin]
   246 								[exBegin]
   249     self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:16rFF)) hexPrintString
   247     self information:(CRC32Stream newCrc32c hashValueOf:(ByteArray new:32 withAll:16rFF)) hexPrintString
   250                                                                 [exEnd]
   248 								[exEnd]
   251   expect 46dd794e
   249   expect 46dd794e
   252                                                                 [exBegin]
   250 								[exBegin]
   253     self information:(CRC32Stream newCrc32c hashValueOf:(0 to:31) asByteArray) hexPrintString
   251     self information:(CRC32Stream newCrc32c hashValueOf:(0 to:31) asByteArray) hexPrintString
   254                                                                 [exEnd]
   252 								[exEnd]
   255 
   253 
   256 "
   254 "
   257 ! !
   255 ! !
   258 
   256 
   259 !CRC32Stream class methodsFor:'instance creation'!
   257 !CRC32Stream class methodsFor:'instance creation'!
   548 !
   546 !
   549 
   547 
   550 version_CVS
   548 version_CVS
   551     ^ '$Header$'
   549     ^ '$Header$'
   552 ! !
   550 ! !
   553