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