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