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