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