1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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 |
|
|
20 |
SmallInteger comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
44
|
25 |
$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.8 1994-01-16 03:46:48 claus Exp $
|
1
|
26 |
|
|
27 |
SmallIntegers are Integers in the range of +/- 2^30 (i.e. 31 bits).
|
3
|
28 |
These are no real objects - they have no instances (not even storage !!)
|
|
29 |
and cannot be subclassed.
|
1
|
30 |
The reason is to save both storage and runtime by not collecting
|
|
31 |
SmallIntegers in the system. SmallInts are marked by having the TAG_INT
|
3
|
32 |
bit set in contrast to Objects which do not. Since this knowledge is
|
1
|
33 |
hardwired into the system (an there is no class-field stored with
|
|
34 |
SmallIntegers) there can be no subclass of SmallInteger (sorry).
|
|
35 |
'!
|
|
36 |
|
|
37 |
!SmallInteger class methodsFor:'instance creation'!
|
|
38 |
|
|
39 |
basicNew
|
|
40 |
"catch instance creation
|
|
41 |
- SmallIntegers cannot be created with new"
|
|
42 |
|
|
43 |
self error:'instances of SmallInteger cannot be created with new'
|
|
44 |
!
|
|
45 |
|
|
46 |
basicNew:size
|
|
47 |
"catch instance creation
|
|
48 |
- SmallIntegers cannot be created with new"
|
|
49 |
|
|
50 |
self error:'instances of SmallInteger cannot be created with new'
|
|
51 |
! !
|
|
52 |
|
|
53 |
!SmallInteger class methodsFor:'constants'!
|
|
54 |
|
|
55 |
maxBits
|
|
56 |
"return the number of bits in instances of me"
|
|
57 |
|
|
58 |
%{ /* NOCONTEXT */
|
|
59 |
RETURN ( _MKSMALLINT(N_INT_BITS) );
|
|
60 |
%}
|
2
|
61 |
|
|
62 |
"SmallInteger maxBits"
|
1
|
63 |
!
|
|
64 |
|
|
65 |
maxBytes
|
|
66 |
"return the number of bytes in instances of me"
|
|
67 |
|
|
68 |
%{ /* NOCONTEXT */
|
|
69 |
RETURN ( _MKSMALLINT(N_INT_BITS / 8 + 1) );
|
|
70 |
%}
|
2
|
71 |
|
|
72 |
"SmallInteger maxBytes"
|
1
|
73 |
!
|
|
74 |
|
|
75 |
minVal
|
|
76 |
"return the smallest Integer representable as SmallInteger"
|
|
77 |
|
|
78 |
%{ /* NOCONTEXT */
|
|
79 |
RETURN ( _MKSMALLINT(_MIN_INT) );
|
|
80 |
%}
|
2
|
81 |
|
|
82 |
"SmallInteger minVal"
|
1
|
83 |
!
|
|
84 |
|
|
85 |
maxVal
|
|
86 |
"return the largest Integer representable as SmallInteger"
|
|
87 |
|
|
88 |
%{ /* NOCONTEXT */
|
|
89 |
RETURN ( _MKSMALLINT(_MAX_INT) );
|
|
90 |
%}
|
2
|
91 |
|
|
92 |
"SmallInteger maxVal"
|
1
|
93 |
! !
|
|
94 |
|
3
|
95 |
!SmallInteger class methodsFor:'queries'!
|
|
96 |
|
|
97 |
isBuiltInClass
|
|
98 |
"this class is known by the run-time-system"
|
|
99 |
|
|
100 |
^ true
|
|
101 |
! !
|
|
102 |
|
1
|
103 |
!SmallInteger methodsFor:'error catching'!
|
|
104 |
|
|
105 |
at:index
|
|
106 |
"catch indexed access - report an error
|
|
107 |
defined here since at: in Object ommits the SmallInteger check"
|
|
108 |
|
|
109 |
self notIndexed
|
|
110 |
!
|
|
111 |
|
|
112 |
basicAt:index
|
|
113 |
"catch indexed access - report an error
|
|
114 |
defined here since basicAt: in Object ommits the SmallInteger check"
|
|
115 |
|
|
116 |
self notIndexed
|
|
117 |
!
|
|
118 |
|
|
119 |
at:index put:anObject
|
|
120 |
"catch indexed access - report an error
|
|
121 |
defined here since at:put: in Object ommits the SmallInteger check"
|
|
122 |
|
|
123 |
self notIndexed
|
|
124 |
!
|
|
125 |
|
|
126 |
basicAt:index put:anObject
|
|
127 |
"catch indexed access - report an error
|
|
128 |
defined here since basicAt:put: in Object ommits the SmallInteger check"
|
|
129 |
|
|
130 |
self notIndexed
|
|
131 |
!
|
|
132 |
|
|
133 |
size
|
|
134 |
"return the number of indexed instvars - SmallIntegers have none
|
|
135 |
defined here since size in Object ommits the SmallInteger check"
|
|
136 |
|
|
137 |
^ 0
|
|
138 |
!
|
|
139 |
|
|
140 |
basicSize
|
|
141 |
"return the number of indexed instvars - SmallIntegers have none
|
|
142 |
defined here since basicSize in Object ommits the SmallInteger check"
|
|
143 |
|
|
144 |
^ 0
|
|
145 |
! !
|
|
146 |
|
|
147 |
!SmallInteger methodsFor:'copying'!
|
|
148 |
|
|
149 |
shallowCopy
|
|
150 |
"return a shallow copy of myself
|
2
|
151 |
- reimplemented here since smallintegers are unique"
|
1
|
152 |
|
|
153 |
^ self
|
|
154 |
!
|
|
155 |
|
13
|
156 |
simpleDeepCopy
|
|
157 |
"return a deep copy of myself
|
|
158 |
- reimplemented here since smallintegers are unique"
|
|
159 |
|
|
160 |
^ self
|
|
161 |
!
|
|
162 |
|
|
163 |
deepCopyUsing:aDictionary
|
|
164 |
"return a deep copy of myself
|
|
165 |
- reimplemented here since smallintegers are unique"
|
|
166 |
|
|
167 |
^ self
|
|
168 |
!
|
|
169 |
|
1
|
170 |
deepCopy
|
|
171 |
"return a deep copy of myself
|
2
|
172 |
- reimplemented here since smallintegers are unique"
|
1
|
173 |
|
|
174 |
^ self
|
|
175 |
! !
|
|
176 |
|
|
177 |
!SmallInteger methodsFor:'comparing'!
|
|
178 |
|
|
179 |
= aNumber
|
|
180 |
"return true, if the arguments value is equal to mine"
|
|
181 |
|
|
182 |
%{ /* NOCONTEXT */
|
|
183 |
|
|
184 |
if (aNumber == self) {
|
|
185 |
RETURN ( true );
|
|
186 |
}
|
|
187 |
if (! _isNonNilObject(aNumber)) {
|
|
188 |
RETURN ( false );
|
|
189 |
}
|
|
190 |
|
|
191 |
if (_qClass(aNumber) == Float) {
|
|
192 |
RETURN ( ((double)_intVal(self) == _floatVal(aNumber)) ? true : false );
|
|
193 |
}
|
|
194 |
%}
|
|
195 |
.
|
|
196 |
aNumber respondsToArithmetic ifFalse:[^ false].
|
|
197 |
^ self retry:#= coercing:aNumber
|
|
198 |
!
|
|
199 |
|
|
200 |
~= aNumber
|
|
201 |
"return true, if the arguments value is not equal to mine"
|
|
202 |
|
|
203 |
%{ /* NOCONTEXT */
|
|
204 |
|
|
205 |
if (aNumber == self) {
|
|
206 |
RETURN ( false );
|
|
207 |
}
|
|
208 |
if (! _isNonNilObject(aNumber)) {
|
|
209 |
RETURN ( true );
|
|
210 |
}
|
|
211 |
|
|
212 |
if (_qClass(aNumber) == Float) {
|
|
213 |
RETURN ( ((double)_intVal(self) == _floatVal(aNumber)) ? false : true );
|
|
214 |
}
|
|
215 |
%}
|
|
216 |
.
|
|
217 |
aNumber respondsToArithmetic ifFalse:[^ true].
|
|
218 |
^ self retry:#~= coercing:aNumber
|
|
219 |
!
|
|
220 |
|
|
221 |
< aNumber
|
|
222 |
"return true, if the argument is greater than the receiver"
|
|
223 |
|
|
224 |
%{ /* NOCONTEXT */
|
|
225 |
|
|
226 |
if (_isSmallInteger(aNumber)) {
|
|
227 |
#ifdef POSITIVE_ADDRESSES
|
|
228 |
RETURN ( (_intVal(self) < _intVal(aNumber)) ? true : false );
|
|
229 |
#else
|
|
230 |
/* tag bit does not change ordering */
|
|
231 |
RETURN ( ((INT)self < (INT)aNumber) ? true : false );
|
|
232 |
#endif
|
|
233 |
}
|
|
234 |
if (_isFloat(aNumber)) {
|
|
235 |
RETURN ( ((double)_intVal(self) < _floatVal(aNumber)) ? true : false );
|
|
236 |
}
|
|
237 |
%}
|
|
238 |
.
|
|
239 |
^ aNumber lessFromInteger:self
|
|
240 |
"^ self retry:#< coercing:aNumber"
|
|
241 |
!
|
|
242 |
|
|
243 |
> aNumber
|
|
244 |
"return true, if the argument is less than the receiver"
|
|
245 |
|
|
246 |
%{ /* NOCONTEXT */
|
|
247 |
|
|
248 |
if (_isSmallInteger(aNumber)) {
|
|
249 |
#ifdef POSITIVE_ADDRESSES
|
|
250 |
RETURN ( (_intVal(self) > _intVal(aNumber)) ? true : false );
|
|
251 |
#else
|
|
252 |
/* tag bit does not change ordering */
|
|
253 |
RETURN ( ((INT)self > (INT)aNumber) ? true : false );
|
|
254 |
#endif
|
|
255 |
}
|
|
256 |
if (_isFloat(aNumber)) {
|
|
257 |
RETURN ( ((double)_intVal(self) > _floatVal(aNumber)) ? true : false );
|
|
258 |
}
|
|
259 |
%}
|
|
260 |
.
|
|
261 |
^ self retry:#> coercing:aNumber
|
|
262 |
!
|
|
263 |
|
|
264 |
>= aNumber
|
|
265 |
"return true, if the argument is less or equal"
|
|
266 |
|
|
267 |
%{ /* NOCONTEXT */
|
|
268 |
|
|
269 |
if (_isSmallInteger(aNumber)) {
|
|
270 |
#ifdef POSITIVE_ADDRESSES
|
|
271 |
RETURN ( (_intVal(self) >= _intVal(aNumber)) ? true : false );
|
|
272 |
#else
|
|
273 |
/* tag bit does not change ordering */
|
|
274 |
RETURN ( ((INT)self >= (INT)aNumber) ? true : false );
|
|
275 |
#endif
|
|
276 |
}
|
|
277 |
if (_isFloat(aNumber)) {
|
|
278 |
RETURN ( ((double)_intVal(self) >= _floatVal(aNumber)) ? true : false );
|
|
279 |
}
|
|
280 |
%}
|
|
281 |
.
|
|
282 |
^ self retry:#>= coercing:aNumber
|
|
283 |
!
|
|
284 |
|
|
285 |
<= aNumber
|
|
286 |
"return true, if the argument is greater or equal"
|
|
287 |
|
|
288 |
%{ /* NOCONTEXT */
|
|
289 |
|
|
290 |
if (_isSmallInteger(aNumber)) {
|
|
291 |
#ifdef POSITIVE_ADDRESSES
|
|
292 |
RETURN ( (_intVal(self) <= _intVal(aNumber)) ? true : false );
|
|
293 |
#else
|
|
294 |
/* tag bit does not change ordering */
|
|
295 |
RETURN ( ((INT)self <= (INT)aNumber) ? true : false );
|
|
296 |
#endif
|
|
297 |
}
|
|
298 |
if (_isFloat(aNumber)) {
|
|
299 |
RETURN ( ((double)_intVal(self) <= _floatVal(aNumber)) ? true : false );
|
|
300 |
}
|
|
301 |
%}
|
|
302 |
.
|
|
303 |
^ self retry:#<= coercing:aNumber
|
|
304 |
!
|
|
305 |
|
|
306 |
identityHash
|
|
307 |
"return an integer useful for hashing on identity"
|
|
308 |
|
13
|
309 |
self >= 0 ifTrue:[
|
1
|
310 |
^ self + 8192
|
|
311 |
].
|
|
312 |
^ self negated + 8192
|
|
313 |
!
|
|
314 |
|
|
315 |
min:aNumber
|
|
316 |
"return the receiver or the argument, whichever is smaller"
|
|
317 |
|
|
318 |
%{ /* NOCONTEXT */
|
|
319 |
|
|
320 |
if (_isSmallInteger(aNumber)) {
|
|
321 |
#ifdef POSITIVE_ADDRESSES
|
|
322 |
if (_intVal(self) < _intVal(aNumber)) {
|
|
323 |
#else
|
|
324 |
/* tag bit does not change ordering */
|
|
325 |
if ((INT)(self) < (INT)(aNumber)) {
|
|
326 |
#endif
|
|
327 |
RETURN ( self );
|
|
328 |
}
|
|
329 |
RETURN ( aNumber );
|
|
330 |
}
|
|
331 |
if (_isFloat(aNumber)) {
|
|
332 |
if ( (double)_intVal(self) < _floatVal(aNumber) ) {
|
|
333 |
RETURN ( self );
|
|
334 |
}
|
|
335 |
RETURN ( aNumber );
|
|
336 |
}
|
|
337 |
%}
|
|
338 |
.
|
|
339 |
(self < aNumber) ifTrue:[^ self].
|
|
340 |
^ aNumber
|
|
341 |
!
|
|
342 |
|
|
343 |
max:aNumber
|
|
344 |
"return the receiver or the argument, whichever is greater"
|
|
345 |
|
|
346 |
%{ /* NOCONTEXT */
|
|
347 |
|
|
348 |
if (_isSmallInteger(aNumber)) {
|
|
349 |
#ifdef POSITIVE_ADDRESSES
|
|
350 |
if (_intVal(self) > _intVal(aNumber)) {
|
|
351 |
#else
|
|
352 |
/* tag bit does not change ordering */
|
|
353 |
if ((INT)(self) > (INT)(aNumber)) {
|
|
354 |
#endif
|
|
355 |
RETURN ( self );
|
|
356 |
}
|
|
357 |
RETURN ( aNumber );
|
|
358 |
}
|
|
359 |
if (_isFloat(aNumber)) {
|
|
360 |
if ( (double)_intVal(self) > _floatVal(aNumber) ) {
|
|
361 |
RETURN ( self );
|
|
362 |
}
|
|
363 |
RETURN ( aNumber );
|
|
364 |
}
|
|
365 |
%}
|
|
366 |
.
|
|
367 |
(self > aNumber) ifTrue:[^ self].
|
|
368 |
^ aNumber
|
|
369 |
! !
|
|
370 |
|
|
371 |
!SmallInteger methodsFor:'testing'!
|
|
372 |
|
|
373 |
negative
|
|
374 |
"return true, if the receiver is less than zero
|
|
375 |
reimplemented here for speed"
|
|
376 |
|
|
377 |
%{ /* NOCONTEXT */
|
|
378 |
|
|
379 |
#ifdef POSITIVE_ADDRESSES
|
|
380 |
RETURN ( (_intVal(self) < 0) ? true : false );
|
|
381 |
#else
|
|
382 |
/* tag bit does not change sign */
|
|
383 |
RETURN ( ((INT)(self) < 0) ? true : false );
|
|
384 |
#endif
|
|
385 |
%}
|
|
386 |
!
|
|
387 |
|
|
388 |
positive
|
|
389 |
"return true, if the receiver is not negative
|
|
390 |
reimplemented here for speed"
|
|
391 |
|
|
392 |
%{ /* NOCONTEXT */
|
|
393 |
|
|
394 |
#ifdef POSITIVE_ADDRESSES
|
|
395 |
RETURN ( (_intVal(self) >= 0) ? true : false );
|
|
396 |
#else
|
|
397 |
/* tag bit does not change sign */
|
|
398 |
RETURN ( ((INT)(self) >= 0) ? true : false );
|
|
399 |
#endif
|
|
400 |
%}
|
|
401 |
!
|
|
402 |
|
|
403 |
strictlyPositive
|
|
404 |
"return true, if the receiver is greater than zero
|
|
405 |
reimplemented here for speed"
|
|
406 |
|
|
407 |
%{ /* NOCONTEXT */
|
|
408 |
|
|
409 |
#ifdef POSITIVE_ADDRESSES
|
|
410 |
RETURN ( (_intVal(self) > 0) ? true : false );
|
|
411 |
#else
|
|
412 |
/* tag bit does not change sign */
|
|
413 |
RETURN ( ((INT)(self) > 0) ? true : false );
|
|
414 |
#endif
|
|
415 |
%}
|
|
416 |
!
|
|
417 |
|
|
418 |
sign
|
|
419 |
"return the sign of the receiver
|
|
420 |
reimplemented here for speed"
|
|
421 |
|
|
422 |
%{ /* NOCONTEXT */
|
|
423 |
|
|
424 |
INT val = _intVal(self);
|
|
425 |
|
|
426 |
if (val < 0) {
|
|
427 |
RETURN ( _MKSMALLINT(-1) );
|
|
428 |
}
|
|
429 |
if (val > 0) {
|
|
430 |
RETURN ( _MKSMALLINT(1) );
|
|
431 |
}
|
|
432 |
RETURN ( _MKSMALLINT(0) );
|
|
433 |
%}
|
|
434 |
!
|
|
435 |
|
|
436 |
between:min and:max
|
|
437 |
"return true if the receiver is less than or equal to the argument max
|
|
438 |
and greater than or equal to the argument min.
|
|
439 |
- reimplemented here for speed"
|
|
440 |
|
|
441 |
%{ /* NOCONTEXT */
|
|
442 |
|
|
443 |
if (_isSmallInteger(min) && _isSmallInteger(max)) {
|
2
|
444 |
REGISTER INT selfVal;
|
1
|
445 |
|
2
|
446 |
selfVal = _intVal(self);
|
1
|
447 |
if (selfVal < _intVal(min)) {
|
|
448 |
RETURN ( false );
|
|
449 |
}
|
|
450 |
if (selfVal > _intVal(max)) {
|
|
451 |
RETURN ( false );
|
|
452 |
}
|
|
453 |
RETURN ( true );
|
|
454 |
}
|
|
455 |
%}
|
|
456 |
.
|
|
457 |
(self < min) ifTrue:[^ false].
|
|
458 |
(self > max) ifTrue:[^ false].
|
|
459 |
^ true
|
|
460 |
!
|
|
461 |
|
|
462 |
even
|
|
463 |
"return true, if the receiver is even"
|
|
464 |
|
|
465 |
%{ /* NOCONTEXT */
|
|
466 |
|
|
467 |
#ifdef POSITIVE_ADDRESSES
|
|
468 |
RETURN ( ((INT)self & 1) ? false : true );
|
|
469 |
#else
|
|
470 |
RETURN ( ((INT)self & ((INT)_MKSMALLINT(1) & ~TAG_INT)) ? false : true );
|
|
471 |
#endif
|
|
472 |
%}
|
|
473 |
!
|
|
474 |
|
|
475 |
odd
|
|
476 |
"return true, if the receiver is odd"
|
|
477 |
|
|
478 |
%{ /* NOCONTEXT */
|
|
479 |
|
|
480 |
#ifdef POSITIVE_ADDRESSES
|
|
481 |
RETURN ( ((INT)self & 1) ? true : false );
|
|
482 |
#else
|
|
483 |
RETURN ( ((INT)self & ((INT)_MKSMALLINT(1) & ~TAG_INT)) ? true : false );
|
|
484 |
#endif
|
|
485 |
%}
|
|
486 |
! !
|
|
487 |
|
|
488 |
!SmallInteger methodsFor:'arithmetic'!
|
|
489 |
|
|
490 |
+ aNumber
|
|
491 |
"return the sum of the receivers value and the arguments value"
|
|
492 |
|
|
493 |
%{ /* NOCONTEXT */
|
|
494 |
|
|
495 |
if (_isSmallInteger(aNumber)) {
|
|
496 |
#ifdef _ADD_IO_IO
|
2
|
497 |
RETURN ( _ADD_IO_IO(self, aNumber) );
|
1
|
498 |
#else
|
|
499 |
REGISTER INT sum;
|
2
|
500 |
extern OBJ _makeLarge();
|
1
|
501 |
|
|
502 |
sum = _intVal(self) + _intVal(aNumber);
|
|
503 |
if ((sum >= _MIN_INT) && (sum <= _MAX_INT)) {
|
|
504 |
RETURN ( _MKSMALLINT(sum) );
|
|
505 |
}
|
2
|
506 |
RETURN ( _makeLarge(sum) );
|
1
|
507 |
#endif
|
|
508 |
}
|
|
509 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
510 |
extern char *newNextPtr, *newEndPtr;
|
|
511 |
OBJ newFloat;
|
2
|
512 |
double val;
|
1
|
513 |
|
2
|
514 |
val = _floatVal(aNumber);
|
1
|
515 |
_qAlignedNew(newFloat, sizeof(struct floatstruct), SENDER);
|
|
516 |
_InstPtr(newFloat)->o_class = Float;
|
|
517 |
_FloatInstPtr(newFloat)->f_floatvalue = (double)(_intVal(self)) + val;
|
|
518 |
RETURN ( newFloat );
|
|
519 |
}
|
|
520 |
%}
|
|
521 |
.
|
|
522 |
^ aNumber sumFromInteger:self
|
|
523 |
!
|
|
524 |
|
|
525 |
- aNumber
|
|
526 |
"return the difference of the receivers value and the arguments value"
|
|
527 |
|
|
528 |
%{ /* NOCONTEXT */
|
|
529 |
|
|
530 |
if (_isSmallInteger(aNumber)) {
|
|
531 |
#ifdef _SUB_IO_IO
|
2
|
532 |
RETURN ( _SUB_IO_IO(self, aNumber) );
|
1
|
533 |
#else
|
|
534 |
REGISTER INT diff;
|
2
|
535 |
extern OBJ _makeLarge();
|
1
|
536 |
|
|
537 |
diff = _intVal(self) - _intVal(aNumber);
|
|
538 |
if ((diff >= _MIN_INT) && (diff <= _MAX_INT)) {
|
|
539 |
RETURN ( _MKSMALLINT(diff) );
|
|
540 |
}
|
2
|
541 |
RETURN ( _makeLarge(diff) );
|
1
|
542 |
#endif
|
|
543 |
}
|
|
544 |
if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
545 |
extern char *newNextPtr, *newEndPtr;
|
|
546 |
OBJ newFloat;
|
2
|
547 |
double val;
|
1
|
548 |
|
2
|
549 |
val = _floatVal(aNumber);
|
1
|
550 |
_qAlignedNew(newFloat, sizeof(struct floatstruct), SENDER);
|
|
551 |
_InstPtr(newFloat)->o_class = Float;
|
|
552 |
_FloatInstPtr(newFloat)->f_floatvalue = (double)(_intVal(self)) - val;
|
|
553 |
RETURN ( newFloat );
|
|
554 |
}
|
|
555 |
%}
|
|
556 |
.
|
|
557 |
^ aNumber differenceFromInteger:self
|
|
558 |
!
|
|
559 |
|
|
560 |
* aNumber
|
|
561 |
"return the product of the receivers value and the arguments value"
|
|
562 |
|
|
563 |
|aLarge|
|
|
564 |
|
|
565 |
%{ /* NOCONTEXT */
|
|
566 |
|
|
567 |
REGISTER INT myValue, otherValue;
|
|
568 |
unsigned INT pHH, pHL, pLH, pLL;
|
|
569 |
|
|
570 |
if (_isSmallInteger(aNumber)) {
|
13
|
571 |
myValue = _intVal(self);
|
|
572 |
otherValue = _intVal(aNumber);
|
44
|
573 |
#if defined(NOTDEF) && defined(__GNUC__) && (__GNUC__ >= 2)
|
41
|
574 |
{
|
|
575 |
long long product;
|
13
|
576 |
|
41
|
577 |
product = myValue * otherValue;
|
|
578 |
if ((product >= (long long)_MIN_INT)
|
|
579 |
&& (product <= (long long)_MAX_INT)) {
|
|
580 |
RETURN ( _MKSMALLINT((int)product) );
|
|
581 |
}
|
|
582 |
}
|
13
|
583 |
#else
|
1
|
584 |
/* this is too slow:
|
|
585 |
* since most machines can do 32*32 to 64 bit multiply,
|
|
586 |
* (or at least 32*32 with Overflow check)
|
|
587 |
* its better to do it this way .. - need an assembler (inline) function here
|
|
588 |
*/
|
|
589 |
if (myValue < 0) myValue = -myValue;
|
|
590 |
if (otherValue < 0) otherValue = -otherValue;
|
|
591 |
pHH = ((myValue >> 16) & 0xFFFF) * ((otherValue >> 16) & 0xFFFF);
|
|
592 |
pHL = ((myValue >> 16) & 0xFFFF) * (otherValue & 0xFFFF);
|
|
593 |
pLH = (myValue & 0xFFFF) * ((otherValue >> 16) & 0xFFFF);
|
|
594 |
pLL = (myValue & 0xFFFF) * (otherValue & 0xFFFF);
|
|
595 |
if (! (pHH || (pHL & 0xFFFFc000) || (pLH & 0xFFFFc000) || (pLL & 0xc0000000))) {
|
|
596 |
RETURN ( _MKSMALLINT(_intVal(self) * _intVal(aNumber)) );
|
|
597 |
}
|
13
|
598 |
#endif
|
1
|
599 |
} else if ((aNumber != nil) && (_qClass(aNumber) == Float)) {
|
|
600 |
extern char *newNextPtr, *newEndPtr;
|
|
601 |
OBJ newFloat;
|
2
|
602 |
double val;
|
1
|
603 |
|
2
|
604 |
val = _floatVal(aNumber);
|
1
|
605 |
_qAlignedNew(newFloat, sizeof(struct floatstruct), SENDER);
|
|
606 |
_InstPtr(newFloat)->o_class = Float;
|
|
607 |
_FloatInstPtr(newFloat)->f_floatvalue = (double)(_intVal(self)) * val;
|
|
608 |
RETURN ( newFloat );
|
|
609 |
}
|
|
610 |
%}
|
|
611 |
.
|
|
612 |
%{
|
|
613 |
extern OBJ LargeInteger, __mu, _value_;
|
|
614 |
static struct inlineCache val = _ILC1;
|
|
615 |
static struct inlineCache mu = _ILC1;
|
|
616 |
|
|
617 |
if (_isSmallInteger(aNumber)) {
|
|
618 |
/*
|
|
619 |
* non overflow case has already been checked
|
|
620 |
*/
|
|
621 |
aLarge = (*val.ilc_func)(LargeInteger, _value_, CON_COMMA nil, &val, self);
|
|
622 |
RETURN ( (*mu.ilc_func)(aLarge, __mu, CON_COMMA nil, &mu, aNumber) );
|
|
623 |
}
|
|
624 |
%}
|
|
625 |
.
|
|
626 |
^ aNumber productFromInteger:self
|
|
627 |
!
|
|
628 |
|
|
629 |
/ aNumber
|
|
630 |
"return the quotient of the receivers value and the arguments value"
|
|
631 |
|
|
632 |
%{ /* NOCONTEXT */
|
|
633 |
|
|
634 |
INT me, t, val;
|
|
635 |
double dval;
|
|
636 |
|
|
637 |
if (_isSmallInteger(aNumber)) {
|
|
638 |
val = _intVal(aNumber);
|
|
639 |
if (val != 0) {
|
|
640 |
me = _intVal(self);
|
|
641 |
t = me / val;
|
|
642 |
#ifdef GOOD_OPTIMIZER
|
|
643 |
if (me % val) {
|
|
644 |
#else
|
|
645 |
/* this is stupid - all I want is to look for a remainder ...
|
|
646 |
but most compilers are too stupid and generate an extra mod instr.
|
|
647 |
for "if (me % val)" even if most div instructions also compute
|
|
648 |
the remainder.
|
|
649 |
therefore I use a multiplication which is faster than a modulu
|
|
650 |
on most machines
|
|
651 |
*/
|
|
652 |
if ((t * val) == me) {
|
|
653 |
#endif
|
|
654 |
RETURN ( _MKSMALLINT(t) );
|
|
655 |
}
|
|
656 |
/*
|
|
657 |
* now disabled - Fractions work
|
|
658 |
*
|
|
659 |
RETURN ( _MKFLOAT((double)_intVal(self) / (double)val, __context) );
|
|
660 |
*/
|
|
661 |
}
|
|
662 |
} else {
|
|
663 |
if (_isFloat(aNumber)) {
|
|
664 |
dval = _floatVal(aNumber);
|
|
665 |
if (dval != 0.0) {
|
|
666 |
me = _intVal(self);
|
|
667 |
RETURN ( _MKFLOAT((double)me / dval COMMA_CON) );
|
|
668 |
}
|
3
|
669 |
}
|
1
|
670 |
}
|
|
671 |
%}
|
|
672 |
.
|
|
673 |
aNumber isInteger ifTrue:[
|
|
674 |
aNumber = 0 ifTrue:[
|
|
675 |
DivisionByZeroSignal raise.
|
|
676 |
^ self
|
|
677 |
].
|
|
678 |
^ Fraction numerator:self denominator:aNumber
|
|
679 |
].
|
|
680 |
^ aNumber quotientFromInteger:self
|
|
681 |
!
|
|
682 |
|
2
|
683 |
// aNumber
|
1
|
684 |
"return the integer part of the quotient of the receivers value
|
|
685 |
and the arguments value"
|
|
686 |
|
|
687 |
%{ /* NOCONTEXT */
|
|
688 |
INT val;
|
|
689 |
|
2
|
690 |
if (_isSmallInteger(aNumber)) {
|
|
691 |
val = _intVal(aNumber);
|
1
|
692 |
if (val != 0) {
|
|
693 |
RETURN ( _MKSMALLINT(_intVal(self) / val) );
|
|
694 |
}
|
2
|
695 |
} else {
|
3
|
696 |
if (_isFraction(aNumber)) {
|
|
697 |
OBJ t;
|
|
698 |
INT num, den;
|
2
|
699 |
|
3
|
700 |
t = _FractionInstPtr(aNumber)->f_numerator;
|
|
701 |
if (_isSmallInteger(t)) {
|
|
702 |
num = _intVal(t);
|
|
703 |
t = _FractionInstPtr(aNumber)->f_denominator;
|
|
704 |
if (_isSmallInteger(t)) {
|
|
705 |
den = _intVal(t);
|
|
706 |
RETURN ( _MKSMALLINT(_intVal(self) * den / num ));
|
|
707 |
}
|
|
708 |
}
|
|
709 |
}
|
1
|
710 |
}
|
|
711 |
%}
|
|
712 |
.
|
2
|
713 |
(aNumber = 0) ifTrue:[
|
1
|
714 |
DivisionByZeroSignal raise.
|
|
715 |
^ self
|
|
716 |
].
|
2
|
717 |
^ self retry:#// coercing:aNumber
|
1
|
718 |
!
|
|
719 |
|
2
|
720 |
\\ aNumber
|
1
|
721 |
"return the integer rest of the receivers value
|
|
722 |
divided by the arguments value"
|
|
723 |
|
|
724 |
%{ /* NOCONTEXT */
|
|
725 |
INT mySelf, val;
|
|
726 |
|
2
|
727 |
if (_isSmallInteger(aNumber)) {
|
1
|
728 |
mySelf = _intVal(self);
|
|
729 |
if (mySelf < 0) mySelf = -mySelf;
|
2
|
730 |
val = _intVal(aNumber);
|
1
|
731 |
if (val != 0) {
|
|
732 |
if (val < 0) {
|
|
733 |
RETURN ( _MKSMALLINT(-(mySelf % -val)) );
|
|
734 |
}
|
|
735 |
RETURN ( _MKSMALLINT(mySelf % val) );
|
|
736 |
}
|
|
737 |
}
|
|
738 |
%}
|
|
739 |
.
|
2
|
740 |
(aNumber = 0) ifTrue:[
|
1
|
741 |
DivisionByZeroSignal raise.
|
|
742 |
^ self
|
|
743 |
].
|
2
|
744 |
^ self retry:#\\ coercing:aNumber
|
1
|
745 |
!
|
|
746 |
|
|
747 |
abs
|
|
748 |
"return the absolute value of the receiver
|
|
749 |
reimplemented here for speed"
|
|
750 |
|
|
751 |
%{ /* NOCONTEXT */
|
|
752 |
|
|
753 |
INT val = _intVal(self);
|
|
754 |
|
|
755 |
if (val != _MIN_INT) {
|
|
756 |
RETURN ( (val < 0) ? _MKSMALLINT(-val) : self );
|
|
757 |
}
|
|
758 |
%}
|
|
759 |
.
|
|
760 |
"only reached for minVal"
|
|
761 |
^ self negated
|
|
762 |
!
|
|
763 |
|
|
764 |
negated
|
|
765 |
"return the negative value of the receiver
|
|
766 |
reimplemented here for speed"
|
|
767 |
|
|
768 |
%{ /* NOCONTEXT */
|
|
769 |
|
|
770 |
INT val = _intVal(self);
|
|
771 |
|
|
772 |
if (val != _MIN_INT) {
|
|
773 |
RETURN ( _MKSMALLINT(- val) );
|
|
774 |
}
|
|
775 |
%}
|
|
776 |
.
|
|
777 |
^ (LargeInteger value:(SmallInteger maxVal)) + 1
|
|
778 |
! !
|
|
779 |
|
|
780 |
!SmallInteger methodsFor:'modulu arithmetic'!
|
|
781 |
|
|
782 |
times:aNumber
|
|
783 |
"return the product of the receiver and the argument as SmallInteger.
|
|
784 |
If the result overflows integer range the value modulu the SmallInteger
|
|
785 |
range is returned.
|
|
786 |
This is of course not always correct, but some code does a modulu anyway
|
|
787 |
and can therefore speed things up by not going through LargeIntegers."
|
|
788 |
|
|
789 |
%{ /* NOCONTEXT */
|
|
790 |
|
|
791 |
if (_isSmallInteger(aNumber)) {
|
|
792 |
RETURN ( _MKSMALLINT((_intVal(self) * _intVal(aNumber)) & 0x7FFFFFFF) );
|
|
793 |
}
|
|
794 |
%}
|
|
795 |
.
|
|
796 |
self primitiveFailed
|
|
797 |
!
|
|
798 |
|
|
799 |
plus:aNumber
|
|
800 |
"return the sum of the receiver and the argument as SmallInteger.
|
|
801 |
If the result overflows integer range, the value modulu the SmallInteger
|
|
802 |
range is returned.
|
|
803 |
This is of course not always correct, but some code does a modulu anyway
|
|
804 |
and can therefore speed things up by not going through LargeIntegers."
|
|
805 |
|
|
806 |
%{ /* NOCONTEXT */
|
|
807 |
|
|
808 |
if (_isSmallInteger(aNumber)) {
|
|
809 |
RETURN ( _MKSMALLINT((_intVal(self) + _intVal(aNumber)) & 0x7FFFFFFF) );
|
|
810 |
}
|
|
811 |
%}
|
|
812 |
.
|
|
813 |
self primitiveFailed
|
|
814 |
! !
|
|
815 |
|
|
816 |
!SmallInteger class methodsFor:'bit mask constants'!
|
|
817 |
|
|
818 |
bitMaskFor:index
|
|
819 |
"return a bitmask for the index's bit (index starts at 1)"
|
|
820 |
|
|
821 |
(index between:1 and:SmallInteger maxBits) ifFalse:[
|
|
822 |
^ self error:'index out of bounds'
|
|
823 |
].
|
|
824 |
^ 1 bitShift:(index - 1)
|
|
825 |
! !
|
|
826 |
|
|
827 |
!SmallInteger methodsFor:'bit operators'!
|
|
828 |
|
|
829 |
bitAt:index
|
|
830 |
"return the value of the index's bit (index starts at 1)"
|
|
831 |
|
|
832 |
|mask|
|
|
833 |
|
|
834 |
(index between:1 and:SmallInteger maxBits) ifFalse:[
|
|
835 |
^ self error:'index out of bounds'
|
|
836 |
].
|
|
837 |
mask := 1 bitShift:(index - 1).
|
|
838 |
((self bitAnd:mask) == 0) ifTrue:[^ 0].
|
|
839 |
^ 1
|
|
840 |
!
|
|
841 |
|
|
842 |
allMask:anInteger
|
|
843 |
"True if all bits in anInteger are 1 in the receiver"
|
|
844 |
|
|
845 |
^(self bitAnd:anInteger) == anInteger
|
|
846 |
!
|
|
847 |
|
|
848 |
anyMask:anInteger
|
|
849 |
"True if any 1 bits in anInteger are 1 in the receiver"
|
|
850 |
|
|
851 |
^(self bitAnd:anInteger) ~~ 0
|
|
852 |
!
|
|
853 |
|
|
854 |
noMask:anInteger
|
|
855 |
"True if no 1 bits in anInteger are 1 in the receiver"
|
|
856 |
|
|
857 |
^(self bitAnd:anInteger) == 0
|
|
858 |
!
|
|
859 |
|
|
860 |
highBit
|
|
861 |
"return the bitIndex of the highest bit set"
|
|
862 |
|
|
863 |
%{ /* NOCONTEXT */
|
|
864 |
|
|
865 |
INT mask, index, bits;
|
|
866 |
|
|
867 |
bits = _intVal(self);
|
|
868 |
if (bits == 0) {
|
|
869 |
RETURN ( _MKSMALLINT(-1) );
|
|
870 |
}
|
|
871 |
#ifdef alpha
|
|
872 |
mask = 0x2000000000000000;
|
|
873 |
index = 62;
|
|
874 |
#else
|
|
875 |
mask = 0x20000000;
|
|
876 |
index = 30;
|
|
877 |
#endif
|
|
878 |
while (index) {
|
|
879 |
if (bits & mask) break;
|
|
880 |
mask = mask >> 1;
|
|
881 |
index--;
|
|
882 |
}
|
|
883 |
RETURN ( _MKSMALLINT(index) );
|
|
884 |
%}
|
|
885 |
!
|
|
886 |
|
|
887 |
lowBit
|
|
888 |
"return the bitIndex of the lowest bit set"
|
|
889 |
%{ /* NOCONTEXT */
|
|
890 |
|
|
891 |
INT mask, index, bits;
|
|
892 |
|
|
893 |
bits = _intVal(self);
|
|
894 |
if (bits == 0) {
|
|
895 |
RETURN ( _MKSMALLINT(-1) );
|
|
896 |
}
|
|
897 |
mask = 1;
|
|
898 |
index = 1;
|
|
899 |
#ifdef alpha
|
|
900 |
while (index != 63) {
|
|
901 |
#else
|
|
902 |
while (index != 31) {
|
|
903 |
#endif
|
|
904 |
if (bits & mask) {
|
|
905 |
RETURN ( _MKSMALLINT(index) );
|
|
906 |
}
|
|
907 |
mask = mask << 1;
|
|
908 |
index++;
|
|
909 |
}
|
|
910 |
RETURN ( _MKSMALLINT(-1) );
|
|
911 |
/* notreached */
|
|
912 |
%}
|
|
913 |
!
|
|
914 |
|
|
915 |
bitShift:shiftCount
|
|
916 |
"return the value of the receiver shifted by shiftCount bits;
|
|
917 |
leftShift if shiftCount > 0; rightShift otherwise"
|
|
918 |
|
|
919 |
%{ /* NOCONTEXT */
|
|
920 |
|
|
921 |
INT bits, count;
|
|
922 |
|
|
923 |
if (_isSmallInteger(shiftCount)) {
|
|
924 |
count = _intVal(shiftCount);
|
|
925 |
bits = _intVal(self);
|
|
926 |
if (count > 0) {
|
|
927 |
RETURN ( _MKSMALLINT(bits << count) );
|
|
928 |
}
|
|
929 |
if (count < 0) {
|
|
930 |
RETURN ( _MKSMALLINT(bits >> -count) );
|
|
931 |
}
|
|
932 |
RETURN (self );
|
|
933 |
}
|
|
934 |
%}
|
|
935 |
.
|
|
936 |
^ self bitShift:(shiftCount coerce:1)
|
|
937 |
!
|
|
938 |
|
|
939 |
bitOr:anInteger
|
13
|
940 |
"return the bitwise-or of the receiver and the argument, anInteger"
|
1
|
941 |
|
|
942 |
%{ /* NOCONTEXT */
|
|
943 |
|
|
944 |
/* oring the tags doesn't change it */
|
|
945 |
if (_isSmallInteger(anInteger)) {
|
|
946 |
RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) );
|
|
947 |
}
|
|
948 |
%}
|
|
949 |
.
|
|
950 |
^ self retry:#bitOr coercing:anInteger
|
|
951 |
!
|
|
952 |
|
|
953 |
bitAnd:anInteger
|
13
|
954 |
"return the bitwise-and of the receiver and the argument, anInteger"
|
1
|
955 |
|
|
956 |
%{ /* NOCONTEXT */
|
|
957 |
|
|
958 |
/* anding the tags doesn't change it */
|
|
959 |
if (_isSmallInteger(anInteger)) {
|
|
960 |
RETURN ( ((OBJ) ((INT)self & (INT)anInteger)) );
|
|
961 |
}
|
|
962 |
%}
|
|
963 |
.
|
|
964 |
^ self retry:#bitAnd coercing:anInteger
|
|
965 |
!
|
|
966 |
|
|
967 |
bitXor:anInteger
|
13
|
968 |
"return the bitwise-exclusive-or of the receiver and the argument, anInteger"
|
1
|
969 |
|
|
970 |
%{ /* NOCONTEXT */
|
|
971 |
|
|
972 |
/* xoring the tags turns it off - or it in again */
|
|
973 |
if (_isSmallInteger(anInteger)) {
|
|
974 |
RETURN ( (OBJ)( ((INT)self ^ (INT)anInteger) | TAG_INT) );
|
|
975 |
}
|
|
976 |
%}
|
|
977 |
.
|
|
978 |
^ self retry:#bitXor coercing:anInteger
|
|
979 |
!
|
|
980 |
|
|
981 |
bitInvert
|
|
982 |
"return the value of the receiver with all bits inverted"
|
|
983 |
|
|
984 |
%{ /* NOCONTEXT */
|
|
985 |
|
|
986 |
/* invert anything except tag bits */
|
|
987 |
RETURN ( ((OBJ) ((INT)self ^ ~TAG_MASK)) );
|
|
988 |
%}
|
|
989 |
!
|
|
990 |
|
|
991 |
bitTest:aMask
|
|
992 |
"return true, if any bit from aMask is set in the receiver"
|
|
993 |
|
|
994 |
%{ /* NOCONTEXT */
|
|
995 |
|
|
996 |
/* and all bits except tag */
|
|
997 |
if (_isSmallInteger(aMask)) {
|
|
998 |
RETURN ( ((INT)self & ((INT)aMask & ~TAG_MASK)) ? true : false );
|
|
999 |
}
|
|
1000 |
%}
|
|
1001 |
.
|
|
1002 |
^ self retry:#bitTest coercing:aMask
|
|
1003 |
! !
|
|
1004 |
|
|
1005 |
!SmallInteger methodsFor:'byte access'!
|
|
1006 |
|
|
1007 |
digitLength
|
|
1008 |
"return the number bytes used by this Integer"
|
|
1009 |
|
|
1010 |
^ self abs highBit - 1 // 8 + 1
|
|
1011 |
!
|
|
1012 |
|
|
1013 |
digitAt:index
|
|
1014 |
"return 8 bits of value, starting at byte index"
|
|
1015 |
|
|
1016 |
%{ /* NOCONTEXT */
|
|
1017 |
|
|
1018 |
INT val;
|
|
1019 |
|
|
1020 |
if (_isSmallInteger(index)) {
|
|
1021 |
val = _intVal(self);
|
|
1022 |
if (val < 0)
|
|
1023 |
val = -val;
|
|
1024 |
switch (_intVal(index)) {
|
|
1025 |
case 1:
|
|
1026 |
RETURN ( _MKSMALLINT( val & 0xFF) );
|
|
1027 |
case 2:
|
|
1028 |
RETURN ( _MKSMALLINT( (val >> 8) & 0xFF) );
|
|
1029 |
case 3:
|
|
1030 |
RETURN ( _MKSMALLINT( (val >> 16) & 0xFF) );
|
|
1031 |
case 4:
|
|
1032 |
RETURN ( _MKSMALLINT( (val >> 24) & 0xFF) );
|
|
1033 |
#ifdef alpha
|
|
1034 |
case 5:
|
|
1035 |
RETURN ( _MKSMALLINT( (val >> 32) & 0xFF) );
|
|
1036 |
case 6:
|
|
1037 |
RETURN ( _MKSMALLINT( (val >> 40) & 0xFF) );
|
|
1038 |
case 7:
|
|
1039 |
RETURN ( _MKSMALLINT( (val >> 48) & 0xFF) );
|
|
1040 |
case 8:
|
|
1041 |
RETURN ( _MKSMALLINT( (val >> 56) & 0xFF) );
|
|
1042 |
#endif
|
|
1043 |
}
|
|
1044 |
}
|
|
1045 |
%}
|
|
1046 |
.
|
|
1047 |
self primitiveFailed
|
|
1048 |
! !
|
|
1049 |
|
|
1050 |
!SmallInteger methodsFor:'misc math functions'!
|
|
1051 |
|
|
1052 |
gcd:anInteger
|
|
1053 |
"return the greatest common divisor (Euclid's algorithm).
|
|
1054 |
This has been redefined here for more speed since due to the
|
|
1055 |
use of gcd in Fraction code, it has become time-critical for
|
|
1056 |
some code. (thanx to MessageTally)"
|
|
1057 |
|
|
1058 |
%{ /* NOCONTEXT */
|
|
1059 |
|
|
1060 |
if (_isSmallInteger(anInteger)) {
|
|
1061 |
INT orgArg, ttt, selfInt, temp;
|
|
1062 |
|
|
1063 |
ttt = orgArg = _intVal(anInteger);
|
2
|
1064 |
if (ttt) {
|
1
|
1065 |
selfInt = _intVal(self);
|
|
1066 |
while (ttt != 0) {
|
|
1067 |
temp = selfInt % ttt;
|
|
1068 |
selfInt = ttt;
|
|
1069 |
ttt = temp;
|
|
1070 |
}
|
2
|
1071 |
/*
|
|
1072 |
* since its not defined in what the sign of
|
|
1073 |
* a modulu result is when the arg is negative,
|
|
1074 |
* change it explicitely here ...
|
|
1075 |
*/
|
|
1076 |
if (orgArg < 0) {
|
|
1077 |
/* result should be negative */
|
1
|
1078 |
if (selfInt > 0) selfInt = -selfInt;
|
2
|
1079 |
} else {
|
|
1080 |
/* result should be positive */
|
|
1081 |
if (selfInt < 0) selfInt = -selfInt;
|
|
1082 |
}
|
1
|
1083 |
RETURN ( _MKSMALLINT(selfInt) );
|
|
1084 |
}
|
|
1085 |
}
|
|
1086 |
%}
|
|
1087 |
.
|
|
1088 |
^ super gcd:anInteger
|
|
1089 |
!
|
|
1090 |
|
|
1091 |
intlog10
|
|
1092 |
"return the truncation of log10 of the receiver -
|
|
1093 |
stupid implementation; used to find out the number of digits needed
|
|
1094 |
to print a number/and for conversion to a LargeInteger"
|
|
1095 |
|
|
1096 |
self <= 0 ifTrue:[
|
|
1097 |
self error:'logarithm of negative integer'
|
|
1098 |
].
|
|
1099 |
self < 10 ifTrue:[^ 1].
|
|
1100 |
self < 100 ifTrue:[^ 2].
|
|
1101 |
self < 1000 ifTrue:[^ 3].
|
|
1102 |
self < 10000 ifTrue:[^ 4].
|
|
1103 |
self < 100000 ifTrue:[^ 5].
|
|
1104 |
self < 1000000 ifTrue:[^ 6].
|
|
1105 |
self < 10000000 ifTrue:[^ 7].
|
|
1106 |
self < 100000000 ifTrue:[^ 8].
|
|
1107 |
self < 1000000000 ifTrue:[^ 9].
|
|
1108 |
^ 10
|
|
1109 |
! !
|
|
1110 |
|
|
1111 |
!SmallInteger methodsFor:'coercing and converting'!
|
|
1112 |
|
|
1113 |
coerce:aNumber
|
41
|
1114 |
"return aNumber converted into receivers type"
|
|
1115 |
|
1
|
1116 |
^ aNumber asInteger
|
|
1117 |
!
|
|
1118 |
|
|
1119 |
generality
|
41
|
1120 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
1121 |
|
1
|
1122 |
^ 20
|
|
1123 |
!
|
|
1124 |
|
|
1125 |
asFloat
|
|
1126 |
"return a Float with same value as receiver"
|
|
1127 |
|
|
1128 |
%{ /* NOCONTEXT */
|
|
1129 |
|
|
1130 |
OBJ newFloat;
|
|
1131 |
|
|
1132 |
_qAlignedNew(newFloat, sizeof(struct floatstruct), SENDER);
|
|
1133 |
_InstPtr(newFloat)->o_class = Float;
|
|
1134 |
_FloatInstPtr(newFloat)->f_floatvalue = _intVal(self);
|
|
1135 |
RETURN ( newFloat );
|
|
1136 |
%}
|
|
1137 |
!
|
|
1138 |
|
|
1139 |
asLargeInteger
|
|
1140 |
"return a LargeInteger with same value as receiver"
|
|
1141 |
|
|
1142 |
^ LargeInteger value:self
|
|
1143 |
!
|
|
1144 |
|
|
1145 |
asCharacter
|
|
1146 |
"Return self as an ascii character"
|
|
1147 |
|
|
1148 |
^ Character value:self
|
|
1149 |
! !
|
|
1150 |
|
|
1151 |
!SmallInteger methodsFor:'iterators'!
|
|
1152 |
|
|
1153 |
timesRepeat:aBlock
|
|
1154 |
"evaluate the argument, aBlock self times"
|
|
1155 |
|
|
1156 |
|count "{ Class: SmallInteger }" |
|
|
1157 |
|
|
1158 |
count := self.
|
|
1159 |
[count > 0] whileTrue:[
|
|
1160 |
aBlock value.
|
|
1161 |
count := count - 1
|
|
1162 |
]
|
|
1163 |
!
|
|
1164 |
|
|
1165 |
to:stop do:aBlock
|
|
1166 |
"reimplemented for speed"
|
|
1167 |
|
|
1168 |
|home index|
|
|
1169 |
%{
|
|
1170 |
REGISTER INT tmp;
|
|
1171 |
INT final;
|
|
1172 |
REGISTER OBJFUNC code;
|
|
1173 |
extern OBJ Block, _value_;
|
|
1174 |
static struct inlineCache blockVal = _ILC1;
|
2
|
1175 |
REGISTER OBJ rHome;
|
1
|
1176 |
|
|
1177 |
if (_isSmallInteger(stop)) {
|
|
1178 |
tmp = _intVal(self);
|
|
1179 |
final = _intVal(stop);
|
2
|
1180 |
if (__isBlock(aBlock)
|
1
|
1181 |
&& ((code = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
|
1182 |
&& (_BlockInstPtr(aBlock)->b_nargs == _MKSMALLINT(1))) {
|
13
|
1183 |
#ifdef NEW_BLOCK_CALL
|
|
1184 |
while (tmp <= final) {
|
|
1185 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1186 |
(*code)(aBlock, CON_COMMA _MKSMALLINT(tmp));
|
|
1187 |
tmp++;
|
|
1188 |
}
|
|
1189 |
#else
|
1
|
1190 |
/*
|
2
|
1191 |
* arg is a compiled block -
|
1
|
1192 |
* directly call it without going through "Block-value"
|
|
1193 |
*/
|
|
1194 |
home = _BlockInstPtr(aBlock)->b_home;
|
2
|
1195 |
rHome = home;
|
|
1196 |
if ((rHome == nil) || (_qSpace(rHome) >= STACKSPACE)) {
|
|
1197 |
/*
|
|
1198 |
* home will not move - keep in in a register
|
|
1199 |
*/
|
|
1200 |
while (tmp <= final) {
|
|
1201 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1202 |
(*code)(rHome, CON_COMMA _MKSMALLINT(tmp));
|
|
1203 |
tmp++;
|
|
1204 |
}
|
10
|
1205 |
} else {
|
2
|
1206 |
while (tmp <= final) {
|
|
1207 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1208 |
(*code)(home, CON_COMMA _MKSMALLINT(tmp));
|
|
1209 |
tmp++;
|
|
1210 |
}
|
1
|
1211 |
}
|
13
|
1212 |
#endif
|
1
|
1213 |
} else {
|
|
1214 |
/*
|
2
|
1215 |
* arg is something else - call it with Block-value"
|
1
|
1216 |
*/
|
|
1217 |
while (tmp <= final) {
|
|
1218 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1219 |
|
2
|
1220 |
(*blockVal.ilc_func)(aBlock, _value_, CON_COMMA nil, &blockVal,
|
|
1221 |
_MKSMALLINT(tmp));
|
1
|
1222 |
tmp++;
|
|
1223 |
}
|
|
1224 |
}
|
|
1225 |
RETURN ( self );
|
|
1226 |
}
|
|
1227 |
%}
|
|
1228 |
.
|
|
1229 |
^super to:stop do:aBlock
|
|
1230 |
!
|
|
1231 |
|
|
1232 |
to:stop by:incr do:aBlock
|
|
1233 |
"reimplemented for speed"
|
|
1234 |
|
|
1235 |
|home index|
|
|
1236 |
%{
|
|
1237 |
REGISTER INT tmp, step;
|
|
1238 |
REGISTER INT final;
|
|
1239 |
REGISTER OBJFUNC code;
|
|
1240 |
extern OBJ Block, _value_;
|
|
1241 |
static struct inlineCache blockVal = _ILC1;
|
2
|
1242 |
REGISTER OBJ rHome;
|
1
|
1243 |
|
|
1244 |
if (_isSmallInteger(incr)
|
|
1245 |
&& _isSmallInteger(stop)) {
|
|
1246 |
tmp = _intVal(self);
|
|
1247 |
final = _intVal(stop);
|
|
1248 |
step = _intVal(incr);
|
2
|
1249 |
if (__isBlock(aBlock)
|
1
|
1250 |
&& ((code = _BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil)
|
|
1251 |
&& (_BlockInstPtr(aBlock)->b_nargs == _MKSMALLINT(1))) {
|
13
|
1252 |
#ifdef NEW_BLOCK_CALL
|
|
1253 |
if (step < 0) {
|
|
1254 |
while (tmp >= final) {
|
|
1255 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1256 |
(*code)(aBlock, CON_COMMA _MKSMALLINT(tmp));
|
|
1257 |
tmp += step;
|
|
1258 |
}
|
41
|
1259 |
} else {
|
13
|
1260 |
while (tmp <= final) {
|
|
1261 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1262 |
(*code)(aBlock, CON_COMMA _MKSMALLINT(tmp));
|
|
1263 |
tmp += step;
|
|
1264 |
}
|
|
1265 |
}
|
|
1266 |
#else
|
1
|
1267 |
/*
|
2
|
1268 |
* arg is a compiled block -
|
1
|
1269 |
* directly call it without going through "Block-value"
|
|
1270 |
*/
|
|
1271 |
home = _BlockInstPtr(aBlock)->b_home;
|
10
|
1272 |
rHome = home;
|
2
|
1273 |
if (step < 0) {
|
|
1274 |
if ((rHome == nil) || (_qSpace(rHome) >= STACKSPACE)) {
|
13
|
1275 |
/*
|
|
1276 |
* home is on stack - will not move
|
|
1277 |
*/
|
2
|
1278 |
while (tmp >= final) {
|
|
1279 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1280 |
(*code)(rHome, CON_COMMA _MKSMALLINT(tmp));
|
|
1281 |
tmp += step;
|
|
1282 |
}
|
10
|
1283 |
} else {
|
2
|
1284 |
while (tmp >= final) {
|
|
1285 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1286 |
(*code)(home, CON_COMMA _MKSMALLINT(tmp));
|
|
1287 |
tmp += step;
|
|
1288 |
}
|
|
1289 |
}
|
|
1290 |
} else {
|
|
1291 |
if ((rHome == nil) || (_qSpace(rHome) >= STACKSPACE)) {
|
13
|
1292 |
/*
|
|
1293 |
* home is on stack - will not move
|
|
1294 |
*/
|
2
|
1295 |
while (tmp <= final) {
|
|
1296 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1297 |
(*code)(rHome, CON_COMMA _MKSMALLINT(tmp));
|
|
1298 |
tmp += step;
|
|
1299 |
}
|
10
|
1300 |
} else {
|
2
|
1301 |
while (tmp <= final) {
|
|
1302 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1303 |
(*code)(home, CON_COMMA _MKSMALLINT(tmp));
|
|
1304 |
tmp += step;
|
|
1305 |
}
|
|
1306 |
}
|
|
1307 |
}
|
13
|
1308 |
#endif
|
2
|
1309 |
} else {
|
|
1310 |
/*
|
|
1311 |
* arg is something else - call it with Block-value"
|
|
1312 |
*/
|
|
1313 |
if (step < 0) {
|
1
|
1314 |
while (tmp >= final) {
|
|
1315 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1316 |
|
2
|
1317 |
(*blockVal.ilc_func)(aBlock, _value_, CON_COMMA nil, &blockVal,
|
|
1318 |
_MKSMALLINT(tmp));
|
1
|
1319 |
tmp += step;
|
|
1320 |
}
|
2
|
1321 |
} else {
|
1
|
1322 |
while (tmp <= final) {
|
|
1323 |
if (InterruptPending != nil) interrupt(CONARG);
|
|
1324 |
|
2
|
1325 |
(*blockVal.ilc_func)(aBlock, _value_, CON_COMMA nil, &blockVal,
|
|
1326 |
_MKSMALLINT(tmp));
|
1
|
1327 |
tmp += step;
|
|
1328 |
}
|
|
1329 |
}
|
|
1330 |
}
|
|
1331 |
RETURN ( self );
|
|
1332 |
}
|
|
1333 |
%}
|
|
1334 |
.
|
2
|
1335 |
^ super to:stop do:aBlock
|
|
1336 |
! !
|
|
1337 |
|
|
1338 |
!SmallInteger class methodsFor:'binary storage'!
|
|
1339 |
|
|
1340 |
binaryDefinitionFrom: stream manager: manager
|
|
1341 |
"read the binary representation as stored in storeBinaryOn:"
|
|
1342 |
|
|
1343 |
| value |
|
|
1344 |
|
|
1345 |
value := stream next bitAnd: 16r7F.
|
|
1346 |
value > 16r3F ifTrue: [
|
|
1347 |
value := value - 16r80
|
|
1348 |
].
|
|
1349 |
value := (value bitShift: 8) bitOr: stream next.
|
|
1350 |
value := (value bitShift: 8) bitOr: stream next.
|
|
1351 |
value := (value bitShift: 8) bitOr: stream next.
|
|
1352 |
^ value
|
|
1353 |
! !
|
|
1354 |
|
|
1355 |
!SmallInteger methodsFor:'binary storage'!
|
|
1356 |
|
|
1357 |
hasSpecialBinaryRepresentation
|
41
|
1358 |
"return true, if the receiver has a special binary representation"
|
|
1359 |
|
2
|
1360 |
^ true
|
|
1361 |
!
|
|
1362 |
|
|
1363 |
storeBinaryOn: stream manager: manager
|
41
|
1364 |
"append a binary representation onto stream.
|
|
1365 |
Redefined since SmallIntegers are stored as their value with the 32nd bit
|
|
1366 |
set as a tag.
|
|
1367 |
To make the binary file a bit more compact, zeros and single byte ints
|
|
1368 |
are stored with a more compact representation (using special type-codes)."
|
2
|
1369 |
|
3
|
1370 |
self == 0 ifTrue:[
|
|
1371 |
stream nextPut: manager codeForZero.
|
|
1372 |
^ self
|
|
1373 |
].
|
|
1374 |
(self between:0 and:255) ifTrue:[
|
|
1375 |
stream nextPut: manager codeForByteInteger.
|
|
1376 |
stream nextPut: self.
|
|
1377 |
^ self
|
|
1378 |
].
|
2
|
1379 |
stream nextPut: (((self bitShift: -24) bitAnd: 16rFF) bitOr: 16r80).
|
|
1380 |
stream nextPut: ((self bitShift: -16) bitAnd: 16rFF).
|
|
1381 |
stream nextPut: ((self bitShift: -8) bitAnd: 16rFF).
|
|
1382 |
stream nextPut: (self bitAnd: 16rFF)
|
1
|
1383 |
! !
|
|
1384 |
|
|
1385 |
!SmallInteger methodsFor:'printing & storing'!
|
|
1386 |
|
|
1387 |
printString
|
|
1388 |
"return my printstring (base 10)"
|
|
1389 |
|
|
1390 |
%{ /* NOCONTEXT */
|
|
1391 |
|
|
1392 |
extern char *newNextPtr, *newEndPtr;
|
|
1393 |
char buffer[30];
|
|
1394 |
OBJ newString;
|
13
|
1395 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
1396 |
OBJ sav = __thisContext;
|
|
1397 |
#endif
|
|
1398 |
|
|
1399 |
sprintf(buffer, "%d", _intVal(self));
|
13
|
1400 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
1401 |
__thisContext = sav;
|
|
1402 |
#endif
|
|
1403 |
_qNew(newString, sizeof(struct stringheader) + strlen(buffer) + 1, SENDER);
|
|
1404 |
_InstPtr(newString)->o_class = String;
|
|
1405 |
strcpy(_stringVal(newString), buffer);
|
|
1406 |
RETURN (newString);
|
|
1407 |
%}
|
|
1408 |
!
|
|
1409 |
|
|
1410 |
printStringRadix:radix
|
|
1411 |
"return my printstring (base 10)"
|
|
1412 |
|
|
1413 |
%{ /* NOCONTEXT */
|
|
1414 |
|
|
1415 |
extern char *newNextPtr, *newEndPtr;
|
|
1416 |
char *format = (char *)0;
|
|
1417 |
char buffer[30];
|
|
1418 |
OBJ newString;
|
|
1419 |
|
|
1420 |
if (_isSmallInteger(radix)) {
|
|
1421 |
switch (_intVal(radix)) {
|
2
|
1422 |
case 10:
|
|
1423 |
format = "%d";
|
|
1424 |
break;
|
|
1425 |
case 16:
|
|
1426 |
format = "%x";
|
|
1427 |
break;
|
|
1428 |
case 8:
|
|
1429 |
format = "%o";
|
|
1430 |
break;
|
|
1431 |
}
|
1
|
1432 |
}
|
|
1433 |
|
|
1434 |
if (format) {
|
13
|
1435 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
1436 |
OBJ sav = __thisContext;
|
|
1437 |
#endif
|
|
1438 |
sprintf(buffer, format, _intVal(self));
|
13
|
1439 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
1440 |
__thisContext = sav;
|
|
1441 |
#endif
|
|
1442 |
_qNew(newString, sizeof(struct stringheader) + strlen(buffer) + 1, SENDER);
|
|
1443 |
_InstPtr(newString)->o_class = String;
|
|
1444 |
strcpy(_stringVal(newString), buffer);
|
|
1445 |
RETURN (newString);
|
|
1446 |
}
|
|
1447 |
%}
|
|
1448 |
.
|
|
1449 |
^ super printStringRadix:radix
|
|
1450 |
!
|
|
1451 |
|
|
1452 |
printfPrintString:formatString
|
|
1453 |
"non-portable, but sometimes useful.
|
|
1454 |
return a printed representation of the receiver
|
|
1455 |
as specified by formatString, which is defined by printf.
|
|
1456 |
No checking for string overrun - must be shorter than 256 chars or else ..."
|
|
1457 |
|
13
|
1458 |
%{ /* STACK: 400 */
|
1
|
1459 |
|
|
1460 |
char buffer[256];
|
|
1461 |
|
|
1462 |
if (_isString(formatString)) {
|
13
|
1463 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
1464 |
OBJ sav = __thisContext;
|
|
1465 |
#endif
|
|
1466 |
sprintf(buffer, _stringVal(formatString), _intVal(self));
|
13
|
1467 |
#ifdef THISCONTEXT_IN_REGISTER
|
1
|
1468 |
__thisContext = sav;
|
|
1469 |
#endif
|
|
1470 |
RETURN ( _MKSTRING(buffer COMMA_SND) );
|
|
1471 |
}
|
|
1472 |
%}
|
|
1473 |
.
|
|
1474 |
self primitiveFailed
|
|
1475 |
|
|
1476 |
"123 printfPrintString:'%%d -> %d'"
|
|
1477 |
"123 printfPrintString:'%%6d -> %6d'"
|
|
1478 |
"123 printfPrintString:'%%x -> %x'"
|
|
1479 |
"123 printfPrintString:'%%4x -> %4x'"
|
|
1480 |
"123 printfPrintString:'%%04x -> %04x'"
|
|
1481 |
! !
|