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