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