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