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