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