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