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