author | Claus Gittinger <cg@exept.de> |
Tue, 11 Nov 2014 12:15:04 +0100 | |
changeset 17025 | 3f7a69ad82f9 |
parent 16885 | c8a865e3c540 |
child 17033 | 9190f2b40619 |
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 |
16448 | 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)) { |
16448 | 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) |
16448 | 1046 |
if (count <= N_INT_BITS) { |
1047 |
unsigned LONGLONG result; |
|
1048 |
||
1049 |
result = (unsigned LONGLONG)bits; |
|
1050 |
result <<= count; |
|
1051 |
if (result <= _MAX_INT) { |
|
1052 |
RETURN ( __mkSmallInteger(result) ); |
|
1053 |
} |
|
1054 |
{ |
|
1055 |
RETURN (__MKLARGEINT64(1, (INT)(result >> 32), (INT)(result & 0xFFFFFFFF))); |
|
1056 |
} |
|
1057 |
} |
|
95 | 1058 |
#else |
16448 | 1059 |
/* |
1060 |
* check for overflow |
|
1061 |
*/ |
|
1062 |
if (count < (N_INT_BITS-1)) { |
|
1063 |
if (! (bits >> (N_INT_BITS - 1 - count))) { |
|
1064 |
RETURN ( __mkSmallInteger(bits << count) ); |
|
1065 |
} |
|
1066 |
/* |
|
1067 |
* so, there is an overflow ... |
|
1068 |
* handle it as largeInteger |
|
1069 |
*/ |
|
1070 |
/* FALL THROUGH */ |
|
1071 |
} |
|
95 | 1072 |
#endif |
16448 | 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)) { |
|
1086 |
RETURN (__mkSmallInteger(0)); |
|
1087 |
} |
|
1088 |
||
1089 |
RETURN ( __mkSmallInteger(bits >> count) ); |
|
1090 |
} |
|
1 | 1091 |
} |
85 | 1092 |
%}. |
1093 |
(shiftCount isMemberOf:SmallInteger) ifTrue:[ |
|
16448 | 1094 |
^ (LargeInteger value:self) bitShift:shiftCount |
85 | 1095 |
]. |
16448 | 1096 |
^ self bitShift:shiftCount asInteger "/ 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 |
|
16448 | 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)) { |
|
16448 | 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) |
16448 | 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 |
16448 | 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 |
16448 | 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:[ |
|
16448 | 1497 |
^ (LargeInteger value:self) rightShift:shiftCount |
10972 | 1498 |
]. |
16448 | 1499 |
^ self rightShift:shiftCount asInteger "/ is this a good idea ? |
10972 | 1500 |
|
1501 |
||
1502 |
" |
|
16448 | 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 |
||
1 | 1548 |
!SmallInteger methodsFor:'byte access'! |
1549 |
||
15910 | 1550 |
byteSwapped |
13991 | 1551 |
"lsb -> msb; |
1552 |
i.e. a.b.c.d -> d.c.b.a" |
|
1553 |
||
15910 | 1554 |
SmallInteger maxBytes == 8 ifTrue:[ |
1555 |
^ self byteSwapped64 |
|
1556 |
] ifFalse:[ |
|
1557 |
^ self byteSwapped32 |
|
1558 |
]. |
|
1559 |
||
1560 |
" |
|
1561 |
16r11223344 byteSwapped hexPrintString |
|
1562 |
16r44332211 byteSwapped hexPrintString |
|
1563 |
" |
|
1564 |
||
1565 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
1566 |
! |
|
1567 |
||
1568 |
byteSwapped16 |
|
1569 |
"for 16bit values only: |
|
1570 |
lsb -> msb; |
|
1571 |
i.e. a.b -> b.a" |
|
1572 |
||
1573 |
%{ /* NOCONTEXT */ |
|
1574 |
unsigned INT v = __intVal(self); |
|
1575 |
unsigned INT swapped; |
|
1576 |
||
1577 |
swapped = ((v>>8)&0xFF) | ((v & 0xFF)<<8); |
|
1578 |
RETURN (__mkSmallInteger(swapped)); |
|
1579 |
%}. |
|
1580 |
||
1581 |
" |
|
1582 |
16r1122 byteSwapped16 hexPrintString |
|
1583 |
16r2211 byteSwapped16 hexPrintString |
|
1584 |
16r332211 byteSwapped16 hexPrintString |
|
1585 |
" |
|
1586 |
! |
|
1587 |
||
1588 |
byteSwapped32 |
|
1589 |
"for 32bit values only: |
|
1590 |
lsb -> msb; |
|
1591 |
i.e. a.b.c.d -> d.c.b.a" |
|
1592 |
||
13991 | 1593 |
%{ /* NOCONTEXT */ |
15910 | 1594 |
unsigned INT v = __intVal(self); |
1595 |
unsigned INT swapped; |
|
1596 |
||
1597 |
#undef HAVE_BSWAP |
|
1598 |
#if __POINTER_SIZE__ == 4 |
|
1599 |
||
1600 |
# if defined(USE_BSWAP) && defined(__BORLANDC__) |
|
1601 |
# define HAVE_BSWAP |
|
1602 |
||
1603 |
_asm { |
|
1604 |
mov eax, v |
|
1605 |
bswap eax |
|
1606 |
mov swapped, eax |
|
1607 |
}; |
|
1608 |
# endif |
|
1609 |
# if defined(USE_BSWAP) && defined(__VISUALC__) |
|
1610 |
# define HAVE_BSWAP |
|
1611 |
||
1612 |
_asm { |
|
1613 |
mov eax, v |
|
1614 |
xchg al, ah |
|
1615 |
rol eax, 16 |
|
1616 |
xchg al, ah |
|
1617 |
mov swapped, eax |
|
1618 |
}; |
|
1619 |
# endif |
|
1620 |
# if defined(USE_BSWAP) && defined(__GNUC__) |
|
1621 |
# define HAVE_BSWAP |
|
1622 |
||
16537 | 1623 |
asm("movl %1, %%eax \n\ |
15910 | 1624 |
bswap %%eax \n\ |
16537 | 1625 |
movl %%eax, %0 \n\ |
15910 | 1626 |
" |
1627 |
: "=rm" (swapped) |
|
1628 |
: "rm" (v)); |
|
1629 |
# endif |
|
1630 |
#endif /* __POINTER_SIZE__ == 4 */ |
|
1631 |
||
1632 |
#if __POINTER_SIZE__ == 8 |
|
1633 |
v &= 0xFFFFFFFF; |
|
1634 |
||
1635 |
# if defined(__x86_64__) && defined(__GNUC__) |
|
1636 |
# define HAVE_BSWAP |
|
16537 | 1637 |
asm("movq %1, %%rax \n\ |
15910 | 1638 |
bswap %%eax \n\ |
16537 | 1639 |
movq %%rax, %0 \n\ |
15910 | 1640 |
" |
1641 |
: "=rm" (swapped) |
|
1642 |
: "rm" (v)); |
|
1643 |
# endif |
|
1644 |
#endif |
|
1645 |
||
1646 |
#ifndef HAVE_BSWAP |
|
1647 |
swapped = ((v>>24) | ((v>>8)&0xFF00) | ((v & 0xFF00)<<8) | ((v & 0xFF)<<24)); |
|
1648 |
#endif |
|
1649 |
||
1650 |
RETURN (__MKUINT(swapped)); |
|
1651 |
%}. |
|
1652 |
||
1653 |
" |
|
1654 |
16r11223344 byteSwapped32 hexPrintString |
|
1655 |
16r44332211 byteSwapped32 hexPrintString |
|
1656 |
" |
|
1657 |
||
1658 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
1659 |
! |
|
1660 |
||
1661 |
byteSwapped64 |
|
1662 |
"for 64bit values only: |
|
1663 |
lsb -> msb; |
|
1664 |
i.e. a.b.c.d.e.f.g.h -> h.g.f.e.d.c.b.a" |
|
1665 |
||
1666 |
%{ /* NOCONTEXT */ |
|
1667 |
unsigned INT v = __intVal(self); |
|
1668 |
unsigned INT swapped; |
|
13991 | 1669 |
|
1670 |
#if __POINTER_SIZE__ == 4 |
|
15910 | 1671 |
// xxxxxxxx 00000000 00000000 00000000 -> 00000000 00000000 00000000 xxxxxxxx |
1672 |
// xxxxxxxx xxxxxxxx |
|
1673 |
// xxxxxxxx xxxxxxxx |
|
1674 |
// xxxxxxxx xxxxxxxx |
|
1675 |
swapped = (v>>24) | ((v>>8)&0xFF00) | ((v & 0xFF00)<<8) | ((v & 0xFF)<<24); |
|
16521 | 1676 |
RETURN(__MKLARGEINT64(1, 0, swapped)); |
15910 | 1677 |
#else |
1678 |
// xxxxxxxx 00000000 00000000 00000000 -> 00000000 00000000 00000000 xxxxxxxx |
|
1679 |
// xxxxxxxx xxxxxxxx |
|
1680 |
// xxxxxxxx xxxxxxxx |
|
1681 |
// xxxxxxxx xxxxxxxx |
|
1682 |
swapped = (v>>56) | ((v>>40)&0xFF00) | ((v>>24) & 0xFF0000) | ((v>>8) & 0xFF000000) |
|
1683 |
| ((v & 0xFF000000)<<8) | ((v & 0x00FF0000)<<24) | ((v & 0x0000FF00)<<40) |
|
1684 |
| ((v & 0xFF)<<56); |
|
1685 |
#endif |
|
16521 | 1686 |
RETURN(__MKUINT( swapped )); |
13991 | 1687 |
%}. |
1688 |
||
1689 |
" |
|
15910 | 1690 |
16r11223344 byteSwapped64 hexPrintString |
1691 |
16r44332211 byteSwapped64 hexPrintString |
|
13991 | 1692 |
" |
1693 |
||
1694 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
1695 |
! |
|
1696 |
||
1 | 1697 |
digitAt:index |
1698 |
"return 8 bits of value, starting at byte index" |
|
1699 |
||
1700 |
%{ /* NOCONTEXT */ |
|
1701 |
||
357 | 1702 |
REGISTER INT val; |
1703 |
INT idx; |
|
1 | 1704 |
|
252 | 1705 |
if (__isSmallInteger(index)) { |
4167 | 1706 |
val = __intVal(self); |
1707 |
if (val < 0) |
|
1708 |
val = -val; |
|
1709 |
switch (idx = __intVal(index)) { |
|
1710 |
case 1: |
|
1711 |
break; |
|
1712 |
case 2: |
|
1713 |
val = (val >> 8); |
|
1714 |
break; |
|
1715 |
case 3: |
|
1716 |
val = (val >> 16); |
|
1717 |
break; |
|
1718 |
case 4: |
|
1719 |
val = (val >> 24); |
|
1720 |
break; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1721 |
#if __POINTER_SIZE__ == 8 |
4167 | 1722 |
case 5: |
1723 |
val = (val >> 32); |
|
1724 |
break; |
|
1725 |
case 6: |
|
1726 |
val = (val >> 40); |
|
1727 |
break; |
|
1728 |
case 7: |
|
1729 |
val = (val >> 48); |
|
1730 |
break; |
|
1731 |
case 8: |
|
1732 |
val = (val >> 56); |
|
1733 |
break; |
|
1 | 1734 |
#endif |
4167 | 1735 |
default: |
1736 |
if (idx < 1) |
|
1737 |
goto bad; /* sorry */ |
|
8913 | 1738 |
RETURN (__mkSmallInteger(0)); |
4167 | 1739 |
} |
8913 | 1740 |
RETURN ( __mkSmallInteger( val & 0xFF) ); |
1 | 1741 |
} |
357 | 1742 |
bad: ; |
77 | 1743 |
%}. |
1744 |
index > 0 ifFalse:[ |
|
4167 | 1745 |
" |
1746 |
index less than 1 - not allowed |
|
1747 |
" |
|
1748 |
^ self primitiveFailed |
|
77 | 1749 |
]. |
1750 |
^ 0 |
|
62 | 1751 |
|
357 | 1752 |
" |
1753 |
(16r12345678 digitAt:1) printStringRadix:16 |
|
1754 |
(16r12345678 digitAt:3) printStringRadix:16 |
|
1755 |
(16r12345678 digitAt:15) printStringRadix:16 |
|
1756 |
(16r12345678 digitAt:0) printStringRadix:16 |
|
1757 |
(16r12345678 digitAt:-10) printStringRadix:16 |
|
1758 |
" |
|
701 | 1759 |
! |
1760 |
||
3892 | 1761 |
digitByteAt:index |
1762 |
"return 8 bits of my signed value, starting at byte index. |
|
1763 |
For positive receivers, this is the same as #digitAt:; |
|
1764 |
for negative ones, the actual bit representation is returned." |
|
1765 |
||
1766 |
%{ /* NOCONTEXT */ |
|
1767 |
||
1768 |
REGISTER INT val; |
|
1769 |
INT idx; |
|
1770 |
||
1771 |
if (__isSmallInteger(index)) { |
|
4167 | 1772 |
val = __intVal(self); |
1773 |
switch (idx = __intVal(index)) { |
|
1774 |
case 1: |
|
1775 |
break; |
|
1776 |
case 2: |
|
1777 |
val = (val >> 8); |
|
1778 |
break; |
|
1779 |
case 3: |
|
1780 |
val = (val >> 16); |
|
1781 |
break; |
|
1782 |
case 4: |
|
1783 |
val = (val >> 24); |
|
1784 |
break; |
|
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
1785 |
#if __POINTER_SIZE__ == 8 |
4167 | 1786 |
case 5: |
1787 |
val = (val >> 32); |
|
1788 |
break; |
|
1789 |
case 6: |
|
1790 |
val = (val >> 40); |
|
1791 |
break; |
|
1792 |
case 7: |
|
1793 |
val = (val >> 48); |
|
1794 |
break; |
|
1795 |
case 8: |
|
1796 |
val = (val >> 56); |
|
1797 |
break; |
|
3892 | 1798 |
#endif |
4167 | 1799 |
default: |
1800 |
if (idx < 1) |
|
1801 |
goto bad; /* sorry */ |
|
1802 |
if (val < 0) { |
|
8913 | 1803 |
RETURN (__mkSmallInteger(0xFF)); |
4167 | 1804 |
} |
8913 | 1805 |
RETURN (__mkSmallInteger(0)); |
4167 | 1806 |
} |
8913 | 1807 |
RETURN ( __mkSmallInteger( val & 0xFF) ); |
3892 | 1808 |
} |
1809 |
bad: ; |
|
1810 |
%}. |
|
1811 |
index > 0 ifFalse:[ |
|
4167 | 1812 |
" |
1813 |
index less than 1 - not allowed |
|
1814 |
" |
|
1815 |
^ self primitiveFailed |
|
3892 | 1816 |
]. |
1817 |
^ 0 |
|
1818 |
||
1819 |
" |
|
8908 | 1820 |
(10 digitByteAt:1) printStringRadix:16 |
1821 |
(10 digitByteAt:3) printStringRadix:16 |
|
3892 | 1822 |
(-10 digitByteAt:1) printStringRadix:16 |
1823 |
(-10 digitByteAt:3) printStringRadix:16 |
|
1824 |
" |
|
1825 |
! |
|
1826 |
||
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1827 |
digitBytes |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1828 |
"return a byteArray filled with the receivers bits |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1829 |
(8 bits of the absolute value per element), |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1830 |
least significant byte is first" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1831 |
|
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1832 |
|absValue |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1833 |
b1 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1834 |
b2 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1835 |
b3 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1836 |
b4 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1837 |
b5 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1838 |
b6 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1839 |
b7 "{ Class: SmallInteger }" digitByteArray| |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1840 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1841 |
" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1842 |
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
|
1843 |
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
|
1844 |
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
|
1845 |
" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1846 |
self == 0 ifTrue: [ |
15568 | 1847 |
^ ByteArray with:0. |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1848 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1849 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1850 |
self < 0 ifTrue: [ |
15568 | 1851 |
absValue := self negated |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1852 |
] ifFalse: [ |
15568 | 1853 |
absValue := self. |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1854 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1855 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1856 |
b1 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1857 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1858 |
absValue == 0 ifTrue:[ |
15568 | 1859 |
digitByteArray := ByteArray with:b1 |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1860 |
] ifFalse:[ |
15568 | 1861 |
b2 := absValue bitAnd:16rFF. |
1862 |
absValue := absValue bitShift:-8. |
|
1863 |
absValue == 0 ifTrue:[ |
|
1864 |
digitByteArray := ByteArray with:b1 with:b2 |
|
1865 |
] ifFalse:[ |
|
1866 |
b3 := absValue bitAnd:16rFF. |
|
1867 |
absValue := absValue bitShift:-8. |
|
1868 |
absValue == 0 ifTrue:[ |
|
1869 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
1870 |
] ifFalse:[ |
|
1871 |
b4 := absValue bitAnd:16rFF. |
|
1872 |
absValue := absValue bitShift:-8. |
|
1873 |
absValue == 0 ifTrue:[ |
|
1874 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 |
|
1875 |
] ifFalse:[ |
|
1876 |
b5 := absValue bitAnd:16rFF. |
|
1877 |
absValue := absValue bitShift:-8. |
|
1878 |
absValue == 0 ifTrue:[ |
|
1879 |
digitByteArray := ByteArray new:5. |
|
1880 |
digitByteArray at:1 put:b1. |
|
1881 |
digitByteArray at:2 put:b2. |
|
1882 |
digitByteArray at:3 put:b3. |
|
1883 |
digitByteArray at:4 put:b4. |
|
1884 |
digitByteArray at:5 put:b5. |
|
1885 |
] ifFalse:[ |
|
1886 |
b6 := absValue bitAnd:16rFF. |
|
1887 |
absValue := absValue bitShift:-8. |
|
1888 |
absValue == 0 ifTrue:[ |
|
1889 |
digitByteArray := ByteArray new:6. |
|
1890 |
digitByteArray at:1 put:b1. |
|
1891 |
digitByteArray at:2 put:b2. |
|
1892 |
digitByteArray at:3 put:b3. |
|
1893 |
digitByteArray at:4 put:b4. |
|
1894 |
digitByteArray at:5 put:b5. |
|
1895 |
digitByteArray at:6 put:b6. |
|
1896 |
] ifFalse:[ |
|
1897 |
b7 := absValue bitAnd:16rFF. |
|
1898 |
absValue := absValue bitShift:-8. |
|
1899 |
absValue == 0 ifTrue:[ |
|
1900 |
digitByteArray := ByteArray new:7. |
|
1901 |
digitByteArray at:1 put:b1. |
|
1902 |
digitByteArray at:2 put:b2. |
|
1903 |
digitByteArray at:3 put:b3. |
|
1904 |
digitByteArray at:4 put:b4. |
|
1905 |
digitByteArray at:5 put:b5. |
|
1906 |
digitByteArray at:6 put:b6. |
|
1907 |
digitByteArray at:7 put:b7. |
|
1908 |
] ifFalse:[ |
|
1909 |
digitByteArray := ByteArray new:8. |
|
1910 |
digitByteArray at:1 put:b1. |
|
1911 |
digitByteArray at:2 put:b2. |
|
1912 |
digitByteArray at:3 put:b3. |
|
1913 |
digitByteArray at:4 put:b4. |
|
1914 |
digitByteArray at:5 put:b5. |
|
1915 |
digitByteArray at:6 put:b6. |
|
1916 |
digitByteArray at:7 put:b7. |
|
1917 |
digitByteArray at:8 put:absValue. |
|
1918 |
] |
|
1919 |
] |
|
1920 |
] |
|
1921 |
] |
|
1922 |
] |
|
1923 |
] |
|
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1924 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1925 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
1926 |
^ digitByteArray |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1927 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1928 |
" |
15568 | 1929 |
16r12 digitBytes hexPrintString |
1930 |
16r1234 digitBytes hexPrintString |
|
1931 |
16r12345678 digitBytes hexPrintString |
|
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1932 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1933 |
! |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1934 |
|
15568 | 1935 |
digitBytesMSB |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1936 |
"return a byteArray filled with the receivers bits |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1937 |
(8 bits of the absolute value per element), |
15568 | 1938 |
most significant byte is first" |
1939 |
||
1940 |
|absValue |
|
1941 |
b1 "{ Class: SmallInteger }" |
|
1942 |
b2 "{ Class: SmallInteger }" |
|
1943 |
b3 "{ Class: SmallInteger }" |
|
1944 |
b4 "{ Class: SmallInteger }" |
|
1945 |
b5 "{ Class: SmallInteger }" |
|
1946 |
b6 "{ Class: SmallInteger }" |
|
1947 |
b7 "{ Class: SmallInteger }" digitByteArray| |
|
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1948 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
1949 |
" |
15568 | 1950 |
could have simply created a 4-byte largeinteger and normalize it. |
1951 |
The code below does the normalize right away, avoiding the |
|
1952 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
1953 |
" |
|
1954 |
self == 0 ifTrue: [ |
|
1955 |
^ ByteArray with:0. |
|
1956 |
]. |
|
1957 |
||
1958 |
self < 0 ifTrue: [ |
|
1959 |
absValue := self negated |
|
1960 |
] ifFalse: [ |
|
1961 |
absValue := self. |
|
1962 |
]. |
|
1963 |
||
1964 |
b1 := absValue bitAnd:16rFF. |
|
1965 |
absValue := absValue bitShift:-8. |
|
1966 |
absValue == 0 ifTrue:[ |
|
1967 |
digitByteArray := ByteArray with:b1 |
|
1968 |
] ifFalse:[ |
|
1969 |
b2 := absValue bitAnd:16rFF. |
|
1970 |
absValue := absValue bitShift:-8. |
|
1971 |
absValue == 0 ifTrue:[ |
|
1972 |
digitByteArray := ByteArray with:b2 with:b1 |
|
1973 |
] ifFalse:[ |
|
1974 |
b3 := absValue bitAnd:16rFF. |
|
1975 |
absValue := absValue bitShift:-8. |
|
1976 |
absValue == 0 ifTrue:[ |
|
1977 |
digitByteArray := ByteArray with:b3 with:b2 with:b1 |
|
1978 |
] ifFalse:[ |
|
1979 |
b4 := absValue bitAnd:16rFF. |
|
1980 |
absValue := absValue bitShift:-8. |
|
1981 |
absValue == 0 ifTrue:[ |
|
1982 |
digitByteArray := ByteArray with:b4 with:b3 with:b2 with:b1 |
|
1983 |
] ifFalse:[ |
|
1984 |
b5 := absValue bitAnd:16rFF. |
|
1985 |
absValue := absValue bitShift:-8. |
|
1986 |
absValue == 0 ifTrue:[ |
|
1987 |
digitByteArray := ByteArray new:5. |
|
1988 |
digitByteArray at:1 put:b5. |
|
1989 |
digitByteArray at:2 put:b4. |
|
1990 |
digitByteArray at:3 put:b3. |
|
1991 |
digitByteArray at:4 put:b2. |
|
1992 |
digitByteArray at:5 put:b1. |
|
1993 |
] ifFalse:[ |
|
1994 |
b6 := absValue bitAnd:16rFF. |
|
1995 |
absValue := absValue bitShift:-8. |
|
1996 |
absValue == 0 ifTrue:[ |
|
1997 |
digitByteArray := ByteArray new:6. |
|
1998 |
digitByteArray at:1 put:b6. |
|
1999 |
digitByteArray at:2 put:b5. |
|
2000 |
digitByteArray at:3 put:b4. |
|
2001 |
digitByteArray at:4 put:b3. |
|
2002 |
digitByteArray at:5 put:b2. |
|
2003 |
digitByteArray at:6 put:b1. |
|
2004 |
] ifFalse:[ |
|
2005 |
b7 := absValue bitAnd:16rFF. |
|
2006 |
absValue := absValue bitShift:-8. |
|
2007 |
absValue == 0 ifTrue:[ |
|
2008 |
digitByteArray := ByteArray new:7. |
|
2009 |
digitByteArray at:1 put:b7. |
|
2010 |
digitByteArray at:2 put:b6. |
|
2011 |
digitByteArray at:3 put:b5. |
|
2012 |
digitByteArray at:4 put:b4. |
|
2013 |
digitByteArray at:5 put:b3. |
|
2014 |
digitByteArray at:6 put:b2. |
|
2015 |
digitByteArray at:7 put:b1. |
|
2016 |
] ifFalse:[ |
|
2017 |
digitByteArray := ByteArray new:8. |
|
2018 |
digitByteArray at:1 put:absValue. |
|
2019 |
digitByteArray at:2 put:b7. |
|
2020 |
digitByteArray at:3 put:b6. |
|
2021 |
digitByteArray at:4 put:b5. |
|
2022 |
digitByteArray at:5 put:b4. |
|
2023 |
digitByteArray at:6 put:b3. |
|
2024 |
digitByteArray at:7 put:b2. |
|
2025 |
digitByteArray at:8 put:b1. |
|
2026 |
] |
|
2027 |
] |
|
2028 |
] |
|
2029 |
] |
|
2030 |
] |
|
2031 |
] |
|
2032 |
]. |
|
2033 |
||
2034 |
^ digitByteArray |
|
2035 |
||
2036 |
" |
|
2037 |
16r12 digitBytesMSB hexPrintString |
|
2038 |
16r1234 digitBytesMSB hexPrintString |
|
2039 |
16r12345678 digitBytesMSB hexPrintString |
|
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2040 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2041 |
! |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2042 |
|
701 | 2043 |
digitLength |
2815 | 2044 |
"return the number bytes required to represent this Integer. |
2045 |
For negative receivers, the digitLength of its absolute value |
|
2046 |
is returned." |
|
2047 |
||
2048 |
%{ /* NOCONTEXT */ |
|
2049 |
||
2050 |
INT val = __intVal(self); |
|
2051 |
||
2052 |
if (val < 0) { |
|
4167 | 2053 |
val = -val; |
2815 | 2054 |
} |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8506
diff
changeset
|
2055 |
#if __POINTER_SIZE__ == 8 |
2815 | 2056 |
if (val & 0xFFFFFFFF00000000L) { |
4167 | 2057 |
if (val & 0xFFFF000000000000L) { |
2058 |
if (val & 0xFF00000000000000L) { |
|
8913 | 2059 |
RETURN ( __mkSmallInteger(8)); |
4167 | 2060 |
} else { |
8913 | 2061 |
RETURN ( __mkSmallInteger(7)); |
4167 | 2062 |
} |
2063 |
} else { |
|
2064 |
if (val & 0x0000FF0000000000L) { |
|
8913 | 2065 |
RETURN ( __mkSmallInteger(6)); |
4167 | 2066 |
} else { |
8913 | 2067 |
RETURN ( __mkSmallInteger(5)); |
4167 | 2068 |
} |
2069 |
} |
|
2815 | 2070 |
} |
2071 |
#endif |
|
2072 |
||
2073 |
if (val & 0xFFFF0000) { |
|
4167 | 2074 |
if (val & 0xFF000000) { |
8913 | 2075 |
RETURN ( __mkSmallInteger(4)); |
4167 | 2076 |
} else { |
8913 | 2077 |
RETURN ( __mkSmallInteger(3)); |
4167 | 2078 |
} |
2815 | 2079 |
} else { |
4167 | 2080 |
if (val & 0x0000FF00) { |
8913 | 2081 |
RETURN ( __mkSmallInteger(2)); |
4167 | 2082 |
} else { |
8913 | 2083 |
RETURN ( __mkSmallInteger(1)); |
4167 | 2084 |
} |
2815 | 2085 |
} |
2086 |
||
2087 |
%}. |
|
701 | 2088 |
^ self abs highBit - 1 // 8 + 1 |
2815 | 2089 |
|
2090 |
" |
|
2091 |
16rFF00000000000000 digitLength |
|
2092 |
-16rFF00000000000000 digitLength |
|
2093 |
||
2094 |
16rFF000000 digitLength |
|
8908 | 2095 |
16rFF0000 digitLength |
2096 |
16rFF00 digitLength |
|
2097 |
16rFF digitLength |
|
2098 |
-16rFF000000 digitLength |
|
2099 |
-16rFF0000 digitLength |
|
2100 |
-16rFF00 digitLength |
|
2101 |
-16rFF digitLength |
|
2815 | 2102 |
" |
14167 | 2103 |
! |
2104 |
||
2105 |
swapBytes |
|
16885 | 2106 |
"swap bytes pair-wise in a positive integer |
2107 |
i.e. a.b.c.d -> b.a.d.c. |
|
2108 |
||
2109 |
Redefined here for speed. |
|
2110 |
Swapping of negative integers is undefined and therefore not supported. |
|
2111 |
This case is handled in the superclass." |
|
14167 | 2112 |
|
2113 |
%{ /* NOCONTEXT */ |
|
2114 |
||
15322 | 2115 |
unsigned INT v = __intVal(self); |
16885 | 2116 |
|
2117 |
if ((INT)v >= 0) { |
|
2118 |
unsigned INT swapped; |
|
15322 | 2119 |
|
2120 |
#if __POINTER_SIZE__ == 8 |
|
16885 | 2121 |
swapped = ((v >> 8) & 0x00FF00FF00FF00FF) | ((v & 0x00FF00FF00FF00FF) << 8); |
15322 | 2122 |
#else |
16885 | 2123 |
swapped = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); |
15322 | 2124 |
#endif /* __POINTER_SIZE__ */ |
16885 | 2125 |
|
2126 |
//if (__ISVALIDINTEGER(swapped)) { // sorry, but this does not work here if (INT)swapped would be negative |
|
2127 |
if (swapped <= _MAX_INT) { |
|
2128 |
RETURN ( __mkSmallInteger(swapped) ); |
|
2129 |
} |
|
2130 |
RETURN (__MKUINT(swapped)); |
|
15322 | 2131 |
} |
14167 | 2132 |
%}. |
2133 |
^ super swapBytes |
|
2134 |
||
2135 |
" |
|
16873 | 2136 |
-1 swapBytes hexPrintString |
15325 | 2137 |
16r11223344 swapBytes hexPrintString |
16873 | 2138 |
16r44332211 swapBytes hexPrintString |
2139 |
self maxVal swapBytes hexPrintString |
|
2140 |
self maxVal swapBytes swapBytes hexPrintString |
|
15325 | 2141 |
16r1122334455667788 swapBytes hexPrintString |
2142 |
16r11223344556677889900 swapBytes hexPrintString |
|
14167 | 2143 |
" |
2144 |
||
2145 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
1 | 2146 |
! ! |
2147 |
||
701 | 2148 |
!SmallInteger methodsFor:'catching messages'! |
1 | 2149 |
|
701 | 2150 |
basicAt:index |
2151 |
"catch indexed access - report an error |
|
2152 |
defined here since basicAt: in Object ommits the SmallInteger check." |
|
2153 |
||
14540 | 2154 |
^ self notIndexed |
1 | 2155 |
! |
2156 |
||
701 | 2157 |
basicAt:index put:anObject |
2158 |
"catch indexed access - report an error |
|
2159 |
defined here since basicAt:put: in Object ommits the SmallInteger check." |
|
2160 |
||
2161 |
self notIndexed |
|
2162 |
! |
|
1 | 2163 |
|
701 | 2164 |
basicSize |
2165 |
"return the number of indexed instvars - SmallIntegers have none. |
|
2166 |
Defined here since basicSize in Object ommits the SmallInteger check." |
|
2167 |
||
2168 |
^ 0 |
|
2169 |
! |
|
2170 |
||
2171 |
size |
|
2172 |
"return the number of indexed instvars - SmallIntegers have none." |
|
2173 |
||
2174 |
^ 0 |
|
1 | 2175 |
! ! |
2176 |
||
5240 | 2177 |
!SmallInteger methodsFor:'coercing & converting'! |
1 | 2178 |
|
701 | 2179 |
asCharacter |
2180 |
"Return a character with the receiver as ascii value" |
|
41 | 2181 |
|
701 | 2182 |
^ Character value:self |
1 | 2183 |
! |
2184 |
||
2185 |
asFloat |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2186 |
"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
|
2187 |
Redefined for performance (machine can do it faster)" |
1 | 2188 |
|
2189 |
%{ /* NOCONTEXT */ |
|
2190 |
||
2191 |
OBJ newFloat; |
|
1133 | 2192 |
double dVal = (double)__intVal(self); |
1 | 2193 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2194 |
__qMKFLOAT(newFloat, dVal); |
1 | 2195 |
RETURN ( newFloat ); |
5412 | 2196 |
%}. |
2197 |
^ self primitiveFailed |
|
1 | 2198 |
! |
2199 |
||
2200 |
asLargeInteger |
|
2201 |
"return a LargeInteger with same value as receiver" |
|
2202 |
||
2203 |
^ LargeInteger value:self |
|
2204 |
! |
|
2205 |
||
1199 | 2206 |
asShortFloat |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
2207 |
"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
|
2208 |
Redefined for performance (machine can do it faster)" |
1199 | 2209 |
|
2210 |
%{ /* NOCONTEXT */ |
|
2211 |
||
2212 |
OBJ dummy = @global(ShortFloat); |
|
2213 |
OBJ newFloat; |
|
2214 |
float fVal = (float)__intVal(self); |
|
2215 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
2216 |
__qMKSFLOAT(newFloat, fVal); |
1199 | 2217 |
RETURN ( newFloat ); |
5412 | 2218 |
%}. |
2219 |
^ self primitiveFailed |
|
1199 | 2220 |
! |
2221 |
||
8408
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
2222 |
codePoint |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
2223 |
"for compatibility with Characters. |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
2224 |
(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
|
2225 |
|
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
2226 |
^ self |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
2227 |
! |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
2228 |
|
701 | 2229 |
coerce:aNumber |
11718 | 2230 |
"convert the argument aNumber into an instance of the receivers class and return it." |
701 | 2231 |
|
2232 |
^ aNumber asInteger |
|
2233 |
! |
|
2234 |
||
2235 |
generality |
|
2236 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
2237 |
||
2238 |
^ 20 |
|
1336 | 2239 |
! |
2240 |
||
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2241 |
signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2242 |
"return a smallInteger from sign-extending the 24'th bit. |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2243 |
May be useful for communication interfaces" |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2244 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2245 |
%{ /* NOCONTEXT */ |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2246 |
INT i = __intVal(self); |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2247 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2248 |
if (i & 0x800000) { |
14632 | 2249 |
i = i | ~0xFFFFFFL; |
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2250 |
} else { |
14632 | 2251 |
i = i & 0x7FFFFF; |
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2252 |
} |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2253 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2254 |
RETURN (__mkSmallInteger(i)); |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2255 |
%}. |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2256 |
^ self primitiveFailed |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2257 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2258 |
" |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2259 |
16rFFFFFF signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2260 |
16r800000 signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2261 |
16r7FFFFF signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2262 |
" |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2263 |
! |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
2264 |
|
1336 | 2265 |
signExtendedByteValue |
2266 |
"return a smallInteger from sign-extending the 8'th bit. |
|
2267 |
May be useful for communication interfaces" |
|
2268 |
||
2269 |
%{ /* NOCONTEXT */ |
|
2781 | 2270 |
INT i = __intVal(self); |
1336 | 2271 |
|
2272 |
if (i & 0x80) { |
|
14632 | 2273 |
i = i | ~0xFFL; |
1336 | 2274 |
} else { |
14632 | 2275 |
i = i & 0x7F; |
1336 | 2276 |
} |
2277 |
||
8913 | 2278 |
RETURN (__mkSmallInteger(i)); |
5412 | 2279 |
%}. |
2280 |
^ self primitiveFailed |
|
1336 | 2281 |
|
2282 |
" |
|
14632 | 2283 |
16rFF signExtendedByteValue |
2284 |
16r80 signExtendedByteValue |
|
2285 |
16r7F signExtendedByteValue |
|
1336 | 2286 |
" |
2287 |
! |
|
2288 |
||
15083 | 2289 |
signExtendedLongValue |
2290 |
"return a smallInteger from sign-extending the 32'th bit. |
|
2291 |
May be useful for communication interfaces" |
|
2292 |
||
2293 |
%{ /* NOCONTEXT */ |
|
2294 |
INT i = __intVal(self); |
|
2295 |
||
2296 |
if (i & 0x80000000) { |
|
2297 |
i = i | ~0xFFFFFFFFL; |
|
2298 |
} else { |
|
2299 |
i = i & 0x7FFFFFFF; |
|
2300 |
} |
|
2301 |
||
2302 |
RETURN (__mkSmallInteger(i)); |
|
2303 |
%}. |
|
2304 |
^ self primitiveFailed |
|
2305 |
||
2306 |
" |
|
2307 |
16rFFFFFFFF signExtendedLongValue |
|
2308 |
16r80000000 signExtendedLongValue |
|
2309 |
16r7FFFFFFF signExtendedLongValue |
|
2310 |
" |
|
2311 |
! |
|
2312 |
||
1336 | 2313 |
signExtendedShortValue |
2314 |
"return a smallInteger from sign-extending the 16'th bit. |
|
2315 |
May be useful for communication interfaces" |
|
2316 |
||
2317 |
%{ /* NOCONTEXT */ |
|
2781 | 2318 |
INT i = __intVal(self); |
1336 | 2319 |
|
2320 |
if (i & 0x8000) { |
|
14632 | 2321 |
i = i | ~0xFFFFL; |
1336 | 2322 |
} else { |
14632 | 2323 |
i = i & 0x7FFF; |
1336 | 2324 |
} |
2325 |
||
8913 | 2326 |
RETURN (__mkSmallInteger(i)); |
5412 | 2327 |
%}. |
2328 |
^ self primitiveFailed |
|
1336 | 2329 |
|
2330 |
" |
|
14632 | 2331 |
16rFFFF signExtendedShortValue |
2332 |
16r8000 signExtendedShortValue |
|
2333 |
16r7FFF signExtendedShortValue |
|
1336 | 2334 |
" |
701 | 2335 |
! ! |
2336 |
||
2337 |
!SmallInteger methodsFor:'comparing'! |
|
2338 |
||
2339 |
< aNumber |
|
2340 |
"return true, if the argument is greater than the receiver" |
|
2341 |
||
2342 |
%{ /* NOCONTEXT */ |
|
2343 |
||
2344 |
if (__isSmallInteger(aNumber)) { |
|
2345 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2346 |
RETURN ( (__intVal(self) < __intVal(aNumber)) ? true : false ); |
701 | 2347 |
#else |
8908 | 2348 |
/* tag bit does not change ordering */ |
2349 |
RETURN ( ((INT)self < (INT)aNumber) ? true : false ); |
|
701 | 2350 |
#endif |
2351 |
} |
|
2352 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2353 |
RETURN ( ((double)__intVal(self) < __floatVal(aNumber)) ? true : false ); |
701 | 2354 |
} |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2355 |
%}. |
701 | 2356 |
^ aNumber lessFromInteger:self |
2357 |
"^ self retry:#< coercing:aNumber" |
|
2358 |
! |
|
2359 |
||
2360 |
<= aNumber |
|
2361 |
"return true, if the argument is greater or equal" |
|
2362 |
||
2363 |
%{ /* NOCONTEXT */ |
|
2364 |
||
2365 |
if (__isSmallInteger(aNumber)) { |
|
2366 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2367 |
RETURN ( (__intVal(self) <= __intVal(aNumber)) ? true : false ); |
701 | 2368 |
#else |
8908 | 2369 |
/* tag bit does not change ordering */ |
2370 |
RETURN ( ((INT)self <= (INT)aNumber) ? true : false ); |
|
701 | 2371 |
#endif |
2372 |
} |
|
2373 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2374 |
RETURN ( ((double)__intVal(self) <= __floatVal(aNumber)) ? true : false ); |
701 | 2375 |
} |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2376 |
%}. |
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2377 |
^ (self > aNumber) not |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2378 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2379 |
"Modified: / 31.7.2002 / 10:07:17 / cg" |
701 | 2380 |
! |
2381 |
||
2382 |
= aNumber |
|
7356 | 2383 |
"return true, if the argument represents the same numeric value |
2384 |
as the receiver, false otherwise" |
|
701 | 2385 |
|
2386 |
%{ /* NOCONTEXT */ |
|
2387 |
||
2388 |
if (aNumber == self) { |
|
8908 | 2389 |
RETURN ( true ); |
701 | 2390 |
} |
2391 |
if (! __isNonNilObject(aNumber)) { |
|
8908 | 2392 |
/* a smallint or nil */ |
2393 |
RETURN ( false ); |
|
701 | 2394 |
} |
2395 |
||
7356 | 2396 |
if (__qIsFloatLike(aNumber)) { |
8908 | 2397 |
RETURN ( ((double)__intVal(self) == __floatVal(aNumber)) ? true : false ); |
701 | 2398 |
} |
7356 | 2399 |
if (__qIsShortFloat(aNumber)) { |
8908 | 2400 |
RETURN ( ((double)__intVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
7356 | 2401 |
} |
2402 |
%}. |
|
2403 |
^ aNumber equalFromInteger:self |
|
701 | 2404 |
! |
2405 |
||
2406 |
> aNumber |
|
2407 |
"return true, if the argument is less than the receiver" |
|
2408 |
||
2409 |
%{ /* NOCONTEXT */ |
|
2410 |
||
2411 |
if (__isSmallInteger(aNumber)) { |
|
2412 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2413 |
RETURN ( (__intVal(self) > __intVal(aNumber)) ? true : false ); |
701 | 2414 |
#else |
8908 | 2415 |
/* tag bit does not change ordering */ |
2416 |
RETURN ( ((INT)self > (INT)aNumber) ? true : false ); |
|
701 | 2417 |
#endif |
2418 |
} |
|
2419 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2420 |
RETURN ( ((double)__intVal(self) > __floatVal(aNumber)) ? true : false ); |
701 | 2421 |
} |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2422 |
%}. |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2423 |
^ (aNumber < self) |
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2424 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2425 |
"Modified: / 31.7.2002 / 10:07:05 / cg" |
701 | 2426 |
! |
2427 |
||
2428 |
>= aNumber |
|
2429 |
"return true, if the argument is less or equal" |
|
2430 |
||
2431 |
%{ /* NOCONTEXT */ |
|
2432 |
||
2433 |
if (__isSmallInteger(aNumber)) { |
|
2434 |
#ifdef POSITIVE_ADDRESSES |
|
8908 | 2435 |
RETURN ( (__intVal(self) >= __intVal(aNumber)) ? true : false ); |
701 | 2436 |
#else |
8908 | 2437 |
/* tag bit does not change ordering */ |
2438 |
RETURN ( ((INT)self >= (INT)aNumber) ? true : false ); |
|
701 | 2439 |
#endif |
2440 |
} |
|
2441 |
if (__isFloatLike(aNumber)) { |
|
8908 | 2442 |
RETURN ( ((double)__intVal(self) >= __floatVal(aNumber)) ? true : false ); |
701 | 2443 |
} |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2444 |
%}. |
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
2445 |
^ (self < aNumber) not |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2446 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
2447 |
"Modified: / 31.7.2002 / 10:07:11 / cg" |
701 | 2448 |
! |
1 | 2449 |
|
701 | 2450 |
hash |
2451 |
"return an integer useful for hashing on value" |
|
2452 |
||
2453 |
self >= 0 ifTrue:[^ self]. |
|
2454 |
^ self negated |
|
2455 |
! |
|
2456 |
||
2457 |
identityHash |
|
2458 |
"return an integer useful for hashing on identity" |
|
2459 |
||
1964
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
2460 |
self >= 0 ifTrue:[^ self]. |
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
2461 |
^ self negated |
1963
ea26c7b9ca1d
experimental: new hash value for smallInts
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2462 |
|
1964
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
2463 |
"Modified: 11.11.1996 / 18:42:14 / cg" |
701 | 2464 |
! |
2465 |
||
2466 |
max:aNumber |
|
2467 |
"return the receiver or the argument, whichever is greater" |
|
2468 |
||
2469 |
%{ /* NOCONTEXT */ |
|
2470 |
||
2471 |
if (__isSmallInteger(aNumber)) { |
|
2216 | 2472 |
#if TAG_INT == 1 |
2473 |
/* tag bit does not change ordering */ |
|
2474 |
if ((INT)(self) > (INT)(aNumber)) |
|
701 | 2475 |
#else |
2216 | 2476 |
if (__intVal(self) > __intVal(aNumber)) |
701 | 2477 |
#endif |
2216 | 2478 |
{ |
701 | 2479 |
RETURN ( self ); |
2480 |
} |
|
2481 |
RETURN ( aNumber ); |
|
2482 |
} |
|
2483 |
if (__isFloatLike(aNumber)) { |
|
1133 | 2484 |
if ( (double)__intVal(self) > __floatVal(aNumber) ) { |
701 | 2485 |
RETURN ( self ); |
2486 |
} |
|
2487 |
RETURN ( aNumber ); |
|
2488 |
} |
|
2216 | 2489 |
%}. |
2490 |
"/ fallback for non-smallInteger argument |
|
2491 |
||
701 | 2492 |
(self > aNumber) ifTrue:[^ self]. |
2493 |
^ aNumber |
|
2494 |
! |
|
2495 |
||
2496 |
min:aNumber |
|
2497 |
"return the receiver or the argument, whichever is smaller" |
|
2498 |
||
2499 |
%{ /* NOCONTEXT */ |
|
2500 |
||
2501 |
if (__isSmallInteger(aNumber)) { |
|
2216 | 2502 |
#if TAG_INT == 1 |
2503 |
/* tag bit does not change ordering */ |
|
2504 |
if ((INT)(self) < (INT)(aNumber)) |
|
701 | 2505 |
#else |
2216 | 2506 |
if (__intVal(self) < __intVal(aNumber)) |
701 | 2507 |
#endif |
2216 | 2508 |
{ |
701 | 2509 |
RETURN ( self ); |
2510 |
} |
|
2511 |
RETURN ( aNumber ); |
|
2512 |
} |
|
2513 |
if (__isFloatLike(aNumber)) { |
|
1133 | 2514 |
if ( (double)__intVal(self) < __floatVal(aNumber) ) { |
701 | 2515 |
RETURN ( self ); |
2516 |
} |
|
2517 |
RETURN ( aNumber ); |
|
2518 |
} |
|
2216 | 2519 |
%}. |
2520 |
"/ fallback for non-smallInteger argument |
|
2521 |
||
701 | 2522 |
(self < aNumber) ifTrue:[^ self]. |
2523 |
^ aNumber |
|
2524 |
! |
|
2525 |
||
2526 |
~= aNumber |
|
2527 |
"return true, if the arguments value is not equal to mine" |
|
2528 |
||
2529 |
%{ /* NOCONTEXT */ |
|
2530 |
||
2531 |
if (aNumber == self) { |
|
8908 | 2532 |
RETURN ( false ); |
701 | 2533 |
} |
2534 |
if (! __isNonNilObject(aNumber)) { |
|
8908 | 2535 |
/* a smallint or nil */ |
2536 |
RETURN ( true ); |
|
701 | 2537 |
} |
2538 |
||
7356 | 2539 |
if (__qIsFloatLike(aNumber)) { |
8908 | 2540 |
RETURN ( ((double)__intVal(self) != __floatVal(aNumber)) ? true : false ); |
7356 | 2541 |
} |
2542 |
if (__qIsShortFloat(aNumber)) { |
|
8908 | 2543 |
RETURN ( ((double)__intVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
701 | 2544 |
} |
2216 | 2545 |
%}. |
7356 | 2546 |
^ (self = aNumber) not |
701 | 2547 |
! ! |
2548 |
||
2549 |
!SmallInteger methodsFor:'copying'! |
|
2550 |
||
2551 |
deepCopy |
|
2552 |
"return a deep copy of myself |
|
2553 |
- reimplemented here since smallintegers are unique" |
|
2554 |
||
2555 |
^ self |
|
2556 |
! |
|
2557 |
||
10947 | 2558 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
701 | 2559 |
"return a deep copy of myself |
2560 |
- reimplemented here since smallintegers are unique" |
|
2561 |
||
2562 |
^ self |
|
2563 |
! |
|
2564 |
||
2565 |
shallowCopy |
|
2566 |
"return a shallow copy of myself |
|
2567 |
- reimplemented here since smallintegers are unique" |
|
2568 |
||
2569 |
^ self |
|
2570 |
! |
|
2571 |
||
2572 |
simpleDeepCopy |
|
2573 |
"return a deep copy of myself |
|
2574 |
- reimplemented here since smallintegers are unique" |
|
2575 |
||
2576 |
^ self |
|
1 | 2577 |
! ! |
2578 |
||
214 | 2579 |
!SmallInteger methodsFor:'iteration'! |
1 | 2580 |
|
2581 |
timesRepeat:aBlock |
|
357 | 2582 |
"evaluate the argument, aBlock self times. |
2583 |
Reimplemented as primitive for speed" |
|
1 | 2584 |
|
357 | 2585 |
%{ |
2586 |
REGISTER INT tmp; |
|
2587 |
static struct inlineCache blockVal = __ILC0(0); |
|
1 | 2588 |
|
357 | 2589 |
tmp = __intVal(self); |
2590 |
if (tmp > 0) { |
|
8908 | 2591 |
if (__isBlockLike(aBlock) |
8913 | 2592 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(0))) { |
8908 | 2593 |
{ |
2594 |
REGISTER OBJFUNC codeVal; |
|
2595 |
||
2596 |
/* |
|
2597 |
* specially tuned version for compiled blocks, |
|
2598 |
* (the most common case) |
|
2599 |
*/ |
|
2600 |
if (((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
|
2678 | 2601 |
#ifdef PARANOIA |
8908 | 2602 |
&& (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
2678 | 2603 |
#endif |
8908 | 2604 |
) { |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2605 |
|
357 | 2606 |
#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
|
2607 |
|
2254 | 2608 |
# 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
|
2609 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2610 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2611 |
|
2254 | 2612 |
# define BLOCK_ARG rHome |
8908 | 2613 |
REGISTER OBJ rHome; |
2614 |
||
2615 |
/* |
|
2616 |
* home on stack - no need to refetch |
|
2617 |
*/ |
|
2618 |
rHome = __BlockInstPtr(aBlock)->b_home; |
|
2619 |
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
|
2620 |
#endif |
8908 | 2621 |
{ |
8919 | 2622 |
#ifdef __UNROLL_LOOPS__ |
2216 | 2623 |
|
8908 | 2624 |
/* |
2625 |
* you are not supposed to program like this - I know what I do |
|
2626 |
*/ |
|
2627 |
while (tmp > 8) { |
|
2628 |
if (InterruptPending != nil) goto interrupted0; |
|
2629 |
continue0: |
|
2630 |
(*codeVal)(BLOCK_ARG); |
|
2631 |
if (InterruptPending != nil) goto interrupted1; |
|
2632 |
continue1: |
|
2633 |
(*codeVal)(BLOCK_ARG); |
|
2634 |
if (InterruptPending != nil) goto interrupted2; |
|
2635 |
continue2: |
|
2636 |
(*codeVal)(BLOCK_ARG); |
|
2637 |
if (InterruptPending != nil) goto interrupted3; |
|
2638 |
continue3: |
|
2639 |
(*codeVal)(BLOCK_ARG); |
|
2640 |
if (InterruptPending != nil) goto interrupted4; |
|
2641 |
continue4: |
|
2642 |
(*codeVal)(BLOCK_ARG); |
|
2643 |
if (InterruptPending != nil) goto interrupted5; |
|
2644 |
continue5: |
|
2645 |
(*codeVal)(BLOCK_ARG); |
|
2646 |
if (InterruptPending != nil) goto interrupted6; |
|
2647 |
continue6: |
|
2648 |
(*codeVal)(BLOCK_ARG); |
|
2649 |
if (InterruptPending != nil) goto interrupted7; |
|
2650 |
continue7: |
|
2651 |
(*codeVal)(BLOCK_ARG); |
|
2652 |
tmp -= 8; |
|
2653 |
} |
|
8919 | 2654 |
#endif /* __UNROLL_LOOPS__ */ |
8908 | 2655 |
do { |
2656 |
if (InterruptPending != nil) goto interruptedX; |
|
2657 |
continueX: |
|
2658 |
(*codeVal)(BLOCK_ARG); |
|
2659 |
} while(--tmp); |
|
2660 |
||
2661 |
RETURN (self); |
|
2662 |
if (0) { |
|
8919 | 2663 |
#ifdef __UNROLL_LOOPS__ |
8908 | 2664 |
interrupted0: |
2665 |
__interruptL(@line); goto continue0; |
|
2666 |
interrupted1: |
|
2667 |
__interruptL(@line); goto continue1; |
|
2668 |
interrupted2: |
|
2669 |
__interruptL(@line); goto continue2; |
|
2670 |
interrupted3: |
|
2671 |
__interruptL(@line); goto continue3; |
|
2672 |
interrupted4: |
|
2673 |
__interruptL(@line); goto continue4; |
|
2674 |
interrupted5: |
|
2675 |
__interruptL(@line); goto continue5; |
|
2676 |
interrupted6: |
|
2677 |
__interruptL(@line); goto continue6; |
|
2678 |
interrupted7: |
|
2679 |
__interruptL(@line); goto continue7; |
|
8919 | 2680 |
#endif /* __UNROLL_LOOPS__ */ |
8908 | 2681 |
interruptedX: |
2682 |
__interruptL(@line); goto continueX; |
|
2683 |
} |
|
2684 |
} |
|
2685 |
} |
|
2686 |
} |
|
1672
1b56d6e95c9e
changes to call dynamic compiled code right after compilation.
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
2687 |
|
2216 | 2688 |
# undef BLOCK_ARG |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
2689 |
|
400 | 2690 |
#ifdef NEW_BLOCK_CALL |
2216 | 2691 |
# define BLOCK_ARG aBlock |
2692 |
# define IBLOCK_ARG nil |
|
400 | 2693 |
#else |
2216 | 2694 |
# define BLOCK_ARG (__BlockInstPtr(aBlock)->b_home) |
2695 |
# define IBLOCK_ARG (__BlockInstPtr(aBlock)->b_home) |
|
400 | 2696 |
#endif |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
2697 |
|
8908 | 2698 |
/* |
2699 |
* sorry - must check for the blocks code within the loops; |
|
2700 |
* it could be recompiled or flushed (in the interrupt) |
|
2701 |
*/ |
|
2702 |
do { |
|
2703 |
REGISTER OBJFUNC codeVal; |
|
2704 |
||
2705 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
|
2706 |
/* |
|
2707 |
* arg is a compiled block with code - |
|
2708 |
* directly call it without going through Block>>value |
|
2709 |
* however, if there is an interrupt, refetch the code pointer. |
|
2710 |
*/ |
|
2711 |
/* stay here, while no interrupts are pending ... */ |
|
2712 |
do { |
|
2713 |
(*codeVal)(BLOCK_ARG); |
|
2714 |
if (InterruptPending != nil) goto outerLoop; |
|
2715 |
} while (--tmp); |
|
2716 |
RETURN (self); |
|
2717 |
} else { |
|
2718 |
if (InterruptPending != nil) __interruptL(@line); |
|
2719 |
||
2720 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
|
2721 |
/* |
|
2722 |
* arg is a compiled block with bytecode - |
|
2723 |
* directly call interpreter without going through Block>>value |
|
2724 |
*/ |
|
2725 |
__interpret(aBlock, 0, nil, IBLOCK_ARG, nil, nil); |
|
2726 |
} else { |
|
2727 |
/* |
|
2728 |
* arg is something else - call it with #value |
|
2729 |
*/ |
|
2730 |
(*blockVal.ilc_func)(aBlock, @symbol(value), nil, &blockVal); |
|
2731 |
} |
|
2732 |
} |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
2733 |
outerLoop: ; |
8908 | 2734 |
} while (--tmp); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2735 |
|
2216 | 2736 |
# undef BLOCK_ARG |
2737 |
# undef IBLOCK_ARG |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2738 |
|
8908 | 2739 |
RETURN (self); |
2740 |
} |
|
2741 |
||
2742 |
/* |
|
2743 |
* not a block-like thingy - call it with #value |
|
2744 |
*/ |
|
2745 |
do { |
|
2746 |
if (InterruptPending != nil) __interruptL(@line); |
|
2747 |
(*blockVal.ilc_func)(aBlock, @symbol(value), nil, &blockVal); |
|
2748 |
} while(--tmp); |
|
2749 |
RETURN (self); |
|
357 | 2750 |
} |
5412 | 2751 |
%}. |
2752 |
^ super timesRepeat:aBlock |
|
357 | 2753 |
|
2754 |
"/ |count "{ Class: SmallInteger }" | |
|
2755 |
"/ |
|
2756 |
"/ count := self. |
|
2757 |
"/ [count > 0] whileTrue:[ |
|
2758 |
"/ aBlock value. |
|
2759 |
"/ count := count - 1 |
|
2760 |
"/ ] |
|
1 | 2761 |
! |
2762 |
||
701 | 2763 |
to:stop by:incr do:aBlock |
2764 |
"reimplemented as primitive for speed" |
|
2765 |
||
2766 |
%{ |
|
2767 |
REGISTER INT tmp, step; |
|
2768 |
REGISTER INT final; |
|
2769 |
static struct inlineCache blockVal = __ILC1(0); |
|
2770 |
||
2771 |
if (__bothSmallInteger(incr, stop)) { |
|
1133 | 2772 |
tmp = __intVal(self); |
2773 |
final = __intVal(stop); |
|
2774 |
step = __intVal(incr); |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2775 |
|
701 | 2776 |
if (__isBlockLike(aBlock) |
8913 | 2777 |
&& (__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
|
2778 |
{ |
2254 | 2779 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2780 |
|
2216 | 2781 |
/* |
2782 |
* specially tuned version for static compiled blocks, called with |
|
2783 |
* home on the stack (the most common case) |
|
2784 |
*/ |
|
4167 | 2785 |
if (((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
2678 | 2786 |
#ifdef PARANOIA |
4167 | 2787 |
&& (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
2678 | 2788 |
#endif |
2789 |
) { |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2790 |
|
701 | 2791 |
#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
|
2792 |
|
2254 | 2793 |
# 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
|
2794 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2795 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2796 |
|
2254 | 2797 |
# define BLOCK_ARG rHome |
2798 |
REGISTER OBJ rHome; |
|
2799 |
rHome = __BlockInstPtr(aBlock)->b_home; |
|
8908 | 2800 |
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
|
2801 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2802 |
#endif |
2254 | 2803 |
{ |
2804 |
if (step < 0) { |
|
2805 |
if (step == -1) { |
|
2806 |
while (tmp >= final) { |
|
2807 |
if (InterruptPending != nil) __interruptL(@line); |
|
8913 | 2808 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2254 | 2809 |
tmp--; |
2216 | 2810 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2811 |
} else { |
2254 | 2812 |
while (tmp >= final) { |
2216 | 2813 |
if (InterruptPending != nil) __interruptL(@line); |
8913 | 2814 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2216 | 2815 |
tmp += step; |
2816 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2817 |
} |
2254 | 2818 |
} else { |
2819 |
if (step == 1) { |
|
2820 |
while (tmp <= final) { |
|
2821 |
if (InterruptPending != nil) __interruptL(@line); |
|
8913 | 2822 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2254 | 2823 |
tmp++; |
2824 |
} |
|
2825 |
} else { |
|
4167 | 2826 |
while (tmp <= final) { |
2254 | 2827 |
if (InterruptPending != nil) __interruptL(@line); |
8913 | 2828 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2254 | 2829 |
tmp += step; |
2830 |
} |
|
2831 |
} |
|
2216 | 2832 |
} |
2254 | 2833 |
RETURN (self); |
2216 | 2834 |
} |
2835 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2836 |
} |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2837 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2838 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2839 |
* 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
|
2840 |
* 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
|
2841 |
*/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2842 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2843 |
# undef BLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2844 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2845 |
#ifdef NEW_BLOCK_CALL |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2846 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2847 |
# define IBLOCK_ARG nil |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2848 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2849 |
# 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
|
2850 |
# 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
|
2851 |
#endif |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2852 |
|
701 | 2853 |
if (step < 0) { |
2854 |
while (tmp >= final) { |
|
2254 | 2855 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2856 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2857 |
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
|
2858 |
|
2254 | 2859 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2216 | 2860 |
/* |
2861 |
* arg is a compiled block with code - |
|
2862 |
* directly call it without going through Block>>value |
|
2863 |
*/ |
|
8913 | 2864 |
(*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
|
2865 |
} else { |
2216 | 2866 |
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
|
2867 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2868 |
* 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
|
2869 |
* 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
|
2870 |
*/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2871 |
#ifdef PASS_ARG_POINTER |
2216 | 2872 |
{ |
2873 |
OBJ idx; |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2874 |
|
8913 | 2875 |
idx = __mkSmallInteger(tmp); |
2216 | 2876 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
2877 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2878 |
#else |
8913 | 2879 |
__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
|
2880 |
#endif |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2881 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2882 |
} else { |
2216 | 2883 |
/* |
2884 |
* arg is something else - call it with #value |
|
2885 |
*/ |
|
8913 | 2886 |
(*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
|
2887 |
} |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2888 |
} |
701 | 2889 |
tmp += step; |
2890 |
} |
|
2891 |
} else { |
|
2892 |
while (tmp <= final) { |
|
2254 | 2893 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2894 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2895 |
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
|
2896 |
|
2254 | 2897 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2216 | 2898 |
/* |
2899 |
* arg is a compiled block with code - |
|
2900 |
* directly call it without going through Block>>value |
|
2901 |
*/ |
|
8913 | 2902 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2216 | 2903 |
} else { |
2904 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
|
2905 |
/* |
|
2906 |
* arg is a compiled block with bytecode - |
|
2907 |
* directly call interpreter without going through Block>>value |
|
2908 |
*/ |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2909 |
#ifdef PASS_ARG_POINTER |
2216 | 2910 |
{ |
2911 |
OBJ idx; |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2912 |
|
8913 | 2913 |
idx = __mkSmallInteger(tmp); |
2216 | 2914 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
2915 |
} |
|
701 | 2916 |
#else |
8913 | 2917 |
__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
|
2918 |
#endif |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2919 |
|
2216 | 2920 |
} else { |
2921 |
/* |
|
2922 |
* arg is something else - call it with #value: |
|
2923 |
*/ |
|
8913 | 2924 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
2216 | 2925 |
} |
2926 |
} |
|
2927 |
tmp += step; |
|
2928 |
} |
|
701 | 2929 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2930 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2931 |
# undef BLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2932 |
# undef IBLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2933 |
|
701 | 2934 |
} else { |
2935 |
/* |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2936 |
* arg is something else - call it with #value: |
701 | 2937 |
*/ |
2938 |
if (step < 0) { |
|
2939 |
while (tmp >= final) { |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2940 |
if (InterruptPending != nil) __interruptL(@line); |
701 | 2941 |
|
8908 | 2942 |
(*blockVal.ilc_func)(aBlock, |
2943 |
@symbol(value:), |
|
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
2944 |
nil, &blockVal, |
8913 | 2945 |
__mkSmallInteger(tmp)); |
701 | 2946 |
tmp += step; |
2947 |
} |
|
2948 |
} else { |
|
2949 |
while (tmp <= final) { |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2950 |
if (InterruptPending != nil) __interruptL(@line); |
701 | 2951 |
|
8908 | 2952 |
(*blockVal.ilc_func)(aBlock, |
2953 |
@symbol(value:), |
|
1688
8a42db1eea60
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
1672
diff
changeset
|
2954 |
nil, &blockVal, |
8913 | 2955 |
__mkSmallInteger(tmp)); |
701 | 2956 |
tmp += step; |
2957 |
} |
|
2958 |
} |
|
2959 |
} |
|
2960 |
RETURN ( self ); |
|
2961 |
} |
|
1672
1b56d6e95c9e
changes to call dynamic compiled code right after compilation.
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
2962 |
%}. |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2963 |
"/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2964 |
"/ 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
|
2965 |
"/ pass on to super ... |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2966 |
|
701 | 2967 |
^ super to:stop by:incr do:aBlock |
2968 |
||
2969 |
" |
|
2970 |
1 to:10 by:3 do:[:i | i printNewline] |
|
2971 |
" |
|
2972 |
! |
|
2973 |
||
1 | 2974 |
to:stop do:aBlock |
62 | 2975 |
"evaluate aBlock for every integer between (and including) the receiver |
2976 |
and the argument, stop. |
|
357 | 2977 |
Reimplemented as primitive for speed" |
1 | 2978 |
|
2979 |
%{ |
|
2980 |
REGISTER INT tmp; |
|
2981 |
INT final; |
|
216 | 2982 |
static struct inlineCache blockVal = __ILC1(0); |
1 | 2983 |
|
252 | 2984 |
if (__isSmallInteger(stop)) { |
1133 | 2985 |
tmp = __intVal(self); |
2986 |
final = __intVal(stop); |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2987 |
|
214 | 2988 |
if (__isBlockLike(aBlock) |
8913 | 2989 |
&& (__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
|
2990 |
{ |
2216 | 2991 |
/* |
2992 |
* 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
|
2993 |
* where called with home on the stack |
2216 | 2994 |
*/ |
2254 | 2995 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
2996 |
|
4167 | 2997 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2254 | 2998 |
|
13 | 2999 |
#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
|
3000 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3001 |
# define BLOCK_ARG aBlock |
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 |
#else |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3004 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3005 |
# define BLOCK_ARG rHome |
2216 | 3006 |
REGISTER OBJ rHome; |
3007 |
rHome = __BlockInstPtr(aBlock)->b_home; |
|
8908 | 3008 |
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
|
3009 |
#endif |
400 | 3010 |
{ |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3011 |
|
2678 | 3012 |
#ifdef PARANOIA |
8908 | 3013 |
if (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
2678 | 3014 |
#endif |
3015 |
{ |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3016 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3017 |
* static compiled blocks ... |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3018 |
*/ |
8919 | 3019 |
#ifdef __UNROLL_LOOPS__ |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3020 |
/* |
2693 | 3021 |
* The following code is designed to run as fast as possible; |
3022 |
* - taken branches only if interrupts are pending |
|
3023 |
* - only forward branches (which are usually predicted as not taken) |
|
3024 |
* - unrolled the loop |
|
3025 |
* |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3026 |
* 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
|
3027 |
*/ |
2693 | 3028 |
# if TAG_INT==1 |
8913 | 3029 |
INT t8 = (INT)(__mkSmallInteger(tmp+8)); |
3030 |
tmp = (INT)(__mkSmallInteger(tmp)); |
|
3031 |
final = (INT)(__mkSmallInteger(final)); |
|
2693 | 3032 |
# else |
2770 | 3033 |
INT t8 = tmp+8; |
2693 | 3034 |
# endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3035 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3036 |
for (;;) { |
2693 | 3037 |
|
3038 |
while (t8 <= final) { |
|
3039 |
# if TAG_INT==1 |
|
2770 | 3040 |
t8 += (INT)(__MASKSMALLINT(8)); |
2693 | 3041 |
# else |
3042 |
t8 += 8; |
|
3043 |
# endif |
|
3044 |
if (InterruptPending != nil) goto interrupted0; |
|
3045 |
continue0: |
|
3046 |
# if TAG_INT==1 |
|
3047 |
(*codeVal)(BLOCK_ARG, tmp); |
|
3048 |
# else |
|
8913 | 3049 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2693 | 3050 |
# endif |
3051 |
if (InterruptPending != nil) goto interrupted1; |
|
3052 |
continue1: |
|
3053 |
# if TAG_INT==1 |
|
2770 | 3054 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(1)) ); |
2693 | 3055 |
# else |
8913 | 3056 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+1)); |
2693 | 3057 |
# endif |
3058 |
if (InterruptPending != nil) goto interrupted2; |
|
3059 |
continue2: |
|
3060 |
# if TAG_INT==1 |
|
2770 | 3061 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(2)) ); |
2693 | 3062 |
# else |
8913 | 3063 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+2)); |
2693 | 3064 |
# endif |
3065 |
if (InterruptPending != nil) goto interrupted3; |
|
3066 |
continue3: |
|
3067 |
# if TAG_INT==1 |
|
2770 | 3068 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(3)) ); |
2693 | 3069 |
# else |
8913 | 3070 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+3)); |
2693 | 3071 |
# endif |
3072 |
if (InterruptPending != nil) goto interrupted4; |
|
3073 |
continue4: |
|
3074 |
# if TAG_INT==1 |
|
2770 | 3075 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(4)) ); |
2693 | 3076 |
# else |
8913 | 3077 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+4)); |
2693 | 3078 |
# endif |
3079 |
if (InterruptPending != nil) goto interrupted5; |
|
3080 |
continue5: |
|
3081 |
# if TAG_INT==1 |
|
2770 | 3082 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(5)) ); |
2693 | 3083 |
# else |
8913 | 3084 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+5)); |
2693 | 3085 |
# endif |
3086 |
if (InterruptPending != nil) goto interrupted6; |
|
3087 |
continue6: |
|
3088 |
# if TAG_INT==1 |
|
2770 | 3089 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(6)) ); |
2693 | 3090 |
# else |
8913 | 3091 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+6)); |
2693 | 3092 |
# endif |
3093 |
if (InterruptPending != nil) goto interrupted7; |
|
3094 |
continue7: |
|
3095 |
# if TAG_INT==1 |
|
2770 | 3096 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(7)) ); |
2693 | 3097 |
# else |
8913 | 3098 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+7)); |
2693 | 3099 |
# endif |
3100 |
||
3101 |
# if TAG_INT==1 |
|
2770 | 3102 |
tmp += (INT)(__MASKSMALLINT(8)); |
2693 | 3103 |
# else |
3104 |
tmp += 8; |
|
3105 |
# endif |
|
3106 |
} |
|
4167 | 3107 |
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
|
3108 |
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
|
3109 |
continueX: |
2693 | 3110 |
# if TAG_INT==1 |
3111 |
(*codeVal)(BLOCK_ARG, tmp); |
|
2770 | 3112 |
tmp += (INT)(__MASKSMALLINT(1)); |
2693 | 3113 |
# else |
8913 | 3114 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2693 | 3115 |
tmp++; |
3116 |
# endif |
|
4167 | 3117 |
} |
3118 |
RETURN (self); |
|
2693 | 3119 |
|
2216 | 3120 |
if (0) { |
3121 |
/* |
|
3122 |
* no discussion about those gotos ... |
|
3123 |
* ... its better for your CPU's pipelines |
|
3124 |
* (if you dont understand why, just dont argue). |
|
3125 |
*/ |
|
3126 |
interrupted7: |
|
3127 |
__interruptL(@line); goto continue7; |
|
3128 |
interrupted6: |
|
3129 |
__interruptL(@line); goto continue6; |
|
3130 |
interrupted5: |
|
3131 |
__interruptL(@line); goto continue5; |
|
3132 |
interrupted4: |
|
3133 |
__interruptL(@line); goto continue4; |
|
3134 |
interrupted3: |
|
3135 |
__interruptL(@line); goto continue3; |
|
3136 |
interrupted2: |
|
3137 |
__interruptL(@line); goto continue2; |
|
3138 |
interrupted1: |
|
3139 |
__interruptL(@line); goto continue1; |
|
3140 |
interrupted0: |
|
3141 |
__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
|
3142 |
interruptedX: |
5295fc0fddb7
oops - right shift by int-size did not work on some machines
Claus Gittinger <cg@exept.de>
parents:
2815
diff
changeset
|
3143 |
__interruptL(@line); goto continueX; |
2216 | 3144 |
} |
2640 | 3145 |
} |
2641 | 3146 |
#else |
2640 | 3147 |
while (tmp <= final) { |
3148 |
if (InterruptPending != nil) __interruptL(@line); |
|
8913 | 3149 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2640 | 3150 |
tmp ++; |
2216 | 3151 |
} |
2640 | 3152 |
RETURN (self); |
8919 | 3153 |
#endif /* __UNROLL_LOOPS__ */ |
2216 | 3154 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3155 |
|
2216 | 3156 |
/* |
3157 |
* mhmh - seems to be a block with dynamic code |
|
3158 |
* must refetch, to allow dynamic recompilation or code flush. |
|
3159 |
*/ |
|
3160 |
while (tmp <= final) { |
|
3161 |
if (InterruptPending != nil) __interruptL(@line); |
|
2254 | 3162 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) == (OBJFUNC)nil) break; |
8913 | 3163 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
2216 | 3164 |
tmp ++; |
3165 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3166 |
|
2216 | 3167 |
if (tmp > final) { |
3168 |
RETURN (self); |
|
3169 |
} |
|
3170 |
} |
|
3171 |
} |
|
3172 |
} |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3173 |
|
2216 | 3174 |
# undef BLOCK_ARG |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3175 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3176 |
#ifdef NEW_BLOCK_CALL |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3177 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3178 |
# define IBLOCK_ARG nil |
400 | 3179 |
#else |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3180 |
# 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
|
3181 |
# define IBLOCK_ARG (__BlockInstPtr(aBlock)->b_home) |
400 | 3182 |
#endif |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
3183 |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3184 |
/* |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3185 |
* 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
|
3186 |
* 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
|
3187 |
*/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3188 |
while (tmp <= final) { |
2254 | 3189 |
REGISTER OBJFUNC codeVal; |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3190 |
|
2254 | 3191 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2216 | 3192 |
/* |
3193 |
* arg is a compiled block with code - |
|
3194 |
* directly call it without going through Block>>value |
|
3195 |
*/ |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3196 |
|
4167 | 3197 |
/* stay here, while no interrupts are pending ... */ |
3198 |
do { |
|
8913 | 3199 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
4167 | 3200 |
if (InterruptPending != nil) goto outerLoop; |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3201 |
tmp++; |
4167 | 3202 |
} while (tmp <= final); |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3203 |
RETURN (self); |
2216 | 3204 |
} else { |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3205 |
if (InterruptPending != nil) __interruptL(@line); |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3206 |
|
2216 | 3207 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
3208 |
/* |
|
3209 |
* arg is a compiled block with bytecode - |
|
3210 |
* directly call interpreter without going through Block>>value |
|
3211 |
*/ |
|
400 | 3212 |
#ifdef PASS_ARG_POINTER |
2216 | 3213 |
{ |
3214 |
OBJ idx; |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3215 |
|
8913 | 3216 |
idx = __mkSmallInteger(tmp); |
2216 | 3217 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
3218 |
} |
|
400 | 3219 |
#else |
8913 | 3220 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
400 | 3221 |
#endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3222 |
|
2216 | 3223 |
} else { |
3224 |
/* |
|
3225 |
* arg is something else - call it with #value: |
|
3226 |
*/ |
|
8913 | 3227 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
2216 | 3228 |
} |
3229 |
} |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3230 |
outerLoop: ; |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3231 |
tmp++; |
214 | 3232 |
} |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3233 |
|
2216 | 3234 |
# undef BLOCK_ARG |
3235 |
# undef IBLOCK_ARG |
|
1 | 3236 |
|
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3237 |
RETURN (self); |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3238 |
} |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3239 |
/* |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3240 |
* arg is something else - call it with #value: |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3241 |
*/ |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3242 |
while (tmp <= final) { |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3243 |
if (InterruptPending != nil) __interruptL(@line); |
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3244 |
|
8908 | 3245 |
(*blockVal.ilc_func)(aBlock, |
3246 |
@symbol(value:), |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3247 |
nil, &blockVal, |
8913 | 3248 |
__mkSmallInteger(tmp)); |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3249 |
tmp++; |
214 | 3250 |
} |
3251 |
RETURN ( self ); |
|
1 | 3252 |
} |
400 | 3253 |
%}. |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3254 |
|
400 | 3255 |
"/ |
3256 |
"/ 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
|
3257 |
"/ pass on to super ... |
400 | 3258 |
"/ |
62 | 3259 |
^ super to:stop do:aBlock |
3260 |
||
216 | 3261 |
" |
3262 |
1 to:10 do:[:i | i printNewline] |
|
3263 |
" |
|
701 | 3264 |
! ! |
1 | 3265 |
|
701 | 3266 |
!SmallInteger methodsFor:'misc math'! |
1 | 3267 |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3268 |
bernoulli |
8908 | 3269 |
"returns the nth Bernoulli number. |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3270 |
The series runs this: |
8908 | 3271 |
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
|
3272 |
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
|
3273 |
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
|
3274 |
Used with taylor series for tan" |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3275 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3276 |
|table p| |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3277 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3278 |
table := #( |
8908 | 3279 |
(1 6) |
3280 |
(-1 30) |
|
3281 |
(1 42) |
|
3282 |
(-1 30) |
|
3283 |
(5 66) |
|
3284 |
(-691 2730) |
|
3285 |
(7 6) |
|
3286 |
(-3617 510) |
|
3287 |
(43867 798) |
|
3288 |
(-174611 330) |
|
3289 |
(854513 138) |
|
3290 |
(-236364091 2730) |
|
3291 |
(8553103 6) |
|
3292 |
(-23749461029 870) |
|
3293 |
(8615841276005 14322) |
|
3294 |
(-7709321041217 510) |
|
3295 |
(2577687858367 6) |
|
3296 |
(-26315271553053477373 1919190) |
|
3297 |
(2929993913841559 6) |
|
3298 |
(-261082718496449122051 13530) |
|
3299 |
). |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3300 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3301 |
self even ifTrue:[ |
8908 | 3302 |
self == 0 ifTrue:[^1]. |
3303 |
p := table at:(self / 2). |
|
3304 |
^ 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
|
3305 |
]. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3306 |
self == 1 ifTrue:[ ^ (1 / 2) ]. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3307 |
^ 0. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3308 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3309 |
" |
8908 | 3310 |
0 bernoulli |
3311 |
1 bernoulli |
|
3312 |
2 bernoulli |
|
3313 |
3 bernoulli |
|
3314 |
4 bernoulli |
|
3315 |
5 bernoulli |
|
3316 |
6 bernoulli |
|
3317 |
8 bernoulli |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3318 |
38 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3319 |
40 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3320 |
41 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3321 |
42 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3322 |
" |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3323 |
! |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3324 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3325 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3326 |
"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
|
3327 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3328 |
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
|
3329 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3330 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3331 |
%{ /* NOCONTEXT */ |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3332 |
INT val, div, mod, mySelf; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3333 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
3334 |
if (__isSmallInteger(aNumber) && |
4167 | 3335 |
(val = __intVal(aNumber)) > 0) { |
3336 |
mySelf = __intVal(self); |
|
3337 |
div = mySelf / val; |
|
3338 |
mod = mySelf % val; |
|
3339 |
||
8913 | 3340 |
RETURN (__ARRAY_WITH2( __mkSmallInteger(div), |
3341 |
__mkSmallInteger(mod))); |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3342 |
} |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3343 |
%}. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3344 |
^ super divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3345 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3346 |
" |
8908 | 3347 |
10 // 3 |
3348 |
10 \\ 3 |
|
3349 |
10 divMod:3 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3350 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3351 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
3352 |
|
701 | 3353 |
gcd:anInteger |
3354 |
"return the greatest common divisor (Euclid's algorithm). |
|
3355 |
This has been redefined here for more speed since due to the |
|
3356 |
use of gcd in Fraction code, it has become time-critical for |
|
3357 |
some code. (thanx to MessageTally)" |
|
3358 |
||
3359 |
%{ /* NOCONTEXT */ |
|
1 | 3360 |
|
701 | 3361 |
if (__isSmallInteger(anInteger)) { |
4167 | 3362 |
INT orgArg, ttt, selfInt, orgSelfInt, temp; |
3363 |
||
3364 |
ttt = orgArg = __intVal(anInteger); |
|
3365 |
if (ttt) { |
|
3366 |
selfInt = orgSelfInt = __intVal(self); |
|
3367 |
while (ttt != 0) { |
|
3368 |
temp = selfInt % ttt; |
|
3369 |
selfInt = ttt; |
|
3370 |
ttt = temp; |
|
3371 |
} |
|
3372 |
/* |
|
3373 |
* since its not defined in C, what the sign of |
|
3374 |
* a modulus result is when the arg is negative, |
|
3375 |
* change it explicitely here ... |
|
3376 |
*/ |
|
3377 |
if (orgArg < 0) { |
|
3378 |
/* result should be negative */ |
|
3379 |
if (orgSelfInt > 0) selfInt = -selfInt; |
|
3380 |
} else { |
|
3381 |
/* result should be positive */ |
|
3382 |
if (orgSelfInt < 0) selfInt = -selfInt; |
|
3383 |
} |
|
8913 | 3384 |
RETURN ( __mkSmallInteger(selfInt) ); |
4167 | 3385 |
} |
1 | 3386 |
} |
3387 |
%} |
|
3388 |
. |
|
701 | 3389 |
^ super gcd:anInteger |
3390 |
! |
|
62 | 3391 |
|
2430
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3392 |
gcd_helper:anInteger |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3393 |
"same as gcd - see knuth & Integer>>gcd:" |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3394 |
|
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3395 |
^ self gcd:anInteger |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3396 |
|
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3397 |
"Created: 1.3.1997 / 16:58:01 / cg" |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3398 |
! |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
3399 |
|
701 | 3400 |
intlog10 |
14492 | 3401 |
"return the truncation of log10 of the receiver. |
3402 |
The same as (self log:10) floor. |
|
3403 |
Stupid implementation, which is used to find out the number of digits needed |
|
701 | 3404 |
to print a number/and for conversion to a LargeInteger. |
7356 | 3405 |
Implemented that way, to allow for tiny systems (PDAs) without a Float class |
701 | 3406 |
(i.e. without log)." |
2 | 3407 |
|
15600 | 3408 |
self > 0 ifTrue:[ |
3409 |
self < 10000 ifTrue:[ |
|
3410 |
self < 10 ifTrue:[^ 0]. |
|
3411 |
self < 100 ifTrue:[^ 1]. |
|
3412 |
self < 1000 ifTrue:[^ 2]. |
|
3413 |
^ 3 |
|
3414 |
]. |
|
3415 |
self < 100000000 ifTrue:[ |
|
3416 |
self < 100000 ifTrue:[^ 4]. |
|
3417 |
self < 1000000 ifTrue:[^ 5]. |
|
3418 |
self < 10000000 ifTrue:[^ 6]. |
|
3419 |
^ 7 |
|
3420 |
]. |
|
3421 |
self < 1000000000 ifTrue:[^ 8]. |
|
3422 |
SmallInteger maxBytes == 4 ifTrue:[ |
|
16039 | 3423 |
"/ on a 32 bit machine, SmallInt cannot be larger |
15600 | 3424 |
^ 9 |
16039 | 3425 |
]. |
3426 |
||
3427 |
"/ 64 bit machine |
|
3428 |
self < 100000000000000 ifTrue:[ |
|
15600 | 3429 |
self < 10000000000 ifTrue:[^ 9]. |
3430 |
self < 100000000000 ifTrue:[^ 10]. |
|
3431 |
self < 1000000000000 ifTrue:[^ 11]. |
|
3432 |
self < 10000000000000 ifTrue:[^ 12]. |
|
16039 | 3433 |
^ 13 |
3434 |
]. |
|
3435 |
self < 1000000000000000 ifTrue:[^ 14]. |
|
3436 |
self < 10000000000000000 ifTrue:[^ 15]. |
|
3437 |
self < 100000000000000000 ifTrue:[^ 16]. |
|
3438 |
self < 1000000000000000000 ifTrue:[^ 17]. |
|
3439 |
^ 18. |
|
14492 | 3440 |
]. |
16039 | 3441 |
|
15600 | 3442 |
^ self class |
3443 |
raise:#domainErrorSignal |
|
3444 |
receiver:self |
|
3445 |
selector:#intlog10 |
|
3446 |
arguments:#() |
|
3447 |
errorString:'logarithm of negative integer' |
|
3448 |
||
8908 | 3449 |
" |
15600 | 3450 |
99 intlog10 |
3451 |
100 intlog10 |
|
14632 | 3452 |
101 intlog10 |
3453 |
(101 log:10) floor |
|
8908 | 3454 |
120 intlog10 |
6344 | 3455 |
-1 intlog10 |
3456 |
" |
|
2 | 3457 |
! ! |
3458 |
||
1 | 3459 |
!SmallInteger methodsFor:'printing & storing'! |
3460 |
||
16039 | 3461 |
asBCD |
3462 |
"return an integer which represents the BCD encoded value of the receiver; |
|
3463 |
that is: each digit of its decimal representation is placed into a nibble |
|
3464 |
of the result. (aka 162 -> 0x162). |
|
3465 |
This conversion is useful for some communication protocols, |
|
16041 | 3466 |
or control systems, which represent numbers this way..." |
16039 | 3467 |
|
3468 |
%{ /* NOCONTEXT */ |
|
3469 |
int i; |
|
3470 |
INT _10000000s = 0, _1000000s = 0; |
|
3471 |
INT _100000s = 0, _10000s = 0, _1000s = 0; |
|
3472 |
INT _100s = 0, _10s = 0, _1s = 0; |
|
3473 |
INT b = __intVal(self); |
|
3474 |
unsigned INT rslt; |
|
3475 |
||
3476 |
if (b <= 99999999) { |
|
3477 |
if (b <= 255) { |
|
3478 |
// the most common case: convert bytes |
|
3479 |
for (i=7; i>=0; i--) { |
|
3480 |
if (_100s >= 5) _100s += 3; |
|
3481 |
if (_10s >= 5) _10s += 3; |
|
3482 |
if (_1s >= 5) _1s += 3; |
|
3483 |
||
3484 |
_100s = (_100s<<1) | (_10s >> 3 & 1); _100s &= 0xF; |
|
3485 |
_10s = (_10s<<1) | (_1s >> 3 & 1); _10s &= 0xF; |
|
3486 |
_1s = (_1s<<1) | (b >> 7 & 1); _1s &= 0xF; |
|
3487 |
b <<= 1; |
|
3488 |
} |
|
3489 |
rslt = (_100s<<8) | (_10s<<4) | _1s; |
|
3490 |
RETURN (__MKSMALLINT( rslt) ); |
|
3491 |
} |
|
3492 |
||
3493 |
for (i=26; i>=0; i--) { |
|
3494 |
if (_10000000s >= 5) _10000000s += 3; |
|
3495 |
if (_1000000s >= 5) _1000000s += 3; |
|
3496 |
if (_100000s >= 5) _100000s += 3; |
|
3497 |
if (_10000s >= 5) _10000s += 3; |
|
3498 |
if (_1000s >= 5) _1000s += 3; |
|
3499 |
if (_100s >= 5) _100s += 3; |
|
3500 |
if (_10s >= 5) _10s += 3; |
|
3501 |
if (_1s >= 5) _1s += 3; |
|
3502 |
||
3503 |
_10000000s = (_10000000s<<1) | (_1000000s >> 3 & 1); _10000000s &= 0xF; |
|
3504 |
_1000000s = (_1000000s<<1) | (_100000s >> 3 & 1); _1000000s &= 0xF; |
|
3505 |
_100000s = (_100000s<<1) | (_10000s >> 3 & 1); _100000s &= 0xF; |
|
3506 |
_10000s = (_10000s<<1) | (_1000s >> 3 & 1); _10000s &= 0xF; |
|
3507 |
_1000s = (_1000s<<1) | (_100s >> 3 & 1); _1000s &= 0xF; |
|
3508 |
_100s = (_100s<<1) | (_10s >> 3 & 1); _100s &= 0xF; |
|
3509 |
_10s = (_10s<<1) | (_1s >> 3 & 1); _10s &= 0xF; |
|
3510 |
_1s = (_1s<<1) | (b >> 26 & 1); _1s &= 0xF; |
|
3511 |
b <<= 1; |
|
3512 |
} |
|
3513 |
||
3514 |
rslt = (_10000000s<<28) |
|
3515 |
| (_1000000s<<24) | (_100000s<<20) | (_10000s<<16) |
|
3516 |
| (_1000s<<12) | (_100s<<8) | (_10s<<4) | _1s; |
|
3517 |
RETURN (__MKUINT( rslt) ); |
|
3518 |
} |
|
3519 |
%}. |
|
3520 |
^ super asBCD. |
|
3521 |
||
3522 |
" |
|
3523 |
99999999 asBCD hexPrintString |
|
3524 |
12812345 asBCD hexPrintString |
|
3525 |
128123 asBCD hexPrintString |
|
3526 |
128901 asBCD hexPrintString |
|
3527 |
12890 asBCD hexPrintString |
|
3528 |
1289 asBCD hexPrintString |
|
3529 |
999 asBCD hexPrintString |
|
3530 |
256 asBCD hexPrintString |
|
3531 |
255 asBCD hexPrintString |
|
3532 |
128 asBCD hexPrintString |
|
3533 |
162 asBCD hexPrintString |
|
3534 |
||
3535 |
999999999 asBCD hexPrintString |
|
3536 |
128123456 asBCD hexPrintString |
|
3537 |
||
3538 |
" |
|
3539 |
! |
|
3540 |
||
62 | 3541 |
printOn:aStream |
68 | 3542 |
"append my printstring (base 10) to aStream." |
62 | 3543 |
|
3544 |
aStream nextPutAll:(self printString) |
|
3545 |
! |
|
3546 |
||
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
3547 |
printOn:aStream base:base showRadix:showRadix |
5988 | 3548 |
"append a string representation of the receiver in the specified numberBase to aStream |
3549 |
(if showRadix is true, with initial XXr) |
|
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
3550 |
The base argument should be between 2 and 36." |
6144 | 3551 |
|
5988 | 3552 |
showRadix ifTrue:[ |
14632 | 3553 |
base printOn:aStream. |
3554 |
aStream nextPut:$r. |
|
5988 | 3555 |
]. |
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
3556 |
|
14632 | 3557 |
(base isInteger and:[ base between:2 and:36 ]) ifTrue:[ |
3558 |
aStream nextPutAll:(self printStringRadix:base) |
|
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
3559 |
] ifFalse:[ |
14632 | 3560 |
super printOn:aStream base:base showRadix:false. |
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
3561 |
]. |
5988 | 3562 |
|
12977
fbf5437b15fb
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12876
diff
changeset
|
3563 |
"Created: / 07-09-2001 / 13:54:40 / cg" |
fbf5437b15fb
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12876
diff
changeset
|
3564 |
"Modified: / 02-08-2010 / 12:25:20 / cg" |
701 | 3565 |
! |
3566 |
||
1 | 3567 |
printString |
3568 |
"return my printstring (base 10)" |
|
3569 |
||
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3570 |
"since this was heavily used in some applications, |
68 | 3571 |
here is an exception to the rule of basing printString |
3572 |
upon the printOn: method." |
|
3573 |
||
1 | 3574 |
%{ /* NOCONTEXT */ |
15259 | 3575 |
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
|
3576 |
char *cp; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3577 |
OBJ newString = nil; |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3578 |
INT myValue; |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3579 |
int negative = 0; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3580 |
int len; |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3581 |
|
15259 | 3582 |
if (self == __MKSMALLINT(0)) { |
3583 |
RETURN (@global(ZeroString)); |
|
3584 |
// RETURN (__MKSTRING_L("0", 1)); |
|
3585 |
} |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3586 |
myValue = __intVal(self); |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3587 |
#ifdef SLOW_CODE |
8908 | 3588 |
/* |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3589 |
* 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
|
3590 |
* (printf is soooo slow) |
369 | 3591 |
*/ |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3592 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3593 |
/* |
15259 | 3594 |
* PROTECT_REGISTERS: |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3595 |
* 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
|
3596 |
* 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
|
3597 |
* 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
|
3598 |
*/ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3599 |
__BEGIN_PROTECT_REGISTERS__ |
15259 | 3600 |
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
|
3601 |
__END_PROTECT_REGISTERS__ |
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3602 |
|
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3603 |
if (len >= 0 && len <= sizeof(buffer)) { |
15259 | 3604 |
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
|
3605 |
} |
2781 | 3606 |
#else |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3607 |
if (myValue < 0) { |
15259 | 3608 |
negative = 1; |
3609 |
myValue = -myValue; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3610 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3611 |
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
|
3612 |
*cp-- = '\0'; |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3613 |
while (myValue != 0) { |
15259 | 3614 |
*cp = '0' + (myValue % 10); |
3615 |
myValue = myValue / 10; |
|
3616 |
cp--; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3617 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3618 |
if (negative) { |
15259 | 3619 |
*cp-- = '-'; |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3620 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3621 |
newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp)); |
2781 | 3622 |
#endif |
282 | 3623 |
if (newString != nil) { |
15259 | 3624 |
RETURN (newString); |
282 | 3625 |
} |
3626 |
%}. |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3627 |
"/ only arrive here, |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3628 |
"/ when having memory problems (i.e. no space for string) ... |
282 | 3629 |
^ super printString |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3630 |
|
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
3631 |
" |
8908 | 3632 |
1234 printString |
3633 |
0 printString |
|
3634 |
-100 printString |
|
15259 | 3635 |
|
3636 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 123456789012 printString ]] 180 180 180 170 180 |
|
3637 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 12345678 printString ]] 140 150 140 150 140 |
|
3638 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 1234 printString ]] 130 140 130 130 130 |
|
3639 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 12 printString ]] 130 120 120 120 110 |
|
3640 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 5 printString ]] 110 110 100 110 90 |
|
3641 |
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
|
3642 |
" |
1 | 3643 |
! |
3644 |
||
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3645 |
printStringRadix:base |
62 | 3646 |
"return my printstring (optimized for bases 16, 10 and 8)" |
1 | 3647 |
|
3705 | 3648 |
|s| |
3649 |
||
3650 |
%{ |
|
15259 | 3651 |
char buffer[64+3]; /* for 64bit machines, base 2, plus sign, plus 0-byte */ |
3705 | 3652 |
char *cp; |
1 | 3653 |
OBJ newString; |
3705 | 3654 |
INT myValue; |
3655 |
int negative = 0; |
|
3706 | 3656 |
INT __base; |
1 | 3657 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3658 |
if (__isSmallInteger(base)) { |
15259 | 3659 |
if (self == __MKSMALLINT(0)) { |
3660 |
RETURN (__MKSTRING_L("0", 1)); |
|
3661 |
} |
|
3662 |
myValue = __intVal(self); |
|
3663 |
__base = __intVal(base); |
|
3706 | 3664 |
|
3705 | 3665 |
#ifdef SLOW_CODE |
15259 | 3666 |
/* disabled, because printf is slower than the code below */ |
3667 |
||
3668 |
switch (__base) { |
|
3669 |
case 10: |
|
3670 |
format = "%"_ld_""; |
|
3671 |
break; |
|
3672 |
case 16: |
|
3673 |
format = "%"_lx_""; |
|
3674 |
break; |
|
3675 |
case 8: |
|
3676 |
format = "%"_lo_""; |
|
3677 |
break; |
|
3678 |
} |
|
3679 |
||
3680 |
if (format) { |
|
3681 |
/* |
|
3682 |
* actually only needed on sparc: since thisContext is |
|
3683 |
* in a global register, which gets destroyed by printf, |
|
3684 |
* manually save it here - very stupid ... |
|
3685 |
*/ |
|
3686 |
__BEGIN_PROTECT_REGISTERS__ |
|
3687 |
||
3688 |
len = snprintf(buffer, sizeof(buffer), format, (long)myValue); |
|
3689 |
||
3690 |
__END_PROTECT_REGISTERS__ |
|
3691 |
||
3692 |
if (len > 0 && len <= sizeof(buffer)) { |
|
3693 |
newString = __MKSTRING_L(buffer, len); |
|
3694 |
if (newString != nil) { |
|
3695 |
RETURN (newString); |
|
3696 |
} |
|
3697 |
} |
|
3698 |
} |
|
3705 | 3699 |
#else |
15259 | 3700 |
if ((__base <= 36) && (__base > 1)) { |
3701 |
if (myValue < 0) { |
|
3702 |
negative = 1; |
|
3703 |
myValue = -myValue; |
|
3704 |
} |
|
3705 |
cp = buffer + sizeof(buffer) - 1; |
|
3706 |
*cp-- = '\0'; |
|
3707 |
while (myValue != 0) { |
|
3708 |
int digit; |
|
3709 |
||
3710 |
digit = myValue % __base; |
|
3711 |
if (digit <= 9) { |
|
3712 |
*cp = '0' + digit; |
|
3713 |
} else { |
|
3714 |
*cp = 'A' + digit - 10; |
|
3715 |
} |
|
3716 |
myValue = myValue / __base; |
|
3717 |
cp--; |
|
3718 |
} |
|
3719 |
if (negative) { |
|
3720 |
*cp-- = '-'; |
|
3721 |
} |
|
3722 |
newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp)); |
|
3723 |
if (newString != nil) { |
|
3724 |
RETURN (newString); |
|
3725 |
} |
|
3726 |
} |
|
3705 | 3727 |
#endif |
1 | 3728 |
} |
314 | 3729 |
%}. |
3705 | 3730 |
"/ arrive here, for bad base, |
3731 |
"/ or when having memory problems (i.e. no space for string) ... |
|
3732 |
||
3733 |
"/ |
|
3734 |
"/ fall back for seldom used bases |
|
3735 |
"/ Notice: cannt use super>>printStringRadix: here, |
|
3736 |
"/ since that would lead to endless recursion ... |
|
3737 |
"/ (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
|
3738 |
|
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3739 |
s := WriteStream on:(String new:10). |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3740 |
super printOn:s base:base. |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3741 |
^ s contents. |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3742 |
|
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3743 |
" |
8908 | 3744 |
127 printStringRadix:16 |
3745 |
123 printStringRadix:12 |
|
3746 |
123 printStringRadix:10 |
|
3747 |
123 printStringRadix:8 |
|
3748 |
123 printStringRadix:3 |
|
3749 |
123 printStringRadix:2 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3750 |
123 printStringRadix:1 |
3705 | 3751 |
|
8908 | 3752 |
-127 printStringRadix:16 |
3753 |
-123 printStringRadix:12 |
|
3754 |
-123 printStringRadix:10 |
|
3755 |
-123 printStringRadix:8 |
|
3756 |
-123 printStringRadix:3 |
|
3757 |
-123 printStringRadix:2 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
3758 |
" |
1 | 3759 |
! |
3760 |
||
3761 |
printfPrintString:formatString |
|
7746 | 3762 |
"non-standard, but sometimes useful. |
1 | 3763 |
return a printed representation of the receiver |
15259 | 3764 |
as specified by formatString, which is defined by the C-function 'printf'. |
8908 | 3765 |
No checking for string overrun - the resulting string |
62 | 3766 |
must be shorter than 256 chars or else ... |
2781 | 3767 |
This method is NONSTANDARD and may be removed without notice; |
15259 | 3768 |
it is provided to allow special conversions in very special situations. |
2781 | 3769 |
Notice that a conversion may not be portable; for example, |
3770 |
to correctly convert an int on a 64-bit alpha, a %ld is required, |
|
14632 | 3771 |
on 64bit mingw or visualc, %lld is required, |
3772 |
while other systems may be happy with a %d. |
|
15259 | 3773 |
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
|
3774 |
Use at your own risk (if at all). |
15259 | 3775 |
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
|
3776 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
1 | 3777 |
|
13 | 3778 |
%{ /* STACK: 400 */ |
1 | 3779 |
char buffer[256]; |
314 | 3780 |
OBJ s; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3781 |
int len; |
1 | 3782 |
|
12482
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
3783 |
if (__isStringLike(formatString)) { |
15259 | 3784 |
/* |
3785 |
* actually only needed on sparc: since thisContext is |
|
3786 |
* in a global register, which gets destroyed by printf, |
|
3787 |
* manually save it here - very stupid ... |
|
3788 |
*/ |
|
3789 |
__BEGIN_PROTECT_REGISTERS__ |
|
3790 |
||
3791 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __intVal(self)); |
|
3792 |
||
3793 |
__END_PROTECT_REGISTERS__ |
|
3794 |
||
3795 |
if (len < 0) goto fail; |
|
3796 |
||
3797 |
s = __MKSTRING_L(buffer, len); |
|
3798 |
if (s != nil) { |
|
3799 |
RETURN (s); |
|
3800 |
} |
|
1 | 3801 |
} |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3802 |
fail: ; |
314 | 3803 |
%}. |
1 | 3804 |
self primitiveFailed |
3805 |
||
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3806 |
" |
15259 | 3807 |
123 printfPrintString:'%%d -> %d' |
3808 |
123 printfPrintString:'%%6d -> %6d' |
|
3809 |
123 printfPrintString:'%%x -> %x' |
|
3810 |
123 printfPrintString:'%%4x -> %4x' |
|
3811 |
123 printfPrintString:'%%04x -> %04x' |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
3812 |
" |
1 | 3813 |
! ! |
701 | 3814 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3815 |
!SmallInteger methodsFor:'private'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3816 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3817 |
sign:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3818 |
"private: for protocol completeness with LargeIntegers" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3819 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3820 |
|absVal| |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3821 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3822 |
absVal := self abs. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3823 |
aNumber < 0 ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3824 |
^ absVal negated |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3825 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3826 |
aNumber == 0 ifTrue:[^ 0]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3827 |
^ absVal |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3828 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3829 |
" |
8908 | 3830 |
-4 sign:-1 |
3831 |
-4 sign:0 |
|
3832 |
-4 sign:1 |
|
3833 |
-4 sign:-1 |
|
3834 |
-4 sign:0 |
|
3835 |
-4 sign:1 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3836 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3837 |
! ! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
3838 |
|
16294 | 3839 |
!SmallInteger methodsFor:'special modulo arithmetic'! |
3840 |
||
3841 |
plus32:aNumber |
|
3842 |
"return the sum of the receiver and the argument, as SmallInteger. |
|
3843 |
The argument must be another SmallInteger. |
|
3844 |
If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned. |
|
3845 |
This is of course not always correct, but allows for C/Java behavior to be emulated." |
|
3846 |
||
3847 |
%{ /* NOCONTEXT */ |
|
3848 |
INT sum; |
|
3849 |
||
3850 |
sum = __unsignedLongIntVal(self) + __unsignedLongIntVal(aNumber); |
|
3851 |
#if __POINTER_SIZE__ == 8 |
|
3852 |
sum &= 0xFFFFFFFFL; |
|
3853 |
#endif |
|
3854 |
RETURN ( __MKUINT(sum)); |
|
3855 |
%}. |
|
3856 |
self primitiveFailed |
|
3857 |
||
3858 |
" |
|
3859 |
16r7FFFFFFF + 1 2147483648 |
|
3860 |
16r7FFFFFFF plus32: 1 |
|
3861 |
" |
|
3862 |
! |
|
3863 |
||
3864 |
plus:aNumber |
|
3865 |
"return the sum of the receiver and the argument, as SmallInteger. |
|
3866 |
The argument must be another SmallInteger. |
|
3867 |
If the result overflows the smallInteger range, the value modulo the |
|
3868 |
smallInteger range is returned (i.e. the low bits of the sum). |
|
3869 |
This is of course not always correct, but some code does a modulo anyway |
|
3870 |
and can therefore speed things up by not going through LargeIntegers." |
|
3871 |
||
3872 |
%{ /* NOCONTEXT */ |
|
3873 |
||
3874 |
if (__isSmallInteger(aNumber)) { |
|
3875 |
INT sum; |
|
3876 |
||
3877 |
sum = __intVal(self) + __intVal(aNumber); |
|
3878 |
if (!__ISVALIDINTEGER(sum)) { |
|
3879 |
/* keep the sign */ |
|
3880 |
sum %= _MAX_INT; |
|
3881 |
} |
|
3882 |
RETURN ( __mkSmallInteger(sum)); |
|
3883 |
} |
|
3884 |
%}. |
|
3885 |
self primitiveFailed |
|
3886 |
||
3887 |
" |
|
3888 |
5 plus:-1 |
|
3889 |
5 plus:1 |
|
3890 |
1 plus:-5 |
|
3891 |
self maxVal plus:1 |
|
3892 |
self maxVal + 1 |
|
3893 |
" |
|
3894 |
! |
|
3895 |
||
3896 |
subtract:aNumber |
|
3897 |
"return the difference of the receiver and the argument, as SmallInteger. |
|
3898 |
The argument must be another SmallInteger. |
|
3899 |
If the result overflows the smallInteger range, the value modulo the |
|
3900 |
smallInteger range is returned (i.e. the low bits of the sum). |
|
3901 |
This is of course not always correct, but some code does a modulo anyway |
|
3902 |
and can therefore speed things up by not going through LargeIntegers." |
|
3903 |
||
3904 |
%{ /* NOCONTEXT */ |
|
3905 |
||
3906 |
if (__isSmallInteger(aNumber)) { |
|
3907 |
INT diff; |
|
3908 |
||
3909 |
diff = __intVal(self) - __intVal(aNumber); |
|
3910 |
if (!__ISVALIDINTEGER(diff)) { |
|
3911 |
/* keep the sign */ |
|
3912 |
diff %= _MAX_INT; |
|
3913 |
} |
|
3914 |
RETURN ( __mkSmallInteger(diff)); |
|
3915 |
} |
|
3916 |
%}. |
|
3917 |
self primitiveFailed |
|
3918 |
||
3919 |
" |
|
3920 |
-1 subtract:5 |
|
3921 |
5 subtract:1 |
|
3922 |
1 subtract:-5 |
|
3923 |
self minVal subtract:1 |
|
3924 |
self minVal - 1 |
|
3925 |
" |
|
3926 |
! |
|
3927 |
||
3928 |
times:aNumber |
|
3929 |
"return the product of the receiver and the argument, as SmallInteger. |
|
3930 |
The argument must be another SmallInteger. |
|
3931 |
If the result overflows the smallInteger range, the value modulo the |
|
3932 |
smallInteger range is returned (i.e. the low bits of the product). |
|
3933 |
This is of course not always correct, but some code does a modulo anyway |
|
3934 |
and can therefore speed things up by not going through LargeIntegers." |
|
3935 |
||
3936 |
%{ /* NOCONTEXT */ |
|
3937 |
||
3938 |
INT myValue, otherValue; |
|
3939 |
unsigned INT productLow, productHi; |
|
3940 |
int negative; |
|
3941 |
||
3942 |
# define low16Bits(foo) ((foo) & 0xFFFF) |
|
3943 |
# define hi16Bits(foo) ((foo) >> 16) |
|
3944 |
# define low32Bits(foo) ((foo) & 0xFFFFFFFFL) |
|
3945 |
# define hi32Bits(foo) ((foo) >> 32) |
|
3946 |
||
3947 |
/* |
|
3948 |
* can we use long long arithmetic ? |
|
3949 |
* |
|
3950 |
* long-long arithmetic seems to be buggy with some systems |
|
3951 |
* (took me a while to find this out :-( |
|
3952 |
* (try 10000 * 10000) |
|
3953 |
*/ |
|
3954 |
#if defined(__sparc__) && defined(__GNUC__) && (__GNUC__ >= 2) |
|
3955 |
# define USE_LONGLONG_FOR_MUL |
|
3956 |
#endif |
|
3957 |
||
3958 |
#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2) |
|
3959 |
# define USE_LONGLONG_FOR_MUL |
|
3960 |
#endif |
|
3961 |
||
3962 |
if (__isSmallInteger(aNumber)) { |
|
3963 |
myValue = __intVal(self); |
|
3964 |
otherValue = __intVal(aNumber); |
|
3965 |
||
3966 |
#if defined(USE_LONGLONG_FOR_MUL) |
|
3967 |
{ |
|
3968 |
# if defined(__alpha__) && !defined(__alpha64__) |
|
3969 |
# define LONGLONG INT64 |
|
3970 |
# else |
|
3971 |
# define LONGLONG long long |
|
3972 |
# endif |
|
3973 |
LONGLONG product; |
|
3974 |
||
3975 |
product = (LONGLONG)myValue * (LONGLONG)otherValue; |
|
3976 |
if (product < 0) { |
|
3977 |
RETURN ( __mkSmallInteger(-(INT)(-product & _MAX_INT))); |
|
3978 |
} |
|
3979 |
RETURN ( __mkSmallInteger((INT)(product & _MAX_INT))); |
|
3980 |
} |
|
3981 |
#else /* no long-long */ |
|
3982 |
negative = 1; |
|
3983 |
if (myValue < 0) { |
|
3984 |
negative = -1; |
|
3985 |
myValue = -myValue; |
|
3986 |
} |
|
3987 |
if (otherValue < 0) { |
|
3988 |
negative = -negative; |
|
3989 |
otherValue = -otherValue; |
|
3990 |
} |
|
3991 |
||
3992 |
# if defined(__GNUC__) && defined(__mc68k__) |
|
3993 |
asm ("mulu%.l %3,%1:%0" |
|
3994 |
: "=d" ((unsigned long)(productLow)), |
|
3995 |
"=d" ((unsigned long)(productHi)) |
|
3996 |
: "%0" ((unsigned long)(myValue)), |
|
3997 |
"dmi" ((unsigned long)(otherValue))); |
|
3998 |
# else |
|
3999 |
# if defined (__GNUC__) && defined(__i386__) |
|
4000 |
asm ("mull %3" |
|
4001 |
: "=a" ((unsigned long)(productLow)), |
|
4002 |
"=d" ((unsigned long)(productHi)) |
|
4003 |
: "%0" ((unsigned long)(myValue)), |
|
4004 |
"rm" ((unsigned long)(otherValue))); |
|
4005 |
# else |
|
4006 |
# if defined(WIN32) && defined(__BORLANDC__) |
|
4007 |
asm { |
|
4008 |
mov eax, myValue |
|
4009 |
mov edx, otherValue |
|
4010 |
mul edx |
|
4011 |
mov productLow, eax |
|
4012 |
mov productHi, edx |
|
4013 |
} |
|
4014 |
# else /* generic */ |
|
4015 |
{ |
|
4016 |
unsigned INT pHH, pHL, pLH, pLL; |
|
4017 |
unsigned INT low1, low2, hi1, hi2; |
|
4018 |
unsigned INT t; |
|
4019 |
||
4020 |
/* unsigned multiply myValue * otherValue -> productHi, productLow |
|
4021 |
* |
|
4022 |
* this is too slow: |
|
4023 |
* since most machines can do 32*32 to 64 bit multiply, |
|
4024 |
* (or at least 32*32 with Overflow check) |
|
4025 |
* - need more assembler (inline) functions here |
|
4026 |
*/ |
|
4027 |
# if __POINTER_SIZE__ == 8 |
|
4028 |
low1 = low32Bits((unsigned INT)myValue); |
|
4029 |
hi1 = hi32Bits((unsigned INT)myValue); |
|
4030 |
low2 = low32Bits((unsigned INT)otherValue); |
|
4031 |
hi2 = hi32Bits((unsigned INT)otherValue); |
|
4032 |
# define LLMASK 0xC000000000000000L |
|
4033 |
# else |
|
4034 |
low1 = low16Bits((unsigned INT)myValue); |
|
4035 |
hi1 = hi16Bits((unsigned INT)myValue); |
|
4036 |
low2 = low16Bits((unsigned INT)otherValue); |
|
4037 |
hi2 = hi16Bits((unsigned INT)otherValue); |
|
4038 |
# define LLMASK 0xC0000000 |
|
4039 |
# endif |
|
4040 |
||
4041 |
pLH = low1 * hi2; |
|
4042 |
pHL = hi1 * low2; |
|
4043 |
pLL = low1 * low2; |
|
4044 |
pHH = hi1 * hi2; |
|
4045 |
||
4046 |
/* |
|
4047 |
* the common case ... |
|
4048 |
*/ |
|
4049 |
if ((pHL == 0) |
|
4050 |
&& (pLH == 0) |
|
4051 |
&& (pHH == 0) |
|
4052 |
&& ((pLL & LLMASK) == 0)) { |
|
4053 |
if (negative < 0) { |
|
4054 |
RETURN ( __mkSmallInteger(- ((INT)pLL)) ); |
|
4055 |
} |
|
4056 |
RETURN ( __mkSmallInteger((INT)pLL) ); |
|
4057 |
} |
|
4058 |
||
4059 |
/* |
|
4060 |
* pHH |--------|--------| |
|
4061 |
* pLH |--------|--------| |
|
4062 |
* pHL |--------|--------| |
|
4063 |
* pLL |--------|--------| |
|
4064 |
*/ |
|
4065 |
||
4066 |
# if __POINTER_SIZE__ == 8 |
|
4067 |
t = low32Bits(pLH) + low32Bits(pHL) + hi32Bits(pLL); |
|
4068 |
productLow = (t << 32) + low32Bits(pLL); |
|
4069 |
productHi = pHH + hi32Bits(t) + hi32Bits(pHL) + hi32Bits(pLH); |
|
4070 |
# else |
|
4071 |
t = low16Bits(pLH) + low16Bits(pHL) + hi16Bits(pLL); |
|
4072 |
productLow = (t << 16) + low16Bits(pLL); |
|
4073 |
productHi = pHH + hi16Bits(t) + hi16Bits(pHL) + hi16Bits(pLH); |
|
4074 |
# endif |
|
4075 |
} |
|
4076 |
# endif /* ! WIN32 */ |
|
4077 |
# endif /* ! (__GNUC__ && __i386__) */ |
|
4078 |
# endif /* ! (__GNUC__ && __mc68k__) */ |
|
4079 |
||
4080 |
if (negative < 0) { |
|
4081 |
RETURN ( __mkSmallInteger(-(INT)(productLow & _MAX_INT))); |
|
4082 |
} |
|
4083 |
RETURN ( __mkSmallInteger((INT)(productLow & _MAX_INT))); |
|
4084 |
#endif /* ! USE_LONGLONG */ |
|
4085 |
} |
|
4086 |
%}. |
|
4087 |
||
4088 |
self primitiveFailed |
|
4089 |
||
4090 |
" |
|
4091 |
5 times:-1 |
|
4092 |
5 times:1 |
|
4093 |
self maxVal-1 times:2 |
|
4094 |
self maxVal-1 times:-2 |
|
4095 |
self maxVal-1 * 2 bitAnd:16r3fffffff |
|
4096 |
" |
|
4097 |
! ! |
|
4098 |
||
4099 |
!SmallInteger methodsFor:'special modulo bit operators'! |
|
4100 |
||
4101 |
bitInvert32 |
|
4102 |
"return the value of the receiver with all bits inverted in 32bit signed int space |
|
4103 |
(changes the sign)" |
|
4104 |
||
4105 |
%{ /* NOCONTEXT */ |
|
4106 |
unsigned INT v; |
|
4107 |
||
4108 |
v = __intVal(self); |
|
4109 |
v = ~v; |
|
4110 |
#if __POINTER_SIZE__ == 8 |
|
4111 |
v &= 0xFFFFFFFFL; |
|
4112 |
#endif |
|
4113 |
RETURN ( __MKUINT(v) ); |
|
4114 |
%}. |
|
4115 |
^ self primitiveFailed |
|
4116 |
||
4117 |
" |
|
4118 |
1 bitInvert32 |
|
4119 |
16r40000000 bitInvert32 |
|
4120 |
16r80000000 bitInvert32 |
|
4121 |
" |
|
4122 |
! |
|
4123 |
||
4124 |
bitRotate32:shiftCount |
|
4125 |
"return the value of the receiver rotated by shiftCount bits, |
|
4126 |
but only within 32 bits, rotating left for positive, right for negative counts. |
|
4127 |
Rotates through the sign bit. |
|
4128 |
Useful for crypt algorithms, or to emulate C/Java semantics." |
|
4129 |
||
4130 |
%{ /* NOCONTEXT */ |
|
4131 |
||
4132 |
unsigned INT bits; |
|
4133 |
int count; |
|
4134 |
||
4135 |
if (__isSmallInteger(shiftCount)) { |
|
4136 |
count = __intVal(shiftCount); |
|
4137 |
count = count % 32; |
|
4138 |
||
4139 |
bits = __intVal(self); |
|
4140 |
if (count > 0) { |
|
4141 |
bits = (bits << count) | (bits >> (32-count)); |
|
4142 |
} else { |
|
4143 |
bits = (bits >> (-count)) | (bits << (32-(-count))); |
|
4144 |
} |
|
4145 |
#if __POINTER_SIZE__ == 8 |
|
4146 |
bits &= 0xFFFFFFFFL; |
|
4147 |
#endif |
|
4148 |
RETURN (__MKUINT(bits)); |
|
4149 |
} |
|
4150 |
%}. |
|
4151 |
^ self primitiveFailed |
|
4152 |
||
4153 |
" |
|
4154 |
128 rotate32:1 |
|
4155 |
||
4156 |
1 rotate32:1 |
|
4157 |
1 rotate32:2 |
|
4158 |
1 rotate32:31 |
|
4159 |
1 rotate32:32 |
|
4160 |
||
4161 |
1 rotate32:-1 |
|
4162 |
1 rotate32:-2 |
|
4163 |
1 rotate32:-3 |
|
4164 |
1 rotate32:-32 |
|
4165 |
" |
|
4166 |
! |
|
4167 |
||
4168 |
bitShift32:shiftCount |
|
4169 |
"return the value of the receiver shifted by shiftCount bits, |
|
4170 |
but only within 32 bits, shifting into/out-of the sign bit. |
|
4171 |
May be useful for communication interfaces, to create ST-numbers |
|
4172 |
from a signed 32bit int value given as individual bytes, |
|
4173 |
or to emulate C/Java semantics." |
|
4174 |
||
4175 |
%{ /* NOCONTEXT */ |
|
4176 |
||
4177 |
INT bits, count; |
|
4178 |
||
4179 |
if (__isSmallInteger(shiftCount)) { |
|
4180 |
count = __intVal(shiftCount); |
|
4181 |
if (count >= 32) { |
|
4182 |
RETURN (__mkSmallInteger(0)); |
|
4183 |
} |
|
4184 |
||
4185 |
bits = __intVal(self); |
|
4186 |
if (count > 0) { |
|
4187 |
bits = bits << count; |
|
4188 |
} else { |
|
4189 |
bits = bits >> (-count); |
|
4190 |
} |
|
4191 |
#if __POINTER_SIZE__ == 8 |
|
4192 |
bits &= 0xFFFFFFFFL; |
|
4193 |
#endif |
|
4194 |
RETURN (__MKINT(bits)); |
|
4195 |
} |
|
4196 |
%}. |
|
4197 |
^ self primitiveFailed |
|
4198 |
||
4199 |
" |
|
4200 |
128 bitShift:24 |
|
4201 |
128 bitShift32:24 |
|
4202 |
||
4203 |
1 bitShift:31 |
|
4204 |
1 bitShift32:31 |
|
4205 |
" |
|
4206 |
! |
|
4207 |
||
4208 |
bitXor32:aNumber |
|
4209 |
"return the xor of the receiver and the argument. |
|
4210 |
The argument must be another SmallInteger or a 4-byte LargeInteger. |
|
4211 |
If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned. |
|
4212 |
This is of course not always correct, but allows for C/Java behavior to be emulated." |
|
4213 |
||
4214 |
%{ /* NOCONTEXT */ |
|
4215 |
INT rslt; |
|
4216 |
||
4217 |
rslt = __unsignedLongIntVal(self) ^ __unsignedLongIntVal(aNumber); |
|
4218 |
#if __POINTER_SIZE__ == 8 |
|
4219 |
rslt &= 0xFFFFFFFFL; |
|
4220 |
#endif |
|
4221 |
RETURN ( __MKUINT(rslt)); |
|
4222 |
%}. |
|
4223 |
self primitiveFailed |
|
4224 |
||
4225 |
" |
|
4226 |
16r7FFFFFFF bitXor: 16r80000000 4294967295 |
|
4227 |
16r7FFFFFFF bitXor32: 16r80000000 |
|
4228 |
" |
|
4229 |
! |
|
4230 |
||
4231 |
unsignedBitShift32:shiftCount |
|
4232 |
"return the value of the receiver shifted by shiftCount bits, |
|
4233 |
but only within 32 unsigned bits. |
|
4234 |
May be useful for communication interfaces, to create ST-numbers |
|
4235 |
from an unsigned 32bit int value given as individual byte, |
|
4236 |
or to emulate C/Java semantics." |
|
4237 |
||
4238 |
%{ /* NOCONTEXT */ |
|
4239 |
||
4240 |
unsigned INT bits; |
|
4241 |
INT count; |
|
4242 |
||
4243 |
if (__isSmallInteger(shiftCount)) { |
|
4244 |
count = __intVal(shiftCount); |
|
4245 |
if (count >= 32) { |
|
4246 |
RETURN (__mkSmallInteger(0)); |
|
4247 |
} |
|
4248 |
||
4249 |
bits = __intVal(self); |
|
4250 |
if (count > 0) { |
|
4251 |
bits = bits << count; |
|
4252 |
} else { |
|
4253 |
bits = bits >> (-count); |
|
4254 |
} |
|
4255 |
#if __POINTER_SIZE__ == 8 |
|
4256 |
bits &= 0xFFFFFFFFL; |
|
4257 |
#endif |
|
4258 |
RETURN (__MKUINT(bits)); |
|
4259 |
} |
|
4260 |
%}. |
|
4261 |
^ self primitiveFailed |
|
4262 |
||
4263 |
" |
|
4264 |
128 unsignedBitShift:24 |
|
4265 |
128 unsignedBitShift32:24 |
|
4266 |
||
4267 |
1 unsignedBitShift:31 |
|
4268 |
1 unsignedBitShift32:31 |
|
4269 |
||
4270 |
-1 unsignedBitShift32:-1 |
|
4271 |
-1 unsignedBitShift32:1 |
|
4272 |
" |
|
4273 |
! ! |
|
4274 |
||
701 | 4275 |
!SmallInteger methodsFor:'testing'! |
4276 |
||
4277 |
between:min and:max |
|
4278 |
"return true if the receiver is less than or equal to the argument max |
|
4279 |
and greater than or equal to the argument min. |
|
4280 |
- reimplemented here for speed" |
|
4281 |
||
4282 |
%{ /* NOCONTEXT */ |
|
4283 |
||
4284 |
if (__bothSmallInteger(min, max)) { |
|
15432 | 4285 |
#if TAG_INT == 1 |
4286 |
// tag bit does not change the magnitude order |
|
4287 |
if ((INT)self < (INT)(min)) { |
|
4288 |
RETURN ( false ); |
|
4289 |
} |
|
4290 |
if ((INT)self > (INT)(max)) { |
|
4291 |
RETURN ( false ); |
|
4292 |
} |
|
4293 |
RETURN ( true ); |
|
4294 |
#else |
|
4295 |
REGISTER INT selfVal; |
|
4296 |
||
4297 |
selfVal = __intVal(self); |
|
4298 |
if (selfVal < __intVal(min)) { |
|
4299 |
RETURN ( false ); |
|
4300 |
} |
|
4301 |
if (selfVal > __intVal(max)) { |
|
4302 |
RETURN ( false ); |
|
4303 |
} |
|
4304 |
RETURN ( true ); |
|
4305 |
#endif |
|
701 | 4306 |
} |
2216 | 4307 |
%}. |
701 | 4308 |
(self < min) ifTrue:[^ false]. |
4309 |
(self > max) ifTrue:[^ false]. |
|
4310 |
^ true |
|
4311 |
! |
|
4312 |
||
4313 |
even |
|
4314 |
"return true, if the receiver is even" |
|
4315 |
||
4316 |
%{ /* NOCONTEXT */ |
|
4317 |
||
2216 | 4318 |
RETURN ( ((INT)self & (INT)__MASKSMALLINT(1)) ? false : true ); |
5412 | 4319 |
%}. |
4320 |
^ super even |
|
701 | 4321 |
! |
4322 |
||
13386 | 4323 |
isPowerOfTwo |
4324 |
"return true, if the receiver is a power of 2" |
|
4325 |
||
4326 |
"/ mhmh: how about the following |
|
4327 |
"/ self == 0 ifTrue:[^ false]. |
|
4328 |
||
4329 |
^ (self bitAnd:(self - 1)) == 0 |
|
4330 |
||
4331 |
" |
|
4332 |
0 isPowerOfTwo |
|
4333 |
1 isPowerOfTwo |
|
4334 |
2 isPowerOfTwo |
|
14632 | 4335 |
3 isPowerOfTwo |
4336 |
4 isPowerOfTwo |
|
4337 |
16r8000000000000000 isPowerOfTwo |
|
4338 |
16r8000000000000001 isPowerOfTwo |
|
13386 | 4339 |
|
4340 |
10000 factorial isPowerOfTwo |
|
14632 | 4341 |
|n| n := 10000 factorial. Time millisecondsToRun:[1000 timesRepeat:[ n isPowerOfTwo]] |
13386 | 4342 |
" |
4343 |
||
4344 |
"Modified: / 20-06-2011 / 12:41:18 / cg" |
|
4345 |
! |
|
4346 |
||
701 | 4347 |
negative |
4348 |
"return true, if the receiver is less than zero |
|
4349 |
reimplemented here for speed" |
|
4350 |
||
4351 |
%{ /* NOCONTEXT */ |
|
4352 |
||
2216 | 4353 |
#if TAG_INT == 1 |
701 | 4354 |
/* tag bit does not change sign */ |
4355 |
RETURN ( ((INT)(self) < 0) ? true : false ); |
|
2216 | 4356 |
#else |
4357 |
RETURN ( (__intVal(self) < 0) ? true : false ); |
|
701 | 4358 |
#endif |
5412 | 4359 |
%}. |
4360 |
^ self < 0 |
|
701 | 4361 |
! |
4362 |
||
14693 | 4363 |
nextPowerOf2 |
4364 |
"return the power of 2 at or above the receiver. |
|
4365 |
Useful for padding." |
|
4366 |
||
4367 |
%{ /* NOCONTEXT */ |
|
4368 |
INT x; |
|
4369 |
||
4370 |
x = __intVal(self) - 1; |
|
4371 |
x |= (x >> 1); |
|
4372 |
x |= (x >> 2); |
|
4373 |
x |= (x >> 4); |
|
4374 |
x |= (x >> 8); |
|
4375 |
x |= (x >> 16); |
|
4376 |
#if __POINTER_SIZE__ == 8 |
|
4377 |
x |= (x >> 32); |
|
4378 |
#endif |
|
4379 |
RETURN (__MKINT(x + 1)); |
|
4380 |
%} |
|
4381 |
" |
|
4382 |
1 nextPowerOf2 |
|
4383 |
2 nextPowerOf2 |
|
4384 |
3 nextPowerOf2 |
|
4385 |
4 nextPowerOf2 |
|
4386 |
5 nextPowerOf2 |
|
4387 |
6 nextPowerOf2 |
|
4388 |
7 nextPowerOf2 |
|
4389 |
8 nextPowerOf2 |
|
4390 |
||
4391 |
22 nextPowerOf2 |
|
16039 | 4392 |
10 factorial nextPowerOf2 |
4393 |
20 factorial nextPowerOf2 |
|
4394 |
100 factorial nextPowerOf2 |
|
14693 | 4395 |
" |
4396 |
! |
|
4397 |
||
701 | 4398 |
odd |
4399 |
"return true, if the receiver is odd" |
|
4400 |
||
4401 |
%{ /* NOCONTEXT */ |
|
4402 |
||
2216 | 4403 |
RETURN ( ((INT)self & (INT)__MASKSMALLINT(1)) ? true : false ); |
5412 | 4404 |
%}. |
4405 |
^ super odd |
|
4406 |
||
701 | 4407 |
! |
4408 |
||
13913 | 4409 |
parityOdd |
4410 |
"return true, if an odd number of bits are set in the receiver, false otherwise. |
|
4411 |
(i.e. true for odd parity) |
|
4412 |
Undefined for negative values (smalltalk does not require the machine to use 2's complement)" |
|
4413 |
||
4414 |
%{ /* NOCONTEXT */ |
|
4415 |
||
4416 |
// tricky, but very fast (google for it, to understand) |
|
4417 |
#if __POINTER_SIZE__ == 4 |
|
14632 | 4418 |
unsigned int v = __intVal(self); |
13913 | 4419 |
|
4420 |
v ^= v >> 16; |
|
4421 |
v ^= v >> 8; |
|
4422 |
v ^= v >> 4; |
|
4423 |
v &= 0xf; |
|
4424 |
RETURN ( ( (0x6996 >> v) & 1 ) ? true : false ); |
|
4425 |
#endif |
|
4426 |
%}. |
|
4427 |
^ super parityOdd |
|
4428 |
||
4429 |
" |
|
14632 | 4430 |
self assert: |
4431 |
(((0 to:255) collect:[:i | i parityOdd ifTrue:1 ifFalse:0]) |
|
4432 |
asByteArray collect:[:c | c + $0 asciiValue]) asString |
|
4433 |
= |
|
4434 |
'0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110100110010110100101100110100110010110011010010110100110010110011010011001011010010110011010010110100110010110100101100110100110010110011010010110100110010110' |
|
4435 |
||
4436 |
self assert:(16r0FFFFFFF parityOdd = 16r0FFFFFFF bitCount odd). |
|
4437 |
self assert:(16r1FFFFFFF parityOdd = 16r1FFFFFFF bitCount odd). |
|
4438 |
self assert:(16r3FFFFFFF parityOdd = 16r3FFFFFFF bitCount odd). |
|
4439 |
self assert:(16r7FFFFFFF parityOdd = 16r7FFFFFFF bitCount odd). |
|
4440 |
self assert:(16rFFFFFFFF parityOdd = 16rFFFFFFFF bitCount odd). |
|
13913 | 4441 |
" |
4442 |
||
4443 |
"Modified (comment): / 09-01-2012 / 19:55:37 / cg" |
|
4444 |
! |
|
4445 |
||
701 | 4446 |
positive |
4447 |
"return true, if the receiver is not negative |
|
4448 |
reimplemented here for speed" |
|
4449 |
||
4450 |
%{ /* NOCONTEXT */ |
|
4451 |
||
2216 | 4452 |
#if TAG_INT == 1 |
701 | 4453 |
/* tag bit does not change sign */ |
4454 |
RETURN ( ((INT)(self) >= 0) ? true : false ); |
|
2216 | 4455 |
#else |
4456 |
RETURN ( (__intVal(self) >= 0) ? true : false ); |
|
701 | 4457 |
#endif |
5412 | 4458 |
%}. |
4459 |
^ super positive |
|
4460 |
||
701 | 4461 |
! |
4462 |
||
4463 |
sign |
|
7471 | 4464 |
"return the sign of the receiver (-1, 0 or 1). |
701 | 4465 |
reimplemented here for speed" |
4466 |
||
4467 |
%{ /* NOCONTEXT */ |
|
4468 |
||
1133 | 4469 |
INT val = __intVal(self); |
701 | 4470 |
|
4471 |
if (val < 0) { |
|
8913 | 4472 |
RETURN ( __mkSmallInteger(-1) ); |
701 | 4473 |
} |
4474 |
if (val > 0) { |
|
8913 | 4475 |
RETURN ( __mkSmallInteger(1) ); |
701 | 4476 |
} |
8913 | 4477 |
RETURN ( __mkSmallInteger(0) ); |
5412 | 4478 |
%}. |
4479 |
^ super sign |
|
701 | 4480 |
! |
4481 |
||
4482 |
strictlyPositive |
|
4483 |
"return true, if the receiver is greater than zero |
|
4484 |
reimplemented here for speed" |
|
4485 |
||
4486 |
%{ /* NOCONTEXT */ |
|
4487 |
||
2216 | 4488 |
#if TAG_INT == 1 |
701 | 4489 |
/* tag bit does not change sign */ |
4490 |
RETURN ( ((INT)(self) > 0) ? true : false ); |
|
2216 | 4491 |
#else |
4492 |
RETURN ( (__intVal(self) > 0) ? true : false ); |
|
701 | 4493 |
#endif |
5412 | 4494 |
%}. |
4495 |
^ super strictlyPositive |
|
701 | 4496 |
! ! |
4497 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4498 |
!SmallInteger class methodsFor:'documentation'! |
701 | 4499 |
|
4500 |
version |
|
16885 | 4501 |
^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.222 2014-10-04 22:36:37 stefan Exp $' |
12482
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
4502 |
! |
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
4503 |
|
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
4504 |
version_CVS |
16885 | 4505 |
^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.222 2014-10-04 22:36:37 stefan Exp $' |
701 | 4506 |
! ! |
14755 | 4507 |
|
15259 | 4508 |
|
4509 |
SmallInteger initialize! |