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