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