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