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