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