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