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