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