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