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