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