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