author | Claus Gittinger <cg@exept.de> |
Sat, 24 Jan 2009 21:40:24 +0100 | |
changeset 11493 | 6496a8ef2fc9 |
parent 10972 | bd9df21d6654 |
child 11500 | 6afe4d6e867c |
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 |
" |
|
5410 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
1 | 14 |
Integer subclass:#SmallInteger |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
15 |
instanceVariableNames:'' |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
16 |
classVariableNames:'' |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
17 |
poolDictionaries:'' |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
18 |
category:'Magnitude-Numbers' |
1 | 19 |
! |
20 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
21 |
!SmallInteger class methodsFor:'documentation'! |
1 | 22 |
|
88 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
214 | 26 |
All Rights Reserved |
88 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
68 | 37 |
documentation |
38 |
" |
|
8908 | 39 |
SmallIntegers are Integers in the range of at least +/- 2^30 |
2785 | 40 |
(i.e. 31 bits, which is not a guaranteed: on an alpha, 63 bits are used, |
41 |
if the system was configured for 64bit mode). |
|
88 | 42 |
|
68 | 43 |
These are no real objects - they have no instances (not even storage !!) |
44 |
and cannot be subclassed. |
|
45 |
The reason is to save both storage and runtime by not collecting |
|
8908 | 46 |
SmallIntegers in the system. SmallInts are marked by having the TAG_INT |
47 |
bit set, in contrast to all other objects which do not. |
|
48 |
Since this knowledge is hardwired into the system (and there is no |
|
49 |
class-field stored with SmallIntegers) there can be no subclass of |
|
88 | 50 |
SmallInteger (sorry). |
51 |
||
52 |
If you really need this kind of thing, create a subclass of Integer, |
|
53 |
with an instance variable holding the value. |
|
1295 | 54 |
|
55 |
[author:] |
|
2216 | 56 |
Claus Gittinger |
1556 | 57 |
|
58 |
[see also:] |
|
2216 | 59 |
Number |
60 |
Float Fraction FixedPoint |
|
61 |
LargeInteger |
|
68 | 62 |
" |
63 |
! ! |
|
1 | 64 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
65 |
!SmallInteger class methodsFor:'instance creation'! |
1 | 66 |
|
67 |
basicNew |
|
68 |
"catch instance creation |
|
69 |
- SmallIntegers cannot be created with new" |
|
70 |
||
71 |
self error:'instances of SmallInteger cannot be created with new' |
|
72 |
! |
|
73 |
||
74 |
basicNew:size |
|
75 |
"catch instance creation |
|
76 |
- SmallIntegers cannot be created with new" |
|
77 |
||
78 |
self error:'instances of SmallInteger cannot be created with new' |
|
79 |
! ! |
|
80 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
81 |
!SmallInteger class methodsFor:'bit mask constants'! |
701 | 82 |
|
83 |
bitMaskFor:index |
|
84 |
"return a bitmask for the index's bit (index starts at 1)" |
|
85 |
||
86 |
(index between:1 and:SmallInteger maxBits) ifFalse:[ |
|
8908 | 87 |
^ SubscriptOutOfBoundsSignal |
88 |
raiseRequestWith:index |
|
89 |
errorString:'index out of bounds' |
|
701 | 90 |
]. |
91 |
^ 1 bitShift:(index - 1) |
|
92 |
! ! |
|
93 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
94 |
!SmallInteger class methodsFor:'constants'! |
1 | 95 |
|
96 |
maxBits |
|
68 | 97 |
"return the number of bits in instances of me. |
98 |
For very special uses only - not constant across implementations" |
|
1 | 99 |
|
100 |
%{ /* NOCONTEXT */ |
|
8913 | 101 |
RETURN ( __mkSmallInteger(N_INT_BITS) ); |
1 | 102 |
%} |
2 | 103 |
|
104 |
"SmallInteger maxBits" |
|
1 | 105 |
! |
106 |
||
107 |
maxBytes |
|
68 | 108 |
"return the number of bytes in instances of me. |
109 |
For very special uses only - not constant across implementations" |
|
1 | 110 |
|
111 |
%{ /* NOCONTEXT */ |
|
8913 | 112 |
RETURN ( __mkSmallInteger(N_INT_BITS / 8 + 1) ); |
1 | 113 |
%} |
2 | 114 |
|
3684 | 115 |
" |
116 |
SmallInteger maxBytes |
|
117 |
" |
|
1 | 118 |
! |
119 |
||
120 |
maxVal |
|
68 | 121 |
"return the largest Integer representable as SmallInteger. |
122 |
For very special uses only - not constant across implementations" |
|
1 | 123 |
|
124 |
%{ /* NOCONTEXT */ |
|
8913 | 125 |
RETURN ( __mkSmallInteger(_MAX_INT) ); |
1 | 126 |
%} |
2 | 127 |
|
128 |
"SmallInteger maxVal" |
|
701 | 129 |
! |
130 |
||
131 |
minVal |
|
132 |
"return the smallest Integer representable as SmallInteger. |
|
133 |
For very special uses only - not constant across implementations" |
|
134 |
||
135 |
%{ /* NOCONTEXT */ |
|
8913 | 136 |
RETURN ( __mkSmallInteger(_MIN_INT) ); |
701 | 137 |
%} |
138 |
||
139 |
"SmallInteger minVal" |
|
1 | 140 |
! ! |
141 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
142 |
!SmallInteger class methodsFor:'queries'! |
3 | 143 |
|
50 | 144 |
canBeSubclassed |
145 |
"return true, if its allowed to create subclasses of the receiver. |
|
3914 | 146 |
Return false here - since it is NOT possible for SmallInteger |
147 |
(due to the tagged representation of SmallIntegers)" |
|
50 | 148 |
|
149 |
^ false |
|
3914 | 150 |
|
151 |
"Modified: / 5.11.1998 / 16:11:27 / cg" |
|
1 | 152 |
! |
153 |
||
2672
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
154 |
hasImmediateInstances |
4661 | 155 |
"return true if this class has immediate instances |
156 |
i.e. if the instances are represented in the pointer itself and |
|
157 |
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
|
158 |
Redefined from Behavior" |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
159 |
|
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
160 |
^ true |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
161 |
|
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
162 |
"Created: 3.6.1997 / 12:01:26 / cg" |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
163 |
! |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
164 |
|
701 | 165 |
isBuiltInClass |
1264 | 166 |
"return true if this class is known by the run-time-system. |
167 |
Here, true is returned." |
|
1 | 168 |
|
169 |
^ true |
|
1264 | 170 |
|
171 |
"Modified: 23.4.1996 / 16:00:33 / cg" |
|
1 | 172 |
! ! |
173 |
||
174 |
!SmallInteger methodsFor:'arithmetic'! |
|
175 |
||
176 |
* aNumber |
|
177 |
"return the product of the receivers value and the arguments value" |
|
178 |
||
179 |
%{ /* NOCONTEXT */ |
|
180 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
181 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
182 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
183 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
184 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
185 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
186 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
187 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
188 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
189 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
190 |
|
68 | 191 |
INT myValue, otherValue; |
192 |
unsigned INT productLow, productHi; |
|
193 |
int negative; |
|
194 |
||
1716
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
195 |
# define low16Bits(foo) ((foo) & 0xFFFF) |
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
196 |
# define hi16Bits(foo) ((foo) >> 16) |
2785 | 197 |
# define low32Bits(foo) ((foo) & 0xFFFFFFFFL) |
198 |
# define hi32Bits(foo) ((foo) >> 32) |
|
95 | 199 |
|
200 |
/* |
|
201 |
* can we use long long arithmetic ? |
|
4176 | 202 |
* |
203 |
* long-long arithmetic seems to be buggy with some systems |
|
204 |
* (took me a while to find this out :-( |
|
205 |
* (try 10000 * 10000) |
|
95 | 206 |
*/ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
207 |
#if defined(__sparc__) && defined(__GNUC__) && (__GNUC__ >= 2) |
4184 | 208 |
# define USE_LONGLONG_FOR_MUL |
4176 | 209 |
#endif |
210 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
211 |
#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2) |
4184 | 212 |
# define USE_LONGLONG_FOR_MUL |
95 | 213 |
#endif |
1 | 214 |
|
252 | 215 |
if (__isSmallInteger(aNumber)) { |
8908 | 216 |
myValue = __intVal(self); |
217 |
otherValue = __intVal(aNumber); |
|
2785 | 218 |
|
4184 | 219 |
#if defined(USE_LONGLONG_FOR_MUL) |
8908 | 220 |
{ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
221 |
# if defined(__alpha__) && !defined(__alpha64__) |
3532
4b1a5cc57f8f
added int op shortFloat inline
Claus Gittinger <cg@exept.de>
parents:
3458
diff
changeset
|
222 |
# define LONGLONG INT64 |
2785 | 223 |
# else |
3532
4b1a5cc57f8f
added int op shortFloat inline
Claus Gittinger <cg@exept.de>
parents:
3458
diff
changeset
|
224 |
# define LONGLONG long long |
2785 | 225 |
# endif |
8908 | 226 |
LONGLONG product; |
227 |
||
228 |
product = (LONGLONG)myValue * (LONGLONG)otherValue; |
|
229 |
if ((product >= (LONGLONG)_MIN_INT) |
|
230 |
&& (product <= (LONGLONG)_MAX_INT)) { |
|
8913 | 231 |
RETURN ( __mkSmallInteger((INT)product) ); |
8908 | 232 |
} |
233 |
if (product < 0) { |
|
234 |
negative = -1; |
|
235 |
product = -product; |
|
236 |
} else { |
|
237 |
negative = 1; |
|
238 |
} |
|
239 |
productHi = product >> 32; |
|
240 |
productLow = product & 0xFFFFFFFFL; |
|
241 |
} |
|
4176 | 242 |
#else /* no long-long */ |
8908 | 243 |
negative = 1; |
244 |
if (myValue < 0) { |
|
245 |
negative = -1; |
|
246 |
myValue = -myValue; |
|
247 |
} |
|
248 |
if (otherValue < 0) { |
|
249 |
negative = -negative; |
|
250 |
otherValue = -otherValue; |
|
251 |
} |
|
68 | 252 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
253 |
# if defined(__GNUC__) && defined(__mc68k__) |
8908 | 254 |
asm ("mulu%.l %3,%1:%0" |
255 |
: "=d" ((unsigned long)(productLow)), |
|
256 |
"=d" ((unsigned long)(productHi)) |
|
257 |
: "%0" ((unsigned long)(myValue)), |
|
258 |
"dmi" ((unsigned long)(otherValue))); |
|
68 | 259 |
# else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
260 |
# if defined (__GNUC__) && defined(__i386__) |
8908 | 261 |
asm ("mull %3" |
262 |
: "=a" ((unsigned long)(productLow)), |
|
263 |
"=d" ((unsigned long)(productHi)) |
|
264 |
: "%0" ((unsigned long)(myValue)), |
|
265 |
"rm" ((unsigned long)(otherValue))); |
|
68 | 266 |
# else |
10342 | 267 |
# if defined(WIN32) && defined(__BORLANDC__) |
8908 | 268 |
asm { |
269 |
mov eax, myValue |
|
270 |
mov edx, otherValue |
|
271 |
mul edx |
|
272 |
mov productLow, eax |
|
273 |
mov productHi, edx |
|
274 |
} |
|
4167 | 275 |
# else /* generic */ |
8908 | 276 |
{ |
277 |
unsigned INT pHH, pHL, pLH, pLL; |
|
278 |
unsigned INT low1, low2, hi1, hi2; |
|
279 |
unsigned INT t; |
|
280 |
||
281 |
/* unsigned multiply myValue * otherValue -> productHi, productLow |
|
282 |
* |
|
283 |
* this is too slow: |
|
284 |
* since most machines can do 32*32 to 64 bit multiply, |
|
285 |
* (or at least 32*32 with Overflow check) |
|
286 |
* - need more assembler (inline) functions here |
|
287 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
288 |
# if __POINTER_SIZE__ == 8 |
8908 | 289 |
low1 = low32Bits((unsigned INT)myValue); |
290 |
hi1 = hi32Bits((unsigned INT)myValue); |
|
291 |
low2 = low32Bits((unsigned INT)otherValue); |
|
292 |
hi2 = hi32Bits((unsigned INT)otherValue); |
|
4167 | 293 |
# define LLMASK 0xC000000000000000L |
294 |
# else |
|
8908 | 295 |
low1 = low16Bits((unsigned INT)myValue); |
296 |
hi1 = hi16Bits((unsigned INT)myValue); |
|
297 |
low2 = low16Bits((unsigned INT)otherValue); |
|
298 |
hi2 = hi16Bits((unsigned INT)otherValue); |
|
4167 | 299 |
# define LLMASK 0xC0000000 |
300 |
# endif |
|
301 |
||
8908 | 302 |
pLH = low1 * hi2; |
303 |
pHL = hi1 * low2; |
|
304 |
pLL = low1 * low2; |
|
305 |
pHH = hi1 * hi2; |
|
306 |
||
307 |
/* |
|
308 |
* the common case ... |
|
309 |
*/ |
|
310 |
if ((pHL == 0) |
|
311 |
&& (pLH == 0) |
|
312 |
&& (pHH == 0) |
|
313 |
&& ((pLL & LLMASK) == 0)) { |
|
314 |
if (negative < 0) { |
|
8913 | 315 |
RETURN ( __mkSmallInteger(- ((INT)pLL)) ); |
8908 | 316 |
} |
8913 | 317 |
RETURN ( __mkSmallInteger((INT)pLL) ); |
8908 | 318 |
} |
319 |
||
320 |
/* |
|
321 |
* pHH |--------|--------| |
|
322 |
* pLH |--------|--------| |
|
323 |
* pHL |--------|--------| |
|
324 |
* pLL |--------|--------| |
|
325 |
*/ |
|
4167 | 326 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
327 |
# if __POINTER_SIZE__ == 8 |
8908 | 328 |
t = low32Bits(pLH) + low32Bits(pHL) + hi32Bits(pLL); |
329 |
productLow = (t << 32) + low32Bits(pLL); |
|
330 |
productHi = pHH + hi32Bits(t) + hi32Bits(pHL) + hi32Bits(pLH); |
|
4167 | 331 |
# else |
8908 | 332 |
t = low16Bits(pLH) + low16Bits(pHL) + hi16Bits(pLL); |
333 |
productLow = (t << 16) + low16Bits(pLL); |
|
334 |
productHi = pHH + hi16Bits(t) + hi16Bits(pHL) + hi16Bits(pLH); |
|
4167 | 335 |
# endif |
8908 | 336 |
} |
4167 | 337 |
# endif /* ! WIN32 */ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
338 |
# endif /* ! (__GNUC__ && __i386__) */ |
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
339 |
# endif /* ! (__GNUC__ && __mc68k__) */ |
4167 | 340 |
|
8908 | 341 |
if (productHi == 0) { |
342 |
if (negative < 0) { |
|
343 |
if (productLow <= -(_MIN_INT)) { |
|
8913 | 344 |
RETURN ( __mkSmallInteger(-((INT)productLow)) ); |
8908 | 345 |
} |
346 |
} else { |
|
347 |
if (productLow <= _MAX_INT) { |
|
8913 | 348 |
RETURN ( __mkSmallInteger(productLow) ); |
8908 | 349 |
} |
350 |
} |
|
351 |
} |
|
4167 | 352 |
#endif /* ! USE_LONGLONG */ |
353 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
354 |
#if __POINTER_SIZE__ == 8 |
8908 | 355 |
RETURN (__MKLARGEINT128(negative, productLow, productHi)); |
4167 | 356 |
#else |
8908 | 357 |
RETURN (__MKLARGEINT64(negative, productLow, productHi)); |
95 | 358 |
#endif |
283 | 359 |
} else if (__isFloatLike(aNumber)) { |
8908 | 360 |
OBJ newFloat; |
361 |
double val = (double)__intVal(self) * __floatVal(aNumber); |
|
362 |
||
363 |
__qMKFLOAT(newFloat, val); |
|
364 |
RETURN ( newFloat ); |
|
3532
4b1a5cc57f8f
added int op shortFloat inline
Claus Gittinger <cg@exept.de>
parents:
3458
diff
changeset
|
365 |
} else if (__isShortFloat(aNumber)) { |
8908 | 366 |
OBJ newFloat; |
367 |
float val = (float)__intVal(self) * __shortFloatVal(aNumber); |
|
368 |
||
369 |
__qMKSFLOAT(newFloat, val); |
|
370 |
RETURN ( newFloat ); |
|
1 | 371 |
} |
2641 | 372 |
%}. |
1 | 373 |
^ aNumber productFromInteger:self |
374 |
! |
|
375 |
||
701 | 376 |
+ aNumber |
377 |
"return the sum of the receivers value and the arguments value" |
|
378 |
||
379 |
%{ /* NOCONTEXT */ |
|
380 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
381 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
382 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
383 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
384 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
385 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
386 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
387 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
388 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
389 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
390 |
|
701 | 391 |
if (__isSmallInteger(aNumber)) { |
392 |
#ifdef _ADD_IO_IO |
|
8908 | 393 |
RETURN ( _ADD_IO_IO(self, aNumber) ); |
701 | 394 |
#else |
8908 | 395 |
REGISTER INT sum; |
396 |
||
397 |
sum = __intVal(self) + __intVal(aNumber); |
|
398 |
if (__ISVALIDINTEGER(sum)) { |
|
8913 | 399 |
RETURN ( __mkSmallInteger(sum) ); |
8908 | 400 |
} |
401 |
RETURN ( __MKLARGEINT(sum) ); |
|
701 | 402 |
#endif |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
403 |
} |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
404 |
if (__isFloatLike(aNumber)) { |
8908 | 405 |
OBJ newFloat; |
406 |
double val = (double)__intVal(self) + __floatVal(aNumber); |
|
407 |
||
408 |
__qMKFLOAT(newFloat, val); |
|
409 |
RETURN ( newFloat ); |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
410 |
} |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
411 |
if (__isShortFloat(aNumber)) { |
8908 | 412 |
OBJ newFloat; |
413 |
float val = (float)__intVal(self) + __shortFloatVal(aNumber); |
|
414 |
||
415 |
__qMKSFLOAT(newFloat, val); |
|
416 |
RETURN ( newFloat ); |
|
701 | 417 |
} |
3532
4b1a5cc57f8f
added int op shortFloat inline
Claus Gittinger <cg@exept.de>
parents:
3458
diff
changeset
|
418 |
%}. |
701 | 419 |
^ aNumber sumFromInteger:self |
420 |
! |
|
421 |
||
422 |
- aNumber |
|
423 |
"return the difference of the receivers value and the arguments value" |
|
424 |
||
425 |
%{ /* NOCONTEXT */ |
|
426 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
427 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
428 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
429 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
430 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
431 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
432 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
433 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
434 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
435 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
436 |
|
701 | 437 |
if (__isSmallInteger(aNumber)) { |
438 |
#ifdef _SUB_IO_IO |
|
8908 | 439 |
RETURN ( _SUB_IO_IO(self, aNumber) ); |
701 | 440 |
#else |
8908 | 441 |
REGISTER INT diff; |
442 |
||
443 |
diff = __intVal(self) - __intVal(aNumber); |
|
444 |
if (__ISVALIDINTEGER(diff)) { |
|
8913 | 445 |
RETURN ( __mkSmallInteger(diff) ); |
8908 | 446 |
} |
447 |
RETURN ( __MKLARGEINT(diff) ); |
|
701 | 448 |
#endif |
8908 | 449 |
} |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
450 |
if (__isFloatLike(aNumber)) { |
8908 | 451 |
OBJ newFloat; |
452 |
double val = (double)__intVal(self) - __floatVal(aNumber); |
|
453 |
||
454 |
__qMKFLOAT(newFloat, val); |
|
455 |
RETURN ( newFloat ); |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
456 |
} |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
457 |
if (__isShortFloat(aNumber)) { |
8908 | 458 |
OBJ newFloat; |
459 |
float val = (float)__intVal(self) - __shortFloatVal(aNumber); |
|
460 |
||
461 |
__qMKSFLOAT(newFloat, val); |
|
462 |
RETURN ( newFloat ); |
|
701 | 463 |
} |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
464 |
%}. |
701 | 465 |
^ aNumber differenceFromInteger:self |
466 |
! |
|
467 |
||
1 | 468 |
/ aNumber |
469 |
"return the quotient of the receivers value and the arguments value" |
|
470 |
||
471 |
%{ /* NOCONTEXT */ |
|
472 |
||
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
473 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
474 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
475 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
476 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
477 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
478 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
479 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
480 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
481 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
482 |
|
1 | 483 |
INT me, t, val; |
484 |
double dval; |
|
485 |
||
252 | 486 |
if (__isSmallInteger(aNumber)) { |
8908 | 487 |
val = __intVal(aNumber); |
488 |
if (val != 0) { |
|
489 |
me = __intVal(self); |
|
490 |
t = me / val; |
|
1 | 491 |
#ifdef GOOD_OPTIMIZER |
8908 | 492 |
if (me % val == 0) { |
1 | 493 |
#else |
8908 | 494 |
/* this is stupid - all I want is to look for a remainder ... |
495 |
but most compilers are too stupid and generate an extra modulus |
|
496 |
instruction for "if (me % val)". |
|
497 |
Even if most divide instructions already leave the remainder in |
|
498 |
some register. |
|
499 |
Therefore I use a multiplication which is faster than a modulo |
|
500 |
on most machines. Hint to GNU people :-) |
|
501 |
*/ |
|
502 |
if ((t * val) == me) { |
|
1 | 503 |
#endif |
8913 | 504 |
RETURN ( __mkSmallInteger(t) ); |
8908 | 505 |
} |
506 |
} |
|
1 | 507 |
} else { |
8908 | 508 |
if (__isFloatLike(aNumber)) { |
509 |
dval = __floatVal(aNumber); |
|
510 |
if (dval != 0.0) { |
|
511 |
OBJ newFloat; |
|
512 |
double val = (double)__intVal(self) / dval; |
|
513 |
||
514 |
__qMKFLOAT(newFloat, val); |
|
515 |
RETURN ( newFloat ); |
|
516 |
} |
|
517 |
} |
|
1 | 518 |
} |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
519 |
%}. |
1 | 520 |
aNumber isInteger ifTrue:[ |
8908 | 521 |
aNumber == 0 ifTrue:[ |
522 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
523 |
]. |
|
524 |
^ Fraction numerator:self denominator:aNumber |
|
1 | 525 |
]. |
526 |
^ aNumber quotientFromInteger:self |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
527 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
528 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
529 |
8 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
530 |
9 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
531 |
9 // 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
532 |
9 quo:4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
533 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
534 |
-8 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
535 |
-9 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
536 |
-9 // 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
537 |
-9 quo:4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
538 |
" |
1 | 539 |
! |
540 |
||
2 | 541 |
// aNumber |
1 | 542 |
"return the integer part of the quotient of the receivers value |
1884 | 543 |
and the arguments value. The result is truncated toward negative infinity |
544 |
and negative, if the operands signs differ. |
|
8908 | 545 |
Be careful with negative results: 9 // 4 = 2, |
546 |
while -9 // 4 = -3. |
|
6067 | 547 |
The following is always true: |
8908 | 548 |
(receiver // aNumber) * aNumber + (receiver \\ aNUmber) = receiver |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
549 |
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
|
550 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
551 |
%{ /* NOCONTEXT */ |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
552 |
|
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
553 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
554 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
555 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
556 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
557 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
558 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
559 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
560 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
561 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
562 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
563 |
INT dividend, divisor, rslt; |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
564 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
565 |
if (__isSmallInteger(aNumber)) { |
8908 | 566 |
divisor = __intVal(aNumber); |
567 |
if (divisor != 0) { |
|
568 |
dividend = __intVal(self); |
|
569 |
rslt = dividend / divisor; |
|
570 |
/* |
|
571 |
* Optimized to speed up positive result |
|
572 |
*/ |
|
573 |
if (rslt <= 0) { |
|
574 |
if (rslt == 0) { |
|
575 |
if ((dividend ^ divisor) < 0) { |
|
576 |
/* |
|
577 |
* result (negative) has been truncated toward 0. |
|
578 |
* Return -1, because we truncate toward negative inf. |
|
579 |
*/ |
|
580 |
rslt = -1; |
|
581 |
} |
|
582 |
} else { |
|
583 |
/* |
|
584 |
* If result (negative) has been truncated toward 0, |
|
585 |
* subtract 1, because we truncate toward negative inf. |
|
586 |
*/ |
|
587 |
if (divisor > 0) { |
|
588 |
if (rslt * divisor > dividend) { |
|
589 |
rslt--; |
|
590 |
} |
|
591 |
} else { |
|
592 |
if (rslt * divisor < dividend) { |
|
593 |
rslt--; |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
} |
|
8913 | 598 |
RETURN ( __mkSmallInteger(rslt) ); |
8908 | 599 |
} |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
600 |
} else { |
8908 | 601 |
if (__isFraction(aNumber)) { |
602 |
OBJ t; |
|
603 |
INT num, den; |
|
604 |
||
605 |
t = __FractionInstPtr(aNumber)->f_numerator; |
|
606 |
if (__isSmallInteger(t)) { |
|
607 |
num = __intVal(t); |
|
608 |
t = __FractionInstPtr(aNumber)->f_denominator; |
|
609 |
if (__isSmallInteger(t)) { |
|
610 |
den = __intVal(t); |
|
8913 | 611 |
RETURN ( __mkSmallInteger(__intVal(self) * den / num )); |
8908 | 612 |
} |
613 |
} |
|
614 |
} |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
615 |
} |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
616 |
%}. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
617 |
(aNumber = 0) ifTrue:[ |
8908 | 618 |
^ ZeroDivide raiseRequestWith:thisContext. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
619 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
620 |
^ self retry:#// coercing:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
621 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
622 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
623 |
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
|
624 |
-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
|
625 |
9 // -4 ~~ -3 ifTrue:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
626 |
-9 // -4 ~~ 2 ifTrue:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
627 |
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
|
628 |
-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
|
629 |
1 // -2 ~~ -1 ifTrue:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
630 |
-1 // -2 ~~ 0 ifTrue:[self halt]. |
8908 | 631 |
10000 // 3600000 ~~ 0 ifTrue:[self halt]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
632 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
633 |
9 quo:4 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
634 |
-9 quo:4 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
635 |
9 quo:-4 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
636 |
-9 quo:-4 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
637 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
638 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
639 |
|
2 | 640 |
\\ aNumber |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
641 |
"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
|
642 |
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
|
643 |
m < |aNumber| AND there is an integer k with (k * aNumber + m) = self |
6067 | 644 |
The following is always true: |
8908 | 645 |
(receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
646 |
Compare with #rem: |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
647 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
648 |
Redefined for speed." |
1 | 649 |
|
650 |
%{ /* NOCONTEXT */ |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
651 |
|
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
652 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
653 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
654 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
655 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
656 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
657 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
658 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
659 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
660 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
661 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
662 |
INT dividend, divisor, rem; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
663 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
664 |
if (__isSmallInteger(aNumber) |
8908 | 665 |
&& (divisor = __intVal(aNumber)) != 0) { |
666 |
/* |
|
667 |
* Note that the sign of something modulo a negative number is undefined |
|
668 |
* in C! |
|
669 |
*/ |
|
670 |
dividend = __intVal(self); |
|
671 |
rem = dividend % divisor; |
|
672 |
if (rem) { |
|
673 |
if ((rem ^ divisor) < 0) { |
|
674 |
/* sign of remainder is different from sign of divisor */ |
|
675 |
rem = -rem; |
|
676 |
} |
|
677 |
if ((dividend ^ divisor) < 0) { |
|
678 |
/* different signs, so division would have returned a |
|
679 |
* negative number. |
|
680 |
* C rounds toward zero, this code will simulate |
|
681 |
* rounding towards negative infinity. |
|
682 |
*/ |
|
683 |
rem = divisor - rem; |
|
684 |
} |
|
685 |
} |
|
8913 | 686 |
RETURN ( __mkSmallInteger(rem) ); |
1 | 687 |
} |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
688 |
%}. |
2 | 689 |
(aNumber = 0) ifTrue:[ |
8908 | 690 |
^ ZeroDivide raiseRequestWith:thisContext. |
1 | 691 |
]. |
2 | 692 |
^ self retry:#\\ coercing:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
693 |
|
8908 | 694 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
695 |
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
|
696 |
-9 \\ 4 == 3 ifFalse:[self halt]. |
8908 | 697 |
9 \\ -4 == -3 ifFalse:[self halt]. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
698 |
-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
|
699 |
(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
|
700 |
(-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
|
701 |
1000 \\ 3600000 == 1000 ifFalse:[self halt] |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
702 |
" |
1 | 703 |
! |
704 |
||
705 |
abs |
|
706 |
"return the absolute value of the receiver |
|
707 |
reimplemented here for speed" |
|
708 |
||
709 |
%{ /* NOCONTEXT */ |
|
710 |
||
1133 | 711 |
INT val = __intVal(self); |
1 | 712 |
|
62 | 713 |
if (val >= 0) { |
8908 | 714 |
RETURN (self); |
62 | 715 |
} |
1 | 716 |
if (val != _MIN_INT) { |
8913 | 717 |
RETURN ( __mkSmallInteger(-val) ); |
1 | 718 |
} |
3430 | 719 |
/* only reached for minVal */ |
720 |
RETURN( __MKLARGEINT(-_MIN_INT)); |
|
324 | 721 |
%}. |
5410 | 722 |
^ super abs |
1 | 723 |
! |
724 |
||
725 |
negated |
|
726 |
"return the negative value of the receiver |
|
727 |
reimplemented here for speed" |
|
728 |
||
729 |
%{ /* NOCONTEXT */ |
|
730 |
||
1133 | 731 |
INT val = __intVal(self); |
1 | 732 |
|
733 |
if (val != _MIN_INT) { |
|
8913 | 734 |
RETURN ( __mkSmallInteger(- val) ); |
1 | 735 |
} |
3430 | 736 |
/* only reached for minVal */ |
737 |
RETURN (__MKLARGEINT( -_MIN_INT)); |
|
324 | 738 |
%}. |
5410 | 739 |
^ 0 - self |
3430 | 740 |
"/ "only reached for minVal" |
741 |
"/ ^ (LargeInteger value:(SmallInteger maxVal)) + 1 |
|
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
742 |
! |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
743 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
744 |
quo:aNumber |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
745 |
"return the integer part of the quotient of the receivers value |
1884 | 746 |
and the arguments value. The result is truncated towards zero |
8908 | 747 |
and negative, if the operands signs differ.. |
6067 | 748 |
The following is always true: |
8908 | 749 |
(receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
750 |
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
|
751 |
for negative results, the remainder is ignored. |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
752 |
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
|
753 |
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
|
754 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
755 |
%{ /* NOCONTEXT */ |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
756 |
INT val; |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
757 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
758 |
if (__isSmallInteger(aNumber)) { |
8908 | 759 |
val = __intVal(aNumber); |
760 |
if (val != 0) { |
|
8913 | 761 |
RETURN ( __mkSmallInteger(__intVal(self) / val) ); |
8908 | 762 |
} |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
763 |
} else { |
8908 | 764 |
if (__isFraction(aNumber)) { |
765 |
OBJ t; |
|
766 |
INT num, den; |
|
767 |
||
768 |
t = __FractionInstPtr(aNumber)->f_numerator; |
|
769 |
if (__isSmallInteger(t)) { |
|
770 |
num = __intVal(t); |
|
771 |
t = __FractionInstPtr(aNumber)->f_denominator; |
|
772 |
if (__isSmallInteger(t)) { |
|
773 |
den = __intVal(t); |
|
8913 | 774 |
RETURN ( __mkSmallInteger(__intVal(self) * den / num )); |
8908 | 775 |
} |
776 |
} |
|
777 |
} |
|
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
778 |
} |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
779 |
%}. |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
780 |
(aNumber = 0) ifTrue:[ |
8908 | 781 |
^ ZeroDivide raiseRequestWith:thisContext. |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
782 |
]. |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
783 |
^ self retry:#quo: coercing:aNumber |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
784 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
785 |
" |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
786 |
9 // 4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
787 |
-9 // 4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
788 |
9 quo:4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
789 |
-9 quo:4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
790 |
" |
1 | 791 |
! ! |
792 |
||
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
793 |
|
1 | 794 |
!SmallInteger methodsFor:'bit operators'! |
795 |
||
701 | 796 |
bitAnd:anInteger |
797 |
"return the bitwise-and of the receiver and the argument, anInteger" |
|
1 | 798 |
|
799 |
%{ /* NOCONTEXT */ |
|
800 |
||
701 | 801 |
/* anding the tags doesn't change it */ |
802 |
if (__isSmallInteger(anInteger)) { |
|
8908 | 803 |
RETURN ( ((OBJ) ((INT)self & (INT)anInteger)) ); |
1 | 804 |
} |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
805 |
%}. |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
806 |
anInteger class == LargeInteger ifTrue:[ |
8908 | 807 |
^ anInteger bitAnd:self |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
808 |
]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
809 |
^ self retry:#bitAnd: coercing:anInteger |
701 | 810 |
|
811 |
"(2r001010100 bitAnd:2r00001111) radixPrintStringRadix:2" |
|
1 | 812 |
! |
813 |
||
8334 | 814 |
bitAt:anIntegerIndex |
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
815 |
"return the value of the index's bit (index starts at 1). |
8908 | 816 |
Notice: the result of bitAt: on negative receivers is not |
817 |
defined in the language standard (since the implementation |
|
818 |
is free to choose any internal representation for integers)" |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
819 |
|
8334 | 820 |
%{ /* NOCONTEXT */ |
821 |
||
822 |
if (__isSmallInteger(anIntegerIndex)) { |
|
8908 | 823 |
INT idx = __smallIntegerVal(anIntegerIndex); |
824 |
if (idx > 0) { |
|
825 |
if (idx > N_INT_BITS) { |
|
826 |
RETURN(__mkSmallInteger(0)); |
|
827 |
} |
|
828 |
RETURN((__smallIntegerVal(self) & (1 << (idx-1))) ? __mkSmallInteger(1) : __mkSmallInteger(0)); |
|
829 |
} |
|
8334 | 830 |
} |
831 |
%}. |
|
832 |
||
8908 | 833 |
^ SubscriptOutOfBoundsSignal |
834 |
raiseRequestWith:anIntegerIndex |
|
835 |
errorString:'index out of bounds' |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
836 |
|
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
837 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
838 |
16r00000001 bitAt:0 |
8908 | 839 |
16r00000001 bitAt:1 |
840 |
16r00000001 bitAt:2 |
|
841 |
16r00008000 bitAt:16 |
|
842 |
16r00800000 bitAt:24 |
|
843 |
16r08000000 bitAt:28 |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
844 |
16r10000000 bitAt:29 |
8908 | 845 |
16r20000000 bitAt:30 |
846 |
16r40000000 bitAt:31 |
|
847 |
16r80000000 bitAt:32 |
|
848 |
16r100000000 bitAt:33 |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
849 |
" |
8334 | 850 |
|
851 |
" Smalltalk implementation: |
|
852 |
|mask| |
|
853 |
||
854 |
anIntegerIndex <= 0 ifTrue:[ |
|
8908 | 855 |
^ SubscriptOutOfBoundsSignal |
856 |
raiseRequestWith:anIntegerIndex |
|
857 |
errorString:'index out of bounds' |
|
8334 | 858 |
]. |
859 |
(anIntegerIndex > SmallInteger maxBits) ifTrue:[^ 0]. |
|
860 |
mask := 1 bitShift:(anIntegerIndex - 1). |
|
861 |
((self bitAnd:mask) == 0) ifTrue:[^ 0]. |
|
862 |
^ 1 |
|
863 |
" |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
864 |
! |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
865 |
|
3156 | 866 |
bitClear:anInteger |
867 |
"return the bitwise-and of the receiver and the complement of the argument, anInteger, |
|
868 |
returning the receiver with bits of the argument cleared." |
|
869 |
||
870 |
%{ /* NOCONTEXT */ |
|
871 |
||
872 |
/* anding the tags doesn't change it */ |
|
873 |
if (__isSmallInteger(anInteger)) { |
|
8908 | 874 |
RETURN ( ((OBJ) (((INT)self & ~(INT)anInteger) | TAG_INT)) ); |
3156 | 875 |
} |
876 |
%}. |
|
877 |
^ self retry:#bitClear: coercing:anInteger |
|
878 |
||
879 |
" |
|
880 |
(2r001010100 bitClear:2r00001111) radixPrintStringRadix:2 |
|
5458 | 881 |
(2r111111111 bitClear:2r00001000) radixPrintStringRadix:2 |
3156 | 882 |
" |
883 |
! |
|
884 |
||
11493 | 885 |
bitCount |
886 |
"return the number of 1-bits in the receiver" |
|
887 |
||
888 |
%{ /* NOCONTEXT */ |
|
889 |
int _cnt = 0; |
|
890 |
int _self = __intVal(self); |
|
891 |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
892 |
int _counts[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; |
|
893 |
||
894 |
_cnt = _counts[_self & 0xF]; _self = _self>>4; // ______xx |
|
895 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
896 |
if (_self != 0) { |
|
897 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; // xxxxxx__ |
|
898 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
899 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
900 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
901 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
902 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
903 |
# if __POINTER_SIZE__ == 8 |
|
904 |
if (_self != 0) { |
|
905 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
906 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
907 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
908 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
909 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
910 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
911 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
912 |
_cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
913 |
} |
|
914 |
# endif |
|
915 |
} |
|
916 |
||
917 |
RETURN ( __MKSMALLINT(_cnt)); |
|
918 |
%} |
|
919 |
||
920 |
" |
|
921 |
1 to:10000 do:[:n | |
|
922 |
self assert:(n bitCount = ((n printStringRadix:2) occurrencesOf:$1)) |
|
923 |
] |
|
924 |
" |
|
925 |
! |
|
926 |
||
701 | 927 |
bitInvert |
928 |
"return the value of the receiver with all bits inverted" |
|
62 | 929 |
|
1 | 930 |
%{ /* NOCONTEXT */ |
931 |
||
701 | 932 |
/* invert anything except tag bits */ |
933 |
RETURN ( ((OBJ) ((INT)self ^ ~TAG_MASK)) ); |
|
5412 | 934 |
%}. |
935 |
^ self primitiveFailed |
|
701 | 936 |
! |
1 | 937 |
|
701 | 938 |
bitOr:anInteger |
939 |
"return the bitwise-or of the receiver and the argument, anInteger" |
|
940 |
||
941 |
%{ /* NOCONTEXT */ |
|
942 |
||
943 |
/* oring the tags doesn't change it */ |
|
944 |
if (__isSmallInteger(anInteger)) { |
|
8908 | 945 |
RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) ); |
1 | 946 |
} |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
947 |
%}. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
948 |
^ self retry:#bitOr: coercing:anInteger |
701 | 949 |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
950 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
951 |
(2r000000100 bitOr:2r00000011) radixPrintStringRadix:2 |
8908 | 952 |
(0 bitOr:16r20000000) hexPrintString |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
953 |
(0 bitOr:16r40000000) hexPrintString |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
954 |
(0 bitOr:16r80000000) hexPrintString |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
955 |
" |
1 | 956 |
! |
957 |
||
958 |
bitShift:shiftCount |
|
959 |
"return the value of the receiver shifted by shiftCount bits; |
|
62 | 960 |
leftShift if shiftCount > 0; rightShift otherwise. |
8908 | 961 |
Notice: the result of bitShift: on negative receivers is not |
962 |
defined in the language standard (since the implementation |
|
963 |
is free to choose any internal representation for integers). |
|
964 |
However, ST/X preserves the sign." |
|
1 | 965 |
|
966 |
%{ /* NOCONTEXT */ |
|
967 |
||
968 |
INT bits, count; |
|
969 |
||
252 | 970 |
if (__isSmallInteger(shiftCount)) { |
8908 | 971 |
bits = __intVal(self); |
972 |
if (bits == 0) { |
|
973 |
RETURN (self); |
|
974 |
} |
|
975 |
||
976 |
count = __intVal(shiftCount); |
|
977 |
||
978 |
if (count > 0) { |
|
979 |
/* |
|
980 |
* a left shift |
|
981 |
*/ |
|
4183
4a745f6f47b6
oops - longlong shift fails with i386,GCC
Claus Gittinger <cg@exept.de>
parents:
4177
diff
changeset
|
982 |
#if defined(USE_LONGLONG_FOR_SHIFT) |
8908 | 983 |
if (count <= N_INT_BITS) { |
984 |
unsigned LONGLONG result; |
|
985 |
||
986 |
result = (unsigned LONGLONG)bits; |
|
987 |
result <<= count; |
|
988 |
if (result <= _MAX_INT) { |
|
8913 | 989 |
RETURN ( __mkSmallInteger(result) ); |
8908 | 990 |
} |
991 |
{ |
|
992 |
RETURN (__MKLARGEINT64(1, (INT)(result >> 32), (INT)(result & 0xFFFFFFFF))); |
|
993 |
} |
|
994 |
} |
|
95 | 995 |
#else |
8908 | 996 |
/* |
997 |
* check for overflow |
|
998 |
*/ |
|
999 |
if (count < (N_INT_BITS-1)) { |
|
1000 |
if (! (bits >> (N_INT_BITS - 1 - count))) { |
|
8913 | 1001 |
RETURN ( __mkSmallInteger(bits << count) ); |
8908 | 1002 |
} |
1003 |
/* |
|
1004 |
* so, there is an overflow ... |
|
1005 |
* handle it as largeInteger |
|
1006 |
*/ |
|
1007 |
/* FALL THROUGH */ |
|
1008 |
} |
|
95 | 1009 |
#endif |
8908 | 1010 |
} else { |
1011 |
if (count == 0) { |
|
1012 |
RETURN (self); |
|
1013 |
} |
|
1014 |
||
1015 |
/* |
|
1016 |
* right shifts cannot overflow |
|
1017 |
* |
|
1018 |
* some machines ignore shifts bigger than |
|
1019 |
* the number of bits in an int ... |
|
1020 |
*/ |
|
1021 |
count = -count; |
|
1022 |
if (count > (N_INT_BITS-1)) { |
|
8913 | 1023 |
RETURN (__mkSmallInteger(0)); |
8908 | 1024 |
} |
1025 |
||
8913 | 1026 |
RETURN ( __mkSmallInteger(bits >> count) ); |
8908 | 1027 |
} |
1 | 1028 |
} |
85 | 1029 |
%}. |
1030 |
(shiftCount isMemberOf:SmallInteger) ifTrue:[ |
|
8908 | 1031 |
^ (LargeInteger value:self) bitShift:shiftCount |
85 | 1032 |
]. |
6400
3746d31f044a
bitShift:float trouble fixed.
Claus Gittinger <cg@exept.de>
parents:
6344
diff
changeset
|
1033 |
^ self bitShift:(1 coerce:shiftCount) "/ is this a good idea ? |
1 | 1034 |
! |
1035 |
||
701 | 1036 |
bitTest:aMask |
5782 | 1037 |
"return true, if any bit from aMask is set in the receiver. |
1038 |
I.e. true, if the bitwise-AND of the receiver and the argument, anInteger |
|
1039 |
is non-0, false otherwise." |
|
1 | 1040 |
|
1041 |
%{ /* NOCONTEXT */ |
|
1042 |
||
701 | 1043 |
/* and all bits except tag */ |
1044 |
if (__isSmallInteger(aMask)) { |
|
8908 | 1045 |
RETURN ( ((INT)self & ((INT)aMask & ~TAG_MASK)) ? true : false ); |
1 | 1046 |
} |
1716
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1047 |
%}. |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
1048 |
aMask class == LargeInteger ifTrue:[ |
8908 | 1049 |
^ (aMask bitAnd:self) ~~ 0 |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
1050 |
]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1051 |
^ self retry:#bitTest: coercing:aMask |
1 | 1052 |
! |
1053 |
||
1054 |
bitXor:anInteger |
|
13 | 1055 |
"return the bitwise-exclusive-or of the receiver and the argument, anInteger" |
1 | 1056 |
|
1057 |
%{ /* NOCONTEXT */ |
|
1058 |
||
1059 |
/* xoring the tags turns it off - or it in again */ |
|
252 | 1060 |
if (__isSmallInteger(anInteger)) { |
214 | 1061 |
RETURN ( (OBJ)( ((INT)self ^ (INT)anInteger) | TAG_INT) ); |
1 | 1062 |
} |
1716
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1063 |
%}. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1064 |
^ self retry:#bitXor: coercing:anInteger |
1 | 1065 |
! |
1066 |
||
3683 | 1067 |
clearBit:anInteger |
8937 | 1068 |
"return a new integer where the specified bit is off. |
5209 | 1069 |
Bits are counted from 1 starting with the least significant. |
1070 |
The methods name may be missleading: the receiver is not changed, |
|
1071 |
but a new number is returned. Should be named #withBitCleared:" |
|
3683 | 1072 |
|
1073 |
%{ /* NOCONTEXT */ |
|
1074 |
||
1075 |
if (__isSmallInteger(anInteger)) { |
|
10342 | 1076 |
int index = __intVal(anInteger); |
1077 |
||
1078 |
if (index > 0) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1079 |
#if __POINTER_SIZE__ == 8 |
10342 | 1080 |
if (index <= 62) |
3683 | 1081 |
#else |
10342 | 1082 |
if (index <= 30) |
3683 | 1083 |
#endif |
10342 | 1084 |
{ |
1085 |
INT mask = __MASKSMALLINT(1 << (index-1)); |
|
1086 |
||
1087 |
RETURN ( ((OBJ) ((INT)self & ~(INT)mask)) ); |
|
1088 |
} |
|
1089 |
RETURN (self); /* nothing to do ... */ |
|
1090 |
} |
|
3683 | 1091 |
} |
1092 |
%}. |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1093 |
^ super clearBit:anInteger |
3683 | 1094 |
|
1095 |
" |
|
1096 |
(16r401 clearBit:1 ) hexPrintString |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1097 |
(16r401 clearBit:0 ) hexPrintString |
3683 | 1098 |
(16r3fffffff clearBit:1) hexPrintString |
8908 | 1099 |
(16r3fffffff clearBit:29) hexPrintString |
1100 |
(16r3fffffff clearBit:30) hexPrintString |
|
1101 |
(16r3fffffff clearBit:31) hexPrintString |
|
1102 |
(16r3fffffff bitAt:29) hexPrintString |
|
1103 |
(16r3fffffff bitAt:30) hexPrintString |
|
1104 |
(16r3fffffff bitAt:31) hexPrintString |
|
1105 |
(16r40000001 clearBit:1) hexPrintString |
|
1106 |
(16rF0000001 clearBit:29) hexPrintString |
|
3683 | 1107 |
(16rF0000001 clearBit:30) hexPrintString |
8908 | 1108 |
(16rF0000001 clearBit:31) hexPrintString |
1109 |
(16rF0000001 clearBit:32) hexPrintString |
|
3683 | 1110 |
" |
1111 |
! |
|
1112 |
||
701 | 1113 |
highBit |
1114 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
8908 | 1115 |
starts at 1 for the least significant bit. |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1116 |
Returns 0 if no bit is set." |
1 | 1117 |
|
1118 |
%{ /* NOCONTEXT */ |
|
1119 |
||
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1120 |
unsigned INT bits; |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1121 |
int index; |
701 | 1122 |
|
1133 | 1123 |
bits = __intVal(self); |
701 | 1124 |
if (bits == 0) { |
8913 | 1125 |
RETURN ( __mkSmallInteger(0) ); |
701 | 1126 |
} |
2785 | 1127 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1128 |
#ifdef __BSR |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1129 |
/* |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1130 |
* so much for CISC CPUS: |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1131 |
* 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
|
1132 |
* (but saves a few code-bytes, though) |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1133 |
*/ |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1134 |
index = __BSR(bits); |
4955
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
1135 |
#else |
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
1136 |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1137 |
index = 0; |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1138 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1139 |
# if __POINTER_SIZE__ == 8 |
4951 | 1140 |
if (bits & 0xFFFFFFFF00000000L) { |
8908 | 1141 |
index += 32; bits = bits >> 32; |
4951 | 1142 |
} |
4955
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
1143 |
# endif |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1144 |
if (bits & 0xFFFF0000L) { |
8908 | 1145 |
index += 16; bits = bits >> 16; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1146 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1147 |
if (bits & 0xFF00) { |
8908 | 1148 |
index += 8; bits = bits >> 8; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1149 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1150 |
if (bits & 0xF0) { |
8908 | 1151 |
index += 4; bits = bits >> 4; |
1 | 1152 |
} |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1153 |
if (bits & 0xC) { |
8908 | 1154 |
index += 2; bits = bits >> 2; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1155 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1156 |
if (bits & 0x2) { |
8908 | 1157 |
index += 1; bits = bits >> 1; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1158 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1159 |
#endif /* no BSR instruction */ |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1160 |
|
8913 | 1161 |
RETURN ( __mkSmallInteger(index+1) ); |
1 | 1162 |
%} |
4951 | 1163 |
" |
8908 | 1164 |
2r0 highBit |
1165 |
2r1 highBit |
|
1166 |
2r10 highBit |
|
1167 |
2r100 highBit |
|
1168 |
2r1000 highBit |
|
1169 |
2r100000000000 highBit |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1170 |
|
4951 | 1171 |
((0 to:64) collect:[:s | 1 bitShift:s]) |
8908 | 1172 |
collect:[:n | n highBit] |
4951 | 1173 |
|
1174 |
(((0 to:64) collect:[:s | 1 bitShift:s]) |
|
8908 | 1175 |
collect:[:n | n highBit]) = (1 to:65) |
4954
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 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1179 |
Time millisecondsToRun:[ |
8908 | 1180 |
1000000 timesRepeat:[ |
1181 |
2r1 highBit |
|
1182 |
] |
|
1183 |
] |
|
4954
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 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1186 |
Time millisecondsToRun:[ |
8908 | 1187 |
1000000 timesRepeat:[ |
1188 |
2r1111 highBit |
|
1189 |
] |
|
1190 |
] |
|
4954
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 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1193 |
Time millisecondsToRun:[ |
8908 | 1194 |
1000000 timesRepeat:[ |
1195 |
2r11111111111111 highBit |
|
1196 |
] |
|
1197 |
] |
|
4954
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 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1200 |
Time millisecondsToRun:[ |
8908 | 1201 |
1000000 timesRepeat:[ |
1202 |
2r11111111111111111111111111 highBit |
|
1203 |
] |
|
1204 |
] |
|
4951 | 1205 |
" |
1206 |
||
1207 |
" |
|
8908 | 1208 |
2r000100 highBit |
1209 |
2r010100 highBit |
|
1210 |
2r000001 highBit |
|
1211 |
0 highBit |
|
1212 |
SmallInteger maxVal highBit |
|
4951 | 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)) { |
8908 | 1225 |
int index = __intVal(anInteger); |
1226 |
||
1227 |
if (index > 0) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1228 |
#if __POINTER_SIZE__ == 8 |
8908 | 1229 |
if (index <= 62) |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1230 |
#else |
8908 | 1231 |
if (index <= 30) |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1232 |
#endif |
8908 | 1233 |
{ |
1234 |
INT mask = __MASKSMALLINT(1 << (index-1)); |
|
1235 |
||
1236 |
RETURN ( ((OBJ) ((INT)self ^ (INT)mask)) ); |
|
1237 |
} |
|
1238 |
} |
|
5496
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 |
" |
8908 | 1244 |
(16r401 invertBit:2 ) hexPrintString |
1245 |
(16r401 invertBit:1 ) hexPrintString |
|
1246 |
(16r30000000 invertBit:1) hexPrintString |
|
1247 |
(16r40000000 invertBit:0) hexPrintString |
|
1248 |
(16r0 invertBit:29) hexPrintString |
|
1249 |
(16r0 invertBit:30) hexPrintString |
|
1250 |
(16r0 invertBit:31) hexPrintString |
|
1251 |
(16r0 invertBit:32) hexPrintString |
|
1252 |
(16r0 invertBit:33) hexPrintString |
|
1253 |
(16r0 invertBit:100) hexPrintString |
|
5496
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 |
|
8908 | 1261 |
starts at 1 for the least significant bit. |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1262 |
Returns 0 if no bit is set." |
701 | 1263 |
|
1264 |
%{ /* NOCONTEXT */ |
|
1265 |
||
6482 | 1266 |
unsigned INT bits; |
1267 |
int index; |
|
701 | 1268 |
|
1133 | 1269 |
bits = __intVal(self); |
701 | 1270 |
if (bits == 0) { |
8913 | 1271 |
RETURN ( __mkSmallInteger(0) ); |
701 | 1272 |
} |
2431 | 1273 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1274 |
#ifdef __BSF |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1275 |
/* |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1276 |
* so much for CISC CPUS: |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1277 |
* the following code is only marginally faster on a PIII-400 |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1278 |
* (and not at all faster on an Athlon...) |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1279 |
* but saves a few code-bytes, though. |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1280 |
*/ |
6482 | 1281 |
index = __BSF(bits); |
8913 | 1282 |
RETURN ( __mkSmallInteger(index + 1) ); |
4955
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
1283 |
#else |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1284 |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1285 |
index = 1; |
2431 | 1286 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1287 |
# if __POINTER_SIZE__ == 8 |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1288 |
if ((bits<<32) == 0) { |
8908 | 1289 |
index += 32; bits >>= 32; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1290 |
} |
4955
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
1291 |
# endif |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1292 |
|
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1293 |
if ((bits & 0xFFFF)==0) { |
8908 | 1294 |
index += 16; bits >>= 16; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1295 |
} |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1296 |
if ((bits & 0xFF)==0) { |
8908 | 1297 |
index += 8; bits >>= 8; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1298 |
} |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1299 |
if ((bits & 0xF)==0) { |
8908 | 1300 |
index += 4; bits >>= 4; |
701 | 1301 |
} |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1302 |
if ((bits & 0x3)==0) { |
8908 | 1303 |
index += 2; bits >>= 2; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1304 |
} |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1305 |
if ((bits & 0x1)==0) { |
8908 | 1306 |
index += 1; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1307 |
} |
4955
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
1308 |
#endif |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1309 |
|
8913 | 1310 |
RETURN ( __mkSmallInteger(index) ); |
701 | 1311 |
%} |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1312 |
|
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1313 |
" |
8908 | 1314 |
0 lowBit |
1315 |
2r0001 lowBit |
|
1316 |
2r0010 lowBit |
|
1317 |
2r0100 lowBit |
|
1318 |
2r1000 lowBit |
|
1319 |
||
1320 |
2r000100 lowBit |
|
1321 |
2r010010 lowBit |
|
1322 |
2r100001 lowBit |
|
1323 |
16r1000 lowBit |
|
1324 |
16r1000000 lowBit |
|
1325 |
16r1000000000000000 lowBit |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1326 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1327 |
Time millisecondsToRun:[ |
8908 | 1328 |
1000000 timesRepeat:[ |
1329 |
2r1000 lowBit |
|
1330 |
] |
|
1331 |
] |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1332 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1333 |
Time millisecondsToRun:[ |
8908 | 1334 |
1000000 timesRepeat:[ |
1335 |
2r11110000000 lowBit |
|
1336 |
] |
|
1337 |
] |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1338 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1339 |
Time millisecondsToRun:[ |
8908 | 1340 |
1000000 timesRepeat:[ |
1341 |
2r1000000000000 lowBit |
|
1342 |
] |
|
1343 |
] |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
1344 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1345 |
Time millisecondsToRun:[ |
8908 | 1346 |
1000000 timesRepeat:[ |
1347 |
2r1000000000000000000000000000 lowBit |
|
1348 |
] |
|
1349 |
] |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
1350 |
" |
5209 | 1351 |
! |
1352 |
||
10972 | 1353 |
rightShift:shiftCount |
1354 |
"return the value of the receiver shifted by shiftCount bits; |
|
1355 |
right shift if shiftCount > 0; left shift otherwise. |
|
1356 |
Notice: the result of bitShift: on negative receivers is not |
|
1357 |
defined in the language standard (since the implementation |
|
1358 |
is free to choose any internal representation for integers). |
|
1359 |
However, ST/X preserves the sign." |
|
1360 |
||
1361 |
%{ /* NOCONTEXT */ |
|
1362 |
||
1363 |
INT bits, count; |
|
1364 |
||
1365 |
if (__isSmallInteger(shiftCount)) { |
|
1366 |
bits = __intVal(self); |
|
1367 |
if (bits == 0) { |
|
1368 |
RETURN (self); |
|
1369 |
} |
|
1370 |
||
1371 |
count = __intVal(shiftCount); |
|
1372 |
||
1373 |
if (count < 0) { |
|
1374 |
/* |
|
1375 |
* a left shift |
|
1376 |
*/ |
|
1377 |
count = -count; |
|
1378 |
#if defined(USE_LONGLONG_FOR_SHIFT) |
|
1379 |
if (count <= N_INT_BITS) { |
|
1380 |
unsigned LONGLONG result; |
|
1381 |
||
1382 |
result = (unsigned LONGLONG)bits; |
|
1383 |
result <<= count; |
|
1384 |
if (result <= _MAX_INT) { |
|
1385 |
RETURN ( __mkSmallInteger(result) ); |
|
1386 |
} |
|
1387 |
{ |
|
1388 |
RETURN (__MKLARGEINT64(1, (INT)(result >> 32), (INT)(result & 0xFFFFFFFF))); |
|
1389 |
} |
|
1390 |
} |
|
1391 |
#else |
|
1392 |
/* |
|
1393 |
* check for overflow |
|
1394 |
*/ |
|
1395 |
if (count < (N_INT_BITS-1)) { |
|
1396 |
if (! (bits >> (N_INT_BITS - 1 - count))) { |
|
1397 |
RETURN ( __mkSmallInteger(bits << count) ); |
|
1398 |
} |
|
1399 |
/* |
|
1400 |
* so, there is an overflow ... |
|
1401 |
* handle it as largeInteger |
|
1402 |
*/ |
|
1403 |
/* FALL THROUGH */ |
|
1404 |
} |
|
1405 |
#endif |
|
1406 |
} else { |
|
1407 |
if (count == 0) { |
|
1408 |
RETURN (self); |
|
1409 |
} |
|
1410 |
||
1411 |
/* |
|
1412 |
* right shifts cannot overflow |
|
1413 |
* |
|
1414 |
* some machines ignore shifts bigger than |
|
1415 |
* the number of bits in an int ... |
|
1416 |
*/ |
|
1417 |
if (count > (N_INT_BITS-1)) { |
|
1418 |
RETURN (__mkSmallInteger(0)); |
|
1419 |
} |
|
1420 |
||
1421 |
RETURN ( __mkSmallInteger(bits >> count) ); |
|
1422 |
} |
|
1423 |
} |
|
1424 |
%}. |
|
1425 |
(shiftCount isMemberOf:SmallInteger) ifTrue:[ |
|
1426 |
^ (LargeInteger value:self) rightShift:shiftCount |
|
1427 |
]. |
|
1428 |
^ self rightShift:(1 coerce:shiftCount) "/ is this a good idea ? |
|
1429 |
||
1430 |
||
1431 |
" |
|
1432 |
16 rightShift:2 |
|
1433 |
4 rightShift:-2 |
|
1434 |
" |
|
1435 |
! |
|
1436 |
||
5209 | 1437 |
setBit:anInteger |
8937 | 1438 |
"return a new integer where the specified bit is on. |
5209 | 1439 |
Bits are counted from 1 starting with the least significant. |
1440 |
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
|
1441 |
but a new number is returned. Should be named #withBitSet:" |
5209 | 1442 |
|
1443 |
%{ /* NOCONTEXT */ |
|
1444 |
||
1445 |
if (__isSmallInteger(anInteger)) { |
|
10342 | 1446 |
int index = __intVal(anInteger); |
1447 |
||
1448 |
if (index > 0) { |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1449 |
#if __POINTER_SIZE__ == 8 |
10342 | 1450 |
if (index <= 62) |
5209 | 1451 |
#else |
10342 | 1452 |
if (index <= 30) |
5209 | 1453 |
#endif |
10342 | 1454 |
{ |
1455 |
INT mask = __MASKSMALLINT(1 << (index-1)); |
|
1456 |
||
1457 |
RETURN ( ((OBJ) ((INT)self | (INT)mask)) ); |
|
1458 |
} |
|
1459 |
} |
|
5209 | 1460 |
} |
1461 |
%}. |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1462 |
^ super setBit:anInteger |
5209 | 1463 |
|
1464 |
" |
|
8908 | 1465 |
(16r401 setBit:2 ) hexPrintString |
1466 |
(16r30000000 setBit:1) hexPrintString |
|
1467 |
(16r40000000 setBit:0) hexPrintString |
|
1468 |
(16r0 setBit:29) hexPrintString |
|
1469 |
(16r0 setBit:30) hexPrintString |
|
1470 |
(16r0 setBit:31) hexPrintString |
|
1471 |
(16r0 setBit:32) hexPrintString |
|
1472 |
(16r0 setBit:33) hexPrintString |
|
1473 |
(16r0 setBit:100) hexPrintString |
|
5209 | 1474 |
" |
6417 | 1475 |
! ! |
1476 |
||
7267 | 1477 |
!SmallInteger methodsFor:'bit operators-32bit'! |
6417 | 1478 |
|
1479 |
asUnsigned32 |
|
1480 |
"return a 32-bit integer with my bit-pattern, but positive. |
|
1481 |
May be required for bit operations on the sign-bit and/or to |
|
1482 |
convert C/Java numbers." |
|
1483 |
||
1484 |
self < 0 ifTrue:[ |
|
8908 | 1485 |
^ 16r100000000 + self |
6417 | 1486 |
]. |
1487 |
^ self |
|
1488 |
||
1489 |
" |
|
8908 | 1490 |
(-1 asUnsigned32) hexPrintString |
1491 |
1 asUnsigned32 |
|
1492 |
(SmallInteger minVal asUnsigned32) hexPrintString |
|
1493 |
(SmallInteger maxVal asUnsigned32) hexPrintString |
|
6417 | 1494 |
" |
1495 |
! |
|
1496 |
||
1497 |
bitShift32:shiftCount |
|
1498 |
"return the value of the receiver shifted by shiftCount bits, |
|
1499 |
but only within 32 bits, shifting into/out-of the sign bit. |
|
1500 |
May be useful for communication interfaces, to create ST-numbers |
|
1501 |
from a signed 32bit int value given as individual bytes, |
|
1502 |
or to emulate C/Java semantics." |
|
1503 |
||
1504 |
%{ /* NOCONTEXT */ |
|
1505 |
||
1506 |
INT bits, count; |
|
1507 |
||
1508 |
if (__isSmallInteger(shiftCount)) { |
|
8908 | 1509 |
count = __intVal(shiftCount); |
1510 |
if (count >= 32) { |
|
8913 | 1511 |
RETURN (__mkSmallInteger(0)); |
8908 | 1512 |
} |
1513 |
||
1514 |
bits = __intVal(self); |
|
1515 |
if (count > 0) { |
|
1516 |
bits = bits << count; |
|
1517 |
} else { |
|
1518 |
bits = bits >> (-count); |
|
1519 |
} |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1520 |
#if __POINTER_SIZE__ == 8 |
8908 | 1521 |
bits &= 0xFFFFFFFFL; |
6417 | 1522 |
#endif |
8908 | 1523 |
RETURN (__MKINT(bits)); |
6417 | 1524 |
} |
1525 |
%}. |
|
1526 |
^ self primitiveFailed |
|
1527 |
||
1528 |
" |
|
8908 | 1529 |
128 bitShift:24 |
1530 |
128 bitShift32:24 |
|
1531 |
||
1532 |
1 bitShift:31 |
|
1533 |
1 bitShift32:31 |
|
6417 | 1534 |
" |
6415
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1535 |
! |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1536 |
|
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1537 |
unsignedBitShift32:shiftCount |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1538 |
"return the value of the receiver shifted by shiftCount bits, |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1539 |
but only within 32 unsigned bits. |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1540 |
May be useful for communication interfaces, to create ST-numbers |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1541 |
from an unsigned 32bit int value given as individual byte, |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1542 |
or to emulate C/Java semantics." |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1543 |
|
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1544 |
%{ /* NOCONTEXT */ |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1545 |
|
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1546 |
unsigned INT bits; |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1547 |
INT count; |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1548 |
|
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1549 |
if (__isSmallInteger(shiftCount)) { |
8908 | 1550 |
count = __intVal(shiftCount); |
1551 |
if (count >= 32) { |
|
8913 | 1552 |
RETURN (__mkSmallInteger(0)); |
8908 | 1553 |
} |
1554 |
||
1555 |
bits = __intVal(self); |
|
1556 |
if (count > 0) { |
|
1557 |
bits = bits << count; |
|
1558 |
} else { |
|
1559 |
bits = bits >> (-count); |
|
1560 |
} |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1561 |
#if __POINTER_SIZE__ == 8 |
8908 | 1562 |
bits &= 0xFFFFFFFFL; |
6415
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1563 |
#endif |
8908 | 1564 |
RETURN (__MKUINT(bits)); |
6415
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1565 |
} |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1566 |
%}. |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1567 |
^ self primitiveFailed |
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1568 |
|
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1569 |
" |
8908 | 1570 |
128 unsignedBitShift:24 |
1571 |
128 unsignedBitShift32:24 |
|
1572 |
||
1573 |
1 unsignedBitShift:31 |
|
1574 |
1 unsignedBitShift32:31 |
|
1575 |
||
1576 |
-1 unsignedBitShift32:-1 |
|
1577 |
-1 unsignedBitShift32:1 |
|
6415
f163a6319b5f
unsigned C/Java 32bit shifts
Claus Gittinger <cg@exept.de>
parents:
6400
diff
changeset
|
1578 |
" |
1 | 1579 |
! ! |
1580 |
||
1581 |
!SmallInteger methodsFor:'byte access'! |
|
1582 |
||
1583 |
digitAt:index |
|
1584 |
"return 8 bits of value, starting at byte index" |
|
1585 |
||
1586 |
%{ /* NOCONTEXT */ |
|
1587 |
||
357 | 1588 |
REGISTER INT val; |
1589 |
INT idx; |
|
1 | 1590 |
|
252 | 1591 |
if (__isSmallInteger(index)) { |
4167 | 1592 |
val = __intVal(self); |
1593 |
if (val < 0) |
|
1594 |
val = -val; |
|
1595 |
switch (idx = __intVal(index)) { |
|
1596 |
case 1: |
|
1597 |
break; |
|
1598 |
case 2: |
|
1599 |
val = (val >> 8); |
|
1600 |
break; |
|
1601 |
case 3: |
|
1602 |
val = (val >> 16); |
|
1603 |
break; |
|
1604 |
case 4: |
|
1605 |
val = (val >> 24); |
|
1606 |
break; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1607 |
#if __POINTER_SIZE__ == 8 |
4167 | 1608 |
case 5: |
1609 |
val = (val >> 32); |
|
1610 |
break; |
|
1611 |
case 6: |
|
1612 |
val = (val >> 40); |
|
1613 |
break; |
|
1614 |
case 7: |
|
1615 |
val = (val >> 48); |
|
1616 |
break; |
|
1617 |
case 8: |
|
1618 |
val = (val >> 56); |
|
1619 |
break; |
|
1 | 1620 |
#endif |
4167 | 1621 |
default: |
1622 |
if (idx < 1) |
|
1623 |
goto bad; /* sorry */ |
|
8913 | 1624 |
RETURN (__mkSmallInteger(0)); |
4167 | 1625 |
} |
8913 | 1626 |
RETURN ( __mkSmallInteger( val & 0xFF) ); |
1 | 1627 |
} |
357 | 1628 |
bad: ; |
77 | 1629 |
%}. |
1630 |
index > 0 ifFalse:[ |
|
4167 | 1631 |
" |
1632 |
index less than 1 - not allowed |
|
1633 |
" |
|
1634 |
^ self primitiveFailed |
|
77 | 1635 |
]. |
1636 |
^ 0 |
|
62 | 1637 |
|
357 | 1638 |
" |
1639 |
(16r12345678 digitAt:1) printStringRadix:16 |
|
1640 |
(16r12345678 digitAt:3) printStringRadix:16 |
|
1641 |
(16r12345678 digitAt:15) printStringRadix:16 |
|
1642 |
(16r12345678 digitAt:0) printStringRadix:16 |
|
1643 |
(16r12345678 digitAt:-10) printStringRadix:16 |
|
1644 |
" |
|
701 | 1645 |
! |
1646 |
||
3892 | 1647 |
digitByteAt:index |
1648 |
"return 8 bits of my signed value, starting at byte index. |
|
1649 |
For positive receivers, this is the same as #digitAt:; |
|
1650 |
for negative ones, the actual bit representation is returned." |
|
1651 |
||
1652 |
%{ /* NOCONTEXT */ |
|
1653 |
||
1654 |
REGISTER INT val; |
|
1655 |
INT idx; |
|
1656 |
||
1657 |
if (__isSmallInteger(index)) { |
|
4167 | 1658 |
val = __intVal(self); |
1659 |
switch (idx = __intVal(index)) { |
|
1660 |
case 1: |
|
1661 |
break; |
|
1662 |
case 2: |
|
1663 |
val = (val >> 8); |
|
1664 |
break; |
|
1665 |
case 3: |
|
1666 |
val = (val >> 16); |
|
1667 |
break; |
|
1668 |
case 4: |
|
1669 |
val = (val >> 24); |
|
1670 |
break; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1671 |
#if __POINTER_SIZE__ == 8 |
4167 | 1672 |
case 5: |
1673 |
val = (val >> 32); |
|
1674 |
break; |
|
1675 |
case 6: |
|
1676 |
val = (val >> 40); |
|
1677 |
break; |
|
1678 |
case 7: |
|
1679 |
val = (val >> 48); |
|
1680 |
break; |
|
1681 |
case 8: |
|
1682 |
val = (val >> 56); |
|
1683 |
break; |
|
3892 | 1684 |
#endif |
4167 | 1685 |
default: |
1686 |
if (idx < 1) |
|
1687 |
goto bad; /* sorry */ |
|
1688 |
if (val < 0) { |
|
8913 | 1689 |
RETURN (__mkSmallInteger(0xFF)); |
4167 | 1690 |
} |
8913 | 1691 |
RETURN (__mkSmallInteger(0)); |
4167 | 1692 |
} |
8913 | 1693 |
RETURN ( __mkSmallInteger( val & 0xFF) ); |
3892 | 1694 |
} |
1695 |
bad: ; |
|
1696 |
%}. |
|
1697 |
index > 0 ifFalse:[ |
|
4167 | 1698 |
" |
1699 |
index less than 1 - not allowed |
|
1700 |
" |
|
1701 |
^ self primitiveFailed |
|
3892 | 1702 |
]. |
1703 |
^ 0 |
|
1704 |
||
1705 |
" |
|
8908 | 1706 |
(10 digitByteAt:1) printStringRadix:16 |
1707 |
(10 digitByteAt:3) printStringRadix:16 |
|
3892 | 1708 |
(-10 digitByteAt:1) printStringRadix:16 |
1709 |
(-10 digitByteAt:3) printStringRadix:16 |
|
1710 |
" |
|
1711 |
! |
|
1712 |
||
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1713 |
digitBytes |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1714 |
"return a byteArray filled with the receivers bits |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1715 |
(8 bits of the absolute value per element), |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1716 |
least significant byte is first" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1717 |
|
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1718 |
|absValue |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1719 |
b1 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1720 |
b2 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1721 |
b3 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1722 |
b4 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1723 |
b5 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1724 |
b6 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1725 |
b7 "{ Class: SmallInteger }" digitByteArray| |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1726 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1727 |
" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1728 |
could have simply created a 4-byte largeinteger and normalize it. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1729 |
The code below does the normalize right away, avoiding the |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1730 |
overhead of producing any intermediate byte-arrays (and the scanning) |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1731 |
" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1732 |
self == 0 ifTrue: [ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1733 |
^ ByteArray with:0. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1734 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1735 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1736 |
self < 0 ifTrue: [ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1737 |
absValue := self negated |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1738 |
] ifFalse: [ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1739 |
absValue := self. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1740 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1741 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1742 |
b1 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1743 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1744 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1745 |
digitByteArray := ByteArray with:b1 |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1746 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1747 |
b2 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1748 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1749 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1750 |
digitByteArray := ByteArray with:b1 with:b2 |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1751 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1752 |
b3 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1753 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1754 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1755 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1756 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1757 |
b4 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1758 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1759 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1760 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1761 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1762 |
b5 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1763 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1764 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1765 |
digitByteArray := ByteArray new:5. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1766 |
digitByteArray at:1 put:b1. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1767 |
digitByteArray at:2 put:b2. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1768 |
digitByteArray at:3 put:b3. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1769 |
digitByteArray at:4 put:b4. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1770 |
digitByteArray at:5 put:b5. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1771 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1772 |
b6 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1773 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1774 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1775 |
digitByteArray := ByteArray new:6. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1776 |
digitByteArray at:1 put:b1. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1777 |
digitByteArray at:2 put:b2. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1778 |
digitByteArray at:3 put:b3. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1779 |
digitByteArray at:4 put:b4. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1780 |
digitByteArray at:5 put:b5. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1781 |
digitByteArray at:6 put:b6. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1782 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1783 |
b7 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1784 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1785 |
absValue == 0 ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1786 |
digitByteArray := ByteArray new:7. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1787 |
digitByteArray at:1 put:b1. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1788 |
digitByteArray at:2 put:b2. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1789 |
digitByteArray at:3 put:b3. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1790 |
digitByteArray at:4 put:b4. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1791 |
digitByteArray at:5 put:b5. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1792 |
digitByteArray at:6 put:b6. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1793 |
digitByteArray at:7 put:b7. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1794 |
] ifFalse:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1795 |
digitByteArray := ByteArray new:8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1796 |
digitByteArray at:1 put:b1. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1797 |
digitByteArray at:2 put:b2. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1798 |
digitByteArray at:3 put:b3. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1799 |
digitByteArray at:4 put:b4. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1800 |
digitByteArray at:5 put:b5. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1801 |
digitByteArray at:6 put:b6. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1802 |
digitByteArray at:7 put:b7. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1803 |
digitByteArray at:8 put:absValue. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1804 |
] |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1805 |
] |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1806 |
] |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1807 |
] |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1808 |
] |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1809 |
] |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1810 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1811 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1812 |
^ digitByteArray |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1813 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1814 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1815 |
16r12 digitBytes |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1816 |
16r1234 digitBytes |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1817 |
16r12345678 digitBytes |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1818 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1819 |
! |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1820 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1821 |
digitBytesMSB:msbFlag |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1822 |
"return a byteArray filled with the receivers bits |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1823 |
(8 bits of the absolute value per element), |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1824 |
if msbflag = true, most significant byte is first, |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1825 |
otherwise least significant byte is first" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1826 |
|
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1827 |
msbFlag ifTrue:[ |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1828 |
^ self digitBytes reversed. "digitBytes has been just created - reverse inplace" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1829 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1830 |
^ self digitBytes |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1831 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1832 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1833 |
16r12 digitBytesMSB:true |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1834 |
16r1234 digitBytesMSB:true |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1835 |
16r1234 digitBytesMSB:false |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1836 |
16r12345678 digitBytesMSB:true |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1837 |
16r12345678 digitBytesMSB:false |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1838 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1839 |
! |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1840 |
|
701 | 1841 |
digitLength |
2815 | 1842 |
"return the number bytes required to represent this Integer. |
1843 |
For negative receivers, the digitLength of its absolute value |
|
1844 |
is returned." |
|
1845 |
||
1846 |
%{ /* NOCONTEXT */ |
|
1847 |
||
1848 |
INT val = __intVal(self); |
|
1849 |
||
1850 |
if (val < 0) { |
|
4167 | 1851 |
val = -val; |
2815 | 1852 |
} |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1853 |
#if __POINTER_SIZE__ == 8 |
2815 | 1854 |
if (val & 0xFFFFFFFF00000000L) { |
4167 | 1855 |
if (val & 0xFFFF000000000000L) { |
1856 |
if (val & 0xFF00000000000000L) { |
|
8913 | 1857 |
RETURN ( __mkSmallInteger(8)); |
4167 | 1858 |
} else { |
8913 | 1859 |
RETURN ( __mkSmallInteger(7)); |
4167 | 1860 |
} |
1861 |
} else { |
|
1862 |
if (val & 0x0000FF0000000000L) { |
|
8913 | 1863 |
RETURN ( __mkSmallInteger(6)); |
4167 | 1864 |
} else { |
8913 | 1865 |
RETURN ( __mkSmallInteger(5)); |
4167 | 1866 |
} |
1867 |
} |
|
2815 | 1868 |
} |
1869 |
#endif |
|
1870 |
||
1871 |
if (val & 0xFFFF0000) { |
|
4167 | 1872 |
if (val & 0xFF000000) { |
8913 | 1873 |
RETURN ( __mkSmallInteger(4)); |
4167 | 1874 |
} else { |
8913 | 1875 |
RETURN ( __mkSmallInteger(3)); |
4167 | 1876 |
} |
2815 | 1877 |
} else { |
4167 | 1878 |
if (val & 0x0000FF00) { |
8913 | 1879 |
RETURN ( __mkSmallInteger(2)); |
4167 | 1880 |
} else { |
8913 | 1881 |
RETURN ( __mkSmallInteger(1)); |
4167 | 1882 |
} |
2815 | 1883 |
} |
1884 |
||
1885 |
%}. |
|
701 | 1886 |
^ self abs highBit - 1 // 8 + 1 |
2815 | 1887 |
|
1888 |
" |
|
1889 |
16rFF00000000000000 digitLength |
|
1890 |
-16rFF00000000000000 digitLength |
|
1891 |
||
1892 |
16rFF000000 digitLength |
|
8908 | 1893 |
16rFF0000 digitLength |
1894 |
16rFF00 digitLength |
|
1895 |
16rFF digitLength |
|
1896 |
-16rFF000000 digitLength |
|
1897 |
-16rFF0000 digitLength |
|
1898 |
-16rFF00 digitLength |
|
1899 |
-16rFF digitLength |
|
2815 | 1900 |
" |
1 | 1901 |
! ! |
1902 |
||
701 | 1903 |
!SmallInteger methodsFor:'catching messages'! |
1 | 1904 |
|
701 | 1905 |
basicAt:index |
1906 |
"catch indexed access - report an error |
|
1907 |
defined here since basicAt: in Object ommits the SmallInteger check." |
|
1908 |
||
1909 |
self notIndexed |
|
1 | 1910 |
! |
1911 |
||
701 | 1912 |
basicAt:index put:anObject |
1913 |
"catch indexed access - report an error |
|
1914 |
defined here since basicAt:put: in Object ommits the SmallInteger check." |
|
1915 |
||
1916 |
self notIndexed |
|
1917 |
! |
|
1 | 1918 |
|
701 | 1919 |
basicSize |
1920 |
"return the number of indexed instvars - SmallIntegers have none. |
|
1921 |
Defined here since basicSize in Object ommits the SmallInteger check." |
|
1922 |
||
1923 |
^ 0 |
|
1924 |
! |
|
1925 |
||
1926 |
size |
|
1927 |
"return the number of indexed instvars - SmallIntegers have none." |
|
1928 |
||
1929 |
^ 0 |
|
1 | 1930 |
! ! |
1931 |
||
5240 | 1932 |
!SmallInteger methodsFor:'coercing & converting'! |
1 | 1933 |
|
701 | 1934 |
asCharacter |
1935 |
"Return a character with the receiver as ascii value" |
|
41 | 1936 |
|
701 | 1937 |
^ Character value:self |
1 | 1938 |
! |
1939 |
||
1940 |
asFloat |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1941 |
"return a Float with same value as receiver. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1942 |
Redefined for performance (machine can do it faster)" |
1 | 1943 |
|
1944 |
%{ /* NOCONTEXT */ |
|
1945 |
||
1946 |
OBJ newFloat; |
|
1133 | 1947 |
double dVal = (double)__intVal(self); |
1 | 1948 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
1949 |
__qMKFLOAT(newFloat, dVal); |
1 | 1950 |
RETURN ( newFloat ); |
5412 | 1951 |
%}. |
1952 |
^ self primitiveFailed |
|
1 | 1953 |
! |
1954 |
||
1955 |
asLargeInteger |
|
1956 |
"return a LargeInteger with same value as receiver" |
|
1957 |
||
1958 |
^ LargeInteger value:self |
|
1959 |
! |
|
1960 |
||
1199 | 1961 |
asShortFloat |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1962 |
"return a ShortFloat with same value as receiver. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
1963 |
Redefined for performance (machine can do it faster)" |
1199 | 1964 |
|
1965 |
%{ /* NOCONTEXT */ |
|
1966 |
||
1967 |
OBJ dummy = @global(ShortFloat); |
|
1968 |
OBJ newFloat; |
|
1969 |
float fVal = (float)__intVal(self); |
|
1970 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
1971 |
__qMKSFLOAT(newFloat, fVal); |
1199 | 1972 |
RETURN ( newFloat ); |
5412 | 1973 |
%}. |
1974 |
^ self primitiveFailed |
|
1199 | 1975 |
! |
1976 |
||
8408
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1977 |
codePoint |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1978 |
"for compatibility with Characters. |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1979 |
(Allows for integers to be stored into U16/U32 strings)" |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1980 |
|
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1981 |
^ self |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1982 |
! |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
1983 |
|
701 | 1984 |
coerce:aNumber |
1985 |
"return aNumber converted into receivers type" |
|
1986 |
||
1987 |
^ aNumber asInteger |
|
1988 |
! |
|
1989 |
||
1990 |
generality |
|
1991 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
1992 |
||
1993 |
^ 20 |
|
1336 | 1994 |
! |
1995 |
||
1996 |
signExtendedByteValue |
|
1997 |
"return a smallInteger from sign-extending the 8'th bit. |
|
1998 |
May be useful for communication interfaces" |
|
1999 |
||
2000 |
%{ /* NOCONTEXT */ |
|
2781 | 2001 |
INT i = __intVal(self); |
1336 | 2002 |
|
2003 |
if (i & 0x80) { |
|
8908 | 2004 |
i = i | ~0xFFL; |
1336 | 2005 |
} else { |
8908 | 2006 |
i = i & 0x7F; |
1336 | 2007 |
} |
2008 |
||
8913 | 2009 |
RETURN (__mkSmallInteger(i)); |
5412 | 2010 |
%}. |
2011 |
^ self primitiveFailed |
|
1336 | 2012 |
|
2013 |
" |
|
2014 |
16rFF signExtendedByteValue |
|
2015 |
16r7F signExtendedByteValue |
|
2016 |
" |
|
2017 |
! |
|
2018 |
||
2019 |
signExtendedShortValue |
|
2020 |
"return a smallInteger from sign-extending the 16'th bit. |
|
2021 |
May be useful for communication interfaces" |
|
2022 |
||
2023 |
%{ /* NOCONTEXT */ |
|
2781 | 2024 |
INT i = __intVal(self); |
1336 | 2025 |
|
2026 |
if (i & 0x8000) { |
|
8908 | 2027 |
i = i | ~0xFFFFL; |
1336 | 2028 |
} else { |
8908 | 2029 |
i = i & 0x7FFF; |
1336 | 2030 |
} |
2031 |
||
8913 | 2032 |
RETURN (__mkSmallInteger(i)); |
5412 | 2033 |
%}. |
2034 |
^ self primitiveFailed |
|
1336 | 2035 |
|
2036 |
" |
|
8908 | 2037 |
16rFFFF signExtendedShortValue |
1336 | 2038 |
16r7FFF signExtendedShortValue |
2039 |
" |
|
701 | 2040 |
! ! |
2041 |
||
2042 |
!SmallInteger methodsFor:'comparing'! |
|
2043 |
||
2044 |
< aNumber |
|
2045 |
"return true, if the argument is greater than the receiver" |
|
2046 |
||
2047 |
%{ /* NOCONTEXT */ |
|
2048 |
||
2049 |
if (__isSmallInteger(aNumber)) { |
|
2050 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2051 |
RETURN ( (__intVal(self) < __intVal(aNumber)) ? true : false ); |
701 | 2052 |
#else |
8908 | 2053 |
/* tag bit does not change ordering */ |
2054 |
RETURN ( ((INT)self < (INT)aNumber) ? true : false ); |
|
701 | 2055 |
#endif |
2056 |
} |
|
2057 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2058 |
RETURN ( ((double)__intVal(self) < __floatVal(aNumber)) ? true : false ); |
701 | 2059 |
} |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2060 |
%}. |
701 | 2061 |
^ aNumber lessFromInteger:self |
2062 |
"^ self retry:#< coercing:aNumber" |
|
2063 |
! |
|
2064 |
||
2065 |
<= aNumber |
|
2066 |
"return true, if the argument is greater or equal" |
|
2067 |
||
2068 |
%{ /* NOCONTEXT */ |
|
2069 |
||
2070 |
if (__isSmallInteger(aNumber)) { |
|
2071 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2072 |
RETURN ( (__intVal(self) <= __intVal(aNumber)) ? true : false ); |
701 | 2073 |
#else |
8908 | 2074 |
/* tag bit does not change ordering */ |
2075 |
RETURN ( ((INT)self <= (INT)aNumber) ? true : false ); |
|
701 | 2076 |
#endif |
2077 |
} |
|
2078 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2079 |
RETURN ( ((double)__intVal(self) <= __floatVal(aNumber)) ? true : false ); |
701 | 2080 |
} |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2081 |
%}. |
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2082 |
^ (self > aNumber) not |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2083 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2084 |
"Modified: / 31.7.2002 / 10:07:17 / cg" |
701 | 2085 |
! |
2086 |
||
2087 |
= aNumber |
|
7356 | 2088 |
"return true, if the argument represents the same numeric value |
2089 |
as the receiver, false otherwise" |
|
701 | 2090 |
|
2091 |
%{ /* NOCONTEXT */ |
|
2092 |
||
2093 |
if (aNumber == self) { |
|
8908 | 2094 |
RETURN ( true ); |
701 | 2095 |
} |
2096 |
if (! __isNonNilObject(aNumber)) { |
|
8908 | 2097 |
/* a smallint or nil */ |
2098 |
RETURN ( false ); |
|
701 | 2099 |
} |
2100 |
||
7356 | 2101 |
if (__qIsFloatLike(aNumber)) { |
8908 | 2102 |
RETURN ( ((double)__intVal(self) == __floatVal(aNumber)) ? true : false ); |
701 | 2103 |
} |
7356 | 2104 |
if (__qIsShortFloat(aNumber)) { |
8908 | 2105 |
RETURN ( ((double)__intVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
7356 | 2106 |
} |
2107 |
%}. |
|
2108 |
^ aNumber equalFromInteger:self |
|
701 | 2109 |
! |
2110 |
||
2111 |
> aNumber |
|
2112 |
"return true, if the argument is less than the receiver" |
|
2113 |
||
2114 |
%{ /* NOCONTEXT */ |
|
2115 |
||
2116 |
if (__isSmallInteger(aNumber)) { |
|
2117 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2118 |
RETURN ( (__intVal(self) > __intVal(aNumber)) ? true : false ); |
701 | 2119 |
#else |
8908 | 2120 |
/* tag bit does not change ordering */ |
2121 |
RETURN ( ((INT)self > (INT)aNumber) ? true : false ); |
|
701 | 2122 |
#endif |
2123 |
} |
|
2124 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2125 |
RETURN ( ((double)__intVal(self) > __floatVal(aNumber)) ? true : false ); |
701 | 2126 |
} |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2127 |
%}. |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2128 |
^ (aNumber < self) |
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2129 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2130 |
"Modified: / 31.7.2002 / 10:07:05 / cg" |
701 | 2131 |
! |
2132 |
||
2133 |
>= aNumber |
|
2134 |
"return true, if the argument is less or equal" |
|
2135 |
||
2136 |
%{ /* NOCONTEXT */ |
|
2137 |
||
2138 |
if (__isSmallInteger(aNumber)) { |
|
2139 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2140 |
RETURN ( (__intVal(self) >= __intVal(aNumber)) ? true : false ); |
701 | 2141 |
#else |
8908 | 2142 |
/* tag bit does not change ordering */ |
2143 |
RETURN ( ((INT)self >= (INT)aNumber) ? true : false ); |
|
701 | 2144 |
#endif |
2145 |
} |
|
2146 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2147 |
RETURN ( ((double)__intVal(self) >= __floatVal(aNumber)) ? true : false ); |
701 | 2148 |
} |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2149 |
%}. |
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2150 |
^ (self < aNumber) not |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2151 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2152 |
"Modified: / 31.7.2002 / 10:07:11 / cg" |
701 | 2153 |
! |
1 | 2154 |
|
701 | 2155 |
hash |
2156 |
"return an integer useful for hashing on value" |
|
2157 |
||
2158 |
self >= 0 ifTrue:[^ self]. |
|
2159 |
^ self negated |
|
2160 |
! |
|
2161 |
||
2162 |
identityHash |
|
2163 |
"return an integer useful for hashing on identity" |
|
2164 |
||
1964
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
2165 |
self >= 0 ifTrue:[^ self]. |
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
2166 |
^ self negated |
1963
ea26c7b9ca1d
experimental: new hash value for smallInts
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2167 |
|
1964
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
2168 |
"Modified: 11.11.1996 / 18:42:14 / cg" |
701 | 2169 |
! |
2170 |
||
2171 |
max:aNumber |
|
2172 |
"return the receiver or the argument, whichever is greater" |
|
2173 |
||
2174 |
%{ /* NOCONTEXT */ |
|
2175 |
||
2176 |
if (__isSmallInteger(aNumber)) { |
|
2216 | 2177 |
#if TAG_INT == 1 |
2178 |
/* tag bit does not change ordering */ |
|
2179 |
if ((INT)(self) > (INT)(aNumber)) |
|
701 | 2180 |
#else |
2216 | 2181 |
if (__intVal(self) > __intVal(aNumber)) |
701 | 2182 |
#endif |
2216 | 2183 |
{ |
701 | 2184 |
RETURN ( self ); |
2185 |
} |
|
2186 |
RETURN ( aNumber ); |
|
2187 |
} |
|
2188 |
if (__isFloatLike(aNumber)) { |
|
1133 | 2189 |
if ( (double)__intVal(self) > __floatVal(aNumber) ) { |
701 | 2190 |
RETURN ( self ); |
2191 |
} |
|
2192 |
RETURN ( aNumber ); |
|
2193 |
} |
|
2216 | 2194 |
%}. |
2195 |
"/ fallback for non-smallInteger argument |
|
2196 |
||
701 | 2197 |
(self > aNumber) ifTrue:[^ self]. |
2198 |
^ aNumber |
|
2199 |
! |
|
2200 |
||
2201 |
min:aNumber |
|
2202 |
"return the receiver or the argument, whichever is smaller" |
|
2203 |
||
2204 |
%{ /* NOCONTEXT */ |
|
2205 |
||
2206 |
if (__isSmallInteger(aNumber)) { |
|
2216 | 2207 |
#if TAG_INT == 1 |
2208 |
/* tag bit does not change ordering */ |
|
2209 |
if ((INT)(self) < (INT)(aNumber)) |
|
701 | 2210 |
#else |
2216 | 2211 |
if (__intVal(self) < __intVal(aNumber)) |
701 | 2212 |
#endif |
2216 | 2213 |
{ |
701 | 2214 |
RETURN ( self ); |
2215 |
} |
|
2216 |
RETURN ( aNumber ); |
|
2217 |
} |
|
2218 |
if (__isFloatLike(aNumber)) { |
|
1133 | 2219 |
if ( (double)__intVal(self) < __floatVal(aNumber) ) { |
701 | 2220 |
RETURN ( self ); |
2221 |
} |
|
2222 |
RETURN ( aNumber ); |
|
2223 |
} |
|
2216 | 2224 |
%}. |
2225 |
"/ fallback for non-smallInteger argument |
|
2226 |
||
701 | 2227 |
(self < aNumber) ifTrue:[^ self]. |
2228 |
^ aNumber |
|
2229 |
! |
|
2230 |
||
2231 |
~= aNumber |
|
2232 |
"return true, if the arguments value is not equal to mine" |
|
2233 |
||
2234 |
%{ /* NOCONTEXT */ |
|
2235 |
||
2236 |
if (aNumber == self) { |
|
8908 | 2237 |
RETURN ( false ); |
701 | 2238 |
} |
2239 |
if (! __isNonNilObject(aNumber)) { |
|
8908 | 2240 |
/* a smallint or nil */ |
2241 |
RETURN ( true ); |
|
701 | 2242 |
} |
2243 |
||
7356 | 2244 |
if (__qIsFloatLike(aNumber)) { |
8908 | 2245 |
RETURN ( ((double)__intVal(self) != __floatVal(aNumber)) ? true : false ); |
7356 | 2246 |
} |
2247 |
if (__qIsShortFloat(aNumber)) { |
|
8908 | 2248 |
RETURN ( ((double)__intVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
701 | 2249 |
} |
2216 | 2250 |
%}. |
7356 | 2251 |
^ (self = aNumber) not |
701 | 2252 |
! ! |
2253 |
||
2254 |
!SmallInteger methodsFor:'copying'! |
|
2255 |
||
2256 |
deepCopy |
|
2257 |
"return a deep copy of myself |
|
2258 |
- reimplemented here since smallintegers are unique" |
|
2259 |
||
2260 |
^ self |
|
2261 |
! |
|
2262 |
||
10947 | 2263 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
701 | 2264 |
"return a deep copy of myself |
2265 |
- reimplemented here since smallintegers are unique" |
|
2266 |
||
2267 |
^ self |
|
2268 |
! |
|
2269 |
||
2270 |
shallowCopy |
|
2271 |
"return a shallow copy of myself |
|
2272 |
- reimplemented here since smallintegers are unique" |
|
2273 |
||
2274 |
^ self |
|
2275 |
! |
|
2276 |
||
2277 |
simpleDeepCopy |
|
2278 |
"return a deep copy of myself |
|
2279 |
- reimplemented here since smallintegers are unique" |
|
2280 |
||
2281 |
^ self |
|
1 | 2282 |
! ! |
2283 |
||
214 | 2284 |
!SmallInteger methodsFor:'iteration'! |
1 | 2285 |
|
2286 |
timesRepeat:aBlock |
|
357 | 2287 |
"evaluate the argument, aBlock self times. |
2288 |
Reimplemented as primitive for speed" |
|
1 | 2289 |
|
357 | 2290 |
%{ |
2291 |
REGISTER INT tmp; |
|
2292 |
static struct inlineCache blockVal = __ILC0(0); |
|
1 | 2293 |
|
357 | 2294 |
tmp = __intVal(self); |
2295 |
if (tmp > 0) { |
|
8908 | 2296 |
if (__isBlockLike(aBlock) |
8913 | 2297 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(0))) { |
8908 | 2298 |
{ |
2299 |
REGISTER OBJFUNC codeVal; |
|
2300 |
||
2301 |
/* |
|
2302 |
* specially tuned version for compiled blocks, |
|
2303 |
* (the most common case) |
|
2304 |
*/ |
|
2305 |
if (((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
|
2678 | 2306 |
#ifdef PARANOIA |
8908 | 2307 |
&& (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
2678 | 2308 |
#endif |
8908 | 2309 |
) { |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2310 |
|
357 | 2311 |
#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
|
2312 |
|
2254 | 2313 |
# 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
|
2314 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2315 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2316 |
|
2254 | 2317 |
# define BLOCK_ARG rHome |
8908 | 2318 |
REGISTER OBJ rHome; |
2319 |
||
2320 |
/* |
|
2321 |
* home on stack - no need to refetch |
|
2322 |
*/ |
|
2323 |
rHome = __BlockInstPtr(aBlock)->b_home; |
|
2324 |
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
|
2325 |
#endif |
8908 | 2326 |
{ |
8919 | 2327 |
#ifdef __UNROLL_LOOPS__ |
2216 | 2328 |
|
8908 | 2329 |
/* |
2330 |
* you are not supposed to program like this - I know what I do |
|
2331 |
*/ |
|
2332 |
while (tmp > 8) { |
|
2333 |
if (InterruptPending != nil) goto interrupted0; |
|
2334 |
continue0: |
|
2335 |
(*codeVal)(BLOCK_ARG); |
|
2336 |
if (InterruptPending != nil) goto interrupted1; |
|
2337 |
continue1: |
|
2338 |
(*codeVal)(BLOCK_ARG); |
|
2339 |
if (InterruptPending != nil) goto interrupted2; |
|
2340 |
continue2: |
|
2341 |
(*codeVal)(BLOCK_ARG); |
|
2342 |
if (InterruptPending != nil) goto interrupted3; |
|
2343 |
continue3: |
|
2344 |
(*codeVal)(BLOCK_ARG); |
|
2345 |
if (InterruptPending != nil) goto interrupted4; |
|
2346 |
continue4: |
|
2347 |
(*codeVal)(BLOCK_ARG); |
|
2348 |
if (InterruptPending != nil) goto interrupted5; |
|
2349 |
continue5: |
|
2350 |
(*codeVal)(BLOCK_ARG); |
|
2351 |
if (InterruptPending != nil) goto interrupted6; |
|
2352 |
continue6: |
|
2353 |
(*codeVal)(BLOCK_ARG); |
|
2354 |
if (InterruptPending != nil) goto interrupted7; |
|
2355 |
continue7: |
|
2356 |
(*codeVal)(BLOCK_ARG); |
|
2357 |
tmp -= 8; |
|
2358 |
} |
|
8919 | 2359 |
#endif /* __UNROLL_LOOPS__ */ |
8908 | 2360 |
do { |
2361 |
if (InterruptPending != nil) goto interruptedX; |
|
2362 |
continueX: |
|
2363 |
(*codeVal)(BLOCK_ARG); |
|
2364 |
} while(--tmp); |
|
2365 |
||
2366 |
RETURN (self); |
|
2367 |
if (0) { |
|
8919 | 2368 |
#ifdef __UNROLL_LOOPS__ |
8908 | 2369 |
interrupted0: |
2370 |
__interruptL(@line); goto continue0; |
|
2371 |
interrupted1: |
|
2372 |
__interruptL(@line); goto continue1; |
|
2373 |
interrupted2: |
|
2374 |
__interruptL(@line); goto continue2; |
|
2375 |
interrupted3: |
|
2376 |
__interruptL(@line); goto continue3; |
|
2377 |
interrupted4: |
|
2378 |
__interruptL(@line); goto continue4; |
|
2379 |
interrupted5: |
|
2380 |
__interruptL(@line); goto continue5; |
|
2381 |
interrupted6: |
|
2382 |
__interruptL(@line); goto continue6; |
|
2383 |
interrupted7: |
|
2384 |
__interruptL(@line); goto continue7; |
|
8919 | 2385 |
#endif /* __UNROLL_LOOPS__ */ |
8908 | 2386 |
interruptedX: |
2387 |
__interruptL(@line); goto continueX; |
|
2388 |
} |
|
2389 |
} |
|
2390 |
} |
|
2391 |
} |
|
1672
1b56d6e95c9e
changes to call dynamic compiled code right after compilation.
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
2392 |
|
2216 | 2393 |
# undef BLOCK_ARG |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
2394 |
|
400 | 2395 |
#ifdef NEW_BLOCK_CALL |
2216 | 2396 |
# define BLOCK_ARG aBlock |
2397 |
# define IBLOCK_ARG nil |
|
400 | 2398 |
#else |
2216 | 2399 |
# define BLOCK_ARG (__BlockInstPtr(aBlock)->b_home) |
2400 |
# define IBLOCK_ARG (__BlockInstPtr(aBlock)->b_home) |
|
400 | 2401 |
#endif |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
2402 |
|
8908 | 2403 |
/* |
2404 |
* sorry - must check for the blocks code within the loops; |
|
2405 |
* it could be recompiled or flushed (in the interrupt) |
|
2406 |
*/ |
|
2407 |
do { |
|
2408 |
REGISTER OBJFUNC codeVal; |
|
2409 |
||
2410 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
|
2411 |
/* |
|
2412 |
* arg is a compiled block with code - |
|
2413 |
* directly call it without going through Block>>value |
|
2414 |
* however, if there is an interrupt, refetch the code pointer. |
|
2415 |
*/ |
|
2416 |
/* stay here, while no interrupts are pending ... */ |
|
2417 |
do { |
|
2418 |
(*codeVal)(BLOCK_ARG); |
|
2419 |
if (InterruptPending != nil) goto outerLoop; |
|
2420 |
} while (--tmp); |
|
2421 |
RETURN (self); |
|
2422 |
} else { |
|
2423 |
if (InterruptPending != nil) __interruptL(@line); |
|
2424 |
||
2425 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
|
2426 |
/* |
|
2427 |
* arg is a compiled block with bytecode - |
|
2428 |
* directly call interpreter without going through Block>>value |
|
2429 |
*/ |
|
2430 |
__interpret(aBlock, 0, nil, IBLOCK_ARG, nil, nil); |
|
2431 |
} else { |
|
2432 |
/* |
|
2433 |
* arg is something else - call it with #value |
|
2434 |
*/ |
|
2435 |
(*blockVal.ilc_func)(aBlock, @symbol(value), nil, &blockVal); |
|
2436 |
} |
|
2437 |
} |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2438 |
outerLoop: ; |
8908 | 2439 |
} while (--tmp); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2440 |
|
2216 | 2441 |
# undef BLOCK_ARG |
2442 |
# undef IBLOCK_ARG |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2443 |
|
8908 | 2444 |
RETURN (self); |
2445 |
} |
|
2446 |
||
2447 |
/* |
|
2448 |
* not a block-like thingy - call it with #value |
|
2449 |
*/ |
|
2450 |
do { |
|
2451 |
if (InterruptPending != nil) __interruptL(@line); |
|
2452 |
(*blockVal.ilc_func)(aBlock, @symbol(value), nil, &blockVal); |
|
2453 |
} while(--tmp); |
|
2454 |
RETURN (self); |
|
357 | 2455 |
} |
5412 | 2456 |
%}. |
2457 |
^ super timesRepeat:aBlock |
|
357 | 2458 |
|
2459 |
"/ |count "{ Class: SmallInteger }" | |
|
2460 |
"/ |
|
2461 |
"/ count := self. |
|
2462 |
"/ [count > 0] whileTrue:[ |
|
2463 |
"/ aBlock value. |
|
2464 |
"/ count := count - 1 |
|
2465 |
"/ ] |
|
1 | 2466 |
! |
2467 |
||
701 | 2468 |
to:stop by:incr do:aBlock |
2469 |
"reimplemented as primitive for speed" |
|
2470 |
||
2471 |
%{ |
|
2472 |
REGISTER INT tmp, step; |
|
2473 |
REGISTER INT final; |
|
2474 |
static struct inlineCache blockVal = __ILC1(0); |
|
2475 |
||
2476 |
if (__bothSmallInteger(incr, stop)) { |
|
1133 | 2477 |
tmp = __intVal(self); |
2478 |
final = __intVal(stop); |
|
2479 |
step = __intVal(incr); |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2480 |
|
701 | 2481 |
if (__isBlockLike(aBlock) |
8913 | 2482 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(1))) { |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2483 |
{ |
2254 | 2484 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2485 |
|
2216 | 2486 |
/* |
2487 |
* specially tuned version for static compiled blocks, called with |
|
2488 |
* home on the stack (the most common case) |
|
2489 |
*/ |
|
4167 | 2490 |
if (((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
2678 | 2491 |
#ifdef PARANOIA |
4167 | 2492 |
&& (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
2678 | 2493 |
#endif |
2494 |
) { |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2495 |
|
701 | 2496 |
#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
|
2497 |
|
2254 | 2498 |
# 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
|
2499 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2500 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2501 |
|
2254 | 2502 |
# define BLOCK_ARG rHome |
2503 |
REGISTER OBJ rHome; |
|
2504 |
rHome = __BlockInstPtr(aBlock)->b_home; |
|
8908 | 2505 |
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
|
2506 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2507 |
#endif |
2254 | 2508 |
{ |
2509 |
if (step < 0) { |
|
2510 |
if (step == -1) { |
|
2511 |
while (tmp >= final) { |
|
2512 |
if (InterruptPending != nil) __interruptL(@line); |
|
8913 | 2513 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2254 | 2514 |
tmp--; |
2216 | 2515 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2516 |
} else { |
2254 | 2517 |
while (tmp >= final) { |
2216 | 2518 |
if (InterruptPending != nil) __interruptL(@line); |
8913 | 2519 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2216 | 2520 |
tmp += step; |
2521 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2522 |
} |
2254 | 2523 |
} else { |
2524 |
if (step == 1) { |
|
2525 |
while (tmp <= final) { |
|
2526 |
if (InterruptPending != nil) __interruptL(@line); |
|
8913 | 2527 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2254 | 2528 |
tmp++; |
2529 |
} |
|
2530 |
} else { |
|
4167 | 2531 |
while (tmp <= final) { |
2254 | 2532 |
if (InterruptPending != nil) __interruptL(@line); |
8913 | 2533 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2254 | 2534 |
tmp += step; |
2535 |
} |
|
2536 |
} |
|
2216 | 2537 |
} |
2254 | 2538 |
RETURN (self); |
2216 | 2539 |
} |
2540 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2541 |
} |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2542 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2543 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2544 |
* 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
|
2545 |
* 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
|
2546 |
*/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2547 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2548 |
# undef BLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2549 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2550 |
#ifdef NEW_BLOCK_CALL |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2551 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2552 |
# define IBLOCK_ARG nil |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2553 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2554 |
# 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
|
2555 |
# 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
|
2556 |
#endif |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2557 |
|
701 | 2558 |
if (step < 0) { |
2559 |
while (tmp >= final) { |
|
2254 | 2560 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2561 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2562 |
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
|
2563 |
|
2254 | 2564 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2216 | 2565 |
/* |
2566 |
* arg is a compiled block with code - |
|
2567 |
* directly call it without going through Block>>value |
|
2568 |
*/ |
|
8913 | 2569 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2570 |
} else { |
2216 | 2571 |
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
|
2572 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2573 |
* 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
|
2574 |
* 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
|
2575 |
*/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2576 |
#ifdef PASS_ARG_POINTER |
2216 | 2577 |
{ |
2578 |
OBJ idx; |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2579 |
|
8913 | 2580 |
idx = __mkSmallInteger(tmp); |
2216 | 2581 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
2582 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2583 |
#else |
8913 | 2584 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2585 |
#endif |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2586 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2587 |
} else { |
2216 | 2588 |
/* |
2589 |
* arg is something else - call it with #value |
|
2590 |
*/ |
|
8913 | 2591 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2592 |
} |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2593 |
} |
701 | 2594 |
tmp += step; |
2595 |
} |
|
2596 |
} else { |
|
2597 |
while (tmp <= final) { |
|
2254 | 2598 |
REGISTER OBJFUNC codeVal; |
2187
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 |
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
|
2601 |
|
2254 | 2602 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2216 | 2603 |
/* |
2604 |
* arg is a compiled block with code - |
|
2605 |
* directly call it without going through Block>>value |
|
2606 |
*/ |
|
8913 | 2607 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2216 | 2608 |
} else { |
2609 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
|
2610 |
/* |
|
2611 |
* arg is a compiled block with bytecode - |
|
2612 |
* directly call interpreter without going through Block>>value |
|
2613 |
*/ |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2614 |
#ifdef PASS_ARG_POINTER |
2216 | 2615 |
{ |
2616 |
OBJ idx; |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2617 |
|
8913 | 2618 |
idx = __mkSmallInteger(tmp); |
2216 | 2619 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
2620 |
} |
|
701 | 2621 |
#else |
8913 | 2622 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2623 |
#endif |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2624 |
|
2216 | 2625 |
} else { |
2626 |
/* |
|
2627 |
* arg is something else - call it with #value: |
|
2628 |
*/ |
|
8913 | 2629 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
2216 | 2630 |
} |
2631 |
} |
|
2632 |
tmp += step; |
|
2633 |
} |
|
701 | 2634 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2635 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2636 |
# undef BLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2637 |
# undef IBLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2638 |
|
701 | 2639 |
} else { |
2640 |
/* |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2641 |
* arg is something else - call it with #value: |
701 | 2642 |
*/ |
2643 |
if (step < 0) { |
|
2644 |
while (tmp >= final) { |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2645 |
if (InterruptPending != nil) __interruptL(@line); |
701 | 2646 |
|
8908 | 2647 |
(*blockVal.ilc_func)(aBlock, |
2648 |
@symbol(value:), |
|
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
2649 |
nil, &blockVal, |
8913 | 2650 |
__mkSmallInteger(tmp)); |
701 | 2651 |
tmp += step; |
2652 |
} |
|
2653 |
} else { |
|
2654 |
while (tmp <= final) { |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2655 |
if (InterruptPending != nil) __interruptL(@line); |
701 | 2656 |
|
8908 | 2657 |
(*blockVal.ilc_func)(aBlock, |
2658 |
@symbol(value:), |
|
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
2659 |
nil, &blockVal, |
8913 | 2660 |
__mkSmallInteger(tmp)); |
701 | 2661 |
tmp += step; |
2662 |
} |
|
2663 |
} |
|
2664 |
} |
|
2665 |
RETURN ( self ); |
|
2666 |
} |
|
1672
1b56d6e95c9e
changes to call dynamic compiled code right after compilation.
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
2667 |
%}. |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2668 |
"/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2669 |
"/ 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
|
2670 |
"/ pass on to super ... |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2671 |
|
701 | 2672 |
^ super to:stop by:incr do:aBlock |
2673 |
||
2674 |
" |
|
2675 |
1 to:10 by:3 do:[:i | i printNewline] |
|
2676 |
" |
|
2677 |
! |
|
2678 |
||
1 | 2679 |
to:stop do:aBlock |
62 | 2680 |
"evaluate aBlock for every integer between (and including) the receiver |
2681 |
and the argument, stop. |
|
357 | 2682 |
Reimplemented as primitive for speed" |
1 | 2683 |
|
2684 |
%{ |
|
2685 |
REGISTER INT tmp; |
|
2686 |
INT final; |
|
216 | 2687 |
static struct inlineCache blockVal = __ILC1(0); |
1 | 2688 |
|
252 | 2689 |
if (__isSmallInteger(stop)) { |
1133 | 2690 |
tmp = __intVal(self); |
2691 |
final = __intVal(stop); |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2692 |
|
214 | 2693 |
if (__isBlockLike(aBlock) |
8913 | 2694 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(1))) { |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2695 |
{ |
2216 | 2696 |
/* |
2697 |
* 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
|
2698 |
* where called with home on the stack |
2216 | 2699 |
*/ |
2254 | 2700 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2701 |
|
4167 | 2702 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2254 | 2703 |
|
13 | 2704 |
#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
|
2705 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2706 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2707 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2708 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2709 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2710 |
# define BLOCK_ARG rHome |
2216 | 2711 |
REGISTER OBJ rHome; |
2712 |
rHome = __BlockInstPtr(aBlock)->b_home; |
|
8908 | 2713 |
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
|
2714 |
#endif |
400 | 2715 |
{ |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2716 |
|
2678 | 2717 |
#ifdef PARANOIA |
8908 | 2718 |
if (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
2678 | 2719 |
#endif |
2720 |
{ |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2721 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2722 |
* static compiled blocks ... |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2723 |
*/ |
8919 | 2724 |
#ifdef __UNROLL_LOOPS__ |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2725 |
/* |
2693 | 2726 |
* The following code is designed to run as fast as possible; |
2727 |
* - taken branches only if interrupts are pending |
|
2728 |
* - only forward branches (which are usually predicted as not taken) |
|
2729 |
* - unrolled the loop |
|
2730 |
* |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2731 |
* 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
|
2732 |
*/ |
2693 | 2733 |
# if TAG_INT==1 |
8913 | 2734 |
INT t8 = (INT)(__mkSmallInteger(tmp+8)); |
2735 |
tmp = (INT)(__mkSmallInteger(tmp)); |
|
2736 |
final = (INT)(__mkSmallInteger(final)); |
|
2693 | 2737 |
# else |
2770 | 2738 |
INT t8 = tmp+8; |
2693 | 2739 |
# endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2740 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2741 |
for (;;) { |
2693 | 2742 |
|
2743 |
while (t8 <= final) { |
|
2744 |
# if TAG_INT==1 |
|
2770 | 2745 |
t8 += (INT)(__MASKSMALLINT(8)); |
2693 | 2746 |
# else |
2747 |
t8 += 8; |
|
2748 |
# endif |
|
2749 |
if (InterruptPending != nil) goto interrupted0; |
|
2750 |
continue0: |
|
2751 |
# if TAG_INT==1 |
|
2752 |
(*codeVal)(BLOCK_ARG, tmp); |
|
2753 |
# else |
|
8913 | 2754 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2693 | 2755 |
# endif |
2756 |
if (InterruptPending != nil) goto interrupted1; |
|
2757 |
continue1: |
|
2758 |
# if TAG_INT==1 |
|
2770 | 2759 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(1)) ); |
2693 | 2760 |
# else |
8913 | 2761 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+1)); |
2693 | 2762 |
# endif |
2763 |
if (InterruptPending != nil) goto interrupted2; |
|
2764 |
continue2: |
|
2765 |
# if TAG_INT==1 |
|
2770 | 2766 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(2)) ); |
2693 | 2767 |
# else |
8913 | 2768 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+2)); |
2693 | 2769 |
# endif |
2770 |
if (InterruptPending != nil) goto interrupted3; |
|
2771 |
continue3: |
|
2772 |
# if TAG_INT==1 |
|
2770 | 2773 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(3)) ); |
2693 | 2774 |
# else |
8913 | 2775 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+3)); |
2693 | 2776 |
# endif |
2777 |
if (InterruptPending != nil) goto interrupted4; |
|
2778 |
continue4: |
|
2779 |
# if TAG_INT==1 |
|
2770 | 2780 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(4)) ); |
2693 | 2781 |
# else |
8913 | 2782 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+4)); |
2693 | 2783 |
# endif |
2784 |
if (InterruptPending != nil) goto interrupted5; |
|
2785 |
continue5: |
|
2786 |
# if TAG_INT==1 |
|
2770 | 2787 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(5)) ); |
2693 | 2788 |
# else |
8913 | 2789 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+5)); |
2693 | 2790 |
# endif |
2791 |
if (InterruptPending != nil) goto interrupted6; |
|
2792 |
continue6: |
|
2793 |
# if TAG_INT==1 |
|
2770 | 2794 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(6)) ); |
2693 | 2795 |
# else |
8913 | 2796 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+6)); |
2693 | 2797 |
# endif |
2798 |
if (InterruptPending != nil) goto interrupted7; |
|
2799 |
continue7: |
|
2800 |
# if TAG_INT==1 |
|
2770 | 2801 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(7)) ); |
2693 | 2802 |
# else |
8913 | 2803 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+7)); |
2693 | 2804 |
# endif |
2805 |
||
2806 |
# if TAG_INT==1 |
|
2770 | 2807 |
tmp += (INT)(__MASKSMALLINT(8)); |
2693 | 2808 |
# else |
2809 |
tmp += 8; |
|
2810 |
# endif |
|
2811 |
} |
|
4167 | 2812 |
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
|
2813 |
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
|
2814 |
continueX: |
2693 | 2815 |
# if TAG_INT==1 |
2816 |
(*codeVal)(BLOCK_ARG, tmp); |
|
2770 | 2817 |
tmp += (INT)(__MASKSMALLINT(1)); |
2693 | 2818 |
# else |
8913 | 2819 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2693 | 2820 |
tmp++; |
2821 |
# endif |
|
4167 | 2822 |
} |
2823 |
RETURN (self); |
|
2693 | 2824 |
|
2216 | 2825 |
if (0) { |
2826 |
/* |
|
2827 |
* no discussion about those gotos ... |
|
2828 |
* ... its better for your CPU's pipelines |
|
2829 |
* (if you dont understand why, just dont argue). |
|
2830 |
*/ |
|
2831 |
interrupted7: |
|
2832 |
__interruptL(@line); goto continue7; |
|
2833 |
interrupted6: |
|
2834 |
__interruptL(@line); goto continue6; |
|
2835 |
interrupted5: |
|
2836 |
__interruptL(@line); goto continue5; |
|
2837 |
interrupted4: |
|
2838 |
__interruptL(@line); goto continue4; |
|
2839 |
interrupted3: |
|
2840 |
__interruptL(@line); goto continue3; |
|
2841 |
interrupted2: |
|
2842 |
__interruptL(@line); goto continue2; |
|
2843 |
interrupted1: |
|
2844 |
__interruptL(@line); goto continue1; |
|
2845 |
interrupted0: |
|
2846 |
__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
|
2847 |
interruptedX: |
5295fc0fddb7
oops - right shift by int-size did not work on some machines
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
2848 |
__interruptL(@line); goto continueX; |
2216 | 2849 |
} |
2640 | 2850 |
} |
2641 | 2851 |
#else |
2640 | 2852 |
while (tmp <= final) { |
2853 |
if (InterruptPending != nil) __interruptL(@line); |
|
8913 | 2854 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2640 | 2855 |
tmp ++; |
2216 | 2856 |
} |
2640 | 2857 |
RETURN (self); |
8919 | 2858 |
#endif /* __UNROLL_LOOPS__ */ |
2216 | 2859 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2860 |
|
2216 | 2861 |
/* |
2862 |
* mhmh - seems to be a block with dynamic code |
|
2863 |
* must refetch, to allow dynamic recompilation or code flush. |
|
2864 |
*/ |
|
2865 |
while (tmp <= final) { |
|
2866 |
if (InterruptPending != nil) __interruptL(@line); |
|
2254 | 2867 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) == (OBJFUNC)nil) break; |
8913 | 2868 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2216 | 2869 |
tmp ++; |
2870 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2871 |
|
2216 | 2872 |
if (tmp > final) { |
2873 |
RETURN (self); |
|
2874 |
} |
|
2875 |
} |
|
2876 |
} |
|
2877 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2878 |
|
2216 | 2879 |
# undef BLOCK_ARG |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2880 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2881 |
#ifdef NEW_BLOCK_CALL |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2882 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2883 |
# define IBLOCK_ARG nil |
400 | 2884 |
#else |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2885 |
# 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
|
2886 |
# define IBLOCK_ARG (__BlockInstPtr(aBlock)->b_home) |
400 | 2887 |
#endif |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
2888 |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2889 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2890 |
* 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
|
2891 |
* 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
|
2892 |
*/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2893 |
while (tmp <= final) { |
2254 | 2894 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2895 |
|
2254 | 2896 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2216 | 2897 |
/* |
2898 |
* arg is a compiled block with code - |
|
2899 |
* directly call it without going through Block>>value |
|
2900 |
*/ |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2901 |
|
4167 | 2902 |
/* stay here, while no interrupts are pending ... */ |
2903 |
do { |
|
8913 | 2904 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
4167 | 2905 |
if (InterruptPending != nil) goto outerLoop; |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2906 |
tmp++; |
4167 | 2907 |
} while (tmp <= final); |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2908 |
RETURN (self); |
2216 | 2909 |
} else { |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2910 |
if (InterruptPending != nil) __interruptL(@line); |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2911 |
|
2216 | 2912 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
2913 |
/* |
|
2914 |
* arg is a compiled block with bytecode - |
|
2915 |
* directly call interpreter without going through Block>>value |
|
2916 |
*/ |
|
400 | 2917 |
#ifdef PASS_ARG_POINTER |
2216 | 2918 |
{ |
2919 |
OBJ idx; |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2920 |
|
8913 | 2921 |
idx = __mkSmallInteger(tmp); |
2216 | 2922 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
2923 |
} |
|
400 | 2924 |
#else |
8913 | 2925 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
400 | 2926 |
#endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2927 |
|
2216 | 2928 |
} else { |
2929 |
/* |
|
2930 |
* arg is something else - call it with #value: |
|
2931 |
*/ |
|
8913 | 2932 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
2216 | 2933 |
} |
2934 |
} |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2935 |
outerLoop: ; |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2936 |
tmp++; |
214 | 2937 |
} |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2938 |
|
2216 | 2939 |
# undef BLOCK_ARG |
2940 |
# undef IBLOCK_ARG |
|
1 | 2941 |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2942 |
RETURN (self); |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2943 |
} |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2944 |
/* |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2945 |
* arg is something else - call it with #value: |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2946 |
*/ |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2947 |
while (tmp <= final) { |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2948 |
if (InterruptPending != nil) __interruptL(@line); |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2949 |
|
8908 | 2950 |
(*blockVal.ilc_func)(aBlock, |
2951 |
@symbol(value:), |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2952 |
nil, &blockVal, |
8913 | 2953 |
__mkSmallInteger(tmp)); |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2954 |
tmp++; |
214 | 2955 |
} |
2956 |
RETURN ( self ); |
|
1 | 2957 |
} |
400 | 2958 |
%}. |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2959 |
|
400 | 2960 |
"/ |
2961 |
"/ 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
|
2962 |
"/ pass on to super ... |
400 | 2963 |
"/ |
62 | 2964 |
^ super to:stop do:aBlock |
2965 |
||
216 | 2966 |
" |
2967 |
1 to:10 do:[:i | i printNewline] |
|
2968 |
" |
|
701 | 2969 |
! ! |
1 | 2970 |
|
701 | 2971 |
!SmallInteger methodsFor:'misc math'! |
1 | 2972 |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2973 |
bernoulli |
8908 | 2974 |
"returns the nth Bernoulli number. |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2975 |
The series runs this: |
8908 | 2976 |
1/6, 1/30, 1/42, 1/30, 5/66, 691/2730, etc |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2977 |
Uses a table of the first 20 bernoulli numbers. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2978 |
So bernoulli(42) will fail for now. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2979 |
Used with taylor series for tan" |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2980 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2981 |
|table p| |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2982 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2983 |
table := #( |
8908 | 2984 |
(1 6) |
2985 |
(-1 30) |
|
2986 |
(1 42) |
|
2987 |
(-1 30) |
|
2988 |
(5 66) |
|
2989 |
(-691 2730) |
|
2990 |
(7 6) |
|
2991 |
(-3617 510) |
|
2992 |
(43867 798) |
|
2993 |
(-174611 330) |
|
2994 |
(854513 138) |
|
2995 |
(-236364091 2730) |
|
2996 |
(8553103 6) |
|
2997 |
(-23749461029 870) |
|
2998 |
(8615841276005 14322) |
|
2999 |
(-7709321041217 510) |
|
3000 |
(2577687858367 6) |
|
3001 |
(-26315271553053477373 1919190) |
|
3002 |
(2929993913841559 6) |
|
3003 |
(-261082718496449122051 13530) |
|
3004 |
). |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3005 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3006 |
self even ifTrue:[ |
8908 | 3007 |
self == 0 ifTrue:[^1]. |
3008 |
p := table at:(self / 2). |
|
3009 |
^ Fraction numerator:(p first) denominator:(p second). |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3010 |
]. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3011 |
self == 1 ifTrue:[ ^ (1 / 2) ]. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3012 |
^ 0. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3013 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3014 |
" |
8908 | 3015 |
0 bernoulli |
3016 |
1 bernoulli |
|
3017 |
2 bernoulli |
|
3018 |
3 bernoulli |
|
3019 |
4 bernoulli |
|
3020 |
5 bernoulli |
|
3021 |
6 bernoulli |
|
3022 |
8 bernoulli |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3023 |
38 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3024 |
40 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3025 |
41 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3026 |
42 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3027 |
" |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3028 |
! |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3029 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3030 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3031 |
"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
|
3032 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3033 |
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
|
3034 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3035 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3036 |
%{ /* NOCONTEXT */ |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3037 |
INT val, div, mod, mySelf; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3038 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
3039 |
if (__isSmallInteger(aNumber) && |
4167 | 3040 |
(val = __intVal(aNumber)) > 0) { |
3041 |
mySelf = __intVal(self); |
|
3042 |
div = mySelf / val; |
|
3043 |
mod = mySelf % val; |
|
3044 |
||
8913 | 3045 |
RETURN (__ARRAY_WITH2( __mkSmallInteger(div), |
3046 |
__mkSmallInteger(mod))); |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3047 |
} |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3048 |
%}. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3049 |
^ super divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3050 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3051 |
" |
8908 | 3052 |
10 // 3 |
3053 |
10 \\ 3 |
|
3054 |
10 divMod:3 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3055 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3056 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3057 |
|
701 | 3058 |
gcd:anInteger |
3059 |
"return the greatest common divisor (Euclid's algorithm). |
|
3060 |
This has been redefined here for more speed since due to the |
|
3061 |
use of gcd in Fraction code, it has become time-critical for |
|
3062 |
some code. (thanx to MessageTally)" |
|
3063 |
||
3064 |
%{ /* NOCONTEXT */ |
|
1 | 3065 |
|
701 | 3066 |
if (__isSmallInteger(anInteger)) { |
4167 | 3067 |
INT orgArg, ttt, selfInt, orgSelfInt, temp; |
3068 |
||
3069 |
ttt = orgArg = __intVal(anInteger); |
|
3070 |
if (ttt) { |
|
3071 |
selfInt = orgSelfInt = __intVal(self); |
|
3072 |
while (ttt != 0) { |
|
3073 |
temp = selfInt % ttt; |
|
3074 |
selfInt = ttt; |
|
3075 |
ttt = temp; |
|
3076 |
} |
|
3077 |
/* |
|
3078 |
* since its not defined in C, what the sign of |
|
3079 |
* a modulus result is when the arg is negative, |
|
3080 |
* change it explicitely here ... |
|
3081 |
*/ |
|
3082 |
if (orgArg < 0) { |
|
3083 |
/* result should be negative */ |
|
3084 |
if (orgSelfInt > 0) selfInt = -selfInt; |
|
3085 |
} else { |
|
3086 |
/* result should be positive */ |
|
3087 |
if (orgSelfInt < 0) selfInt = -selfInt; |
|
3088 |
} |
|
8913 | 3089 |
RETURN ( __mkSmallInteger(selfInt) ); |
4167 | 3090 |
} |
1 | 3091 |
} |
3092 |
%} |
|
3093 |
. |
|
701 | 3094 |
^ super gcd:anInteger |
3095 |
! |
|
62 | 3096 |
|
2430
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3097 |
gcd_helper:anInteger |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3098 |
"same as gcd - see knuth & Integer>>gcd:" |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3099 |
|
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3100 |
^ self gcd:anInteger |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3101 |
|
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3102 |
"Created: 1.3.1997 / 16:58:01 / cg" |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3103 |
! |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3104 |
|
701 | 3105 |
intlog10 |
3106 |
"return the truncation of log10 of the receiver - |
|
3107 |
stupid implementation; used to find out the number of digits needed |
|
3108 |
to print a number/and for conversion to a LargeInteger. |
|
7356 | 3109 |
Implemented that way, to allow for tiny systems (PDAs) without a Float class |
701 | 3110 |
(i.e. without log)." |
2 | 3111 |
|
701 | 3112 |
self <= 0 ifTrue:[ |
8908 | 3113 |
^ self class |
3114 |
raise:#domainErrorSignal |
|
3115 |
receiver:self |
|
3116 |
selector:#intlog10 |
|
3117 |
arguments:#() |
|
3118 |
errorString:'logarithm of negative integer' |
|
2 | 3119 |
]. |
7356 | 3120 |
self < 10 ifTrue:[^ 0]. |
3121 |
self < 100 ifTrue:[^ 1]. |
|
3122 |
self < 1000 ifTrue:[^ 2]. |
|
3123 |
self < 10000 ifTrue:[^ 3]. |
|
3124 |
self < 100000 ifTrue:[^ 4]. |
|
3125 |
self < 1000000 ifTrue:[^ 5]. |
|
3126 |
self < 10000000 ifTrue:[^ 6]. |
|
3127 |
self < 100000000 ifTrue:[^ 7]. |
|
3128 |
self < 1000000000 ifTrue:[^ 8]. |
|
3129 |
^ 9 |
|
3130 |
||
8908 | 3131 |
" |
3132 |
99 intlog10 |
|
3133 |
100 intlog10 |
|
3134 |
120 intlog10 |
|
6344 | 3135 |
-1 intlog10 |
3136 |
" |
|
2 | 3137 |
! ! |
3138 |
||
3135 | 3139 |
!SmallInteger methodsFor:'modulo arithmetic'! |
2 | 3140 |
|
701 | 3141 |
plus:aNumber |
3142 |
"return the sum of the receiver and the argument, as SmallInteger. |
|
3143 |
The argument must be another SmallInteger. |
|
8908 | 3144 |
If the result overflows the smallInteger range, the value modulo the |
701 | 3145 |
smallInteger range is returned (i.e. the low bits of the sum). |
3135 | 3146 |
This is of course not always correct, but some code does a modulo anyway |
701 | 3147 |
and can therefore speed things up by not going through LargeIntegers." |
41 | 3148 |
|
701 | 3149 |
%{ /* NOCONTEXT */ |
3150 |
||
3151 |
if (__isSmallInteger(aNumber)) { |
|
8908 | 3152 |
INT sum; |
3153 |
||
3154 |
sum = __intVal(self) + __intVal(aNumber); |
|
3155 |
if (!__ISVALIDINTEGER(sum)) { |
|
3156 |
/* keep the sign */ |
|
3157 |
sum %= _MAX_INT; |
|
3158 |
} |
|
8913 | 3159 |
RETURN ( __mkSmallInteger(sum)); |
701 | 3160 |
} |
2785 | 3161 |
%}. |
701 | 3162 |
self primitiveFailed |
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3163 |
|
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3164 |
" |
8908 | 3165 |
5 plus:-1 |
3166 |
5 plus:1 |
|
3167 |
1 plus:-5 |
|
3168 |
self maxVal plus:1 |
|
3169 |
self maxVal + 1 |
|
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3170 |
" |
2 | 3171 |
! |
3172 |
||
701 | 3173 |
subtract:aNumber |
3174 |
"return the difference of the receiver and the argument, as SmallInteger. |
|
3175 |
The argument must be another SmallInteger. |
|
8908 | 3176 |
If the result overflows the smallInteger range, the value modulo the |
701 | 3177 |
smallInteger range is returned (i.e. the low bits of the sum). |
3135 | 3178 |
This is of course not always correct, but some code does a modulo anyway |
701 | 3179 |
and can therefore speed things up by not going through LargeIntegers." |
3180 |
||
3181 |
%{ /* NOCONTEXT */ |
|
2 | 3182 |
|
701 | 3183 |
if (__isSmallInteger(aNumber)) { |
8908 | 3184 |
INT diff; |
3185 |
||
3186 |
diff = __intVal(self) - __intVal(aNumber); |
|
3187 |
if (!__ISVALIDINTEGER(diff)) { |
|
3188 |
/* keep the sign */ |
|
3189 |
diff %= _MAX_INT; |
|
3190 |
} |
|
8913 | 3191 |
RETURN ( __mkSmallInteger(diff)); |
701 | 3192 |
} |
2216 | 3193 |
%}. |
701 | 3194 |
self primitiveFailed |
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3195 |
|
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3196 |
" |
8908 | 3197 |
-1 subtract:5 |
3198 |
5 subtract:1 |
|
3199 |
1 subtract:-5 |
|
3200 |
self minVal subtract:1 |
|
3201 |
self minVal - 1 |
|
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3202 |
" |
701 | 3203 |
! |
3204 |
||
3205 |
times:aNumber |
|
3206 |
"return the product of the receiver and the argument, as SmallInteger. |
|
3207 |
The argument must be another SmallInteger. |
|
8908 | 3208 |
If the result overflows the smallInteger range, the value modulo the |
701 | 3209 |
smallInteger range is returned (i.e. the low bits of the product). |
3135 | 3210 |
This is of course not always correct, but some code does a modulo anyway |
701 | 3211 |
and can therefore speed things up by not going through LargeIntegers." |
3212 |
||
3213 |
%{ /* NOCONTEXT */ |
|
3214 |
||
8506 | 3215 |
INT myValue, otherValue; |
3216 |
unsigned INT productLow, productHi; |
|
3217 |
int negative; |
|
3218 |
||
3219 |
# define low16Bits(foo) ((foo) & 0xFFFF) |
|
3220 |
# define hi16Bits(foo) ((foo) >> 16) |
|
3221 |
# define low32Bits(foo) ((foo) & 0xFFFFFFFFL) |
|
3222 |
# define hi32Bits(foo) ((foo) >> 32) |
|
3223 |
||
3224 |
/* |
|
3225 |
* can we use long long arithmetic ? |
|
3226 |
* |
|
3227 |
* long-long arithmetic seems to be buggy with some systems |
|
3228 |
* (took me a while to find this out :-( |
|
3229 |
* (try 10000 * 10000) |
|
3230 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3231 |
#if defined(__sparc__) && defined(__GNUC__) && (__GNUC__ >= 2) |
8506 | 3232 |
# define USE_LONGLONG_FOR_MUL |
3233 |
#endif |
|
3234 |
||
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3235 |
#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2) |
8506 | 3236 |
# define USE_LONGLONG_FOR_MUL |
3237 |
#endif |
|
3238 |
||
701 | 3239 |
if (__isSmallInteger(aNumber)) { |
8908 | 3240 |
myValue = __intVal(self); |
3241 |
otherValue = __intVal(aNumber); |
|
8506 | 3242 |
|
3243 |
#if defined(USE_LONGLONG_FOR_MUL) |
|
8908 | 3244 |
{ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3245 |
# if defined(__alpha__) && !defined(__alpha64__) |
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3246 |
# define LONGLONG INT64 |
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3247 |
# else |
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3248 |
# define LONGLONG long long |
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3249 |
# endif |
8908 | 3250 |
LONGLONG product; |
3251 |
||
3252 |
product = (LONGLONG)myValue * (LONGLONG)otherValue; |
|
3253 |
if (product < 0) { |
|
8913 | 3254 |
RETURN ( __mkSmallInteger(-(INT)(-product & 0x3FFFFFFF))); |
8908 | 3255 |
} |
8913 | 3256 |
RETURN ( __mkSmallInteger((INT)(product & 0x3FFFFFFF))); |
8908 | 3257 |
} |
8506 | 3258 |
#else /* no long-long */ |
8908 | 3259 |
negative = 1; |
3260 |
if (myValue < 0) { |
|
3261 |
negative = -1; |
|
3262 |
myValue = -myValue; |
|
3263 |
} |
|
3264 |
if (otherValue < 0) { |
|
3265 |
negative = -negative; |
|
3266 |
otherValue = -otherValue; |
|
3267 |
} |
|
8506 | 3268 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3269 |
# if defined(__GNUC__) && defined(__mc68k__) |
8908 | 3270 |
asm ("mulu%.l %3,%1:%0" |
3271 |
: "=d" ((unsigned long)(productLow)), |
|
3272 |
"=d" ((unsigned long)(productHi)) |
|
3273 |
: "%0" ((unsigned long)(myValue)), |
|
3274 |
"dmi" ((unsigned long)(otherValue))); |
|
8506 | 3275 |
# else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3276 |
# if defined (__GNUC__) && defined(__i386__) |
8908 | 3277 |
asm ("mull %3" |
3278 |
: "=a" ((unsigned long)(productLow)), |
|
3279 |
"=d" ((unsigned long)(productHi)) |
|
3280 |
: "%0" ((unsigned long)(myValue)), |
|
3281 |
"rm" ((unsigned long)(otherValue))); |
|
8506 | 3282 |
# else |
10342 | 3283 |
# if defined(WIN32) && defined(__BORLANDC__) |
8908 | 3284 |
asm { |
3285 |
mov eax, myValue |
|
3286 |
mov edx, otherValue |
|
3287 |
mul edx |
|
3288 |
mov productLow, eax |
|
3289 |
mov productHi, edx |
|
3290 |
} |
|
8506 | 3291 |
# else /* generic */ |
8908 | 3292 |
{ |
3293 |
unsigned INT pHH, pHL, pLH, pLL; |
|
3294 |
unsigned INT low1, low2, hi1, hi2; |
|
3295 |
unsigned INT t; |
|
3296 |
||
3297 |
/* unsigned multiply myValue * otherValue -> productHi, productLow |
|
3298 |
* |
|
3299 |
* this is too slow: |
|
3300 |
* since most machines can do 32*32 to 64 bit multiply, |
|
3301 |
* (or at least 32*32 with Overflow check) |
|
3302 |
* - need more assembler (inline) functions here |
|
3303 |
*/ |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3304 |
# if __POINTER_SIZE__ == 8 |
8908 | 3305 |
low1 = low32Bits((unsigned INT)myValue); |
3306 |
hi1 = hi32Bits((unsigned INT)myValue); |
|
3307 |
low2 = low32Bits((unsigned INT)otherValue); |
|
3308 |
hi2 = hi32Bits((unsigned INT)otherValue); |
|
8506 | 3309 |
# define LLMASK 0xC000000000000000L |
3310 |
# else |
|
8908 | 3311 |
low1 = low16Bits((unsigned INT)myValue); |
3312 |
hi1 = hi16Bits((unsigned INT)myValue); |
|
3313 |
low2 = low16Bits((unsigned INT)otherValue); |
|
3314 |
hi2 = hi16Bits((unsigned INT)otherValue); |
|
8506 | 3315 |
# define LLMASK 0xC0000000 |
3316 |
# endif |
|
3317 |
||
8908 | 3318 |
pLH = low1 * hi2; |
3319 |
pHL = hi1 * low2; |
|
3320 |
pLL = low1 * low2; |
|
3321 |
pHH = hi1 * hi2; |
|
3322 |
||
3323 |
/* |
|
3324 |
* the common case ... |
|
3325 |
*/ |
|
3326 |
if ((pHL == 0) |
|
3327 |
&& (pLH == 0) |
|
3328 |
&& (pHH == 0) |
|
3329 |
&& ((pLL & LLMASK) == 0)) { |
|
3330 |
if (negative < 0) { |
|
8913 | 3331 |
RETURN ( __mkSmallInteger(- ((INT)pLL)) ); |
8908 | 3332 |
} |
8913 | 3333 |
RETURN ( __mkSmallInteger((INT)pLL) ); |
8908 | 3334 |
} |
3335 |
||
3336 |
/* |
|
3337 |
* pHH |--------|--------| |
|
3338 |
* pLH |--------|--------| |
|
3339 |
* pHL |--------|--------| |
|
3340 |
* pLL |--------|--------| |
|
3341 |
*/ |
|
8506 | 3342 |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3343 |
# if __POINTER_SIZE__ == 8 |
8908 | 3344 |
t = low32Bits(pLH) + low32Bits(pHL) + hi32Bits(pLL); |
3345 |
productLow = (t << 32) + low32Bits(pLL); |
|
3346 |
productHi = pHH + hi32Bits(t) + hi32Bits(pHL) + hi32Bits(pLH); |
|
8506 | 3347 |
# else |
8908 | 3348 |
t = low16Bits(pLH) + low16Bits(pHL) + hi16Bits(pLL); |
3349 |
productLow = (t << 16) + low16Bits(pLL); |
|
3350 |
productHi = pHH + hi16Bits(t) + hi16Bits(pHL) + hi16Bits(pLH); |
|
8506 | 3351 |
# endif |
8908 | 3352 |
} |
8506 | 3353 |
# endif /* ! WIN32 */ |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3354 |
# endif /* ! (__GNUC__ && __i386__) */ |
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
3355 |
# endif /* ! (__GNUC__ && __mc68k__) */ |
8506 | 3356 |
|
8908 | 3357 |
if (negative < 0) { |
8913 | 3358 |
RETURN ( __mkSmallInteger(-(INT)(productLow & 0x3FFFFFFF))); |
8908 | 3359 |
} |
8913 | 3360 |
RETURN ( __mkSmallInteger((INT)(productLow & 0x3FFFFFFF))); |
8506 | 3361 |
#endif /* ! USE_LONGLONG */ |
701 | 3362 |
} |
2216 | 3363 |
%}. |
8506 | 3364 |
|
701 | 3365 |
self primitiveFailed |
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3366 |
|
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3367 |
" |
8908 | 3368 |
5 times:-1 |
3369 |
5 times:1 |
|
3370 |
self maxVal-1 times:2 |
|
3371 |
self maxVal-1 times:-2 |
|
3372 |
self maxVal-1 * 2 bitAnd:16r3fffffff |
|
8498
854b2c98379e
keep the sign when doing modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
8417
diff
changeset
|
3373 |
" |
1 | 3374 |
! ! |
3375 |
||
3376 |
!SmallInteger methodsFor:'printing & storing'! |
|
3377 |
||
62 | 3378 |
printOn:aStream |
68 | 3379 |
"append my printstring (base 10) to aStream." |
62 | 3380 |
|
3381 |
aStream nextPutAll:(self printString) |
|
3382 |
! |
|
3383 |
||
5988 | 3384 |
printOn:aStream base:radix showRadix:showRadix |
3385 |
"append a string representation of the receiver in the specified numberBase to aStream |
|
3386 |
(if showRadix is true, with initial XXr) |
|
701 | 3387 |
The radix argument should be between 2 and 36." |
3388 |
||
8365 | 3389 |
(radix between:2 and:36) ifFalse:[ |
8908 | 3390 |
^ super printOn:aStream base:radix showRadix:showRadix. |
3391 |
"/ self error:'invalid radix' |
|
6144 | 3392 |
]. |
3393 |
||
5988 | 3394 |
showRadix ifTrue:[ |
8908 | 3395 |
radix printOn:aStream. |
3396 |
aStream nextPutAll:'r'. |
|
5988 | 3397 |
]. |
701 | 3398 |
aStream nextPutAll:(self printStringRadix:radix) |
5988 | 3399 |
|
3400 |
"Created: / 7.9.2001 / 13:54:40 / cg" |
|
701 | 3401 |
! |
3402 |
||
1 | 3403 |
printString |
3404 |
"return my printstring (base 10)" |
|
3405 |
||
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3406 |
"since this was heavily used in some applications, |
68 | 3407 |
here is an exception to the rule of basing printString |
3408 |
upon the printOn: method." |
|
3409 |
||
1 | 3410 |
%{ /* NOCONTEXT */ |
3411 |
||
3412 |
char buffer[30]; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3413 |
char *cp; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3414 |
OBJ newString = nil; |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3415 |
INT myValue; |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3416 |
int negative = 0; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3417 |
int len; |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3418 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3419 |
myValue = __intVal(self); |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3420 |
if (myValue == 0) { |
8908 | 3421 |
RETURN (__MKSTRING_L("0", 1)); |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3422 |
} |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3423 |
#ifdef SLOW_CODE |
8908 | 3424 |
/* |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3425 |
* 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
|
3426 |
* (printf is soooo slow) |
369 | 3427 |
*/ |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3428 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3429 |
/* |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3430 |
* actually only needed on sparc: since thisContext is |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3431 |
* in a global register, which gets destroyed by printf, |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3432 |
* manually save it here - very stupid ... |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3433 |
*/ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3434 |
__BEGIN_PROTECT_REGISTERS__ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3435 |
|
8908 | 3436 |
# ifdef __NEED_L_MODIFIER_IN_PRINTF__ |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3437 |
len = snprintf(buffer, sizeof(buffer), "%ld", myValue); |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3438 |
# else |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3439 |
len = snprintf(buffer, sizeof(buffer), "%d", myValue); |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3440 |
# endif |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3441 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3442 |
__END_PROTECT_REGISTERS__ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3443 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3444 |
if (len >= 0 && len <= sizeof(buffer)) { |
8908 | 3445 |
newString = __MKSTRING_L(buffer, len); |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3446 |
} |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3447 |
|
2781 | 3448 |
#else |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3449 |
if (myValue < 0) { |
8908 | 3450 |
negative = 1; |
3451 |
myValue = -myValue; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3452 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3453 |
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
|
3454 |
*cp-- = '\0'; |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3455 |
while (myValue != 0) { |
8908 | 3456 |
*cp = '0' + (myValue % 10); |
3457 |
myValue = myValue / 10; |
|
3458 |
cp--; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3459 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3460 |
if (negative) { |
8908 | 3461 |
*cp-- = '-'; |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3462 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3463 |
newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp)); |
2781 | 3464 |
#endif |
282 | 3465 |
if (newString != nil) { |
8908 | 3466 |
RETURN (newString); |
282 | 3467 |
} |
3468 |
%}. |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3469 |
"/ only arrive here, |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3470 |
"/ when having memory problems (i.e. no space for string) ... |
282 | 3471 |
^ super printString |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3472 |
|
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3473 |
" |
8908 | 3474 |
1234 printString |
3475 |
0 printString |
|
3476 |
-100 printString |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3477 |
" |
1 | 3478 |
! |
3479 |
||
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3480 |
printStringRadix:base |
62 | 3481 |
"return my printstring (optimized for bases 16, 10 and 8)" |
1 | 3482 |
|
3705 | 3483 |
|s| |
3484 |
||
3485 |
%{ |
|
3486 |
char buffer[64+3]; /* for alpha, base 2 plus sign */ |
|
3487 |
char *cp; |
|
1 | 3488 |
OBJ newString; |
3705 | 3489 |
INT myValue; |
3490 |
int negative = 0; |
|
3706 | 3491 |
INT __base; |
1 | 3492 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3493 |
if (__isSmallInteger(base)) { |
8908 | 3494 |
myValue = __intVal(self); |
3495 |
if (myValue == 0) { |
|
3496 |
RETURN (__MKSTRING_L("0", 1)); |
|
3497 |
} |
|
3498 |
__base = __intVal(base); |
|
3706 | 3499 |
|
3705 | 3500 |
#ifdef SLOW_CODE |
8908 | 3501 |
switch (__base) { |
3502 |
case 10: |
|
3503 |
#ifdef __NEED_L_MODIFIER_IN_PRINTF__ |
|
3504 |
format = "%ld"; |
|
2781 | 3505 |
#else |
8908 | 3506 |
format = "%d"; |
2781 | 3507 |
#endif |
8908 | 3508 |
break; |
3509 |
case 16: |
|
3510 |
#ifdef __NEED_L_MODIFIER_IN_PRINTF__ |
|
3511 |
format = "%lx"; |
|
2781 | 3512 |
#else |
8908 | 3513 |
format = "%x"; |
3705 | 3514 |
#endif |
8908 | 3515 |
break; |
3516 |
case 8: |
|
3517 |
#ifdef __NEED_L_MODIFIER_IN_PRINTF__ |
|
3518 |
format = "%lo"; |
|
3705 | 3519 |
#else |
8908 | 3520 |
format = "%o"; |
2781 | 3521 |
#endif |
8908 | 3522 |
break; |
3523 |
} |
|
3524 |
||
3525 |
if (format) { |
|
3526 |
/* |
|
3527 |
* actually only needed on sparc: since thisContext is |
|
3528 |
* in a global register, which gets destroyed by printf, |
|
3529 |
* manually save it here - very stupid ... |
|
3530 |
*/ |
|
3531 |
__BEGIN_PROTECT_REGISTERS__ |
|
3532 |
||
3533 |
len = snprintf(buffer, sizeof(buffer), format, myValue); |
|
3534 |
||
3535 |
__END_PROTECT_REGISTERS__ |
|
3536 |
||
3537 |
if (len > 0 && len <= sizeof(buffer)) { |
|
3538 |
newString = __MKSTRING_L(buffer, len); |
|
3539 |
if (newString != nil) { |
|
3540 |
RETURN (newString); |
|
3541 |
} |
|
3542 |
} |
|
3543 |
} |
|
3705 | 3544 |
#else |
8908 | 3545 |
if ((__base <= 36) && (__base > 1)) { |
3546 |
if (myValue < 0) { |
|
3547 |
negative = 1; |
|
3548 |
myValue = -myValue; |
|
3549 |
} |
|
3550 |
cp = buffer + sizeof(buffer) - 1; |
|
3551 |
*cp-- = '\0'; |
|
3552 |
while (myValue != 0) { |
|
3553 |
int digit; |
|
3554 |
||
3555 |
digit = myValue % __base; |
|
3556 |
if (digit <= 9) { |
|
3557 |
*cp = '0' + digit; |
|
3558 |
} else { |
|
3559 |
*cp = 'A' + digit - 10; |
|
3560 |
} |
|
3561 |
myValue = myValue / __base; |
|
3562 |
cp--; |
|
3563 |
} |
|
3564 |
if (negative) { |
|
3565 |
*cp-- = '-'; |
|
3566 |
} |
|
3567 |
newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp)); |
|
3568 |
if (newString != nil) { |
|
3569 |
RETURN (newString); |
|
3570 |
} |
|
3571 |
} |
|
3705 | 3572 |
#endif |
1 | 3573 |
} |
314 | 3574 |
%}. |
3705 | 3575 |
"/ arrive here, for bad base, |
3576 |
"/ or when having memory problems (i.e. no space for string) ... |
|
3577 |
||
3578 |
"/ |
|
3579 |
"/ fall back for seldom used bases |
|
3580 |
"/ Notice: cannt use super>>printStringRadix: here, |
|
3581 |
"/ since that would lead to endless recursion ... |
|
3582 |
"/ (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
|
3583 |
|
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3584 |
s := WriteStream on:(String new:10). |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3585 |
super printOn:s base:base. |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3586 |
^ s contents. |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3587 |
|
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3588 |
" |
8908 | 3589 |
127 printStringRadix:16 |
3590 |
123 printStringRadix:12 |
|
3591 |
123 printStringRadix:10 |
|
3592 |
123 printStringRadix:8 |
|
3593 |
123 printStringRadix:3 |
|
3594 |
123 printStringRadix:2 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3595 |
123 printStringRadix:1 |
3705 | 3596 |
|
8908 | 3597 |
-127 printStringRadix:16 |
3598 |
-123 printStringRadix:12 |
|
3599 |
-123 printStringRadix:10 |
|
3600 |
-123 printStringRadix:8 |
|
3601 |
-123 printStringRadix:3 |
|
3602 |
-123 printStringRadix:2 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3603 |
" |
1 | 3604 |
! |
3605 |
||
3606 |
printfPrintString:formatString |
|
7746 | 3607 |
"non-standard, but sometimes useful. |
1 | 3608 |
return a printed representation of the receiver |
62 | 3609 |
as specified by formatString, which is defined by the C- |
3610 |
function 'printf'. |
|
8908 | 3611 |
No checking for string overrun - the resulting string |
62 | 3612 |
must be shorter than 256 chars or else ... |
2781 | 3613 |
This method is NONSTANDARD and may be removed without notice; |
3614 |
it is provided to allow special conversions in very special |
|
3615 |
situaltions. |
|
3616 |
Notice that a conversion may not be portable; for example, |
|
3617 |
to correctly convert an int on a 64-bit alpha, a %ld is required, |
|
3618 |
while other systems may be happy with a %d ... |
|
3619 |
Use at your own risk (if at all)" |
|
1 | 3620 |
|
13 | 3621 |
%{ /* STACK: 400 */ |
1 | 3622 |
char buffer[256]; |
314 | 3623 |
OBJ s; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3624 |
int len; |
1 | 3625 |
|
62 | 3626 |
if (__isString(formatString)) { |
8908 | 3627 |
/* |
3628 |
* actually only needed on sparc: since thisContext is |
|
3629 |
* in a global register, which gets destroyed by printf, |
|
3630 |
* manually save it here - very stupid ... |
|
3631 |
*/ |
|
3632 |
__BEGIN_PROTECT_REGISTERS__ |
|
3633 |
||
3634 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __intVal(self)); |
|
3635 |
||
3636 |
__END_PROTECT_REGISTERS__ |
|
3637 |
||
3638 |
if (len < 0) goto fail; |
|
3639 |
||
3640 |
s = __MKSTRING_L(buffer, len); |
|
3641 |
if (s != nil) { |
|
3642 |
RETURN (s); |
|
3643 |
} |
|
1 | 3644 |
} |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3645 |
fail: ; |
314 | 3646 |
%}. |
1 | 3647 |
self primitiveFailed |
3648 |
||
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3649 |
" |
8908 | 3650 |
123 printfPrintString:'%%d -> %d' |
3651 |
123 printfPrintString:'%%6d -> %6d' |
|
3652 |
123 printfPrintString:'%%x -> %x' |
|
3653 |
123 printfPrintString:'%%4x -> %4x' |
|
3654 |
123 printfPrintString:'%%04x -> %04x' |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3655 |
" |
1 | 3656 |
! ! |
701 | 3657 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3658 |
!SmallInteger methodsFor:'private'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3659 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3660 |
sign:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3661 |
"private: for protocol completeness with LargeIntegers" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3662 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3663 |
|absVal| |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3664 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3665 |
absVal := self abs. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3666 |
aNumber < 0 ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3667 |
^ absVal negated |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3668 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3669 |
aNumber == 0 ifTrue:[^ 0]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3670 |
^ absVal |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3671 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3672 |
" |
8908 | 3673 |
-4 sign:-1 |
3674 |
-4 sign:0 |
|
3675 |
-4 sign:1 |
|
3676 |
-4 sign:-1 |
|
3677 |
-4 sign:0 |
|
3678 |
-4 sign:1 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3679 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3680 |
! ! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3681 |
|
701 | 3682 |
!SmallInteger methodsFor:'testing'! |
3683 |
||
3684 |
between:min and:max |
|
3685 |
"return true if the receiver is less than or equal to the argument max |
|
3686 |
and greater than or equal to the argument min. |
|
3687 |
- reimplemented here for speed" |
|
3688 |
||
3689 |
%{ /* NOCONTEXT */ |
|
3690 |
||
3691 |
if (__bothSmallInteger(min, max)) { |
|
3692 |
REGISTER INT selfVal; |
|
3693 |
||
1133 | 3694 |
selfVal = __intVal(self); |
3695 |
if (selfVal < __intVal(min)) { |
|
701 | 3696 |
RETURN ( false ); |
3697 |
} |
|
1133 | 3698 |
if (selfVal > __intVal(max)) { |
701 | 3699 |
RETURN ( false ); |
3700 |
} |
|
3701 |
RETURN ( true ); |
|
3702 |
} |
|
2216 | 3703 |
%}. |
701 | 3704 |
(self < min) ifTrue:[^ false]. |
3705 |
(self > max) ifTrue:[^ false]. |
|
3706 |
^ true |
|
3707 |
! |
|
3708 |
||
3709 |
even |
|
3710 |
"return true, if the receiver is even" |
|
3711 |
||
3712 |
%{ /* NOCONTEXT */ |
|
3713 |
||
2216 | 3714 |
RETURN ( ((INT)self & (INT)__MASKSMALLINT(1)) ? false : true ); |
5412 | 3715 |
%}. |
3716 |
^ super even |
|
701 | 3717 |
! |
3718 |
||
3719 |
negative |
|
3720 |
"return true, if the receiver is less than zero |
|
3721 |
reimplemented here for speed" |
|
3722 |
||
3723 |
%{ /* NOCONTEXT */ |
|
3724 |
||
2216 | 3725 |
#if TAG_INT == 1 |
701 | 3726 |
/* tag bit does not change sign */ |
3727 |
RETURN ( ((INT)(self) < 0) ? true : false ); |
|
2216 | 3728 |
#else |
3729 |
RETURN ( (__intVal(self) < 0) ? true : false ); |
|
701 | 3730 |
#endif |
5412 | 3731 |
%}. |
3732 |
^ self < 0 |
|
701 | 3733 |
! |
3734 |
||
3735 |
odd |
|
3736 |
"return true, if the receiver is odd" |
|
3737 |
||
3738 |
%{ /* NOCONTEXT */ |
|
3739 |
||
2216 | 3740 |
RETURN ( ((INT)self & (INT)__MASKSMALLINT(1)) ? true : false ); |
5412 | 3741 |
%}. |
3742 |
^ super odd |
|
3743 |
||
701 | 3744 |
! |
3745 |
||
3746 |
positive |
|
3747 |
"return true, if the receiver is not negative |
|
3748 |
reimplemented here for speed" |
|
3749 |
||
3750 |
%{ /* NOCONTEXT */ |
|
3751 |
||
2216 | 3752 |
#if TAG_INT == 1 |
701 | 3753 |
/* tag bit does not change sign */ |
3754 |
RETURN ( ((INT)(self) >= 0) ? true : false ); |
|
2216 | 3755 |
#else |
3756 |
RETURN ( (__intVal(self) >= 0) ? true : false ); |
|
701 | 3757 |
#endif |
5412 | 3758 |
%}. |
3759 |
^ super positive |
|
3760 |
||
701 | 3761 |
! |
3762 |
||
3763 |
sign |
|
7471 | 3764 |
"return the sign of the receiver (-1, 0 or 1). |
701 | 3765 |
reimplemented here for speed" |
3766 |
||
3767 |
%{ /* NOCONTEXT */ |
|
3768 |
||
1133 | 3769 |
INT val = __intVal(self); |
701 | 3770 |
|
3771 |
if (val < 0) { |
|
8913 | 3772 |
RETURN ( __mkSmallInteger(-1) ); |
701 | 3773 |
} |
3774 |
if (val > 0) { |
|
8913 | 3775 |
RETURN ( __mkSmallInteger(1) ); |
701 | 3776 |
} |
8913 | 3777 |
RETURN ( __mkSmallInteger(0) ); |
5412 | 3778 |
%}. |
3779 |
^ super sign |
|
701 | 3780 |
! |
3781 |
||
3782 |
strictlyPositive |
|
3783 |
"return true, if the receiver is greater than zero |
|
3784 |
reimplemented here for speed" |
|
3785 |
||
3786 |
%{ /* NOCONTEXT */ |
|
3787 |
||
2216 | 3788 |
#if TAG_INT == 1 |
701 | 3789 |
/* tag bit does not change sign */ |
3790 |
RETURN ( ((INT)(self) > 0) ? true : false ); |
|
2216 | 3791 |
#else |
3792 |
RETURN ( (__intVal(self) > 0) ? true : false ); |
|
701 | 3793 |
#endif |
5412 | 3794 |
%}. |
3795 |
^ super strictlyPositive |
|
701 | 3796 |
! ! |
3797 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3798 |
!SmallInteger class methodsFor:'documentation'! |
701 | 3799 |
|
3800 |
version |
|
11493 | 3801 |
^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.172 2009-01-24 20:40:24 cg Exp $' |
701 | 3802 |
! ! |