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