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