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