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