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