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