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