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