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