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