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