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