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