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