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