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