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