author | Claus Gittinger <cg@exept.de> |
Tue, 19 Oct 1999 22:01:44 +0200 | |
changeset 4924 | 9bec9689f646 |
parent 4919 | ba0dbdaceab1 |
child 4925 | e6373ec0ff8a |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
13 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
14 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
15 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
16 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
17 |
category:'Collections-Arrayed' |
1 | 18 |
! |
19 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
20 |
!ByteArray class methodsFor:'documentation'! |
88 | 21 |
|
563 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
25 |
All Rights Reserved |
|
26 |
||
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
88 | 36 |
documentation |
37 |
" |
|
38 |
ByteArrays store integers in the range 0..255. |
|
129 | 39 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 40 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 41 |
be used to hold the data for bitmaps, images and other bulk data. |
42 |
ByteArrays are also used to store the bytecode-instructions of an |
|
43 |
interpreted method and are used as superclass for Strings. |
|
44 |
||
45 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
46 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
47 |
as elements in a constant array. |
|
48 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
49 |
||
50 |
If you have to communicate structure-data (in the C-sense) with external |
|
51 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
52 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
53 |
and store C-structure fields. |
|
1266 | 54 |
|
4062 | 55 |
[memory requirements:] |
4661 | 56 |
OBJ-HEADER + size |
4062 | 57 |
|
2145 | 58 |
[warning:] |
4661 | 59 |
read the warning about 'growing fixed size collection' |
60 |
in ArrayedCollection's documentation |
|
2145 | 61 |
|
1290 | 62 |
[author:] |
4661 | 63 |
Claus Gittinger |
1290 | 64 |
|
1266 | 65 |
[See also:] |
4661 | 66 |
Array CharacterArray String |
88 | 67 |
" |
2 | 68 |
! ! |
69 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
70 |
!ByteArray class methodsFor:'instance creation'! |
1 | 71 |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
72 |
from:aByteArray |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
73 |
"return new instance which is a copy of aByteArray" |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
74 |
|
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
75 |
|len bytes| |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
76 |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
77 |
len := aByteArray size. |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
78 |
bytes := self new:len. |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
79 |
bytes replaceBytesFrom:1 to:len with:aByteArray startingAt:1. |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
80 |
^ bytes |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
81 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
82 |
"Created: / 5.3.1998 / 15:57:52 / stefan" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
83 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
84 |
|
563 | 85 |
fromPackedString:aString |
86 |
"ST-80 compatibility: decode a byteArray from a packed string in which |
|
87 |
6bits are encoded per character. The argument, aString must be a multiple |
|
88 |
of 4 in size (since 24 is the lcm of 6 and 8). This is somewhat like |
|
89 |
the radix-encoding used in good old PDP11 times ;-) |
|
90 |
ST-80 uses this encoding for Images ... |
|
91 |
PS: It took a while to figure that one out ... I dont like it ;-)" |
|
92 |
||
93 |
|index "{ Class: SmallInteger }" |
|
94 |
dstIndex "{ Class: SmallInteger }" |
|
95 |
stop "{ Class: SmallInteger }" |
|
2447 | 96 |
sixBits "{ Class: SmallInteger }" |
97 |
n "{ Class: SmallInteger }" |
|
563 | 98 |
sz "{ Class: SmallInteger }" |
99 |
lastCharacter bytes| |
|
100 |
||
101 |
sz := aString size. |
|
102 |
sz == 0 ifTrue:[^ self new]. |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
103 |
sz := sz - (aString occurrencesOf:Character cr). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
104 |
sz := sz - (aString occurrencesOf:Character tab). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
105 |
|
563 | 106 |
stop := sz // 4 * 3. |
3135 | 107 |
"the size modulo 3 is encoded in the last character, if its in the |
563 | 108 |
range 97 .. otherwise, its exact." |
109 |
||
110 |
lastCharacter := aString last. |
|
111 |
lastCharacter asciiValue > 96 ifTrue:[ |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
112 |
stop := stop - 3 + lastCharacter asciiValue - 96 |
563 | 113 |
]. |
114 |
bytes := self new:stop. |
|
2447 | 115 |
|
563 | 116 |
index := 1. dstIndex := 1. |
117 |
[dstIndex <= stop] whileTrue:[ |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
118 |
"/ take 4 characters ... |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
119 |
"/ cg: allow lineBreak befor eeach group of 4 |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
120 |
sixBits := (aString at:index) asciiValue. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
121 |
[sixBits < 32] whileTrue:[ |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
122 |
index := index + 1. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
123 |
sixBits := (aString at:index) asciiValue. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
124 |
]. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
125 |
sixBits := sixBits bitAnd:16r3F. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
126 |
n := sixBits. |
2447 | 127 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
128 |
sixBits := (aString at:index+1) asciiValue. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
129 |
sixBits := sixBits bitAnd:16r3F. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
130 |
n := (n bitShift:6) + sixBits. |
2447 | 131 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
132 |
sixBits := (aString at:index+2) asciiValue. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
133 |
sixBits := sixBits bitAnd:16r3F. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
134 |
n := (n bitShift:6) + sixBits. |
2447 | 135 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
136 |
sixBits := (aString at:index+3) asciiValue. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
137 |
sixBits := sixBits bitAnd:16r3F. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
138 |
n := (n bitShift:6) + sixBits. |
2447 | 139 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
140 |
index := index + 4. |
2447 | 141 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
142 |
"/ now have 24 bits in n |
2447 | 143 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
144 |
bytes at:dstIndex put:(n bitShift:-16). |
2447 | 145 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
146 |
dstIndex < stop ifTrue:[ |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
147 |
bytes at:dstIndex+1 put:((n bitShift:-8) bitAnd:16rFF). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
148 |
dstIndex+2 <= stop ifTrue:[ |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
149 |
bytes at:dstIndex+2 put:(n bitAnd:16rFF). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
150 |
] |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
151 |
]. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
152 |
dstIndex := dstIndex + 3. |
563 | 153 |
]. |
154 |
^ bytes |
|
155 |
||
156 |
" |
|
157 |
ByteArray fromPackedString:(#[1 1 1 1] asPackedString) |
|
158 |
ByteArray fromPackedString:(#[1 1 1 1 1] asPackedString) |
|
159 |
ByteArray fromPackedString:(#[1 1 1 1 1 1] asPackedString) |
|
160 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1] asPackedString) |
|
161 |
ByteArray fromPackedString:(#[1 1 1 1 1 1 1 1] asPackedString) |
|
162 |
||
163 |
" |
|
2447 | 164 |
|
3135 | 165 |
"Modified: / 6.3.1997 / 15:28:52 / cg" |
166 |
"Modified: / 18.12.1997 / 17:17:11 / stefan" |
|
563 | 167 |
! |
168 |
||
1 | 169 |
uninitializedNew:anInteger |
170 |
"return a new instance of the receiver with uninitialized |
|
171 |
(i.e. undefined) contents. The indexed elements have any random |
|
75 | 172 |
value. However, any named instance variables are still nilled. |
173 |
For use, when contents will be set anyway shortly after - this |
|
174 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
175 |
Of course, it only makes a difference for very big ByteArrays, such |
|
176 |
as used for images/bitmaps. |
|
177 |
||
178 |
Notice: if you want to port code using uninitializedNew: to another |
|
179 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
|
180 |
method to the ByteArray class of the other smalltalk." |
|
1 | 181 |
|
182 |
%{ /* NOCONTEXT */ |
|
183 |
OBJ newobj; |
|
184 |
INT instsize, nInstVars, nindexedinstvars; |
|
185 |
REGISTER OBJ *op; |
|
186 |
||
249 | 187 |
if (__isSmallInteger(anInteger)) { |
1133 | 188 |
nindexedinstvars = __intVal(anInteger); |
155 | 189 |
if (nindexedinstvars >= 0) { |
249 | 190 |
if (self == ByteArray) { |
191 |
/* |
|
192 |
* the most common case |
|
193 |
*/ |
|
194 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
2915 | 195 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
835 | 196 |
__qCheckedNew(newobj, instsize); |
1133 | 197 |
__InstPtr(newobj)->o_class = self; |
249 | 198 |
RETURN (newobj ); |
155 | 199 |
} |
249 | 200 |
} |
1133 | 201 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
249 | 202 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
834 | 203 |
__PROTECT_CONTEXT__ |
2915 | 204 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
834 | 205 |
__UNPROTECT_CONTEXT__ |
281 | 206 |
if (newobj != nil) { |
1133 | 207 |
__InstPtr(newobj)->o_class = self; |
367 | 208 |
__qSTORE(newobj, self); |
249 | 209 |
if (nInstVars) { |
210 |
/* |
|
211 |
* still have to nil out named instvars ... |
|
212 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
213 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
1133 | 214 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 215 |
#else |
249 | 216 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
217 |
/* |
|
218 |
* knowing that nil is 0 |
|
219 |
*/ |
|
1133 | 220 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
249 | 221 |
# else |
1133 | 222 |
op = __InstPtr(newobj)->i_instvars; |
249 | 223 |
while (nInstVars--) |
224 |
*op++ = nil; |
|
225 |
# endif |
|
1 | 226 |
#endif |
155 | 227 |
} |
249 | 228 |
RETURN ( newobj ); |
155 | 229 |
} |
230 |
} |
|
1 | 231 |
} |
232 |
%} |
|
233 |
. |
|
12 | 234 |
(anInteger isMemberOf:SmallInteger) ifTrue:[ |
155 | 235 |
(anInteger < 0) ifTrue:[ |
236 |
^ self error:'bad (negative) argument to new' |
|
237 |
]. |
|
238 |
" |
|
239 |
memory allocation failed. |
|
240 |
When we arrive here, there was no memory, even after |
|
241 |
a garbage collect. This means, that the VM wanted to |
|
242 |
get some more memory from the Operatingsystem, which |
|
243 |
was not kind enough to give some. |
|
244 |
" |
|
245 |
^ ObjectMemory allocationFailureSignal raise. |
|
12 | 246 |
]. |
1 | 247 |
^ self basicNew:anInteger |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
248 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
249 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
250 |
with:aByteArray from:start to:stop |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
251 |
"return new instance with a copy of aByteArray |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
252 |
beginning at index start up to and including index stop" |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
253 |
|
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
254 |
|len bytes| |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
255 |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
256 |
len := stop-start+1. |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
257 |
bytes := self new:len. |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
258 |
bytes replaceBytesFrom:1 to:len with:aByteArray startingAt:start. |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
259 |
^ bytes |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
260 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
261 |
"Modified: / 5.3.1998 / 16:00:18 / stefan" |
1 | 262 |
! ! |
263 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
264 |
!ByteArray class methodsFor:'binary storage'! |
1 | 265 |
|
550 | 266 |
binaryDefinitionFrom: stream manager: manager |
267 |
"get a ByteArray from the binary stream. |
|
3071 | 268 |
ByteArrays are stored as 4-byte size, followed by the bytes. |
269 |
This is only invoked for long bytearrays. |
|
270 |
Short ones are stored with 1byte length." |
|
271 |
||
272 |
|b len| |
|
1 | 273 |
|
550 | 274 |
"take care of subclasses ..." |
275 |
self == ByteArray ifTrue:[ |
|
4661 | 276 |
len := stream nextNumber:4. |
277 |
b := ByteArray uninitializedNew:len. |
|
278 |
stream nextBytes:len into:b startingAt:1. |
|
279 |
^ b |
|
550 | 280 |
]. |
3071 | 281 |
^ super binaryDefinitionFrom:stream manager:manager |
282 |
||
283 |
"Modified: / 2.11.1997 / 16:19:49 / cg" |
|
550 | 284 |
! ! |
285 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
286 |
!ByteArray class methodsFor:'queries'! |
550 | 287 |
|
288 |
isBuiltInClass |
|
1266 | 289 |
"return true if this class is known by the run-time-system. |
290 |
Here, true is returned for myself, false for subclasses." |
|
550 | 291 |
|
292 |
^ self == ByteArray |
|
1266 | 293 |
|
294 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
550 | 295 |
! ! |
296 |
||
297 |
!ByteArray methodsFor:'accessing'! |
|
1 | 298 |
|
563 | 299 |
basicAt:index |
300 |
"return the indexed instance variable with index, anInteger |
|
1019 | 301 |
- redefined here to be slighly faster than the default in Object. |
302 |
Q: is it worth the extra code ?" |
|
563 | 303 |
|
304 |
%{ /* NOCONTEXT */ |
|
305 |
||
306 |
REGISTER int indx; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
307 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
308 |
REGISTER OBJ cls; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
309 |
REGISTER int nIndex; |
563 | 310 |
|
311 |
if (__isSmallInteger(index)) { |
|
4661 | 312 |
indx = __intVal(index) - 1; |
313 |
slf = self; |
|
314 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
315 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
316 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
317 |
goto fail; |
|
318 |
} |
|
319 |
if (indx < 0) goto fail; |
|
320 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
321 |
} |
|
322 |
nIndex = __byteArraySize(slf); |
|
323 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
324 |
RETURN ( __MKSMALLINT(__ByteArrayInstPtr(slf)->ba_element[indx]) ); |
|
325 |
} |
|
563 | 326 |
} |
1019 | 327 |
fail: ; |
328 |
%}. |
|
563 | 329 |
^ super basicAt:index |
330 |
! |
|
331 |
||
1 | 332 |
basicAt:index put:value |
1230 | 333 |
"set the indexed instance variable with index, anInteger to value. |
334 |
Returns value (sigh). |
|
1019 | 335 |
- redefined here to be slighly faster than the default in Object. |
336 |
Q: is it worth the extra code ?" |
|
1 | 337 |
|
338 |
%{ /* NOCONTEXT */ |
|
339 |
||
340 |
REGISTER int indx; |
|
341 |
int nIndex; |
|
342 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
343 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
344 |
REGISTER OBJ cls; |
1 | 345 |
|
249 | 346 |
if (__bothSmallInteger(index, value)) { |
4661 | 347 |
val = __intVal(value); |
348 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
349 |
indx = __intVal(index) - 1; |
|
350 |
slf = self; |
|
351 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
352 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
353 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
354 |
goto fail; |
|
355 |
} |
|
356 |
if (indx < 0) goto fail; |
|
357 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
358 |
} |
|
359 |
nIndex = __byteArraySize(slf); |
|
360 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
361 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
362 |
RETURN ( value ); |
|
363 |
} |
|
364 |
} |
|
1 | 365 |
} |
1019 | 366 |
fail: ; |
367 |
%}. |
|
1 | 368 |
^ super basicAt:index put:value |
1230 | 369 |
|
370 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
2 | 371 |
! |
372 |
||
4128 | 373 |
bitAt:index |
374 |
"return the bit at index" |
|
375 |
||
376 |
%{ /* NOCONTEXT */ |
|
377 |
||
378 |
REGISTER int indx; |
|
379 |
REGISTER int byte; |
|
380 |
int nIndex; |
|
381 |
REGISTER OBJ slf; |
|
382 |
REGISTER OBJ cls; |
|
383 |
||
384 |
if (__isSmallInteger(index)) { |
|
4661 | 385 |
indx = __intVal(index) - 1; |
386 |
slf = self; |
|
4128 | 387 |
|
4661 | 388 |
byte = indx / 8; |
389 |
indx = indx % 8; |
|
4128 | 390 |
|
4661 | 391 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
392 |
if (indx < 0) goto badIndex; |
|
393 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
394 |
} |
|
395 |
nIndex = __byteArraySize(slf); |
|
396 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
397 |
RETURN ( __MKSMALLINT((__ByteArrayInstPtr(slf)->ba_element[byte] & (1 << indx)) != 0) ); |
|
398 |
} |
|
4128 | 399 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
400 |
badIndex: ; |
4128 | 401 |
%}. |
402 |
^ self primitiveFailed |
|
403 |
||
404 |
" |
|
405 |
#[ 1 1 1 1 ] bitAt:9 |
|
406 |
#[ 1 1 1 1 ] bitAt:11 |
|
407 |
#[ 2 2 2 2 ] bitAt:10 |
|
408 |
" |
|
409 |
||
410 |
||
411 |
||
412 |
! |
|
413 |
||
414 |
bitSetAt:index |
|
4269 | 415 |
"set the bit at index (index starts with 1)" |
4128 | 416 |
|
417 |
%{ /* NOCONTEXT */ |
|
418 |
||
419 |
REGISTER int indx; |
|
420 |
REGISTER int byte; |
|
421 |
int nIndex; |
|
422 |
REGISTER OBJ slf; |
|
423 |
REGISTER OBJ cls; |
|
424 |
||
425 |
if (__isSmallInteger(index)) { |
|
4661 | 426 |
indx = __intVal(index) - 1; |
427 |
slf = self; |
|
4128 | 428 |
|
4661 | 429 |
byte = indx / 8; |
430 |
indx = indx % 8; |
|
4128 | 431 |
|
4661 | 432 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
433 |
if (indx < 0) goto badIndex; |
|
434 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
435 |
} |
|
436 |
nIndex = __byteArraySize(slf); |
|
437 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
438 |
__ByteArrayInstPtr(slf)->ba_element[byte] |= (1 << indx); |
|
439 |
RETURN (slf); |
|
440 |
} |
|
4128 | 441 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
442 |
badIndex: ; |
4128 | 443 |
%}. |
444 |
^ self primitiveFailed |
|
445 |
||
446 |
" |
|
4269 | 447 |
#[ 0 0 0 0 ] bitSetAt:1 |
448 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
449 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
450 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
4128 | 451 |
#[ 0 0 0 0 ] bitSetAt:10 |
452 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
453 |
" |
|
454 |
||
455 |
||
456 |
||
457 |
! |
|
458 |
||
54 | 459 |
byteAt:index |
249 | 460 |
"return the byte at index. |
461 |
For ByteArray, this is the same as basicAt:; |
|
462 |
however, for strings or symbols, this returns a numeric byteValue |
|
463 |
instead of a character." |
|
464 |
||
465 |
%{ /* NOCONTEXT */ |
|
466 |
||
467 |
REGISTER int indx; |
|
468 |
int nIndex; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
469 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
470 |
REGISTER OBJ cls; |
54 | 471 |
|
249 | 472 |
if (__isSmallInteger(index)) { |
4661 | 473 |
indx = __intVal(index) - 1; |
474 |
slf = self; |
|
475 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
476 |
if (indx < 0) goto badIndex; |
|
477 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
478 |
} |
|
479 |
nIndex = __byteArraySize(slf); |
|
480 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
481 |
RETURN ( __MKSMALLINT(__ByteArrayInstPtr(slf)->ba_element[indx]) ); |
|
482 |
} |
|
249 | 483 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
484 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
485 |
%}. |
249 | 486 |
^ (super basicAt:index) asInteger |
54 | 487 |
! |
488 |
||
249 | 489 |
byteAt:index put:value |
490 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
491 |
However, for Strings, this expects a byteValue to be stored." |
|
492 |
||
493 |
%{ /* NOCONTEXT */ |
|
494 |
||
495 |
REGISTER int indx; |
|
496 |
int nIndex; |
|
497 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
498 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
499 |
REGISTER OBJ cls; |
54 | 500 |
|
249 | 501 |
if (__bothSmallInteger(index, value)) { |
4661 | 502 |
val = __intVal(value); |
503 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
504 |
indx = __intVal(index) - 1; |
|
505 |
slf = self; |
|
506 |
if ((cls = __qClass(slf)) != ByteArray) { |
|
507 |
if (indx < 0) goto badIndex; |
|
508 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
509 |
} |
|
510 |
nIndex = __byteArraySize(slf); |
|
511 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
512 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
513 |
RETURN ( value ); |
|
514 |
} |
|
515 |
} |
|
249 | 516 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
517 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
518 |
%}. |
249 | 519 |
^ super basicAt:index put:value |
54 | 520 |
! |
521 |
||
2 | 522 |
doubleWordAt:index put:value |
523 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
359 | 524 |
The value should be in the range 0 to 16rFFFFFFFF |
525 |
(for negative values, the stored value is not defined). |
|
526 |
The value is stored in the machines natural byte order. |
|
527 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
|
528 |
||
529 |
|t| |
|
530 |
||
531 |
%{ /* NOCONTEXT */ |
|
532 |
||
533 |
REGISTER int indx; |
|
534 |
int nIndex; |
|
535 |
union { |
|
2950 | 536 |
unsigned char u_char[4]; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
537 |
unsigned int u_uint; |
359 | 538 |
} val; |
539 |
OBJ cls; |
|
2950 | 540 |
unsigned char *byteP; |
359 | 541 |
|
542 |
if (__isSmallInteger(index)) { |
|
543 |
if (__isSmallInteger(value)) { |
|
1133 | 544 |
val.u_uint = __intVal(value); |
359 | 545 |
} else { |
546 |
val.u_uint = __longIntVal(value); |
|
547 |
if (val.u_uint == 0) goto error; |
|
548 |
} |
|
549 |
||
1133 | 550 |
indx = __intVal(index); |
359 | 551 |
if (indx > 0) { |
1133 | 552 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
553 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
359 | 554 |
nIndex = __qSize(self) - OHDR_SIZE; |
555 |
if ((indx+3) <= nIndex) { |
|
2950 | 556 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
557 |
#if defined(i386) || defined(UNALIGNED_FETCH_OK) |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
558 |
((unsigned int *)byteP)[0] = val.u_uint; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
559 |
#else |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
560 |
if (((unsigned INT)byteP & 3) == 0) { |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
561 |
((unsigned int *)byteP)[0] = val.u_uint; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
562 |
} else { |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
563 |
byteP[0] = val.u_char[0]; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
564 |
byteP[1] = val.u_char[1]; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
565 |
byteP[2] = val.u_char[2]; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
566 |
byteP[3] = val.u_char[3]; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
567 |
} |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
568 |
#endif |
359 | 569 |
RETURN ( value ); |
570 |
} |
|
571 |
} |
|
572 |
} |
|
573 |
error: ; |
|
574 |
%}. |
|
575 |
^ SubscriptOutOfBoundsSignal raise. |
|
576 |
||
577 |
" |
|
578 |
|b| |
|
579 |
b := ByteArray new:4. |
|
580 |
b doubleWordAt:1 put:16r04030201. |
|
581 |
b inspect |
|
582 |
" |
|
583 |
! |
|
584 |
||
585 |
doubleWordAt:index put:value MSB:msb |
|
586 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
129 | 587 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 588 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 589 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 590 |
|
591 |
|t| |
|
592 |
||
593 |
%{ /* NOCONTEXT */ |
|
594 |
||
595 |
REGISTER int indx; |
|
596 |
int nIndex; |
|
597 |
int val; |
|
249 | 598 |
OBJ cls; |
2950 | 599 |
unsigned char *byteP; |
2 | 600 |
|
359 | 601 |
if (__isSmallInteger(index)) { |
602 |
if (__isSmallInteger(value)) { |
|
1133 | 603 |
val = __intVal(value); |
359 | 604 |
} else { |
605 |
val = __longIntVal(value); |
|
606 |
if (val == 0) goto error; |
|
607 |
} |
|
1133 | 608 |
indx = __intVal(index); |
359 | 609 |
if (indx > 0) { |
1133 | 610 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
611 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
359 | 612 |
nIndex = __qSize(self) - OHDR_SIZE; |
613 |
if ((indx+3) <= nIndex) { |
|
2950 | 614 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
359 | 615 |
if (msb == true) { |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
616 |
/* |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
617 |
* most significant byte first (i.e sparc order) |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
618 |
*/ |
4275 | 619 |
#if defined(__MSBFIRST) ||defined(sparc) |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
620 |
if (((INT)byteP & 3) == 0) { |
4661 | 621 |
((int *)byteP)[0] = val; |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
622 |
} else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
623 |
#endif |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
624 |
{ |
4661 | 625 |
byteP[3] = val & 0xFF; |
626 |
val >>= 8; |
|
627 |
byteP[2] = val & 0xFF; |
|
628 |
val >>= 8; |
|
629 |
byteP[1] = val & 0xFF; |
|
630 |
val >>= 8; |
|
631 |
byteP[0] = val & 0xFF; |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
632 |
} |
359 | 633 |
} else { |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
634 |
/* |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
635 |
* least significant byte first (i.e i386/alpha order) |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
636 |
*/ |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
637 |
#if defined(i386) || (defined(__LSBFIRST) && defined(UNALIGNED_FETCH_OK)) |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
638 |
((int *)byteP)[0] = val; |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
639 |
#else |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
640 |
# if defined(__LSBFIRST) |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
641 |
if (((unsigned INT)byteP & 3) == 0) { |
4661 | 642 |
((int *)byteP)[0] = val; |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
643 |
} else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
644 |
# endif |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
645 |
{ |
4661 | 646 |
byteP[0] = val & 0xFF; |
647 |
val >>= 8; |
|
648 |
byteP[1] = val & 0xFF; |
|
649 |
val >>= 8; |
|
650 |
byteP[2] = val & 0xFF; |
|
651 |
val >>= 8; |
|
652 |
byteP[3] = val & 0xFF; |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
653 |
} |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
654 |
#endif |
249 | 655 |
} |
359 | 656 |
RETURN ( value ); |
155 | 657 |
} |
658 |
} |
|
2 | 659 |
} |
359 | 660 |
error: ; |
661 |
%}. |
|
662 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 663 |
|
664 |
" |
|
665 |
|b| |
|
359 | 666 |
b := ByteArray new:8. |
667 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
668 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
129 | 669 |
b inspect |
670 |
" |
|
54 | 671 |
! |
672 |
||
563 | 673 |
wordAt:index |
674 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
675 |
The value is retrieved in the machines natural byte order |
|
676 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
677 |
||
678 |
%{ /* NOCONTEXT */ |
|
54 | 679 |
|
563 | 680 |
REGISTER int indx; |
681 |
int nIndex; |
|
682 |
union { |
|
2950 | 683 |
unsigned char u_char[2]; |
563 | 684 |
unsigned short u_ushort; |
685 |
} val; |
|
686 |
OBJ cls; |
|
2950 | 687 |
unsigned char *byteP; |
54 | 688 |
|
563 | 689 |
if (__isSmallInteger(index)) { |
1133 | 690 |
indx = __intVal(index); |
563 | 691 |
if (indx > 0) { |
1133 | 692 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
693 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 694 |
nIndex = __byteArraySize(self); |
695 |
if ((indx+1) <= nIndex) { |
|
2950 | 696 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
697 |
#if defined(i386) || defined(UNALIGNED_FETCH_OK) |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
698 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
699 |
#else |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
700 |
/* |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
701 |
* mhmh to be measured: |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
702 |
* the if may hurt more than the additional |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
703 |
* memory cycles on some machines ... |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
704 |
*/ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
705 |
if (((INT)byteP & 1) == 0) { |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
706 |
/* aligned */ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
707 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
708 |
} else { |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
709 |
val.u_char[0] = byteP[0]; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
710 |
val.u_char[1] = byteP[1]; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
711 |
} |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
712 |
#endif |
1133 | 713 |
RETURN ( __MKSMALLINT(val.u_ushort) ); |
563 | 714 |
} |
715 |
} |
|
716 |
} |
|
717 |
%}. |
|
718 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 719 |
! |
720 |
||
563 | 721 |
wordAt:index MSB:msb |
722 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
723 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
724 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
725 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
726 |
||
727 |
%{ /* NOCONTEXT */ |
|
728 |
||
729 |
REGISTER int indx; |
|
730 |
int nIndex; |
|
731 |
int val; |
|
732 |
OBJ cls; |
|
2950 | 733 |
unsigned char *byteP; |
54 | 734 |
|
563 | 735 |
if (__isSmallInteger(index)) { |
1133 | 736 |
indx = __intVal(index); |
563 | 737 |
if (indx > 0) { |
1133 | 738 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
739 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 740 |
nIndex = __byteArraySize(self); |
741 |
if ((indx+1) <= nIndex) { |
|
2950 | 742 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
563 | 743 |
if (msb == true) { |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
744 |
/* |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
745 |
* most significant byte first (i.e sparc order) |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
746 |
*/ |
4275 | 747 |
#if defined(__MSBFIRST) ||defined(sparc) |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
748 |
/* |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
749 |
* mhmh to be measured: |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
750 |
* the if may hurt more than the additional |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
751 |
* memory cycles on some machines ... |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
752 |
*/ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
753 |
if (((INT)byteP & 1) == 0) { |
4661 | 754 |
/* aligned */ |
755 |
val = ((unsigned short *)byteP)[0]; |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
756 |
} else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
757 |
#endif |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
758 |
{ |
4661 | 759 |
val = byteP[0]; |
760 |
val = (val << 8) + byteP[1]; |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
761 |
} |
563 | 762 |
} else { |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
763 |
/* |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
764 |
* least significant byte first (i.e i386/alpha order) |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
765 |
*/ |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
766 |
#if defined(i386) || (defined(__LSBFIRST) && defined(UNALIGNED_FETCH_OK)) |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
767 |
val = ((unsigned short *)byteP)[0]; |
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
768 |
#else |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
769 |
# if defined(__LSBFIRST) |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
770 |
/* |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
771 |
* mhmh to be measured: |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
772 |
* the if may hurt more than the additional |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
773 |
* memory cycles on some machines ... |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
774 |
*/ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
775 |
if (((INT)byteP & 1) == 0) { |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
776 |
/* aligned */ |
4661 | 777 |
val = ((unsigned short *)byteP)[0]; |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
778 |
} else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
779 |
# endif |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
780 |
{ |
4661 | 781 |
val = byteP[1]; |
782 |
val = (val << 8) + byteP[0]; |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
783 |
} |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
784 |
#endif |
563 | 785 |
} |
1133 | 786 |
RETURN ( __MKSMALLINT(val) ); |
563 | 787 |
} |
788 |
} |
|
789 |
} |
|
790 |
%}. |
|
791 |
^ SubscriptOutOfBoundsSignal raise. |
|
54 | 792 |
! |
793 |
||
563 | 794 |
wordAt:index put:value |
795 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
796 |
The stored value must be in the range 0 .. 16rFFFF. |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
797 |
The value is stored in the machines natural byteorder, |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
798 |
i.e. this method should only be used to fill byteArrays which are |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
799 |
used internally (not passed to other machines). |
563 | 800 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
801 |
||
802 |
%{ /* NOCONTEXT */ |
|
54 | 803 |
|
563 | 804 |
REGISTER int indx; |
805 |
int nIndex; |
|
806 |
int v; |
|
807 |
union { |
|
2950 | 808 |
unsigned char u_char[2]; |
563 | 809 |
unsigned short u_ushort; |
810 |
} val; |
|
811 |
OBJ cls; |
|
2950 | 812 |
unsigned char *byteP; |
129 | 813 |
|
563 | 814 |
if (__bothSmallInteger(index, value)) { |
1133 | 815 |
indx = __intVal(index); |
563 | 816 |
if (indx > 0) { |
1133 | 817 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
818 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 819 |
nIndex = __byteArraySize(self); |
820 |
if ((indx+1) <= nIndex) { |
|
1133 | 821 |
val.u_ushort = v = __intVal(value); |
563 | 822 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
2950 | 823 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
824 |
#if defined(i386) || defined(UNALIGNED_FETCH_OK) |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
825 |
((unsigned short *)byteP)[0] = val.u_ushort; |
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
826 |
#else |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
827 |
/* |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
828 |
* mhmh to be measured: |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
829 |
* the if may hurt more than the additional |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
830 |
* memory cycles on some machines ... |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
831 |
*/ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
832 |
if (((INT)byteP & 1) == 0) { |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
833 |
/* aligned */ |
4661 | 834 |
((unsigned short *)byteP)[0] = val.u_ushort; |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
835 |
} else { |
4661 | 836 |
byteP[0] = val.u_char[0]; |
837 |
byteP[1] = val.u_char[1]; |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
838 |
} |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
839 |
#endif |
563 | 840 |
RETURN ( value ); |
841 |
} |
|
842 |
} |
|
843 |
} |
|
844 |
} |
|
845 |
%}. |
|
846 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
847 |
^ self elementBoundsError |
|
848 |
]. |
|
849 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 850 |
|
851 |
" |
|
563 | 852 |
|b| |
853 |
b := ByteArray new:4. |
|
854 |
b wordAt:1 put:16r0102. |
|
855 |
b wordAt:3 put:16r0304. |
|
856 |
b inspect |
|
129 | 857 |
" |
858 |
! |
|
859 |
||
563 | 860 |
wordAt:index put:value MSB:msb |
861 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
862 |
The stored value must be in the range 0 .. 16rFFFF. |
|
863 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
|
864 |
lower index) if msb is false, MSB-first otherwise. |
|
865 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
866 |
||
867 |
%{ /* NOCONTEXT */ |
|
868 |
||
869 |
REGISTER int indx; |
|
870 |
int nIndex; |
|
871 |
int val; |
|
872 |
OBJ cls; |
|
2950 | 873 |
unsigned char *byteP; |
563 | 874 |
|
875 |
if (__bothSmallInteger(index, value)) { |
|
1133 | 876 |
indx = __intVal(index); |
563 | 877 |
if (indx > 0) { |
1133 | 878 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
879 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
563 | 880 |
nIndex = __byteArraySize(self); |
881 |
if ((indx+1) <= nIndex) { |
|
1133 | 882 |
val = __intVal(value); |
563 | 883 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
2950 | 884 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
563 | 885 |
if (msb == true) { |
4661 | 886 |
/* |
887 |
* most significant byte first (i.e sparc order) |
|
888 |
*/ |
|
4275 | 889 |
#if defined(__MSBFIRST) ||defined(sparc) |
4661 | 890 |
/* |
891 |
* mhmh to be measured: |
|
892 |
* the if may hurt more than the additional |
|
893 |
* memory cycles on some machines ... |
|
894 |
*/ |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
895 |
if (((INT)byteP & 1) == 0) { |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
896 |
/* aligned */ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
897 |
((unsigned short *)byteP)[0] = val; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
898 |
} else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
899 |
#endif |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
900 |
{ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
901 |
byteP[1] = val & 0xFF; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
902 |
byteP[0] = (val>>8) & 0xFF; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
903 |
} |
563 | 904 |
} else { |
4661 | 905 |
/* |
906 |
* least significant byte first (i.e i386/alpha order) |
|
907 |
*/ |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
908 |
#if defined(i386) || (defined(__LSBFIRST) && defined(UNALIGNED_FETCH_OK)) |
4661 | 909 |
((unsigned short *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
910 |
#else |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
911 |
# if defined(__LSBFIRST) |
4661 | 912 |
/* |
913 |
* mhmh to be measured: |
|
914 |
* the if may hurt more than the additional |
|
915 |
* memory cycles on some machines ... |
|
916 |
*/ |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
917 |
if (((INT)byteP & 1) == 0) { |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
918 |
/* aligned */ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
919 |
((unsigned short *)byteP)[0] = val; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
920 |
} else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
921 |
# endif |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
922 |
{ |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
923 |
byteP[0] = val & 0xFF; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
924 |
byteP[1] = (val>>8) & 0xFF; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
925 |
} |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
926 |
#endif |
563 | 927 |
} |
928 |
RETURN ( value ); |
|
929 |
} |
|
930 |
} |
|
931 |
} |
|
932 |
} |
|
933 |
%}. |
|
934 |
((value < 0) or:[value > 16rFFFF]) ifTrue:[ |
|
935 |
^ self elementBoundsError |
|
936 |
]. |
|
937 |
^ SubscriptOutOfBoundsSignal raise. |
|
129 | 938 |
|
939 |
" |
|
563 | 940 |
|b| |
941 |
b := ByteArray new:8. |
|
942 |
b wordAt:1 put:16r0102 MSB:false. |
|
943 |
b wordAt:3 put:16r0304 MSB:false. |
|
944 |
b wordAt:5 put:16r0102 MSB:true. |
|
945 |
b wordAt:7 put:16r0304 MSB:true. |
|
946 |
b inspect |
|
129 | 947 |
" |
1 | 948 |
! ! |
949 |
||
550 | 950 |
!ByteArray methodsFor:'binary storage'! |
951 |
||
952 |
storeBinaryDefinitionOn:stream manager:manager |
|
953 |
"append a binary representation of the receiver onto stream. |
|
3071 | 954 |
Redefined since short ByteArrays can be stored with a special type code |
1261 | 955 |
in a more compact way. |
3071 | 956 |
This is an internal interface for the binary storage mechanism." |
550 | 957 |
|
2464 | 958 |
|myClass myBasicSize| |
959 |
||
550 | 960 |
"not, if I have named instance variables" |
2464 | 961 |
(myClass := self class) instSize ~~ 0 ifTrue:[ |
4661 | 962 |
^ super storeBinaryDefinitionOn:stream manager:manager |
550 | 963 |
]. |
3071 | 964 |
|
965 |
myBasicSize := self basicSize. |
|
966 |
||
967 |
"/ can use a more compact representation; |
|
968 |
"/ but not for subclasses ... |
|
969 |
||
970 |
((myClass == ByteArray) |
|
971 |
and:[myBasicSize <= 255]) ifTrue:[ |
|
4661 | 972 |
"/ special encoding: <codeForByteArray> <len> <bytes> ... |
973 |
stream nextPut:(manager codeForByteArray); nextPut:myBasicSize. |
|
3071 | 974 |
] ifFalse:[ |
4661 | 975 |
manager putIdOfClass:myClass on:stream. |
976 |
stream nextNumber:4 put:myBasicSize. |
|
3071 | 977 |
]. |
2464 | 978 |
stream nextPutBytes:myBasicSize from:self startingAt:1. |
1261 | 979 |
|
3071 | 980 |
"Modified: / 2.11.1997 / 15:28:45 / cg" |
550 | 981 |
! ! |
982 |
||
3345 | 983 |
!ByteArray methodsFor:'comparing'! |
984 |
||
985 |
= aByteArray |
|
986 |
"Compare the receiver with the argument and return true if the |
|
987 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
988 |
Otherwise return false." |
|
989 |
||
990 |
%{ /* NOCONTEXT */ |
|
991 |
||
992 |
int l1, l2; |
|
993 |
REGISTER OBJ s = aByteArray; |
|
994 |
unsigned char *cp1, *cp2; |
|
995 |
OBJ cls; |
|
996 |
OBJ myCls; |
|
997 |
INT addrDelta; |
|
998 |
||
999 |
if (s == self) { |
|
4661 | 1000 |
RETURN ( true ); |
3345 | 1001 |
} |
1002 |
if (! __isNonNilObject(s)) { |
|
4661 | 1003 |
RETURN ( false ); |
3345 | 1004 |
} |
1005 |
||
1006 |
cls = __qClass(s); |
|
1007 |
myCls = __qClass(self); |
|
1008 |
||
1009 |
if (cls == myCls) { |
|
4661 | 1010 |
cp2 = __stringVal(s); |
1011 |
l2 = __byteArraySize(s); |
|
3345 | 1012 |
|
4661 | 1013 |
cp1 = __stringVal(self); |
1014 |
l1 = __byteArraySize(self); |
|
3345 | 1015 |
|
4661 | 1016 |
if (l1 != l2) { |
1017 |
RETURN ( false ); |
|
1018 |
} |
|
3345 | 1019 |
|
4661 | 1020 |
/* |
1021 |
* care for instances of subclasses ... |
|
1022 |
*/ |
|
1023 |
if (cls != ByteArray) { |
|
1024 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
3345 | 1025 |
|
4661 | 1026 |
cp2 += n; |
1027 |
cp1 += n; |
|
1028 |
l1 -= n; |
|
1029 |
} |
|
3345 | 1030 |
|
4661 | 1031 |
addrDelta = cp2 - cp1; |
1032 |
while (l1 >= sizeof(unsigned INT)) { |
|
1033 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1034 |
RETURN (false); |
|
1035 |
} |
|
1036 |
l1 -= sizeof(unsigned INT); |
|
1037 |
cp1 += sizeof(unsigned INT); |
|
1038 |
} |
|
1039 |
if (l1 >= sizeof(unsigned short)) { |
|
1040 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1041 |
RETURN (false); |
|
1042 |
} |
|
1043 |
l1 -= sizeof(unsigned short); |
|
1044 |
cp1 += sizeof(unsigned short); |
|
1045 |
} |
|
1046 |
while (l1) { |
|
1047 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1048 |
RETURN (false); |
|
1049 |
} |
|
1050 |
l1--; |
|
1051 |
cp1++; |
|
1052 |
} |
|
3345 | 1053 |
|
4661 | 1054 |
RETURN (true); |
3345 | 1055 |
} |
1056 |
%}. |
|
1057 |
^ super = aByteArray |
|
1058 |
! ! |
|
1059 |
||
12 | 1060 |
!ByteArray methodsFor:'converting'! |
1061 |
||
1062 |
asByteArray |
|
1063 |
"return the receiver as a byteArray" |
|
1064 |
||
1065 |
"could be an instance of a subclass..." |
|
1066 |
self class == ByteArray ifTrue:[ |
|
155 | 1067 |
^ self |
12 | 1068 |
]. |
1069 |
^ super asByteArray |
|
129 | 1070 |
|
1071 |
" |
|
1072 |
'hello world' asByteArray |
|
1073 |
#(1 2 3 4 5 6 7) asByteArray |
|
1074 |
#(1 2 256 4 5 6 7) asByteArray |
|
1075 |
" |
|
4778 | 1076 |
! |
1077 |
||
4807 | 1078 |
decodeAsLiteralArray |
1079 |
"given a literalEncoding in the receiver, |
|
1080 |
create & return the corresponding object. |
|
1081 |
The inverse operation to #literalArrayEncoding." |
|
1082 |
||
1083 |
^ self |
|
1084 |
||
1085 |
! |
|
1086 |
||
4778 | 1087 |
literalArrayEncoding |
1088 |
"encode myself as an array literal, from which a copy of the receiver |
|
1089 |
can be reconstructed with #decodeAsLiteralArray." |
|
1090 |
||
1091 |
^ self |
|
1092 |
||
1093 |
" |
|
1094 |
#[1 2 3] literalArrayEncoding |
|
1095 |
" |
|
1096 |
||
1097 |
||
12 | 1098 |
! ! |
1099 |
||
92 | 1100 |
!ByteArray methodsFor:'copying'! |
1101 |
||
1102 |
copyFrom:start to:stop |
|
1103 |
"return the subcollection starting at index start, anInteger and ending |
|
1104 |
at stop, anInteger. |
|
1105 |
- reimplemented here for speed" |
|
1106 |
||
1107 |
%{ /* NOCONTEXT */ |
|
1108 |
||
1109 |
REGISTER unsigned char *srcp; |
|
1110 |
REGISTER unsigned char *dstp; |
|
1111 |
REGISTER int count; |
|
1112 |
int len, index1, index2, sz; |
|
1113 |
OBJ newByteArray; |
|
1114 |
||
1115 |
if (__isByteArray(self) |
|
249 | 1116 |
&& __bothSmallInteger(start, stop)) { |
1117 |
len = __byteArraySize(self); |
|
1133 | 1118 |
index1 = __intVal(start); |
1119 |
index2 = __intVal(stop); |
|
92 | 1120 |
|
155 | 1121 |
if ((index1 <= index2) && (index1 > 0)) { |
1122 |
if (index2 <= len) { |
|
1123 |
count = index2 - index1 + 1; |
|
834 | 1124 |
__PROTECT_CONTEXT__ |
155 | 1125 |
sz = OHDR_SIZE + count; |
2915 | 1126 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
834 | 1127 |
__UNPROTECT_CONTEXT__ |
155 | 1128 |
if (newByteArray != nil) { |
1133 | 1129 |
__InstPtr(newByteArray)->o_class = ByteArray; |
1130 |
dstp = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
2648 | 1131 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
1132 |
||
1133 |
#ifdef bcopy4 |
|
2915 | 1134 |
if ((((unsigned INT)srcp & 3) == 0) |
1135 |
&& (((unsigned INT)dstp & 3) == 0)) { |
|
1136 |
/* copy aligned part */ |
|
1137 |
int nW = count >> 2; |
|
2648 | 1138 |
|
2915 | 1139 |
if (count & 3) { |
1140 |
nW++; |
|
1141 |
} |
|
1142 |
bcopy4(srcp, dstp, nW); |
|
1143 |
RETURN ( newByteArray ); |
|
1144 |
} |
|
2648 | 1145 |
#endif /* bcopy4 */ |
1146 |
||
92 | 1147 |
#ifdef FAST_MEMCPY |
2648 | 1148 |
bcopy(srcp, dstp, count); |
92 | 1149 |
#else |
155 | 1150 |
while (count--) { |
1151 |
*dstp++ = *srcp++; |
|
1152 |
} |
|
92 | 1153 |
#endif |
155 | 1154 |
RETURN ( newByteArray ); |
1155 |
} |
|
1156 |
} |
|
1157 |
} |
|
92 | 1158 |
} |
1159 |
%}. |
|
1160 |
" |
|
1161 |
fall back in case of non-integer index or out-of-bound index; |
|
1162 |
will eventually lead to an out-of-bound signal raise |
|
1163 |
" |
|
1164 |
^ super copyFrom:start to:stop |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1165 |
! |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1166 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1167 |
symbolFrom:start to:stop |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1168 |
"make a symbol from the characters of the subcollection starting |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1169 |
at index start, anInteger and ending at stop, anInteger. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1170 |
This saves us garbage and character copying." |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1171 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1172 |
|sym| |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1173 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1174 |
%{ /* STACK:1024 */ |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1175 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1176 |
REGISTER unsigned char *srcp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1177 |
REGISTER unsigned char *endp; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1178 |
REGISTER int count; |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1179 |
int len, index1, index2; |
4086 | 1180 |
unsigned char scratchBuffer[1024], savec; |
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1181 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1182 |
if (__isByteArray(self) && __bothSmallInteger(start, stop)) { |
4661 | 1183 |
len = __byteArraySize(self); |
1184 |
index1 = __intVal(start); |
|
1185 |
index2 = __intVal(stop); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1186 |
|
4661 | 1187 |
if ((index1 <= index2) && (index1 > 0) && (index2 <= len)) { |
1188 |
count = index2 - index1 + 1; |
|
1189 |
srcp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
|
1190 |
if (index2 < len) { |
|
1191 |
/* temporarily stuff in a '\0' */ |
|
1192 |
endp = srcp + count + 1; |
|
1193 |
savec = *endp; |
|
1194 |
*endp = '\0'; |
|
1195 |
sym = __MKSYMBOL(srcp, 0); |
|
4086 | 1196 |
/* must refetch endp (in case of a GC */ |
4661 | 1197 |
endp = __ByteArrayInstPtr(self)->ba_element + index1 + count; |
1198 |
*endp = savec; |
|
1199 |
} else { |
|
1200 |
/* not enough space for '\0', copy the bytes */ |
|
4128 | 1201 |
if (count < sizeof(scratchBuffer)) { |
4661 | 1202 |
bcopy(srcp, scratchBuffer, count); |
1203 |
scratchBuffer[count] = '\0'; |
|
1204 |
sym = __MKSYMBOL(scratchBuffer, 0); |
|
4086 | 1205 |
} |
4661 | 1206 |
} |
1207 |
} |
|
1208 |
if (sym != nil) |
|
1209 |
RETURN(sym); |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1210 |
} |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1211 |
%}. |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1212 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1213 |
fall back in case of non-integer index or out-of-bound index |
4086 | 1214 |
or not enough stack-memory available; |
1215 |
may eventually lead to an out-of-bound signal raise |
|
3325
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1216 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1217 |
^ (super copyFrom:start to:stop) asString asSymbol |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1218 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1219 |
|
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1220 |
" |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1221 |
'abcdefghijklmnop' symbolFrom:1 to:3 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1222 |
'abcdefghijklmnop' symbolFrom:3 to:16 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1223 |
'abcdefghijklmnop' symbolFrom:3 to:17 |
e958a341f41d
Add #from: and #with:form:to class methods.
Stefan Vogel <sv@exept.de>
parents:
3234
diff
changeset
|
1224 |
" |
92 | 1225 |
! ! |
1226 |
||
4154 | 1227 |
!ByteArray methodsFor:'filling & replacing'! |
1 | 1228 |
|
155 | 1229 |
from:start to:stop put:aNumber |
1230 |
"fill part of the receiver with aNumber. |
|
1231 |
- reimplemented here for speed" |
|
1232 |
||
1233 |
%{ /* NOCONTEXT */ |
|
1234 |
||
1235 |
REGISTER unsigned char *dstp; |
|
1236 |
REGISTER int count, value; |
|
249 | 1237 |
int len, index1, index2; |
1238 |
OBJ cls; |
|
155 | 1239 |
|
249 | 1240 |
if (__isSmallInteger(aNumber) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1241 |
&& __bothSmallInteger(start, stop) |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1242 |
&& __isBytes(self)) { |
2915 | 1243 |
len = __byteArraySize(self); |
1244 |
index1 = __intVal(start); |
|
1245 |
index2 = __intVal(stop); |
|
2866 | 1246 |
|
2915 | 1247 |
dstp = __ByteArrayInstPtr(self)->ba_element + index1 - 1; |
1248 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1249 |
int nInst; |
|
155 | 1250 |
|
2915 | 1251 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1252 |
dstp += nInst; |
|
1253 |
len -= nInst; |
|
1254 |
} |
|
249 | 1255 |
|
2915 | 1256 |
value = __intVal(aNumber); |
1257 |
if (((unsigned)value <= 0xFF) /* i.e. (value >= 0) && (value <= 255) */ |
|
1258 |
&& (index1 <= index2) |
|
1259 |
&& (index1 > 0)) { |
|
1260 |
if (index2 <= len) { |
|
1261 |
count = index2 - index1 + 1; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1262 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1263 |
#ifdef memset4 |
2915 | 1264 |
if (count > 20) { |
1265 |
while (((unsigned INT)dstp & 3) != 0) { |
|
1266 |
*dstp++ = value; |
|
1267 |
count--; |
|
1268 |
} |
|
1269 |
{ |
|
1270 |
int n4 = count & ~3; |
|
1271 |
int v4, nW; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1272 |
|
2915 | 1273 |
v4 = (value << 8) | value; |
1274 |
v4 = (v4 << 16) | v4; |
|
1275 |
nW = n4>>2; |
|
1276 |
memset4(dstp, v4, nW); |
|
1277 |
count -= n4; |
|
1278 |
dstp += n4; |
|
1279 |
} |
|
1280 |
while (count--) { |
|
1281 |
*dstp++ = value; |
|
1282 |
} |
|
1283 |
RETURN (self); |
|
1284 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1285 |
#endif /* memset4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1286 |
|
155 | 1287 |
#ifdef FAST_MEMSET |
2915 | 1288 |
memset(dstp, value, count); |
155 | 1289 |
#else |
422 | 1290 |
# ifdef UNROLL_LOOPS |
2915 | 1291 |
while (count >= 8) { |
1292 |
dstp[0] = dstp[1] = dstp[2] = dstp[3] = |
|
1293 |
dstp[4] = dstp[5] = dstp[6] = dstp[7] = value; |
|
1294 |
dstp += 8; |
|
1295 |
count -= 8; |
|
1296 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1297 |
# endif /* UNROLL_LOOPS */ |
2915 | 1298 |
while (count--) { |
1299 |
*dstp++ = value; |
|
1300 |
} |
|
155 | 1301 |
#endif |
2915 | 1302 |
RETURN (self); |
1303 |
} |
|
1304 |
} |
|
155 | 1305 |
} |
1306 |
%}. |
|
1307 |
" |
|
1308 |
fall back in case of non-integer index or out-of-bound index/value; |
|
1309 |
will eventually lead to an out-of-bound signal raise |
|
1310 |
" |
|
1311 |
^ super from:start to:stop put:aNumber |
|
1312 |
! |
|
1313 |
||
249 | 1314 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
3676 | 1315 |
"replace elements from another collection, which must be a ByteArray- |
1316 |
like collection. |
|
1317 |
||
1318 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1319 |
therefore the change may affect all others referencing the receiver." |
|
1 | 1320 |
|
54 | 1321 |
%{ /* NOCONTEXT */ |
1 | 1322 |
|
54 | 1323 |
int nIndex, repNIndex; |
1324 |
int startIndex, stopIndex; |
|
1325 |
REGISTER unsigned char *src; |
|
1326 |
REGISTER int repStartIndex; |
|
1327 |
int repStopIndex, count; |
|
1328 |
REGISTER unsigned char *dst; |
|
249 | 1329 |
OBJ cls; |
1 | 1330 |
|
2894 | 1331 |
#ifndef NO_PRIM_BYTEARR |
249 | 1332 |
if (__isBytes(aCollection) |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
1333 |
&& __isBytes(self) |
249 | 1334 |
&& __bothSmallInteger(start, stop) |
1335 |
&& __isSmallInteger(repStart)) { |
|
4661 | 1336 |
startIndex = __intVal(start) - 1; |
1337 |
if (startIndex >= 0) { |
|
1338 |
dst = (__ByteArrayInstPtr(self)->ba_element) + startIndex; |
|
1339 |
nIndex = __byteArraySize(self); |
|
2866 | 1340 |
|
4661 | 1341 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
1342 |
int nInst; |
|
249 | 1343 |
|
4661 | 1344 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1345 |
dst += nInst; |
|
1346 |
nIndex -= nInst; |
|
1347 |
} |
|
2866 | 1348 |
|
4661 | 1349 |
stopIndex = __intVal(stop) - 1; |
1350 |
count = stopIndex - startIndex + 1; |
|
1351 |
if (count == 0) { |
|
1352 |
RETURN ( self ); |
|
1353 |
} |
|
249 | 1354 |
|
4661 | 1355 |
if ((count > 0) && (stopIndex < nIndex)) { |
1356 |
repStartIndex = __intVal(repStart) - 1; |
|
1357 |
if (repStartIndex >= 0) { |
|
1358 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
1359 |
src = (__ByteArrayInstPtr(aCollection)->ba_element) + repStartIndex; |
|
1360 |
if ((cls = __qClass(aCollection)) != @global(ByteArray)) { |
|
1361 |
int nInst; |
|
2866 | 1362 |
|
4661 | 1363 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1364 |
src += nInst; |
|
1365 |
repNIndex -= nInst; |
|
1366 |
} |
|
249 | 1367 |
|
4661 | 1368 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
1369 |
if (repStopIndex < repNIndex) { |
|
1370 |
if (aCollection == self) { |
|
1371 |
/* take care of overlapping copy */ |
|
1372 |
if (src < dst) { |
|
1373 |
/* must do a reverse copy */ |
|
1374 |
src += count; |
|
1375 |
dst += count; |
|
1376 |
while (count-- > 0) { |
|
1377 |
*--dst = *--src; |
|
1378 |
} |
|
1379 |
RETURN ( self ); |
|
1380 |
} |
|
1381 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1382 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1383 |
#ifdef bcopy4 |
4661 | 1384 |
if ((((unsigned INT)src & 3) == 0) |
1385 |
&& (((unsigned INT)dst & 3) == 0)) { |
|
1386 |
/* copy aligned part */ |
|
1387 |
int nW = count >> 2; |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1388 |
|
4661 | 1389 |
bcopy4(src, dst, nW); |
1390 |
if ((count = count & 3) != 0) { |
|
1391 |
/* copy any remaining part */ |
|
1392 |
src += (nW<<2); |
|
1393 |
dst += (nW<<2); |
|
1394 |
while (count--) { |
|
1395 |
*dst++ = *src++; |
|
1396 |
} |
|
1397 |
} |
|
1398 |
RETURN ( self ); |
|
1399 |
} |
|
4159
4511561766e1
tuned replaceBytes for alpha64
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
1400 |
#else |
4511561766e1
tuned replaceBytes for alpha64
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
1401 |
# ifdef alpha64 |
4661 | 1402 |
if ((((unsigned INT)src & 7) == 0) |
1403 |
&& (((unsigned INT)dst & 7) == 0)) { |
|
1404 |
/* copy aligned part */ |
|
1405 |
int nW = count >> 3; |
|
4159
4511561766e1
tuned replaceBytes for alpha64
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
1406 |
|
4661 | 1407 |
while (count >= 8) { |
1408 |
((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0]; |
|
1409 |
dst += 8; |
|
1410 |
src += 8; |
|
1411 |
count -= 8; |
|
1412 |
} |
|
1413 |
while (count--) { |
|
1414 |
*dst++ = *src++; |
|
1415 |
} |
|
1416 |
RETURN ( self ); |
|
1417 |
} |
|
4159
4511561766e1
tuned replaceBytes for alpha64
Claus Gittinger <cg@exept.de>
parents:
4154
diff
changeset
|
1418 |
# endif /* alpha64 */ |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1419 |
#endif /* bcopy4 */ |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1420 |
|
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1421 |
#ifdef FAST_MEMCPY |
4661 | 1422 |
bcopy(src, dst, count); |
1 | 1423 |
#else |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1424 |
# ifdef UNROLL_LOOPS |
4661 | 1425 |
while (count >= 8) { |
1426 |
dst[0] = src[0]; dst[1] = src[1]; |
|
1427 |
dst[2] = src[2]; dst[3] = src[3]; |
|
1428 |
dst[4] = src[4]; dst[5] = src[5]; |
|
1429 |
dst[6] = src[6]; dst[7] = src[7]; |
|
1430 |
dst += 8; src += 8; |
|
1431 |
count -= 8; |
|
1432 |
} |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
1433 |
# endif /* UNROLL_LOOPS */ |
4661 | 1434 |
while (count-- > 0) { |
1435 |
*dst++ = *src++; |
|
1436 |
} |
|
1 | 1437 |
#endif |
4661 | 1438 |
RETURN ( self ); |
1439 |
} |
|
1440 |
} |
|
1441 |
} |
|
1442 |
} |
|
54 | 1443 |
} |
2894 | 1444 |
#endif |
437 | 1445 |
%}. |
1446 |
" |
|
1447 |
fall back in case of non-ByteArray argument, |
|
1448 |
or for the error report if any index is invalid |
|
1449 |
" |
|
1450 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
563 | 1451 |
! |
1452 |
||
3676 | 1453 |
replaceBytesFrom:startIndex with:replacementCollection startingAt:repStartIndex |
1454 |
"replace elements from another collection, which must be |
|
1455 |
byte-array-like. |
|
1456 |
||
1457 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1458 |
therefore the change may affect all others referencing the receiver." |
|
1459 |
||
1460 |
^ self |
|
4661 | 1461 |
replaceBytesFrom:startIndex |
1462 |
to:(startIndex + replacementCollection size - repStartIndex) |
|
1463 |
with:replacementCollection |
|
1464 |
startingAt:repStartIndex |
|
3676 | 1465 |
|
1466 |
" |
|
1467 |
args: startIndex : <integer> |
|
4661 | 1468 |
replacementCollection : <collection of <bytes> > |
1469 |
repStartIndex : <integer> |
|
3676 | 1470 |
|
1471 |
returns: self |
|
1472 |
" |
|
1473 |
||
1474 |
"Created: / 27.7.1998 / 16:56:46 / cg" |
|
1475 |
"Modified: / 27.7.1998 / 16:58:38 / cg" |
|
1476 |
! |
|
1477 |
||
1478 |
replaceFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
|
1169 | 1479 |
"replace elements in the receiver between index start and stop, |
3234 | 1480 |
with elements taken from replacementCollection starting at repStart. |
3676 | 1481 |
Return the receiver. |
1482 |
||
1483 |
Notice: This operation modifies the receiver, NOT a copy; |
|
1484 |
therefore the change may affect all others referencing the receiver." |
|
563 | 1485 |
|
4783 | 1486 |
((aCollection class == self class) |
1487 |
or:[aCollection class isBytes]) ifTrue:[ |
|
1488 |
^ self replaceBytesFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
|
563 | 1489 |
]. |
3676 | 1490 |
^ super replaceFrom:startIndex to:stopIndex with:aCollection startingAt:repStartIndex |
1169 | 1491 |
|
3676 | 1492 |
" |
1493 |
args: startIndex : <integer> |
|
4783 | 1494 |
stopIndex : <integer> |
1495 |
replacementCollection : <collection of <bytes> > |
|
1496 |
repStartIndex : <integer> |
|
3676 | 1497 |
|
1498 |
returns: self |
|
1499 |
" |
|
1500 |
||
1501 |
"Modified: / 27.7.1998 / 16:58:33 / cg" |
|
1 | 1502 |
! ! |
1503 |
||
129 | 1504 |
!ByteArray methodsFor:'image manipulation support'! |
1 | 1505 |
|
4919 | 1506 |
bitAndBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
1507 |
"replace bytes in the receiver with the result of a bitAnd operation. |
|
1508 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1509 |
||
1510 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitAnd: |
|
1511 |
||
1512 |
" |
|
1513 |
#[1 2 3 4 5 6 7 8] |
|
1514 |
bitAndBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
|
1515 |
#[1 2 3 4 5 6 7 8] |
|
1516 |
bitAndBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
|
1517 |
" |
|
1518 |
||
1519 |
! |
|
1520 |
||
1521 |
bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:ruleSymbol |
|
1522 |
"perform a special case of an aligned bitBlit operation. |
|
1523 |
Bytes in the receiver from dstStart to dstEnd are destructively replaced by the result |
|
1524 |
of some logical operation, as specified by the ruleSymbol. |
|
1525 |
SourceBytes are fetched starting at sourceOffset. |
|
1526 |
Valid rule symbols are: |
|
1527 |
#copy - trivial; same as replaceBytesFrom:to:with:startingAt: |
|
1528 |
#bitXor: - xoring; byte[dI] = byte[dI] bitXor:(srcByte[sI]) |
|
1529 |
#bitAnd: - anding; byte[dI] = byte[dI] bitAnd:(srcByte[sI]) |
|
1530 |
#bitOr: - oring; byte[dI] = byte[dI] bitOr:(srcByte[sI]) |
|
1531 |
#+ - adding; byte[dI] = (byte[dI] + (srcByte[sI])) mod: 256 |
|
1532 |
#- - subtract; byte[dI] = (byte[dI] - (srcByte[sI])) mod: 256 |
|
1533 |
Warning: this is a destructive operation - elements in the receiver are overwritten. |
|
1534 |
" |
|
1535 |
||
1536 |
|srcIdx| |
|
1537 |
||
1538 |
%{ |
|
1539 |
if ((__isByteArray(sourceBytes)) |
|
1540 |
&& (__qClass(self) == ByteArray) |
|
1541 |
&& __isSmallInteger(dstStart) |
|
1542 |
&& __isSmallInteger(dstEnd) |
|
1543 |
&& __isSmallInteger(sourceStart)) { |
|
1544 |
unsigned char *srcP = __ByteArrayInstPtr(sourceBytes)->ba_element; |
|
1545 |
unsigned char *dstP = __ByteArrayInstPtr(self)->ba_element; |
|
1546 |
int srcLen = __byteArraySize(sourceBytes); |
|
1547 |
int dstLen = __byteArraySize(self); |
|
1548 |
int __srcStart = __intVal(sourceStart); |
|
1549 |
int __dstStart = __intVal(dstStart); |
|
1550 |
int count = __intVal(dstEnd) - __dstStart + 1; |
|
1551 |
||
1552 |
if ((__dstStart >= 1) |
|
1553 |
&& (__srcStart >= 1) |
|
1554 |
&& ((__dstStart + count - 1) <= dstLen) |
|
1555 |
&& ((__srcStart + count - 1) <= srcLen)) { |
|
1556 |
srcP += __srcStart - 1; |
|
1557 |
dstP += __dstStart - 1; |
|
1558 |
||
1559 |
#define OP_LOOP(OP) \ |
|
1560 |
while (count > 16) { \ |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1561 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1562 |
((unsigned int *)dstP)[1] OP (((unsigned int *)srcP)[1]); \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1563 |
((unsigned int *)dstP)[2] OP (((unsigned int *)srcP)[2]); \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1564 |
((unsigned int *)dstP)[3] OP (((unsigned int *)srcP)[3]); \ |
4919 | 1565 |
srcP += 16; \ |
1566 |
dstP += 16; \ |
|
1567 |
count -= 16; \ |
|
1568 |
} \ |
|
1569 |
while (count > 4) { \ |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1570 |
((unsigned int *)dstP)[0] OP (((unsigned int *)srcP)[0]); \ |
4919 | 1571 |
srcP += 4; \ |
1572 |
dstP += 4; \ |
|
1573 |
count -= 4; \ |
|
1574 |
} \ |
|
1575 |
while (count > 0) { \ |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1576 |
*dstP OP (*srcP); \ |
4919 | 1577 |
srcP++; \ |
1578 |
dstP++; \ |
|
1579 |
count--; \ |
|
1580 |
} |
|
1581 |
||
1582 |
||
1583 |
if (ruleSymbol == @symbol(bitXor:)) { |
|
1584 |
OP_LOOP( ^= ) |
|
1585 |
RETURN (self); |
|
1586 |
} |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1587 |
if (ruleSymbol == @symbol(bitXorNot:)) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1588 |
OP_LOOP( ^=~ ) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1589 |
RETURN (self); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1590 |
} |
4919 | 1591 |
if (ruleSymbol == @symbol(bitAnd:)) { |
1592 |
OP_LOOP( &= ) |
|
1593 |
RETURN (self); |
|
1594 |
} |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1595 |
if (ruleSymbol == @symbol(bitAndNot:)) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1596 |
OP_LOOP( &=~ ) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1597 |
RETURN (self); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1598 |
} |
4919 | 1599 |
if (ruleSymbol == @symbol(bitOr:)) { |
1600 |
OP_LOOP( |= ) |
|
1601 |
RETURN (self); |
|
1602 |
} |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1603 |
if (ruleSymbol == @symbol(bitOrNot:)) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1604 |
OP_LOOP( |=~ ) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1605 |
RETURN (self); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1606 |
} |
4919 | 1607 |
if (ruleSymbol == @symbol(copy)) { |
1608 |
OP_LOOP( = ) |
|
1609 |
RETURN (self); |
|
1610 |
} |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1611 |
if (ruleSymbol == @symbol(copyNot)) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1612 |
OP_LOOP( =~ ) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1613 |
RETURN (self); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1614 |
} |
4919 | 1615 |
if (ruleSymbol == @symbol(+)) { |
1616 |
OP_LOOP( += ) |
|
1617 |
RETURN (self); |
|
1618 |
} |
|
1619 |
if (ruleSymbol == @symbol(-)) { |
|
1620 |
OP_LOOP( -= ) |
|
1621 |
RETURN (self); |
|
1622 |
} |
|
1623 |
} |
|
1624 |
} |
|
1625 |
%}. |
|
1626 |
ruleSymbol == #copy ifTrue:[ |
|
1627 |
self replaceFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart. |
|
1628 |
^ self |
|
1629 |
]. |
|
1630 |
||
1631 |
srcIdx := sourceStart. |
|
1632 |
dstStart to:dstEnd do:[:dstIdx | |
|
1633 |
self at:dstIdx put:((self at:dstIdx) perform:ruleSymbol with:(sourceBytes at:srcIdx)). |
|
1634 |
srcIdx := srcIdx + 1. |
|
1635 |
]. |
|
1636 |
||
1637 |
" |
|
1638 |
#[1 2 3 4 5 6 7 8] |
|
1639 |
bitBlitBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
|
1640 |
#[1 2 3 4 5 6 7 8] |
|
1641 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#bitXor: |
|
1642 |
#[1 2 3 4 5 6 7 8] |
|
1643 |
bitBlitBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#bitAnd: |
|
1644 |
#[1 2 3 4 5 6 7 8] |
|
1645 |
bitBlitBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 rule:#+ |
|
1646 |
#[1 2 3 4 5 6 7 8] |
|
1647 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 1 1 1 1] startingAt:1 rule:#+ |
|
1648 |
#[1 2 3 4 5 6 7 8] |
|
1649 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#+ |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1650 |
#[1 2 3 4 5 6 7 8] |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
1651 |
bitBlitBytesFrom:1 to:4 with:#[1 1 1 1 2 2 2 2] startingAt:5 rule:#copyNot |
4919 | 1652 |
|
1653 |
#[1 2 3 4 5 6 7 8] |
|
1654 |
bitBlitBytesFrom:1 to:8 with:(1 to:8) startingAt:1 rule:#+ |
|
1655 |
" |
|
1656 |
! |
|
1657 |
||
1658 |
bitOrBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
|
1659 |
"replace bytes in the receiver with the result of a bitOr operation. |
|
1660 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1661 |
||
1662 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitOr: |
|
1663 |
||
1664 |
" |
|
1665 |
#[1 2 3 4 5 6 7 8] |
|
1666 |
bitOrBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
|
1667 |
#[1 2 3 4 5 6 7 8] |
|
1668 |
bitOrBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
|
1669 |
" |
|
1670 |
||
1671 |
! |
|
1672 |
||
1673 |
bitXorBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart |
|
1674 |
"replace bytes in the receiver with the result of an bitXor operation. |
|
1675 |
Warning: this is a destructive operation - elements in the receiver are overwritten." |
|
1676 |
||
1677 |
^ self bitBlitBytesFrom:dstStart to:dstEnd with:sourceBytes startingAt:sourceStart rule:#bitXor: |
|
1678 |
||
1679 |
" |
|
1680 |
#[1 2 3 4 5 6 7 8] |
|
1681 |
bitXorBytesFrom:1 to:3 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
|
1682 |
#[1 2 3 4 5 6 7 8] |
|
1683 |
bitXorBytesFrom:1 to:8 with:#[1 2 3 4 5 6 7 8] startingAt:1 |
|
1684 |
#[1 2 3 4 5 6 7 8] |
|
1685 |
bitXorBytesFrom:1 to:8 with:#[1 1 1 1 1 1 1 1] startingAt:1 |
|
1686 |
" |
|
1687 |
||
1688 |
! |
|
1689 |
||
2866 | 1690 |
compressPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
129 | 1691 |
"given the receiver with 8-bit pixels, compress them into aByteArray |
1692 |
with nBitsPerPixel-depth pixels. The width/height-arguments are needed |
|
1693 |
to allow for any padding. On the fly, the source bytes are translated |
|
1694 |
using aMapByteArray (if non-nil). |
|
1695 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1696 |
value of n is found in map at:(n + 1). |
|
1697 |
Output bits are filled left-to right, i.e. the first byte in the input |
|
1698 |
corresponds to the high bit(s) if the first byte in the input. |
|
1699 |
This method can be used to convert 8-bit image data to mono, 2-bit and 4-bit |
|
1700 |
bitmaps. |
|
1701 |
It can also be used to compress byte-arrays into bitArrays." |
|
1702 |
||
1703 |
%{ /* NOCONTEXT */ |
|
1704 |
||
1705 |
REGISTER unsigned char *src, *dst; |
|
1706 |
REGISTER int wrun; |
|
1707 |
unsigned char *dstNext; |
|
1472
abaa73dc0e9a
check for destination overrun
Claus Gittinger <cg@exept.de>
parents:
1459
diff
changeset
|
1708 |
unsigned char *dstEnd; |
129 | 1709 |
int bytesPerRow, mask, shift0, shift; |
1710 |
int w, h, hrun; |
|
1711 |
int srcBytes, dstBytes; |
|
1712 |
int bitsPerPixel; |
|
1713 |
int bits; |
|
1714 |
int ncells; |
|
1715 |
unsigned char *map; |
|
1716 |
||
1133 | 1717 |
if ((__qClass(self) == @global(ByteArray)) |
1718 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
|
249 | 1719 |
&& __isSmallInteger(nBitsPerPixel) |
1720 |
&& __bothSmallInteger(height, width)) { |
|
2915 | 1721 |
if ((aMapByteArray != nil) |
1722 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
1723 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1724 |
} else { |
|
1725 |
map = (unsigned char *)0; |
|
1726 |
} |
|
129 | 1727 |
|
2915 | 1728 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1729 |
w = __intVal(width); |
|
1730 |
h = __intVal(height); |
|
1731 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1732 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
1733 |
dstEnd = dst + __byteArraySize(aByteArray); |
|
1734 |
switch (bitsPerPixel) { |
|
1735 |
case 1: |
|
1736 |
mask = 0x01; |
|
1737 |
break; |
|
1738 |
case 2: |
|
1739 |
mask = 0x03; |
|
1740 |
break; |
|
1741 |
case 4: |
|
1742 |
mask = 0x0F; |
|
1743 |
break; |
|
1744 |
case 8: |
|
1745 |
mask = 0xFF; |
|
1746 |
break; |
|
1747 |
default: |
|
1748 |
printf("invalid depth in compressPixels\n"); |
|
1749 |
goto fail; |
|
1750 |
} |
|
1751 |
if (map) { |
|
1752 |
/* |
|
1753 |
* if a map is present, it must have entries for |
|
1754 |
* all possible byte-values (i.e. its size must be >= 256) |
|
1755 |
*/ |
|
1756 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256) { |
|
1757 |
printf("invalid map in compressPixels\n"); |
|
1758 |
goto fail; |
|
1759 |
} |
|
1760 |
} |
|
129 | 1761 |
|
2915 | 1762 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1763 |
dstBytes = bytesPerRow * h; |
|
1764 |
srcBytes = w * h; |
|
2866 | 1765 |
|
2915 | 1766 |
if ((__byteArraySize(self) >= srcBytes) |
1767 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1768 |
for (hrun=h; hrun; hrun--) { |
|
1769 |
dstNext = dst + bytesPerRow; |
|
1770 |
bits = 0; shift = 8; |
|
1771 |
if (map) { |
|
1772 |
for (wrun=w; wrun; wrun--) { |
|
1773 |
bits = (bits << bitsPerPixel) | (map[*src++] & mask); |
|
1774 |
shift -= bitsPerPixel; |
|
1775 |
if (shift == 0) { |
|
1776 |
if (dst == dstEnd) goto fail; |
|
1777 |
*dst++ = bits; |
|
1778 |
bits = 0; shift = 8; |
|
1779 |
} |
|
1780 |
} |
|
1781 |
} else { |
|
1782 |
for (wrun=w; wrun; wrun--) { |
|
1783 |
bits = (bits << bitsPerPixel) | (*src++ & mask); |
|
1784 |
shift -= bitsPerPixel; |
|
1785 |
if (shift == 0) { |
|
1786 |
if (dst == dstEnd) goto fail; |
|
1787 |
*dst++ = bits; |
|
1788 |
bits = 0; shift = 8; |
|
1789 |
} |
|
1790 |
} |
|
1791 |
} |
|
1792 |
if (shift != 8) { |
|
1793 |
if (dst == dstEnd) goto fail; |
|
1794 |
*dst = bits; |
|
1795 |
} |
|
1796 |
dst = dstNext; |
|
1797 |
} |
|
1798 |
RETURN ( self ); |
|
1799 |
} |
|
129 | 1800 |
} |
1801 |
fail: ; |
|
2866 | 1802 |
%}. |
129 | 1803 |
self primitiveFailed |
1804 |
||
1805 |
"Example1: |
|
1806 |
compress 1 byte-per-pixel bitmap to 1-bit-per-pixel bitmap |
|
1807 |
" |
|
1808 |
" |
|
1809 |
|inBits outBits| |
|
1810 |
||
1811 |
inBits := #[0 0 0 0 1 1 1 1 |
|
2915 | 1812 |
0 0 1 1 0 0 1 1 |
1813 |
0 1 0 1 0 1 0 1 |
|
1814 |
1 1 1 1 0 0 0 0]. |
|
129 | 1815 |
outBits := ByteArray new:4. |
1816 |
inBits compressPixels:1 width:8 height:4 |
|
2915 | 1817 |
into:outBits mapping:nil. |
129 | 1818 |
outBits inspect |
1819 |
" |
|
1820 |
||
1821 |
"Example2: |
|
1822 |
compress byte-array into a bitArray, translating 99 to 0-bits, |
|
1823 |
and 176 to 1-bits. (just a stupid example) |
|
1824 |
" |
|
1825 |
" |
|
1826 |
|inBits outBits map| |
|
1827 |
||
1828 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1829 |
map := ByteArray new:256. |
|
1830 |
map at:176+1 put:1. |
|
1831 |
||
1832 |
outBits := ByteArray new:2. |
|
1833 |
inBits compressPixels:1 width:16 height:1 |
|
2915 | 1834 |
into:outBits mapping:map. |
129 | 1835 |
outBits inspect |
1836 |
" |
|
1837 |
||
1838 |
"Example3: |
|
1839 |
compress byte-array into a bitArray, translating everything below 128 to 0-bits, |
|
1840 |
and 128 to 255 to 1-bits.99 to 0-bits (another stupid example) |
|
1841 |
" |
|
1842 |
" |
|
1843 |
|inBits outBits map| |
|
1844 |
||
1845 |
inBits := #[176 176 176 176 99 99 99 99 176 176 99 99 176 99 176 99]. |
|
1846 |
map := ByteArray new:256. |
|
1847 |
map atAll:(128+1 to:255+1) put:1. |
|
1848 |
||
1849 |
outBits := ByteArray new:2. |
|
1850 |
inBits compressPixels:1 width:16 height:1 |
|
2915 | 1851 |
into:outBits mapping:map. |
129 | 1852 |
outBits inspect |
1853 |
" |
|
563 | 1854 |
! |
1855 |
||
4829 | 1856 |
copyReverse |
1857 |
"reverse the order of my elements inplace - |
|
1858 |
written as a primitive for speed on image manipulations (mirror)" |
|
1859 |
||
1860 |
%{ /* NOCONTEXT */ |
|
1861 |
||
1862 |
REGISTER unsigned char *src, *dst; |
|
1863 |
REGISTER int cnt; |
|
1864 |
REGISTER unsigned t; |
|
1865 |
OBJ cls; |
|
1866 |
OBJ newByteArray; |
|
1867 |
int sz; |
|
1868 |
unsigned char *start; |
|
1869 |
||
1870 |
if (__qClass(self) == @global(ByteArray)) { |
|
1871 |
cnt = __byteArraySize(self); |
|
1872 |
__PROTECT_CONTEXT__ |
|
1873 |
sz = OHDR_SIZE + cnt; |
|
1874 |
__qNew(newByteArray, sz); /* OBJECT ALLOCATION */ |
|
1875 |
__UNPROTECT_CONTEXT__ |
|
1876 |
if (newByteArray != nil) { |
|
1877 |
__InstPtr(newByteArray)->o_class = ByteArray; |
|
1878 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1879 |
start = __ByteArrayInstPtr(newByteArray)->ba_element; |
|
1880 |
dst = start + cnt - 1; |
|
1881 |
while (dst >= start) { |
|
1882 |
*dst-- = *src++; |
|
1883 |
} |
|
1884 |
RETURN ( newByteArray ); |
|
1885 |
} |
|
1886 |
} |
|
1887 |
%}. |
|
1888 |
^ super copy reverse |
|
1889 |
||
1890 |
" |
|
1891 |
#[1 2 3 4 5] copyReverse |
|
1892 |
#[1 2 3 4] copyReverse |
|
1893 |
" |
|
1894 |
! |
|
1895 |
||
2866 | 1896 |
expandPixels:nBitsPerPixel width:width height:height into:aByteArray mapping:aMapByteArray |
563 | 1897 |
"given the receiver with nBitsPerPixel-depth pixels, expand them into |
1898 |
aByteArray with 8-bit pixels. The width/height-arguments are needed |
|
1899 |
to skip any padded src-bits. On the fly, the destination pixels |
|
1900 |
are translated using aMapByteArray (if non-nil). |
|
1901 |
Input bits are read left-to right, i.e. the first byte in the output |
|
1902 |
corresponds to the high bit(s) in the inputs first byte. |
|
1903 |
This is used to display mono, 2-bit and 4-bit bitmaps on grey-scale/color |
|
1904 |
machines. With nBitsPerPixel==8, this is a translate operation. |
|
1905 |
Notice that smalltalk indexing begins at 1; thus the map-index for a byte |
|
1906 |
value of n is found in map at:(n + 1). |
|
1907 |
It can also be used to expand bit-arrays into byteArrays. |
|
1908 |
This method is specialized for ByteArray arguments - it will not handle |
|
1909 |
anything else." |
|
1910 |
||
1911 |
%{ /* NOCONTEXT */ |
|
1912 |
||
1913 |
REGISTER unsigned char *src, *dst; |
|
1914 |
REGISTER int wrun; |
|
1915 |
unsigned char *srcNext; |
|
1916 |
int bytesPerRow, mask, shift0, shift; |
|
1917 |
int w, h, hrun; |
|
1918 |
int srcBytes, dstBytes; |
|
1919 |
int bitsPerPixel; |
|
1920 |
int bits; |
|
1921 |
int ncells; |
|
1922 |
unsigned char *map; |
|
1923 |
||
1133 | 1924 |
if ((__qClass(self) == @global(ByteArray)) |
1925 |
&& (__qClass(aByteArray) == @global(ByteArray)) |
|
563 | 1926 |
&& __isSmallInteger(nBitsPerPixel) |
1927 |
&& __bothSmallInteger(height, width)) { |
|
4661 | 1928 |
if ((aMapByteArray != nil) |
1929 |
&& (__Class(aMapByteArray) == @global(ByteArray))) { |
|
1930 |
map = __ByteArrayInstPtr(aMapByteArray)->ba_element; |
|
1931 |
} else { |
|
1932 |
map = (unsigned char *)0; |
|
1933 |
} |
|
563 | 1934 |
|
4661 | 1935 |
bitsPerPixel = __intVal(nBitsPerPixel); |
1936 |
w = __intVal(width); |
|
1937 |
h = __intVal(height); |
|
1938 |
src = __ByteArrayInstPtr(self)->ba_element; |
|
1939 |
dst = __ByteArrayInstPtr(aByteArray)->ba_element; |
|
1940 |
switch (bitsPerPixel) { |
|
1941 |
case 1: |
|
1942 |
mask = 0x01; |
|
1943 |
break; |
|
1944 |
case 2: |
|
1945 |
mask = 0x03; |
|
1946 |
break; |
|
1947 |
case 4: |
|
1948 |
mask = 0x0F; |
|
1949 |
break; |
|
1950 |
case 8: |
|
1951 |
mask = 0xFF; |
|
1952 |
break; |
|
1953 |
default: |
|
1954 |
printf("invalid depth in expandPixels\n"); |
|
1955 |
goto fail; |
|
1956 |
} |
|
1957 |
ncells = mask + 1; |
|
1958 |
if (map) { |
|
1959 |
/* |
|
1960 |
* if a map is present, it must have the correct size |
|
1961 |
* (i.e. 2 raisedTo:nBitsPerPixel) |
|
1962 |
*/ |
|
1963 |
if ((__qSize(aMapByteArray) - OHDR_SIZE) < ncells) { |
|
1964 |
printf("invalid map in expandPixels\n"); |
|
1965 |
goto fail; |
|
1966 |
} |
|
1967 |
} |
|
563 | 1968 |
|
4661 | 1969 |
bytesPerRow = (w * bitsPerPixel + 7) / 8; |
1970 |
shift0 = 8 - bitsPerPixel; |
|
1971 |
srcBytes = bytesPerRow * h; |
|
1972 |
dstBytes = w * h; |
|
1456 | 1973 |
|
4661 | 1974 |
if ((__byteArraySize(self) >= srcBytes) |
1975 |
&& (__byteArraySize(aByteArray) >= dstBytes)) { |
|
1976 |
for (hrun=h; hrun; hrun--) { |
|
1977 |
srcNext = src + bytesPerRow; |
|
1978 |
shift = shift0; |
|
1979 |
if (map) { |
|
1980 |
if (shift0 == 0) { |
|
1981 |
/* translate only */ |
|
1982 |
for (wrun=w; wrun; wrun--) { |
|
1983 |
bits = *src++; |
|
1984 |
*dst++ = map[bits]; |
|
1985 |
} |
|
1986 |
} else { |
|
1987 |
for (wrun=w; wrun; wrun--) { |
|
1988 |
if (shift == shift0) { |
|
1989 |
bits = *src++; |
|
1990 |
} |
|
1991 |
*dst++ = map[(bits >> shift) & mask]; |
|
1992 |
shift -= bitsPerPixel; |
|
1993 |
if (shift < 0) { |
|
1994 |
shift = shift0; |
|
1995 |
} |
|
1996 |
} |
|
1997 |
} |
|
1998 |
} else { |
|
1999 |
for (wrun=w; wrun; wrun--) { |
|
2000 |
if (shift == shift0) { |
|
2001 |
bits = *src++; |
|
2002 |
} |
|
2003 |
*dst++ = (bits >> shift) & mask; |
|
2004 |
shift -= bitsPerPixel; |
|
2005 |
if (shift < 0) { |
|
2006 |
shift = shift0; |
|
2007 |
} |
|
2008 |
} |
|
2009 |
} |
|
2010 |
src = srcNext; |
|
2011 |
} |
|
2012 |
RETURN ( self ); |
|
2013 |
} |
|
2014 |
printf("buffer size: self:%d expect at least:%d\n", |
|
2015 |
__byteArraySize(self), srcBytes); |
|
2016 |
printf("buffer size: arg:%d expect at least:%d\n", |
|
2017 |
__byteArraySize(aByteArray), dstBytes); |
|
563 | 2018 |
} |
2866 | 2019 |
printf("invalid args\n"); |
1527
af119b16aa16
better handle the translate-only case in #expandPixels:
ca
parents:
1505
diff
changeset
|
2020 |
|
563 | 2021 |
fail: ; |
2866 | 2022 |
%}. |
563 | 2023 |
self primitiveFailed |
2024 |
||
2025 |
"Example1: |
|
2026 |
expand 1-bit-per-pixel bitmap into a 1byte-per-pixel byteArray |
|
2027 |
" |
|
2028 |
" |
|
2029 |
|inBits outBits| |
|
2030 |
||
2031 |
inBits := #[2r11110000 |
|
4661 | 2032 |
2r11001100 |
2033 |
2r01010101 |
|
2034 |
2r00001111]. |
|
563 | 2035 |
outBits := ByteArray new:(8*4). |
2036 |
inBits expandPixels:1 width:8 height:4 |
|
4661 | 2037 |
into:outBits mapping:nil. |
563 | 2038 |
outBits inspect |
2039 |
" |
|
2040 |
||
2041 |
"Example2: |
|
2042 |
expand bit-array into a byteArray, translating 0-bits to 99, |
|
2043 |
1-bits to 176. (just a stupid example) |
|
2044 |
" |
|
2045 |
" |
|
2046 |
|inBits outBits| |
|
2047 |
||
2048 |
inBits := #[2r11110000 2r11001100]. |
|
2049 |
outBits := ByteArray new:16. |
|
2050 |
inBits expandPixels:1 width:16 height:1 |
|
4661 | 2051 |
into:outBits mapping:#[99 176]. |
563 | 2052 |
outBits inspect |
2053 |
" |
|
2054 |
! |
|
2055 |
||
2056 |
invert |
|
2057 |
"invert all bytes - used with image manipulations |
|
2058 |
written as a primitive for speed. |
|
2059 |
Q: is this really needed ?" |
|
2060 |
||
2061 |
%{ /* NOCONTEXT */ |
|
2062 |
||
2063 |
REGISTER unsigned char *dst; |
|
2064 |
REGISTER unsigned long *ldst; |
|
2065 |
REGISTER int cnt; |
|
2066 |
||
1133 | 2067 |
if (__qClass(self) == @global(ByteArray)) { |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2068 |
cnt = __byteArraySize(self); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2069 |
dst = __ByteArrayInstPtr(self)->ba_element; |
2768 | 2070 |
if (! ((INT)dst & (sizeof(long)-1))) { |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2071 |
ldst = (unsigned long *)dst; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2072 |
while (cnt > 16) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2073 |
ldst[0] = ~(ldst[0]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2074 |
ldst[1] = ~(ldst[1]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2075 |
ldst[2] = ~(ldst[2]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2076 |
ldst[3] = ~(ldst[3]); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2077 |
ldst += 4; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2078 |
cnt -= 16; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2079 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2080 |
while (cnt >= sizeof(long)) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2081 |
*ldst = ~(*ldst); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2082 |
ldst++; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2083 |
cnt -= sizeof(long); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2084 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2085 |
dst = (unsigned char *)ldst; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2086 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2087 |
while (cnt--) { |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2088 |
*dst = ~(*dst); |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2089 |
dst++; |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2090 |
} |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2091 |
RETURN ( self ); |
563 | 2092 |
} |
2093 |
%} |
|
2094 |
. |
|
2095 |
self primitiveFailed |
|
2096 |
! |
|
2097 |
||
2098 |
reverse |
|
2585 | 2099 |
"reverse the order of my elements inplace - |
563 | 2100 |
written as a primitive for speed on image manipulations (mirror)" |
2101 |
||
2102 |
%{ /* NOCONTEXT */ |
|
2103 |
||
2104 |
REGISTER unsigned char *p1, *p2; |
|
2105 |
REGISTER int cnt; |
|
2106 |
REGISTER unsigned t; |
|
2107 |
OBJ cls; |
|
2108 |
||
1133 | 2109 |
if (__qClass(self) == @global(ByteArray)) { |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2110 |
cnt = __byteArraySize(self); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2111 |
p1 = __ByteArrayInstPtr(self)->ba_element; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2112 |
p2 = p1 + cnt - 1; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2113 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2114 |
#if defined(i386) && defined(__GNUC__) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2115 |
/* |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2116 |
* can we use the bswap instruction ? |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2117 |
*/ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2118 |
if (__cpuType__ >= 586) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2119 |
if ((cnt & 3) == 0) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2120 |
unsigned int *ip1, *ip2; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2121 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2122 |
ip1 = (unsigned int *)p1; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2123 |
ip2 = (unsigned int *)(p2 - 3); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2124 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2125 |
# define BSWAP(__val) \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2126 |
({ \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2127 |
int __rslt; \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2128 |
asm("bswap %0 \n \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2129 |
" : "=r" (__rslt) \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2130 |
: "0" ((int)(__val)) \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2131 |
); \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2132 |
__rslt; \ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2133 |
}) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2134 |
ip2 -= 5; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2135 |
while (ip1 <= ip2) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2136 |
int t1, t2; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2137 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2138 |
t1 = ip1[0]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2139 |
t2 = ip2[5]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2140 |
ip2[5] = BSWAP(t1); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2141 |
ip1[0] = BSWAP(t2); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2142 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2143 |
t1 = ip1[1]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2144 |
t2 = ip2[4]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2145 |
ip2[4] = BSWAP(t1); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2146 |
ip1[1] = BSWAP(t2); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2147 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2148 |
t1 = ip1[2]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2149 |
t2 = ip2[3]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2150 |
ip2[3] = BSWAP(t1); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2151 |
ip1[2] = BSWAP(t2); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2152 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2153 |
t1 = ip1[3]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2154 |
t2 = ip2[2]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2155 |
ip2[2] = BSWAP(t1); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2156 |
ip1[3] = BSWAP(t2); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2157 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2158 |
ip1 += 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2159 |
ip2 -= 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2160 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2161 |
ip2 += 5; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2162 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2163 |
while (ip1 < ip2) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2164 |
int t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2165 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2166 |
t = BSWAP(*ip1); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2167 |
*ip1++ = BSWAP(*ip2); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2168 |
*ip2-- = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2169 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2170 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2171 |
if (ip1 == ip2) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2172 |
int t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2173 |
t = *ip1; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2174 |
t = BSWAP(t); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2175 |
*ip1 = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2176 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2177 |
RETURN ( self ); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2178 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2179 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2180 |
#endif /* i386 && __GNUC__ */ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2181 |
p2 -= 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2182 |
while (p1 <= p2) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2183 |
t = p1[0]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2184 |
p1[0] = p2[4]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2185 |
p2[4] = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2186 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2187 |
t = p1[1]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2188 |
p1[1] = p2[3]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2189 |
p2[3] = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2190 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2191 |
t = p1[2]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2192 |
p1[2] = p2[2]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2193 |
p2[2] = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2194 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2195 |
t = p1[3]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2196 |
p1[3] = p2[1]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2197 |
p2[1] = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2198 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2199 |
p1 += 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2200 |
p2 -= 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2201 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2202 |
p2 += 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2203 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2204 |
while (p1 < p2) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2205 |
t = *p1; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2206 |
*p1++ = *p2; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2207 |
*p2-- = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2208 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2209 |
RETURN ( self ); |
563 | 2210 |
} |
2211 |
%}. |
|
2212 |
^ super reverse |
|
2585 | 2213 |
|
2214 |
" |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2215 |
#[1 2 3 4 5] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2216 |
#[1 2 3 4] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2217 |
#[1 2 3 4 5 6] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2218 |
#[1 2 3 4 5 6 7] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2219 |
#[1 2 3 4 5 6 7 8] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2220 |
#[1 2 3 4 5 6 7 8 9] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2221 |
#[1 2 3 4 5 6 7 8 9 10] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2222 |
#[1 2 3 4 5 6 7 8 9 10 11 12] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2223 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2224 |
#[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2225 |
(1 to:255) asByteArray reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2226 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2227 |
1 to:1024 do:[:i| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2228 |
|bytes rBytes| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2229 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2230 |
bytes := ((1 to:i) asArray collect:[:i | i bitAnd:255]) asByteArray. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2231 |
rBytes := bytes copy. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2232 |
rBytes reverse reverse ~= bytes ifTrue:[ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2233 |
self halt |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2234 |
] |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2235 |
]. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2236 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2237 |
Time millisecondsToRun:[ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2238 |
10000000 timesRepeat:[ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2239 |
#[1 2 3 4 5 6 7 8] reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2240 |
] |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2241 |
] |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2242 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2243 |
|b| |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2244 |
b := (0 to:255) asByteArray. |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2245 |
Time millisecondsToRun:[ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2246 |
10000000 timesRepeat:[ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2247 |
b reverse |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2248 |
] |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2249 |
] |
2585 | 2250 |
" |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2251 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2252 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2253 |
swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2254 |
"swap bytes inplace - |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2255 |
written as a primitive for speed on image grabbing (if display order is different)" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2256 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2257 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2258 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2259 |
REGISTER unsigned char *p; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2260 |
REGISTER int cnt; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2261 |
REGISTER unsigned t; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2262 |
|
1133 | 2263 |
if (__qClass(self) == @global(ByteArray)) { |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2264 |
cnt = __byteArraySize(self); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2265 |
cnt = cnt & ~1; /* make it even */ |
1133 | 2266 |
p = __ByteArrayInstPtr(self)->ba_element; |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2267 |
while (cnt > 0) { |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2268 |
#ifdef OLD |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2269 |
t = p[0]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2270 |
p[0] = p[1]; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2271 |
p[1] = t; |
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2272 |
#else |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2273 |
unsigned short s; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2274 |
|
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2275 |
s = ((unsigned short *)p)[0]; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2276 |
s = (s >> 8) | (s << 8); |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2277 |
((unsigned short *)p)[0] = s; |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
2278 |
#endif |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2279 |
p += 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2280 |
cnt -= 2; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2281 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2282 |
RETURN ( self ); |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2283 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2284 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2285 |
^ super swapBytes "/ rubbish - there is no one currenly |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2286 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2287 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2288 |
#[1 2 3 4 5 6 7 8 9 10] copy swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2289 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapBytes |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2290 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2291 |
! |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2292 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2293 |
swapLongs |
2866 | 2294 |
"swap long bytes inplace - any partial longs at the end |
2295 |
are not swapped. |
|
2296 |
written as a primitive for speed on image grabbing |
|
2297 |
(if display order is different)" |
|
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2298 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2299 |
%{ /* NOCONTEXT */ |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2300 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2301 |
REGISTER unsigned char *p; |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2302 |
REGISTER int cnt; |
931 | 2303 |
REGISTER unsigned t; |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2304 |
|
1133 | 2305 |
if (__qClass(self) == @global(ByteArray)) { |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2306 |
cnt = __byteArraySize(self); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2307 |
cnt = cnt & ~3; /* make it even */ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2308 |
p = __ByteArrayInstPtr(self)->ba_element; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2309 |
while (cnt > 0) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2310 |
t = p[0]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2311 |
p[0] = p[3]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2312 |
p[3] = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2313 |
t = p[1]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2314 |
p[1] = p[2]; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2315 |
p[2] = t; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2316 |
p += 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2317 |
cnt -= 4; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2318 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2319 |
RETURN ( self ); |
930
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2320 |
} |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2321 |
%}. |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2322 |
^ super swapLongs "/ rubbish - there is no one currenly |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2323 |
|
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2324 |
" |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2325 |
#[1 2 3 4 5 6 7 8 9] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2326 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2327 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2328 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongs |
dd2ba4051d6a
added swapBytes & swapLongs (needed when grabbing depth8/depth16 image from screen)
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
2329 |
" |
4365 | 2330 |
! |
2331 |
||
2332 |
swapLongsFrom:startIndex to:endIndex |
|
2333 |
"swap longs inplace - any partial longs at the end |
|
2334 |
are not swapped. |
|
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2335 |
Swapping is from startIndex to (exclusiv) endIndex; |
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2336 |
indexing starts at 1." |
4365 | 2337 |
|
2338 |
%{ /* NOCONTEXT */ |
|
2339 |
||
2340 |
REGISTER unsigned char *p; |
|
2341 |
REGISTER int limit; |
|
2342 |
REGISTER unsigned t; |
|
2343 |
||
2344 |
if ((__qClass(self) == @global(ByteArray)) |
|
2345 |
&& __isSmallInteger(startIndex) |
|
2346 |
&& __isSmallInteger(endIndex)) { |
|
4661 | 2347 |
unsigned int __idx = __intVal(startIndex); |
2348 |
unsigned int __endIdx = __intVal(endIndex); |
|
4365 | 2349 |
|
4661 | 2350 |
limit = __byteArraySize(self) + 1; |
2351 |
if (__endIdx < limit) { |
|
2352 |
limit = __endIdx; |
|
2353 |
} |
|
2354 |
p = __ByteArrayInstPtr(self)->ba_element; |
|
2355 |
p = p + __idx - 1; |
|
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2356 |
|
4661 | 2357 |
while ((__idx+4) <= limit) { |
2358 |
t = p[0]; |
|
2359 |
p[0] = p[3]; |
|
2360 |
p[3] = t; |
|
2361 |
t = p[1]; |
|
2362 |
p[1] = p[2]; |
|
2363 |
p[2] = t; |
|
2364 |
p += 4; |
|
2365 |
__idx += 4; |
|
2366 |
} |
|
2367 |
RETURN ( self ); |
|
4365 | 2368 |
} |
2369 |
%}. |
|
2370 |
^ super swapLongsFrom:startIndex to:endIndex "/ rubbish - there is no one currenly |
|
2371 |
||
2372 |
" |
|
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2373 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:1 to:5 |
4365 | 2374 |
#[1 2 3 4 5 6 7 8 9 10] copy swapLongsFrom:1 to:11 |
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2375 |
#[1 2 3 4 5 6 7 8 9 10 11] copy swapLongsFrom:1 to:12 |
4365 | 2376 |
#[1 2 3 4 5 6 7 8 9 10 11 12] copy swapLongsFrom:1 to:13 |
4367
3d481db099ec
oops - swapLongsFrom:to: was wrong
Claus Gittinger <cg@exept.de>
parents:
4365
diff
changeset
|
2377 |
#[1 2 3 4 5 6 7 8 9] copy swapLongsFrom:5 to:10 |
4365 | 2378 |
" |
1 | 2379 |
! ! |
550 | 2380 |
|
2381 |
!ByteArray methodsFor:'printing & storing'! |
|
2382 |
||
2383 |
asPackedString |
|
2384 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
2385 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
2386 |
as a corresponding character (32+value) and the resulting string |
|
2387 |
is returned. The resulting string is always a multiple of 4 (since |
|
2388 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
2389 |
encoded in the last character. |
|
2390 |
ST-80 uses this encoding for Images ... |
|
2391 |
PS: I dont like it ;-)" |
|
2392 |
||
2393 |
|outStream |
|
1974 | 2394 |
index "{ Class:SmallInteger}" |
2395 |
nextIndex "{ Class:SmallInteger}" |
|
2396 |
stop "{ Class:SmallInteger}" |
|
2397 |
n "{ Class:SmallInteger}" |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2398 |
mod "{ Class:SmallInteger}" |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2399 |
cpl| |
550 | 2400 |
|
2401 |
outStream := WriteStream on:String new. |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2402 |
cpl := 0. |
550 | 2403 |
index := 1. |
2404 |
stop := self size. |
|
2405 |
[index <= stop] whileTrue:[ |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2406 |
"take 3 source bytes" |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2407 |
n := (self at:index) bitShift:16. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2408 |
(index < stop) ifTrue:[ |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2409 |
nextIndex := index + 1. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2410 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2411 |
(nextIndex < stop) ifTrue:[ |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2412 |
n := n bitOr:(self at:(index + 2)). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2413 |
]. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2414 |
]. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2415 |
|
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2416 |
"took me a while to find that one out ..." |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2417 |
n := n bitXor:16r820820. |
550 | 2418 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2419 |
outStream nextPut:(Character value:(n bitShift:-18) + 32). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2420 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2421 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2422 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2423 |
index := index + 3. |
550 | 2424 |
|
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2425 |
"/ cg: |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2426 |
"/ lineBreak after every 120 characters |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2427 |
"/ fromPackedString will ignore those |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2428 |
cpl := cpl + 4. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2429 |
cpl >= 120 ifTrue:[ |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2430 |
outStream nextPut:Character cr. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2431 |
outStream nextPut:Character tab. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2432 |
cpl := 0. |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2433 |
]. |
550 | 2434 |
]. |
1973 | 2435 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
4809
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2436 |
outStream position:(outStream position - 1). |
ec743bebdd72
break long packedStrings after every 120 chars.
Claus Gittinger <cg@exept.de>
parents:
4807
diff
changeset
|
2437 |
outStream nextPut:(Character value:(mod + 96)). |
550 | 2438 |
]. |
2439 |
^ outStream contents |
|
1973 | 2440 |
|
1974 | 2441 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
563 | 2442 |
! |
2443 |
||
2444 |
displayString |
|
1244 | 2445 |
"return a printed representation of the receiver for displaying" |
2446 |
||
563 | 2447 |
^ self storeString |
2448 |
||
2449 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 2450 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 2451 |
! |
2452 |
||
4050 | 2453 |
printOn:aStream base:radix |
2454 |
"append a printed representation to aStream in the given number base." |
|
2455 |
||
2456 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
|
4661 | 2457 |
aStream nextPutAll:'#['. |
2458 |
self |
|
2459 |
do:[:byte | byte printOn:aStream base:radix] |
|
2460 |
separatedBy:[aStream space]. |
|
2461 |
aStream nextPut:$]. |
|
2462 |
^ self |
|
4050 | 2463 |
]. |
2464 |
^ super printOn:aStream |
|
2465 |
||
2466 |
" |
|
2467 |
#[1 2 3 4 5] printOn:Transcript base:2 |
|
2468 |
||
2469 |
#[1 2 3 4 5] storeString |
|
2470 |
#[1 2 3 4 5] displayString |
|
2471 |
#[1 2 3 4 5] printString |
|
2472 |
" |
|
2473 |
||
2474 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
|
2475 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
2476 |
! |
|
2477 |
||
563 | 2478 |
storeOn:aStream |
2479 |
"append a printed representation from which the receiver can be |
|
2480 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2481 |
||
1082 | 2482 |
self class == ByteArray ifTrue:[ "/ care for subclasses |
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2483 |
aStream nextPutAll:'#['. |
2915 | 2484 |
self |
2485 |
do:[:byte | byte storeOn:aStream] |
|
2486 |
separatedBy:[aStream space]. |
|
2579
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2487 |
aStream nextPutAll:']'. |
7e0783458c2f
much faster #from:to:put: and #replaceBytes on i386 platforms
Claus Gittinger <cg@exept.de>
parents:
2464
diff
changeset
|
2488 |
^ self |
563 | 2489 |
]. |
2490 |
^ super storeOn:aStream |
|
2491 |
||
2492 |
" |
|
1082 | 2493 |
#[1 2 3 4 5] storeOn:Transcript |
2494 |
||
2495 |
#[1 2 3 4 5] storeString |
|
2915 | 2496 |
#[1 2 3 4 5] displayString |
1082 | 2497 |
#[1 2 3 4 5] printString |
563 | 2498 |
" |
1082 | 2499 |
|
2915 | 2500 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
550 | 2501 |
! ! |
2502 |
||
2503 |
!ByteArray methodsFor:'queries'! |
|
2504 |
||
2505 |
indexOf:aByte startingAt:start |
|
2506 |
"return the index of the first occurrence of the argument, aByte |
|
2507 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
2508 |
- reimplemented here for speed" |
|
2509 |
||
2510 |
%{ /* NOCONTEXT */ |
|
2511 |
||
2512 |
REGISTER unsigned char *cp; |
|
2513 |
REGISTER int index, byteValue; |
|
2514 |
REGISTER int len; |
|
2515 |
OBJ cls; |
|
2516 |
||
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
2517 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
1133 | 2518 |
byteValue = __intVal(aByte); |
550 | 2519 |
|
1019 | 2520 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
2521 |
/* |
|
2522 |
* searching for something which cannot be found |
|
2523 |
*/ |
|
1133 | 2524 |
RETURN ( __MKSMALLINT(0) ); |
1019 | 2525 |
} |
550 | 2526 |
|
1019 | 2527 |
if (__isSmallInteger(start)) { |
1133 | 2528 |
index = __intVal(start); |
1019 | 2529 |
len = __byteArraySize(self); |
1133 | 2530 |
cp = __ByteArrayInstPtr(self)->ba_element; |
2531 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
1019 | 2532 |
int nInst; |
550 | 2533 |
|
1133 | 2534 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
1019 | 2535 |
cp += nInst; |
2536 |
len -= nInst; |
|
2537 |
} |
|
2538 |
cp += index - 1; |
|
2539 |
while (index <= len) { |
|
2540 |
if (*cp == byteValue) { |
|
1133 | 2541 |
RETURN ( __MKSMALLINT(index) ); |
1019 | 2542 |
} |
2543 |
index++; |
|
2544 |
cp++; |
|
2545 |
} |
|
1133 | 2546 |
RETURN ( __MKSMALLINT(0) ); |
1019 | 2547 |
} |
550 | 2548 |
} |
2549 |
%}. |
|
2550 |
^ super indexOf:aByte startingAt:start |
|
2551 |
||
2552 |
" |
|
2553 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 |
|
2554 |
" |
|
2555 |
! |
|
2556 |
||
1082 | 2557 |
isLiteral |
4661 | 2558 |
"return true, if the receiver can be used as a literal constant in ST syntax |
1082 | 2559 |
(i.e. can be used in constant arrays)" |
2560 |
||
1244 | 2561 |
"/ no, simply returning true here is a mistake: |
2562 |
"/ it could be a subclass of ByteArray |
|
2563 |
"/ (of which the compiler does not know at all ...) |
|
1082 | 2564 |
|
2565 |
^ self class == ByteArray |
|
1244 | 2566 |
|
2567 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
1082 | 2568 |
! |
2569 |
||
563 | 2570 |
max |
2571 |
"return the maximum value in the receiver - |
|
2572 |
redefined to speedup image processing and sound-player |
|
2573 |
(which need a fast method for this on byteArrays)" |
|
550 | 2574 |
|
563 | 2575 |
%{ /* NOCONTEXT */ |
550 | 2576 |
|
2577 |
REGISTER unsigned char *cp; |
|
563 | 2578 |
REGISTER int index, max; |
2579 |
int len; |
|
550 | 2580 |
|
1133 | 2581 |
if (__qClass(self) == @global(ByteArray)) { |
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2582 |
max = 0; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2583 |
index = 0; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2584 |
len = __qSize(self) - OHDR_SIZE; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2585 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2586 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2587 |
#if defined(i386) && defined(__GNUC__) |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2588 |
{ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2589 |
extern int __cpu_hasMMX; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2590 |
/* |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2591 |
* can we use mmx instructions ? |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2592 |
*/ |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2593 |
if ((__cpuType__ >= 586) && __cpu_hasMMX) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2594 |
printf("mmx\n"); |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2595 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2596 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2597 |
#endif |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2598 |
while (++index <= len) { |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2599 |
unsigned int byte; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2600 |
|
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2601 |
byte = *cp; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2602 |
cp++; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2603 |
if (byte > max) max = byte; |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2604 |
} |
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2605 |
RETURN ( __MKSMALLINT(max) ); |
550 | 2606 |
} |
563 | 2607 |
%}. |
2608 |
^ super max |
|
550 | 2609 |
|
2610 |
" |
|
563 | 2611 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2612 |
" |
1082 | 2613 |
! ! |
2614 |
||
2615 |
!ByteArray methodsFor:'special queries'! |
|
550 | 2616 |
|
2617 |
usageCounts |
|
2618 |
"return an array filled with value-counts - |
|
2619 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2620 |
(i.e. to build up a histogram of color usage within an image)." |
|
2621 |
||
2622 |
|counts| |
|
2623 |
||
2624 |
counts := Array basicNew:256. |
|
2625 |
||
2626 |
%{ /* STACK: 2000 */ |
|
2627 |
||
2628 |
REGISTER unsigned char *cp; |
|
2629 |
REGISTER int nByte; |
|
2630 |
REGISTER int index; |
|
2631 |
int icounts[256]; |
|
2632 |
||
1133 | 2633 |
if ((__qClass(self) == @global(ByteArray)) && __isArray(counts)) { |
550 | 2634 |
/* |
2635 |
* zero counts |
|
2636 |
*/ |
|
2637 |
for (index=0; index<256; index++) { |
|
2638 |
icounts[index] = 0; |
|
2639 |
} |
|
2640 |
||
2641 |
/* |
|
2642 |
* count |
|
2643 |
*/ |
|
2644 |
nByte = __qSize(self) - OHDR_SIZE; |
|
1133 | 2645 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
550 | 2646 |
while (nByte--) { |
2647 |
icounts[*cp++]++; |
|
2648 |
} |
|
2649 |
||
2650 |
/* |
|
2651 |
* make it real counts |
|
2652 |
*/ |
|
2653 |
for (index=0; index<256; index++) { |
|
1133 | 2654 |
__ArrayInstPtr(counts)->a_element[index] = __MKSMALLINT(icounts[index]); |
550 | 2655 |
} |
2656 |
RETURN ( counts ); |
|
2657 |
} |
|
2658 |
%} |
|
2659 |
. |
|
2660 |
self primitiveFailed |
|
2661 |
||
2662 |
" |
|
2663 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
|
2664 |
" |
|
2665 |
! |
|
2666 |
||
563 | 2667 |
usedValues |
2668 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
2669 |
This is needed specially in the bitmap/Imageclasses to find used colors |
|
2866 | 2670 |
of an image." |
550 | 2671 |
|
563 | 2672 |
%{ /* STACK: 400 */ |
550 | 2673 |
|
2674 |
REGISTER unsigned char *cp; |
|
2866 | 2675 |
unsigned char *endp; |
563 | 2676 |
REGISTER int len; |
2890 | 2677 |
OBJ result; |
2866 | 2678 |
union { |
2915 | 2679 |
unsigned char flags[256]; |
2680 |
int toForceAlignmentOfFlags; |
|
2866 | 2681 |
} f; |
2682 |
||
2683 |
#ifdef TO_BE_MEASURED |
|
2684 |
int coverage = 0; |
|
2685 |
#endif |
|
550 | 2686 |
|
1133 | 2687 |
if (__qClass(self) == @global(ByteArray)) { |
2915 | 2688 |
memset(f.flags, 0, sizeof(f.flags)); |
2689 |
len = __qSize(self) - OHDR_SIZE; |
|
2690 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2866 | 2691 |
|
2915 | 2692 |
/* for each used byte, set flag */ |
2693 |
while (len > 0) { |
|
2866 | 2694 |
#ifdef TO_BE_MEASURED |
2915 | 2695 |
unsigned byte; |
563 | 2696 |
|
2915 | 2697 |
byte = *cp; |
2698 |
if (! f.flags[byte]) { |
|
2699 |
f.flags[byte] = 1; |
|
2700 |
coverage++; |
|
2701 |
if (coverage == 256) { |
|
2702 |
/* no need to scan rest */ |
|
2703 |
break; |
|
2704 |
} |
|
2705 |
} |
|
2866 | 2706 |
#else |
2915 | 2707 |
f.flags[*cp] = 1; |
2866 | 2708 |
#endif |
2915 | 2709 |
cp++; |
2710 |
len--; |
|
2711 |
} |
|
563 | 2712 |
|
2915 | 2713 |
/* count 1's */ |
2714 |
len = 0; |
|
2715 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
2716 |
if ( *((unsigned int *)cp)) { |
|
2717 |
if (cp[0]) len++; |
|
2718 |
if (cp[1]) len++; |
|
2719 |
if (cp[2]) len++; |
|
2720 |
if (cp[3]) len++; |
|
2721 |
} |
|
2722 |
cp += 4; |
|
2723 |
} |
|
1133 | 2724 |
|
2915 | 2725 |
/* create ByteArray of used values */ |
2726 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
2727 |
if (result) { |
|
2728 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
2729 |
for (len=0; len < 256; len++) { |
|
2730 |
if (f.flags[len]) |
|
2731 |
*cp++ = len; |
|
2732 |
} |
|
2733 |
RETURN ( result ); |
|
2734 |
} |
|
550 | 2735 |
} |
2866 | 2736 |
%}. |
2737 |
^ self asIdentitySet asByteArray |
|
550 | 2738 |
|
2739 |
" |
|
563 | 2740 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 2741 |
" |
2742 |
! ! |
|
2743 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
2744 |
!ByteArray class methodsFor:'documentation'! |
628 | 2745 |
|
2746 |
version |
|
4924
9bec9689f646
reverse - 50% speedup on i386; 30% on all others
Claus Gittinger <cg@exept.de>
parents:
4919
diff
changeset
|
2747 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.124 1999-10-19 20:01:44 cg Exp $' |
628 | 2748 |
! ! |