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