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