author | Claus Gittinger <cg@exept.de> |
Thu, 07 Dec 1995 22:38:49 +0100 | |
changeset 701 | a309e3ef7faf |
parent 587 | 6b0b960020d5 |
child 809 | 5eef87c2907b |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
214 | 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 |
" |
|
12 |
||
13 |
Integer subclass:#SmallInteger |
|
701 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
68 | 20 |
!SmallInteger class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
214 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
68 | 36 |
documentation |
37 |
" |
|
88 | 38 |
SmallIntegers are Integers in the range of at least +/- 2^30 |
39 |
(i.e. 31 bits, which is not guaranteed; it could be more on 64 bit cpus). |
|
40 |
||
68 | 41 |
These are no real objects - they have no instances (not even storage !!) |
42 |
and cannot be subclassed. |
|
43 |
The reason is to save both storage and runtime by not collecting |
|
44 |
SmallIntegers in the system. SmallInts are marked by having the TAG_INT |
|
88 | 45 |
bit set, in contrast to all other objects which do not. |
46 |
Since this knowledge is hardwired into the system (and there is no |
|
47 |
class-field stored with SmallIntegers) there can be no subclass of |
|
48 |
SmallInteger (sorry). |
|
49 |
||
50 |
If you really need this kind of thing, create a subclass of Integer, |
|
51 |
with an instance variable holding the value. |
|
68 | 52 |
" |
53 |
! ! |
|
1 | 54 |
|
55 |
!SmallInteger class methodsFor:'instance creation'! |
|
56 |
||
57 |
basicNew |
|
58 |
"catch instance creation |
|
59 |
- SmallIntegers cannot be created with new" |
|
60 |
||
61 |
self error:'instances of SmallInteger cannot be created with new' |
|
62 |
! |
|
63 |
||
64 |
basicNew:size |
|
65 |
"catch instance creation |
|
66 |
- SmallIntegers cannot be created with new" |
|
67 |
||
68 |
self error:'instances of SmallInteger cannot be created with new' |
|
69 |
! ! |
|
70 |
||
701 | 71 |
!SmallInteger class methodsFor:'binary storage'! |
72 |
||
73 |
binaryDefinitionFrom: stream manager: manager |
|
74 |
"read the binary representation as stored in storeBinaryOn:" |
|
75 |
||
76 |
| value | |
|
77 |
||
78 |
value := stream next bitAnd: 16r7F. |
|
79 |
value > 16r3F ifTrue: [ |
|
80 |
value := value - 16r80 |
|
81 |
]. |
|
82 |
value := (value bitShift: 8) bitOr: stream next. |
|
83 |
value := (value bitShift: 8) bitOr: stream next. |
|
84 |
value := (value bitShift: 8) bitOr: stream next. |
|
85 |
^ value |
|
86 |
! ! |
|
87 |
||
88 |
!SmallInteger class methodsFor:'bit mask constants'! |
|
89 |
||
90 |
bitMaskFor:index |
|
91 |
"return a bitmask for the index's bit (index starts at 1)" |
|
92 |
||
93 |
(index between:1 and:SmallInteger maxBits) ifFalse:[ |
|
94 |
^ self error:'index out of bounds' |
|
95 |
]. |
|
96 |
^ 1 bitShift:(index - 1) |
|
97 |
! ! |
|
98 |
||
1 | 99 |
!SmallInteger class methodsFor:'constants'! |
100 |
||
101 |
maxBits |
|
68 | 102 |
"return the number of bits in instances of me. |
103 |
For very special uses only - not constant across implementations" |
|
1 | 104 |
|
105 |
%{ /* NOCONTEXT */ |
|
106 |
RETURN ( _MKSMALLINT(N_INT_BITS) ); |
|
107 |
%} |
|
2 | 108 |
|
109 |
"SmallInteger maxBits" |
|
1 | 110 |
! |
111 |
||
112 |
maxBytes |
|
68 | 113 |
"return the number of bytes in instances of me. |
114 |
For very special uses only - not constant across implementations" |
|
1 | 115 |
|
116 |
%{ /* NOCONTEXT */ |
|
117 |
RETURN ( _MKSMALLINT(N_INT_BITS / 8 + 1) ); |
|
118 |
%} |
|
2 | 119 |
|
120 |
"SmallInteger maxBytes" |
|
1 | 121 |
! |
122 |
||
123 |
maxVal |
|
68 | 124 |
"return the largest Integer representable as SmallInteger. |
125 |
For very special uses only - not constant across implementations" |
|
1 | 126 |
|
127 |
%{ /* NOCONTEXT */ |
|
128 |
RETURN ( _MKSMALLINT(_MAX_INT) ); |
|
129 |
%} |
|
2 | 130 |
|
131 |
"SmallInteger maxVal" |
|
701 | 132 |
! |
133 |
||
134 |
minVal |
|
135 |
"return the smallest Integer representable as SmallInteger. |
|
136 |
For very special uses only - not constant across implementations" |
|
137 |
||
138 |
%{ /* NOCONTEXT */ |
|
139 |
RETURN ( _MKSMALLINT(_MIN_INT) ); |
|
140 |
%} |
|
141 |
||
142 |
"SmallInteger minVal" |
|
1 | 143 |
! ! |
144 |
||
3 | 145 |
!SmallInteger class methodsFor:'queries'! |
146 |
||
50 | 147 |
canBeSubclassed |
148 |
"return true, if its allowed to create subclasses of the receiver. |
|
68 | 149 |
Return nil here - since it is NOT possible for SmallInteger" |
50 | 150 |
|
151 |
^ false |
|
1 | 152 |
! |
153 |
||
701 | 154 |
isBuiltInClass |
155 |
"this class is known by the run-time-system" |
|
1 | 156 |
|
157 |
^ true |
|
158 |
! ! |
|
159 |
||
160 |
!SmallInteger methodsFor:'arithmetic'! |
|
161 |
||
162 |
* aNumber |
|
163 |
"return the product of the receivers value and the arguments value" |
|
164 |
||
165 |
%{ /* NOCONTEXT */ |
|
166 |
||
68 | 167 |
INT myValue, otherValue; |
168 |
unsigned INT productLow, productHi; |
|
169 |
int negative; |
|
170 |
||
95 | 171 |
# define lowBits(foo) ((foo) & 0xFFFF) |
172 |
# define hiBits(foo) ((foo) >> 16) |
|
173 |
||
174 |
/* |
|
175 |
* can we use long long arithmetic ? |
|
176 |
*/ |
|
177 |
#if defined(__GNUC__) && (__GNUC__ >= 2) |
|
178 |
/* |
|
179 |
* commented, since long-long arithmetic seems to |
|
180 |
* be buggy in some implementations (sparc) ... |
|
181 |
* (took me a while to find this out :-( |
|
182 |
*/ |
|
183 |
# ifdef NOTDEF |
|
184 |
# define _LONGLONG |
|
185 |
# endif |
|
186 |
#endif |
|
1 | 187 |
|
252 | 188 |
if (__isSmallInteger(aNumber)) { |
214 | 189 |
myValue = _intVal(self); |
190 |
otherValue = _intVal(aNumber); |
|
95 | 191 |
#if defined(_LONGLONG) |
214 | 192 |
{ |
193 |
long long product; |
|
13 | 194 |
|
214 | 195 |
product = (long long)myValue * (long long)otherValue; |
196 |
if ((product >= (long long)_MIN_INT) |
|
197 |
&& (product <= (long long)_MAX_INT)) { |
|
198 |
RETURN ( _MKSMALLINT((int)product) ); |
|
199 |
} |
|
200 |
if (product < 0) { |
|
201 |
negative = 1; |
|
202 |
product = -product; |
|
203 |
} else { |
|
204 |
negative = 0; |
|
205 |
} |
|
206 |
productHi = product >> 32; |
|
207 |
productLow = product & 0xFFFFFFFF; |
|
208 |
} |
|
13 | 209 |
#else |
214 | 210 |
negative = 0; |
211 |
if (myValue < 0) { |
|
212 |
negative = 1; |
|
213 |
myValue = -myValue; |
|
214 |
} |
|
215 |
if (otherValue < 0) { |
|
216 |
negative = (1 - negative); |
|
217 |
otherValue = -otherValue; |
|
218 |
} |
|
68 | 219 |
|
220 |
# if defined(__GNUC__) && defined(mc68k) |
|
214 | 221 |
asm ("mulu%.l %3,%1:%0" |
222 |
: "=d" ((unsigned long)(productLow)), |
|
223 |
"=d" ((unsigned long)(productHi)) |
|
224 |
: "%0" ((unsigned long)(myValue)), |
|
225 |
"dmi" ((unsigned long)(otherValue))); |
|
68 | 226 |
# else |
227 |
# if defined (__GNUC__) && defined(i386) |
|
214 | 228 |
asm ("mull %3" |
229 |
: "=a" ((unsigned long)(productLow)), |
|
230 |
"=d" ((unsigned long)(productHi)) |
|
231 |
: "%0" ((unsigned long)(myValue)), |
|
232 |
"rm" ((unsigned long)(otherValue))); |
|
68 | 233 |
# else |
214 | 234 |
{ |
235 |
unsigned INT pHH, pHL, pLH, pLL; |
|
236 |
unsigned INT low1, low2, hi1, hi2; |
|
237 |
unsigned INT t; |
|
68 | 238 |
|
214 | 239 |
/* unsigned multiply myValue * otherValue -> productHi, productLow |
240 |
* |
|
241 |
* this is too slow: |
|
242 |
* since most machines can do 32*32 to 64 bit multiply, |
|
243 |
* (or at least 32*32 with Overflow check) |
|
244 |
* - need more assembler (inline) functions here |
|
245 |
*/ |
|
246 |
low1 = lowBits(myValue); |
|
247 |
hi1 = hiBits(myValue); |
|
248 |
low2 = lowBits(otherValue); |
|
249 |
hi2 = hiBits(otherValue); |
|
68 | 250 |
|
214 | 251 |
pLH = low1 * hi2; |
252 |
pHL = hi1 * low2; |
|
253 |
pLL = low1 * low2; |
|
68 | 254 |
|
214 | 255 |
/* |
256 |
* the common case ... |
|
257 |
*/ |
|
258 |
if ((pHL == 0) |
|
259 |
&& (pLH == 0) |
|
260 |
&& ((pLL & 0xC0000000) == 0)) { |
|
261 |
if (negative) { |
|
262 |
RETURN ( _MKSMALLINT(- ((INT)pLL)) ); |
|
263 |
} |
|
264 |
RETURN ( _MKSMALLINT((INT)pLL) ); |
|
265 |
} |
|
68 | 266 |
|
214 | 267 |
pHH = hi1 * hi2; |
68 | 268 |
|
214 | 269 |
/* |
270 |
* pHH |--------|--------| |
|
271 |
* pLH |--------|--------| |
|
272 |
* pHL |--------|--------| |
|
273 |
* pLL |--------|--------| |
|
274 |
*/ |
|
68 | 275 |
|
214 | 276 |
t = lowBits(pLH) + lowBits(pHL) + hiBits(pLL); |
277 |
productLow = (t << 16) + lowBits(pLL); |
|
278 |
productHi = pHH + hiBits(t) + hiBits(pHL) + hiBits(pLH); |
|
279 |
} |
|
68 | 280 |
# endif |
281 |
# endif |
|
214 | 282 |
if (productHi == 0) { |
283 |
if (negative) { |
|
284 |
if (productLow <= -(_MIN_INT)) { |
|
285 |
RETURN ( _MKSMALLINT(-((INT)productLow)) ); |
|
286 |
} |
|
287 |
} else { |
|
288 |
if (productLow <= _MAX_INT) { |
|
289 |
RETURN ( _MKSMALLINT(productLow) ); |
|
290 |
} |
|
291 |
} |
|
292 |
} |
|
95 | 293 |
#endif |
294 |
||
214 | 295 |
{ |
296 |
extern OBJ LargeInteger; |
|
297 |
static struct inlineCache val = _ILC5; |
|
298 |
OBJ aLarge; |
|
68 | 299 |
|
214 | 300 |
aLarge = (*val.ilc_func)(LargeInteger, |
301 |
@symbol(sign:value16:value16:value16:value16:), |
|
302 |
CON_COMMA nil, &val, |
|
303 |
negative ? _MKSMALLINT(-1) : _MKSMALLINT(1), |
|
304 |
_MKSMALLINT(lowBits(productLow)), |
|
305 |
_MKSMALLINT(hiBits(productLow)), |
|
306 |
_MKSMALLINT(lowBits(productHi)), |
|
307 |
_MKSMALLINT(hiBits(productHi)) ); |
|
308 |
RETURN(aLarge); |
|
309 |
} |
|
283 | 310 |
} else if (__isFloatLike(aNumber)) { |
214 | 311 |
OBJ newFloat; |
533
c75af3681d0f
attention qMKFLOAT macro may perform a GC - dont use floatVal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
312 |
double val = (double)_intVal(self) * _floatVal(aNumber); |
1 | 313 |
|
533
c75af3681d0f
attention qMKFLOAT macro may perform a GC - dont use floatVal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
314 |
_qMKFLOAT(newFloat, val, SENDER); |
214 | 315 |
RETURN ( newFloat ); |
1 | 316 |
} |
317 |
%} |
|
318 |
. |
|
319 |
^ aNumber productFromInteger:self |
|
320 |
! |
|
321 |
||
701 | 322 |
+ aNumber |
323 |
"return the sum of the receivers value and the arguments value" |
|
324 |
||
325 |
%{ /* NOCONTEXT */ |
|
326 |
||
327 |
if (__isSmallInteger(aNumber)) { |
|
328 |
#ifdef _ADD_IO_IO |
|
329 |
RETURN ( _ADD_IO_IO(self, aNumber) ); |
|
330 |
#else |
|
331 |
REGISTER INT sum; |
|
332 |
extern OBJ _MKLARGEINT(); |
|
333 |
||
334 |
sum = _intVal(self) + _intVal(aNumber); |
|
335 |
if ((sum >= _MIN_INT) && (sum <= _MAX_INT)) { |
|
336 |
RETURN ( _MKSMALLINT(sum) ); |
|
337 |
} |
|
338 |
RETURN ( _MKLARGEINT(sum) ); |
|
339 |
#endif |
|
340 |
} |
|
341 |
if (__isFloatLike(aNumber)) { |
|
342 |
OBJ newFloat; |
|
343 |
double val = (double)_intVal(self) + _floatVal(aNumber); |
|
344 |
||
345 |
_qMKFLOAT(newFloat, val, SENDER); |
|
346 |
RETURN ( newFloat ); |
|
347 |
} |
|
348 |
%} |
|
349 |
. |
|
350 |
^ aNumber sumFromInteger:self |
|
351 |
! |
|
352 |
||
353 |
- aNumber |
|
354 |
"return the difference of the receivers value and the arguments value" |
|
355 |
||
356 |
%{ /* NOCONTEXT */ |
|
357 |
||
358 |
if (__isSmallInteger(aNumber)) { |
|
359 |
#ifdef _SUB_IO_IO |
|
360 |
RETURN ( _SUB_IO_IO(self, aNumber) ); |
|
361 |
#else |
|
362 |
REGISTER INT diff; |
|
363 |
extern OBJ _MKLARGEINT(); |
|
364 |
||
365 |
diff = _intVal(self) - _intVal(aNumber); |
|
366 |
if ((diff >= _MIN_INT) && (diff <= _MAX_INT)) { |
|
367 |
RETURN ( _MKSMALLINT(diff) ); |
|
368 |
} |
|
369 |
RETURN ( _MKLARGEINT(diff) ); |
|
370 |
#endif |
|
371 |
} |
|
372 |
if (__isFloatLike(aNumber)) { |
|
373 |
OBJ newFloat; |
|
374 |
double val = (double)_intVal(self) - _floatVal(aNumber); |
|
375 |
||
376 |
_qMKFLOAT(newFloat, val, SENDER); |
|
377 |
RETURN ( newFloat ); |
|
378 |
} |
|
379 |
%} |
|
380 |
. |
|
381 |
^ aNumber differenceFromInteger:self |
|
382 |
! |
|
383 |
||
1 | 384 |
/ aNumber |
385 |
"return the quotient of the receivers value and the arguments value" |
|
386 |
||
387 |
%{ /* NOCONTEXT */ |
|
388 |
||
389 |
INT me, t, val; |
|
390 |
double dval; |
|
391 |
||
252 | 392 |
if (__isSmallInteger(aNumber)) { |
214 | 393 |
val = _intVal(aNumber); |
394 |
if (val != 0) { |
|
395 |
me = _intVal(self); |
|
396 |
t = me / val; |
|
1 | 397 |
#ifdef GOOD_OPTIMIZER |
214 | 398 |
if (me % val) { |
1 | 399 |
#else |
214 | 400 |
/* this is stupid - all I want is to look for a remainder ... |
401 |
but most compilers are too stupid and generate an extra modulu |
|
402 |
instruction for "if (me % val)". |
|
403 |
Even if most divide instructions already leave the remainder in |
|
404 |
some register. |
|
405 |
Therefore I use a multiplication which is faster than a modulu |
|
406 |
on most machines. Hint to GNU people :-) |
|
407 |
*/ |
|
408 |
if ((t * val) == me) { |
|
1 | 409 |
#endif |
214 | 410 |
RETURN ( _MKSMALLINT(t) ); |
411 |
} |
|
412 |
} |
|
1 | 413 |
} else { |
283 | 414 |
if (__isFloatLike(aNumber)) { |
214 | 415 |
dval = _floatVal(aNumber); |
416 |
if (dval != 0.0) { |
|
417 |
OBJ newFloat; |
|
533
c75af3681d0f
attention qMKFLOAT macro may perform a GC - dont use floatVal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
418 |
double val = (double)_intVal(self) / dval; |
62 | 419 |
|
533
c75af3681d0f
attention qMKFLOAT macro may perform a GC - dont use floatVal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
420 |
_qMKFLOAT(newFloat, val, SENDER); |
214 | 421 |
RETURN ( newFloat ); |
422 |
} |
|
423 |
} |
|
1 | 424 |
} |
425 |
%} |
|
426 |
. |
|
427 |
aNumber isInteger ifTrue:[ |
|
324 | 428 |
aNumber == 0 ifTrue:[ |
214 | 429 |
^ DivisionByZeroSignal raise. |
430 |
]. |
|
431 |
^ Fraction numerator:self denominator:aNumber |
|
1 | 432 |
]. |
433 |
^ aNumber quotientFromInteger:self |
|
434 |
! |
|
435 |
||
2 | 436 |
// aNumber |
1 | 437 |
"return the integer part of the quotient of the receivers value |
438 |
and the arguments value" |
|
439 |
||
440 |
%{ /* NOCONTEXT */ |
|
441 |
INT val; |
|
442 |
||
252 | 443 |
if (__isSmallInteger(aNumber)) { |
214 | 444 |
val = _intVal(aNumber); |
445 |
if (val != 0) { |
|
446 |
RETURN ( _MKSMALLINT(_intVal(self) / val) ); |
|
447 |
} |
|
2 | 448 |
} else { |
214 | 449 |
if (__isFraction(aNumber)) { |
450 |
OBJ t; |
|
451 |
INT num, den; |
|
2 | 452 |
|
214 | 453 |
t = _FractionInstPtr(aNumber)->f_numerator; |
252 | 454 |
if (__isSmallInteger(t)) { |
214 | 455 |
num = _intVal(t); |
456 |
t = _FractionInstPtr(aNumber)->f_denominator; |
|
252 | 457 |
if (__isSmallInteger(t)) { |
214 | 458 |
den = _intVal(t); |
459 |
RETURN ( _MKSMALLINT(_intVal(self) * den / num )); |
|
460 |
} |
|
461 |
} |
|
462 |
} |
|
1 | 463 |
} |
464 |
%} |
|
465 |
. |
|
2 | 466 |
(aNumber = 0) ifTrue:[ |
214 | 467 |
^ DivisionByZeroSignal raise. |
1 | 468 |
]. |
2 | 469 |
^ self retry:#// coercing:aNumber |
1 | 470 |
! |
471 |
||
2 | 472 |
\\ aNumber |
1 | 473 |
"return the integer rest of the receivers value |
474 |
divided by the arguments value" |
|
475 |
||
476 |
%{ /* NOCONTEXT */ |
|
477 |
INT mySelf, val; |
|
478 |
||
252 | 479 |
if (__isSmallInteger(aNumber)) { |
214 | 480 |
mySelf = _intVal(self); |
481 |
if (mySelf < 0) mySelf = -mySelf; |
|
482 |
val = _intVal(aNumber); |
|
483 |
if (val != 0) { |
|
484 |
if (val < 0) { |
|
485 |
RETURN ( _MKSMALLINT(-(mySelf % -val)) ); |
|
486 |
} |
|
487 |
RETURN ( _MKSMALLINT(mySelf % val) ); |
|
488 |
} |
|
1 | 489 |
} |
490 |
%} |
|
491 |
. |
|
2 | 492 |
(aNumber = 0) ifTrue:[ |
214 | 493 |
^ DivisionByZeroSignal raise. |
1 | 494 |
]. |
2 | 495 |
^ self retry:#\\ coercing:aNumber |
1 | 496 |
! |
497 |
||
498 |
abs |
|
499 |
"return the absolute value of the receiver |
|
500 |
reimplemented here for speed" |
|
501 |
||
502 |
%{ /* NOCONTEXT */ |
|
503 |
||
504 |
INT val = _intVal(self); |
|
505 |
||
62 | 506 |
if (val >= 0) { |
214 | 507 |
RETURN (self); |
62 | 508 |
} |
1 | 509 |
if (val != _MIN_INT) { |
214 | 510 |
RETURN ( _MKSMALLINT(-val) ); |
1 | 511 |
} |
324 | 512 |
%}. |
1 | 513 |
"only reached for minVal" |
514 |
^ self negated |
|
515 |
! |
|
516 |
||
517 |
negated |
|
518 |
"return the negative value of the receiver |
|
519 |
reimplemented here for speed" |
|
520 |
||
521 |
%{ /* NOCONTEXT */ |
|
522 |
||
523 |
INT val = _intVal(self); |
|
524 |
||
525 |
if (val != _MIN_INT) { |
|
214 | 526 |
RETURN ( _MKSMALLINT(- val) ); |
1 | 527 |
} |
324 | 528 |
%}. |
529 |
"only reached for minVal" |
|
1 | 530 |
^ (LargeInteger value:(SmallInteger maxVal)) + 1 |
531 |
! ! |
|
532 |
||
701 | 533 |
!SmallInteger methodsFor:'binary storage'! |
1 | 534 |
|
701 | 535 |
hasSpecialBinaryRepresentation |
536 |
"return true, if the receiver has a special binary representation" |
|
1 | 537 |
|
701 | 538 |
^ true |
1 | 539 |
! |
540 |
||
701 | 541 |
storeBinaryOn: stream manager: manager |
542 |
"append a binary representation onto stream. |
|
543 |
Redefined since SmallIntegers are stored as their value with the 32nd bit |
|
544 |
set as a tag. |
|
545 |
To make the binary file a bit more compact, zeros and single byte ints |
|
546 |
are stored with a more compact representation (using special type-codes)." |
|
62 | 547 |
|
701 | 548 |
self == 0 ifTrue:[ |
549 |
stream nextPut: manager codeForZero. |
|
550 |
^ self |
|
1 | 551 |
]. |
701 | 552 |
(self between:0 and:255) ifTrue:[ |
553 |
stream nextPut: manager codeForByteInteger. |
|
554 |
stream nextPut: self. |
|
555 |
^ self |
|
556 |
]. |
|
557 |
stream nextPut: (((self bitShift: -24) bitAnd: 16rFF) bitOr: 16r80). |
|
558 |
stream nextPut: ((self bitShift: -16) bitAnd: 16rFF). |
|
559 |
stream nextPut: ((self bitShift: -8) bitAnd: 16rFF). |
|
560 |
stream nextPut: (self bitAnd: 16rFF) |
|
1 | 561 |
! ! |
562 |
||
563 |
!SmallInteger methodsFor:'bit operators'! |
|
564 |
||
565 |
allMask:anInteger |
|
62 | 566 |
"return true if all 1-bits in anInteger are also 1 in the receiver" |
567 |
||
568 |
^ (self bitAnd:anInteger) == anInteger |
|
1 | 569 |
|
62 | 570 |
"2r00001111 allMask:2r00000001" |
571 |
"2r00001111 allMask:2r00011110" |
|
572 |
"2r00001111 allMask:2r00000000" |
|
1 | 573 |
! |
574 |
||
575 |
anyMask:anInteger |
|
62 | 576 |
"return true if any 1-bits in anInteger is also 1 in the receiver. |
577 |
(somewhat incorrect, if the mask is zero)" |
|
1 | 578 |
|
62 | 579 |
^ (self bitAnd:anInteger) ~~ 0 |
580 |
||
581 |
"2r00001111 anyMask:2r00000001" |
|
582 |
"2r00001111 anyMask:2r11110000" |
|
1 | 583 |
! |
584 |
||
701 | 585 |
bitAnd:anInteger |
586 |
"return the bitwise-and of the receiver and the argument, anInteger" |
|
1 | 587 |
|
588 |
%{ /* NOCONTEXT */ |
|
589 |
||
701 | 590 |
/* anding the tags doesn't change it */ |
591 |
if (__isSmallInteger(anInteger)) { |
|
592 |
RETURN ( ((OBJ) ((INT)self & (INT)anInteger)) ); |
|
1 | 593 |
} |
594 |
%} |
|
701 | 595 |
. |
596 |
^ self retry:#bitAnd coercing:anInteger |
|
597 |
||
598 |
"(2r001010100 bitAnd:2r00001111) radixPrintStringRadix:2" |
|
1 | 599 |
! |
600 |
||
701 | 601 |
bitAt:index |
602 |
"return the value of the index's bit (index starts at 1). |
|
603 |
Notice: the result of bitAt: on negative receivers is not |
|
604 |
defined in the language standard (since the implementation |
|
605 |
is free to choose any internal representation for integers)" |
|
606 |
||
607 |
|mask| |
|
608 |
||
609 |
(index between:1 and:SmallInteger maxBits) ifFalse:[ |
|
610 |
^ self error:'index out of bounds' |
|
611 |
]. |
|
612 |
mask := 1 bitShift:(index - 1). |
|
613 |
((self bitAnd:mask) == 0) ifTrue:[^ 0]. |
|
614 |
^ 1 |
|
615 |
! |
|
616 |
||
617 |
bitInvert |
|
618 |
"return the value of the receiver with all bits inverted" |
|
62 | 619 |
|
1 | 620 |
%{ /* NOCONTEXT */ |
621 |
||
701 | 622 |
/* invert anything except tag bits */ |
623 |
RETURN ( ((OBJ) ((INT)self ^ ~TAG_MASK)) ); |
|
624 |
%} |
|
625 |
! |
|
1 | 626 |
|
701 | 627 |
bitOr:anInteger |
628 |
"return the bitwise-or of the receiver and the argument, anInteger" |
|
629 |
||
630 |
%{ /* NOCONTEXT */ |
|
631 |
||
632 |
/* oring the tags doesn't change it */ |
|
633 |
if (__isSmallInteger(anInteger)) { |
|
634 |
RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) ); |
|
1 | 635 |
} |
636 |
%} |
|
701 | 637 |
. |
638 |
^ self retry:#bitOr coercing:anInteger |
|
639 |
||
640 |
"(2r000000100 bitOr:2r00000011) radixPrintStringRadix:2" |
|
1 | 641 |
! |
642 |
||
643 |
bitShift:shiftCount |
|
644 |
"return the value of the receiver shifted by shiftCount bits; |
|
62 | 645 |
leftShift if shiftCount > 0; rightShift otherwise. |
95 | 646 |
Notice: the result of bitShift: on negative receivers is not |
214 | 647 |
defined in the language standard (since the implementation |
648 |
is free to choose any internal representation for integers)" |
|
1 | 649 |
|
650 |
%{ /* NOCONTEXT */ |
|
651 |
||
652 |
INT bits, count; |
|
653 |
||
252 | 654 |
if (__isSmallInteger(shiftCount)) { |
214 | 655 |
count = _intVal(shiftCount); |
656 |
bits = _intVal(self); |
|
657 |
if (count > 0) { |
|
95 | 658 |
#if defined(_LONGLONG) |
214 | 659 |
unsigned long long result; |
95 | 660 |
|
214 | 661 |
result = bits; |
662 |
if (count <= N_INT_BITS) { |
|
663 |
result <<= count; |
|
664 |
if (result <= _MAX_INT) { |
|
665 |
RETURN ( _MKSMALLINT(result) ); |
|
666 |
} |
|
667 |
{ |
|
668 |
extern OBJ LargeInteger; |
|
669 |
static struct inlineCache val = _ILC5; |
|
670 |
OBJ aLarge; |
|
95 | 671 |
|
214 | 672 |
aLarge = (*val.ilc_func)(LargeInteger, |
673 |
@symbol(sign:value16:value16:value16:value16:), |
|
674 |
CON_COMMA nil, &val, |
|
675 |
_MKSMALLINT(1), |
|
676 |
_MKSMALLINT(lowBits(result)), |
|
677 |
_MKSMALLINT(hiBits(result)), |
|
678 |
_MKSMALLINT(lowBits(result >> 32)), |
|
679 |
_MKSMALLINT(hiBits(result >> 32)) ); |
|
680 |
RETURN(aLarge); |
|
681 |
} |
|
682 |
} |
|
95 | 683 |
#else |
214 | 684 |
/* |
685 |
* check for overflow |
|
686 |
*/ |
|
687 |
if (count < (N_INT_BITS-1)) { |
|
688 |
if (! (bits >> (N_INT_BITS - 1 - count))) { |
|
689 |
RETURN ( _MKSMALLINT(bits << count) ); |
|
690 |
} |
|
691 |
/* |
|
692 |
* so, there is an overflow ... |
|
693 |
* handle it as largeInteger |
|
694 |
*/ |
|
695 |
/* FALL THROUGH */ |
|
696 |
} |
|
95 | 697 |
#endif |
214 | 698 |
} else { |
699 |
/* |
|
700 |
* right shifts cannot overflow |
|
701 |
*/ |
|
702 |
if (count < 0) { |
|
703 |
/* |
|
704 |
* some machines ignore shifts bigger than |
|
705 |
* the number of bits in an int ... |
|
706 |
*/ |
|
707 |
if (count < (-N_INT_BITS-1)) |
|
708 |
RETURN (_MKSMALLINT(0)); |
|
709 |
RETURN ( _MKSMALLINT(bits >> -count) ); |
|
710 |
} |
|
711 |
RETURN (self ); |
|
712 |
} |
|
1 | 713 |
} |
85 | 714 |
%}. |
715 |
(shiftCount isMemberOf:SmallInteger) ifTrue:[ |
|
214 | 716 |
^ (LargeInteger value:self) bitShift:shiftCount |
85 | 717 |
]. |
1 | 718 |
^ self bitShift:(shiftCount coerce:1) |
719 |
! |
|
720 |
||
701 | 721 |
bitTest:aMask |
722 |
"return true, if any bit from aMask is set in the receiver" |
|
1 | 723 |
|
724 |
%{ /* NOCONTEXT */ |
|
725 |
||
701 | 726 |
/* and all bits except tag */ |
727 |
if (__isSmallInteger(aMask)) { |
|
728 |
RETURN ( ((INT)self & ((INT)aMask & ~TAG_MASK)) ? true : false ); |
|
1 | 729 |
} |
730 |
%} |
|
731 |
. |
|
701 | 732 |
^ self retry:#bitTest coercing:aMask |
1 | 733 |
! |
734 |
||
735 |
bitXor:anInteger |
|
13 | 736 |
"return the bitwise-exclusive-or of the receiver and the argument, anInteger" |
1 | 737 |
|
738 |
%{ /* NOCONTEXT */ |
|
739 |
||
740 |
/* xoring the tags turns it off - or it in again */ |
|
252 | 741 |
if (__isSmallInteger(anInteger)) { |
214 | 742 |
RETURN ( (OBJ)( ((INT)self ^ (INT)anInteger) | TAG_INT) ); |
1 | 743 |
} |
744 |
%} |
|
745 |
. |
|
746 |
^ self retry:#bitXor coercing:anInteger |
|
747 |
! |
|
748 |
||
701 | 749 |
highBit |
750 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
751 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
1 | 752 |
|
753 |
%{ /* NOCONTEXT */ |
|
754 |
||
701 | 755 |
INT mask, index, bits; |
756 |
||
757 |
bits = _intVal(self); |
|
758 |
if (bits == 0) { |
|
759 |
RETURN ( _MKSMALLINT(-1) ); |
|
760 |
} |
|
761 |
#ifdef alpha |
|
762 |
mask = 0x2000000000000000; |
|
763 |
index = 62; |
|
764 |
#else |
|
765 |
mask = 0x20000000; |
|
766 |
index = 30; |
|
767 |
#endif |
|
768 |
while (index) { |
|
769 |
if (bits & mask) break; |
|
770 |
mask = mask >> 1; |
|
771 |
index--; |
|
1 | 772 |
} |
701 | 773 |
RETURN ( _MKSMALLINT(index) ); |
1 | 774 |
%} |
701 | 775 |
"2r000100 highBit" |
776 |
"2r010100 highBit" |
|
777 |
"2r000001 highBit" |
|
778 |
"0 highBit" |
|
779 |
"SmallInteger maxVal highBit" |
|
780 |
! |
|
781 |
||
782 |
lowBit |
|
783 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
784 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
785 |
||
786 |
%{ /* NOCONTEXT */ |
|
787 |
||
788 |
INT mask, index, bits; |
|
789 |
||
790 |
bits = _intVal(self); |
|
791 |
if (bits == 0) { |
|
792 |
RETURN ( _MKSMALLINT(-1) ); |
|
793 |
} |
|
794 |
mask = 1; |
|
795 |
index = 1; |
|
796 |
#ifdef alpha |
|
797 |
while (index != 63) { |
|
798 |
#else |
|
799 |
while (index != 31) { |
|
800 |
#endif |
|
801 |
if (bits & mask) { |
|
802 |
RETURN ( _MKSMALLINT(index) ); |
|
803 |
} |
|
804 |
mask = mask << 1; |
|
805 |
index++; |
|
806 |
} |
|
807 |
RETURN ( _MKSMALLINT(-1) ); |
|
808 |
%} |
|
809 |
"2r000100 lowBit" |
|
810 |
"2r010010 lowBit" |
|
811 |
"2r100001 lowBit" |
|
812 |
"0 lowBit" |
|
813 |
! |
|
814 |
||
815 |
noMask:anInteger |
|
816 |
"return true if no 1-bit in anInteger is 1 in the receiver" |
|
817 |
||
818 |
^ (self bitAnd:anInteger) == 0 |
|
819 |
||
820 |
"2r00001111 noMask:2r00000001" |
|
821 |
"2r00001111 noMask:2r11110000" |
|
1 | 822 |
! ! |
823 |
||
824 |
!SmallInteger methodsFor:'byte access'! |
|
825 |
||
826 |
digitAt:index |
|
827 |
"return 8 bits of value, starting at byte index" |
|
828 |
||
829 |
%{ /* NOCONTEXT */ |
|
830 |
||
357 | 831 |
REGISTER INT val; |
832 |
INT idx; |
|
1 | 833 |
|
252 | 834 |
if (__isSmallInteger(index)) { |
214 | 835 |
val = _intVal(self); |
836 |
if (val < 0) |
|
837 |
val = -val; |
|
357 | 838 |
switch (idx = _intVal(index)) { |
214 | 839 |
case 1: |
357 | 840 |
break; |
214 | 841 |
case 2: |
357 | 842 |
val = (val >> 8); |
843 |
break; |
|
214 | 844 |
case 3: |
357 | 845 |
val = (val >> 16); |
846 |
break; |
|
214 | 847 |
case 4: |
357 | 848 |
val = (val >> 24); |
849 |
break; |
|
1 | 850 |
#ifdef alpha |
214 | 851 |
case 5: |
357 | 852 |
val = (val >> 32); |
853 |
break; |
|
214 | 854 |
case 6: |
357 | 855 |
val = (val >> 40); |
856 |
break; |
|
214 | 857 |
case 7: |
357 | 858 |
val = (val >> 48); |
859 |
break; |
|
214 | 860 |
case 8: |
357 | 861 |
val = (val >> 56); |
862 |
break; |
|
1 | 863 |
#endif |
357 | 864 |
default: |
865 |
if (idx < 1) |
|
866 |
goto bad; /* sorry */ |
|
867 |
val = 0; |
|
868 |
break; |
|
214 | 869 |
} |
357 | 870 |
RETURN ( _MKSMALLINT( val & 0xFF) ); |
1 | 871 |
} |
357 | 872 |
bad: ; |
77 | 873 |
%}. |
874 |
index > 0 ifFalse:[ |
|
357 | 875 |
" |
876 |
index less than 1 - not allowed |
|
877 |
" |
|
878 |
^ self primitiveFailed |
|
77 | 879 |
]. |
880 |
^ 0 |
|
62 | 881 |
|
357 | 882 |
" |
883 |
(16r12345678 digitAt:1) printStringRadix:16 |
|
884 |
(16r12345678 digitAt:3) printStringRadix:16 |
|
885 |
(16r12345678 digitAt:15) printStringRadix:16 |
|
886 |
(16r12345678 digitAt:0) printStringRadix:16 |
|
887 |
(16r12345678 digitAt:-10) printStringRadix:16 |
|
888 |
" |
|
701 | 889 |
! |
890 |
||
891 |
digitLength |
|
892 |
"return the number bytes used by this Integer" |
|
893 |
||
894 |
^ self abs highBit - 1 // 8 + 1 |
|
1 | 895 |
! ! |
896 |
||
701 | 897 |
!SmallInteger methodsFor:'catching messages'! |
1 | 898 |
|
701 | 899 |
basicAt:index |
900 |
"catch indexed access - report an error |
|
901 |
defined here since basicAt: in Object ommits the SmallInteger check." |
|
902 |
||
903 |
self notIndexed |
|
1 | 904 |
! |
905 |
||
701 | 906 |
basicAt:index put:anObject |
907 |
"catch indexed access - report an error |
|
908 |
defined here since basicAt:put: in Object ommits the SmallInteger check." |
|
909 |
||
910 |
self notIndexed |
|
911 |
! |
|
1 | 912 |
|
701 | 913 |
basicSize |
914 |
"return the number of indexed instvars - SmallIntegers have none. |
|
915 |
Defined here since basicSize in Object ommits the SmallInteger check." |
|
916 |
||
917 |
^ 0 |
|
918 |
! |
|
919 |
||
920 |
size |
|
921 |
"return the number of indexed instvars - SmallIntegers have none." |
|
922 |
||
923 |
^ 0 |
|
1 | 924 |
! ! |
925 |
||
926 |
!SmallInteger methodsFor:'coercing and converting'! |
|
927 |
||
701 | 928 |
asCharacter |
929 |
"Return a character with the receiver as ascii value" |
|
41 | 930 |
|
701 | 931 |
^ Character value:self |
1 | 932 |
! |
933 |
||
934 |
asFloat |
|
935 |
"return a Float with same value as receiver" |
|
936 |
||
937 |
%{ /* NOCONTEXT */ |
|
938 |
||
939 |
OBJ newFloat; |
|
587 | 940 |
double dVal = (double)_intVal(self); |
1 | 941 |
|
587 | 942 |
_qMKFLOAT(newFloat, dVal, SENDER); |
1 | 943 |
RETURN ( newFloat ); |
944 |
%} |
|
945 |
! |
|
946 |
||
947 |
asLargeInteger |
|
948 |
"return a LargeInteger with same value as receiver" |
|
949 |
||
950 |
^ LargeInteger value:self |
|
951 |
! |
|
952 |
||
701 | 953 |
coerce:aNumber |
954 |
"return aNumber converted into receivers type" |
|
955 |
||
956 |
^ aNumber asInteger |
|
957 |
! |
|
958 |
||
959 |
generality |
|
960 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
961 |
||
962 |
^ 20 |
|
963 |
! ! |
|
964 |
||
965 |
!SmallInteger methodsFor:'comparing'! |
|
966 |
||
967 |
< aNumber |
|
968 |
"return true, if the argument is greater than the receiver" |
|
969 |
||
970 |
%{ /* NOCONTEXT */ |
|
971 |
||
972 |
if (__isSmallInteger(aNumber)) { |
|
973 |
#ifdef POSITIVE_ADDRESSES |
|
974 |
RETURN ( (_intVal(self) < _intVal(aNumber)) ? true : false ); |
|
975 |
#else |
|
976 |
/* tag bit does not change ordering */ |
|
977 |
RETURN ( ((INT)self < (INT)aNumber) ? true : false ); |
|
978 |
#endif |
|
979 |
} |
|
980 |
if (__isFloatLike(aNumber)) { |
|
981 |
RETURN ( ((double)_intVal(self) < _floatVal(aNumber)) ? true : false ); |
|
982 |
} |
|
983 |
%} |
|
984 |
. |
|
985 |
^ aNumber lessFromInteger:self |
|
986 |
"^ self retry:#< coercing:aNumber" |
|
987 |
! |
|
988 |
||
989 |
<= aNumber |
|
990 |
"return true, if the argument is greater or equal" |
|
991 |
||
992 |
%{ /* NOCONTEXT */ |
|
993 |
||
994 |
if (__isSmallInteger(aNumber)) { |
|
995 |
#ifdef POSITIVE_ADDRESSES |
|
996 |
RETURN ( (_intVal(self) <= _intVal(aNumber)) ? true : false ); |
|
997 |
#else |
|
998 |
/* tag bit does not change ordering */ |
|
999 |
RETURN ( ((INT)self <= (INT)aNumber) ? true : false ); |
|
1000 |
#endif |
|
1001 |
} |
|
1002 |
if (__isFloatLike(aNumber)) { |
|
1003 |
RETURN ( ((double)_intVal(self) <= _floatVal(aNumber)) ? true : false ); |
|
1004 |
} |
|
1005 |
%} |
|
1006 |
. |
|
1007 |
^ self retry:#<= coercing:aNumber |
|
1008 |
! |
|
1009 |
||
1010 |
= aNumber |
|
1011 |
"return true, if the arguments value is equal to mine" |
|
1012 |
||
1013 |
%{ /* NOCONTEXT */ |
|
1014 |
||
1015 |
if (aNumber == self) { |
|
1016 |
RETURN ( true ); |
|
1017 |
} |
|
1018 |
if (! __isNonNilObject(aNumber)) { |
|
1019 |
/* a smallint or nil */ |
|
1020 |
RETURN ( false ); |
|
1021 |
} |
|
1022 |
||
1023 |
if (__isFloatLike(aNumber)) { |
|
1024 |
RETURN ( ((double)_intVal(self) == _floatVal(aNumber)) ? true : false ); |
|
1025 |
} |
|
1026 |
%} |
|
1027 |
. |
|
1028 |
aNumber respondsToArithmetic ifFalse:[^ false]. |
|
1029 |
^ self retry:#= coercing:aNumber |
|
1030 |
! |
|
1031 |
||
1032 |
> aNumber |
|
1033 |
"return true, if the argument is less than the receiver" |
|
1034 |
||
1035 |
%{ /* NOCONTEXT */ |
|
1036 |
||
1037 |
if (__isSmallInteger(aNumber)) { |
|
1038 |
#ifdef POSITIVE_ADDRESSES |
|
1039 |
RETURN ( (_intVal(self) > _intVal(aNumber)) ? true : false ); |
|
1040 |
#else |
|
1041 |
/* tag bit does not change ordering */ |
|
1042 |
RETURN ( ((INT)self > (INT)aNumber) ? true : false ); |
|
1043 |
#endif |
|
1044 |
} |
|
1045 |
if (__isFloatLike(aNumber)) { |
|
1046 |
RETURN ( ((double)_intVal(self) > _floatVal(aNumber)) ? true : false ); |
|
1047 |
} |
|
1048 |
%} |
|
1049 |
. |
|
1050 |
^ self retry:#> coercing:aNumber |
|
1051 |
! |
|
1052 |
||
1053 |
>= aNumber |
|
1054 |
"return true, if the argument is less or equal" |
|
1055 |
||
1056 |
%{ /* NOCONTEXT */ |
|
1057 |
||
1058 |
if (__isSmallInteger(aNumber)) { |
|
1059 |
#ifdef POSITIVE_ADDRESSES |
|
1060 |
RETURN ( (_intVal(self) >= _intVal(aNumber)) ? true : false ); |
|
1061 |
#else |
|
1062 |
/* tag bit does not change ordering */ |
|
1063 |
RETURN ( ((INT)self >= (INT)aNumber) ? true : false ); |
|
1064 |
#endif |
|
1065 |
} |
|
1066 |
if (__isFloatLike(aNumber)) { |
|
1067 |
RETURN ( ((double)_intVal(self) >= _floatVal(aNumber)) ? true : false ); |
|
1068 |
} |
|
1069 |
%} |
|
1070 |
. |
|
1071 |
^ self retry:#>= coercing:aNumber |
|
1072 |
! |
|
1 | 1073 |
|
701 | 1074 |
hash |
1075 |
"return an integer useful for hashing on value" |
|
1076 |
||
1077 |
self >= 0 ifTrue:[^ self]. |
|
1078 |
^ self negated |
|
1079 |
! |
|
1080 |
||
1081 |
identityHash |
|
1082 |
"return an integer useful for hashing on identity" |
|
1083 |
||
1084 |
self >= 0 ifTrue:[^ self]. |
|
1085 |
^ self negated |
|
1086 |
! |
|
1087 |
||
1088 |
max:aNumber |
|
1089 |
"return the receiver or the argument, whichever is greater" |
|
1090 |
||
1091 |
%{ /* NOCONTEXT */ |
|
1092 |
||
1093 |
if (__isSmallInteger(aNumber)) { |
|
1094 |
#ifdef POSITIVE_ADDRESSES |
|
1095 |
if (_intVal(self) > _intVal(aNumber)) { |
|
1096 |
#else |
|
1097 |
/* tag bit does not change ordering */ |
|
1098 |
if ((INT)(self) > (INT)(aNumber)) { |
|
1099 |
#endif |
|
1100 |
RETURN ( self ); |
|
1101 |
} |
|
1102 |
RETURN ( aNumber ); |
|
1103 |
} |
|
1104 |
if (__isFloatLike(aNumber)) { |
|
1105 |
if ( (double)_intVal(self) > _floatVal(aNumber) ) { |
|
1106 |
RETURN ( self ); |
|
1107 |
} |
|
1108 |
RETURN ( aNumber ); |
|
1109 |
} |
|
1110 |
%} |
|
1111 |
. |
|
1112 |
(self > aNumber) ifTrue:[^ self]. |
|
1113 |
^ aNumber |
|
1114 |
! |
|
1115 |
||
1116 |
min:aNumber |
|
1117 |
"return the receiver or the argument, whichever is smaller" |
|
1118 |
||
1119 |
%{ /* NOCONTEXT */ |
|
1120 |
||
1121 |
if (__isSmallInteger(aNumber)) { |
|
1122 |
#ifdef POSITIVE_ADDRESSES |
|
1123 |
if (_intVal(self) < _intVal(aNumber)) { |
|
1124 |
#else |
|
1125 |
/* tag bit does not change ordering */ |
|
1126 |
if ((INT)(self) < (INT)(aNumber)) { |
|
1127 |
#endif |
|
1128 |
RETURN ( self ); |
|
1129 |
} |
|
1130 |
RETURN ( aNumber ); |
|
1131 |
} |
|
1132 |
if (__isFloatLike(aNumber)) { |
|
1133 |
if ( (double)_intVal(self) < _floatVal(aNumber) ) { |
|
1134 |
RETURN ( self ); |
|
1135 |
} |
|
1136 |
RETURN ( aNumber ); |
|
1137 |
} |
|
1138 |
%} |
|
1139 |
. |
|
1140 |
(self < aNumber) ifTrue:[^ self]. |
|
1141 |
^ aNumber |
|
1142 |
! |
|
1143 |
||
1144 |
~= aNumber |
|
1145 |
"return true, if the arguments value is not equal to mine" |
|
1146 |
||
1147 |
%{ /* NOCONTEXT */ |
|
1148 |
||
1149 |
if (aNumber == self) { |
|
1150 |
RETURN ( false ); |
|
1151 |
} |
|
1152 |
if (! __isNonNilObject(aNumber)) { |
|
1153 |
/* a smallint or nil */ |
|
1154 |
RETURN ( true ); |
|
1155 |
} |
|
1156 |
||
1157 |
if (__isFloatLike(aNumber)) { |
|
1158 |
RETURN ( ((double)_intVal(self) == _floatVal(aNumber)) ? false : true ); |
|
1159 |
} |
|
1160 |
%} |
|
1161 |
. |
|
1162 |
aNumber respondsToArithmetic ifFalse:[^ true]. |
|
1163 |
^ self retry:#~= coercing:aNumber |
|
1164 |
! ! |
|
1165 |
||
1166 |
!SmallInteger methodsFor:'copying'! |
|
1167 |
||
1168 |
deepCopy |
|
1169 |
"return a deep copy of myself |
|
1170 |
- reimplemented here since smallintegers are unique" |
|
1171 |
||
1172 |
^ self |
|
1173 |
! |
|
1174 |
||
1175 |
deepCopyUsing:aDictionary |
|
1176 |
"return a deep copy of myself |
|
1177 |
- reimplemented here since smallintegers are unique" |
|
1178 |
||
1179 |
^ self |
|
1180 |
! |
|
1181 |
||
1182 |
shallowCopy |
|
1183 |
"return a shallow copy of myself |
|
1184 |
- reimplemented here since smallintegers are unique" |
|
1185 |
||
1186 |
^ self |
|
1187 |
! |
|
1188 |
||
1189 |
simpleDeepCopy |
|
1190 |
"return a deep copy of myself |
|
1191 |
- reimplemented here since smallintegers are unique" |
|
1192 |
||
1193 |
^ self |
|
1 | 1194 |
! ! |
1195 |
||
214 | 1196 |
!SmallInteger methodsFor:'iteration'! |
1 | 1197 |
|
1198 |
timesRepeat:aBlock |
|
357 | 1199 |
"evaluate the argument, aBlock self times. |
1200 |
Reimplemented as primitive for speed" |
|
1 | 1201 |
|
357 | 1202 |
|home| |
1203 |
%{ |
|
1204 |
REGISTER INT tmp; |
|
1205 |
REGISTER OBJFUNC code; |
|
1206 |
extern OBJ Block; |
|
1207 |
static struct inlineCache blockVal = __ILC0(0); |
|
1208 |
REGISTER OBJ rHome; |
|
1 | 1209 |
|
357 | 1210 |
tmp = __intVal(self); |
1211 |
if (tmp > 0) { |
|
1212 |
if (__isBlockLike(aBlock) |
|
1213 |
&& (_BlockInstPtr(aBlock)->b_nargs == _MKSMALLINT(0))) { |
|
394 | 1214 |
if ((code = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
357 | 1215 |
#ifdef NEW_BLOCK_CALL |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1216 |
do { |
394 | 1217 |
if (InterruptPending != nil) interrupt(CONARG); |
1218 |
(*code)(aBlock COMMA_CON); |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1219 |
} while(--tmp); |
394 | 1220 |
RETURN (self); |
357 | 1221 |
#else /* old BLOCK_CALL */ |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1222 |
/* |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1223 |
* arg is a compiled block - |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1224 |
* directly call it without going through "Block-value" |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1225 |
*/ |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1226 |
rHome = _BlockInstPtr(aBlock)->b_home; |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1227 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) { |
394 | 1228 |
/* |
1229 |
* home will not move - keep in in a register |
|
1230 |
*/ |
|
1231 |
# if defined(UNROLL_LOOPS) |
|
1232 |
while (tmp > 4) { |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1233 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1234 |
(*code)(rHome COMMA_CON); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1235 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1236 |
(*code)(rHome COMMA_CON); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1237 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1238 |
(*code)(rHome COMMA_CON); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1239 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1240 |
(*code)(rHome COMMA_CON); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1241 |
tmp -= 4; |
394 | 1242 |
} |
1243 |
# endif |
|
1244 |
do { |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1245 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1246 |
(*code)(rHome COMMA_CON); |
394 | 1247 |
} while(--tmp); |
1248 |
RETURN (self); |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1249 |
} |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1250 |
home = rHome; |
357 | 1251 |
do { |
1252 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1253 |
(*code)(home COMMA_CON); |
|
1254 |
} while(--tmp); |
|
394 | 1255 |
RETURN (self); |
357 | 1256 |
#endif /* NEW_BLOCK_CALL */ |
394 | 1257 |
} |
1258 |
if (_BlockInstPtr(aBlock)->b_bytecodes != nil) { |
|
400 | 1259 |
#ifdef PASS_ARG_POINTER |
1260 |
extern OBJ __interpret(); |
|
1261 |
#else |
|
1262 |
extern OBJ interpret(); |
|
1263 |
#endif |
|
1264 |
||
394 | 1265 |
/* |
1266 |
* an interpreted block |
|
1267 |
*/ |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1268 |
home = _BlockInstPtr(aBlock)->b_home; |
400 | 1269 |
#if defined(mips) || defined(rs6000) |
1270 |
# define INDIRECT_CALL_IS_FASTER |
|
1271 |
#endif |
|
1272 |
||
1273 |
#ifdef PASS_ARG_POINTER |
|
1274 |
# ifdef INDIRECT_CALL_IS_FASTER |
|
1275 |
code = __interpret; |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1276 |
# define INTERPRET (*code) |
400 | 1277 |
# else |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1278 |
# define INTERPRET __interpret |
400 | 1279 |
# endif |
1280 |
#else |
|
1281 |
# ifdef INDIRECT_CALL_IS_FASTER |
|
1282 |
code = interpret; |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1283 |
# define INTERPRET (*code) |
400 | 1284 |
# else |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1285 |
# define INTERPRET interpret |
400 | 1286 |
# endif |
1287 |
#endif |
|
1288 |
#ifdef NEW_BLOCK_CALL |
|
1289 |
# define HOME nil |
|
1290 |
#else |
|
1291 |
# define HOME home |
|
1292 |
#endif |
|
1293 |
#if defined(UNROLL_LOOPS) |
|
1294 |
while (tmp > 4) { |
|
1295 |
if (InterruptPending != nil) interrupt(CONARG); |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1296 |
INTERPRET(aBlock, 0, nil, HOME COMMA_SND, nil); |
400 | 1297 |
if (InterruptPending != nil) interrupt(CONARG); |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1298 |
INTERPRET(aBlock, 0, nil, HOME COMMA_SND, nil); |
400 | 1299 |
if (InterruptPending != nil) interrupt(CONARG); |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1300 |
INTERPRET(aBlock, 0, nil, HOME COMMA_SND, nil); |
400 | 1301 |
if (InterruptPending != nil) interrupt(CONARG); |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1302 |
INTERPRET(aBlock, 0, nil, HOME COMMA_SND, nil); |
400 | 1303 |
tmp -= 4; |
1304 |
} |
|
1305 |
#endif |
|
394 | 1306 |
do { |
1307 |
if (InterruptPending != nil) interrupt(CONARG); |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1308 |
INTERPRET(aBlock, 0, nil, HOME COMMA_SND, nil); |
394 | 1309 |
} while(--tmp); |
1310 |
RETURN (self); |
|
1311 |
} |
|
357 | 1312 |
} |
394 | 1313 |
/* |
1314 |
* arg is something else - call it with value" |
|
1315 |
*/ |
|
1316 |
do { |
|
1317 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1318 |
||
1319 |
(*blockVal.ilc_func)(aBlock, @symbol(value), CON_COMMA nil, &blockVal); |
|
1320 |
} while(--tmp); |
|
357 | 1321 |
} |
400 | 1322 |
#undef INTERPRET |
1323 |
#undef HOME |
|
357 | 1324 |
%} |
1325 |
||
1326 |
"/ |count "{ Class: SmallInteger }" | |
|
1327 |
"/ |
|
1328 |
"/ count := self. |
|
1329 |
"/ [count > 0] whileTrue:[ |
|
1330 |
"/ aBlock value. |
|
1331 |
"/ count := count - 1 |
|
1332 |
"/ ] |
|
1 | 1333 |
! |
1334 |
||
701 | 1335 |
to:stop by:incr do:aBlock |
1336 |
"reimplemented as primitive for speed" |
|
1337 |
||
1338 |
|home| |
|
1339 |
%{ |
|
1340 |
REGISTER INT tmp, step; |
|
1341 |
REGISTER INT final; |
|
1342 |
REGISTER OBJFUNC code; |
|
1343 |
extern OBJ Block; |
|
1344 |
static struct inlineCache blockVal = __ILC1(0); |
|
1345 |
REGISTER OBJ rHome; |
|
1346 |
||
1347 |
if (__bothSmallInteger(incr, stop)) { |
|
1348 |
tmp = _intVal(self); |
|
1349 |
final = _intVal(stop); |
|
1350 |
step = _intVal(incr); |
|
1351 |
if (__isBlockLike(aBlock) |
|
1352 |
&& ((code = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
|
1353 |
&& (_BlockInstPtr(aBlock)->b_nargs == _MKSMALLINT(1))) { |
|
1354 |
#ifdef NEW_BLOCK_CALL |
|
1355 |
if (step < 0) { |
|
1356 |
while (tmp >= final) { |
|
1357 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1358 |
(*code)(aBlock, CON_COMMA _MKSMALLINT(tmp)); |
|
1359 |
tmp += step; |
|
1360 |
} |
|
1361 |
} else { |
|
1362 |
while (tmp <= final) { |
|
1363 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1364 |
(*code)(aBlock, CON_COMMA _MKSMALLINT(tmp)); |
|
1365 |
tmp += step; |
|
1366 |
} |
|
1367 |
} |
|
1368 |
#else |
|
1369 |
/* |
|
1370 |
* arg is a compiled block - |
|
1371 |
* directly call it without going through "Block-value" |
|
1372 |
*/ |
|
1373 |
home = _BlockInstPtr(aBlock)->b_home; |
|
1374 |
rHome = home; |
|
1375 |
if (step < 0) { |
|
1376 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) { |
|
1377 |
/* |
|
1378 |
* home is on stack - will not move |
|
1379 |
*/ |
|
1380 |
while (tmp >= final) { |
|
1381 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1382 |
(*code)(rHome, CON_COMMA _MKSMALLINT(tmp)); |
|
1383 |
tmp += step; |
|
1384 |
} |
|
1385 |
} else { |
|
1386 |
while (tmp >= final) { |
|
1387 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1388 |
(*code)(home, CON_COMMA _MKSMALLINT(tmp)); |
|
1389 |
tmp += step; |
|
1390 |
} |
|
1391 |
} |
|
1392 |
} else { |
|
1393 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) { |
|
1394 |
/* |
|
1395 |
* home is on stack - will not move |
|
1396 |
*/ |
|
1397 |
while (tmp <= final) { |
|
1398 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1399 |
(*code)(rHome, CON_COMMA _MKSMALLINT(tmp)); |
|
1400 |
tmp += step; |
|
1401 |
} |
|
1402 |
} else { |
|
1403 |
while (tmp <= final) { |
|
1404 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1405 |
(*code)(home, CON_COMMA _MKSMALLINT(tmp)); |
|
1406 |
tmp += step; |
|
1407 |
} |
|
1408 |
} |
|
1409 |
} |
|
1410 |
#endif |
|
1411 |
} else { |
|
1412 |
/* |
|
1413 |
* arg is something else - call it with value" |
|
1414 |
*/ |
|
1415 |
if (step < 0) { |
|
1416 |
while (tmp >= final) { |
|
1417 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1418 |
||
1419 |
(*blockVal.ilc_func)(aBlock, |
|
1420 |
@symbol(value:), |
|
1421 |
CON_COMMA nil, &blockVal, |
|
1422 |
_MKSMALLINT(tmp)); |
|
1423 |
tmp += step; |
|
1424 |
} |
|
1425 |
} else { |
|
1426 |
while (tmp <= final) { |
|
1427 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1428 |
||
1429 |
(*blockVal.ilc_func)(aBlock, |
|
1430 |
@symbol(value:), |
|
1431 |
CON_COMMA nil, &blockVal, |
|
1432 |
_MKSMALLINT(tmp)); |
|
1433 |
tmp += step; |
|
1434 |
} |
|
1435 |
} |
|
1436 |
} |
|
1437 |
RETURN ( self ); |
|
1438 |
} |
|
1439 |
%} |
|
1440 |
. |
|
1441 |
^ super to:stop by:incr do:aBlock |
|
1442 |
||
1443 |
" |
|
1444 |
1 to:10 by:3 do:[:i | i printNewline] |
|
1445 |
" |
|
1446 |
! |
|
1447 |
||
1 | 1448 |
to:stop do:aBlock |
62 | 1449 |
"evaluate aBlock for every integer between (and including) the receiver |
1450 |
and the argument, stop. |
|
357 | 1451 |
Reimplemented as primitive for speed" |
1 | 1452 |
|
77 | 1453 |
|home| |
1 | 1454 |
%{ |
1455 |
REGISTER INT tmp; |
|
1456 |
INT final; |
|
1457 |
REGISTER OBJFUNC code; |
|
214 | 1458 |
extern OBJ Block; |
216 | 1459 |
static struct inlineCache blockVal = __ILC1(0); |
2 | 1460 |
REGISTER OBJ rHome; |
1 | 1461 |
|
252 | 1462 |
if (__isSmallInteger(stop)) { |
214 | 1463 |
tmp = _intVal(self); |
1464 |
final = _intVal(stop); |
|
1465 |
if (__isBlockLike(aBlock) |
|
1466 |
&& (_BlockInstPtr(aBlock)->b_nargs == _MKSMALLINT(1))) { |
|
400 | 1467 |
if ((code = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
13 | 1468 |
#ifdef NEW_BLOCK_CALL |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1469 |
while (tmp <= final) { |
400 | 1470 |
if (InterruptPending != nil) interrupt(CONARG); |
1471 |
(*code)(aBlock, CON_COMMA _MKSMALLINT(tmp)); |
|
1472 |
tmp++; |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1473 |
} |
77 | 1474 |
#else /* old BLOCK_CALL */ |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1475 |
/* |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1476 |
* arg is a compiled block - |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1477 |
* directly call it without going through "Block-value" |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1478 |
*/ |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1479 |
rHome = _BlockInstPtr(aBlock)->b_home; |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1480 |
if ((rHome == nil) || (__qSpace(rHome) >= STACKSPACE)) { |
400 | 1481 |
/* |
1482 |
* home will not move - keep in in a register |
|
1483 |
* since this is also the most common case, |
|
1484 |
* its worth trading a bit memory for speed here ... |
|
1485 |
*/ |
|
394 | 1486 |
# if defined(UNROLL_LOOPS) |
400 | 1487 |
{ |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1488 |
int t4; |
370 | 1489 |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1490 |
while ((t4 = tmp+4) < final) { |
400 | 1491 |
OBJ idx = _MKSMALLINT(tmp); |
1492 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1493 |
(*code)(rHome, CON_COMMA idx); |
|
1494 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1495 |
(*code)(rHome, CON_COMMA _ADD_INT(idx, 1)); |
|
1496 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1497 |
(*code)(rHome, CON_COMMA _ADD_INT(idx,2)); |
|
1498 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1499 |
(*code)(rHome, CON_COMMA _ADD_INT(idx,3)); |
|
1500 |
tmp = t4; |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1501 |
} |
400 | 1502 |
} |
1503 |
# endif |
|
1504 |
while (tmp <= final) { |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1505 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1506 |
(*code)(rHome, CON_COMMA _MKSMALLINT(tmp)); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1507 |
tmp++; |
400 | 1508 |
} |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1509 |
} else { |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1510 |
home = rHome; |
400 | 1511 |
while (tmp <= final) { |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1512 |
if (InterruptPending != nil) interrupt(CONARG); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1513 |
(*code)(home, CON_COMMA _MKSMALLINT(tmp)); |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1514 |
tmp++; |
370 | 1515 |
} |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1516 |
} |
400 | 1517 |
RETURN (self); |
1518 |
#endif /* NEW_BLOCK_CALL */ |
|
1519 |
} |
|
1520 |
if (_BlockInstPtr(aBlock)->b_bytecodes != nil) { |
|
1521 |
/* |
|
1522 |
* an interpreted block |
|
1523 |
*/ |
|
1524 |
#ifdef PASS_ARG_POINTER |
|
1525 |
extern OBJ __interpret(); |
|
1526 |
#else |
|
1527 |
extern OBJ interpret(); |
|
1528 |
#endif |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1529 |
home = _BlockInstPtr(aBlock)->b_home; |
400 | 1530 |
#ifdef PASS_ARG_POINTER |
1531 |
# ifdef INDIRECT_CALL_IS_FASTER |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1532 |
code = __interpret; |
400 | 1533 |
# define INTERPRET (*code) |
1534 |
# else |
|
1535 |
# define INTERPRET __interpret |
|
394 | 1536 |
# endif |
400 | 1537 |
#else |
1538 |
# ifdef INDIRECT_CALL_IS_FASTER |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1539 |
code = interpret; |
400 | 1540 |
# define INTERPRET (*code) |
1541 |
# else |
|
1542 |
# define INTERPRET interpret |
|
1543 |
# endif |
|
1544 |
#endif |
|
1545 |
#ifdef NEW_BLOCK_CALL |
|
1546 |
# define HOME nil |
|
1547 |
#else |
|
1548 |
# define HOME home |
|
1549 |
#endif |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1550 |
while (tmp <= final) { |
400 | 1551 |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1552 |
if (InterruptPending != nil) interrupt(CONARG); |
400 | 1553 |
#ifdef PASS_ARG_POINTER |
1554 |
{ |
|
1555 |
OBJ idx; |
|
1556 |
idx = __MKSMALLINT(tmp); |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1557 |
INTERPRET(aBlock, 1, nil, HOME COMMA_SND, nil, &idx); |
400 | 1558 |
} |
1559 |
#else |
|
1560 |
INTERPRET(aBlock, 1, nil, HOME COMMA_SND, nil, __MKSMALLINT(tmp)); |
|
1561 |
#endif |
|
214 | 1562 |
tmp++; |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1563 |
} |
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
400
diff
changeset
|
1564 |
RETURN (self); |
214 | 1565 |
} |
400 | 1566 |
} |
1567 |
/* |
|
1568 |
* arg is something else - call it with value" |
|
1569 |
*/ |
|
1570 |
while (tmp <= final) { |
|
1571 |
if (InterruptPending != nil) interrupt(CONARG); |
|
1 | 1572 |
|
400 | 1573 |
(*blockVal.ilc_func)(aBlock, |
214 | 1574 |
@symbol(value:), |
1575 |
CON_COMMA nil, &blockVal, |
|
1576 |
_MKSMALLINT(tmp)); |
|
400 | 1577 |
tmp++; |
214 | 1578 |
} |
1579 |
RETURN ( self ); |
|
1 | 1580 |
} |
400 | 1581 |
%}. |
1582 |
"/ |
|
1583 |
"/ arrive here if stop is not a smallInteger |
|
1584 |
"/ |
|
1585 |
||
62 | 1586 |
^ super to:stop do:aBlock |
1587 |
||
216 | 1588 |
" |
1589 |
1 to:10 do:[:i | i printNewline] |
|
1590 |
" |
|
701 | 1591 |
! ! |
1 | 1592 |
|
701 | 1593 |
!SmallInteger methodsFor:'misc math'! |
1 | 1594 |
|
701 | 1595 |
gcd:anInteger |
1596 |
"return the greatest common divisor (Euclid's algorithm). |
|
1597 |
This has been redefined here for more speed since due to the |
|
1598 |
use of gcd in Fraction code, it has become time-critical for |
|
1599 |
some code. (thanx to MessageTally)" |
|
1600 |
||
1601 |
%{ /* NOCONTEXT */ |
|
1 | 1602 |
|
701 | 1603 |
if (__isSmallInteger(anInteger)) { |
1604 |
INT orgArg, ttt, selfInt, temp; |
|
1605 |
||
1606 |
ttt = orgArg = _intVal(anInteger); |
|
1607 |
if (ttt) { |
|
1608 |
selfInt = _intVal(self); |
|
1609 |
while (ttt != 0) { |
|
1610 |
temp = selfInt % ttt; |
|
1611 |
selfInt = ttt; |
|
1612 |
ttt = temp; |
|
214 | 1613 |
} |
1614 |
/* |
|
701 | 1615 |
* since its not defined in what the sign of |
1616 |
* a modulu result is when the arg is negative, |
|
1617 |
* change it explicitely here ... |
|
214 | 1618 |
*/ |
701 | 1619 |
if (orgArg < 0) { |
1620 |
/* result should be negative */ |
|
1621 |
if (selfInt > 0) selfInt = -selfInt; |
|
214 | 1622 |
} else { |
701 | 1623 |
/* result should be positive */ |
1624 |
if (selfInt < 0) selfInt = -selfInt; |
|
214 | 1625 |
} |
701 | 1626 |
RETURN ( _MKSMALLINT(selfInt) ); |
214 | 1627 |
} |
1 | 1628 |
} |
1629 |
%} |
|
1630 |
. |
|
701 | 1631 |
^ super gcd:anInteger |
1632 |
! |
|
62 | 1633 |
|
701 | 1634 |
intlog10 |
1635 |
"return the truncation of log10 of the receiver - |
|
1636 |
stupid implementation; used to find out the number of digits needed |
|
1637 |
to print a number/and for conversion to a LargeInteger. |
|
1638 |
Implemented that way, to allow for tiny systems without a Float class |
|
1639 |
(i.e. without log)." |
|
2 | 1640 |
|
701 | 1641 |
self <= 0 ifTrue:[ |
1642 |
self error:'logarithm of negative integer' |
|
2 | 1643 |
]. |
701 | 1644 |
self < 10 ifTrue:[^ 1]. |
1645 |
self < 100 ifTrue:[^ 2]. |
|
1646 |
self < 1000 ifTrue:[^ 3]. |
|
1647 |
self < 10000 ifTrue:[^ 4]. |
|
1648 |
self < 100000 ifTrue:[^ 5]. |
|
1649 |
self < 1000000 ifTrue:[^ 6]. |
|
1650 |
self < 10000000 ifTrue:[^ 7]. |
|
1651 |
self < 100000000 ifTrue:[^ 8]. |
|
1652 |
self < 1000000000 ifTrue:[^ 9]. |
|
1653 |
^ 10 |
|
2 | 1654 |
! ! |
1655 |
||
701 | 1656 |
!SmallInteger methodsFor:'modulu arithmetic'! |
2 | 1657 |
|
701 | 1658 |
plus:aNumber |
1659 |
"return the sum of the receiver and the argument, as SmallInteger. |
|
1660 |
The argument must be another SmallInteger. |
|
1661 |
If the result overflows the smallInteger range, the value modulu the |
|
1662 |
smallInteger range is returned (i.e. the low bits of the sum). |
|
1663 |
This is of course not always correct, but some code does a modulu anyway |
|
1664 |
and can therefore speed things up by not going through LargeIntegers." |
|
41 | 1665 |
|
701 | 1666 |
%{ /* NOCONTEXT */ |
1667 |
||
1668 |
if (__isSmallInteger(aNumber)) { |
|
1669 |
RETURN ( _MKSMALLINT((_intVal(self) + _intVal(aNumber)) & 0x7FFFFFFF) ); |
|
1670 |
} |
|
1671 |
%} |
|
1672 |
. |
|
1673 |
self primitiveFailed |
|
2 | 1674 |
! |
1675 |
||
701 | 1676 |
subtract:aNumber |
1677 |
"return the difference of the receiver and the argument, as SmallInteger. |
|
1678 |
The argument must be another SmallInteger. |
|
1679 |
If the result overflows the smallInteger range, the value modulu the |
|
1680 |
smallInteger range is returned (i.e. the low bits of the sum). |
|
1681 |
This is of course not always correct, but some code does a modulu anyway |
|
1682 |
and can therefore speed things up by not going through LargeIntegers." |
|
1683 |
||
1684 |
%{ /* NOCONTEXT */ |
|
2 | 1685 |
|
701 | 1686 |
if (__isSmallInteger(aNumber)) { |
1687 |
RETURN ( _MKSMALLINT((_intVal(self) - _intVal(aNumber)) & 0x7FFFFFFF) ); |
|
1688 |
} |
|
1689 |
%} |
|
1690 |
. |
|
1691 |
self primitiveFailed |
|
1692 |
! |
|
1693 |
||
1694 |
times:aNumber |
|
1695 |
"return the product of the receiver and the argument, as SmallInteger. |
|
1696 |
The argument must be another SmallInteger. |
|
1697 |
If the result overflows the smallInteger range, the value modulu the |
|
1698 |
smallInteger range is returned (i.e. the low bits of the product). |
|
1699 |
This is of course not always correct, but some code does a modulu anyway |
|
1700 |
and can therefore speed things up by not going through LargeIntegers." |
|
1701 |
||
1702 |
%{ /* NOCONTEXT */ |
|
1703 |
||
1704 |
if (__isSmallInteger(aNumber)) { |
|
1705 |
RETURN ( _MKSMALLINT((_intVal(self) * _intVal(aNumber)) & 0x7FFFFFFF) ); |
|
1706 |
} |
|
1707 |
%} |
|
1708 |
. |
|
1709 |
self primitiveFailed |
|
1 | 1710 |
! ! |
1711 |
||
1712 |
!SmallInteger methodsFor:'printing & storing'! |
|
1713 |
||
62 | 1714 |
printOn:aStream |
68 | 1715 |
"append my printstring (base 10) to aStream." |
62 | 1716 |
|
1717 |
aStream nextPutAll:(self printString) |
|
1718 |
! |
|
1719 |
||
701 | 1720 |
printOn:aStream base:radix |
1721 |
"append my printstring in any number base to aStream. |
|
1722 |
The radix argument should be between 2 and 36." |
|
1723 |
||
1724 |
aStream nextPutAll:(self printStringRadix:radix) |
|
1725 |
! |
|
1726 |
||
1 | 1727 |
printString |
1728 |
"return my printstring (base 10)" |
|
1729 |
||
68 | 1730 |
"since printf knows pretty good how to do this, |
1731 |
here is an exception to the rule of basing printString |
|
1732 |
upon the printOn: method." |
|
1733 |
||
1 | 1734 |
%{ /* NOCONTEXT */ |
1735 |
||
1736 |
char buffer[30]; |
|
1737 |
OBJ newString; |
|
13 | 1738 |
#ifdef THISCONTEXT_IN_REGISTER |
369 | 1739 |
/* |
1740 |
* actually only needed on sparc: since thisContext is |
|
1741 |
* in a global register, which gets destroyed by printf, |
|
1742 |
* manually save it here - very stupid ... |
|
1743 |
*/ |
|
1744 |
extern OBJ __thisContext__; |
|
1745 |
__thisContext__ = __thisContext; |
|
1 | 1746 |
#endif |
1747 |
||
1748 |
sprintf(buffer, "%d", _intVal(self)); |
|
13 | 1749 |
#ifdef THISCONTEXT_IN_REGISTER |
369 | 1750 |
__thisContext = __thisContext__; |
1751 |
__thisContext__ = nil; |
|
1 | 1752 |
#endif |
369 | 1753 |
|
314 | 1754 |
newString = _MKSTRING(buffer COMMA_SND); |
282 | 1755 |
if (newString != nil) { |
305 | 1756 |
RETURN (newString); |
282 | 1757 |
} |
1758 |
%}. |
|
1759 |
^ super printString |
|
1 | 1760 |
! |
1761 |
||
1762 |
printStringRadix:radix |
|
62 | 1763 |
"return my printstring (optimized for bases 16, 10 and 8)" |
1 | 1764 |
|
1765 |
%{ /* NOCONTEXT */ |
|
1766 |
||
1767 |
char *format = (char *)0; |
|
1768 |
char buffer[30]; |
|
1769 |
OBJ newString; |
|
1770 |
||
252 | 1771 |
if (__isSmallInteger(radix)) { |
214 | 1772 |
switch (_intVal(radix)) { |
1773 |
case 10: |
|
1774 |
format = "%d"; |
|
1775 |
break; |
|
1776 |
case 16: |
|
1777 |
format = "%x"; |
|
1778 |
break; |
|
1779 |
case 8: |
|
1780 |
format = "%o"; |
|
1781 |
break; |
|
1782 |
} |
|
1 | 1783 |
} |
1784 |
||
1785 |
if (format) { |
|
13 | 1786 |
#ifdef THISCONTEXT_IN_REGISTER |
379 | 1787 |
/* |
1788 |
* actually only needed on sparc: since thisContext is |
|
1789 |
* in a global register, which gets destroyed by printf, |
|
1790 |
* manually save it here - very stupid ... |
|
1791 |
*/ |
|
1792 |
extern OBJ __thisContext__; |
|
1793 |
__thisContext__ = __thisContext; |
|
1 | 1794 |
#endif |
214 | 1795 |
sprintf(buffer, format, _intVal(self)); |
13 | 1796 |
#ifdef THISCONTEXT_IN_REGISTER |
369 | 1797 |
__thisContext = __thisContext__; |
1798 |
__thisContext = nil; |
|
1 | 1799 |
#endif |
324 | 1800 |
newString = _MKSTRING(buffer COMMA_SND); |
1801 |
if (newString != nil) { |
|
282 | 1802 |
RETURN (newString); |
324 | 1803 |
} |
1 | 1804 |
} |
314 | 1805 |
%}. |
1806 |
" |
|
1807 |
fall back for seldom used bases |
|
1808 |
" |
|
1 | 1809 |
^ super printStringRadix:radix |
62 | 1810 |
|
1811 |
"123 printStringRadix:16" |
|
1812 |
"123 printStringRadix:8" |
|
1813 |
"123 printStringRadix:2" |
|
1814 |
"123 printStringRadix:3" |
|
1815 |
"123 printStringRadix:1" |
|
1 | 1816 |
! |
1817 |
||
1818 |
printfPrintString:formatString |
|
1819 |
"non-portable, but sometimes useful. |
|
1820 |
return a printed representation of the receiver |
|
62 | 1821 |
as specified by formatString, which is defined by the C- |
1822 |
function 'printf'. |
|
1823 |
No checking for string overrun - the resulting string |
|
1824 |
must be shorter than 256 chars or else ... |
|
1825 |
This method is NONSTANDARD and may be removed without notice." |
|
1 | 1826 |
|
13 | 1827 |
%{ /* STACK: 400 */ |
1 | 1828 |
|
1829 |
char buffer[256]; |
|
314 | 1830 |
OBJ s; |
1 | 1831 |
|
62 | 1832 |
if (__isString(formatString)) { |
13 | 1833 |
#ifdef THISCONTEXT_IN_REGISTER |
379 | 1834 |
/* |
1835 |
* actually only needed on sparc: since thisContext is |
|
1836 |
* in a global register, which gets destroyed by printf, |
|
1837 |
* manually save it here - very stupid ... |
|
1838 |
*/ |
|
1839 |
extern OBJ __thisContext__; |
|
1840 |
__thisContext__ = __thisContext; |
|
1 | 1841 |
#endif |
214 | 1842 |
sprintf(buffer, _stringVal(formatString), _intVal(self)); |
13 | 1843 |
#ifdef THISCONTEXT_IN_REGISTER |
369 | 1844 |
__thisContext = __thisContext__; |
1845 |
__thisContext__ = nil; |
|
1 | 1846 |
#endif |
314 | 1847 |
s = _MKSTRING(buffer COMMA_SND); |
1848 |
if (s != nil) { |
|
1849 |
RETURN (s); |
|
1850 |
} |
|
1 | 1851 |
} |
314 | 1852 |
%}. |
1 | 1853 |
self primitiveFailed |
1854 |
||
1855 |
"123 printfPrintString:'%%d -> %d'" |
|
1856 |
"123 printfPrintString:'%%6d -> %6d'" |
|
1857 |
"123 printfPrintString:'%%x -> %x'" |
|
1858 |
"123 printfPrintString:'%%4x -> %4x'" |
|
1859 |
"123 printfPrintString:'%%04x -> %04x'" |
|
1860 |
! ! |
|
701 | 1861 |
|
1862 |
!SmallInteger methodsFor:'testing'! |
|
1863 |
||
1864 |
between:min and:max |
|
1865 |
"return true if the receiver is less than or equal to the argument max |
|
1866 |
and greater than or equal to the argument min. |
|
1867 |
- reimplemented here for speed" |
|
1868 |
||
1869 |
%{ /* NOCONTEXT */ |
|
1870 |
||
1871 |
if (__bothSmallInteger(min, max)) { |
|
1872 |
REGISTER INT selfVal; |
|
1873 |
||
1874 |
selfVal = _intVal(self); |
|
1875 |
if (selfVal < _intVal(min)) { |
|
1876 |
RETURN ( false ); |
|
1877 |
} |
|
1878 |
if (selfVal > _intVal(max)) { |
|
1879 |
RETURN ( false ); |
|
1880 |
} |
|
1881 |
RETURN ( true ); |
|
1882 |
} |
|
1883 |
%} |
|
1884 |
. |
|
1885 |
(self < min) ifTrue:[^ false]. |
|
1886 |
(self > max) ifTrue:[^ false]. |
|
1887 |
^ true |
|
1888 |
! |
|
1889 |
||
1890 |
even |
|
1891 |
"return true, if the receiver is even" |
|
1892 |
||
1893 |
%{ /* NOCONTEXT */ |
|
1894 |
||
1895 |
#ifdef POSITIVE_ADDRESSES |
|
1896 |
RETURN ( ((INT)self & 1) ? false : true ); |
|
1897 |
#else |
|
1898 |
RETURN ( ((INT)self & ((INT)_MKSMALLINT(1) & ~TAG_INT)) ? false : true ); |
|
1899 |
#endif |
|
1900 |
%} |
|
1901 |
! |
|
1902 |
||
1903 |
negative |
|
1904 |
"return true, if the receiver is less than zero |
|
1905 |
reimplemented here for speed" |
|
1906 |
||
1907 |
%{ /* NOCONTEXT */ |
|
1908 |
||
1909 |
#ifdef POSITIVE_ADDRESSES |
|
1910 |
RETURN ( (_intVal(self) < 0) ? true : false ); |
|
1911 |
#else |
|
1912 |
/* tag bit does not change sign */ |
|
1913 |
RETURN ( ((INT)(self) < 0) ? true : false ); |
|
1914 |
#endif |
|
1915 |
%} |
|
1916 |
! |
|
1917 |
||
1918 |
odd |
|
1919 |
"return true, if the receiver is odd" |
|
1920 |
||
1921 |
%{ /* NOCONTEXT */ |
|
1922 |
||
1923 |
#ifdef POSITIVE_ADDRESSES |
|
1924 |
RETURN ( ((INT)self & 1) ? true : false ); |
|
1925 |
#else |
|
1926 |
RETURN ( ((INT)self & ((INT)_MKSMALLINT(1) & ~TAG_INT)) ? true : false ); |
|
1927 |
#endif |
|
1928 |
%} |
|
1929 |
! |
|
1930 |
||
1931 |
positive |
|
1932 |
"return true, if the receiver is not negative |
|
1933 |
reimplemented here for speed" |
|
1934 |
||
1935 |
%{ /* NOCONTEXT */ |
|
1936 |
||
1937 |
#ifdef POSITIVE_ADDRESSES |
|
1938 |
RETURN ( (_intVal(self) >= 0) ? true : false ); |
|
1939 |
#else |
|
1940 |
/* tag bit does not change sign */ |
|
1941 |
RETURN ( ((INT)(self) >= 0) ? true : false ); |
|
1942 |
#endif |
|
1943 |
%} |
|
1944 |
! |
|
1945 |
||
1946 |
sign |
|
1947 |
"return the sign of the receiver |
|
1948 |
reimplemented here for speed" |
|
1949 |
||
1950 |
%{ /* NOCONTEXT */ |
|
1951 |
||
1952 |
INT val = _intVal(self); |
|
1953 |
||
1954 |
if (val < 0) { |
|
1955 |
RETURN ( _MKSMALLINT(-1) ); |
|
1956 |
} |
|
1957 |
if (val > 0) { |
|
1958 |
RETURN ( _MKSMALLINT(1) ); |
|
1959 |
} |
|
1960 |
RETURN ( _MKSMALLINT(0) ); |
|
1961 |
%} |
|
1962 |
! |
|
1963 |
||
1964 |
strictlyPositive |
|
1965 |
"return true, if the receiver is greater than zero |
|
1966 |
reimplemented here for speed" |
|
1967 |
||
1968 |
%{ /* NOCONTEXT */ |
|
1969 |
||
1970 |
#ifdef POSITIVE_ADDRESSES |
|
1971 |
RETURN ( (_intVal(self) > 0) ? true : false ); |
|
1972 |
#else |
|
1973 |
/* tag bit does not change sign */ |
|
1974 |
RETURN ( ((INT)(self) > 0) ? true : false ); |
|
1975 |
#endif |
|
1976 |
%} |
|
1977 |
! ! |
|
1978 |
||
1979 |
!SmallInteger class methodsFor:'documentation'! |
|
1980 |
||
1981 |
version |
|
1982 |
^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.40 1995-12-07 21:36:33 cg Exp $' |
|
1983 |
! ! |