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