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