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