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