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