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