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