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