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