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