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