author | Claus Gittinger <cg@exept.de> |
Sat, 06 Jun 2015 13:42:28 +0200 | |
changeset 18449 | 48c31fe80359 |
parent 17535 | 44798ad63741 |
child 18120 | e3a375d5f6a8 |
child 18584 | ee039e582133 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
155 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5425 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17377 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
3208
2d71538b9fd5
now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents:
3205
diff
changeset
|
16 |
UninterpretedBytes variableByteSubclass:#ByteArray |
1002
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
17 |
instanceVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
18 |
classVariableNames:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
19 |
poolDictionaries:'' |
c44e50c09986
care for 16bit subclass instances (in tuned methods)
Claus Gittinger <cg@exept.de>
parents:
931
diff
changeset
|
20 |
category:'Collections-Arrayed' |
1 | 21 |
! |
22 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
23 |
!ByteArray class methodsFor:'documentation'! |
88 | 24 |
|
563 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
88 | 39 |
documentation |
40 |
" |
|
41 |
ByteArrays store integers in the range 0..255. |
|
129 | 42 |
In contrast to normal arrays (which store pointers to their elements), |
362 | 43 |
byteArrays store the values in a dense & compact way. ByteArrays can |
129 | 44 |
be used to hold the data for bitmaps, images and other bulk data. |
45 |
ByteArrays are also used to store the bytecode-instructions of an |
|
46 |
interpreted method and are used as superclass for Strings. |
|
47 |
||
48 |
ByteArrays can be used as literals i.e. you can enter ByteArray-constants |
|
49 |
as #[ element1 element2 .... elementN] and also use byteArray constants |
|
50 |
as elements in a constant array. |
|
51 |
As in: #( #[1 1 1] #[2 2 2] #[3 3 3]) |
|
52 |
||
53 |
If you have to communicate structure-data (in the C-sense) with external |
|
54 |
programs/data-bases, see a companion class (Structure) in the goodies directory. |
|
55 |
It allows the definition of subclasses of ByteArray, which transparently fetch |
|
56 |
and store C-structure fields. |
|
1266 | 57 |
|
4062 | 58 |
[memory requirements:] |
4661 | 59 |
OBJ-HEADER + size |
4062 | 60 |
|
2145 | 61 |
[warning:] |
4661 | 62 |
read the warning about 'growing fixed size collection' |
63 |
in ArrayedCollection's documentation |
|
2145 | 64 |
|
1290 | 65 |
[author:] |
4661 | 66 |
Claus Gittinger |
1290 | 67 |
|
1266 | 68 |
[See also:] |
4661 | 69 |
Array CharacterArray String |
88 | 70 |
" |
2 | 71 |
! ! |
72 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
73 |
!ByteArray class methodsFor:'instance creation'! |
1 | 74 |
|
75 |
uninitializedNew:anInteger |
|
76 |
"return a new instance of the receiver with uninitialized |
|
77 |
(i.e. undefined) contents. The indexed elements have any random |
|
8908 | 78 |
value. However, any named instance variables are still nilled. |
75 | 79 |
For use, when contents will be set anyway shortly after - this |
80 |
is a bit faster than the regular basicNew:, which clears the bytes. |
|
81 |
Of course, it only makes a difference for very big ByteArrays, such |
|
82 |
as used for images/bitmaps. |
|
83 |
||
84 |
Notice: if you want to port code using uninitializedNew: to another |
|
8908 | 85 |
smalltalk, you have to add an 'uninitializedNew: -> basicNew:'-calling |
75 | 86 |
method to the ByteArray class of the other smalltalk." |
1 | 87 |
|
88 |
%{ /* NOCONTEXT */ |
|
89 |
OBJ newobj; |
|
90 |
INT instsize, nInstVars, nindexedinstvars; |
|
91 |
REGISTER OBJ *op; |
|
92 |
||
249 | 93 |
if (__isSmallInteger(anInteger)) { |
8908 | 94 |
nindexedinstvars = __intVal(anInteger); |
95 |
if (nindexedinstvars >= 0) { |
|
96 |
if (self == ByteArray) { |
|
97 |
/* |
|
98 |
* the most common case |
|
99 |
*/ |
|
100 |
instsize = OHDR_SIZE + nindexedinstvars; |
|
101 |
if (__CanDoQuickNew(instsize)) { /* OBJECT ALLOCATION */ |
|
102 |
__qCheckedNew(newobj, instsize); |
|
103 |
__InstPtr(newobj)->o_class = self; |
|
104 |
__qSTORE(newobj, self); |
|
105 |
RETURN (newobj ); |
|
106 |
} |
|
107 |
} else { |
|
108 |
/* |
|
109 |
* Take care for subclasses like TwoByteString |
|
110 |
*/ |
|
111 |
switch (__smallIntegerVal(__ClassInstPtr(self)->c_flags) & ARRAYMASK) { |
|
112 |
case BYTEARRAY: |
|
113 |
break; |
|
114 |
||
115 |
case WORDARRAY: |
|
116 |
case SWORDARRAY: |
|
117 |
nindexedinstvars *= 2; |
|
118 |
break; |
|
119 |
||
120 |
case LONGARRAY: |
|
121 |
case SLONGARRAY: |
|
122 |
nindexedinstvars *= 4; |
|
123 |
break; |
|
124 |
||
125 |
default: |
|
126 |
/* don't know about this array type, delegate to super */ |
|
127 |
goto out; |
|
128 |
} |
|
129 |
} |
|
130 |
nInstVars = __intVal(__ClassInstPtr(self)->c_ninstvars); |
|
131 |
instsize = OHDR_SIZE + __OBJS2BYTES__(nInstVars) + nindexedinstvars; |
|
132 |
__PROTECT_CONTEXT__ |
|
133 |
__qNew(newobj, instsize); /* OBJECT ALLOCATION */ |
|
134 |
__UNPROTECT_CONTEXT__ |
|
135 |
if (newobj != nil) { |
|
136 |
__InstPtr(newobj)->o_class = self; |
|
137 |
__qSTORE(newobj, self); |
|
138 |
if (nInstVars) { |
|
139 |
/* |
|
140 |
* still have to nil out named instvars ... |
|
141 |
*/ |
|
788
e80f1c42b87b
dont use memset4 if its undefined
Claus Gittinger <cg@exept.de>
parents:
628
diff
changeset
|
142 |
#if defined(memset4) && defined(FAST_OBJECT_MEMSET4) |
8908 | 143 |
memset4(__InstPtr(newobj)->i_instvars, nil, nInstVars); |
1 | 144 |
#else |
249 | 145 |
# if defined(FAST_MEMSET) && !defined(NEGATIVE_ADDRESSES) |
8908 | 146 |
/* |
147 |
* knowing that nil is 0 |
|
148 |
*/ |
|
149 |
memset(__InstPtr(newobj)->i_instvars, 0, instsize - OHDR_SIZE); |
|
249 | 150 |
# else |
8908 | 151 |
op = __InstPtr(newobj)->i_instvars; |
152 |
while (nInstVars--) |
|
153 |
*op++ = nil; |
|
249 | 154 |
# endif |
1 | 155 |
#endif |
8908 | 156 |
} |
157 |
RETURN ( newobj ); |
|
158 |
} |
|
159 |
} |
|
1 | 160 |
} |
8159
ac6725a62e17
#uninitializedNew: - take care of WORDARRAYS etc.
Stefan Vogel <sv@exept.de>
parents:
8096
diff
changeset
|
161 |
out:; |
5425 | 162 |
%}. |
1 | 163 |
^ self basicNew:anInteger |
164 |
! ! |
|
165 |
||
11502 | 166 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
167 |
!ByteArray class methodsFor:'queries'! |
550 | 168 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
169 |
elementByteSize |
16851
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
170 |
"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
|
171 |
Here, 1 is returned" |
e7212682103b
comment/format in: #elementByteSize
Claus Gittinger <cg@exept.de>
parents:
16807
diff
changeset
|
172 |
|
13706
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
173 |
^ 1 |
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
174 |
|
62f6489aa2c6
faster elementByteSize query
Claus Gittinger <cg@exept.de>
parents:
13703
diff
changeset
|
175 |
"Created: / 15-09-2011 / 14:12:53 / cg" |
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 |
|
550 | 178 |
isBuiltInClass |
1266 | 179 |
"return true if this class is known by the run-time-system. |
180 |
Here, true is returned for myself, false for subclasses." |
|
550 | 181 |
|
182 |
^ self == ByteArray |
|
1266 | 183 |
|
184 |
"Modified: 23.4.1996 / 15:56:25 / cg" |
|
550 | 185 |
! ! |
186 |
||
17535 | 187 |
|
10884 | 188 |
!ByteArray methodsFor:'Compatibility-Squeak'! |
189 |
||
12144 | 190 |
bitXor:aByteArray |
191 |
"return a new byteArray containing the bitWise-xor of the receiver's and the |
|
192 |
argument's bytes" |
|
193 |
||
15625 | 194 |
|size size1| |
195 |
||
196 |
"size := self size min:aByteArray size" |
|
197 |
size := self size. |
|
198 |
size1 := aByteArray size. |
|
199 |
size1 < size ifTrue:[ |
|
200 |
size := size1. |
|
12144 | 201 |
]. |
15625 | 202 |
|
203 |
^ self copy |
|
204 |
bitXorBytesFrom:1 to:size with:aByteArray startingAt:1; |
|
205 |
yourself. |
|
206 |
||
207 |
" |
|
208 |
#[0 1 2 3 4] bitXor:#[0 1 2 3 4] |
|
209 |
#[0 1 2 3 4] bitXor:#[0 1 2 3] |
|
210 |
" |
|
10884 | 211 |
! ! |
212 |
||
12091 | 213 |
!ByteArray methodsFor:'Compatibility-VW'! |
214 |
||
215 |
asByteString |
|
216 |
^ self asString |
|
217 |
! ! |
|
218 |
||
550 | 219 |
!ByteArray methodsFor:'accessing'! |
1 | 220 |
|
563 | 221 |
basicAt:index |
222 |
"return the indexed instance variable with index, anInteger |
|
1019 | 223 |
- redefined here to be slighly faster than the default in Object. |
224 |
Q: is it worth the extra code ?" |
|
563 | 225 |
|
226 |
%{ /* NOCONTEXT */ |
|
227 |
||
228 |
REGISTER int indx; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
229 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
230 |
REGISTER OBJ cls; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
231 |
REGISTER int nIndex; |
563 | 232 |
|
233 |
if (__isSmallInteger(index)) { |
|
4661 | 234 |
indx = __intVal(index) - 1; |
235 |
slf = self; |
|
236 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
237 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
238 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
239 |
goto fail; |
|
240 |
} |
|
241 |
if (indx < 0) goto fail; |
|
242 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
243 |
} |
|
244 |
nIndex = __byteArraySize(slf); |
|
245 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 246 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 247 |
} |
563 | 248 |
} |
1019 | 249 |
fail: ; |
250 |
%}. |
|
563 | 251 |
^ super basicAt:index |
252 |
! |
|
253 |
||
1 | 254 |
basicAt:index put:value |
1230 | 255 |
"set the indexed instance variable with index, anInteger to value. |
256 |
Returns value (sigh). |
|
1019 | 257 |
- redefined here to be slighly faster than the default in Object. |
258 |
Q: is it worth the extra code ?" |
|
1 | 259 |
|
260 |
%{ /* NOCONTEXT */ |
|
261 |
||
262 |
REGISTER int indx; |
|
263 |
int nIndex; |
|
264 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
265 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
266 |
REGISTER OBJ cls; |
1 | 267 |
|
249 | 268 |
if (__bothSmallInteger(index, value)) { |
4661 | 269 |
val = __intVal(value); |
270 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
271 |
indx = __intVal(index) - 1; |
|
272 |
slf = self; |
|
273 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
274 |
if (((INT)__ClassInstPtr(cls)->c_flags & __MASKSMALLINT(ARRAYMASK)) |
|
275 |
!= __MASKSMALLINT(BYTEARRAY)) { |
|
276 |
goto fail; |
|
277 |
} |
|
278 |
if (indx < 0) goto fail; |
|
279 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
280 |
} |
|
281 |
nIndex = __byteArraySize(slf); |
|
282 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
283 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
284 |
RETURN ( value ); |
|
285 |
} |
|
286 |
} |
|
1 | 287 |
} |
1019 | 288 |
fail: ; |
289 |
%}. |
|
1 | 290 |
^ super basicAt:index put:value |
1230 | 291 |
|
292 |
"Modified: 19.4.1996 / 11:14:40 / cg" |
|
2 | 293 |
! |
294 |
||
4128 | 295 |
bitAt:index |
11502 | 296 |
"return the bit at index (1 based index) as 0 or 1" |
5439 | 297 |
|
298 |
|byteIndex bitIndex0 byte| |
|
4128 | 299 |
|
300 |
%{ /* NOCONTEXT */ |
|
301 |
||
302 |
REGISTER int indx; |
|
303 |
REGISTER int byte; |
|
304 |
int nIndex; |
|
305 |
REGISTER OBJ slf; |
|
306 |
REGISTER OBJ cls; |
|
307 |
||
308 |
if (__isSmallInteger(index)) { |
|
12879 | 309 |
indx = __intVal(index) - 1; |
310 |
slf = self; |
|
311 |
||
312 |
byte = indx / 8; |
|
313 |
indx = indx % 8; |
|
314 |
||
315 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
316 |
if (indx < 0) goto badIndex; |
|
317 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
318 |
} |
|
319 |
nIndex = __byteArraySize(slf); |
|
320 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
321 |
RETURN ( __mkSmallInteger(((__ByteArrayInstPtr(slf)->ba_element[byte] & (1 << indx)) != 0)) ); |
|
322 |
} |
|
4128 | 323 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
324 |
badIndex: ; |
4128 | 325 |
%}. |
5439 | 326 |
byteIndex := ((index-1) // 8) + 1. |
327 |
bitIndex0 := ((index-1) \\ 8). |
|
328 |
byte := self at:byteIndex. |
|
329 |
^ byte bitTest:(1 bitShift:bitIndex0). |
|
4128 | 330 |
|
331 |
" |
|
12879 | 332 |
#[ 1 1 1 1 ] bitAt:9 |
4128 | 333 |
#[ 1 1 1 1 ] bitAt:11 |
334 |
#[ 2 2 2 2 ] bitAt:10 |
|
335 |
" |
|
336 |
! |
|
337 |
||
5439 | 338 |
bitClearAt:index |
8908 | 339 |
"clear the bit at index (index starts with 1)" |
5439 | 340 |
|
341 |
|byteIndex bitIndex0 byte| |
|
4128 | 342 |
|
343 |
%{ /* NOCONTEXT */ |
|
344 |
||
345 |
REGISTER int indx; |
|
346 |
REGISTER int byte; |
|
347 |
int nIndex; |
|
348 |
REGISTER OBJ slf; |
|
349 |
REGISTER OBJ cls; |
|
350 |
||
5439 | 351 |
if (0 /* __isSmallInteger(index) */) { |
8908 | 352 |
indx = __intVal(index) - 1; |
353 |
slf = self; |
|
354 |
||
355 |
byte = indx / 8; |
|
356 |
indx = indx % 8; |
|
357 |
||
358 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
359 |
if (indx < 0) goto badIndex; |
|
360 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
361 |
} |
|
362 |
nIndex = __byteArraySize(slf); |
|
363 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
364 |
__ByteArrayInstPtr(slf)->ba_element[byte] &= ~(1 << indx); |
|
365 |
RETURN (slf); |
|
366 |
} |
|
4128 | 367 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
368 |
badIndex: ; |
4128 | 369 |
%}. |
5439 | 370 |
byteIndex := ((index-1) // 8) + 1. |
371 |
bitIndex0 := ((index-1) \\ 8). |
|
372 |
byte := self at:byteIndex. |
|
373 |
byte := byte bitClear:(1 bitShift:bitIndex0). |
|
374 |
self at:byteIndex put:byte. |
|
375 |
||
376 |
" |
|
8908 | 377 |
#[0 0 0 0] copy bitClearAt:1 |
378 |
#[0 0 0 0] copy bitClearAt:7 |
|
379 |
#[0 0 0 0] copy bitClearAt:8 |
|
380 |
#[0 0 0 0] copy bitClearAt:9 |
|
5439 | 381 |
" |
4128 | 382 |
" |
8908 | 383 |
#[ 0 0 0 0 ] bitSetAt:1 |
384 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
385 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
386 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
387 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
388 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
5439 | 389 |
" |
390 |
||
391 |
||
392 |
||
393 |
! |
|
394 |
||
395 |
bitSetAt:index |
|
8908 | 396 |
"set the bit at index (index starts with 1)" |
5439 | 397 |
|
398 |
|byteIndex bitIndex0 byte| |
|
399 |
||
400 |
%{ /* NOCONTEXT */ |
|
401 |
||
402 |
REGISTER int indx; |
|
403 |
REGISTER int byte; |
|
404 |
int nIndex; |
|
405 |
REGISTER OBJ slf; |
|
406 |
REGISTER OBJ cls; |
|
407 |
||
408 |
if (0 /* __isSmallInteger(index) */) { |
|
8908 | 409 |
indx = __intVal(index) - 1; |
410 |
slf = self; |
|
411 |
||
412 |
byte = indx / 8; |
|
413 |
indx = indx % 8; |
|
414 |
||
415 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
416 |
if (indx < 0) goto badIndex; |
|
417 |
byte += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
418 |
} |
|
419 |
nIndex = __byteArraySize(slf); |
|
420 |
if ((unsigned)byte < (unsigned)nIndex) { |
|
421 |
__ByteArrayInstPtr(slf)->ba_element[byte] |= (1 << indx); |
|
422 |
RETURN (slf); |
|
423 |
} |
|
5439 | 424 |
} |
425 |
badIndex: ; |
|
426 |
%}. |
|
427 |
byteIndex := ((index-1) // 8) + 1. |
|
428 |
bitIndex0 := ((index-1) \\ 8). |
|
429 |
byte := self at:byteIndex. |
|
430 |
byte := byte bitOr:(1 bitShift:bitIndex0). |
|
431 |
self at:byteIndex put:byte. |
|
432 |
||
433 |
" |
|
8908 | 434 |
#[0 0 0 0] copy bitSetAt:1 |
435 |
#[0 0 0 0] copy bitSetAt:7 |
|
436 |
#[0 0 0 0] copy bitSetAt:8 |
|
437 |
#[0 0 0 0] copy bitSetAt:9 |
|
5439 | 438 |
" |
439 |
" |
|
8908 | 440 |
#[ 0 0 0 0 ] bitSetAt:1 |
441 |
#[ 0 0 0 0 ] bitSetAt:4 |
|
442 |
#[ 0 0 0 0 ] bitSetAt:8 |
|
443 |
#[ 0 0 0 0 ] bitSetAt:9 |
|
444 |
#[ 0 0 0 0 ] bitSetAt:10 |
|
445 |
#[ 0 0 0 0 ] bitSetAt:11 |
|
4128 | 446 |
" |
447 |
||
448 |
||
449 |
||
5554 | 450 |
! ! |
451 |
||
452 |
!ByteArray methodsFor:'accessing-bytes'! |
|
4128 | 453 |
|
54 | 454 |
byteAt:index |
8908 | 455 |
"return the byte at index. |
456 |
For ByteArray, this is the same as basicAt:; |
|
249 | 457 |
however, for strings or symbols, this returns a numeric byteValue |
458 |
instead of a character." |
|
459 |
||
460 |
%{ /* NOCONTEXT */ |
|
461 |
||
462 |
REGISTER int indx; |
|
463 |
int nIndex; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
464 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
465 |
REGISTER OBJ cls; |
54 | 466 |
|
249 | 467 |
if (__isSmallInteger(index)) { |
4661 | 468 |
indx = __intVal(index) - 1; |
469 |
slf = self; |
|
470 |
if ((cls = __qClass(slf)) != @global(ByteArray)) { |
|
471 |
if (indx < 0) goto badIndex; |
|
472 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
473 |
} |
|
474 |
nIndex = __byteArraySize(slf); |
|
475 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
8913 | 476 |
RETURN ( __mkSmallInteger((__ByteArrayInstPtr(slf)->ba_element[indx])) ); |
4661 | 477 |
} |
249 | 478 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
479 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
480 |
%}. |
249 | 481 |
^ (super basicAt:index) asInteger |
54 | 482 |
! |
483 |
||
249 | 484 |
byteAt:index put:value |
485 |
"set the byte at index. For ByteArray, this is the same as basicAt:put:. |
|
486 |
However, for Strings, this expects a byteValue to be stored." |
|
487 |
||
488 |
%{ /* NOCONTEXT */ |
|
489 |
||
490 |
REGISTER int indx; |
|
491 |
int nIndex; |
|
492 |
int val; |
|
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
493 |
REGISTER OBJ slf; |
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
494 |
REGISTER OBJ cls; |
54 | 495 |
|
249 | 496 |
if (__bothSmallInteger(index, value)) { |
4661 | 497 |
val = __intVal(value); |
498 |
if ((unsigned)(val) <= 0xFF /* i.e. (val >= 0) && (val <= 255) */) { |
|
499 |
indx = __intVal(index) - 1; |
|
500 |
slf = self; |
|
501 |
if ((cls = __qClass(slf)) != ByteArray) { |
|
502 |
if (indx < 0) goto badIndex; |
|
503 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
504 |
} |
|
505 |
nIndex = __byteArraySize(slf); |
|
506 |
if ((unsigned)indx < (unsigned)nIndex) { |
|
507 |
__ByteArrayInstPtr(slf)->ba_element[indx] = val; |
|
508 |
RETURN ( value ); |
|
509 |
} |
|
510 |
} |
|
249 | 511 |
} |
4386
ecc88f1c1169
oops - subclasses with instVars allowed for index-less-than-1-access
Claus Gittinger <cg@exept.de>
parents:
4367
diff
changeset
|
512 |
badIndex: ; |
1147
a83c86d02b59
can do low..hi index check with one compare
Claus Gittinger <cg@exept.de>
parents:
1133
diff
changeset
|
513 |
%}. |
249 | 514 |
^ super basicAt:index put:value |
5554 | 515 |
! ! |
516 |
||
517 |
!ByteArray methodsFor:'accessing-longs'! |
|
54 | 518 |
|
2 | 519 |
doubleWordAt:index put:value |
520 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
359 | 521 |
The value should be in the range 0 to 16rFFFFFFFF |
522 |
(for negative values, the stored value is not defined). |
|
523 |
The value is stored in the machines natural byte order. |
|
524 |
Q: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
|
525 |
||
526 |
|t| |
|
527 |
||
528 |
%{ /* NOCONTEXT */ |
|
529 |
||
530 |
REGISTER int indx; |
|
531 |
int nIndex; |
|
532 |
union { |
|
8908 | 533 |
unsigned char u_char[4]; |
534 |
unsigned int u_uint; |
|
359 | 535 |
} val; |
536 |
OBJ cls; |
|
2950 | 537 |
unsigned char *byteP; |
359 | 538 |
|
539 |
if (__isSmallInteger(index)) { |
|
8908 | 540 |
if (__isSmallInteger(value)) { |
541 |
val.u_uint = __intVal(value); |
|
542 |
} else { |
|
543 |
val.u_uint = __longIntVal(value); |
|
544 |
if (val.u_uint == 0) goto error; |
|
545 |
} |
|
546 |
||
547 |
indx = __intVal(index); |
|
548 |
if (indx > 0) { |
|
549 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
550 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
551 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
552 |
if ((indx+3) <= nIndex) { |
|
553 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
554 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
8908 | 555 |
((unsigned int *)byteP)[0] = val.u_uint; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
556 |
#else |
8908 | 557 |
if (((unsigned INT)byteP & 3) == 0) { |
558 |
((unsigned int *)byteP)[0] = val.u_uint; |
|
559 |
} else { |
|
560 |
byteP[0] = val.u_char[0]; |
|
561 |
byteP[1] = val.u_char[1]; |
|
562 |
byteP[2] = val.u_char[2]; |
|
563 |
byteP[3] = val.u_char[3]; |
|
564 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
565 |
#endif |
8908 | 566 |
RETURN ( value ); |
567 |
} |
|
568 |
} |
|
359 | 569 |
} |
570 |
error: ; |
|
571 |
%}. |
|
5439 | 572 |
^ super doubleWordAt:index put:value. |
359 | 573 |
|
574 |
" |
|
575 |
|b| |
|
576 |
b := ByteArray new:4. |
|
577 |
b doubleWordAt:1 put:16r04030201. |
|
578 |
b inspect |
|
579 |
" |
|
580 |
! |
|
581 |
||
582 |
doubleWordAt:index put:value MSB:msb |
|
583 |
"set the 4-bytes starting at index from the (unsigned) Integer value. |
|
129 | 584 |
The value must be in the range 0 to 16rFFFFFFFF. |
359 | 585 |
The value is stored MSB-first if msb is true; LSB-first otherwise. |
129 | 586 |
question: should it store signed values ? (see ByteArray signedDoubleWordAt:put:)" |
2 | 587 |
|
588 |
|t| |
|
589 |
||
590 |
%{ /* NOCONTEXT */ |
|
591 |
||
592 |
REGISTER int indx; |
|
593 |
int nIndex; |
|
594 |
int val; |
|
249 | 595 |
OBJ cls; |
2950 | 596 |
unsigned char *byteP; |
2 | 597 |
|
359 | 598 |
if (__isSmallInteger(index)) { |
8908 | 599 |
if (__isSmallInteger(value)) { |
600 |
val = __intVal(value); |
|
601 |
} else { |
|
602 |
val = __longIntVal(value); |
|
603 |
if (val == 0) goto error; |
|
604 |
} |
|
605 |
indx = __intVal(index); |
|
606 |
if (indx > 0) { |
|
607 |
if ((cls = __qClass(self)) != @global(ByteArray)) |
|
608 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
609 |
nIndex = __qSize(self) - OHDR_SIZE; |
|
610 |
if ((indx+3) <= nIndex) { |
|
611 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
612 |
if (msb == true) { |
|
613 |
/* |
|
614 |
* most significant byte first (i.e sparc order) |
|
615 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
616 |
#if defined(__MSBFIRST__) |
8908 | 617 |
if (((INT)byteP & 3) == 0) { |
618 |
((int *)byteP)[0] = val; |
|
619 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
620 |
#endif |
8908 | 621 |
{ |
622 |
byteP[3] = val & 0xFF; |
|
623 |
val >>= 8; |
|
624 |
byteP[2] = val & 0xFF; |
|
625 |
val >>= 8; |
|
626 |
byteP[1] = val & 0xFF; |
|
627 |
val >>= 8; |
|
628 |
byteP[0] = val & 0xFF; |
|
629 |
} |
|
630 |
} else { |
|
631 |
/* |
|
632 |
* least significant byte first (i.e i386/alpha order) |
|
633 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
634 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
8908 | 635 |
((int *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
636 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
637 |
# if defined(__LSBFIRST__) |
8908 | 638 |
if (((unsigned INT)byteP & 3) == 0) { |
639 |
((int *)byteP)[0] = val; |
|
640 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
641 |
# endif |
8908 | 642 |
{ |
643 |
byteP[0] = val & 0xFF; |
|
644 |
val >>= 8; |
|
645 |
byteP[1] = val & 0xFF; |
|
646 |
val >>= 8; |
|
647 |
byteP[2] = val & 0xFF; |
|
648 |
val >>= 8; |
|
649 |
byteP[3] = val & 0xFF; |
|
650 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
651 |
#endif |
8908 | 652 |
} |
653 |
RETURN ( value ); |
|
654 |
} |
|
655 |
} |
|
2 | 656 |
} |
359 | 657 |
error: ; |
658 |
%}. |
|
5439 | 659 |
^ super doubleWordAt:index put:value MSB:msb |
129 | 660 |
|
661 |
" |
|
662 |
|b| |
|
359 | 663 |
b := ByteArray new:8. |
664 |
b doubleWordAt:1 put:16r04030201 MSB:true. |
|
665 |
b doubleWordAt:5 put:16r04030201 MSB:false. |
|
129 | 666 |
b inspect |
667 |
" |
|
5554 | 668 |
! ! |
669 |
||
670 |
!ByteArray methodsFor:'accessing-shorts'! |
|
54 | 671 |
|
563 | 672 |
wordAt:index |
673 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
674 |
The value is retrieved in the machines natural byte order |
|
675 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
676 |
||
677 |
%{ /* NOCONTEXT */ |
|
54 | 678 |
|
563 | 679 |
REGISTER int indx; |
680 |
int nIndex; |
|
681 |
union { |
|
12879 | 682 |
unsigned char u_char[2]; |
683 |
unsigned short u_ushort; |
|
563 | 684 |
} val; |
2950 | 685 |
unsigned char *byteP; |
54 | 686 |
|
563 | 687 |
if (__isSmallInteger(index)) { |
12879 | 688 |
indx = __intVal(index); |
689 |
if (indx > 0) { |
|
690 |
if (!__isByteArrayLike(self)) |
|
691 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
692 |
nIndex = __byteArraySize(self); |
|
693 |
if ((indx+1) <= nIndex) { |
|
694 |
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
|
695 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
12879 | 696 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
697 |
#else |
12879 | 698 |
/* |
699 |
* mhmh to be measured: |
|
700 |
* the if may hurt more than the additional |
|
701 |
* memory cycles on some machines ... |
|
702 |
*/ |
|
703 |
if (((INT)byteP & 1) == 0) { |
|
704 |
/* aligned */ |
|
705 |
val.u_ushort = ((unsigned short *)byteP)[0]; |
|
706 |
} else { |
|
707 |
val.u_char[0] = byteP[0]; |
|
708 |
val.u_char[1] = byteP[1]; |
|
709 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
710 |
#endif |
12879 | 711 |
RETURN ( __mkSmallInteger((val.u_ushort)) ); |
712 |
} |
|
713 |
} |
|
563 | 714 |
} |
715 |
%}. |
|
5439 | 716 |
^ super wordAt:index |
54 | 717 |
! |
718 |
||
563 | 719 |
wordAt:index MSB:msb |
720 |
"return the 2-bytes starting at index as an (unsigned) Integer. |
|
721 |
The value is retrieved MSB (high 8 bits at lower index) if msb is true; |
|
722 |
LSB-first (i.e. low 8-bits at lower byte index) if its false. |
|
723 |
Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)" |
|
724 |
||
725 |
%{ /* NOCONTEXT */ |
|
726 |
||
727 |
REGISTER int indx; |
|
728 |
int nIndex; |
|
729 |
int val; |
|
2950 | 730 |
unsigned char *byteP; |
54 | 731 |
|
563 | 732 |
if (__isSmallInteger(index)) { |
12879 | 733 |
indx = __intVal(index); |
734 |
if (indx > 0) { |
|
735 |
if (!__isByteArrayLike(self)) |
|
736 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
737 |
nIndex = __byteArraySize(self); |
|
738 |
if ((indx+1) <= nIndex) { |
|
739 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
740 |
if (msb == true) { |
|
741 |
/* |
|
742 |
* most significant byte first (i.e sparc order) |
|
743 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
744 |
#if defined(__MSBFIRST__) |
12879 | 745 |
/* |
746 |
* mhmh to be measured: |
|
747 |
* the if may hurt more than the additional |
|
748 |
* memory cycles on some machines ... |
|
749 |
*/ |
|
750 |
if (((INT)byteP & 1) == 0) { |
|
751 |
/* aligned */ |
|
752 |
val = ((unsigned short *)byteP)[0]; |
|
753 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
754 |
#endif |
12879 | 755 |
{ |
756 |
val = byteP[0]; |
|
757 |
val = (val << 8) + byteP[1]; |
|
758 |
} |
|
759 |
} else { |
|
760 |
/* |
|
761 |
* least significant byte first (i.e i386/alpha order) |
|
762 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
763 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
12879 | 764 |
val = ((unsigned short *)byteP)[0]; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
765 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
766 |
# if defined(__LSBFIRST__) |
12879 | 767 |
/* |
768 |
* mhmh to be measured: |
|
769 |
* the if may hurt more than the additional |
|
770 |
* memory cycles on some machines ... |
|
771 |
*/ |
|
772 |
if (((INT)byteP & 1) == 0) { |
|
773 |
/* aligned */ |
|
774 |
val = ((unsigned short *)byteP)[0]; |
|
775 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
776 |
# endif |
12879 | 777 |
{ |
778 |
val = byteP[1]; |
|
779 |
val = (val << 8) + byteP[0]; |
|
780 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
781 |
#endif |
12879 | 782 |
} |
783 |
RETURN ( __mkSmallInteger(val) ); |
|
784 |
} |
|
785 |
} |
|
563 | 786 |
} |
787 |
%}. |
|
5439 | 788 |
^ super wordAt:index MSB:msb |
54 | 789 |
! |
790 |
||
563 | 791 |
wordAt:index put:value |
792 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
8908 | 793 |
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
|
794 |
The value is stored in the machines natural byteorder, |
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
795 |
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
|
796 |
used internally (not passed to other machines). |
563 | 797 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
798 |
||
799 |
%{ /* NOCONTEXT */ |
|
54 | 800 |
|
563 | 801 |
REGISTER int indx; |
802 |
int nIndex; |
|
803 |
int v; |
|
804 |
union { |
|
12879 | 805 |
unsigned char u_char[2]; |
806 |
unsigned short u_ushort; |
|
563 | 807 |
} val; |
2950 | 808 |
unsigned char *byteP; |
129 | 809 |
|
563 | 810 |
if (__bothSmallInteger(index, value)) { |
12879 | 811 |
indx = __intVal(index); |
812 |
if (indx > 0) { |
|
813 |
if (!__isByteArrayLike(self)) |
|
814 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
815 |
nIndex = __byteArraySize(self); |
|
816 |
if ((indx+1) <= nIndex) { |
|
817 |
val.u_ushort = v = __intVal(value); |
|
818 |
if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
819 |
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
|
820 |
#if defined(__i386__) || defined(UNALIGNED_FETCH_OK) |
12879 | 821 |
((unsigned short *)byteP)[0] = val.u_ushort; |
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
822 |
#else |
12879 | 823 |
/* |
824 |
* mhmh to be measured: |
|
825 |
* the if may hurt more than the additional |
|
826 |
* memory cycles on some machines ... |
|
827 |
*/ |
|
828 |
if (((INT)byteP & 1) == 0) { |
|
829 |
/* aligned */ |
|
830 |
((unsigned short *)byteP)[0] = val.u_ushort; |
|
831 |
} else { |
|
832 |
byteP[0] = val.u_char[0]; |
|
833 |
byteP[1] = val.u_char[1]; |
|
834 |
} |
|
3422
09c13ab58bb2
i386 can do unaligned accesses
Claus Gittinger <cg@exept.de>
parents:
3345
diff
changeset
|
835 |
#endif |
12879 | 836 |
RETURN ( value ); |
837 |
} |
|
838 |
} |
|
839 |
} |
|
563 | 840 |
} |
841 |
%}. |
|
5439 | 842 |
^ super wordAt:index put:value |
129 | 843 |
|
844 |
" |
|
563 | 845 |
|b| |
846 |
b := ByteArray new:4. |
|
847 |
b wordAt:1 put:16r0102. |
|
848 |
b wordAt:3 put:16r0304. |
|
8908 | 849 |
b inspect |
129 | 850 |
" |
851 |
! |
|
852 |
||
563 | 853 |
wordAt:index put:value MSB:msb |
854 |
"set the 2-bytes starting at index from the (unsigned) Integer value. |
|
8908 | 855 |
The stored value must be in the range 0 .. 16rFFFF. |
563 | 856 |
The value is stored LSB-first (i.e. the low 8bits are stored at the |
857 |
lower index) if msb is false, MSB-first otherwise. |
|
858 |
Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)" |
|
859 |
||
860 |
%{ /* NOCONTEXT */ |
|
861 |
||
862 |
REGISTER int indx; |
|
863 |
int nIndex; |
|
864 |
int val; |
|
865 |
OBJ cls; |
|
2950 | 866 |
unsigned char *byteP; |
563 | 867 |
|
868 |
if (__bothSmallInteger(index, value)) { |
|
12879 | 869 |
indx = __intVal(index); |
870 |
if (indx > 0) { |
|
871 |
if (!__isByteArrayLike(self)) |
|
872 |
indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
873 |
nIndex = __byteArraySize(self); |
|
874 |
if ((indx+1) <= nIndex) { |
|
875 |
val = __intVal(value); |
|
876 |
if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) { |
|
877 |
byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1; |
|
878 |
if (msb == true) { |
|
879 |
/* |
|
880 |
* most significant byte first (i.e sparc order) |
|
881 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
882 |
#if defined(__MSBFIRST__) |
12879 | 883 |
/* |
884 |
* mhmh to be measured: |
|
885 |
* the if may hurt more than the additional |
|
886 |
* memory cycles on some machines ... |
|
887 |
*/ |
|
888 |
if (((INT)byteP & 1) == 0) { |
|
889 |
/* aligned */ |
|
890 |
((unsigned short *)byteP)[0] = val; |
|
891 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
892 |
#endif |
12879 | 893 |
{ |
894 |
byteP[1] = val & 0xFF; |
|
895 |
byteP[0] = (val>>8) & 0xFF; |
|
896 |
} |
|
897 |
} else { |
|
898 |
/* |
|
899 |
* least significant byte first (i.e i386/alpha order) |
|
900 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
901 |
#if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK)) |
12879 | 902 |
((unsigned short *)byteP)[0] = val; |
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
903 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8623
diff
changeset
|
904 |
# if defined(__LSBFIRST__) |
12879 | 905 |
/* |
906 |
* mhmh to be measured: |
|
907 |
* the if may hurt more than the additional |
|
908 |
* memory cycles on some machines ... |
|
909 |
*/ |
|
910 |
if (((INT)byteP & 1) == 0) { |
|
911 |
/* aligned */ |
|
912 |
((unsigned short *)byteP)[0] = val; |
|
913 |
} else |
|
4274
52fe89c37b2e
tuned wordAt/doubleWordAt & atPut methods
Claus Gittinger <cg@exept.de>
parents:
4269
diff
changeset
|
914 |
# endif |
12879 | 915 |
{ |
916 |
byteP[0] = val & 0xFF; |
|
917 |
byteP[1] = (val>>8) & 0xFF; |
|
918 |
} |
|
3425
f7faef75f0d8
tuned word & doubleWord access for i386
Claus Gittinger <cg@exept.de>
parents:
3422
diff
changeset
|
919 |
#endif |
12879 | 920 |
} |
921 |
RETURN ( value ); |
|
922 |
} |
|
923 |
} |
|
924 |
} |
|
563 | 925 |
} |
926 |
%}. |
|
5439 | 927 |
^ super wordAt:index put:value MSB:msb |
129 | 928 |
|
929 |
" |
|
563 | 930 |
|b| |
931 |
b := ByteArray new:8. |
|
932 |
b wordAt:1 put:16r0102 MSB:false. |
|
933 |
b wordAt:3 put:16r0304 MSB:false. |
|
934 |
b wordAt:5 put:16r0102 MSB:true. |
|
935 |
b wordAt:7 put:16r0304 MSB:true. |
|
8908 | 936 |
b inspect |
129 | 937 |
" |
1 | 938 |
! ! |
939 |
||
11502 | 940 |
|
3345 | 941 |
!ByteArray methodsFor:'comparing'! |
942 |
||
943 |
= aByteArray |
|
944 |
"Compare the receiver with the argument and return true if the |
|
945 |
receiver is equal to the argument (i.e. has the same size and elements). |
|
946 |
Otherwise return false." |
|
947 |
||
948 |
%{ /* NOCONTEXT */ |
|
949 |
||
950 |
int l1, l2; |
|
951 |
REGISTER OBJ s = aByteArray; |
|
952 |
unsigned char *cp1, *cp2; |
|
953 |
OBJ cls; |
|
954 |
OBJ myCls; |
|
955 |
INT addrDelta; |
|
956 |
||
957 |
if (s == self) { |
|
12879 | 958 |
RETURN ( true ); |
3345 | 959 |
} |
960 |
if (! __isNonNilObject(s)) { |
|
12879 | 961 |
RETURN ( false ); |
3345 | 962 |
} |
963 |
||
964 |
cls = __qClass(s); |
|
965 |
myCls = __qClass(self); |
|
966 |
||
967 |
if (cls == myCls) { |
|
12879 | 968 |
l2 = __byteArraySize(s); |
969 |
l1 = __byteArraySize(self); |
|
970 |
if (l1 != l2) { |
|
971 |
RETURN ( false ); |
|
972 |
} |
|
973 |
||
974 |
cp1 = __byteArrayVal(self); |
|
975 |
cp2 = __byteArrayVal(s); |
|
976 |
||
977 |
/* |
|
978 |
* care for instances of subclasses ... |
|
979 |
*/ |
|
980 |
if (cls != ByteArray) { |
|
981 |
int n = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
982 |
||
983 |
cp2 += n; |
|
984 |
cp1 += n; |
|
985 |
l1 -= n; |
|
986 |
} |
|
987 |
||
988 |
addrDelta = cp2 - cp1; |
|
989 |
while (l1 >= (sizeof(unsigned INT) * 4)) { |
|
990 |
if (((unsigned INT *)cp1)[0] != ((unsigned INT *)(cp1+addrDelta))[0]) { |
|
991 |
RETURN (false); |
|
992 |
} |
|
993 |
if (((unsigned INT *)cp1)[1] != ((unsigned INT *)(cp1+addrDelta))[1]) { |
|
994 |
RETURN (false); |
|
995 |
} |
|
996 |
if (((unsigned INT *)cp1)[2] != ((unsigned INT *)(cp1+addrDelta))[2]) { |
|
997 |
RETURN (false); |
|
998 |
} |
|
999 |
if (((unsigned INT *)cp1)[3] != ((unsigned INT *)(cp1+addrDelta))[3]) { |
|
1000 |
RETURN (false); |
|
1001 |
} |
|
1002 |
l1 -= sizeof(unsigned INT)*4; |
|
1003 |
cp1 += sizeof(unsigned INT)*4; |
|
1004 |
} |
|
1005 |
while (l1 >= sizeof(unsigned INT)) { |
|
1006 |
if (*((unsigned INT *)cp1) != *((unsigned INT *)(cp1+addrDelta))) { |
|
1007 |
RETURN (false); |
|
1008 |
} |
|
1009 |
l1 -= sizeof(unsigned INT); |
|
1010 |
cp1 += sizeof(unsigned INT); |
|
1011 |
} |
|
1012 |
if (l1 >= sizeof(unsigned short)) { |
|
1013 |
if (*((unsigned short *)cp1) != *((unsigned short *)(cp1+addrDelta))) { |
|
1014 |
RETURN (false); |
|
1015 |
} |
|
1016 |
l1 -= sizeof(unsigned short); |
|
1017 |
cp1 += sizeof(unsigned short); |
|
1018 |
} |
|
1019 |
while (l1) { |
|
1020 |
if (*cp1 != *(cp1+addrDelta)) { |
|
1021 |
RETURN (false); |
|
1022 |
} |
|
1023 |
l1--; |
|
1024 |
cp1++; |
|
1025 |
} |
|
1026 |
||
1027 |
RETURN (true); |
|
3345 | 1028 |
} |
1029 |
%}. |
|
1030 |
^ super = aByteArray |
|
1031 |
! ! |
|
1032 |
||
12 | 1033 |
!ByteArray methodsFor:'converting'! |
1034 |
||
1035 |
asByteArray |
|
1036 |
"return the receiver as a byteArray" |
|
1037 |
||
1038 |
"could be an instance of a subclass..." |
|
1039 |
self class == ByteArray ifTrue:[ |
|
155 | 1040 |
^ self |
12 | 1041 |
]. |
1042 |
^ super asByteArray |
|
129 | 1043 |
|
1044 |
" |
|
8908 | 1045 |
'hello world' asByteArray |
1046 |
#(1 2 3 4 5 6 7) asByteArray |
|
1047 |
#(1 2 256 4 5 6 7) asByteArray |
|
129 | 1048 |
" |
4778 | 1049 |
! |
1050 |
||
12453
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1051 |
asImmutableByteArray |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1052 |
"return a write-protected copy of myself" |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1053 |
|
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1054 |
^ self copy changeClassTo:ImmutableByteArray |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1055 |
! |
74715f6d4cbe
added: #asImmutableByteArray
Claus Gittinger <cg@exept.de>
parents:
12276
diff
changeset
|
1056 |
|
12276 | 1057 |
asInteger |
1058 |
"convert myself to an integer - the first byte is most significant. |
|
1059 |
This is also in Squeak." |
|
1060 |
||
16795 | 1061 |
^ self asIntegerMSB:true |
12276 | 1062 |
|
1063 |
" |
|
14135 | 1064 |
#[ 2 ] asInteger hexPrintString |
1065 |
#[ 16r1 16r2 ] asInteger hexPrintString |
|
1066 |
#[4 0 0 0 0 0 0 0] asInteger hexPrintString |
|
12276 | 1067 |
" |
1068 |
! |
|
1069 |
||
12621 | 1070 |
asIntegerMSB:isMSBFirst |
1071 |
"convert myself to an integer - the first byte is most significant. |
|
1072 |
This is also in Squeak." |
|
1073 |
||
14135 | 1074 |
^ (LargeInteger digitBytes:self MSB:isMSBFirst) compressed |
12621 | 1075 |
|
1076 |
" |
|
14135 | 1077 |
(#[ 2 ] asIntegerMSB:true) hexPrintString |
1078 |
(#[ 16r1 16r2 ] asIntegerMSB:true) hexPrintString |
|
1079 |
(#[ 16r1 16r2 ] asIntegerMSB:false) hexPrintString |
|
1080 |
(#[4 0 0 0 0 0 0 0] asIntegerMSB:true) hexPrintString |
|
12621 | 1081 |
" |
1082 |
! |
|
1083 |
||
7117 | 1084 |
asPackedString |
1085 |
"ST-80 compatibility: encode the receiver into an ascii String |
|
1086 |
with 6bits encoded per character. Each group of 6 bits is encoded |
|
1087 |
as a corresponding character (32+value) and the resulting string |
|
1088 |
is returned. The resulting string is always a multiple of 4 (since |
|
1089 |
24 is the lcm of 6 and 8) and the number of remaining characters is |
|
1090 |
encoded in the last character. |
|
16799 | 1091 |
ST-80 uses this encoding for Images... |
1092 |
This is a base64 encoding, very similar (but not equal) to the algorithm used in RFC1421. |
|
1093 |
PS: I don't like it ;-) |
|
1094 |
See also: fromPackedString: - the reverse operation" |
|
7117 | 1095 |
|
8908 | 1096 |
|outStream |
7117 | 1097 |
index "{ Class:SmallInteger}" |
1098 |
nextIndex "{ Class:SmallInteger}" |
|
1099 |
stop "{ Class:SmallInteger}" |
|
1100 |
n "{ Class:SmallInteger}" |
|
1101 |
mod "{ Class:SmallInteger}" |
|
1102 |
cpl| |
|
1103 |
||
1104 |
outStream := WriteStream on:String new. |
|
8908 | 1105 |
index := 1. |
7117 | 1106 |
stop := self size. |
1107 |
||
1108 |
stop > 100 ifTrue:[ |
|
16799 | 1109 |
"/ cg: |
1110 |
"/ initial lineBreak |
|
1111 |
outStream cr. |
|
7117 | 1112 |
]. |
1113 |
cpl := 0. |
|
1114 |
||
1115 |
[index <= stop] whileTrue:[ |
|
16799 | 1116 |
"take 3 source bytes" |
1117 |
n := (self at:index) bitShift:16. |
|
1118 |
(index < stop) ifTrue:[ |
|
1119 |
nextIndex := index + 1. |
|
1120 |
n := n bitOr:((self at:nextIndex) bitShift:8). |
|
1121 |
(nextIndex < stop) ifTrue:[ |
|
1122 |
n := n bitOr:(self at:(index + 2)). |
|
1123 |
]. |
|
1124 |
]. |
|
1125 |
index := index + 3. |
|
1126 |
||
1127 |
"took me a while to find that one out ..." |
|
1128 |
n := n bitXor:16r820820. |
|
1129 |
||
1130 |
outStream nextPut:(Character value:((n bitShift:-18) bitAnd:16r3F) + 32). |
|
1131 |
outStream nextPut:(Character value:((n bitShift:-12) bitAnd:16r3F) + 32). |
|
1132 |
outStream nextPut:(Character value:((n bitShift:-6) bitAnd:16r3F) + 32). |
|
1133 |
outStream nextPut:(Character value:(n bitAnd:16r3F) + 32). |
|
1134 |
||
1135 |
"/ cg: |
|
1136 |
"/ lineBreak after every 120 characters |
|
1137 |
"/ fromPackedString will ignore those |
|
1138 |
cpl := cpl + 4. |
|
1139 |
cpl >= 120 ifTrue:[ |
|
1140 |
outStream cr. |
|
1141 |
cpl := 0. |
|
1142 |
]. |
|
7117 | 1143 |
]. |
1144 |
(mod := stop \\ 3) ~~ 0 ifTrue:[ |
|
16799 | 1145 |
outStream backStep. |
1146 |
outStream nextPut:(Character value:(mod + 96)). |
|
7117 | 1147 |
]. |
1148 |
^ outStream contents |
|
1149 |
||
16799 | 1150 |
" |
1151 |
#[ 16r00 16r01 16r02 16r04 16r08 16r10 16r20 16r40 16r80 ] asPackedString |
|
1152 |
" |
|
1153 |
||
7117 | 1154 |
"Modified: 12.11.1996 / 15:45:02 / cg" |
1155 |
! |
|
1156 |
||
5900 | 1157 |
asString |
1158 |
"speed up string conversions" |
|
1159 |
||
1160 |
|size| |
|
1161 |
||
5901 | 1162 |
self class == ByteArray ifTrue:[ |
8908 | 1163 |
size := self size. |
1164 |
^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1. |
|
5901 | 1165 |
]. |
1166 |
^ super asString |
|
5900 | 1167 |
|
1168 |
" |
|
1169 |
#[60 61 62 63] asString |
|
1170 |
" |
|
1171 |
! |
|
1172 |
||
17377 | 1173 |
beImmutable |
1174 |
"make myself write-protected" |
|
1175 |
||
1176 |
self changeClassTo:ImmutableByteArray |
|
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 |
||
17535 | 2608 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
2609 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
16741 | 2610 |
(self class == ByteArray and:[aGCOrStream isStream]) ifTrue:[ |
2611 |
self storeOn:aGCOrStream. |
|
2612 |
^ self |
|
6513
d7bc1d1dab4b
displayString fix for subclasses
Claus Gittinger <cg@exept.de>
parents:
6125
diff
changeset
|
2613 |
]. |
13078
7934a86b395b
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
12932
diff
changeset
|
2614 |
^ super displayOn:aGCOrStream |
563 | 2615 |
|
2616 |
"Created: 25.10.1995 / 13:33:26 / cg" |
|
1244 | 2617 |
"Modified: 22.4.1996 / 12:54:06 / cg" |
563 | 2618 |
! |
2619 |
||
7172 | 2620 |
hexPrintOn:aStream |
7245 | 2621 |
"print as hex string, eg: 'FF0243'. |
2622 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
7172 | 2623 |
|
2624 |
self do:[:byte| |
|
8908 | 2625 |
byte printOn:aStream base:16 size:2 fill:$0. |
7172 | 2626 |
]. |
2627 |
||
2628 |
" |
|
2629 |
#[1 2 3 4 10 17] hexPrintOn:Transcript |
|
2630 |
" |
|
7245 | 2631 |
|
2632 |
" |
|
2633 |
|s| |
|
2634 |
s := String streamContents:[:s | #[1 2 3 4 10 17] hexPrintOn:s]. |
|
8908 | 2635 |
ByteArray fromHexString:s |
7245 | 2636 |
" |
7172 | 2637 |
! |
2638 |
||
9379 | 2639 |
hexPrintOn:aStream withSeparator:aSeparatorStringOrCharacter |
2640 |
"print as hex string, eg: 'FF:02:43'" |
|
2641 |
||
2642 |
self do:[:byte| |
|
12879 | 2643 |
byte printOn:aStream base:16 size:2 fill:$0. |
9379 | 2644 |
] separatedBy:[ |
12879 | 2645 |
aSeparatorStringOrCharacter printOn:aStream |
9379 | 2646 |
]. |
2647 |
||
2648 |
" |
|
2649 |
#[1 2 3 4 10 17] hexPrintOn:Transcript withSeparator:$: |
|
2650 |
" |
|
2651 |
! |
|
2652 |
||
9368 | 2653 |
hexPrintString |
2654 |
"print as hex string, eg: 'FF0243'. |
|
2655 |
This string can be used in #fromHexString: to recreate the byteArray" |
|
2656 |
||
2657 |
|s| |
|
2658 |
||
2659 |
s := '' writeStream. |
|
12932 | 2660 |
self do:[:byteOrCharacter | |
13703 | 2661 |
|byte| |
2662 |
byte := byteOrCharacter isCharacter |
|
2663 |
ifTrue:[byteOrCharacter codePoint] |
|
2664 |
ifFalse:[byteOrCharacter]. |
|
2665 |
byte printOn:s base:16 size:2 fill:$0. |
|
9368 | 2666 |
]. |
2667 |
^ s contents. |
|
2668 |
||
2669 |
" |
|
2670 |
#[1 2 3 4 10 17] hexPrintString |
|
2671 |
" |
|
2672 |
||
2673 |
" |
|
2674 |
ByteArray fromHexString:#[1 2 3 4 10 17] hexPrintString |
|
2675 |
" |
|
12932 | 2676 |
|
2677 |
"Modified: / 03-07-2010 / 01:59:19 / cg" |
|
9368 | 2678 |
! |
2679 |
||
9379 | 2680 |
hexPrintStringWithSeparator:aSeparatorStringOrCharacter |
2681 |
"print as hex string, eg: 'FF:02:43'." |
|
2682 |
||
2683 |
|s| |
|
2684 |
||
2685 |
s := '' writeStream. |
|
2686 |
self hexPrintOn:s withSeparator:aSeparatorStringOrCharacter. |
|
2687 |
^ s contents. |
|
2688 |
||
2689 |
" |
|
2690 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:$: |
|
11860 | 2691 |
#[1 2 3 4 10 17] hexPrintStringWithSeparator:Character space |
9379 | 2692 |
" |
2693 |
! |
|
2694 |
||
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2695 |
printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2696 |
"append a printed representation to aStream" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2697 |
|
12487 | 2698 |
|cls| |
2699 |
||
2700 |
"/ care for subclasses |
|
12879 | 2701 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2702 |
aStream nextPutAll:'#['. |
|
2703 |
self |
|
2704 |
do:[:byte | byte printOn:aStream] |
|
2705 |
separatedBy:[aStream space]. |
|
2706 |
aStream nextPut:$]. |
|
2707 |
^ self |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2708 |
]. |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2709 |
^ super printOn:aStream |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2710 |
|
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 |
#[1 2 3 4 5] printOn:Transcript |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2713 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2714 |
|
8908 | 2715 |
#[1 2 3 4 5] storeString |
2716 |
#[1 2 3 4 5] displayString |
|
2717 |
#[1 2 3 4 5] printString |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2718 |
" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2719 |
|
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2720 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2721 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2722 |
! |
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2723 |
|
4050 | 2724 |
printOn:aStream base:radix |
2725 |
"append a printed representation to aStream in the given number base." |
|
2726 |
||
6125 | 2727 |
^ self printOn:aStream base:radix showRadix:false |
2728 |
||
2729 |
" |
|
2730 |
#[1 2 3 4 5] printOn:Transcript base:2 |
|
2731 |
'Hello World' printOn:Transcript base:2 |
|
2732 |
||
8908 | 2733 |
#[1 2 3 4 5] storeString |
2734 |
#[1 2 3 4 5] displayString |
|
2735 |
#[1 2 3 4 5] printString |
|
6125 | 2736 |
" |
2737 |
||
2738 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
2739 |
"Modified: / 31.10.2001 / 09:43:56 / cg" |
|
2740 |
! |
|
2741 |
||
2742 |
printOn:aStream base:radix showRadix:showRadix |
|
2743 |
"append a printed representation to aStream in the given number base." |
|
2744 |
||
12487 | 2745 |
|cls| |
2746 |
||
2747 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
|
12879 | 2748 |
"/ care for subclasses |
2749 |
aStream nextPutAll:'#['. |
|
2750 |
self |
|
2751 |
do:[:byte | byte printOn:aStream base:radix showRadix:showRadix] |
|
2752 |
separatedBy:[aStream space]. |
|
2753 |
aStream nextPut:$]. |
|
2754 |
^ self |
|
4050 | 2755 |
]. |
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2756 |
^ self printOn:aStream |
4050 | 2757 |
|
2758 |
" |
|
5019
4cd8b02e26e1
Redefine #PrintOn: to print in #[] notation.
Stefan Vogel <sv@exept.de>
parents:
4983
diff
changeset
|
2759 |
#[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
|
2760 |
'Hello World' printOn:Transcript base:2 |
4050 | 2761 |
|
8908 | 2762 |
#[1 2 3 4 5] storeString |
2763 |
#[1 2 3 4 5] displayString |
|
2764 |
#[1 2 3 4 5] printString |
|
4050 | 2765 |
" |
2766 |
||
2767 |
"Modified: / 12.9.1997 / 22:11:33 / cg" |
|
2768 |
"Modified: / 17.3.1999 / 17:01:31 / stefan" |
|
6125 | 2769 |
"Created: / 31.10.2001 / 09:43:41 / cg" |
4050 | 2770 |
! |
2771 |
||
563 | 2772 |
storeOn:aStream |
2773 |
"append a printed representation from which the receiver can be |
|
2774 |
reconstructed to aStream. (reimplemented to make it look better)" |
|
2775 |
||
12487 | 2776 |
|cls| |
2777 |
||
12879 | 2778 |
((cls := self class) == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[ |
2779 |
"/ care for subclasses |
|
2780 |
aStream nextPutAll:'#['. |
|
2781 |
self |
|
2782 |
do:[:byte | byte storeOn:aStream] |
|
2783 |
separatedBy:[aStream space]. |
|
2784 |
aStream nextPutAll:']'. |
|
2785 |
^ self |
|
563 | 2786 |
]. |
2787 |
^ super storeOn:aStream |
|
2788 |
||
2789 |
" |
|
8908 | 2790 |
#[1 2 3 4 5] storeOn:Transcript |
2791 |
||
2792 |
#[1 2 3 4 5] storeString |
|
2793 |
#[1 2 3 4 5] displayString |
|
2794 |
#[1 2 3 4 5] printString |
|
563 | 2795 |
" |
1082 | 2796 |
|
2915 | 2797 |
"Modified: 12.9.1997 / 22:11:33 / cg" |
550 | 2798 |
! ! |
2799 |
||
7822 | 2800 |
!ByteArray methodsFor:'queries'! |
1082 | 2801 |
|
16804 | 2802 |
containsNon7BitAscii |
2803 |
"return true, if any byte in the receiver has the 7th bit on. |
|
2804 |
This my look as a too specific operation to be put here, |
|
2805 |
put it is very helpful for UTF8 string reading (Java class reader), |
|
2806 |
to quickly determine, if UTF8 decoding is needed or not. |
|
2807 |
As most strings in a class fiel are in fact only containing 7bit ascii, |
|
2808 |
this should speedup class file reading considerably" |
|
2809 |
||
16805 | 2810 |
%{ /* NOCONTEXT */ |
16804 | 2811 |
REGISTER unsigned char *cp; |
2812 |
REGISTER unsigned char *endP; |
|
2813 |
||
2814 |
if (__isByteArray(self)) { |
|
2815 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
16807
bc405567df40
Fix typo in previous version (__ByteArraySize)
Stefan Vogel <sv@exept.de>
parents:
16806
diff
changeset
|
2816 |
endP = cp + __byteArraySize(self); |
16806 | 2817 |
#if __POINTER_SIZE__ == 8 |
2818 |
while (cp+8 < endP) { |
|
2819 |
if ( ((unsigned INT *)cp)[0] & 0x8080808080808080) RETURN( true ); |
|
2820 |
cp += 8; |
|
2821 |
} |
|
2822 |
#endif |
|
16804 | 2823 |
while (cp+4 < endP) { |
2824 |
if ( ((unsigned int *)cp)[0] & 0x80808080) RETURN( true ); |
|
2825 |
cp += 4; |
|
2826 |
} |
|
2827 |
while (cp < endP) { |
|
2828 |
if (*cp++ & 0x80) RETURN( true ); |
|
2829 |
} |
|
2830 |
RETURN ( false ); |
|
2831 |
} |
|
2832 |
%} |
|
2833 |
. |
|
2834 |
^ self contains:[:b | b bitTest:16r80]. |
|
2835 |
||
2836 |
" |
|
2837 |
#[1 2 3 1 2 3 1 2 127 ] containsNon7BitAscii |
|
2838 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] containsNon7BitAscii |
|
2839 |
" |
|
2840 |
! |
|
2841 |
||
563 | 2842 |
max |
2843 |
"return the maximum value in the receiver - |
|
8908 | 2844 |
redefined to speedup image processing and sound-player |
563 | 2845 |
(which need a fast method for this on byteArrays)" |
550 | 2846 |
|
563 | 2847 |
%{ /* NOCONTEXT */ |
550 | 2848 |
|
2849 |
REGISTER unsigned char *cp; |
|
563 | 2850 |
REGISTER int index, max; |
2851 |
int len; |
|
550 | 2852 |
|
14135 | 2853 |
if (__isByteArrayLike(self)) { |
2854 |
max = 0; |
|
2855 |
index = 0; |
|
2856 |
len = __qSize(self) - OHDR_SIZE; |
|
2857 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2858 |
||
2859 |
while (++index <= len) { |
|
2860 |
unsigned int byte; |
|
2861 |
||
2862 |
byte = *cp; |
|
2863 |
cp++; |
|
2864 |
if (byte > max) { |
|
2865 |
max = byte; |
|
2866 |
if (byte == 255) break; |
|
2867 |
} |
|
2868 |
} |
|
2869 |
RETURN ( __mkSmallInteger(max) ); |
|
550 | 2870 |
} |
563 | 2871 |
%}. |
2872 |
^ super max |
|
550 | 2873 |
|
2874 |
" |
|
8908 | 2875 |
#[1 2 3 1 2 3 1 2 19] max |
550 | 2876 |
" |
5554 | 2877 |
! |
550 | 2878 |
|
10819 | 2879 |
referencesAny:aCollection |
10821 | 2880 |
"redefined to speed up searching when many byteArray instances are present" |
2881 |
||
10819 | 2882 |
self class == ByteArray ifTrue:[^ false]. |
2883 |
^ super referencesAny:aCollection |
|
2884 |
! |
|
2885 |
||
550 | 2886 |
usageCounts |
2887 |
"return an array filled with value-counts - |
|
2888 |
This is needed in the bitmap/image classes to get info on color usage. |
|
2889 |
(i.e. to build up a histogram of color usage within an image)." |
|
2890 |
||
2891 |
|counts| |
|
2892 |
||
2893 |
counts := Array basicNew:256. |
|
2894 |
||
2895 |
%{ /* STACK: 2000 */ |
|
2896 |
||
2897 |
REGISTER unsigned char *cp; |
|
2898 |
REGISTER int nByte; |
|
2899 |
REGISTER int index; |
|
8913 | 2900 |
INT icounts[256]; |
550 | 2901 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
2902 |
if (__isByteArrayLike(self) && __isArray(counts)) { |
12879 | 2903 |
/* |
2904 |
* zero counts |
|
2905 |
*/ |
|
2906 |
for (index=0; index<256; index++) { |
|
2907 |
icounts[index] = 0; |
|
2908 |
} |
|
2909 |
||
2910 |
/* |
|
2911 |
* count |
|
2912 |
*/ |
|
2913 |
nByte = __qSize(self) - OHDR_SIZE; |
|
2914 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2915 |
while (nByte--) { |
|
2916 |
icounts[*cp++]++; |
|
2917 |
} |
|
2918 |
||
2919 |
/* |
|
2920 |
* make it real counts |
|
2921 |
*/ |
|
2922 |
for (index=0; index<256; index++) { |
|
2923 |
__ArrayInstPtr(counts)->a_element[index] = __mkSmallInteger(icounts[index]); |
|
2924 |
} |
|
2925 |
RETURN ( counts ); |
|
550 | 2926 |
} |
2927 |
%} |
|
2928 |
. |
|
2929 |
self primitiveFailed |
|
2930 |
||
2931 |
" |
|
8908 | 2932 |
#[1 2 3 1 2 3 1 2 250 251 250 251 255] usageCounts |
550 | 2933 |
" |
2934 |
! |
|
2935 |
||
563 | 2936 |
usedValues |
2937 |
"return a new ByteArray with all used values (actually a kind of Set); |
|
8908 | 2938 |
This is needed specially in the bitmap/Imageclasses to find used colors |
2866 | 2939 |
of an image." |
550 | 2940 |
|
563 | 2941 |
%{ /* STACK: 400 */ |
550 | 2942 |
|
2943 |
REGISTER unsigned char *cp; |
|
2866 | 2944 |
unsigned char *endp; |
563 | 2945 |
REGISTER int len; |
2890 | 2946 |
OBJ result; |
2866 | 2947 |
union { |
12879 | 2948 |
unsigned char flags[256]; |
2949 |
int toForceAlignmentOfFlags; |
|
2866 | 2950 |
} f; |
2951 |
||
2952 |
#ifdef TO_BE_MEASURED |
|
2953 |
int coverage = 0; |
|
2954 |
#endif |
|
550 | 2955 |
|
12768
4c56c6dd923d
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12719
diff
changeset
|
2956 |
if (__isByteArrayLike(self)) { |
12879 | 2957 |
memset(f.flags, 0, sizeof(f.flags)); |
2958 |
len = __qSize(self) - OHDR_SIZE; |
|
2959 |
cp = &(__ByteArrayInstPtr(self)->ba_element[0]); |
|
2960 |
||
2961 |
/* for each used byte, set flag */ |
|
2962 |
while (len > 0) { |
|
2866 | 2963 |
#ifdef TO_BE_MEASURED |
12879 | 2964 |
unsigned byte; |
2965 |
||
2966 |
byte = *cp; |
|
2967 |
if (! f.flags[byte]) { |
|
2968 |
f.flags[byte] = 1; |
|
2969 |
coverage++; |
|
2970 |
if (coverage == 256) { |
|
2971 |
/* no need to scan rest */ |
|
2972 |
break; |
|
2973 |
} |
|
2974 |
} |
|
2866 | 2975 |
#else |
12879 | 2976 |
f.flags[*cp] = 1; |
2866 | 2977 |
#endif |
12879 | 2978 |
cp++; |
2979 |
len--; |
|
2980 |
} |
|
2981 |
||
2982 |
/* count 1's */ |
|
2983 |
len = 0; |
|
2984 |
for (cp=f.flags, endp=f.flags+256; cp < endp;) { |
|
2985 |
if ( *((unsigned int *)cp)) { |
|
2986 |
if (cp[0]) len++; |
|
2987 |
if (cp[1]) len++; |
|
2988 |
if (cp[2]) len++; |
|
2989 |
if (cp[3]) len++; |
|
2990 |
} |
|
2991 |
cp += 4; |
|
2992 |
} |
|
2993 |
||
2994 |
/* create ByteArray of used values */ |
|
2995 |
result = __BYTEARRAY_UNINITIALIZED_NEW_INT(len); |
|
2996 |
if (result) { |
|
2997 |
cp = __ByteArrayInstPtr(result)->ba_element; |
|
2998 |
for (len=0; len < 256; len++) { |
|
2999 |
if (f.flags[len]) |
|
3000 |
*cp++ = len; |
|
3001 |
} |
|
3002 |
RETURN ( result ); |
|
3003 |
} |
|
550 | 3004 |
} |
2866 | 3005 |
%}. |
3006 |
^ self asIdentitySet asByteArray |
|
550 | 3007 |
|
3008 |
" |
|
8908 | 3009 |
#[1 2 3 1 2 3 1 2 3 1 2 3 4 5 6 4 5 6] usedValues |
550 | 3010 |
" |
3011 |
! ! |
|
3012 |
||
7822 | 3013 |
!ByteArray methodsFor:'searching'! |
3014 |
||
3015 |
indexOf:aByte startingAt:start |
|
3016 |
"return the index of the first occurrence of the argument, aByte |
|
3017 |
in the receiver starting at start, anInteger; return 0 if not found. |
|
3018 |
- reimplemented here for speed" |
|
3019 |
||
3020 |
%{ /* NOCONTEXT */ |
|
3021 |
||
3022 |
REGISTER unsigned char *cp; |
|
8913 | 3023 |
REGISTER INT index; |
3024 |
REGISTER int byteValue; |
|
7822 | 3025 |
REGISTER int len; |
3026 |
OBJ cls; |
|
3027 |
||
3028 |
if (__isSmallInteger(aByte) &&__isBytes(self)) { |
|
14135 | 3029 |
byteValue = __intVal(aByte); |
3030 |
||
3031 |
if (byteValue & ~0xFF /* i.e. (byteValue < 0) || (byteValue > 255) */) { |
|
3032 |
/* |
|
3033 |
* searching for something which cannot be found |
|
3034 |
*/ |
|
3035 |
RETURN ( __mkSmallInteger(0) ); |
|
3036 |
} |
|
3037 |
||
3038 |
if (__isSmallInteger(start)) { |
|
3039 |
index = __intVal(start); |
|
3040 |
len = __byteArraySize(self); |
|
3041 |
cp = __ByteArrayInstPtr(self)->ba_element; |
|
3042 |
if ((cls = __qClass(self)) != @global(ByteArray)) { |
|
3043 |
int nInst; |
|
3044 |
||
3045 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
3046 |
cp += nInst; |
|
3047 |
len -= nInst; |
|
3048 |
} |
|
3049 |
cp += index - 1; |
|
14382
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3050 |
#ifdef __UNROLL_LOOPS__ |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3051 |
while ((index+4) < len) { |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3052 |
if (cp[0] == byteValue) { RETURN ( __mkSmallInteger(index) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3053 |
if (cp[1] == byteValue) { RETURN ( __mkSmallInteger(index+1) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3054 |
if (cp[2] == byteValue) { RETURN ( __mkSmallInteger(index+2) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3055 |
if (cp[3] == byteValue) { RETURN ( __mkSmallInteger(index+3) ); } |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3056 |
index += 4; |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3057 |
cp += 4; |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3058 |
} |
dcbff20f0a14
changed: #indexOf:startingAt:
Claus Gittinger <cg@exept.de>
parents:
14135
diff
changeset
|
3059 |
#endif |
14135 | 3060 |
while (index <= len) { |
3061 |
if (*cp == byteValue) { |
|
3062 |
RETURN ( __mkSmallInteger(index) ); |
|
3063 |
} |
|
3064 |
index++; |
|
3065 |
cp++; |
|
3066 |
} |
|
3067 |
RETURN ( __mkSmallInteger(0) ); |
|
3068 |
} |
|
7822 | 3069 |
} |
3070 |
%}. |
|
3071 |
^ super indexOf:aByte startingAt:start |
|
3072 |
||
3073 |
" |
|
14135 | 3074 |
#[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5] indexOf:0 startingAt:1 |
7822 | 3075 |
" |
3076 |
! ! |
|
3077 |
||
17535 | 3078 |
|
5554 | 3079 |
!ByteArray methodsFor:'testing'! |
3080 |
||
3081 |
isByteArray |
|
3082 |
"return true, if the receiver is some kind of bytearray; |
|
3083 |
true is returned here - the method is redefined from Object." |
|
3084 |
||
3085 |
^ true |
|
3086 |
||
3087 |
||
3088 |
! |
|
3089 |
||
3090 |
isLiteral |
|
3091 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
3092 |
(i.e. can be used in constant arrays)" |
|
3093 |
||
3094 |
"/ no, simply returning true here is a mistake: |
|
8908 | 3095 |
"/ it could be a subclass of ByteArray |
5554 | 3096 |
"/ (of which the compiler does not know at all ...) |
3097 |
||
3098 |
^ self class == ByteArray |
|
3099 |
||
3100 |
"Modified: 22.4.1996 / 12:55:30 / cg" |
|
3101 |
! ! |
|
3102 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
3103 |
!ByteArray class methodsFor:'documentation'! |
628 | 3104 |
|
3105 |
version |
|
17535 | 3106 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.225 2015-02-20 23:55:55 cg Exp $' |
12091 | 3107 |
! |
3108 |
||
3109 |
version_CVS |
|
17535 | 3110 |
^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.225 2015-02-20 23:55:55 cg Exp $' |
628 | 3111 |
! ! |
15274 | 3112 |