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