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