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