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