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