author | Jan Vrany <jan.vrany@labware.com> |
Fri, 03 Dec 2021 12:26:39 +0000 | |
branch | jv |
changeset 25438 | 8ee2cac9f9f4 |
parent 25426 | 963f86568b2d |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
23107 | 3 |
COPYRIGHT (c) 2018 Jan Vrany |
20339 | 4 |
All Rights Reserved |
1 | 5 |
|
6 |
This software is furnished under a license and may be used |
|
7 |
only in accordance with the terms of that license and with the |
|
8 |
inclusion of the above copyright notice. This software may not |
|
9 |
be provided or otherwise made available to, or used by, any |
|
10 |
other person. No title to or ownership of the software is |
|
11 |
hereby transferred. |
|
12 |
" |
|
5410 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
17689 | 15 |
"{ NameSpace: Smalltalk }" |
16 |
||
1 | 17 |
Integer subclass:#SmallInteger |
20819 | 18 |
instanceVariableNames:'' |
19 |
classVariableNames:'ZeroString' |
|
20 |
poolDictionaries:'' |
|
21 |
category:'Magnitude-Numbers' |
|
1 | 22 |
! |
23 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
24 |
!SmallInteger class methodsFor:'documentation'! |
1 | 25 |
|
88 | 26 |
copyright |
27 |
" |
|
28 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
23107 | 29 |
COPYRIGHT (c) 2018 Jan Vrany |
20339 | 30 |
All Rights Reserved |
88 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
68 | 41 |
documentation |
42 |
" |
|
8908 | 43 |
SmallIntegers are Integers in the range of at least +/- 2^30 |
19318 | 44 |
i.e. 31 bits, but this is not a guaranteed: |
20339 | 45 |
on an alpha or x86_64, 63 bits are used, if the system was configured for 64bit mode. |
46 |
under the Schteam-VM, 64 bits are used (i.e. a full long integer) |
|
88 | 47 |
|
68 | 48 |
These are no real objects - they have no instances (not even storage !!) |
49 |
and cannot be subclassed. |
|
21155 | 50 |
The reason is to save both storage and runtime by not boxing and |
51 |
garbage collecting SmallIntegers in the system. |
|
52 |
SmallInts are marked by having the TAG_INT |
|
8908 | 53 |
bit set, in contrast to all other objects which do not. |
54 |
Since this knowledge is hardwired into the system (and there is no |
|
55 |
class-field stored with SmallIntegers) there can be no subclass of |
|
88 | 56 |
SmallInteger (sorry). |
57 |
||
58 |
If you really need this kind of thing, create a subclass of Integer, |
|
59 |
with an instance variable holding the value. |
|
1295 | 60 |
|
21155 | 61 |
Because the range and sharing of SmallIntegers is different among implementations |
62 |
(both in different dialects, and in different architectures within the Smalltalk/X family), |
|
63 |
you should not depend on the identity of two integers with the same value. |
|
64 |
For portable code, when comparing integers, use #'=' and #'~=' (instead of #'==' / #'~~'), |
|
65 |
unless you are comparing very small integers in the -1024 .. 0 .. 1024 range. |
|
66 |
||
1295 | 67 |
[author:] |
20339 | 68 |
Claus Gittinger |
1556 | 69 |
|
70 |
[see also:] |
|
20339 | 71 |
Number |
72 |
Float Fraction FixedPoint |
|
73 |
LargeInteger |
|
68 | 74 |
" |
75 |
! ! |
|
1 | 76 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
77 |
!SmallInteger class methodsFor:'instance creation'! |
1 | 78 |
|
79 |
basicNew |
|
80 |
"catch instance creation |
|
81 |
- SmallIntegers cannot be created with new" |
|
82 |
||
83 |
self error:'instances of SmallInteger cannot be created with new' |
|
84 |
! |
|
85 |
||
86 |
basicNew:size |
|
87 |
"catch instance creation |
|
88 |
- SmallIntegers cannot be created with new" |
|
89 |
||
90 |
self error:'instances of SmallInteger cannot be created with new' |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
91 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
92 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
93 |
fastFromString:aString at:startIndex |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
94 |
"return the next SmallInteger from the string starting at startIndex. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
95 |
No spaces are skipped. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
96 |
Raises an error, if the index is out of bounds, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
97 |
Returns garbage if the argument string is not a valid integer. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
98 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
99 |
This is a specially tuned entry (using a low-level C-call to atol). |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
100 |
It has been added to allow high speed string decomposition into |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
101 |
numbers, especially for mass-data (reading millions of numbers)." |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
102 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
103 |
%{ /* NOCONTEXT */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
104 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
105 |
char *cp = (char *)(__stringVal(aString)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
106 |
int idx = __intVal(startIndex) - 1; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
107 |
long atol(const char *); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
108 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
109 |
if ((unsigned)idx < __stringSize(aString)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
110 |
long val = atol(cp + idx); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
111 |
RETURN (__MKINT(val)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
112 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
113 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
114 |
%}. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
115 |
self primitiveFailed. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
116 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
117 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
118 |
SmallInteger fastFromString:'hello12345' at:6 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
119 |
SmallInteger fastFromString:'12345' at:1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
120 |
SmallInteger fastFromString:'12345' at:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
121 |
SmallInteger fastFromString:'12345' at:3 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
122 |
SmallInteger fastFromString:'12345' at:4 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
123 |
SmallInteger fastFromString:'12345' at:5 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
124 |
SmallInteger fastFromString:'12345' at:6 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
125 |
SmallInteger fastFromString:'12345' at:0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
126 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
127 |
Time millisecondsToRun:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
128 |
1000000 timesRepeat:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
129 |
SmallInteger readFrom:'12345' |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
130 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
131 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
132 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
133 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
134 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
135 |
Time millisecondsToRun:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
136 |
1000000 timesRepeat:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
137 |
SmallInteger fastFromString:'12345' at:1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
138 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
139 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
140 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
141 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
142 |
"Created: / 27-10-2018 / 08:52:32 / Claus Gittinger" |
1 | 143 |
! ! |
144 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
145 |
!SmallInteger class methodsFor:'bit mask constants'! |
701 | 146 |
|
147 |
bitMaskFor:index |
|
148 |
"return a bitmask for the index's bit (index starts at 1)" |
|
149 |
||
150 |
(index between:1 and:SmallInteger maxBits) ifFalse:[ |
|
20339 | 151 |
^ SubscriptOutOfBoundsSignal |
152 |
raiseRequestWith:index |
|
153 |
errorString:'index out of bounds' |
|
701 | 154 |
]. |
155 |
^ 1 bitShift:(index - 1) |
|
156 |
! ! |
|
157 |
||
15259 | 158 |
!SmallInteger class methodsFor:'class initialization'! |
159 |
||
160 |
initialize |
|
161 |
ZeroString := '0' |
|
162 |
! ! |
|
163 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
164 |
!SmallInteger class methodsFor:'constants'! |
1 | 165 |
|
166 |
maxBits |
|
68 | 167 |
"return the number of bits in instances of me. |
168 |
For very special uses only - not constant across implementations" |
|
1 | 169 |
|
170 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
171 |
#ifdef __SCHTEAM__ |
18227 | 172 |
// longs are always 64bits |
173 |
return context._RETURN ( STInteger._new(64) ); |
|
174 |
#else |
|
8913 | 175 |
RETURN ( __mkSmallInteger(N_INT_BITS) ); |
18227 | 176 |
#endif |
1 | 177 |
%} |
18227 | 178 |
" |
179 |
SmallInteger maxBits |
|
180 |
" |
|
1 | 181 |
! |
182 |
||
183 |
maxBytes |
|
68 | 184 |
"return the number of bytes in instances of me. |
19486 | 185 |
For very special uses only - not constant across implementations. |
186 |
Notice: this is inlined by the compiler(s) as a constant, |
|
187 |
therefore, a query like 'SmallInteger maxBytes == 8' |
|
188 |
costs nothing; it is compiled in as a constant." |
|
1 | 189 |
|
190 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
191 |
#ifdef __SCHTEAM__ |
18227 | 192 |
// longs are always 8 bytes |
193 |
return context._RETURN ( STInteger._new(8) ); |
|
194 |
#else |
|
8913 | 195 |
RETURN ( __mkSmallInteger(N_INT_BITS / 8 + 1) ); |
18227 | 196 |
#endif |
1 | 197 |
%} |
3684 | 198 |
" |
199 |
SmallInteger maxBytes |
|
200 |
" |
|
1 | 201 |
! |
202 |
||
203 |
maxVal |
|
68 | 204 |
"return the largest Integer representable as SmallInteger. |
205 |
For very special uses only - not constant across implementations" |
|
1 | 206 |
|
207 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
208 |
#ifdef __SCHTEAM__ |
18227 | 209 |
return context._RETURN ( STInteger._MAX_INTVAL ); |
210 |
#else |
|
8913 | 211 |
RETURN ( __mkSmallInteger(_MAX_INT) ); |
18227 | 212 |
#endif |
1 | 213 |
%} |
18227 | 214 |
" |
215 |
SmallInteger maxVal |
|
216 |
" |
|
701 | 217 |
! |
218 |
||
219 |
minVal |
|
220 |
"return the smallest Integer representable as SmallInteger. |
|
221 |
For very special uses only - not constant across implementations" |
|
222 |
||
223 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
224 |
#ifdef __SCHTEAM__ |
18227 | 225 |
return context._RETURN ( STInteger._MIN_INTVAL ); |
226 |
#else |
|
8913 | 227 |
RETURN ( __mkSmallInteger(_MIN_INT) ); |
18227 | 228 |
#endif |
701 | 229 |
%} |
18227 | 230 |
" |
231 |
SmallInteger minVal |
|
232 |
" |
|
1 | 233 |
! ! |
234 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
235 |
!SmallInteger class methodsFor:'queries'! |
3 | 236 |
|
50 | 237 |
canBeSubclassed |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
238 |
"return true, if it's allowed to create subclasses of the receiver. |
3914 | 239 |
Return false here - since it is NOT possible for SmallInteger |
240 |
(due to the tagged representation of SmallIntegers)" |
|
50 | 241 |
|
242 |
^ false |
|
3914 | 243 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
244 |
"Modified: / 05-11-1998 / 16:11:27 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
245 |
"Modified (comment): / 13-02-2017 / 20:30:46 / cg" |
1 | 246 |
! |
247 |
||
2672
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
248 |
hasImmediateInstances |
4661 | 249 |
"return true if this class has immediate instances |
250 |
i.e. if the instances are represented in the pointer itself and |
|
251 |
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
|
252 |
Redefined from Behavior" |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
253 |
|
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
254 |
^ true |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
255 |
|
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
256 |
"Created: 3.6.1997 / 12:01:26 / cg" |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
257 |
! |
dc3662188b2c
added #hasImmediateInstances for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
2653
diff
changeset
|
258 |
|
701 | 259 |
isBuiltInClass |
1264 | 260 |
"return true if this class is known by the run-time-system. |
261 |
Here, true is returned." |
|
1 | 262 |
|
263 |
^ true |
|
1264 | 264 |
|
265 |
"Modified: 23.4.1996 / 16:00:33 / cg" |
|
1 | 266 |
! ! |
267 |
||
268 |
!SmallInteger methodsFor:'arithmetic'! |
|
269 |
||
270 |
* aNumber |
|
11728 | 271 |
"return the product of the receiver and the argument" |
1 | 272 |
|
273 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
274 |
#ifdef __SCHTEAM__ |
18236 | 275 |
return context._RETURN( self.times( aNumber )); |
18227 | 276 |
#else |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
277 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
278 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
279 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
280 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
281 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
282 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
283 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
284 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
285 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
286 |
|
68 | 287 |
INT myValue, otherValue; |
288 |
unsigned INT productLow, productHi; |
|
289 |
int negative; |
|
290 |
||
19994 | 291 |
myValue = __intVal(self); |
292 |
||
1716
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
293 |
# define low16Bits(foo) ((foo) & 0xFFFF) |
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
294 |
# define hi16Bits(foo) ((foo) >> 16) |
2785 | 295 |
# define low32Bits(foo) ((foo) & 0xFFFFFFFFL) |
296 |
# define hi32Bits(foo) ((foo) >> 32) |
|
95 | 297 |
|
298 |
/* |
|
299 |
* can we use long long arithmetic ? |
|
4176 | 300 |
* |
301 |
* long-long arithmetic seems to be buggy with some systems |
|
302 |
* (took me a while to find this out :-( |
|
303 |
* (try 10000 * 10000) |
|
95 | 304 |
*/ |
18227 | 305 |
# if defined(__sparc__) && defined(__GNUC__) && (__GNUC__ >= 2) |
306 |
# define USE_LONGLONG_FOR_MUL |
|
307 |
# endif |
|
308 |
||
309 |
# if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2) |
|
310 |
# define USE_LONGLONG_FOR_MUL |
|
311 |
# endif |
|
1 | 312 |
|
252 | 313 |
if (__isSmallInteger(aNumber)) { |
20339 | 314 |
otherValue = __intVal(aNumber); |
2785 | 315 |
|
18227 | 316 |
# if defined(USE_LONGLONG_FOR_MUL) |
20339 | 317 |
{ |
18227 | 318 |
# if defined(__alpha__) && !defined(__alpha64__) |
319 |
# define LONGLONG INT64 |
|
320 |
# else |
|
321 |
# define LONGLONG long long |
|
322 |
# endif |
|
20339 | 323 |
LONGLONG product; |
324 |
||
325 |
product = (LONGLONG)myValue * (LONGLONG)otherValue; |
|
326 |
if ((product >= (LONGLONG)_MIN_INT) |
|
327 |
&& (product <= (LONGLONG)_MAX_INT)) { |
|
328 |
RETURN ( __mkSmallInteger((INT)product) ); |
|
329 |
} |
|
330 |
if (product < 0) { |
|
331 |
negative = -1; |
|
332 |
product = -product; |
|
333 |
} else { |
|
334 |
negative = 1; |
|
335 |
} |
|
336 |
productHi = product >> 32; |
|
337 |
productLow = product & 0xFFFFFFFFLL; |
|
338 |
} |
|
18227 | 339 |
# else /* no long-long */ |
20339 | 340 |
negative = 1; |
341 |
if (myValue < 0) { |
|
342 |
negative = -1; |
|
343 |
myValue = -myValue; |
|
344 |
} |
|
345 |
if (otherValue < 0) { |
|
346 |
negative = -negative; |
|
347 |
otherValue = -otherValue; |
|
348 |
} |
|
68 | 349 |
|
18227 | 350 |
# if defined(__GNUC__) && defined(__mc68k__) |
20339 | 351 |
asm ("mulu%.l %3,%1:%0" |
352 |
: "=d" ((unsigned long)(productLow)), |
|
353 |
"=d" ((unsigned long)(productHi)) |
|
354 |
: "%0" ((unsigned long)(myValue)), |
|
355 |
"dmi" ((unsigned long)(otherValue))); |
|
18227 | 356 |
# else |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
357 |
# if defined (__GNUC__) && defined(__x86__) |
20339 | 358 |
asm ("mull %3" |
359 |
: "=a" ((unsigned long)(productLow)), |
|
360 |
"=d" ((unsigned long)(productHi)) |
|
361 |
: "%0" ((unsigned long)(myValue)), |
|
362 |
"rm" ((unsigned long)(otherValue))); |
|
18227 | 363 |
# else |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
364 |
# if defined(__win32__) && defined(__BORLANDC__) |
20339 | 365 |
asm { |
366 |
mov eax, myValue |
|
367 |
mov edx, otherValue |
|
368 |
mul edx |
|
369 |
mov productLow, eax |
|
370 |
mov productHi, edx |
|
371 |
} |
|
18227 | 372 |
# else /* generic */ |
20339 | 373 |
{ |
374 |
unsigned INT pHH, pHL, pLH, pLL; |
|
375 |
unsigned INT low1, low2, hi1, hi2; |
|
376 |
unsigned INT t; |
|
377 |
||
378 |
/* unsigned multiply myValue * otherValue -> productHi, productLow |
|
379 |
* |
|
380 |
* this is too slow: |
|
381 |
* since most machines can do 32*32 to 64 bit multiply, |
|
382 |
* (or at least 32*32 with Overflow check) |
|
383 |
* - need more assembler (inline) functions here |
|
384 |
*/ |
|
18227 | 385 |
# if __POINTER_SIZE__ == 8 |
20339 | 386 |
low1 = low32Bits((unsigned INT)myValue); |
387 |
hi1 = hi32Bits((unsigned INT)myValue); |
|
388 |
low2 = low32Bits((unsigned INT)otherValue); |
|
389 |
hi2 = hi32Bits((unsigned INT)otherValue); |
|
20333 | 390 |
# define LLMASK 0xC000000000000000LL |
18227 | 391 |
# else |
20339 | 392 |
low1 = low16Bits((unsigned INT)myValue); |
393 |
hi1 = hi16Bits((unsigned INT)myValue); |
|
394 |
low2 = low16Bits((unsigned INT)otherValue); |
|
395 |
hi2 = hi16Bits((unsigned INT)otherValue); |
|
20333 | 396 |
# define LLMASK 0xC0000000L |
18227 | 397 |
# endif |
4167 | 398 |
|
20339 | 399 |
pLH = low1 * hi2; |
400 |
pHL = hi1 * low2; |
|
401 |
pLL = low1 * low2; |
|
402 |
pHH = hi1 * hi2; |
|
403 |
||
404 |
/* |
|
405 |
* the common case ... |
|
406 |
*/ |
|
407 |
if ((pHL == 0) |
|
408 |
&& (pLH == 0) |
|
409 |
&& (pHH == 0) |
|
410 |
&& ((pLL & LLMASK) == 0)) { |
|
411 |
if (negative < 0) { |
|
412 |
RETURN ( __mkSmallInteger(- ((INT)pLL)) ); |
|
413 |
} |
|
414 |
RETURN ( __mkSmallInteger((INT)pLL) ); |
|
415 |
} |
|
416 |
||
417 |
/* |
|
418 |
* pHH |--------|--------| |
|
419 |
* pLH |--------|--------| |
|
420 |
* pHL |--------|--------| |
|
421 |
* pLL |--------|--------| |
|
422 |
*/ |
|
4167 | 423 |
|
18227 | 424 |
# if __POINTER_SIZE__ == 8 |
20339 | 425 |
t = low32Bits(pLH) + low32Bits(pHL) + hi32Bits(pLL); |
426 |
productLow = (t << 32) + low32Bits(pLL); |
|
427 |
productHi = pHH + hi32Bits(t) + hi32Bits(pHL) + hi32Bits(pLH); |
|
18227 | 428 |
# else |
20339 | 429 |
t = low16Bits(pLH) + low16Bits(pHL) + hi16Bits(pLL); |
430 |
productLow = (t << 16) + low16Bits(pLL); |
|
431 |
productHi = pHH + hi16Bits(t) + hi16Bits(pHL) + hi16Bits(pLH); |
|
18227 | 432 |
# endif |
20339 | 433 |
} |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
434 |
# endif /* ! __win32__ */ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
435 |
# endif /* ! (__GNUC__ && __x86__) */ |
18227 | 436 |
# endif /* ! (__GNUC__ && __mc68k__) */ |
4167 | 437 |
|
20339 | 438 |
if (productHi == 0) { |
439 |
if (negative < 0) { |
|
440 |
if (productLow <= -(_MIN_INT)) { |
|
441 |
RETURN ( __mkSmallInteger(-((INT)productLow)) ); |
|
442 |
} |
|
443 |
} else { |
|
444 |
if (productLow <= _MAX_INT) { |
|
445 |
RETURN ( __mkSmallInteger(productLow) ); |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
18227 | 449 |
# endif /* ! USE_LONGLONG */ |
450 |
||
451 |
# if __POINTER_SIZE__ == 8 |
|
20339 | 452 |
RETURN (__MKLARGEINT128(negative, productLow, productHi)); |
18227 | 453 |
# else |
20339 | 454 |
RETURN (__MKLARGEINT64(negative, productLow, productHi)); |
18227 | 455 |
# endif |
283 | 456 |
} else if (__isFloatLike(aNumber)) { |
20339 | 457 |
OBJ newFloat; |
458 |
double val = (double)myValue * __floatVal(aNumber); |
|
459 |
||
460 |
__qMKFLOAT(newFloat, val); |
|
461 |
RETURN ( newFloat ); |
|
3532
4b1a5cc57f8f
added int op shortFloat inline
Claus Gittinger <cg@exept.de>
parents:
3458
diff
changeset
|
462 |
} else if (__isShortFloat(aNumber)) { |
20339 | 463 |
OBJ newFloat; |
464 |
float val = (float)myValue * __shortFloatVal(aNumber); |
|
465 |
||
466 |
__qMKSFLOAT(newFloat, val); |
|
467 |
RETURN ( newFloat ); |
|
18815 | 468 |
} else if (__isFractionLike(aNumber)) { |
20339 | 469 |
OBJ t = __FractionInstPtr(aNumber)->f_numerator; |
470 |
||
471 |
if (myValue == 0) { |
|
472 |
RETURN(__mkSmallInteger(0)); |
|
473 |
} |
|
474 |
||
475 |
if (__isSmallInteger(t)) { |
|
476 |
INT num = __intVal(t); |
|
477 |
t = __FractionInstPtr(aNumber)->f_denominator; |
|
478 |
if (__isSmallInteger(t)) { |
|
479 |
INT prod = myValue * num; |
|
480 |
if (prod / myValue == num) { // check for overflow |
|
481 |
INT den = __intVal(t); |
|
482 |
INT quo = prod / den; |
|
483 |
if (quo * den == prod) { // check for integer result |
|
484 |
RETURN ( __mkSmallInteger(quo) ); |
|
485 |
} |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
1 | 489 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
490 |
#endif /* not __SCHTEAM__ */ |
2641 | 491 |
%}. |
1 | 492 |
^ aNumber productFromInteger:self |
18815 | 493 |
|
494 |
" |
|
20339 | 495 |
3 * (1/2) |
496 |
6 * (1/2) |
|
497 |
6 * (-1/2) |
|
18815 | 498 |
" |
1 | 499 |
! |
500 |
||
701 | 501 |
+ aNumber |
18849 | 502 |
"return the sum of the receiver's value and the argument's value" |
701 | 503 |
|
504 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
505 |
#ifdef __SCHTEAM__ |
18236 | 506 |
return context._RETURN( self.plus( aNumber )); |
18227 | 507 |
#else |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
508 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
509 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
510 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
511 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
512 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
513 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
514 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
515 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
516 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
517 |
|
701 | 518 |
if (__isSmallInteger(aNumber)) { |
18227 | 519 |
# ifdef _ADD_IO_IO |
20339 | 520 |
RETURN ( _ADD_IO_IO(self, aNumber) ); |
18227 | 521 |
# else |
20339 | 522 |
REGISTER INT sum; |
523 |
||
524 |
sum = __intVal(self) + __intVal(aNumber); |
|
525 |
if (__ISVALIDINTEGER(sum)) { |
|
526 |
RETURN ( __mkSmallInteger(sum) ); |
|
527 |
} |
|
528 |
RETURN ( __MKLARGEINT(sum) ); |
|
18227 | 529 |
# endif |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
530 |
} |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
531 |
if (__isFloatLike(aNumber)) { |
20339 | 532 |
OBJ newFloat; |
533 |
double val = (double)__intVal(self) + __floatVal(aNumber); |
|
534 |
||
535 |
__qMKFLOAT(newFloat, val); |
|
536 |
RETURN ( newFloat ); |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
537 |
} |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
538 |
if (__isShortFloat(aNumber)) { |
20339 | 539 |
OBJ newFloat; |
540 |
float val = (float)__intVal(self) + __shortFloatVal(aNumber); |
|
541 |
||
542 |
__qMKSFLOAT(newFloat, val); |
|
543 |
RETURN ( newFloat ); |
|
701 | 544 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
545 |
#endif /* not __SCHTEAM__ */ |
3532
4b1a5cc57f8f
added int op shortFloat inline
Claus Gittinger <cg@exept.de>
parents:
3458
diff
changeset
|
546 |
%}. |
701 | 547 |
^ aNumber sumFromInteger:self |
548 |
! |
|
549 |
||
550 |
- aNumber |
|
18849 | 551 |
"return the difference of the receiver's value and the argument's value" |
701 | 552 |
|
553 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
554 |
#ifdef __SCHTEAM__ |
18236 | 555 |
return context._RETURN( self.minus( aNumber )); |
18227 | 556 |
#else |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
557 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
558 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
559 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
560 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
561 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
562 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
563 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
564 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
565 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
566 |
|
701 | 567 |
if (__isSmallInteger(aNumber)) { |
18227 | 568 |
# ifdef _SUB_IO_IO |
20339 | 569 |
RETURN ( _SUB_IO_IO(self, aNumber) ); |
18227 | 570 |
# else |
20339 | 571 |
REGISTER INT diff = __intVal(self) - __intVal(aNumber); |
572 |
if (__ISVALIDINTEGER(diff)) { |
|
573 |
RETURN ( __mkSmallInteger(diff) ); |
|
574 |
} |
|
575 |
RETURN ( __MKLARGEINT(diff) ); |
|
18227 | 576 |
# endif |
8908 | 577 |
} |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
578 |
if (__isFloatLike(aNumber)) { |
20339 | 579 |
OBJ newFloat; |
580 |
double val = (double)__intVal(self) - __floatVal(aNumber); |
|
581 |
||
582 |
__qMKFLOAT(newFloat, val); |
|
583 |
RETURN ( newFloat ); |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
584 |
} |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
585 |
if (__isShortFloat(aNumber)) { |
20339 | 586 |
OBJ newFloat; |
587 |
float val = (float)__intVal(self) - __shortFloatVal(aNumber); |
|
588 |
||
589 |
__qMKSFLOAT(newFloat, val); |
|
590 |
RETURN ( newFloat ); |
|
701 | 591 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
592 |
#endif /* not __SCHTEAM__ */ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
593 |
%}. |
701 | 594 |
^ aNumber differenceFromInteger:self |
595 |
! |
|
596 |
||
1 | 597 |
/ aNumber |
18849 | 598 |
"return the quotient of the receiver's value and the argument's value" |
1 | 599 |
|
600 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
601 |
#ifdef __SCHTEAM__ |
18236 | 602 |
return context._RETURN( self.quotient( aNumber )); |
18227 | 603 |
#else |
1 | 604 |
|
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
605 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
606 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
607 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
608 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
609 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
610 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
611 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
612 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
613 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
614 |
|
19994 | 615 |
INT t, val; |
1 | 616 |
double dval; |
19994 | 617 |
INT myValue = __intVal(self); |
1 | 618 |
|
252 | 619 |
if (__isSmallInteger(aNumber)) { |
20339 | 620 |
val = __intVal(aNumber); |
621 |
if (val != 0) { |
|
622 |
t = myValue / val; |
|
18227 | 623 |
# ifdef GOOD_OPTIMIZER |
20339 | 624 |
if (myValue % val == 0) { |
18227 | 625 |
# else |
20339 | 626 |
/* this is stupid - all I want is to look for a remainder ... |
627 |
but most compilers are too stupid and generate an extra modulus |
|
628 |
instruction for "if (me % val)". |
|
629 |
Even if most divide instructions already leave the remainder in |
|
630 |
some register. |
|
631 |
Therefore I use a multiplication which is faster than a modulo |
|
632 |
on most machines. Hint to GNU people :-) |
|
633 |
*/ |
|
634 |
if ((t * val) == myValue) { |
|
18227 | 635 |
# endif |
20339 | 636 |
RETURN ( __mkSmallInteger(t) ); |
637 |
} |
|
638 |
} |
|
1 | 639 |
} else { |
20339 | 640 |
if (__isFloatLike(aNumber)) { |
641 |
dval = __floatVal(aNumber); |
|
642 |
if (dval != 0.0) { |
|
643 |
OBJ newFloat; |
|
644 |
double val = (double)myValue / dval; |
|
645 |
||
646 |
__qMKFLOAT(newFloat, val); |
|
647 |
RETURN ( newFloat ); |
|
648 |
} |
|
649 |
} |
|
1 | 650 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
651 |
#endif /* not __SCHTEAM__ */ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
652 |
%}. |
1 | 653 |
aNumber isInteger ifTrue:[ |
20339 | 654 |
aNumber == 0 ifTrue:[ |
655 |
^ ZeroDivide raiseRequestWith:thisContext. |
|
656 |
]. |
|
657 |
^ Fraction numerator:self denominator:aNumber |
|
1 | 658 |
]. |
659 |
^ aNumber quotientFromInteger: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 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
662 |
8 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
663 |
9 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
664 |
9 // 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
665 |
9 quo:4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
666 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
667 |
-8 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
668 |
-9 / 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
669 |
-9 // 4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
670 |
-9 quo:4 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
671 |
" |
1 | 672 |
! |
673 |
||
2 | 674 |
// aNumber |
18849 | 675 |
"return the integer part of the quotient of the receiver's value |
676 |
and the argument's value. |
|
17033 | 677 |
The result is truncated toward negative infinity |
678 |
and will be negative, if the operands signs differ. |
|
679 |
The following is always true: |
|
20339 | 680 |
(receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver |
17033 | 681 |
|
14795 | 682 |
Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. |
20339 | 683 |
Especially surprising (because of truncation toward negative infinity): |
684 |
-1 // 10 -> -1 (because -(1/10) is truncated towards next smaller integer, which is -1. |
|
685 |
-10 // 3 -> -4 (because -(10/3) is truncated towards next smaller integer, which is -4. |
|
686 |
||
687 |
See #quo: which truncates toward zero and returns -2 in the above case |
|
688 |
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
|
689 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
690 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
691 |
#ifdef __SCHTEAM__ |
18236 | 692 |
return context._RETURN( self.quotientTruncated( aNumber )); |
18227 | 693 |
#else |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
694 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
695 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
696 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
697 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
698 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
699 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
700 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
701 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
702 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
703 |
|
19994 | 704 |
INT divisor, rslt; |
705 |
INT dividend = __intVal(self); |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
706 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
707 |
if (__isSmallInteger(aNumber)) { |
20339 | 708 |
divisor = __intVal(aNumber); |
709 |
if (divisor != 0) { |
|
710 |
rslt = dividend / divisor; |
|
711 |
/* |
|
712 |
* Optimized to speed up positive result |
|
713 |
*/ |
|
714 |
if (rslt <= 0) { |
|
715 |
if (rslt == 0) { |
|
716 |
if ((dividend ^ divisor) < 0) { |
|
717 |
/* |
|
718 |
* result (negative) has been truncated toward 0. |
|
719 |
* Return -1, because we truncate toward negative inf. |
|
720 |
*/ |
|
721 |
rslt = -1; |
|
722 |
} |
|
723 |
} else { |
|
724 |
/* |
|
725 |
* If result (negative) has been truncated toward 0, |
|
726 |
* subtract 1, because we truncate toward negative inf. |
|
727 |
*/ |
|
728 |
if (divisor > 0) { |
|
729 |
if (rslt * divisor > dividend) { |
|
730 |
rslt--; |
|
731 |
} |
|
732 |
} else { |
|
733 |
if (rslt * divisor < dividend) { |
|
734 |
rslt--; |
|
735 |
} |
|
736 |
} |
|
737 |
} |
|
738 |
} |
|
739 |
RETURN ( __mkSmallInteger(rslt) ); |
|
740 |
} |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
741 |
} else { |
20339 | 742 |
if (__isFractionLike(aNumber)) { |
743 |
OBJ t = __FractionInstPtr(aNumber)->f_numerator; |
|
744 |
if (__isSmallInteger(t)) { |
|
745 |
INT num = __intVal(t); |
|
746 |
t = __FractionInstPtr(aNumber)->f_denominator; |
|
747 |
if (__isSmallInteger(t)) { |
|
748 |
INT den = __intVal(t); |
|
749 |
INT prod; |
|
18815 | 750 |
#if 0 && defined(__GNUC__) // supported from GCC 5 |
20339 | 751 |
if (!__builtin_mul_overflow(myself, den, &prod)) { |
752 |
goto out; // overflow, try harder... |
|
753 |
} |
|
18815 | 754 |
#else |
20339 | 755 |
prod = dividend * den; |
756 |
// make sure, that no overflow occurred |
|
757 |
if (prod / den != dividend) { |
|
758 |
goto out; // overflow, try harder... |
|
759 |
} |
|
18815 | 760 |
#endif |
20339 | 761 |
rslt = prod / num; |
762 |
||
763 |
/* |
|
764 |
* Optimized to speed up positive result |
|
765 |
*/ |
|
766 |
if (rslt <= 0) { |
|
767 |
if (rslt == 0) { |
|
768 |
if ((dividend ^ num) < 0) { |
|
769 |
/* |
|
770 |
* result (negative) has been truncated toward 0. |
|
771 |
* Return -1, because we truncate toward negative inf. |
|
772 |
*/ |
|
773 |
rslt = -1; |
|
774 |
} |
|
775 |
} else { |
|
776 |
/* |
|
777 |
* If result (negative) has been truncated toward 0, |
|
778 |
* subtract 1, because we truncate toward negative inf. |
|
779 |
*/ |
|
780 |
if (num > 0) { |
|
781 |
if (rslt * num > prod) rslt--; |
|
782 |
} else { |
|
783 |
if (rslt * num < prod) rslt--; |
|
784 |
} |
|
785 |
} |
|
786 |
} |
|
787 |
RETURN ( __mkSmallInteger(rslt) ); |
|
788 |
} |
|
789 |
} |
|
790 |
} |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
791 |
} |
18815 | 792 |
out:; |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
793 |
#endif /* not __SCHTEAM__ */ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
794 |
%}. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
795 |
(aNumber = 0) ifTrue:[ |
20339 | 796 |
^ ZeroDivide raiseRequestWith:thisContext. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
797 |
]. |
13006 | 798 |
^ aNumber integerQuotientFromInteger:self |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
799 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
800 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
801 |
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
|
802 |
-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
|
803 |
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
|
804 |
-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
|
805 |
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
|
806 |
-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
|
807 |
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
|
808 |
-1 // -2 ~~ 0 ifTrue:[self halt]. |
18815 | 809 |
|
18947 | 810 |
-7 // (4/3) |
811 |
-7 quo: (4/3) |
|
812 |
||
813 |
7 // (-4/3) |
|
814 |
7 quo: (-4/3) |
|
18815 | 815 |
|
8908 | 816 |
10000 // 3600000 ~~ 0 ifTrue:[self halt]. |
18815 | 817 |
12 // (1 / 1000000000000000000) |
818 |
12 // (1 / 100000000000000) |
|
819 |
12 // 0.00000000000001s |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
820 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
821 |
9 quo:4 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
822 |
-9 quo:4 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
823 |
9 quo:-4 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
824 |
-9 quo:-4 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
825 |
" |
13006 | 826 |
|
827 |
"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
|
828 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
829 |
|
2 | 830 |
\\ aNumber |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
831 |
"Answer the integer remainder m defined by division with truncation toward |
18227 | 832 |
negative infinity. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
833 |
m < |aNumber| AND there is an integer k with (k * aNumber + m) = self |
17033 | 834 |
|
835 |
The returned remainder has the same sign as aNumber. |
|
6067 | 836 |
The following is always true: |
20339 | 837 |
(receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver |
17033 | 838 |
|
839 |
Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. |
|
18227 | 840 |
Especially surprising: |
20339 | 841 |
-1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, |
842 |
and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). |
|
843 |
-10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, |
|
844 |
and -4 * 4 gives -12, so we need to add 2 to get the original -10. |
|
17033 | 845 |
|
846 |
See #rem: which is the corresponding remainder for division via #quo:. |
|
847 |
||
848 |
Redefined here for speed." |
|
1 | 849 |
|
850 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
851 |
#ifdef __SCHTEAM__ |
18236 | 852 |
return context._RETURN( self.remainder( aNumber )); |
18227 | 853 |
#else |
6064
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
854 |
/* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
855 |
* notice: |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
856 |
* the following inline code handles some common cases, |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
857 |
* and exists as an optimization, to speed up those cases. |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
858 |
* |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
859 |
* Conceptionally, (and for most other argument types), |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
860 |
* mixed arithmetic is implemented by double dispatching |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
861 |
* (see the message send at the bottom) |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
862 |
*/ |
04bde2eeb749
comments about double dispatching
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
863 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
864 |
INT dividend, divisor, rem; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
865 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
866 |
if (__isSmallInteger(aNumber) |
20339 | 867 |
&& (divisor = __intVal(aNumber)) != 0) { |
868 |
/* |
|
869 |
* Note that the sign of something modulo a negative number is undefined |
|
870 |
* in C! |
|
871 |
*/ |
|
872 |
dividend = __intVal(self); |
|
873 |
rem = dividend % divisor; |
|
874 |
if (rem) { |
|
875 |
if ((rem ^ divisor) < 0) { |
|
876 |
/* sign of remainder is different from sign of divisor */ |
|
877 |
rem = -rem; |
|
878 |
} |
|
879 |
if ((dividend ^ divisor) < 0) { |
|
880 |
/* different signs, so division would have returned a |
|
881 |
* negative number. |
|
882 |
* C rounds toward zero, this code will simulate |
|
883 |
* rounding towards negative infinity. |
|
884 |
*/ |
|
885 |
rem = divisor - rem; |
|
886 |
} |
|
887 |
} |
|
888 |
RETURN ( __mkSmallInteger(rem) ); |
|
1 | 889 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
890 |
#endif /* not __SCHTEAM__ */ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
891 |
%}. |
2 | 892 |
(aNumber = 0) ifTrue:[ |
20339 | 893 |
^ ZeroDivide raiseRequestWith:thisContext. |
1 | 894 |
]. |
14003 | 895 |
^ aNumber moduloFromInteger:self |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
896 |
|
8908 | 897 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3914
diff
changeset
|
898 |
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
|
899 |
-9 \\ 4 == 3 ifFalse:[self halt]. |
8908 | 900 |
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
|
901 |
-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
|
902 |
(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
|
903 |
(-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
|
904 |
1000 \\ 3600000 == 1000 ifFalse:[self halt] |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
905 |
" |
13006 | 906 |
|
14003 | 907 |
"Modified: / 12-02-2012 / 20:43:40 / cg" |
1 | 908 |
! |
909 |
||
910 |
abs |
|
911 |
"return the absolute value of the receiver |
|
912 |
reimplemented here for speed" |
|
913 |
||
914 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
915 |
#ifdef __SCHTEAM__ |
18227 | 916 |
return context._RETURN( self.abs()); |
917 |
#else |
|
1133 | 918 |
INT val = __intVal(self); |
1 | 919 |
|
62 | 920 |
if (val >= 0) { |
20339 | 921 |
RETURN (self); |
62 | 922 |
} |
1 | 923 |
if (val != _MIN_INT) { |
20339 | 924 |
RETURN ( __mkSmallInteger(-val) ); |
1 | 925 |
} |
3430 | 926 |
/* only reached for minVal */ |
927 |
RETURN( __MKLARGEINT(-_MIN_INT)); |
|
18227 | 928 |
#endif |
324 | 929 |
%}. |
5410 | 930 |
^ super abs |
1 | 931 |
! |
932 |
||
933 |
negated |
|
934 |
"return the negative value of the receiver |
|
935 |
reimplemented here for speed" |
|
936 |
||
937 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
938 |
#ifdef __SCHTEAM__ |
18227 | 939 |
return context._RETURN( self.negated()); |
940 |
#else |
|
1133 | 941 |
INT val = __intVal(self); |
1 | 942 |
|
943 |
if (val != _MIN_INT) { |
|
20333 | 944 |
RETURN ( __mkSmallInteger(- val) ); |
1 | 945 |
} |
3430 | 946 |
/* only reached for minVal */ |
947 |
RETURN (__MKLARGEINT( -_MIN_INT)); |
|
18227 | 948 |
#endif |
324 | 949 |
%}. |
5410 | 950 |
^ 0 - self |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
951 |
! |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
952 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
953 |
quo:aNumber |
18849 | 954 |
"return the integer part of the quotient of the receiver's value |
955 |
and the argument's value. The result is truncated towards zero |
|
8908 | 956 |
and negative, if the operands signs differ.. |
6067 | 957 |
The following is always true: |
20339 | 958 |
(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
|
959 |
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
|
960 |
for negative results, the remainder is ignored. |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
961 |
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
|
962 |
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
|
963 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
964 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
965 |
#ifdef __SCHTEAM__ |
18236 | 966 |
return context._RETURN( self.quotient(aNumber)); |
18227 | 967 |
#else |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
968 |
if (__isSmallInteger(aNumber)) { |
20339 | 969 |
INT val = __intVal(aNumber); |
970 |
if (val != 0) { |
|
971 |
RETURN ( __mkSmallInteger(__intVal(self) / val) ); |
|
972 |
} |
|
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
973 |
} else { |
20339 | 974 |
if (__isFractionLike(aNumber)) { |
975 |
OBJ t = __FractionInstPtr(aNumber)->f_numerator; |
|
976 |
if (__isSmallInteger(t)) { |
|
977 |
INT num = __intVal(t); |
|
978 |
t = __FractionInstPtr(aNumber)->f_denominator; |
|
979 |
if (__isSmallInteger(t)) { |
|
980 |
INT den = __intVal(t); |
|
981 |
INT myself = __intVal(self); |
|
982 |
INT prod; |
|
18815 | 983 |
#if 0 && defined(__GNUC__) // supported from GCC 5 |
20339 | 984 |
if (__builtin_mul_overflow(myself, den, &prod)) { |
985 |
RETURN ( __mkSmallInteger(prod / num )); |
|
986 |
} |
|
18815 | 987 |
#else |
20339 | 988 |
prod = myself * den; |
989 |
// make sure, that no overflow occurred |
|
990 |
if (prod / den == myself) { |
|
991 |
RETURN ( __mkSmallInteger(prod / num )); |
|
992 |
} |
|
18815 | 993 |
#endif |
20339 | 994 |
} |
995 |
} |
|
996 |
} |
|
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
997 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
998 |
#endif /* not __SCHTEAM__ */ |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
999 |
%}. |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1000 |
(aNumber = 0) ifTrue:[ |
20339 | 1001 |
^ ZeroDivide raiseRequestWith:thisContext. |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1002 |
]. |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1003 |
^ self retry:#quo: coercing:aNumber |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1004 |
|
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1005 |
" |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1006 |
9 // 4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1007 |
-9 // 4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1008 |
9 quo:4 |
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1009 |
-9 quo:4 |
18815 | 1010 |
|
18947 | 1011 |
-7 // (4/3) |
1012 |
-7 quo: (4/3) |
|
1013 |
||
1014 |
7 // (-4/3) |
|
1015 |
7 quo: (-4/3) |
|
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
1016 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1017 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1018 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1019 |
sqrt |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1020 |
"return the square root value of the receiver |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1021 |
reimplemented here for speed" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1022 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1023 |
%{ /* NOCONTEXT */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1024 |
#ifdef __SCHTEAM__ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1025 |
return context._RETURN( self.sqrt()); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1026 |
#else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1027 |
double val = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1028 |
extern double sqrt(); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1029 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1030 |
if (val >= 0.0) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1031 |
RETURN (__MKFLOAT(sqrt(val))); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1032 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1033 |
#endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1034 |
%}. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1035 |
^ self class |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1036 |
raise:#imaginaryResultSignal |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1037 |
receiver:self |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1038 |
selector:#sqrt |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1039 |
arguments:#() |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1040 |
errorString:'bad (negative) receiver in sqrt' |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1041 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1042 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1043 |
2 sqrt |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1044 |
-2 sqrt |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1045 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1046 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1047 |
"Created: / 08-05-2017 / 14:57:07 / stefan" |
1 | 1048 |
! ! |
1049 |
||
1050 |
!SmallInteger methodsFor:'bit operators'! |
|
1051 |
||
701 | 1052 |
bitAnd:anInteger |
1053 |
"return the bitwise-and of the receiver and the argument, anInteger" |
|
1 | 1054 |
|
1055 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1056 |
#ifdef __SCHTEAM__ |
18236 | 1057 |
return context._RETURN( self.bitAnd(anInteger)); |
18227 | 1058 |
#else |
701 | 1059 |
/* anding the tags doesn't change it */ |
1060 |
if (__isSmallInteger(anInteger)) { |
|
20339 | 1061 |
RETURN ( ((OBJ) ((INT)self & (INT)anInteger)) ); |
1 | 1062 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1063 |
#endif /* not __SCHTEAM__ */ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1064 |
%}. |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
1065 |
anInteger class == LargeInteger ifTrue:[ |
20339 | 1066 |
^ anInteger bitAnd:self |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
1067 |
]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1068 |
^ self retry:#bitAnd: coercing:anInteger |
701 | 1069 |
|
1070 |
"(2r001010100 bitAnd:2r00001111) radixPrintStringRadix:2" |
|
1 | 1071 |
! |
1072 |
||
3156 | 1073 |
bitClear:anInteger |
1074 |
"return the bitwise-and of the receiver and the complement of the argument, anInteger, |
|
18897 | 1075 |
returning the receiver with bits of the argument cleared. |
19070 | 1076 |
(i.e. the same as self bitAnd:aMaskInteger bitInvert). |
18897 | 1077 |
The method's name may be misleading: the receiver is not changed, |
1078 |
but a new number is returned. Should be named #withBitCleared:" |
|
3156 | 1079 |
|
1080 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1081 |
#ifdef __SCHTEAM__ |
18236 | 1082 |
return context._RETURN( self.bitClear(anInteger)); |
18227 | 1083 |
#else |
3156 | 1084 |
/* anding the tags doesn't change it */ |
1085 |
if (__isSmallInteger(anInteger)) { |
|
20339 | 1086 |
RETURN ( ((OBJ) (((INT)self & ~(INT)anInteger) | TAG_INT)) ); |
3156 | 1087 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1088 |
#endif /* not __SCHTEAM__ */ |
3156 | 1089 |
%}. |
1090 |
^ self retry:#bitClear: coercing:anInteger |
|
1091 |
||
1092 |
" |
|
1093 |
(2r001010100 bitClear:2r00001111) radixPrintStringRadix:2 |
|
5458 | 1094 |
(2r111111111 bitClear:2r00001000) radixPrintStringRadix:2 |
19070 | 1095 |
|
1096 |
(2r001010100 bitAnd:2r00001111 bitInvert) radixPrintStringRadix:2 |
|
3156 | 1097 |
" |
1098 |
! |
|
1099 |
||
11493 | 1100 |
bitCount |
1101 |
"return the number of 1-bits in the receiver" |
|
1102 |
||
1103 |
%{ /* NOCONTEXT */ |
|
18897 | 1104 |
#ifdef __SCHTEAM__ |
1105 |
long _v1, _v2; |
|
1106 |
long _self = self.longValue(); |
|
18947 | 1107 |
long _cnt; |
1108 |
||
18897 | 1109 |
_v1 = _self & 0xFFFFFFFF; |
1110 |
_v1 = _v1 - ((_v1 >> 1) & 0x55555555); |
|
1111 |
_v1 = (_v1 & 0x33333333) + ((_v1 >> 2) & 0x33333333); |
|
1112 |
_v1 = ((_v1 + (_v1 >> 4)) & 0x0F0F0F0F); |
|
1113 |
||
18904 | 1114 |
_v2 = (_self >> 32) & 0xFFFFFFFF; |
18897 | 1115 |
_v2 = _v2 - ((_v2 >> 1) & 0x55555555); |
1116 |
_v2 = (_v2 & 0x33333333) + ((_v2 >> 2) & 0x33333333); |
|
1117 |
_v2 = ((_v2 + (_v2 >> 4)) & 0x0F0F0F0F); |
|
1118 |
||
1119 |
_cnt = ((_v1 * 0x01010101) >> 24) + ((_v2 * 0x01010101) >> 24); |
|
18948 | 1120 |
return __c__._RETURN( STInteger._qnew( (byte)(_cnt) ) ); |
18897 | 1121 |
#else |
13908 | 1122 |
unsigned int _cnt; |
18897 | 1123 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1124 |
// popcnt is actually slower on some (older) cpus; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1125 |
// and almost equal to ALG3 on modern intel hardware. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1126 |
// So ALGO3 is good for all |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1127 |
# if 0 && (defined(__GNUC__) || defined(clang)) && (defined(__x86__) || defined(__x86_64__)) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1128 |
# define ALGORITHM_4 |
18897 | 1129 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1130 |
# define ALGORITHM_3 |
18897 | 1131 |
# endif |
15322 | 1132 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1133 |
# if defined( ALGORITHM_1 ) |
13908 | 1134 |
// old k&r code; might be better if only one or two bits are set |
1135 |
||
18897 | 1136 |
unsigned INT _self = __intVal(self); |
1137 |
||
13908 | 1138 |
_cnt = 0; |
11666 | 1139 |
while (_self) { |
20339 | 1140 |
_cnt++; |
1141 |
_self = _self & (_self - 1); |
|
11493 | 1142 |
} |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1143 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1144 |
# elif defined( ALGORITHM_2 ) |
13908 | 1145 |
// seems to be faster on the average (and has better worst case) |
1146 |
||
1147 |
static unsigned char table[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; |
|
18897 | 1148 |
unsigned INT _self = __intVal(self); |
13908 | 1149 |
|
1150 |
_cnt = 0; |
|
1151 |
while (_self) { |
|
20339 | 1152 |
_cnt += table[ _self & 0x0F ]; |
1153 |
_self >>= 4; |
|
13908 | 1154 |
} |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1155 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1156 |
# elif defined( ALGORITHM_3 ) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1157 |
|
13912 | 1158 |
// the fastest, but hard (impossible) to understand (google for fastest bit count) |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1159 |
|
18898 | 1160 |
unsigned INT _self = __intVal(self); |
1161 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1162 |
# if __POINTER_SIZE__ == 8 |
13912 | 1163 |
unsigned int _v1, _v2; |
1164 |
||
1165 |
_v1 = _self & 0xFFFFFFFF; |
|
1166 |
_v1 = _v1 - ((_v1 >> 1) & 0x55555555); |
|
1167 |
_v1 = (_v1 & 0x33333333) + ((_v1 >> 2) & 0x33333333); |
|
1168 |
_v1 = ((_v1 + (_v1 >> 4)) & 0x0F0F0F0F); |
|
1169 |
||
1170 |
_v2 = (unsigned int)(_self >> 32); |
|
1171 |
_v2 = _v2 - ((_v2 >> 1) & 0x55555555); |
|
1172 |
_v2 = (_v2 & 0x33333333) + ((_v2 >> 2) & 0x33333333); |
|
1173 |
_v2 = ((_v2 + (_v2 >> 4)) & 0x0F0F0F0F); |
|
1174 |
||
1175 |
_cnt = ((_v1 * 0x01010101) >> 24) + ((_v2 * 0x01010101) >> 24); |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1176 |
# else // POINTER_SIZE not 8 |
13908 | 1177 |
_cnt = _self - ((_self >> 1) & 0x55555555); |
1178 |
_cnt = (_cnt & 0x33333333) + ((_cnt >> 2) & 0x33333333); |
|
1179 |
_cnt = ((_cnt + (_cnt >> 4)) & 0x0F0F0F0F); |
|
13912 | 1180 |
_cnt = (_cnt * 0x01010101) >> 24; |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1181 |
# endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1182 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1183 |
# elif defined( ALGORITHM_4 ) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1184 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1185 |
// using the popcnt instruction (x86 only); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1186 |
// strange enough, this is slower than ALGO3 on some (older) Pentiums |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1187 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1188 |
# if __POINTER_SIZE__ == 8 |
18897 | 1189 |
unsigned INT _v; |
1190 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1191 |
#define _POPCNT(__op) \ |
18897 | 1192 |
({ \ |
20339 | 1193 |
INT __rslt; \ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1194 |
asm("xor %%rax,%%rax \n \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1195 |
popcnt %1,%%rax \n \ |
20339 | 1196 |
" : "=a" (__rslt) \ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1197 |
: "g" ((INT)(__op)) \ |
20339 | 1198 |
: "cc"); \ |
1199 |
(OBJ)__rslt; \ |
|
18897 | 1200 |
}) |
1201 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1202 |
_v = ((INT)self) - TAG_INT; |
18897 | 1203 |
_cnt = _POPCNT(_v); |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1204 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1205 |
# else // not POINTER_SIZE 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1206 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1207 |
#define _POPCNT(__op) \ |
18897 | 1208 |
({ \ |
20339 | 1209 |
INT __rslt; \ |
1210 |
asm("xor %%eax,%%eax \n \ |
|
1211 |
popcnt %1,%%eax \n \ |
|
1212 |
" : "=a" (__rslt) \ |
|
1213 |
: "g" ((INT)(__v)) \ |
|
1214 |
: "cc"); \ |
|
1215 |
(OBJ)__rslt; \ |
|
18897 | 1216 |
}) |
1217 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1218 |
_v = ((INT)self) - TAG_INT; |
18897 | 1219 |
_cnt = _POPCNT(_v); |
13908 | 1220 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1221 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1222 |
# elif defined( ALGORITHM_5 ) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1223 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1224 |
// using the builtin_popcnt intrinsic |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1225 |
unsigned INT _v; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1226 |
_v = ((INT)self) - TAG_INT; // remove the tag |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1227 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1228 |
# if __POINTER_SIZE__ == 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1229 |
_cnt = __builtin_popcountll(_v); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1230 |
# else // not POINTER_SIZE 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1231 |
_cnt = __builtin_popcountl(_v); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1232 |
# endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1233 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1234 |
# else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1235 |
error error error "which ALGORITHM?" |
13908 | 1236 |
# endif |
11493 | 1237 |
|
1238 |
RETURN ( __MKSMALLINT(_cnt)); |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1239 |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1240 |
#endif /* not __SCHTEAM__ */ |
18227 | 1241 |
%}. |
1242 |
^ super bitCount. |
|
11493 | 1243 |
|
1244 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1245 |
16rAA bitCount |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1246 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1247 |
TimeDuration toRun:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1248 |
1 to:10000000 do:[:n | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1249 |
n bitCount |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1250 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1251 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1252 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1253 |
AL1: 967ms 958ms 971ms 930ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1254 |
AL2: 900ms 872ms 877ms 870ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1255 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1256 |
AL3: 879ms 849ms 831ms 849ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1257 |
AL4: 858ms 852ms 846ms 810ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1258 |
AL5: 830ms 843ms 835ms 845ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1259 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1260 |
Mac PB2012/2.6Ghz I7 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1261 |
AL3: 855ms 885ms 859ms 878ms 844ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1262 |
AL5: 877ms 877ms 846ms 890ms 853ms |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1263 |
|
13908 | 1264 |
1 to:1000000 do:[:n | |
20339 | 1265 |
self assert:(n bitCount = ((n printStringRadix:2) occurrencesOf:$1)) |
13909 | 1266 |
]. |
1267 |
||
18897 | 1268 |
#( 16r00000000 |
20339 | 1269 |
16r00010000 16r00100000 16r01000000 16r10000000 |
1270 |
16r00020000 16r00200000 16r02000000 16r20000000 |
|
1271 |
16r00040000 16r00400000 16r04000000 16r40000000 |
|
1272 |
16r00080000 16r00800000 16r08000000 16r80000000 |
|
1273 |
||
1274 |
16rFFFFFFFF 16r7FFFFFFF 16r3FFFFFFF 16r1FFFFFFF |
|
1275 |
16rEEEEEEEE 16r7EEEEEEE 16r3EEEEEEE 16r1EEEEEEE |
|
1276 |
16rDDDDDDDD 16r7DDDDDDD 16r3DDDDDDD 16r1DDDDDDD |
|
1277 |
16rCCCCCCCC 16r7CCCCCCC 16r3CCCCCCC 16r1CCCCCCC |
|
1278 |
||
1279 |
16r8000000000010000 16r8000000000100000 16r8000000001000000 16r8000000010000000 |
|
1280 |
16r8000000000020000 16r8000000000200000 16r8000000002000000 16r8000000020000000 |
|
1281 |
16r8000000000040000 16r8000000000400000 16r8000000004000000 16r8000000040000000 |
|
1282 |
16r8000000000080000 16r8000000000800000 16r8000000008000000 16r8000000080000000 |
|
1283 |
||
1284 |
16r80000000FFFFFFFF 16r800000007FFFFFFF 16r800000003FFFFFFF 16r800000001FFFFFFF |
|
1285 |
16r80000000EEEEEEEE 16r800000007EEEEEEE 16r800000003EEEEEEE 16r800000001EEEEEEE |
|
1286 |
16r80000000DDDDDDDD 16r800000007DDDDDDD 16r800000003DDDDDDD 16r800000001DDDDDDD |
|
1287 |
16r80000000CCCCCCCC 16r800000007CCCCCCC 16r800000003CCCCCCC 16r800000001CCCCCCC |
|
1288 |
||
1289 |
16rFFFFFFFFFFFFFFFF 16r7FFFFFFFFFFFFFFF 16r3FFFFFFFFFFFFFFF 16r1FFFFFFFFFFFFFFF |
|
13909 | 1290 |
) do:[:n | |
20339 | 1291 |
self assert:(n bitCount = ((n printStringRadix:2) occurrencesOf:$1)) |
11493 | 1292 |
] |
13908 | 1293 |
|
1294 |
1 to:10000000 do:[:n | |
|
20339 | 1295 |
(n bitCount) |
13908 | 1296 |
] |
11493 | 1297 |
" |
13908 | 1298 |
|
13912 | 1299 |
"Modified: / 09-01-2012 / 19:12:41 / cg" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1300 |
"Modified: / 20-03-2019 / 12:49:47 / Claus Gittinger" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1301 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1302 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1303 |
bitDeinterleave:n |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1304 |
"extract count integers from an n-way Morton number as a vector; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1305 |
This is the inverse operation from bitInterleave: - see comment there. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1306 |
i.e. if count is 3, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1307 |
and the receiver's bits are |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1308 |
cN bN aN ... c2 b2 a2 c1 b1 a1 c0 b0 a0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1309 |
then the result will be a vector containing the numbers a,b,c with bits: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1310 |
aN ... a2 a1 a0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1311 |
bN ... b2 b1 b0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1312 |
cN ... c2 c1 c0. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1313 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1314 |
Morton numbers are great to linearize 2D coordinates |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1315 |
eg. to sort 2D points by distances" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1316 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1317 |
%{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1318 |
#if __POINTER_SIZE__ == 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1319 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1320 |
# define M5555555555555555 0x5555555555555555 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1321 |
# define M3333333333333333 0x3333333333333333 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1322 |
# define M0f0f0f0f0f0f0f0f 0x0f0f0f0f0f0f0f0f |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1323 |
# define M00ff00ff00ff00ff 0x00ff00ff00ff00ff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1324 |
# define M0000ffff0000ffff 0x0000ffff0000ffff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1325 |
# define M00000000ffffffff 0x00000000ffffffff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1326 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1327 |
# define M9249249249249249 0x9249249249249249 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1328 |
# define M30c30c30c30c30c3 0x30c30c30c30c30c3 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1329 |
# define Mf00f00f00f00f00f 0xf00f00f00f00f00f |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1330 |
# define M00ff0000ff0000ff 0x00ff0000ff0000ff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1331 |
# define Mffff00000000ffff 0xffff00000000ffff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1332 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1333 |
#else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1334 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1335 |
# define M5555555555555555 0x55555555 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1336 |
# define M3333333333333333 0x33333333 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1337 |
# define M0f0f0f0f0f0f0f0f 0x0f0f0f0f |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1338 |
# define M00ff00ff00ff00ff 0x00ff00ff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1339 |
# define M0000ffff0000ffff 0x0000ffff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1340 |
# define M00000000ffffffff 0xffffffff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1341 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1342 |
# define M9249249249249249 0x49249249 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1343 |
# define M30c30c30c30c30c3 0xc30c30c3 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1344 |
# define Mf00f00f00f00f00f 0x0f00f00f |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1345 |
# define M00ff0000ff0000ff 0xff0000ff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1346 |
# define Mffff00000000ffff 0x0000ffff |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1347 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1348 |
#endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1349 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1350 |
unsigned INT bits = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1351 |
unsigned INT a, b; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1352 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1353 |
if (n == __MKSMALLINT(2)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1354 |
# define morton2(x,dst) \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1355 |
{ \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1356 |
unsigned INT t; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1357 |
t = (x) & M5555555555555555; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1358 |
t = (t | (t >> 1)) & M3333333333333333; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1359 |
t = (t | (t >> 2)) & M0f0f0f0f0f0f0f0f; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1360 |
t = (t | (t >> 4)) & M00ff00ff00ff00ff; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1361 |
t = (t | (t >> 8)) & M0000ffff0000ffff; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1362 |
t = (t | (t >> 16)) & M00000000ffffffff; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1363 |
dst = t; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1364 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1365 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1366 |
morton2(bits, a); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1367 |
morton2(bits>>1, b); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1368 |
RETURN (__ARRAY_WITH2(__MKSMALLINT(a), __MKSMALLINT(b))); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1369 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1370 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1371 |
if (n == __MKSMALLINT(3)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1372 |
unsigned INT c; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1373 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1374 |
# define morton3(x,dst) \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1375 |
{ \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1376 |
unsigned INT t; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1377 |
t = (x) & M9249249249249249; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1378 |
t = (t | (t >> 2)) & M30c30c30c30c30c3; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1379 |
t = (t | (t >> 4)) & Mf00f00f00f00f00f; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1380 |
t = (t | (t >> 8)) & M00ff0000ff0000ff; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1381 |
t = (t | (t >> 16)) & Mffff00000000ffff; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1382 |
t = (t | (t >> 32)) & M00000000ffffffff; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1383 |
dst = t; \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1384 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1385 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1386 |
morton3(bits, a); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1387 |
morton3(bits>>1, b); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1388 |
morton3(bits>>2, c); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1389 |
RETURN (__ARRAY_WITH3(__MKSMALLINT(a), __MKSMALLINT(b), __MKSMALLINT(c))); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1390 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1391 |
done: ; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1392 |
%}. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1393 |
^ super bitDeinterleave:n |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1394 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1395 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1396 |
(2r1100 bitInterleaveWith:2r1001) bitDeinterleave:2 -> #(12 9) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1397 |
(197 bitInterleaveWith:144) bitDeinterleave:2 -> #(197 144) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1398 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1399 |
(197 bitInterleaveWith:144 and:62) bitDeinterleave:3 -> #(197 144 62) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1400 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1401 |
|a b| |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1402 |
(0 to:31) do:[:bitA | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1403 |
a := 1 << bitA. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1404 |
(0 to:31) do:[:bitB | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1405 |
b := 1 << bitB. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1406 |
self assert:( (a bitInterleaveWith:b) bitDeinterleave:2 ) = {a . b } |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1407 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1408 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1409 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1410 |
|a b c| |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1411 |
(0 to:31) do:[:bitA | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1412 |
a := 1 << bitA. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1413 |
(0 to:31) do:[:bitB | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1414 |
b := 1 << bitB. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1415 |
(0 to:31) do:[:bitC | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1416 |
c := 1 << bitC. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1417 |
self assert:( (a bitInterleaveWith:b and:c) bitDeinterleave:3 ) = {a . b . c} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1418 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1419 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1420 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1421 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1422 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1423 |
"Created: / 28-08-2017 / 18:32:48 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1424 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1425 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1426 |
bitInterleaveWith:anInteger |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1427 |
"generate a Morton number (-> https://en.wikipedia.org/wiki/Morton_number_(number_theory)) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1428 |
by interleaving bits of the receiver |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1429 |
(at even positions if counting from 1) with bits of the argument (at odd bit positions). |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1430 |
Thus, if the bits of the receiver are |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1431 |
aN ... a2 a1 a0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1432 |
and those of the argument are: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1433 |
bN ... b2 b1 b0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1434 |
the result is |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1435 |
bN aN ... b2 a2 b1 a1 b0 a0. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1436 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1437 |
Morton numbers are great to linearize 2D coordinates |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1438 |
eg. to sort 2D points by distances" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1439 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1440 |
%{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1441 |
#if __POINTER_SIZE__ == 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1442 |
# define __SLOW_MULTIPLY__ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1443 |
# ifndef __SLOW_MULTIPLY__ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1444 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1445 |
// the following is only faster, if multiplication is faster than a memory fetch |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1446 |
if (__isSmallInteger(anInteger)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1447 |
INT _a = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1448 |
INT _b = __intVal(anInteger); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1449 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1450 |
if ( (((unsigned)_a)<=0xFFFFFFFF) && (((unsigned)_b)<=0xFFFFFFFF) ) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1451 |
int shift = 0; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1452 |
unsigned INT val = 0; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1453 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1454 |
// Interleave bits of (8-bit) a and b, so that all of the |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1455 |
// bits of a are in the even positions and b in the odd; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1456 |
// resulting in a 16-bit Morton Number. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1457 |
# define interleaveBytes(a,b) \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1458 |
((((a * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL >> 49) & 0x5555) \ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1459 |
| (((b * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL >> 48) & 0xAAAA)) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1460 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1461 |
while (_a | _b) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1462 |
val |= (interleaveBytes((_a & 0xFF), (_b & 0xFF)) << shift); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1463 |
_a = _a >> 8; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1464 |
_b = _b >> 8; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1465 |
shift += 16; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1466 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1467 |
RETURN (__MKUINT(val) ); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1468 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1469 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1470 |
# else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1471 |
# if __POINTER_SIZE__ == 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1472 |
# define HALF_INT_MAX 0xFFFFFFFFFFFFFFFFLL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1473 |
# define M55555555 0x5555555555555555LL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1474 |
# define M33333333 0x3333333333333333LL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1475 |
# define M0F0F0F0F 0x0F0F0F0F0F0F0F0FLL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1476 |
# define M00FF00FF 0x00FF00FF00FF00FFLL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1477 |
# define M0000FFFF 0x0000FFFF0000FFFFLL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1478 |
# else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1479 |
# define HALF_INT_MAX 0xFFFFFFFFL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1480 |
# define M55555555 0x55555555L |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1481 |
# define M33333333 0x33333333L |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1482 |
# define M0F0F0F0F 0x0F0F0F0FL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1483 |
# define M00FF00FF 0x00FF00FFL |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1484 |
# define M0000FFFF 0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1485 |
# endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1486 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1487 |
if (__isSmallInteger(anInteger)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1488 |
INT _a = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1489 |
INT _b = __intVal(anInteger); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1490 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1491 |
if ( (((unsigned INT)_a)<=HALF_INT_MAX) && (((unsigned INT)_b)<=HALF_INT_MAX) ) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1492 |
unsigned INT val; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1493 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1494 |
// spread the bits (...xxx -> ...0x0x0x) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1495 |
_a = (_a | (_a << 16)) & M0000FFFF; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1496 |
_a = (_a | (_a << 8)) & M00FF00FF; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1497 |
_a = (_a | (_a << 4)) & M0F0F0F0F; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1498 |
_a = (_a | (_a << 2)) & M33333333; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1499 |
_a = (_a | (_a << 1)) & M55555555; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1500 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1501 |
_b = (_b | (_b << 16)) & M0000FFFF; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1502 |
_b = (_b | (_b << 8)) & M00FF00FF; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1503 |
_b = (_b | (_b << 4)) & M0F0F0F0F; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1504 |
_b = (_b | (_b << 2)) & M33333333; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1505 |
_b = (_b | (_b << 1)) & M55555555; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1506 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1507 |
// merge the bits (...0a0a0a and ...0b0b0b => ...bababa) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1508 |
val = _a | (_b << 1); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1509 |
RETURN (__MKUINT(val) ); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1510 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1511 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1512 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1513 |
# endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1514 |
#endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1515 |
%}. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1516 |
^ super bitInterleaveWith:anInteger |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1517 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1518 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1519 |
(2r1100 bitInterleaveWith:2r1001) printStringRadix:2 -> '11 01 00 10' |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1520 |
(2r11000101 bitInterleaveWith:2r10010000) printStringRadix:2'1101001000010001 -> '11 01 00 10 00 01 00 01' |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1521 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1522 |
|a b| |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1523 |
(0 to:31) do:[:bitA | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1524 |
a := 1 << bitA. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1525 |
(0 to:31) do:[:bitB | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1526 |
b := 1 << bitB. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1527 |
self assert:( (a bitInterleaveWith:b) bitDeinterleave:2 ) = {a . b } |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1528 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1529 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1530 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1531 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1532 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1533 |
"Created: / 28-08-2017 / 14:33:52 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1534 |
"Modified (comment): / 28-08-2017 / 19:30:14 / cg" |
11493 | 1535 |
! |
1536 |
||
701 | 1537 |
bitInvert |
18897 | 1538 |
"return the value of the receiver with all bits inverted. |
1539 |
The method's name may be misleading: the receiver is not changed, |
|
1540 |
but a new number is returned. Could be named #withBitsInverted" |
|
62 | 1541 |
|
1 | 1542 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1543 |
#ifdef __SCHTEAM__ |
18897 | 1544 |
long _self = self.longValue(); |
1545 |
return __c__._RETURN( STInteger._new( ~_self ) ); |
|
18227 | 1546 |
#else |
701 | 1547 |
/* invert anything except tag bits */ |
1548 |
RETURN ( ((OBJ) ((INT)self ^ ~TAG_MASK)) ); |
|
18897 | 1549 |
#endif /* not __SCHTEAM__ */ |
5412 | 1550 |
%}. |
18227 | 1551 |
^ super bitInvert |
701 | 1552 |
! |
1 | 1553 |
|
19069 | 1554 |
bitInvertByte |
1555 |
"return a new integer, where the low 8 bits are masked and complemented. |
|
1556 |
This returns an unsigned version of what bitInvert would return. |
|
1557 |
(i.e. same as self bitInvert bitAnd:16rFF)" |
|
1558 |
||
1559 |
%{ /* NOCONTEXT */ |
|
1560 |
#ifdef __SCHTEAM__ |
|
1561 |
long _self = self.longValue(); |
|
1562 |
return __c__._RETURN( STInteger._new( ~_self & 0xFF) ); |
|
1563 |
#else |
|
1564 |
/* invert anything except tag bits */ |
|
1565 |
RETURN ( (OBJ) (((INT)self ^ ~TAG_MASK) & (INT)(__mkSmallInteger(0xFF))) ); |
|
1566 |
#endif /* not __SCHTEAM__ */ |
|
1567 |
%}. |
|
1568 |
^ super bitInvertByte |
|
19263 | 1569 |
|
19069 | 1570 |
" |
1571 |
16r7f bitInvert |
|
1572 |
16r7f bitInvertByte |
|
1573 |
||
1574 |
16r80 bitInvert |
|
1575 |
16r80 bitInvertByte |
|
1576 |
||
1577 |
16rff bitInvert |
|
1578 |
16rff bitInvertByte |
|
1579 |
" |
|
1580 |
! |
|
1581 |
||
701 | 1582 |
bitOr:anInteger |
1583 |
"return the bitwise-or of the receiver and the argument, anInteger" |
|
1584 |
||
1585 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1586 |
#ifdef __SCHTEAM__ |
18236 | 1587 |
return context._RETURN( self.bitOr( anInteger )); |
18227 | 1588 |
#else |
701 | 1589 |
/* oring the tags doesn't change it */ |
1590 |
if (__isSmallInteger(anInteger)) { |
|
20339 | 1591 |
RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) ); |
1 | 1592 |
} |
18897 | 1593 |
#endif /* not __SCHTEAM__ */ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1594 |
%}. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1595 |
^ self retry:#bitOr: coercing:anInteger |
701 | 1596 |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1597 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1598 |
(2r000000100 bitOr:2r00000011) radixPrintStringRadix:2 |
8908 | 1599 |
(0 bitOr:16r20000000) hexPrintString |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1600 |
(0 bitOr:16r40000000) hexPrintString |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1601 |
(0 bitOr:16r80000000) hexPrintString |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
1602 |
" |
1 | 1603 |
! |
1604 |
||
18897 | 1605 |
bitReversed |
1606 |
"swap (i.e. reverse) bits in an integer |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1607 |
i.e. a.b.c.d....x.y.z -> z.y.x...b.a.d.c. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1608 |
Warning: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1609 |
do not use this without care: it depends on the machine's |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1610 |
word size; i.e. a 64bit machine will return a different result as a 32bit machine. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1611 |
Better use one of the bitReversedXX methods. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1612 |
This my vanish or be replaced by something better" |
18897 | 1613 |
|
1614 |
%{ /* NOCONTEXT */ |
|
1615 |
#ifndef __SCHTEAM__ |
|
1616 |
unsigned INT v = __intVal(self); |
|
1617 |
||
1618 |
# if __POINTER_SIZE__ == 8 |
|
1619 |
// swap odd and even bits |
|
1620 |
v = ((v >> 1) & 0x5555555555555555) | ((v & 0x5555555555555555) << 1); |
|
1621 |
// swap consecutive pairs |
|
1622 |
v = ((v >> 2) & 0x3333333333333333) | ((v & 0x3333333333333333) << 2); |
|
18947 | 1623 |
// swap nibbles ... |
18897 | 1624 |
v = ((v >> 4) & 0x0F0F0F0F0F0F0F0F) | ((v & 0x0F0F0F0F0F0F0F0F) << 4); |
1625 |
// swap bytes |
|
1626 |
v = ((v >> 8) & 0x00FF00FF00FF00FF) | ((v & 0x00FF00FF00FF00FF) << 8); |
|
1627 |
// swap 2-byte pairs |
|
1628 |
v = ((v >> 16) & 0x0000FFFF0000FFFF) | (( v & 0x0000FFFF0000FFFF) << 16); |
|
1629 |
// swap 4-byte long pairs |
|
1630 |
v = ((v >> 32) & 0x00000000FFFFFFFF) | (( v & 0x00000000FFFFFFFF) << 32); |
|
1631 |
# else |
|
1632 |
// swap odd and even bits |
|
1633 |
v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1); |
|
1634 |
// swap consecutive pairs |
|
1635 |
v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2); |
|
18947 | 1636 |
// swap nibbles ... |
18897 | 1637 |
v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4); |
1638 |
// swap bytes |
|
1639 |
v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); |
|
1640 |
// swap 2-byte long pairs |
|
1641 |
v = ((v >> 16) & 0x0000FFFF) | ((v & 0x0000FFFF) << 16); |
|
1642 |
# endif |
|
1643 |
||
1644 |
if (v <= _MAX_INT) { |
|
20339 | 1645 |
RETURN ( __mkSmallInteger(v) ); |
18897 | 1646 |
} |
1647 |
RETURN (__MKUINT(v)); |
|
1648 |
#endif /* not __SCHTEAM__ */ |
|
1649 |
%}. |
|
1650 |
^ super bitReversed |
|
18947 | 1651 |
|
18897 | 1652 |
" |
1653 |
2r1001 bitReversed printStringRadix:2 |
|
1654 |
2r100111010011 bitReversed printStringRadix:2 |
|
1655 |
-1 bitReversed printStringRadix:2 |
|
1656 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1657 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1658 |
"Modified (comment): / 27-03-2019 / 18:28:47 / Claus Gittinger" |
18897 | 1659 |
! |
1660 |
||
1661 |
bitReversed16 |
|
18947 | 1662 |
"swap (i.e. reverse) the low 16 bits in an integer |
18897 | 1663 |
the high bits are ignored and clear in the result |
1664 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
|
1665 |
||
1666 |
%{ /* NOCONTEXT */ |
|
1667 |
#ifndef __SCHTEAM__ |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1668 |
unsigned int v = __intVal(self) & 0xFFFF; |
18897 | 1669 |
|
1670 |
// swap odd and even bits |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1671 |
v = ((v >> 1) & 0x5555) | ((v & 0x5555) << 1); |
18897 | 1672 |
// swap consecutive pairs |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1673 |
v = ((v >> 2) & 0x3333) | ((v & 0x3333) << 2); |
18947 | 1674 |
// swap nibbles ... |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1675 |
v = ((v >> 4) & 0x0F0F) | ((v & 0x0F0F) << 4); |
18897 | 1676 |
// swap bytes |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1677 |
v = ((v >> 8) & 0x00FF) | ((v & 0x00FF) << 8); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1678 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1679 |
RETURN ( __mkSmallInteger(v) ); |
18897 | 1680 |
#endif /* not __SCHTEAM__ */ |
1681 |
%}. |
|
1682 |
^ super bitReversed16 |
|
1683 |
||
1684 |
" |
|
1685 |
2r1001 bitReversed16 printStringRadix:2 |
|
1686 |
2r100111010011 bitReversed16 printStringRadix:2 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1687 |
16r1ABCD bitReversed16 printStringRadix:2 |
18897 | 1688 |
-1 bitReversed16 printStringRadix:2 |
1689 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1690 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1691 |
"Modified: / 24-03-2019 / 11:55:34 / Claus Gittinger" |
18897 | 1692 |
! |
1693 |
||
1694 |
bitReversed32 |
|
1695 |
"swap (i.e. reverse) the low 32 bits in an integer |
|
1696 |
the high bits are ignored and clear in the result |
|
1697 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
|
1698 |
||
1699 |
%{ /* NOCONTEXT */ |
|
1700 |
#ifndef __SCHTEAM__ |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1701 |
unsigned int v = __intVal(self) & 0xFFFFFFFF; |
18897 | 1702 |
|
1703 |
// swap odd and even bits |
|
1704 |
v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1); |
|
1705 |
// swap consecutive pairs |
|
1706 |
v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2); |
|
18947 | 1707 |
// swap nibbles ... |
18897 | 1708 |
v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4); |
1709 |
// swap bytes |
|
1710 |
v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1711 |
// swap 16bit words |
18897 | 1712 |
v = ((v >> 16) & 0x0000FFFF) | ((v & 0x0000FFFF) << 16); |
1713 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1714 |
# if __POINTER_SIZE__ == 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1715 |
RETURN ( __mkSmallInteger(v) ); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1716 |
# else |
18897 | 1717 |
if (v <= _MAX_INT) { |
20339 | 1718 |
RETURN ( __mkSmallInteger(v) ); |
18897 | 1719 |
} |
1720 |
RETURN (__MKUINT(v)); |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1721 |
# endif /* not __SCHTEAM__ */ |
18897 | 1722 |
#endif /* not __SCHTEAM__ */ |
1723 |
%}. |
|
1724 |
^ super bitReversed32 |
|
1725 |
||
1726 |
" |
|
1727 |
2r1001 bitReversed32 printStringRadix:2 |
|
1728 |
2r100111010011 bitReversed32 printStringRadix:2 |
|
1729 |
-1 bitReversed32 printStringRadix:2 |
|
1730 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1731 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1732 |
"Modified (format): / 27-03-2019 / 17:54:43 / Claus Gittinger" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1733 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1734 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1735 |
bitReversed64 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1736 |
"swap (i.e. reverse) the low 64 bits in an integer |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1737 |
the high bits are ignored and clear in the result |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1738 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1739 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1740 |
%{ /* NOCONTEXT */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1741 |
#ifndef __SCHTEAM__ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1742 |
unsigned INT v = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1743 |
# if __POINTER_SIZE__ == 8 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1744 |
// swap odd and even bits |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1745 |
v = ((v >> 1) & 0x5555555555555555ULL) | ((v & 0x5555555555555555ULL) << 1); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1746 |
// swap consecutive pairs |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1747 |
v = ((v >> 2) & 0x3333333333333333ULL) | ((v & 0x3333333333333333ULL) << 2); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1748 |
// swap nibbles ... |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1749 |
v = ((v >> 4) & 0x0F0F0F0F0F0F0F0FULL) | ((v & 0x0F0F0F0F0F0F0F0FULL) << 4); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1750 |
// swap bytes |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1751 |
v = ((v >> 8) & 0x00FF00FF00FF00FFULL) | ((v & 0x00FF00FF00FF00FFULL) << 8); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1752 |
// swap 16bit words |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1753 |
v = ((v >> 16) & 0x0000FFFF0000FFFFULL) | ((v & 0x0000FFFF0000FFFFULL) << 16); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1754 |
// swap 32bit words |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1755 |
v = ((v >> 32) & 0x00000000FFFFFFFFULL) | ((v & 0x00000000FFFFFFFFULL) << 32); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1756 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1757 |
if (v <= _MAX_INT) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1758 |
RETURN ( __mkSmallInteger(v) ); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1759 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1760 |
RETURN (__MKUINT(v)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1761 |
# else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1762 |
// swap odd and even bits |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1763 |
v = ((v >> 1) & 0x55555555UL) | ((v & 0x55555555UL) << 1); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1764 |
// swap consecutive pairs |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1765 |
v = ((v >> 2) & 0x33333333UL) | ((v & 0x33333333UL) << 2); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1766 |
// swap nibbles ... |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1767 |
v = ((v >> 4) & 0x0F0F0F0FUL) | ((v & 0x0F0F0F0FUL) << 4); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1768 |
// swap bytes |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1769 |
v = ((v >> 8) & 0x00FF00FFUL) | ((v & 0x00FF00FFUL) << 8); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1770 |
// swap 16bit words |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1771 |
v = ((v >> 16) & 0x0000FFFFUL) | ((v & 0x0000FFFFUL) << 16); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1772 |
// swap 32bit words |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1773 |
RETURN (__MKLARGEINT64(1, (INT)0, (INT)v)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1774 |
# endif |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1775 |
#endif /* not __SCHTEAM__ */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1776 |
%}. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1777 |
^ super bitReversed64 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1778 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1779 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1780 |
2r1001 bitReversed64 printStringRadix:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1781 |
2r100111010011 bitReversed64 printStringRadix:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1782 |
-1 bitReversed64 printStringRadix:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1783 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1784 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1785 |
"Created: / 27-03-2019 / 18:11:11 / Claus Gittinger" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1786 |
"Modified: / 27-03-2019 / 15:25:26 / stefan" |
18897 | 1787 |
! |
1788 |
||
1789 |
bitReversed8 |
|
18947 | 1790 |
"swap (i.e. reverse) the low 8 bits in an integer |
18897 | 1791 |
the high bits are ignored and clear in the result |
1792 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
|
1793 |
||
1794 |
%{ /* NOCONTEXT */ |
|
1795 |
#ifndef __SCHTEAM__ |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1796 |
unsigned int v = __intVal(self) & 0xFF; |
18897 | 1797 |
|
1798 |
// swap odd and even bits |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1799 |
v = ((v >> 1) & 0x55) | ((v & 0x55) << 1); |
18897 | 1800 |
// swap consecutive pairs |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1801 |
v = ((v >> 2) & 0x33) | ((v & 0x33) << 2); |
18947 | 1802 |
// swap nibbles ... |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1803 |
v = ((v >> 4) & 0x0F) | ((v & 0x0F) << 4); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1804 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1805 |
RETURN ( __mkSmallInteger(v) ); |
18897 | 1806 |
#endif /* not __SCHTEAM__ */ |
1807 |
%}. |
|
1808 |
^ super bitReversed8 |
|
1809 |
||
1810 |
" |
|
1811 |
2r1001 bitReversed8 printStringRadix:2 |
|
1812 |
2r10011101 bitReversed8 printStringRadix:2 |
|
1813 |
2r111110011101 bitReversed8 printStringRadix:2 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1814 |
16r1234 bitReversed8 printStringRadix:2 |
18897 | 1815 |
-1 bitReversed8 printStringRadix:2 |
1816 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1817 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1818 |
"Modified: / 24-03-2019 / 11:57:14 / Claus Gittinger" |
18897 | 1819 |
! |
1820 |
||
1 | 1821 |
bitShift:shiftCount |
1822 |
"return the value of the receiver shifted by shiftCount bits; |
|
62 | 1823 |
leftShift if shiftCount > 0; rightShift otherwise. |
8908 | 1824 |
Notice: the result of bitShift: on negative receivers is not |
20339 | 1825 |
defined in the language standard (since the implementation |
1826 |
is free to choose any internal representation for integers). |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1827 |
However, ST/X preserves the sign; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1828 |
i.e. it is an arithmetic shift as long as you stay within the |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1829 |
number of bits supported by the platform." |
1 | 1830 |
|
1831 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1832 |
#ifdef __SCHTEAM__ |
18236 | 1833 |
return context._RETURN( self.bitShift( shiftCount )); |
18227 | 1834 |
#else |
1 | 1835 |
INT bits, count; |
1836 |
||
252 | 1837 |
if (__isSmallInteger(shiftCount)) { |
20339 | 1838 |
bits = __intVal(self); |
1839 |
if (bits == 0) { |
|
1840 |
RETURN (self); |
|
1841 |
} |
|
1842 |
count = __intVal(shiftCount); |
|
1843 |
||
1844 |
if (count > 0) { |
|
1845 |
INT sign = 1; |
|
1846 |
if (bits < 0) { |
|
1847 |
bits = -bits; |
|
1848 |
sign = -1; |
|
1849 |
} |
|
1850 |
/* |
|
1851 |
* a left shift |
|
1852 |
*/ |
|
18227 | 1853 |
# if defined(USE_LONGLONG_FOR_SHIFT) |
20339 | 1854 |
if (count <= N_INT_BITS) { |
1855 |
unsigned LONGLONG result; |
|
1856 |
||
1857 |
result = (unsigned LONGLONG)bits; |
|
1858 |
result <<= count; |
|
1859 |
if (result <= _MAX_INT) { |
|
1860 |
if (sign < 0) { |
|
1861 |
RETURN ( __MKINT(-result) ); |
|
1862 |
} |
|
1863 |
RETURN ( __mkSmallInteger(result) ); |
|
1864 |
} |
|
1865 |
{ |
|
1866 |
RETURN (__MKLARGEINT64(sign, (INT)(result >> 32), (INT)(result & 0xFFFFFFFF))); |
|
1867 |
} |
|
1868 |
} |
|
18227 | 1869 |
# else |
20339 | 1870 |
/* |
1871 |
* check for overflow |
|
1872 |
*/ |
|
1873 |
if (count < (N_INT_BITS-1)) { |
|
1874 |
if (! (bits >> (N_INT_BITS - 1 - count))) { |
|
1875 |
INT result = bits << count; |
|
1876 |
||
1877 |
if (sign < 0) { |
|
1878 |
RETURN ( __MKINT(-result) ); |
|
1879 |
} |
|
1880 |
RETURN ( __mkSmallInteger(result) ); |
|
1881 |
} |
|
1882 |
/* |
|
1883 |
* so, there is an overflow ... |
|
1884 |
* handle it as largeInteger |
|
1885 |
*/ |
|
1886 |
/* FALL THROUGH */ |
|
1887 |
} |
|
18227 | 1888 |
# endif |
20339 | 1889 |
} else { |
1890 |
if (count == 0) { |
|
1891 |
RETURN (self); |
|
1892 |
} |
|
1893 |
||
1894 |
/* |
|
1895 |
* right shifts cannot overflow |
|
1896 |
* |
|
1897 |
* some machines ignore shifts bigger than |
|
1898 |
* the number of bits in an int ... |
|
1899 |
*/ |
|
1900 |
count = -count; |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1901 |
if (count > N_INT_BITS) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1902 |
RETURN (__mkSmallInteger(bits < 0 ? -1 : 0)); |
20339 | 1903 |
} |
1904 |
||
1905 |
RETURN ( __mkSmallInteger(bits >> count) ); |
|
1906 |
} |
|
1 | 1907 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1908 |
#endif /* not __SCHTEAM__ */ |
85 | 1909 |
%}. |
1910 |
(shiftCount isMemberOf:SmallInteger) ifTrue:[ |
|
20339 | 1911 |
^ (LargeInteger value:self) bitShift:shiftCount |
85 | 1912 |
]. |
16448 | 1913 |
^ self bitShift:shiftCount asInteger "/ is this a good idea ? |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1914 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1915 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1916 |
16 bitShift:-1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1917 |
16 bitShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1918 |
16 bitShift:-63 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1919 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1920 |
-16 bitShift:-1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1921 |
-16 bitShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1922 |
-16 bitShift:-63 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1923 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1924 |
4 rightShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1925 |
-4 rightShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1926 |
-4 rightShift:63 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1927 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1928 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1929 |
"Modified (comment): / 03-06-2019 / 17:49:15 / Claus Gittinger" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1930 |
"Modified: / 09-07-2019 / 14:24:07 / Stefan Reise" |
1 | 1931 |
! |
1932 |
||
701 | 1933 |
bitTest:aMask |
5782 | 1934 |
"return true, if any bit from aMask is set in the receiver. |
1935 |
I.e. true, if the bitwise-AND of the receiver and the argument, anInteger |
|
1936 |
is non-0, false otherwise." |
|
1 | 1937 |
|
1938 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1939 |
#ifdef __SCHTEAM__ |
18232 | 1940 |
return context._RETURN( |
20339 | 1941 |
( self.bitAnd( aMask ) == STInteger._0 ) |
1942 |
? STObject.False : STObject.True ); |
|
18232 | 1943 |
/* NOTREACHED */ |
18227 | 1944 |
#else |
701 | 1945 |
/* and all bits except tag */ |
1946 |
if (__isSmallInteger(aMask)) { |
|
20339 | 1947 |
RETURN ( ((INT)self & ((INT)aMask & ~TAG_MASK)) ? true : false ); |
1 | 1948 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1949 |
#endif /* not __SCHTEAM__ */ |
1716
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1950 |
%}. |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
1951 |
aMask class == LargeInteger ifTrue:[ |
20339 | 1952 |
^ (aMask bitAnd:self) ~~ 0 |
4613
a722d598c1bb
faster and:/bitTest: with a largeInteger
Claus Gittinger <cg@exept.de>
parents:
4523
diff
changeset
|
1953 |
]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1954 |
^ self retry:#bitTest: coercing:aMask |
11519 | 1955 |
|
1956 |
" |
|
14632 | 1957 |
2r10001 bitTest:2r00001 |
1958 |
2r10001 bitTest:2r00010 |
|
1959 |
2r10001 bitTest:2r00100 |
|
1960 |
2r10001 bitTest:2r01000 |
|
1961 |
2r10001 bitTest:2r10000 |
|
1962 |
2r10001 bitTest:2r10001 |
|
1963 |
2r10001 bitTest:2r10010 |
|
11519 | 1964 |
" |
1 | 1965 |
! |
1966 |
||
1967 |
bitXor:anInteger |
|
13 | 1968 |
"return the bitwise-exclusive-or of the receiver and the argument, anInteger" |
1 | 1969 |
|
1970 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1971 |
#ifdef __SCHTEAM__ |
18236 | 1972 |
return context._RETURN( self.bitXor( anInteger )); |
18227 | 1973 |
#else |
1 | 1974 |
/* xoring the tags turns it off - or it in again */ |
252 | 1975 |
if (__isSmallInteger(anInteger)) { |
20339 | 1976 |
RETURN ( (OBJ)( ((INT)self ^ (INT)anInteger) | TAG_INT) ); |
1 | 1977 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1978 |
#endif /* not __SCHTEAM__ */ |
1716
52fc3e02c5bd
use new MKLARGEINT64 to generate 64 bit result;
Claus Gittinger <cg@exept.de>
parents:
1695
diff
changeset
|
1979 |
%}. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
1980 |
^ self retry:#bitXor: coercing:anInteger |
1 | 1981 |
! |
1982 |
||
701 | 1983 |
highBit |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
1984 |
"return the bitIndex of the highest bit set. |
19601 | 1985 |
The returned bitIndex starts at 1 for the least significant bit. |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1986 |
Returns 0 if no bit is set. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1987 |
Notice for negative numbers, the returned value is undefined (actually: nonsense), |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1988 |
because for 2's complement representation, conceptionally all high bits are 1. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1989 |
But because we use a sign-magnitude representation for large integers, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1990 |
you'll get the high bit of the absolute magnitude for numbers above the SmallInteger |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
1991 |
range, in contrast to the highbit of the negative number if within the SmallInt range." |
1 | 1992 |
|
1993 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
1994 |
#ifdef __SCHTEAM__ |
18234 | 1995 |
{ |
20339 | 1996 |
long bits = self.longValue(); |
1997 |
int bitNr = 0; |
|
1998 |
||
1999 |
if (bits != 0) { |
|
2000 |
if ((bits & 0xFFFFFFFF00000000L) != 0) { |
|
2001 |
bitNr += 32; bits >>= 32; |
|
2002 |
} |
|
2003 |
if ((bits & 0xFFFF0000L) != 0) { |
|
2004 |
bitNr += 16; bits >>= 16; |
|
2005 |
} |
|
2006 |
if ((bits & 0xFF00) != 0) { |
|
2007 |
bitNr += 8; bits >>= 8; |
|
2008 |
} |
|
2009 |
if ((bits & 0xF0) != 0) { |
|
2010 |
bitNr += 4; bits >>= 4; |
|
2011 |
} |
|
2012 |
if ((bits & 0xC) != 0) { |
|
2013 |
bitNr += 2; bits >>= 2; |
|
2014 |
} |
|
2015 |
if ((bits & 0x2) != 0) { |
|
2016 |
bitNr += 1; bits >>= 1; |
|
2017 |
} |
|
2018 |
bitNr += 1; |
|
2019 |
} |
|
2020 |
return context._RETURN( STInteger._new(bitNr) ); |
|
18234 | 2021 |
} |
2022 |
/* NOTREACHED */ |
|
18227 | 2023 |
#else |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2024 |
unsigned INT bits; |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2025 |
int index; |
701 | 2026 |
|
18227 | 2027 |
# ifdef __BSR |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2028 |
/* |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2029 |
* so much for CISC CPUS: |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2030 |
* 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
|
2031 |
* (but saves a few code-bytes, though) |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2032 |
*/ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2033 |
index = __BSR((INT)self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2034 |
RETURN ( __mkSmallInteger(index) ); |
18227 | 2035 |
# else |
4955
a294a8a19434
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4954
diff
changeset
|
2036 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2037 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2038 |
* this uses the FPU, which counts the bits during the normalization |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2039 |
* and leaves the high bitnumber in the exponent... |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2040 |
* Notice: due to rounding mode details, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2041 |
* this code only works for <=53 bit integers (i.e. only on 32bit machines) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2042 |
* we never use it - it is only here as a hint. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2043 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2044 |
# if (POINTER_SIZE == 4) && defined( USE_IEE_FLOAT_BITS ) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2045 |
union { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2046 |
double ff; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2047 |
int ll[2]; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2048 |
} uu; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2049 |
int bNr; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2050 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2051 |
uu.ff=(double)((INT)(self)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2052 |
bNr = (uu.ll[1]>>20)-1023; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2053 |
return __mkSmallInteger(bNr); // assumes x86 endianness |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2054 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2055 |
# else |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2056 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2057 |
* general fallback; not super-fast, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2058 |
* but fast enough on all machines (better than a bit-masking loop, definitely). |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2059 |
* Notice, the code runs completely in registers, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2060 |
* so it is probably faster than a table-lookup version (at least if its not in the cache) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2061 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2062 |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2063 |
index = 0; |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2064 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2065 |
bits = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2066 |
if (bits == 0) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2067 |
RETURN ( __mkSmallInteger(0) ); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2068 |
} |
18227 | 2069 |
# if __POINTER_SIZE__ == 8 |
4951 | 2070 |
if (bits & 0xFFFFFFFF00000000L) { |
20339 | 2071 |
index += 32; bits >>= 32; |
4951 | 2072 |
} |
18227 | 2073 |
# endif |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2074 |
if (bits & 0xFFFF0000L) { |
20339 | 2075 |
index += 16; bits >>= 16; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2076 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2077 |
if (bits & 0xFF00) { |
20339 | 2078 |
index += 8; bits >>= 8; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2079 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2080 |
if (bits & 0xF0) { |
20339 | 2081 |
index += 4; bits >>= 4; |
1 | 2082 |
} |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2083 |
if (bits & 0xC) { |
20339 | 2084 |
index += 2; bits >>= 2; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2085 |
} |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2086 |
if (bits & 0x2) { |
20339 | 2087 |
index += 1; bits >>= 1; |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2088 |
} |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2089 |
# endif /* not IEE float */ |
18227 | 2090 |
# endif /* no BSR instruction */ |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2091 |
|
8913 | 2092 |
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
|
2093 |
#endif /* not __SCHTEAM__ */ |
18227 | 2094 |
%}. |
2095 |
^ super highBit |
|
2096 |
||
4951 | 2097 |
" |
8908 | 2098 |
2r0 highBit |
2099 |
2r1 highBit |
|
2100 |
2r10 highBit |
|
2101 |
2r100 highBit |
|
2102 |
2r1000 highBit |
|
2103 |
2r100000000000 highBit |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2104 |
|
4951 | 2105 |
((0 to:64) collect:[:s | 1 bitShift:s]) |
20339 | 2106 |
collect:[:n | n highBit] |
4951 | 2107 |
|
2108 |
(((0 to:64) collect:[:s | 1 bitShift:s]) |
|
20339 | 2109 |
collect:[:n | n highBit]) = (1 to:65) |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2110 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2111 |
|
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2112 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2113 |
Time millisecondsToRun:[ |
20339 | 2114 |
1000000 timesRepeat:[ |
2115 |
2r1 highBit |
|
2116 |
] |
|
8908 | 2117 |
] |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2118 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2119 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2120 |
Time millisecondsToRun:[ |
20339 | 2121 |
1000000 timesRepeat:[ |
2122 |
2r1111 highBit |
|
2123 |
] |
|
8908 | 2124 |
] |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2125 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2126 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2127 |
Time millisecondsToRun:[ |
20339 | 2128 |
1000000 timesRepeat:[ |
2129 |
2r11111111111111 highBit |
|
2130 |
] |
|
8908 | 2131 |
] |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2132 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2133 |
" |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2134 |
Time millisecondsToRun:[ |
20339 | 2135 |
1000000 timesRepeat:[ |
2136 |
2r11111111111111111111111111 highBit |
|
2137 |
] |
|
8908 | 2138 |
] |
4951 | 2139 |
" |
2140 |
||
2141 |
" |
|
8908 | 2142 |
2r000100 highBit |
2143 |
2r010100 highBit |
|
2144 |
2r000001 highBit |
|
2145 |
0 highBit |
|
2146 |
SmallInteger maxVal highBit |
|
4951 | 2147 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2148 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2149 |
"Modified: / 05-07-2017 / 16:56:08 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2150 |
"Modified (comment): / 08-06-2019 / 02:07:54 / Claus Gittinger" |
701 | 2151 |
! |
2152 |
||
2153 |
lowBit |
|
2154 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
8908 | 2155 |
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
|
2156 |
Returns 0 if no bit is set." |
701 | 2157 |
|
2158 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2159 |
#ifdef __SCHTEAM__ |
18370 | 2160 |
long bits = self.longValue(); |
2161 |
int index = 0; |
|
2162 |
||
2163 |
if (bits != 0) { |
|
20339 | 2164 |
if ((bits & 0xFFFFFFFFL)==0) { |
2165 |
index += 32; bits >>= 32; |
|
2166 |
} |
|
2167 |
if ((bits & 0xFFFFL)==0) { |
|
2168 |
index += 16; bits >>= 16; |
|
2169 |
} |
|
2170 |
if ((bits & 0xFFL)==0) { |
|
2171 |
index += 8; bits >>= 8; |
|
2172 |
} |
|
2173 |
if ((bits & 0xFL)==0) { |
|
2174 |
index += 4; bits >>= 4; |
|
2175 |
} |
|
2176 |
if ((bits & 0x3L)==0) { |
|
2177 |
index += 2; bits >>= 2; |
|
2178 |
} |
|
2179 |
if ((bits & 0x1L)==0) { |
|
2180 |
index += 1; |
|
2181 |
} |
|
18370 | 2182 |
} |
2183 |
return __c__._RETURN( STInteger._qnew( index ) ); |
|
18227 | 2184 |
#else |
6482 | 2185 |
unsigned INT bits; |
2186 |
int index; |
|
701 | 2187 |
|
1133 | 2188 |
bits = __intVal(self); |
701 | 2189 |
if (bits == 0) { |
20339 | 2190 |
RETURN ( __mkSmallInteger(0) ); |
701 | 2191 |
} |
2431 | 2192 |
|
18227 | 2193 |
# ifdef __BSF |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2194 |
/* |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2195 |
* so much for CISC CPUS: |
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2196 |
* 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
|
2197 |
* (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
|
2198 |
* but saves a few code-bytes, though. |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2199 |
*/ |
6482 | 2200 |
index = __BSF(bits); |
8913 | 2201 |
RETURN ( __mkSmallInteger(index + 1) ); |
18227 | 2202 |
# else |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2203 |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2204 |
index = 1; |
2431 | 2205 |
|
18227 | 2206 |
# if __POINTER_SIZE__ == 8 |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2207 |
if ((bits<<32) == 0) { |
20339 | 2208 |
index += 32; bits >>= 32; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2209 |
} |
18227 | 2210 |
# endif |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2211 |
|
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2212 |
if ((bits & 0xFFFF)==0) { |
20339 | 2213 |
index += 16; bits >>= 16; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2214 |
} |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2215 |
if ((bits & 0xFF)==0) { |
20339 | 2216 |
index += 8; bits >>= 8; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2217 |
} |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2218 |
if ((bits & 0xF)==0) { |
20339 | 2219 |
index += 4; bits >>= 4; |
701 | 2220 |
} |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2221 |
if ((bits & 0x3)==0) { |
20339 | 2222 |
index += 2; bits >>= 2; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2223 |
} |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2224 |
if ((bits & 0x1)==0) { |
20339 | 2225 |
index += 1; |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2226 |
} |
18227 | 2227 |
# endif |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2228 |
|
8913 | 2229 |
RETURN ( __mkSmallInteger(index) ); |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2230 |
#endif /* not __SCHTEAM__ */ |
18227 | 2231 |
%}. |
18370 | 2232 |
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
|
2233 |
|
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2234 |
" |
8908 | 2235 |
0 lowBit |
2236 |
2r0001 lowBit |
|
2237 |
2r0010 lowBit |
|
2238 |
2r0100 lowBit |
|
2239 |
2r1000 lowBit |
|
2240 |
||
2241 |
2r000100 lowBit |
|
2242 |
2r010010 lowBit |
|
2243 |
2r100001 lowBit |
|
2244 |
16r1000 lowBit |
|
2245 |
16r1000000 lowBit |
|
2246 |
16r1000000000000000 lowBit |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2247 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2248 |
Time millisecondsToRun:[ |
20339 | 2249 |
1000000 timesRepeat:[ |
2250 |
2r1000 lowBit |
|
2251 |
] |
|
8908 | 2252 |
] |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2253 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2254 |
Time millisecondsToRun:[ |
20339 | 2255 |
1000000 timesRepeat:[ |
2256 |
2r11110000000 lowBit |
|
2257 |
] |
|
8908 | 2258 |
] |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2259 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2260 |
Time millisecondsToRun:[ |
20339 | 2261 |
1000000 timesRepeat:[ |
2262 |
2r1000000000000 lowBit |
|
2263 |
] |
|
8908 | 2264 |
] |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6417
diff
changeset
|
2265 |
|
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2266 |
Time millisecondsToRun:[ |
20339 | 2267 |
1000000 timesRepeat:[ |
2268 |
2r1000000000000000000000000000 lowBit |
|
2269 |
] |
|
8908 | 2270 |
] |
4954
af47cd256ddf
slightly better lowBit & highBit on intel
Claus Gittinger <cg@exept.de>
parents:
4953
diff
changeset
|
2271 |
" |
5209 | 2272 |
! |
2273 |
||
10972 | 2274 |
rightShift:shiftCount |
2275 |
"return the value of the receiver shifted by shiftCount bits; |
|
2276 |
right shift if shiftCount > 0; left shift otherwise. |
|
2277 |
Notice: the result of bitShift: on negative receivers is not |
|
20339 | 2278 |
defined in the language standard (since the implementation |
2279 |
is free to choose any internal representation for integers). |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2280 |
However, ST/X preserves the sign, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2281 |
i.e. it is an arithmetic shift as long as you stay within the |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2282 |
number of bits supported by the platform." |
10972 | 2283 |
|
2284 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2285 |
#ifdef __SCHTEAM__ |
18227 | 2286 |
#else |
10972 | 2287 |
INT bits, count; |
2288 |
||
2289 |
if (__isSmallInteger(shiftCount)) { |
|
20339 | 2290 |
bits = __intVal(self); |
2291 |
if (bits == 0) { |
|
2292 |
RETURN (self); |
|
2293 |
} |
|
2294 |
||
2295 |
count = __intVal(shiftCount); |
|
2296 |
||
2297 |
if (count < 0) { |
|
2298 |
/* |
|
2299 |
* a left shift |
|
2300 |
*/ |
|
2301 |
count = -count; |
|
18227 | 2302 |
# if defined(USE_LONGLONG_FOR_SHIFT) |
20339 | 2303 |
if (count <= N_INT_BITS) { |
2304 |
unsigned LONGLONG result; |
|
2305 |
||
2306 |
result = (unsigned LONGLONG)bits; |
|
2307 |
result <<= count; |
|
2308 |
if (result <= _MAX_INT) { |
|
2309 |
RETURN ( __mkSmallInteger(result) ); |
|
2310 |
} |
|
2311 |
{ |
|
2312 |
RETURN (__MKLARGEINT64(1, (INT)(result >> 32), (INT)(result & 0xFFFFFFFF))); |
|
2313 |
} |
|
2314 |
} |
|
18227 | 2315 |
# else |
20339 | 2316 |
/* |
2317 |
* check for overflow |
|
2318 |
*/ |
|
2319 |
if (count < (N_INT_BITS-1)) { |
|
2320 |
if (! (bits >> (N_INT_BITS - 1 - count))) { |
|
2321 |
RETURN ( __mkSmallInteger(bits << count) ); |
|
2322 |
} |
|
2323 |
/* |
|
2324 |
* so, there is an overflow ... |
|
2325 |
* handle it as largeInteger |
|
2326 |
*/ |
|
2327 |
/* FALL THROUGH */ |
|
2328 |
} |
|
18227 | 2329 |
# endif |
20339 | 2330 |
} else { |
2331 |
if (count == 0) { |
|
2332 |
RETURN (self); |
|
2333 |
} |
|
2334 |
||
2335 |
/* |
|
2336 |
* right shifts cannot overflow |
|
2337 |
* |
|
2338 |
* some machines ignore shifts bigger than |
|
2339 |
* the number of bits in an int ... |
|
2340 |
*/ |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2341 |
if (count >= (__POINTER_SIZE__*8)) { |
20339 | 2342 |
RETURN (__mkSmallInteger(0)); |
2343 |
} |
|
2344 |
||
2345 |
RETURN ( __mkSmallInteger(bits >> count) ); |
|
2346 |
} |
|
10972 | 2347 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2348 |
#endif /* not __SCHTEAM__ */ |
10972 | 2349 |
%}. |
2350 |
(shiftCount isMemberOf:SmallInteger) ifTrue:[ |
|
20339 | 2351 |
^ (LargeInteger value:self) rightShift:shiftCount |
10972 | 2352 |
]. |
16448 | 2353 |
^ self rightShift:shiftCount asInteger "/ is this a good idea ? |
10972 | 2354 |
|
2355 |
||
2356 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2357 |
16 rightShift:1 |
20339 | 2358 |
16 rightShift:2 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2359 |
16 rightShift:63 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2360 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2361 |
-16 rightShift:1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2362 |
-16 rightShift:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2363 |
-16 rightShift:63 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2364 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2365 |
1 rightShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2366 |
-1 rightShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2367 |
|
20339 | 2368 |
4 rightShift:-2 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2369 |
-4 rightShift:-2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2370 |
-4 rightShift:63 |
10972 | 2371 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2372 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2373 |
"Modified: / 25-08-2017 / 12:30:42 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2374 |
"Modified (comment): / 03-06-2019 / 17:50:22 / Claus Gittinger" |
19051 | 2375 |
! ! |
2376 |
||
2377 |
!SmallInteger methodsFor:'bit operators - indexed'! |
|
2378 |
||
2379 |
bitAt:anIntegerIndex |
|
2380 |
"return the value of the index's bit (index starts at 1) as 0 or 1. |
|
2381 |
Notice: the result of bitAt: on negative receivers is not |
|
20339 | 2382 |
defined in the language standard (since the implementation |
2383 |
is free to choose any internal representation for integers)" |
|
19051 | 2384 |
|
2385 |
%{ /* NOCONTEXT */ |
|
2386 |
#ifdef __SCHTEAM__ |
|
2387 |
return context._RETURN( self.bitAt(anIntegerIndex)); |
|
2388 |
#else |
|
2389 |
if (__isSmallInteger(anIntegerIndex)) { |
|
20339 | 2390 |
INT idx = __smallIntegerVal(anIntegerIndex); |
2391 |
if (idx > 0) { |
|
2392 |
if (idx > N_INT_BITS) { |
|
2393 |
RETURN(__mkSmallInteger(0)); |
|
2394 |
} |
|
2395 |
RETURN((__smallIntegerVal(self) & ((INT)1 << (idx-1))) ? __mkSmallInteger(1) : __mkSmallInteger(0)); |
|
2396 |
} |
|
19051 | 2397 |
} |
2398 |
#endif /* not __SCHTEAM__ */ |
|
2399 |
%}. |
|
2400 |
||
2401 |
^ SubscriptOutOfBoundsError |
|
20339 | 2402 |
raiseRequestWith:anIntegerIndex |
2403 |
errorString:'index out of bounds' |
|
19051 | 2404 |
|
2405 |
" |
|
2406 |
16r000000001 bitAt:0 -> error |
|
2407 |
16r000000001 bitAt:1 |
|
2408 |
16r000000001 bitAt:2 |
|
2409 |
16r000008000 bitAt:16 |
|
2410 |
16r000800000 bitAt:24 |
|
2411 |
16r008000000 bitAt:28 |
|
2412 |
16r010000000 bitAt:29 |
|
2413 |
16r020000000 bitAt:30 |
|
2414 |
16r040000000 bitAt:31 |
|
2415 |
16r080000000 bitAt:32 |
|
2416 |
16r100000000 bitAt:33 |
|
2417 |
" |
|
2418 |
||
2419 |
" Smalltalk implementation: |
|
2420 |
|mask| |
|
2421 |
||
2422 |
anIntegerIndex <= 0 ifTrue:[ |
|
20339 | 2423 |
^ SubscriptOutOfBoundsSignal |
2424 |
raiseRequestWith:anIntegerIndex |
|
2425 |
errorString:'index out of bounds' |
|
19051 | 2426 |
]. |
2427 |
(anIntegerIndex > SmallInteger maxBits) ifTrue:[^ 0]. |
|
2428 |
mask := 1 bitShift:(anIntegerIndex - 1). |
|
2429 |
((self bitAnd:mask) == 0) ifTrue:[^ 0]. |
|
2430 |
^ 1 |
|
2431 |
" |
|
2432 |
! |
|
2433 |
||
2434 |
clearBit:anInteger |
|
2435 |
"return a new integer where the specified bit is off. |
|
2436 |
Bits are counted from 1 starting with the least significant. |
|
2437 |
The method's name may be misleading: the receiver is not changed, |
|
2438 |
but a new number is returned. Should be named #withBitCleared:" |
|
2439 |
||
2440 |
%{ /* NOCONTEXT */ |
|
2441 |
#ifndef __SCHTEAM__ |
|
2442 |
if (__isSmallInteger(anInteger)) { |
|
20339 | 2443 |
int index = __intVal(anInteger); |
2444 |
||
2445 |
if (index > 0) { |
|
19051 | 2446 |
# if __POINTER_SIZE__ == 8 |
20339 | 2447 |
if (index <= 62) |
19051 | 2448 |
# else |
20339 | 2449 |
if (index <= 30) |
19051 | 2450 |
# endif |
20339 | 2451 |
{ |
2452 |
INT mask = __MASKSMALLINT( ((INT)1 << (index-1))); |
|
2453 |
||
2454 |
RETURN ( ((OBJ) ((INT)self & ~(INT)mask)) ); |
|
2455 |
} |
|
2456 |
RETURN (self); /* nothing to do ... */ |
|
2457 |
} |
|
19051 | 2458 |
} |
2459 |
#endif /* not __SCHTEAM__ */ |
|
2460 |
%}. |
|
2461 |
^ super clearBit:anInteger |
|
2462 |
||
2463 |
" |
|
2464 |
(16r401 clearBit:1 ) hexPrintString |
|
2465 |
(16r401 clearBit:0 ) hexPrintString |
|
2466 |
(16r3fffffff clearBit:1) hexPrintString |
|
2467 |
(16r3fffffff clearBit:29) hexPrintString |
|
2468 |
(16r3fffffff clearBit:30) hexPrintString |
|
2469 |
(16r3fffffff clearBit:31) hexPrintString |
|
2470 |
(16r3fffffff bitAt:29) hexPrintString |
|
2471 |
(16r3fffffff bitAt:30) hexPrintString |
|
2472 |
(16r3fffffff bitAt:31) hexPrintString |
|
2473 |
(16r40000001 clearBit:1) hexPrintString |
|
2474 |
(16rF0000001 clearBit:29) hexPrintString |
|
2475 |
(16rF0000001 clearBit:30) hexPrintString |
|
2476 |
(16rF0000001 clearBit:31) hexPrintString |
|
2477 |
(16rF0000001 clearBit:32) hexPrintString |
|
2478 |
(16r1F0000001 clearBit:33) hexPrintString |
|
2479 |
" |
|
2480 |
! |
|
2481 |
||
2482 |
invertBit:anInteger |
|
2483 |
"return a new number where the specified bit is inverted. |
|
2484 |
Bits are counted from 1 starting with the least significant. |
|
2485 |
The method's name may be misleading: the receiver is not changed, |
|
2486 |
but a new number is returned. Should be named #withBitInverted:" |
|
2487 |
||
2488 |
%{ /* NOCONTEXT */ |
|
2489 |
#ifndef __SCHTEAM__ |
|
2490 |
if (__isSmallInteger(anInteger)) { |
|
20339 | 2491 |
int index = __intVal(anInteger); |
2492 |
||
2493 |
if (index > 0) { |
|
19051 | 2494 |
# if __POINTER_SIZE__ == 8 |
20339 | 2495 |
if (index <= 62) |
19051 | 2496 |
# else |
20339 | 2497 |
if (index <= 30) |
19051 | 2498 |
# endif |
20339 | 2499 |
{ |
2500 |
INT mask = __MASKSMALLINT((INT)1 << (index-1)); |
|
2501 |
||
2502 |
RETURN ( ((OBJ) ((INT)self ^ (INT)mask)) ); |
|
2503 |
} |
|
2504 |
} |
|
19051 | 2505 |
} |
2506 |
#endif /* not __SCHTEAM__ */ |
|
2507 |
%}. |
|
2508 |
^ super invertBit:anInteger |
|
2509 |
||
2510 |
" |
|
2511 |
(16r401 invertBit:2 ) hexPrintString |
|
2512 |
(16r401 invertBit:1 ) hexPrintString |
|
2513 |
(16r30000000 invertBit:1) hexPrintString |
|
2514 |
(16r40000000 invertBit:0) hexPrintString |
|
2515 |
(16r0 invertBit:29) hexPrintString |
|
2516 |
(16r0 invertBit:30) hexPrintString |
|
2517 |
(16r0 invertBit:31) hexPrintString |
|
2518 |
(16r0 invertBit:32) hexPrintString |
|
2519 |
(16r0 invertBit:33) hexPrintString |
|
2520 |
(16r0 invertBit:100) hexPrintString |
|
2521 |
" |
|
10972 | 2522 |
! |
2523 |
||
5209 | 2524 |
setBit:anInteger |
8937 | 2525 |
"return a new integer where the specified bit is on. |
5209 | 2526 |
Bits are counted from 1 starting with the least significant. |
18897 | 2527 |
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
|
2528 |
but a new number is returned. Should be named #withBitSet:" |
5209 | 2529 |
|
2530 |
%{ /* NOCONTEXT */ |
|
18897 | 2531 |
#ifndef __SCHTEAM__ |
18227 | 2532 |
if (__isSmallInteger(anInteger)) { |
20339 | 2533 |
int index = __intVal(anInteger); |
2534 |
||
2535 |
if (index > 0) { |
|
18227 | 2536 |
# if __POINTER_SIZE__ == 8 |
20339 | 2537 |
if (index <= 62) |
18227 | 2538 |
# else |
20339 | 2539 |
if (index <= 30) |
18227 | 2540 |
# endif |
20339 | 2541 |
{ |
2542 |
INT mask = __MASKSMALLINT((INT)1 << (index-1)); |
|
2543 |
||
2544 |
RETURN ( ((OBJ) ((INT)self | (INT)mask)) ); |
|
2545 |
} |
|
2546 |
} |
|
5209 | 2547 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2548 |
#endif /* not __SCHTEAM__ */ |
5209 | 2549 |
%}. |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5458
diff
changeset
|
2550 |
^ super setBit:anInteger |
5209 | 2551 |
|
2552 |
" |
|
8908 | 2553 |
(16r401 setBit:2 ) hexPrintString |
2554 |
(16r30000000 setBit:1) hexPrintString |
|
2555 |
(16r40000000 setBit:0) hexPrintString |
|
2556 |
(16r0 setBit:29) hexPrintString |
|
2557 |
(16r0 setBit:30) hexPrintString |
|
2558 |
(16r0 setBit:31) hexPrintString |
|
2559 |
(16r0 setBit:32) hexPrintString |
|
2560 |
(16r0 setBit:33) hexPrintString |
|
2561 |
(16r0 setBit:100) hexPrintString |
|
5209 | 2562 |
" |
6417 | 2563 |
! ! |
2564 |
||
1 | 2565 |
!SmallInteger methodsFor:'byte access'! |
2566 |
||
18227 | 2567 |
byteSwapped |
13991 | 2568 |
"lsb -> msb; |
2569 |
i.e. a.b.c.d -> d.c.b.a" |
|
2570 |
||
15910 | 2571 |
SmallInteger maxBytes == 8 ifTrue:[ |
20339 | 2572 |
^ self byteSwapped64 |
15910 | 2573 |
] ifFalse:[ |
20339 | 2574 |
^ self byteSwapped32 |
15910 | 2575 |
]. |
2576 |
||
2577 |
" |
|
18227 | 2578 |
16r11223344 byteSwapped hexPrintString |
2579 |
16r44332211 byteSwapped hexPrintString |
|
15910 | 2580 |
" |
2581 |
||
2582 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
2583 |
! |
|
2584 |
||
2585 |
byteSwapped16 |
|
2586 |
"for 16bit values only: |
|
2587 |
lsb -> msb; |
|
2588 |
i.e. a.b -> b.a" |
|
2589 |
||
2590 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2591 |
#ifndef __SCHTEAM__ |
15910 | 2592 |
unsigned INT v = __intVal(self); |
2593 |
unsigned INT swapped; |
|
2594 |
||
2595 |
swapped = ((v>>8)&0xFF) | ((v & 0xFF)<<8); |
|
2596 |
RETURN (__mkSmallInteger(swapped)); |
|
18897 | 2597 |
#endif /* not __SCHTEAM__ */ |
15910 | 2598 |
%}. |
18227 | 2599 |
^ super byteSwapped16 |
15910 | 2600 |
|
2601 |
" |
|
18227 | 2602 |
16r1122 byteSwapped16 hexPrintString |
2603 |
16r2211 byteSwapped16 hexPrintString |
|
2604 |
16r332211 byteSwapped16 hexPrintString |
|
15910 | 2605 |
" |
2606 |
! |
|
2607 |
||
2608 |
byteSwapped32 |
|
2609 |
"for 32bit values only: |
|
2610 |
lsb -> msb; |
|
2611 |
i.e. a.b.c.d -> d.c.b.a" |
|
2612 |
||
13991 | 2613 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2614 |
#ifndef __SCHTEAM__ |
15910 | 2615 |
unsigned INT v = __intVal(self); |
2616 |
unsigned INT swapped; |
|
2617 |
||
18227 | 2618 |
# undef HAVE_BSWAP |
2619 |
# if __POINTER_SIZE__ == 4 |
|
2620 |
||
2621 |
# if defined(USE_BSWAP) && defined(__BORLANDC__) |
|
15910 | 2622 |
# define HAVE_BSWAP |
2623 |
||
2624 |
_asm { |
|
20339 | 2625 |
mov eax, v |
2626 |
bswap eax |
|
2627 |
mov swapped, eax |
|
15910 | 2628 |
}; |
18227 | 2629 |
# endif |
2630 |
# if defined(USE_BSWAP) && defined(__VISUALC__) |
|
15910 | 2631 |
# define HAVE_BSWAP |
2632 |
||
2633 |
_asm { |
|
20339 | 2634 |
mov eax, v |
2635 |
xchg al, ah |
|
2636 |
rol eax, 16 |
|
2637 |
xchg al, ah |
|
2638 |
mov swapped, eax |
|
15910 | 2639 |
}; |
18227 | 2640 |
# endif |
2641 |
# if defined(USE_BSWAP) && defined(__GNUC__) |
|
15910 | 2642 |
# define HAVE_BSWAP |
2643 |
||
16537 | 2644 |
asm("movl %1, %%eax \n\ |
20339 | 2645 |
bswap %%eax \n\ |
2646 |
movl %%eax, %0 \n\ |
|
2647 |
" |
|
2648 |
: "=rm" (swapped) |
|
2649 |
: "rm" (v)); |
|
18227 | 2650 |
# endif |
2651 |
# endif /* __POINTER_SIZE__ == 4 */ |
|
2652 |
||
2653 |
# if __POINTER_SIZE__ == 8 |
|
15910 | 2654 |
v &= 0xFFFFFFFF; |
2655 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
2656 |
# if defined(__x86_64__) && defined(__GNUC__) && !defined(__CLANG__) |
15910 | 2657 |
# define HAVE_BSWAP |
19133 | 2658 |
|
16537 | 2659 |
asm("movq %1, %%rax \n\ |
20339 | 2660 |
bswap %%eax \n\ |
2661 |
movq %%rax, %0 \n\ |
|
2662 |
" |
|
2663 |
: "=rm" (swapped) |
|
2664 |
: "rm" (v)); |
|
18227 | 2665 |
# endif |
15910 | 2666 |
# endif |
18227 | 2667 |
|
2668 |
# ifndef HAVE_BSWAP |
|
15910 | 2669 |
swapped = ((v>>24) | ((v>>8)&0xFF00) | ((v & 0xFF00)<<8) | ((v & 0xFF)<<24)); |
18227 | 2670 |
# endif |
15910 | 2671 |
|
2672 |
RETURN (__MKUINT(swapped)); |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2673 |
#endif /* not __SCHTEAM__ */ |
15910 | 2674 |
%}. |
18227 | 2675 |
^ super byteSwapped32 |
15910 | 2676 |
|
2677 |
" |
|
18227 | 2678 |
16r11223344 byteSwapped32 hexPrintString |
2679 |
16r44332211 byteSwapped32 hexPrintString |
|
15910 | 2680 |
" |
2681 |
||
2682 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
2683 |
! |
|
2684 |
||
2685 |
byteSwapped64 |
|
2686 |
"for 64bit values only: |
|
2687 |
lsb -> msb; |
|
2688 |
i.e. a.b.c.d.e.f.g.h -> h.g.f.e.d.c.b.a" |
|
2689 |
||
2690 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2691 |
#ifndef __SCHTEAM__ |
15910 | 2692 |
unsigned INT v = __intVal(self); |
2693 |
unsigned INT swapped; |
|
13991 | 2694 |
|
18227 | 2695 |
# if __POINTER_SIZE__ == 4 |
2696 |
// xxxxxxxx 00000000 00000000 00000000 -> 00000000 00000000 00000000 xxxxxxxx |
|
15910 | 2697 |
// xxxxxxxx xxxxxxxx |
2698 |
// xxxxxxxx xxxxxxxx |
|
2699 |
// xxxxxxxx xxxxxxxx |
|
2700 |
swapped = (v>>24) | ((v>>8)&0xFF00) | ((v & 0xFF00)<<8) | ((v & 0xFF)<<24); |
|
16521 | 2701 |
RETURN(__MKLARGEINT64(1, 0, swapped)); |
18227 | 2702 |
# else |
2703 |
// xxxxxxxx 00000000 00000000 00000000 -> 00000000 00000000 00000000 xxxxxxxx |
|
15910 | 2704 |
// xxxxxxxx xxxxxxxx |
2705 |
// xxxxxxxx xxxxxxxx |
|
2706 |
// xxxxxxxx xxxxxxxx |
|
2707 |
swapped = (v>>56) | ((v>>40)&0xFF00) | ((v>>24) & 0xFF0000) | ((v>>8) & 0xFF000000) |
|
20339 | 2708 |
| ((v & 0xFF000000)<<8) | ((v & 0x00FF0000)<<24) | ((v & 0x0000FF00)<<40) |
2709 |
| ((v & 0xFF)<<56); |
|
18227 | 2710 |
# endif |
2711 |
RETURN(__MKUINT( swapped )); |
|
18897 | 2712 |
#endif /* not __SCHTEAM__ */ |
13991 | 2713 |
%}. |
18227 | 2714 |
^ super byteSwapped64 |
13991 | 2715 |
|
2716 |
" |
|
18227 | 2717 |
16r11223344 byteSwapped64 hexPrintString |
2718 |
16r44332211 byteSwapped64 hexPrintString |
|
13991 | 2719 |
" |
2720 |
||
2721 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
2722 |
! |
|
2723 |
||
1 | 2724 |
digitAt:index |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2725 |
"return 8 bits of the absolute value, starting at byte index. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2726 |
See also digitByteAt:" |
1 | 2727 |
|
2728 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2729 |
#ifdef __SCHTEAM__ |
18410 | 2730 |
int idx0Based = index.intValue() - 1; |
2731 |
||
2732 |
if (idx0Based <= 7) { |
|
20339 | 2733 |
long myVal = self.longValue(); |
2734 |
if (myVal < 0) { |
|
2735 |
myVal = -myVal; |
|
2736 |
} |
|
2737 |
int byteVal = (int)((myVal >> (idx0Based * 8)) & 0xFF); |
|
2738 |
return __c__._RETURN( STInteger._qnew(byteVal) ); |
|
18410 | 2739 |
} |
2740 |
if (idx0Based > 0) { |
|
20339 | 2741 |
return __c__._RETURN( STInteger._0 ); |
18227 | 2742 |
} |
2743 |
#else |
|
357 | 2744 |
REGISTER INT val; |
2745 |
INT idx; |
|
1 | 2746 |
|
252 | 2747 |
if (__isSmallInteger(index)) { |
20339 | 2748 |
val = __intVal(self); |
2749 |
if (val < 0) |
|
2750 |
val = -val; |
|
2751 |
switch (idx = __intVal(index)) { |
|
2752 |
case 1: |
|
2753 |
break; |
|
2754 |
case 2: |
|
2755 |
val = (val >> 8); |
|
2756 |
break; |
|
2757 |
case 3: |
|
2758 |
val = (val >> 16); |
|
2759 |
break; |
|
2760 |
case 4: |
|
2761 |
val = (val >> 24); |
|
2762 |
break; |
|
18227 | 2763 |
# if __POINTER_SIZE__ == 8 |
20339 | 2764 |
case 5: |
2765 |
val = (val >> 32); |
|
2766 |
break; |
|
2767 |
case 6: |
|
2768 |
val = (val >> 40); |
|
2769 |
break; |
|
2770 |
case 7: |
|
2771 |
val = (val >> 48); |
|
2772 |
break; |
|
2773 |
case 8: |
|
2774 |
val = (val >> 56); |
|
2775 |
break; |
|
18227 | 2776 |
# endif |
20339 | 2777 |
default: |
2778 |
if (idx < 1) |
|
2779 |
goto bad; /* sorry */ |
|
2780 |
RETURN (__mkSmallInteger(0)); |
|
2781 |
} |
|
2782 |
RETURN ( __mkSmallInteger( val & 0xFF) ); |
|
1 | 2783 |
} |
357 | 2784 |
bad: ; |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2785 |
#endif /* not __SCHTEAM__ */ |
77 | 2786 |
%}. |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2787 |
index <= 0 ifTrue:[ |
20339 | 2788 |
" |
2789 |
index less than 1 - not allowed |
|
2790 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2791 |
^ SubscriptOutOfBoundsError |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2792 |
raiseRequestWith:index |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2793 |
errorString:'index out of bounds' |
77 | 2794 |
]. |
2795 |
^ 0 |
|
62 | 2796 |
|
357 | 2797 |
" |
2798 |
(16r12345678 digitAt:1) printStringRadix:16 |
|
2799 |
(16r12345678 digitAt:3) printStringRadix:16 |
|
2800 |
(16r12345678 digitAt:15) printStringRadix:16 |
|
2801 |
(16r12345678 digitAt:0) printStringRadix:16 |
|
2802 |
(16r12345678 digitAt:-10) printStringRadix:16 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2803 |
(16r12345678 digitAt:-10) printStringRadix:16 |
357 | 2804 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2805 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
2806 |
"Modified: / 29-01-2020 / 17:26:59 / Stefan Vogel" |
701 | 2807 |
! |
2808 |
||
3892 | 2809 |
digitByteAt:index |
2810 |
"return 8 bits of my signed value, starting at byte index. |
|
2811 |
For positive receivers, this is the same as #digitAt:; |
|
2812 |
for negative ones, the actual bit representation is returned." |
|
2813 |
||
2814 |
%{ /* NOCONTEXT */ |
|
18410 | 2815 |
#ifdef __SCHTEAM__ |
2816 |
int idx0Based = index.intValue() - 1; |
|
2817 |
long myVal = self.longValue(); |
|
2818 |
||
2819 |
if (idx0Based <= 7) { |
|
20339 | 2820 |
int byteVal = (int)((myVal >> (idx0Based * 8)) & 0xFF); |
2821 |
return __c__._RETURN( STInteger._qnew(byteVal) ); |
|
18410 | 2822 |
} |
2823 |
if (idx0Based > 0) { |
|
20339 | 2824 |
if (myVal < 0) { |
2825 |
return __c__._RETURN( STInteger._M1 ); |
|
2826 |
} else { |
|
2827 |
return __c__._RETURN( STInteger._0 ); |
|
2828 |
} |
|
18410 | 2829 |
} |
2830 |
#else |
|
3892 | 2831 |
REGISTER INT val; |
2832 |
INT idx; |
|
2833 |
||
2834 |
if (__isSmallInteger(index)) { |
|
20339 | 2835 |
val = __intVal(self); |
2836 |
switch (idx = __intVal(index)) { |
|
2837 |
case 1: |
|
2838 |
break; |
|
2839 |
case 2: |
|
2840 |
val = (val >> 8); |
|
2841 |
break; |
|
2842 |
case 3: |
|
2843 |
val = (val >> 16); |
|
2844 |
break; |
|
2845 |
case 4: |
|
2846 |
val = (val >> 24); |
|
2847 |
break; |
|
18227 | 2848 |
# if __POINTER_SIZE__ == 8 |
20339 | 2849 |
case 5: |
2850 |
val = (val >> 32); |
|
2851 |
break; |
|
2852 |
case 6: |
|
2853 |
val = (val >> 40); |
|
2854 |
break; |
|
2855 |
case 7: |
|
2856 |
val = (val >> 48); |
|
2857 |
break; |
|
2858 |
case 8: |
|
2859 |
val = (val >> 56); |
|
2860 |
break; |
|
18227 | 2861 |
# endif |
20339 | 2862 |
default: |
2863 |
if (idx < 1) |
|
2864 |
goto bad; /* sorry */ |
|
2865 |
if (val < 0) { |
|
2866 |
RETURN (__mkSmallInteger(0xFF)); |
|
2867 |
} |
|
2868 |
RETURN (__mkSmallInteger(0)); |
|
2869 |
} |
|
2870 |
RETURN ( __mkSmallInteger( val & 0xFF) ); |
|
3892 | 2871 |
} |
2872 |
bad: ; |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
2873 |
#endif /* not __SCHTEAM__ */ |
3892 | 2874 |
%}. |
2875 |
index > 0 ifFalse:[ |
|
20339 | 2876 |
" |
2877 |
index less than 1 - not allowed |
|
2878 |
" |
|
2879 |
^ self primitiveFailed |
|
3892 | 2880 |
]. |
2881 |
^ 0 |
|
2882 |
||
2883 |
" |
|
8908 | 2884 |
(10 digitByteAt:1) printStringRadix:16 |
2885 |
(10 digitByteAt:3) printStringRadix:16 |
|
3892 | 2886 |
(-10 digitByteAt:1) printStringRadix:16 |
2887 |
(-10 digitByteAt:3) printStringRadix:16 |
|
2888 |
" |
|
2889 |
! |
|
2890 |
||
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2891 |
digitBytes |
18410 | 2892 |
"return a byteArray filled with the receiver's bits |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2893 |
(8 bits of the absolute value per element), |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2894 |
least significant byte is first" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2895 |
|
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2896 |
|absValue |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2897 |
b1 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2898 |
b2 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2899 |
b3 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2900 |
b4 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2901 |
b5 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2902 |
b6 "{ Class: SmallInteger }" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2903 |
b7 "{ Class: SmallInteger }" digitByteArray| |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2904 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2905 |
" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2906 |
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
|
2907 |
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
|
2908 |
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
|
2909 |
" |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2910 |
self == 0 ifTrue: [ |
20339 | 2911 |
^ ByteArray with:0. |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2912 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2913 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2914 |
self < 0 ifTrue: [ |
20339 | 2915 |
absValue := self negated |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2916 |
] ifFalse: [ |
20339 | 2917 |
absValue := self. |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2918 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2919 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2920 |
b1 := absValue bitAnd:16rFF. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2921 |
absValue := absValue bitShift:-8. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2922 |
absValue == 0 ifTrue:[ |
20339 | 2923 |
digitByteArray := ByteArray with:b1 |
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2924 |
] ifFalse:[ |
20339 | 2925 |
b2 := absValue bitAnd:16rFF. |
2926 |
absValue := absValue bitShift:-8. |
|
2927 |
absValue == 0 ifTrue:[ |
|
2928 |
digitByteArray := ByteArray with:b1 with:b2 |
|
2929 |
] ifFalse:[ |
|
2930 |
b3 := absValue bitAnd:16rFF. |
|
2931 |
absValue := absValue bitShift:-8. |
|
2932 |
absValue == 0 ifTrue:[ |
|
2933 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
2934 |
] ifFalse:[ |
|
2935 |
b4 := absValue bitAnd:16rFF. |
|
2936 |
absValue := absValue bitShift:-8. |
|
2937 |
absValue == 0 ifTrue:[ |
|
2938 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 |
|
2939 |
] ifFalse:[ |
|
2940 |
b5 := absValue bitAnd:16rFF. |
|
2941 |
absValue := absValue bitShift:-8. |
|
2942 |
absValue == 0 ifTrue:[ |
|
2943 |
digitByteArray := ByteArray new:5. |
|
2944 |
digitByteArray at:1 put:b1. |
|
2945 |
digitByteArray at:2 put:b2. |
|
2946 |
digitByteArray at:3 put:b3. |
|
2947 |
digitByteArray at:4 put:b4. |
|
2948 |
digitByteArray at:5 put:b5. |
|
2949 |
] ifFalse:[ |
|
2950 |
b6 := absValue bitAnd:16rFF. |
|
2951 |
absValue := absValue bitShift:-8. |
|
2952 |
absValue == 0 ifTrue:[ |
|
2953 |
digitByteArray := ByteArray new:6. |
|
2954 |
digitByteArray at:1 put:b1. |
|
2955 |
digitByteArray at:2 put:b2. |
|
2956 |
digitByteArray at:3 put:b3. |
|
2957 |
digitByteArray at:4 put:b4. |
|
2958 |
digitByteArray at:5 put:b5. |
|
2959 |
digitByteArray at:6 put:b6. |
|
2960 |
] ifFalse:[ |
|
2961 |
b7 := absValue bitAnd:16rFF. |
|
2962 |
absValue := absValue bitShift:-8. |
|
2963 |
absValue == 0 ifTrue:[ |
|
2964 |
digitByteArray := ByteArray new:7. |
|
2965 |
digitByteArray at:1 put:b1. |
|
2966 |
digitByteArray at:2 put:b2. |
|
2967 |
digitByteArray at:3 put:b3. |
|
2968 |
digitByteArray at:4 put:b4. |
|
2969 |
digitByteArray at:5 put:b5. |
|
2970 |
digitByteArray at:6 put:b6. |
|
2971 |
digitByteArray at:7 put:b7. |
|
2972 |
] ifFalse:[ |
|
2973 |
digitByteArray := ByteArray new:8. |
|
2974 |
digitByteArray at:1 put:b1. |
|
2975 |
digitByteArray at:2 put:b2. |
|
2976 |
digitByteArray at:3 put:b3. |
|
2977 |
digitByteArray at:4 put:b4. |
|
2978 |
digitByteArray at:5 put:b5. |
|
2979 |
digitByteArray at:6 put:b6. |
|
2980 |
digitByteArray at:7 put:b7. |
|
2981 |
digitByteArray at:8 put:absValue. |
|
2982 |
] |
|
2983 |
] |
|
2984 |
] |
|
2985 |
] |
|
2986 |
] |
|
2987 |
] |
|
10487
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2988 |
]. |
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2989 |
|
f6287f4b83c9
Redefined #digitBytes without using an intermediate LargeInteger
Stefan Vogel <sv@exept.de>
parents:
10342
diff
changeset
|
2990 |
^ digitByteArray |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2991 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2992 |
" |
15568 | 2993 |
16r12 digitBytes hexPrintString |
2994 |
16r1234 digitBytes hexPrintString |
|
2995 |
16r12345678 digitBytes hexPrintString |
|
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2996 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2997 |
! |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
2998 |
|
15568 | 2999 |
digitBytesMSB |
18410 | 3000 |
"return a byteArray filled with the receiver's bits |
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
3001 |
(8 bits of the absolute value per element), |
15568 | 3002 |
most significant byte is first" |
3003 |
||
3004 |
|absValue |
|
3005 |
b1 "{ Class: SmallInteger }" |
|
3006 |
b2 "{ Class: SmallInteger }" |
|
3007 |
b3 "{ Class: SmallInteger }" |
|
3008 |
b4 "{ Class: SmallInteger }" |
|
3009 |
b5 "{ Class: SmallInteger }" |
|
3010 |
b6 "{ Class: SmallInteger }" |
|
3011 |
b7 "{ Class: SmallInteger }" digitByteArray| |
|
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
3012 |
|
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
3013 |
" |
15568 | 3014 |
could have simply created a 4-byte largeinteger and normalize it. |
3015 |
The code below does the normalize right away, avoiding the |
|
3016 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
3017 |
" |
|
3018 |
self == 0 ifTrue: [ |
|
20339 | 3019 |
^ ByteArray with:0. |
15568 | 3020 |
]. |
3021 |
||
3022 |
self < 0 ifTrue: [ |
|
20339 | 3023 |
absValue := self negated |
15568 | 3024 |
] ifFalse: [ |
20339 | 3025 |
absValue := self. |
15568 | 3026 |
]. |
3027 |
||
3028 |
b1 := absValue bitAnd:16rFF. |
|
3029 |
absValue := absValue bitShift:-8. |
|
3030 |
absValue == 0 ifTrue:[ |
|
20339 | 3031 |
digitByteArray := ByteArray with:b1 |
15568 | 3032 |
] ifFalse:[ |
20339 | 3033 |
b2 := absValue bitAnd:16rFF. |
3034 |
absValue := absValue bitShift:-8. |
|
3035 |
absValue == 0 ifTrue:[ |
|
3036 |
digitByteArray := ByteArray with:b2 with:b1 |
|
3037 |
] ifFalse:[ |
|
3038 |
b3 := absValue bitAnd:16rFF. |
|
3039 |
absValue := absValue bitShift:-8. |
|
3040 |
absValue == 0 ifTrue:[ |
|
3041 |
digitByteArray := ByteArray with:b3 with:b2 with:b1 |
|
3042 |
] ifFalse:[ |
|
3043 |
b4 := absValue bitAnd:16rFF. |
|
3044 |
absValue := absValue bitShift:-8. |
|
3045 |
absValue == 0 ifTrue:[ |
|
3046 |
digitByteArray := ByteArray with:b4 with:b3 with:b2 with:b1 |
|
3047 |
] ifFalse:[ |
|
3048 |
b5 := absValue bitAnd:16rFF. |
|
3049 |
absValue := absValue bitShift:-8. |
|
3050 |
absValue == 0 ifTrue:[ |
|
3051 |
digitByteArray := ByteArray new:5. |
|
3052 |
digitByteArray at:1 put:b5. |
|
3053 |
digitByteArray at:2 put:b4. |
|
3054 |
digitByteArray at:3 put:b3. |
|
3055 |
digitByteArray at:4 put:b2. |
|
3056 |
digitByteArray at:5 put:b1. |
|
3057 |
] ifFalse:[ |
|
3058 |
b6 := absValue bitAnd:16rFF. |
|
3059 |
absValue := absValue bitShift:-8. |
|
3060 |
absValue == 0 ifTrue:[ |
|
3061 |
digitByteArray := ByteArray new:6. |
|
3062 |
digitByteArray at:1 put:b6. |
|
3063 |
digitByteArray at:2 put:b5. |
|
3064 |
digitByteArray at:3 put:b4. |
|
3065 |
digitByteArray at:4 put:b3. |
|
3066 |
digitByteArray at:5 put:b2. |
|
3067 |
digitByteArray at:6 put:b1. |
|
3068 |
] ifFalse:[ |
|
3069 |
b7 := absValue bitAnd:16rFF. |
|
3070 |
absValue := absValue bitShift:-8. |
|
3071 |
absValue == 0 ifTrue:[ |
|
3072 |
digitByteArray := ByteArray new:7. |
|
3073 |
digitByteArray at:1 put:b7. |
|
3074 |
digitByteArray at:2 put:b6. |
|
3075 |
digitByteArray at:3 put:b5. |
|
3076 |
digitByteArray at:4 put:b4. |
|
3077 |
digitByteArray at:5 put:b3. |
|
3078 |
digitByteArray at:6 put:b2. |
|
3079 |
digitByteArray at:7 put:b1. |
|
3080 |
] ifFalse:[ |
|
3081 |
digitByteArray := ByteArray new:8. |
|
3082 |
digitByteArray at:1 put:absValue. |
|
3083 |
digitByteArray at:2 put:b7. |
|
3084 |
digitByteArray at:3 put:b6. |
|
3085 |
digitByteArray at:4 put:b5. |
|
3086 |
digitByteArray at:5 put:b4. |
|
3087 |
digitByteArray at:6 put:b3. |
|
3088 |
digitByteArray at:7 put:b2. |
|
3089 |
digitByteArray at:8 put:b1. |
|
3090 |
] |
|
3091 |
] |
|
3092 |
] |
|
3093 |
] |
|
3094 |
] |
|
3095 |
] |
|
15568 | 3096 |
]. |
3097 |
||
3098 |
^ digitByteArray |
|
3099 |
||
3100 |
" |
|
3101 |
16r12 digitBytesMSB hexPrintString |
|
3102 |
16r1234 digitBytesMSB hexPrintString |
|
3103 |
16r12345678 digitBytesMSB hexPrintString |
|
4827
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
3104 |
" |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
3105 |
! |
905b98a07798
New: #digitBytes and #digitBytesMSB:
Claus Gittinger <cg@exept.de>
parents:
4661
diff
changeset
|
3106 |
|
701 | 3107 |
digitLength |
19321 | 3108 |
"return the number of bytes needed for the unsigned binary representation of the receiver. |
3109 |
For negative receivers, the result is not defined by the language standard. |
|
3110 |
ST/X returns the digitLength of its absolute value." |
|
2815 | 3111 |
|
3112 |
%{ /* NOCONTEXT */ |
|
18410 | 3113 |
#ifdef __SCHTEAM__ |
3114 |
long val = self.longValue(); |
|
3115 |
int offs = 0; |
|
3116 |
||
3117 |
if (val < 0) val = -val; |
|
3118 |
if ((val & 0xFFFFFFFF00000000L) != 0) { |
|
20339 | 3119 |
val >>= 32; |
3120 |
offs = 4; |
|
18410 | 3121 |
} |
3122 |
if ((val & 0xFFFF0000) != 0) { |
|
20339 | 3123 |
if ((val & 0xFF000000) != 0) { |
3124 |
offs += 4; |
|
3125 |
} else { |
|
3126 |
offs += 3; |
|
3127 |
} |
|
18410 | 3128 |
} else { |
20339 | 3129 |
if ((val & 0x0000FF00)!= 0) { |
3130 |
offs += 2; |
|
3131 |
} else { |
|
3132 |
offs += 1; |
|
3133 |
} |
|
18410 | 3134 |
} |
3135 |
return __c__._RETURN( STInteger._qnew(offs) ); |
|
3136 |
#else |
|
2815 | 3137 |
INT val = __intVal(self); |
18410 | 3138 |
int offs = 0; |
2815 | 3139 |
|
3140 |
if (val < 0) { |
|
20339 | 3141 |
val = -val; |
2815 | 3142 |
} |
18227 | 3143 |
# if __POINTER_SIZE__ == 8 |
2815 | 3144 |
if (val & 0xFFFFFFFF00000000L) { |
20339 | 3145 |
val >>= 32; |
3146 |
offs = 4; |
|
2815 | 3147 |
} |
18227 | 3148 |
# endif |
2815 | 3149 |
|
3150 |
if (val & 0xFFFF0000) { |
|
20339 | 3151 |
if (val & 0xFF000000) { |
3152 |
RETURN ( __mkSmallInteger(4+offs)); |
|
3153 |
} else { |
|
3154 |
RETURN ( __mkSmallInteger(3+offs)); |
|
3155 |
} |
|
2815 | 3156 |
} else { |
20339 | 3157 |
if (val & 0x0000FF00) { |
3158 |
RETURN ( __mkSmallInteger(2+offs)); |
|
3159 |
} else { |
|
3160 |
RETURN ( __mkSmallInteger(1+offs)); |
|
3161 |
} |
|
2815 | 3162 |
} |
18410 | 3163 |
#endif /* not SCHTEAM */ |
2815 | 3164 |
%}. |
701 | 3165 |
^ self abs highBit - 1 // 8 + 1 |
2815 | 3166 |
|
3167 |
" |
|
3168 |
16rFF00000000000000 digitLength |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3169 |
16r-FF00000000000000 digitLength |
2815 | 3170 |
|
3171 |
16rFF000000 digitLength |
|
8908 | 3172 |
16rFF0000 digitLength |
3173 |
16rFF00 digitLength |
|
3174 |
16rFF digitLength |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3175 |
16r-FF000000 digitLength |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3176 |
16r-FF0000 digitLength |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3177 |
16r-FF00 digitLength |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3178 |
16r-FF digitLength |
2815 | 3179 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3180 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3181 |
"Modified: / 19-09-2017 / 16:34:26 / stefan" |
14167 | 3182 |
! |
3183 |
||
3184 |
swapBytes |
|
16885 | 3185 |
"swap bytes pair-wise in a positive integer |
3186 |
i.e. a.b.c.d -> b.a.d.c. |
|
18897 | 3187 |
The name may be misleading; actually a new integer is returned, |
3188 |
and the receiver is not modified. |
|
18947 | 3189 |
|
16885 | 3190 |
Redefined here for speed. |
3191 |
Swapping of negative integers is undefined and therefore not supported. |
|
3192 |
This case is handled in the superclass." |
|
14167 | 3193 |
|
3194 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3195 |
#ifndef __SCHTEAM__ |
15322 | 3196 |
unsigned INT v = __intVal(self); |
16885 | 3197 |
|
3198 |
if ((INT)v >= 0) { |
|
20339 | 3199 |
unsigned INT swapped; |
18227 | 3200 |
|
3201 |
# if __POINTER_SIZE__ == 8 |
|
20339 | 3202 |
swapped = ((v >> 8) & 0x00FF00FF00FF00FF) | ((v & 0x00FF00FF00FF00FF) << 8); |
18227 | 3203 |
# else |
20339 | 3204 |
swapped = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8); |
18227 | 3205 |
# endif /* __POINTER_SIZE__ */ |
3206 |
||
20339 | 3207 |
//if (__ISVALIDINTEGER(swapped)) { // sorry, but this does not work here if (INT)swapped would be negative |
3208 |
if (swapped <= _MAX_INT) { |
|
3209 |
RETURN ( __mkSmallInteger(swapped) ); |
|
3210 |
} |
|
3211 |
RETURN (__MKUINT(swapped)); |
|
15322 | 3212 |
} |
18227 | 3213 |
#endif |
14167 | 3214 |
%}. |
3215 |
^ super swapBytes |
|
3216 |
||
3217 |
" |
|
18227 | 3218 |
-1 swapBytes hexPrintString |
3219 |
16r11223344 swapBytes hexPrintString |
|
3220 |
16r44332211 swapBytes hexPrintString |
|
16873 | 3221 |
self maxVal swapBytes hexPrintString |
3222 |
self maxVal swapBytes swapBytes hexPrintString |
|
18227 | 3223 |
16r1122334455667788 swapBytes hexPrintString |
3224 |
16r11223344556677889900 swapBytes hexPrintString |
|
14167 | 3225 |
" |
3226 |
||
3227 |
"Created: / 09-01-2012 / 23:01:33 / cg" |
|
1 | 3228 |
! ! |
3229 |
||
701 | 3230 |
!SmallInteger methodsFor:'catching messages'! |
1 | 3231 |
|
701 | 3232 |
basicAt:index |
3233 |
"catch indexed access - report an error |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3234 |
defined here since basicAt: in Object omits the SmallInteger check." |
701 | 3235 |
|
14540 | 3236 |
^ self notIndexed |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3237 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3238 |
"Modified (comment): / 17-05-2017 / 16:34:34 / mawalch" |
1 | 3239 |
! |
3240 |
||
701 | 3241 |
basicAt:index put:anObject |
3242 |
"catch indexed access - report an error |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3243 |
defined here since basicAt:put: in Object omits the SmallInteger check." |
701 | 3244 |
|
3245 |
self notIndexed |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3246 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3247 |
"Modified (comment): / 17-05-2017 / 16:34:40 / mawalch" |
701 | 3248 |
! |
1 | 3249 |
|
701 | 3250 |
basicSize |
3251 |
"return the number of indexed instvars - SmallIntegers have none. |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3252 |
Defined here since basicSize in Object omits the SmallInteger check." |
701 | 3253 |
|
3254 |
^ 0 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3255 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3256 |
"Modified (comment): / 17-05-2017 / 16:34:49 / mawalch" |
701 | 3257 |
! |
3258 |
||
3259 |
size |
|
3260 |
"return the number of indexed instvars - SmallIntegers have none." |
|
3261 |
||
3262 |
^ 0 |
|
1 | 3263 |
! ! |
3264 |
||
5240 | 3265 |
!SmallInteger methodsFor:'coercing & converting'! |
1 | 3266 |
|
701 | 3267 |
asCharacter |
18897 | 3268 |
"Return a character with the receiver as ascii (actually: unicode) value" |
41 | 3269 |
|
701 | 3270 |
^ Character value:self |
18897 | 3271 |
|
3272 |
" |
|
3273 |
Character value:16r61 |
|
3274 |
Character value:16r161 |
|
3275 |
" |
|
1 | 3276 |
! |
3277 |
||
3278 |
asFloat |
|
18897 | 3279 |
"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
|
3280 |
Redefined for performance (machine can do it faster)" |
1 | 3281 |
|
3282 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3283 |
#ifdef __SCHTEAM__ |
18227 | 3284 |
return context._RETURN( STDouble._new((double)(self.longValue())) ); |
3285 |
#else |
|
1 | 3286 |
OBJ newFloat; |
1133 | 3287 |
double dVal = (double)__intVal(self); |
1 | 3288 |
|
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
3289 |
__qMKFLOAT(newFloat, dVal); |
1 | 3290 |
RETURN ( newFloat ); |
18227 | 3291 |
#endif |
5412 | 3292 |
%}. |
3293 |
^ self primitiveFailed |
|
1 | 3294 |
! |
3295 |
||
3296 |
asLargeInteger |
|
18897 | 3297 |
"return a LargeInteger with same value as receiver. |
3298 |
Not for general use: |
|
3299 |
Notice, that this returns an unnormalized large int (i.e. a large with a smallint value), |
|
3300 |
which are normally not present in the system, and not handled correctly by many functions. |
|
3301 |
This exists only as a helper for some algorithms and converters" |
|
1 | 3302 |
|
3303 |
^ LargeInteger value:self |
|
3304 |
! |
|
3305 |
||
1199 | 3306 |
asShortFloat |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
3307 |
"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
|
3308 |
Redefined for performance (machine can do it faster)" |
1199 | 3309 |
|
3310 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3311 |
#ifdef __SCHTEAM__ |
18227 | 3312 |
return context._RETURN( STFloat._new((float)(self.longValue())) ); |
3313 |
#else |
|
1199 | 3314 |
OBJ dummy = @global(ShortFloat); |
3315 |
OBJ newFloat; |
|
3316 |
float fVal = (float)__intVal(self); |
|
3317 |
||
1695
465e1eba8e8e
removed useless context-arg of all qMK macros - needs full recompile.
Claus Gittinger <cg@exept.de>
parents:
1688
diff
changeset
|
3318 |
__qMKSFLOAT(newFloat, fVal); |
1199 | 3319 |
RETURN ( newFloat ); |
18227 | 3320 |
#endif |
5412 | 3321 |
%}. |
3322 |
^ self primitiveFailed |
|
1199 | 3323 |
! |
3324 |
||
18676 | 3325 |
asUnsignedInt |
18947 | 3326 |
"return an integer representing my unsigned INT value. |
18897 | 3327 |
Notice, that the returned integer's size |
18676 | 3328 |
depends heavily on the underlying INT size; |
3329 |
You will get 16rFFFFFFFF on 32bit machines, |
|
3330 |
but 16rFFFFFFFFFFFFFFFF on 64 bit machines. |
|
18897 | 3331 |
So use this only for printing or certain bit operations (emulating C semantics)." |
18676 | 3332 |
|
3333 |
%{ /* NOCONTEXT */ |
|
3334 |
INT iVal = __intVal(self); |
|
3335 |
OBJ uiVal; |
|
3336 |
||
3337 |
uiVal = __MKUINT( (unsigned INT)iVal ); |
|
3338 |
RETURN ( uiVal ); |
|
3339 |
%}. |
|
3340 |
^ self primitiveFailed |
|
3341 |
||
3342 |
" |
|
3343 |
-1 asUnsignedInt hexPrintString -> 'FFFFFFFFFFFFFFFF' |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3344 |
16r-8000 asUnsignedInt hexPrintString -> ''FFFFFFFFFFFF8000'' |
18676 | 3345 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3346 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3347 |
"Modified (comment): / 19-09-2017 / 16:34:01 / stefan" |
18676 | 3348 |
! |
3349 |
||
8408
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
3350 |
codePoint |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
3351 |
"for compatibility with Characters. |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
3352 |
(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
|
3353 |
|
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
3354 |
^ self |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
3355 |
! |
61f12d6c1ab0
juergen gmeiners bug fix for bug #69
Claus Gittinger <cg@exept.de>
parents:
8365
diff
changeset
|
3356 |
|
701 | 3357 |
generality |
3358 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
3359 |
||
3360 |
^ 20 |
|
1336 | 3361 |
! |
3362 |
||
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3363 |
signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3364 |
"return a smallInteger from sign-extending the 24'th bit. |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3365 |
May be useful for communication interfaces" |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3366 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3367 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3368 |
#ifndef __SCHTEAM__ |
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3369 |
INT i = __intVal(self); |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3370 |
|
19263 | 3371 |
if (i & 0x800000L) { |
20339 | 3372 |
i = i | ~((INT)0xFFFFFF); |
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3373 |
} else { |
20339 | 3374 |
i = i & 0x7FFFFF; |
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3375 |
} |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3376 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3377 |
RETURN (__mkSmallInteger(i)); |
18227 | 3378 |
#endif |
12866
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3379 |
%}. |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3380 |
^ self primitiveFailed |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3381 |
|
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3382 |
" |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3383 |
16rFFFFFF signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3384 |
16r800000 signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3385 |
16r7FFFFF signExtended24BitValue |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3386 |
" |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3387 |
! |
42e5e690c197
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
12787
diff
changeset
|
3388 |
|
1336 | 3389 |
signExtendedByteValue |
3390 |
"return a smallInteger from sign-extending the 8'th bit. |
|
3391 |
May be useful for communication interfaces" |
|
3392 |
||
3393 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3394 |
#ifndef __SCHTEAM__ |
2781 | 3395 |
INT i = __intVal(self); |
1336 | 3396 |
|
3397 |
if (i & 0x80) { |
|
20339 | 3398 |
i = i | ~((INT)0xFF); |
1336 | 3399 |
} else { |
20339 | 3400 |
i = i & 0x7F; |
1336 | 3401 |
} |
3402 |
||
8913 | 3403 |
RETURN (__mkSmallInteger(i)); |
18227 | 3404 |
#endif |
5412 | 3405 |
%}. |
3406 |
^ self primitiveFailed |
|
1336 | 3407 |
|
3408 |
" |
|
14632 | 3409 |
16rFF signExtendedByteValue |
3410 |
16r80 signExtendedByteValue |
|
3411 |
16r7F signExtendedByteValue |
|
1336 | 3412 |
" |
3413 |
! |
|
3414 |
||
15083 | 3415 |
signExtendedLongValue |
3416 |
"return a smallInteger from sign-extending the 32'th bit. |
|
3417 |
May be useful for communication interfaces" |
|
3418 |
||
3419 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3420 |
#ifndef __SCHTEAM__ |
15083 | 3421 |
INT i = __intVal(self); |
3422 |
||
19263 | 3423 |
if (i & 0x80000000L) { |
20339 | 3424 |
i = i | ~((INT)0xFFFFFFFF); |
15083 | 3425 |
} else { |
20339 | 3426 |
i = i & 0x7FFFFFFFL; |
15083 | 3427 |
} |
3428 |
||
19264 | 3429 |
RETURN (__MKINT(i)); |
18227 | 3430 |
#endif |
15083 | 3431 |
%}. |
3432 |
^ self primitiveFailed |
|
3433 |
||
3434 |
" |
|
19264 | 3435 |
16rFFFFFFFF signExtendedLongValue -> -1 |
3436 |
16r80000000 signExtendedLongValue -> -2147483648 |
|
3437 |
16r7FFFFFFF signExtendedLongValue -> 2147483647 |
|
15083 | 3438 |
" |
3439 |
! |
|
3440 |
||
1336 | 3441 |
signExtendedShortValue |
3442 |
"return a smallInteger from sign-extending the 16'th bit. |
|
3443 |
May be useful for communication interfaces" |
|
3444 |
||
3445 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3446 |
#ifndef __SCHTEAM__ |
2781 | 3447 |
INT i = __intVal(self); |
1336 | 3448 |
|
3449 |
if (i & 0x8000) { |
|
20339 | 3450 |
i = i | ~((INT)0xFFFF); |
1336 | 3451 |
} else { |
20339 | 3452 |
i = i & 0x7FFF; |
1336 | 3453 |
} |
3454 |
||
8913 | 3455 |
RETURN (__mkSmallInteger(i)); |
18227 | 3456 |
#endif |
5412 | 3457 |
%}. |
3458 |
^ self primitiveFailed |
|
1336 | 3459 |
|
3460 |
" |
|
14632 | 3461 |
16rFFFF signExtendedShortValue |
3462 |
16r8000 signExtendedShortValue |
|
3463 |
16r7FFF signExtendedShortValue |
|
1336 | 3464 |
" |
701 | 3465 |
! ! |
3466 |
||
3467 |
!SmallInteger methodsFor:'comparing'! |
|
3468 |
||
3469 |
< aNumber |
|
3470 |
"return true, if the argument is greater than the receiver" |
|
3471 |
||
3472 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3473 |
#ifdef __SCHTEAM__ |
18236 | 3474 |
return context._RETURN( self.ltP( aNumber )); |
18227 | 3475 |
#else |
701 | 3476 |
if (__isSmallInteger(aNumber)) { |
18227 | 3477 |
# ifdef POSITIVE_ADDRESSES |
20339 | 3478 |
RETURN ( (__intVal(self) < __intVal(aNumber)) ? true : false ); |
18227 | 3479 |
# else |
20339 | 3480 |
/* tag bit does not change ordering */ |
3481 |
RETURN ( ((INT)self < (INT)aNumber) ? true : false ); |
|
18227 | 3482 |
# endif |
701 | 3483 |
} |
3484 |
if (__isFloatLike(aNumber)) { |
|
20339 | 3485 |
RETURN ( ((double)__intVal(self) < __floatVal(aNumber)) ? true : false ); |
701 | 3486 |
} |
18897 | 3487 |
#endif /* not __SCHTEAM__ */ |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3488 |
%}. |
701 | 3489 |
^ aNumber lessFromInteger:self |
3490 |
"^ self retry:#< coercing:aNumber" |
|
3491 |
! |
|
3492 |
||
3493 |
<= aNumber |
|
3494 |
"return true, if the argument is greater or equal" |
|
3495 |
||
3496 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3497 |
#ifdef __SCHTEAM__ |
18236 | 3498 |
return context._RETURN( self.leP( aNumber )); |
18227 | 3499 |
#else |
701 | 3500 |
|
3501 |
if (__isSmallInteger(aNumber)) { |
|
18227 | 3502 |
# ifdef POSITIVE_ADDRESSES |
20339 | 3503 |
RETURN ( (__intVal(self) <= __intVal(aNumber)) ? true : false ); |
18227 | 3504 |
# else |
20339 | 3505 |
/* tag bit does not change ordering */ |
3506 |
RETURN ( ((INT)self <= (INT)aNumber) ? true : false ); |
|
18227 | 3507 |
# endif |
701 | 3508 |
} |
3509 |
if (__isFloatLike(aNumber)) { |
|
20339 | 3510 |
RETURN ( ((double)__intVal(self) <= __floatVal(aNumber)) ? true : false ); |
701 | 3511 |
} |
18897 | 3512 |
#endif /* not __SCHTEAM__ */ |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
3513 |
%}. |
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
3514 |
^ (self > aNumber) not |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3515 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3516 |
"Modified: / 31.7.2002 / 10:07:17 / cg" |
701 | 3517 |
! |
3518 |
||
3519 |
= aNumber |
|
7356 | 3520 |
"return true, if the argument represents the same numeric value |
3521 |
as the receiver, false otherwise" |
|
701 | 3522 |
|
3523 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3524 |
#ifdef __SCHTEAM__ |
18699 | 3525 |
if (aNumber.isNumber()) { |
20339 | 3526 |
return context._RETURN( aNumber.eqvP( self.longValue() )); |
18699 | 3527 |
} |
18227 | 3528 |
#else |
701 | 3529 |
|
3530 |
if (aNumber == self) { |
|
20339 | 3531 |
RETURN ( true ); |
701 | 3532 |
} |
3533 |
if (! __isNonNilObject(aNumber)) { |
|
20339 | 3534 |
/* a smallint or nil */ |
3535 |
RETURN ( false ); |
|
701 | 3536 |
} |
3537 |
||
7356 | 3538 |
if (__qIsFloatLike(aNumber)) { |
20339 | 3539 |
RETURN ( ((double)__intVal(self) == __floatVal(aNumber)) ? true : false ); |
701 | 3540 |
} |
7356 | 3541 |
if (__qIsShortFloat(aNumber)) { |
20339 | 3542 |
RETURN ( ((double)__intVal(self) == __shortFloatVal(aNumber)) ? true : false ); |
7356 | 3543 |
} |
18897 | 3544 |
#endif /* not __SCHTEAM__ */ |
7356 | 3545 |
%}. |
3546 |
^ aNumber equalFromInteger:self |
|
701 | 3547 |
! |
3548 |
||
3549 |
> aNumber |
|
3550 |
"return true, if the argument is less than the receiver" |
|
3551 |
||
3552 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3553 |
#ifdef __SCHTEAM__ |
18236 | 3554 |
return context._RETURN( self.gtP( aNumber )); |
18227 | 3555 |
#else |
701 | 3556 |
|
3557 |
if (__isSmallInteger(aNumber)) { |
|
18227 | 3558 |
# ifdef POSITIVE_ADDRESSES |
20339 | 3559 |
RETURN ( (__intVal(self) > __intVal(aNumber)) ? true : false ); |
18227 | 3560 |
# else |
20339 | 3561 |
/* tag bit does not change ordering */ |
3562 |
RETURN ( ((INT)self > (INT)aNumber) ? true : false ); |
|
18227 | 3563 |
# endif |
701 | 3564 |
} |
3565 |
if (__isFloatLike(aNumber)) { |
|
20339 | 3566 |
RETURN ( ((double)__intVal(self) > __floatVal(aNumber)) ? true : false ); |
701 | 3567 |
} |
18897 | 3568 |
#endif /* not __SCHTEAM__ */ |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
3569 |
%}. |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3570 |
^ (aNumber < self) |
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3571 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3572 |
"Modified: / 31.7.2002 / 10:07:05 / cg" |
701 | 3573 |
! |
3574 |
||
3575 |
>= aNumber |
|
3576 |
"return true, if the argument is less or equal" |
|
3577 |
||
3578 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3579 |
#ifdef __SCHTEAM__ |
18236 | 3580 |
return context._RETURN( self.geP( aNumber )); |
18227 | 3581 |
#else |
701 | 3582 |
|
3583 |
if (__isSmallInteger(aNumber)) { |
|
18227 | 3584 |
# ifdef POSITIVE_ADDRESSES |
20339 | 3585 |
RETURN ( (__intVal(self) >= __intVal(aNumber)) ? true : false ); |
18227 | 3586 |
# else |
20339 | 3587 |
/* tag bit does not change ordering */ |
3588 |
RETURN ( ((INT)self >= (INT)aNumber) ? true : false ); |
|
18227 | 3589 |
# endif |
701 | 3590 |
} |
3591 |
if (__isFloatLike(aNumber)) { |
|
20339 | 3592 |
RETURN ( ((double)__intVal(self) >= __floatVal(aNumber)) ? true : false ); |
701 | 3593 |
} |
18897 | 3594 |
#endif /* not __SCHTEAM__ */ |
5930
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
3595 |
%}. |
d7728046c2e1
compare fallBacks changed to enable VW compatible double dispatching
Claus Gittinger <cg@exept.de>
parents:
5782
diff
changeset
|
3596 |
^ (self < aNumber) not |
6676
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3597 |
|
15f2e3080e13
double dispatch fallback code in comparing fixed
Claus Gittinger <cg@exept.de>
parents:
6482
diff
changeset
|
3598 |
"Modified: / 31.7.2002 / 10:07:11 / cg" |
701 | 3599 |
! |
1 | 3600 |
|
701 | 3601 |
hash |
3602 |
"return an integer useful for hashing on value" |
|
3603 |
||
3604 |
self >= 0 ifTrue:[^ self]. |
|
3605 |
^ self negated |
|
3606 |
! |
|
3607 |
||
17247 | 3608 |
hashMultiply |
3609 |
"used in some squeak code to generate an alternative hash value for integers" |
|
3610 |
||
3611 |
|low| |
|
3612 |
||
3613 |
low := self bitAnd: 16r3FFF. |
|
18227 | 3614 |
^ (9741 * low |
17247 | 3615 |
+ ((9741 * (self bitShift: -14) + (101 * low) bitAnd: 16383) * 16384)) |
20339 | 3616 |
bitAnd: 16r0FFFFFFF |
17247 | 3617 |
|
3618 |
" |
|
18227 | 3619 |
1 hashMultiply |
3620 |
2 hashMultiply |
|
3621 |
3 hashMultiply |
|
3622 |
100 hashMultiply |
|
17247 | 3623 |
" |
3624 |
! |
|
3625 |
||
701 | 3626 |
identityHash |
3627 |
"return an integer useful for hashing on identity" |
|
3628 |
||
1964
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
3629 |
self >= 0 ifTrue:[^ self]. |
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
3630 |
^ self negated |
1963
ea26c7b9ca1d
experimental: new hash value for smallInts
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3631 |
|
1964
20bb7197d19f
oops - the last one was no good
Claus Gittinger <cg@exept.de>
parents:
1963
diff
changeset
|
3632 |
"Modified: 11.11.1996 / 18:42:14 / cg" |
701 | 3633 |
! |
3634 |
||
3635 |
max:aNumber |
|
3636 |
"return the receiver or the argument, whichever is greater" |
|
3637 |
||
3638 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3639 |
#ifndef __SCHTEAM__ |
701 | 3640 |
|
3641 |
if (__isSmallInteger(aNumber)) { |
|
18227 | 3642 |
# if TAG_INT == 1 |
20339 | 3643 |
/* tag bit does not change ordering */ |
3644 |
if ((INT)(self) > (INT)(aNumber)) |
|
18227 | 3645 |
# else |
20339 | 3646 |
if (__intVal(self) > __intVal(aNumber)) |
18227 | 3647 |
# endif |
20339 | 3648 |
{ |
3649 |
RETURN ( self ); |
|
3650 |
} |
|
3651 |
RETURN ( aNumber ); |
|
701 | 3652 |
} |
3653 |
if (__isFloatLike(aNumber)) { |
|
20339 | 3654 |
if ( (double)__intVal(self) > __floatVal(aNumber) ) { |
3655 |
RETURN ( self ); |
|
3656 |
} |
|
3657 |
RETURN ( aNumber ); |
|
701 | 3658 |
} |
18897 | 3659 |
#endif /* not __SCHTEAM__ */ |
2216 | 3660 |
%}. |
3661 |
"/ fallback for non-smallInteger argument |
|
3662 |
||
701 | 3663 |
(self > aNumber) ifTrue:[^ self]. |
3664 |
^ aNumber |
|
3665 |
! |
|
3666 |
||
3667 |
min:aNumber |
|
3668 |
"return the receiver or the argument, whichever is smaller" |
|
3669 |
||
3670 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3671 |
#ifndef __SCHTEAM__ |
701 | 3672 |
|
3673 |
if (__isSmallInteger(aNumber)) { |
|
18227 | 3674 |
# if TAG_INT == 1 |
20339 | 3675 |
/* tag bit does not change ordering */ |
3676 |
if ((INT)(self) < (INT)(aNumber)) |
|
18227 | 3677 |
# else |
20339 | 3678 |
if (__intVal(self) < __intVal(aNumber)) |
18227 | 3679 |
# endif |
20339 | 3680 |
{ |
3681 |
RETURN ( self ); |
|
3682 |
} |
|
3683 |
RETURN ( aNumber ); |
|
701 | 3684 |
} |
3685 |
if (__isFloatLike(aNumber)) { |
|
20339 | 3686 |
if ( (double)__intVal(self) < __floatVal(aNumber) ) { |
3687 |
RETURN ( self ); |
|
3688 |
} |
|
3689 |
RETURN ( aNumber ); |
|
701 | 3690 |
} |
18897 | 3691 |
#endif /* not __SCHTEAM__ */ |
2216 | 3692 |
%}. |
3693 |
"/ fallback for non-smallInteger argument |
|
3694 |
||
701 | 3695 |
(self < aNumber) ifTrue:[^ self]. |
3696 |
^ aNumber |
|
3697 |
! |
|
3698 |
||
3699 |
~= aNumber |
|
3700 |
"return true, if the arguments value is not equal to mine" |
|
3701 |
||
3702 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3703 |
#ifdef __SCHTEAM__ |
18699 | 3704 |
if (aNumber.isNumber()) { |
20339 | 3705 |
return context._RETURN( (aNumber.isEqv( self.longValue() )) ? STObject.False : STObject.True); |
3706 |
/* NOTREACHED */ |
|
18699 | 3707 |
} |
18236 | 3708 |
#else |
701 | 3709 |
|
3710 |
if (aNumber == self) { |
|
20339 | 3711 |
RETURN ( false ); |
701 | 3712 |
} |
3713 |
if (! __isNonNilObject(aNumber)) { |
|
20339 | 3714 |
/* a smallint or nil */ |
3715 |
RETURN ( true ); |
|
701 | 3716 |
} |
3717 |
||
7356 | 3718 |
if (__qIsFloatLike(aNumber)) { |
20339 | 3719 |
RETURN ( ((double)__intVal(self) != __floatVal(aNumber)) ? true : false ); |
7356 | 3720 |
} |
3721 |
if (__qIsShortFloat(aNumber)) { |
|
20339 | 3722 |
RETURN ( ((double)__intVal(self) != __shortFloatVal(aNumber)) ? true : false ); |
701 | 3723 |
} |
18897 | 3724 |
#endif /* not __SCHTEAM__ */ |
2216 | 3725 |
%}. |
7356 | 3726 |
^ (self = aNumber) not |
701 | 3727 |
! ! |
3728 |
||
3729 |
!SmallInteger methodsFor:'copying'! |
|
3730 |
||
3731 |
deepCopy |
|
3732 |
"return a deep copy of myself |
|
3733 |
- reimplemented here since smallintegers are unique" |
|
3734 |
||
3735 |
^ self |
|
3736 |
! |
|
3737 |
||
10947 | 3738 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
701 | 3739 |
"return a deep copy of myself |
3740 |
- reimplemented here since smallintegers are unique" |
|
3741 |
||
3742 |
^ self |
|
3743 |
! |
|
3744 |
||
3745 |
shallowCopy |
|
3746 |
"return a shallow copy of myself |
|
3747 |
- reimplemented here since smallintegers are unique" |
|
3748 |
||
3749 |
^ self |
|
3750 |
! |
|
3751 |
||
3752 |
simpleDeepCopy |
|
3753 |
"return a deep copy of myself |
|
3754 |
- reimplemented here since smallintegers are unique" |
|
3755 |
||
3756 |
^ self |
|
1 | 3757 |
! ! |
3758 |
||
214 | 3759 |
!SmallInteger methodsFor:'iteration'! |
1 | 3760 |
|
3761 |
timesRepeat:aBlock |
|
357 | 3762 |
"evaluate the argument, aBlock self times. |
3763 |
Reimplemented as primitive for speed" |
|
1 | 3764 |
|
357 | 3765 |
%{ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3766 |
#ifndef __SCHTEAM__ |
357 | 3767 |
REGISTER INT tmp; |
3768 |
static struct inlineCache blockVal = __ILC0(0); |
|
1 | 3769 |
|
357 | 3770 |
tmp = __intVal(self); |
3771 |
if (tmp > 0) { |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3772 |
if (__isBlockLike(aBlock) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3773 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(0))) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3774 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3775 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3776 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3777 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3778 |
* specially tuned version for compiled blocks, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3779 |
* (the most common case) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3780 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3781 |
if (((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
18227 | 3782 |
# ifdef PARANOIA |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3783 |
&& (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
18227 | 3784 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3785 |
) { |
2254 | 3786 |
# define BLOCK_ARG aBlock |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3787 |
{ |
18227 | 3788 |
# ifdef __UNROLL_LOOPS__ |
2216 | 3789 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3790 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3791 |
* you are not supposed to program like this - I know what I do |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3792 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3793 |
while (tmp > 8) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3794 |
if (InterruptPending != nil) goto interrupted0; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3795 |
continue0: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3796 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3797 |
if (InterruptPending != nil) goto interrupted1; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3798 |
continue1: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3799 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3800 |
if (InterruptPending != nil) goto interrupted2; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3801 |
continue2: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3802 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3803 |
if (InterruptPending != nil) goto interrupted3; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3804 |
continue3: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3805 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3806 |
if (InterruptPending != nil) goto interrupted4; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3807 |
continue4: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3808 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3809 |
if (InterruptPending != nil) goto interrupted5; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3810 |
continue5: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3811 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3812 |
if (InterruptPending != nil) goto interrupted6; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3813 |
continue6: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3814 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3815 |
if (InterruptPending != nil) goto interrupted7; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3816 |
continue7: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3817 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3818 |
tmp -= 8; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3819 |
} |
18227 | 3820 |
# endif /* __UNROLL_LOOPS__ */ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3821 |
do { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3822 |
if (InterruptPending != nil) goto interruptedX; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3823 |
continueX: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3824 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3825 |
} while(--tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3826 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3827 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3828 |
if (0) { |
18227 | 3829 |
# ifdef __UNROLL_LOOPS__ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3830 |
interrupted0: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3831 |
__interruptL(@line); goto continue0; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3832 |
interrupted1: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3833 |
__interruptL(@line); goto continue1; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3834 |
interrupted2: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3835 |
__interruptL(@line); goto continue2; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3836 |
interrupted3: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3837 |
__interruptL(@line); goto continue3; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3838 |
interrupted4: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3839 |
__interruptL(@line); goto continue4; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3840 |
interrupted5: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3841 |
__interruptL(@line); goto continue5; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3842 |
interrupted6: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3843 |
__interruptL(@line); goto continue6; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3844 |
interrupted7: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3845 |
__interruptL(@line); goto continue7; |
18227 | 3846 |
# endif /* __UNROLL_LOOPS__ */ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3847 |
interruptedX: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3848 |
__interruptL(@line); goto continueX; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3849 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3850 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3851 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3852 |
} |
1672
1b56d6e95c9e
changes to call dynamic compiled code right after compilation.
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
3853 |
|
2216 | 3854 |
# undef BLOCK_ARG |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
3855 |
|
2216 | 3856 |
# define BLOCK_ARG aBlock |
3857 |
# define IBLOCK_ARG nil |
|
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
3858 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3859 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3860 |
* sorry - must check for the blocks code within the loops; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3861 |
* it could be recompiled or flushed (in the interrupt) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3862 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3863 |
do { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3864 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3865 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3866 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3867 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3868 |
* arg is a compiled block with code - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3869 |
* directly call it without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3870 |
* however, if there is an interrupt, refetch the code pointer. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3871 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3872 |
/* stay here, while no interrupts are pending ... */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3873 |
do { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3874 |
(*codeVal)(BLOCK_ARG); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3875 |
if (InterruptPending != nil) goto outerLoop; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3876 |
} while (--tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3877 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3878 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3879 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3880 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3881 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3882 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3883 |
* arg is a compiled block with bytecode - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3884 |
* directly call interpreter without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3885 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3886 |
__interpret(aBlock, 0, nil, IBLOCK_ARG, nil, nil); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3887 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3888 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3889 |
* arg is something else - call it with #value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3890 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3891 |
(*blockVal.ilc_func)(aBlock, @symbol(value), nil, &blockVal); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3892 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3893 |
} |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
3894 |
outerLoop: ; |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3895 |
} while (--tmp); |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3896 |
|
2216 | 3897 |
# undef BLOCK_ARG |
3898 |
# undef IBLOCK_ARG |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3899 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3900 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3901 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3902 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3903 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3904 |
* not a block-like thingy - call it with #value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3905 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3906 |
do { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3907 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3908 |
(*blockVal.ilc_func)(aBlock, @symbol(value), nil, &blockVal); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3909 |
} while(--tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3910 |
RETURN (self); |
357 | 3911 |
} |
18227 | 3912 |
#endif |
5412 | 3913 |
%}. |
3914 |
^ super timesRepeat:aBlock |
|
357 | 3915 |
|
3916 |
"/ |count "{ Class: SmallInteger }" | |
|
3917 |
"/ |
|
3918 |
"/ count := self. |
|
3919 |
"/ [count > 0] whileTrue:[ |
|
3920 |
"/ aBlock value. |
|
3921 |
"/ count := count - 1 |
|
3922 |
"/ ] |
|
1 | 3923 |
! |
3924 |
||
701 | 3925 |
to:stop by:incr do:aBlock |
3926 |
"reimplemented as primitive for speed" |
|
3927 |
||
3928 |
%{ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
3929 |
#ifndef __SCHTEAM__ |
701 | 3930 |
REGISTER INT tmp, step; |
3931 |
REGISTER INT final; |
|
3932 |
static struct inlineCache blockVal = __ILC1(0); |
|
3933 |
||
3934 |
if (__bothSmallInteger(incr, stop)) { |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3935 |
tmp = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3936 |
final = __intVal(stop); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3937 |
step = __intVal(incr); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3938 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3939 |
if (__isBlockLike(aBlock) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3940 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(1))) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3941 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3942 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3943 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3944 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3945 |
* specially tuned version for static compiled blocks, called with |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3946 |
* home on the stack (the most common case) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3947 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3948 |
if (((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) |
18227 | 3949 |
# ifdef PARANOIA |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3950 |
&& (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
18227 | 3951 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3952 |
) { |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3953 |
|
2254 | 3954 |
# define BLOCK_ARG aBlock |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3955 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3956 |
if (step < 0) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3957 |
if (step == -1) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3958 |
while (tmp >= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3959 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3960 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3961 |
tmp--; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3962 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3963 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3964 |
while (tmp >= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3965 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3966 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3967 |
tmp += step; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3968 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3969 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3970 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3971 |
if (step == 1) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3972 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3973 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3974 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3975 |
tmp++; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3976 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3977 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3978 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3979 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3980 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3981 |
tmp += step; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3982 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3983 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3984 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3985 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3986 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3987 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3988 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3989 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3990 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3991 |
* sorry - must check for the blocks code within the loops; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3992 |
* it could be recompiled or flushed (in the interrupt) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
3993 |
*/ |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3994 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3995 |
# undef BLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3996 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3997 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3998 |
# define IBLOCK_ARG nil |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
3999 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4000 |
if (step < 0) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4001 |
while (tmp >= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4002 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4003 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4004 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4005 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4006 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4007 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4008 |
* arg is a compiled block with code - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4009 |
* directly call it without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4010 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4011 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4012 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4013 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4014 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4015 |
* arg is a compiled block with bytecode - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4016 |
* directly call interpreter without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4017 |
*/ |
18227 | 4018 |
# ifdef PASS_ARG_POINTER |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4019 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4020 |
OBJ idx; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4021 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4022 |
idx = __mkSmallInteger(tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4023 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4024 |
} |
18227 | 4025 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4026 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
18227 | 4027 |
# endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4028 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4029 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4030 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4031 |
* arg is something else - call it with #value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4032 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4033 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4034 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4035 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4036 |
tmp += step; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4037 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4038 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4039 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4040 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4041 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4042 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4043 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4044 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4045 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4046 |
* arg is a compiled block with code - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4047 |
* directly call it without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4048 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4049 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4050 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4051 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4052 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4053 |
* arg is a compiled block with bytecode - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4054 |
* directly call interpreter without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4055 |
*/ |
18227 | 4056 |
# ifdef PASS_ARG_POINTER |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4057 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4058 |
OBJ idx; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4059 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4060 |
idx = __mkSmallInteger(tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4061 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4062 |
} |
18227 | 4063 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4064 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
18227 | 4065 |
# endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4066 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4067 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4068 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4069 |
* arg is something else - call it with #value: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4070 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4071 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4072 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4073 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4074 |
tmp += step; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4075 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4076 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4077 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4078 |
# undef BLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4079 |
# undef IBLOCK_ARG |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4080 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4081 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4082 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4083 |
* arg is something else - call it with #value: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4084 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4085 |
if (step < 0) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4086 |
while (tmp >= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4087 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4088 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4089 |
(*blockVal.ilc_func)(aBlock, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4090 |
@symbol(value:), |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4091 |
nil, &blockVal, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4092 |
__mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4093 |
tmp += step; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4094 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4095 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4096 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4097 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4098 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4099 |
(*blockVal.ilc_func)(aBlock, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4100 |
@symbol(value:), |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4101 |
nil, &blockVal, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4102 |
__mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4103 |
tmp += step; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4104 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4105 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4106 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4107 |
RETURN ( self ); |
701 | 4108 |
} |
18227 | 4109 |
#endif |
1672
1b56d6e95c9e
changes to call dynamic compiled code right after compilation.
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
4110 |
%}. |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4111 |
"/ |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4112 |
"/ 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
|
4113 |
"/ pass on to super ... |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4114 |
|
701 | 4115 |
^ super to:stop by:incr do:aBlock |
4116 |
||
4117 |
" |
|
4118 |
1 to:10 by:3 do:[:i | i printNewline] |
|
4119 |
" |
|
4120 |
! |
|
4121 |
||
1 | 4122 |
to:stop do:aBlock |
62 | 4123 |
"evaluate aBlock for every integer between (and including) the receiver |
4124 |
and the argument, stop. |
|
357 | 4125 |
Reimplemented as primitive for speed" |
1 | 4126 |
|
4127 |
%{ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4128 |
#ifndef __SCHTEAM__ |
1 | 4129 |
REGISTER INT tmp; |
4130 |
INT final; |
|
216 | 4131 |
static struct inlineCache blockVal = __ILC1(0); |
1 | 4132 |
|
252 | 4133 |
if (__isSmallInteger(stop)) { |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4134 |
tmp = __intVal(self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4135 |
final = __intVal(stop); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4136 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4137 |
if (__isBlockLike(aBlock) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4138 |
&& (__BlockInstPtr(aBlock)->b_nargs == __mkSmallInteger(1))) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4139 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4140 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4141 |
* specially tuned version for the most common case, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4142 |
* where called with home on the stack |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4143 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4144 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4145 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4146 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
2254 | 4147 |
|
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4148 |
# define BLOCK_ARG aBlock |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4149 |
{ |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4150 |
|
18227 | 4151 |
# ifdef PARANOIA |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4152 |
if (! ((INT)(__BlockInstPtr(aBlock)->b_flags) & __MASKSMALLINT(F_DYNAMIC))) |
18227 | 4153 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4154 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4155 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4156 |
* static compiled blocks ... |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4157 |
*/ |
18227 | 4158 |
# ifdef __UNROLL_LOOPS__ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4159 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4160 |
* The following code is designed to run as fast as possible; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4161 |
* - taken branches only if interrupts are pending |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4162 |
* - only forward branches (which are usually predicted as not taken) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4163 |
* - unrolled the loop |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4164 |
* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4165 |
* you are not supposed to program like this - I know what I do |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4166 |
*/ |
18227 | 4167 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4168 |
INT t8 = (INT)(__mkSmallInteger(tmp+8)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4169 |
tmp = (INT)(__mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4170 |
final = (INT)(__mkSmallInteger(final)); |
18227 | 4171 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4172 |
INT t8 = tmp+8; |
18227 | 4173 |
# endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4174 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4175 |
for (;;) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4176 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4177 |
while (t8 <= final) { |
18227 | 4178 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4179 |
t8 += (INT)(__MASKSMALLINT(8)); |
18227 | 4180 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4181 |
t8 += 8; |
18227 | 4182 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4183 |
if (InterruptPending != nil) goto interrupted0; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4184 |
continue0: |
18227 | 4185 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4186 |
(*codeVal)(BLOCK_ARG, tmp); |
18227 | 4187 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4188 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
18227 | 4189 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4190 |
if (InterruptPending != nil) goto interrupted1; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4191 |
continue1: |
18227 | 4192 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4193 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(1)) ); |
18227 | 4194 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4195 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+1)); |
18227 | 4196 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4197 |
if (InterruptPending != nil) goto interrupted2; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4198 |
continue2: |
18227 | 4199 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4200 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(2)) ); |
18227 | 4201 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4202 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+2)); |
18227 | 4203 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4204 |
if (InterruptPending != nil) goto interrupted3; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4205 |
continue3: |
18227 | 4206 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4207 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(3)) ); |
18227 | 4208 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4209 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+3)); |
18227 | 4210 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4211 |
if (InterruptPending != nil) goto interrupted4; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4212 |
continue4: |
18227 | 4213 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4214 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(4)) ); |
18227 | 4215 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4216 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+4)); |
18227 | 4217 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4218 |
if (InterruptPending != nil) goto interrupted5; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4219 |
continue5: |
18227 | 4220 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4221 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(5)) ); |
18227 | 4222 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4223 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+5)); |
18227 | 4224 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4225 |
if (InterruptPending != nil) goto interrupted6; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4226 |
continue6: |
18227 | 4227 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4228 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(6)) ); |
18227 | 4229 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4230 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+6)); |
18227 | 4231 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4232 |
if (InterruptPending != nil) goto interrupted7; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4233 |
continue7: |
18227 | 4234 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4235 |
(*codeVal)(BLOCK_ARG, tmp+(INT)(__MASKSMALLINT(7)) ); |
18227 | 4236 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4237 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp+7)); |
18227 | 4238 |
# endif |
4239 |
||
4240 |
# if TAG_INT==1 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4241 |
tmp += (INT)(__MASKSMALLINT(8)); |
18227 | 4242 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4243 |
tmp += 8; |
18227 | 4244 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4245 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4246 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4247 |
if (InterruptPending != nil) goto interruptedX; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4248 |
continueX: |
18227 | 4249 |
# if TAG_INT==1 |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4250 |
(*codeVal)(BLOCK_ARG, tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4251 |
tmp += (INT)(__MASKSMALLINT(1)); |
18227 | 4252 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4253 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4254 |
tmp++; |
18227 | 4255 |
# endif |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4256 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4257 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4258 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4259 |
if (0) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4260 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4261 |
* no discussion about those gotos ... |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4262 |
* ... its better for your CPU's pipelines |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4263 |
* (if you don't understand why, just don't argue). |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4264 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4265 |
interrupted7: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4266 |
__interruptL(@line); goto continue7; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4267 |
interrupted6: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4268 |
__interruptL(@line); goto continue6; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4269 |
interrupted5: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4270 |
__interruptL(@line); goto continue5; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4271 |
interrupted4: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4272 |
__interruptL(@line); goto continue4; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4273 |
interrupted3: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4274 |
__interruptL(@line); goto continue3; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4275 |
interrupted2: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4276 |
__interruptL(@line); goto continue2; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4277 |
interrupted1: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4278 |
__interruptL(@line); goto continue1; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4279 |
interrupted0: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4280 |
__interruptL(@line); goto continue0; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4281 |
interruptedX: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4282 |
__interruptL(@line); goto continueX; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4283 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4284 |
} |
18227 | 4285 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4286 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4287 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4288 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4289 |
tmp ++; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4290 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4291 |
RETURN (self); |
18227 | 4292 |
# endif /* __UNROLL_LOOPS__ */ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4293 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4294 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4295 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4296 |
* mhmh - seems to be a block with dynamic code |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4297 |
* must refetch, to allow dynamic recompilation or code flush. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4298 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4299 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4300 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4301 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) == (OBJFUNC)nil) break; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4302 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4303 |
tmp ++; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4304 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4305 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4306 |
if (tmp > final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4307 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4308 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4309 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4310 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4311 |
} |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4312 |
|
2216 | 4313 |
# undef BLOCK_ARG |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4314 |
|
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4315 |
# define BLOCK_ARG aBlock |
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4316 |
# define IBLOCK_ARG nil |
1036
d79dc9e4c6f5
binary storage stuff is not needed here
Claus Gittinger <cg@exept.de>
parents:
927
diff
changeset
|
4317 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4318 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4319 |
* sorry - must check for the blocks code within the loops; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4320 |
* it could be recompiled or flushed (in the interrupt) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4321 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4322 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4323 |
REGISTER OBJFUNC codeVal; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4324 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4325 |
if ((codeVal = __BlockInstPtr(aBlock)->b_code) != (OBJFUNC)nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4326 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4327 |
* arg is a compiled block with code - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4328 |
* directly call it without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4329 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4330 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4331 |
/* stay here, while no interrupts are pending ... */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4332 |
do { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4333 |
(*codeVal)(BLOCK_ARG, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4334 |
if (InterruptPending != nil) goto outerLoop; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4335 |
tmp++; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4336 |
} while (tmp <= final); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4337 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4338 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4339 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4340 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4341 |
if (__BlockInstPtr(aBlock)->b_bytecodes != nil) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4342 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4343 |
* arg is a compiled block with bytecode - |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4344 |
* directly call interpreter without going through Block>>value |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4345 |
*/ |
18227 | 4346 |
# ifdef PASS_ARG_POINTER |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4347 |
{ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4348 |
OBJ idx; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4349 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4350 |
idx = __mkSmallInteger(tmp); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4351 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, &idx); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4352 |
} |
18227 | 4353 |
# else |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4354 |
__interpret(aBlock, 1, nil, IBLOCK_ARG, nil, nil, __mkSmallInteger(tmp)); |
18227 | 4355 |
# endif |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4356 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4357 |
} else { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4358 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4359 |
* arg is something else - call it with #value: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4360 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4361 |
(*blockVal.ilc_func)(aBlock, @symbol(value:), nil, &blockVal, __mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4362 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4363 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4364 |
outerLoop: ; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4365 |
tmp++; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4366 |
} |
2650
8a31202275ff
oops - last optimization was wrong
Claus Gittinger <cg@exept.de>
parents:
2649
diff
changeset
|
4367 |
|
2216 | 4368 |
# undef BLOCK_ARG |
4369 |
# undef IBLOCK_ARG |
|
1 | 4370 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4371 |
RETURN (self); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4372 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4373 |
/* |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4374 |
* arg is something else - call it with #value: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4375 |
*/ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4376 |
while (tmp <= final) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4377 |
if (InterruptPending != nil) __interruptL(@line); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4378 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4379 |
(*blockVal.ilc_func)(aBlock, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4380 |
@symbol(value:), |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4381 |
nil, &blockVal, |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4382 |
__mkSmallInteger(tmp)); |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4383 |
tmp++; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4384 |
} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4385 |
RETURN ( self ); |
1 | 4386 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4387 |
#endif /* not __SCHTEAM__ */ |
400 | 4388 |
%}. |
2187
37b82d044439
care for blocks being recompiled, or code being flushed while
Claus Gittinger <cg@exept.de>
parents:
1964
diff
changeset
|
4389 |
|
400 | 4390 |
"/ |
4391 |
"/ 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
|
4392 |
"/ pass on to super ... |
400 | 4393 |
"/ |
62 | 4394 |
^ super to:stop do:aBlock |
4395 |
||
216 | 4396 |
" |
4397 |
1 to:10 do:[:i | i printNewline] |
|
4398 |
" |
|
701 | 4399 |
! ! |
1 | 4400 |
|
701 | 4401 |
!SmallInteger methodsFor:'misc math'! |
1 | 4402 |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4403 |
bernoulli |
8908 | 4404 |
"returns the nth Bernoulli number. |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4405 |
The series runs this: |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4406 |
1, 1/2, 1/6, 0, -1/30, 0, 1/42, 0, -1/30, 0, 5/66, 0, -691/2730, etc |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4407 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4408 |
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
|
4409 |
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
|
4410 |
Used with taylor series for tan" |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4411 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4412 |
|table p| |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4413 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4414 |
table := #( |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4415 |
(1 6) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4416 |
(-1 30) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4417 |
(1 42) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4418 |
(-1 30) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4419 |
(5 66) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4420 |
(-691 2730) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4421 |
(7 6) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4422 |
(-3617 510) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4423 |
(43867 798) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4424 |
(-174611 330) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4425 |
(854513 138) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4426 |
(-236364091 2730) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4427 |
(8553103 6) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4428 |
(-23749461029 870) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4429 |
(8615841276005 14322) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4430 |
(-7709321041217 510) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4431 |
(2577687858367 6) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4432 |
(-26315271553053477373 1919190) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4433 |
(2929993913841559 6) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4434 |
(-261082718496449122051 13530) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4435 |
). |
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4436 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4437 |
self even ifTrue:[ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4438 |
self == 0 ifTrue:[^1]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4439 |
p := table at:(self / 2). |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4440 |
^ 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
|
4441 |
]. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4442 |
self == 1 ifTrue:[ ^ (1 / 2) ]. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4443 |
^ 0. |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4444 |
|
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4445 |
" |
8908 | 4446 |
0 bernoulli |
4447 |
1 bernoulli |
|
4448 |
2 bernoulli |
|
4449 |
3 bernoulli |
|
4450 |
4 bernoulli |
|
4451 |
5 bernoulli |
|
4452 |
6 bernoulli |
|
4453 |
8 bernoulli |
|
7438
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4454 |
38 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4455 |
40 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4456 |
41 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4457 |
42 bernoulli |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4458 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4459 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4460 |
"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
|
4461 |
! |
f5f6fd13b1df
code refactoring for metaNumbers and complete double dispatch.
Claus Gittinger <cg@exept.de>
parents:
7356
diff
changeset
|
4462 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4463 |
divMod:aNumber |
18227 | 4464 |
"return an array filled with |
20339 | 4465 |
(self // aNumber) and (self \\ aNumber). |
17036 | 4466 |
The returned remainder has the same sign as aNumber. |
17033 | 4467 |
The following is always true: |
20339 | 4468 |
(receiver // something) * something + (receiver \\ something) = receiver |
17036 | 4469 |
|
4470 |
Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. |
|
18227 | 4471 |
Especially surprising: |
20339 | 4472 |
-1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, |
4473 |
and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). |
|
4474 |
-10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, |
|
4475 |
and -4 * 4 gives -12, so we need to add 2 to get the original -10. |
|
17036 | 4476 |
|
4477 |
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
|
4478 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4479 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4480 |
#ifndef __SCHTEAM__ |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4481 |
INT val, div, mod, mySelf; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4482 |
|
17033 | 4483 |
if (__isSmallInteger(aNumber) |
4484 |
&& ((val = __intVal(aNumber)) > 0) |
|
4485 |
&& ((mySelf = __intVal(self)) >= 0)) { |
|
20339 | 4486 |
div = mySelf / val; |
4487 |
mod = mySelf % val; |
|
4488 |
||
4489 |
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
|
4490 |
} |
18227 | 4491 |
#endif |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4492 |
%}. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4493 |
^ super divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4494 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4495 |
" |
17033 | 4496 |
10 // 3 -> 3 |
4497 |
10 \\ 3 -> 1 |
|
4498 |
||
4499 |
10 // -3 -> -4 |
|
4500 |
10 \\ -3 -> -2 |
|
4501 |
||
4502 |
-10 // 3 -> -4 |
|
4503 |
-10 \\ 3 -> 2 |
|
4504 |
||
4505 |
-10 // -3 -> 3 |
|
4506 |
-10 \\ -3 -> -1 |
|
4507 |
||
4508 |
-78 \\ 10 2 |
|
4509 |
-78 // 10 -8 |
|
4510 |
||
4511 |
10 divMod:3 -> #(3 1) because 3*3 + 1 = 10 |
|
4512 |
10 divMod:-3 -> #(-4 -2) because -4*-3 + (-2) = 10 |
|
18227 | 4513 |
-10 divMod:3 -> #(-4 2) because -4*3 + 2 = -10 |
17033 | 4514 |
-10 divMod:-3 -> #(3 -1) because -3*3 + (-1) = -10 |
4515 |
||
4516 |
1000000000000000000000 divMod:3 -> #(333333333333333333333 1) |
|
17036 | 4517 |
1000000000000000000000 divMod:-3 -> #(-333333333333333333334 -2) |
4518 |
-1000000000000000000000 divMod:3 -> #(-333333333333333333334 2) |
|
4519 |
-1000000000000000000000 divMod:-3 -> #(333333333333333333333 -1) |
|
18227 | 4520 |
100 factorial divMod:103 |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4521 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4522 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
4523 |
|
701 | 4524 |
gcd:anInteger |
4525 |
"return the greatest common divisor (Euclid's algorithm). |
|
4526 |
This has been redefined here for more speed since due to the |
|
4527 |
use of gcd in Fraction code, it has become time-critical for |
|
4528 |
some code. (thanx to MessageTally)" |
|
4529 |
||
4530 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4531 |
#ifndef __SCHTEAM__ |
701 | 4532 |
if (__isSmallInteger(anInteger)) { |
20339 | 4533 |
INT orgArg, ttt, selfInt, orgSelfInt, temp; |
4534 |
||
4535 |
ttt = orgArg = __intVal(anInteger); |
|
4536 |
if (ttt) { |
|
4537 |
selfInt = orgSelfInt = __intVal(self); |
|
4538 |
while (ttt != 0) { |
|
4539 |
temp = selfInt % ttt; |
|
4540 |
selfInt = ttt; |
|
4541 |
ttt = temp; |
|
4542 |
} |
|
4543 |
/* |
|
4544 |
* since its not defined in C, what the sign of |
|
4545 |
* a modulus result is when the arg is negative, |
|
4546 |
* change it explicitely here ... |
|
4547 |
*/ |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4548 |
if ((orgArg < 0) != (orgSelfInt < 0)) { |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4549 |
/* flip result's sign */ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4550 |
selfInt = -selfInt; |
20339 | 4551 |
} |
4552 |
RETURN ( __mkSmallInteger(selfInt) ); |
|
4553 |
} |
|
1 | 4554 |
} |
18227 | 4555 |
#endif |
4556 |
%}. |
|
701 | 4557 |
^ super gcd:anInteger |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4558 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4559 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4560 |
45 gcd:30 15 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4561 |
-45 gcd:30 15 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4562 |
45 gcd:-30 -15 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4563 |
-45 gcd:-30 -15 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4564 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4565 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4566 |
"Modified: / 18-01-2019 / 16:08:31 / Claus Gittinger" |
701 | 4567 |
! |
62 | 4568 |
|
2430
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4569 |
gcd_helper:anInteger |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4570 |
"same as gcd - see knuth & Integer>>gcd:" |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4571 |
|
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4572 |
^ self gcd:anInteger |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4573 |
|
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4574 |
"Created: 1.3.1997 / 16:58:01 / cg" |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4575 |
! |
cbbf16de1bd7
better gcd for large numbers.
Claus Gittinger <cg@exept.de>
parents:
2254
diff
changeset
|
4576 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4577 |
integerLog10 |
14492 | 4578 |
"return the truncation of log10 of the receiver. |
4579 |
The same as (self log:10) floor. |
|
4580 |
Stupid implementation, which is used to find out the number of digits needed |
|
701 | 4581 |
to print a number/and for conversion to a LargeInteger. |
7356 | 4582 |
Implemented that way, to allow for tiny systems (PDAs) without a Float class |
701 | 4583 |
(i.e. without log)." |
2 | 4584 |
|
15600 | 4585 |
self > 0 ifTrue:[ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4586 |
self < 10000 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4587 |
self < 10 ifTrue:[^ 0]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4588 |
self < 100 ifTrue:[^ 1]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4589 |
self < 1000 ifTrue:[^ 2]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4590 |
^ 3 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4591 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4592 |
self < 100000000 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4593 |
self < 100000 ifTrue:[^ 4]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4594 |
self < 1000000 ifTrue:[^ 5]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4595 |
self < 10000000 ifTrue:[^ 6]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4596 |
^ 7 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4597 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4598 |
self < 1000000000 ifTrue:[^ 8]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4599 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4600 |
SmallInteger maxBytes == 4 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4601 |
"/ on a 32 bit machine, SmallInt cannot be larger |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4602 |
^ 9 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4603 |
] ifFalse:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4604 |
"/ 64 bit machine |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4605 |
self < 100000000000000 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4606 |
self < 10000000000 ifTrue:[^ 9]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4607 |
self < 100000000000 ifTrue:[^ 10]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4608 |
self < 1000000000000 ifTrue:[^ 11]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4609 |
self < 10000000000000 ifTrue:[^ 12]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4610 |
^ 13 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4611 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4612 |
self < 1000000000000000 ifTrue:[^ 14]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4613 |
self < 10000000000000000 ifTrue:[^ 15]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4614 |
self < 100000000000000000 ifTrue:[^ 16]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4615 |
self < 1000000000000000000 ifTrue:[^ 17]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4616 |
^ 18. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4617 |
]. |
14492 | 4618 |
]. |
16039 | 4619 |
|
15600 | 4620 |
^ self class |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4621 |
raise:(self = 0 ifTrue:[#infiniteResultSignal] ifFalse:[#domainErrorSignal]) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4622 |
receiver:self |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4623 |
selector:#integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4624 |
arguments:#() |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4625 |
errorString:'bad receiver in log10 (not strictly positive)' |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4626 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4627 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4628 |
99 integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4629 |
100 integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4630 |
101 integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4631 |
(101 log:10) floor |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4632 |
120 integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4633 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4634 |
-1 integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4635 |
0 integerLog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4636 |
Number trapInfinity:[ 0 integerLog10 ] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4637 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4638 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4639 |
"Created: / 02-07-2017 / 01:19:09 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4640 |
"Modified: / 28-08-2017 / 13:53:32 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4641 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4642 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4643 |
intlog10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4644 |
<resource: #obsolete> |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4645 |
"return the truncation of log10 of the receiver. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4646 |
The same as (self log:10) floor. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4647 |
Stupid implementation, which is used to find out the number of digits needed |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4648 |
to print a number/and for conversion to a LargeInteger. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4649 |
Implemented that way, to allow for tiny systems (PDAs) without a Float class |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4650 |
(i.e. without log)." |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4651 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4652 |
^ self integerLog10 |
15600 | 4653 |
|
8908 | 4654 |
" |
18227 | 4655 |
99 intlog10 |
4656 |
100 intlog10 |
|
14632 | 4657 |
101 intlog10 |
4658 |
(101 log:10) floor |
|
8908 | 4659 |
120 intlog10 |
6344 | 4660 |
-1 intlog10 |
4661 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4662 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4663 |
"Modified: / 02-07-2017 / 01:19:05 / cg" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4664 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4665 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4666 |
raisedToInteger:exp |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4667 |
"return the receiver raised to an integer exp" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4668 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4669 |
"hack for fast power-of-2 raising" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4670 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4671 |
exp <= 0 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4672 |
exp == 0 ifTrue:[^ 1]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4673 |
self == 0 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4674 |
^ self class |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4675 |
raise:#domainErrorSignal |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4676 |
receiver:self |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4677 |
selector:#raisedToInteger: |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4678 |
arguments:{exp} |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4679 |
errorString:'attempt to raise 0 by negative power' |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4680 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4681 |
^ 1 / (self raisedToInteger:exp negated) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4682 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4683 |
self == 1 ifTrue:[^ 1]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4684 |
self == 2 ifTrue:[^ self bitShift:(exp - 1)]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4685 |
exp == 1 ifTrue:[^ self]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4686 |
self isPowerOf2 ifTrue:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4687 |
^ self bitShift:(self lowBit - 1) * (exp - 1) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4688 |
]. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4689 |
^ super raisedToInteger:exp |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4690 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4691 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4692 |
-1 raisedToInteger:0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4693 |
-1 raisedToInteger:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4694 |
-1 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4695 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4696 |
2 raisedToInteger:0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4697 |
2 raisedToInteger:1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4698 |
2 raisedToInteger:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4699 |
2 raisedToInteger:10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4700 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4701 |
4 raisedToInteger:0 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4702 |
4 raisedToInteger:1 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4703 |
4 raisedToInteger:2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4704 |
4 raisedToInteger:10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4705 |
8 raisedToInteger:10 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4706 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4707 |
-10 to:100 do:[:a | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4708 |
-10 to:100 do:[:b | |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4709 |
(a = 0 and:[b<0]) ifFalse:[ |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4710 |
Transcript showCR:e'a: {a} b: {b} old: {(a perform:#raisedToInteger: inClass:ArithmeticValue withArguments:{b})} new: {(a raisedToInteger:b)}'. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4711 |
self assert:(a raisedToInteger:b) = (a perform:#raisedToInteger: inClass:ArithmeticValue withArguments:{b}) |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4712 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4713 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4714 |
] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4715 |
Time microsecondsToRun:[(2 raisedTo:448)] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4716 |
Time microsecondsToRun:[(2 perform:#raisedToInteger: inClass:ArithmeticValue withArguments:#(448))] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4717 |
Time microsecondsToRun:[(2 raisedTo:2000)] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4718 |
Time microsecondsToRun:[(2 perform:#raisedToInteger: inClass:ArithmeticValue withArguments:#(2000))] |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4719 |
" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4720 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4721 |
"Created: / 30-05-2020 / 11:08:51 / cg" |
2 | 4722 |
! ! |
4723 |
||
1 | 4724 |
!SmallInteger methodsFor:'printing & storing'! |
4725 |
||
16039 | 4726 |
asBCD |
4727 |
"return an integer which represents the BCD encoded value of the receiver; |
|
4728 |
that is: each digit of its decimal representation is placed into a nibble |
|
18410 | 4729 |
of the result. (aka 162 -> 0x162). The BCD hex string looks like the original decimal. |
16039 | 4730 |
This conversion is useful for some communication protocols, |
16041 | 4731 |
or control systems, which represent numbers this way..." |
16039 | 4732 |
|
4733 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4734 |
#ifndef __SCHTEAM__ |
18410 | 4735 |
// the following code is a leftover from times when division was expensive; |
4736 |
// in modern cpu's, conditional branches are often more expensive than divisions, |
|
4737 |
// so it is questionable, if the effort below is still worth it. |
|
4738 |
// (and asBCD is really used seldom in some serial communication protocols |
|
4739 |
// for control systems) |
|
16039 | 4740 |
int i; |
18227 | 4741 |
INT _10000000s = 0, _1000000s = 0; |
16039 | 4742 |
INT _100000s = 0, _10000s = 0, _1000s = 0; |
4743 |
INT _100s = 0, _10s = 0, _1s = 0; |
|
4744 |
INT b = __intVal(self); |
|
4745 |
unsigned INT rslt; |
|
4746 |
||
4747 |
if (b <= 99999999) { |
|
20339 | 4748 |
if (b <= 255) { |
4749 |
// the most common case: convert bytes |
|
4750 |
for (i=7; i>=0; i--) { |
|
4751 |
if (_100s >= 5) _100s += 3; |
|
4752 |
if (_10s >= 5) _10s += 3; |
|
4753 |
if (_1s >= 5) _1s += 3; |
|
4754 |
||
4755 |
_100s = (_100s<<1) | (_10s >> 3 & 1); _100s &= 0xF; |
|
4756 |
_10s = (_10s<<1) | (_1s >> 3 & 1); _10s &= 0xF; |
|
4757 |
_1s = (_1s<<1) | (b >> 7 & 1); _1s &= 0xF; |
|
4758 |
b <<= 1; |
|
4759 |
} |
|
4760 |
rslt = (_100s<<8) | (_10s<<4) | _1s; |
|
4761 |
RETURN (__MKSMALLINT( rslt) ); |
|
4762 |
} |
|
4763 |
||
4764 |
for (i=26; i>=0; i--) { |
|
4765 |
if (_10000000s >= 5) _10000000s += 3; |
|
4766 |
if (_1000000s >= 5) _1000000s += 3; |
|
4767 |
if (_100000s >= 5) _100000s += 3; |
|
4768 |
if (_10000s >= 5) _10000s += 3; |
|
4769 |
if (_1000s >= 5) _1000s += 3; |
|
4770 |
if (_100s >= 5) _100s += 3; |
|
4771 |
if (_10s >= 5) _10s += 3; |
|
4772 |
if (_1s >= 5) _1s += 3; |
|
4773 |
||
4774 |
_10000000s = (_10000000s<<1) | (_1000000s >> 3 & 1); _10000000s &= 0xF; |
|
4775 |
_1000000s = (_1000000s<<1) | (_100000s >> 3 & 1); _1000000s &= 0xF; |
|
4776 |
_100000s = (_100000s<<1) | (_10000s >> 3 & 1); _100000s &= 0xF; |
|
4777 |
_10000s = (_10000s<<1) | (_1000s >> 3 & 1); _10000s &= 0xF; |
|
4778 |
_1000s = (_1000s<<1) | (_100s >> 3 & 1); _1000s &= 0xF; |
|
4779 |
_100s = (_100s<<1) | (_10s >> 3 & 1); _100s &= 0xF; |
|
4780 |
_10s = (_10s<<1) | (_1s >> 3 & 1); _10s &= 0xF; |
|
4781 |
_1s = (_1s<<1) | (b >> 26 & 1); _1s &= 0xF; |
|
4782 |
b <<= 1; |
|
4783 |
} |
|
4784 |
||
4785 |
rslt = (_10000000s<<28) |
|
4786 |
| (_1000000s<<24) | (_100000s<<20) | (_10000s<<16) |
|
4787 |
| (_1000s<<12) | (_100s<<8) | (_10s<<4) | _1s; |
|
4788 |
RETURN (__MKUINT( rslt) ); |
|
16039 | 4789 |
} |
18227 | 4790 |
#endif |
16039 | 4791 |
%}. |
4792 |
^ super asBCD. |
|
4793 |
||
4794 |
" |
|
18227 | 4795 |
99999999 asBCD hexPrintString |
4796 |
12812345 asBCD hexPrintString |
|
4797 |
128123 asBCD hexPrintString |
|
4798 |
128901 asBCD hexPrintString |
|
4799 |
12890 asBCD hexPrintString |
|
4800 |
1289 asBCD hexPrintString |
|
4801 |
999 asBCD hexPrintString |
|
4802 |
256 asBCD hexPrintString |
|
4803 |
255 asBCD hexPrintString |
|
4804 |
128 asBCD hexPrintString |
|
4805 |
162 asBCD hexPrintString |
|
4806 |
||
4807 |
999999999 asBCD hexPrintString |
|
4808 |
128123456 asBCD hexPrintString |
|
16039 | 4809 |
|
4810 |
" |
|
4811 |
! |
|
4812 |
||
62 | 4813 |
printOn:aStream |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4814 |
"append my printstring (base 10) to aStream. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4815 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4816 |
I use #printString instead of #printOn: as basic print mechanism." |
62 | 4817 |
|
4818 |
aStream nextPutAll:(self printString) |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4819 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4820 |
"Modified (comment): / 10-10-2017 / 14:09:00 / cg" |
62 | 4821 |
! |
4822 |
||
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
4823 |
printOn:aStream base:base showRadix:showRadix |
5988 | 4824 |
"append a string representation of the receiver in the specified numberBase to aStream |
4825 |
(if showRadix is true, with initial XXr) |
|
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
4826 |
The base argument should be between 2 and 36." |
6144 | 4827 |
|
20141 | 4828 |
|absBase| |
4829 |
||
4830 |
(base isInteger and:[absBase := base abs. absBase between:2 and:36]) ifTrue:[ |
|
20339 | 4831 |
showRadix ifTrue:[ |
4832 |
absBase printOn:aStream. |
|
4833 |
aStream nextPut:$r. |
|
4834 |
]. |
|
4835 |
aStream nextPutAll:(self printStringRadix:base) |
|
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
4836 |
] ifFalse:[ |
20339 | 4837 |
super printOn:aStream base:base showRadix:true. |
11792
6782c723834a
changed #printOn:based:showRadix:
Stefan Vogel <sv@exept.de>
parents:
11728
diff
changeset
|
4838 |
]. |
5988 | 4839 |
|
12977
fbf5437b15fb
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12876
diff
changeset
|
4840 |
"Created: / 07-09-2001 / 13:54:40 / cg" |
fbf5437b15fb
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12876
diff
changeset
|
4841 |
"Modified: / 02-08-2010 / 12:25:20 / cg" |
701 | 4842 |
! |
4843 |
||
1 | 4844 |
printString |
4845 |
"return my printstring (base 10)" |
|
4846 |
||
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4847 |
"since this was heavily used in some applications, |
68 | 4848 |
here is an exception to the rule of basing printString |
4849 |
upon the printOn: method." |
|
4850 |
||
1 | 4851 |
%{ /* NOCONTEXT */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4852 |
#ifdef __SCHTEAM__ |
18227 | 4853 |
return context._RETURN( new STString( java.lang.Long.toString(self.longValue()) )); |
4854 |
#else |
|
18592 | 4855 |
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
|
4856 |
char *cp; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
4857 |
OBJ newString = nil; |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4858 |
INT myValue; |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4859 |
int negative = 0; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
4860 |
|
15259 | 4861 |
if (self == __MKSMALLINT(0)) { |
20339 | 4862 |
RETURN (@global(ZeroString)); |
15259 | 4863 |
// RETURN (__MKSTRING_L("0", 1)); |
4864 |
} |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
4865 |
myValue = __intVal(self); |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4866 |
if (myValue < 0) { |
20339 | 4867 |
negative = 1; |
4868 |
myValue = -myValue; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4869 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4870 |
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
|
4871 |
*cp-- = '\0'; |
18592 | 4872 |
for ( ; myValue != 0; cp--) { |
20339 | 4873 |
*cp = '0' + (myValue % 10); |
4874 |
myValue /= 10; |
|
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4875 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4876 |
if (negative) { |
20339 | 4877 |
*cp-- = '-'; |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4878 |
} |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4879 |
newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp)); |
282 | 4880 |
if (newString != nil) { |
20339 | 4881 |
RETURN (newString); |
282 | 4882 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
4883 |
#endif /* not __SCHTEAM__ */ |
282 | 4884 |
%}. |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4885 |
"/ only arrive here, |
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4886 |
"/ when having memory problems (i.e. no space for string) ... |
282 | 4887 |
^ super printString |
3704
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4888 |
|
4dd3cb7ae956
SmallInteger>>printString now twice as fast (for andi and olbi)
Claus Gittinger <cg@exept.de>
parents:
3684
diff
changeset
|
4889 |
" |
8908 | 4890 |
1234 printString |
4891 |
0 printString |
|
4892 |
-100 printString |
|
15259 | 4893 |
|
4894 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 123456789012 printString ]] 180 180 180 170 180 |
|
4895 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 12345678 printString ]] 140 150 140 150 140 |
|
4896 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 1234 printString ]] 130 140 130 130 130 |
|
4897 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 12 printString ]] 130 120 120 120 110 |
|
4898 |
Time millisecondsToRun:[ 1000000 timesRepeat:[ 5 printString ]] 110 110 100 110 90 |
|
18227 | 4899 |
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
|
4900 |
" |
1 | 4901 |
! |
4902 |
||
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
4903 |
printStringRadix:base |
20141 | 4904 |
"return my printstring (optimized for bases 16, 10 and 8). |
4905 |
Print digits > 0 as uppercase chars if base > 0, |
|
4906 |
as lowercase chars if base < 0." |
|
1 | 4907 |
|
3705 | 4908 |
|s| |
4909 |
||
4910 |
%{ |
|
18401 | 4911 |
#ifdef __SCHTEAM__ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
4912 |
int __base = Math.abs(base.intValue()); |
18401 | 4913 |
long myValue = self.longValue(); |
4914 |
java.lang.String __s; |
|
4915 |
||
4916 |
switch (__base) { |
|
20339 | 4917 |
case 2: |
4918 |
__s = java.lang.Long.toBinaryString(myValue); |
|
4919 |
break; |
|
4920 |
||
4921 |
case 8: |
|
4922 |
__s = java.lang.Long.toOctalString(myValue); |
|
4923 |
break; |
|
4924 |
||
4925 |
case 10: |
|
4926 |
__s = java.lang.Long.toString(myValue); |
|
4927 |
break; |
|
4928 |
||
4929 |
case 16: |
|
4930 |
__s = java.lang.Long.toHexString(myValue); |
|
4931 |
break; |
|
4932 |
||
4933 |
default: |
|
4934 |
{ |
|
4935 |
boolean negative = false; |
|
4936 |
__s = ""; |
|
4937 |
||
4938 |
if ((__base > 36) || (__base < 2)) { |
|
4939 |
throw new SmalltalkError("invalid base: ", base); |
|
4940 |
} |
|
4941 |
if (myValue < 0) { |
|
4942 |
negative = true; |
|
4943 |
myValue = -myValue; |
|
4944 |
} |
|
4945 |
while (myValue != 0) { |
|
4946 |
int digit; |
|
4947 |
char ch; |
|
4948 |
||
4949 |
digit = (int)(myValue % __base); |
|
4950 |
if (digit <= 9) { |
|
4951 |
ch = (char)('0' + digit); |
|
4952 |
} else { |
|
4953 |
ch = (char)('A' + digit - 10); |
|
4954 |
} |
|
4955 |
__s = ch + __s; |
|
4956 |
myValue = myValue / __base; |
|
4957 |
} |
|
4958 |
if (negative) { |
|
4959 |
__s = "-" + __s; |
|
4960 |
} |
|
4961 |
break; |
|
4962 |
} |
|
18401 | 4963 |
} |
4964 |
return context._RETURN( new STString( __s )); |
|
4965 |
#else |
|
20141 | 4966 |
static char ucDigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
4967 |
static char lcDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
|
1 | 4968 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
4969 |
if (__isSmallInteger(base)) { |
20339 | 4970 |
char *digits; |
4971 |
INT __base; |
|
4972 |
||
4973 |
if (self == __MKSMALLINT(0)) { |
|
4974 |
RETURN (@global(ZeroString)); |
|
4975 |
} |
|
4976 |
__base = __intVal(base); |
|
4977 |
if (__base < 0) { |
|
4978 |
__base = - __base; |
|
4979 |
digits = lcDigits; |
|
4980 |
} else { |
|
4981 |
digits = ucDigits; |
|
4982 |
} |
|
4983 |
||
4984 |
if ((__base < sizeof(ucDigits)) && (__base > 1)) { |
|
4985 |
char buffer[64+5]; /* for 64bit machines, base 2, plus sign, plus 0-byte */ |
|
4986 |
char *cp; |
|
4987 |
OBJ newString; |
|
4988 |
int negative = 0; |
|
4989 |
INT myValue = __intVal(self); |
|
4990 |
||
4991 |
if (myValue < 0) { |
|
4992 |
negative = 1; |
|
4993 |
myValue = -myValue; |
|
4994 |
} |
|
4995 |
cp = buffer + sizeof(buffer) - 1; |
|
4996 |
*cp-- = '\0'; |
|
4997 |
for (; myValue != 0; cp--) { |
|
4998 |
*cp = digits[myValue % __base]; |
|
4999 |
myValue /= __base; |
|
5000 |
} |
|
5001 |
if (negative) { |
|
5002 |
*cp-- = '-'; |
|
5003 |
} |
|
5004 |
newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp)); |
|
5005 |
if (newString != nil) { |
|
5006 |
RETURN (newString); |
|
5007 |
} |
|
5008 |
} |
|
1 | 5009 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5010 |
#endif /* not __SCHTEAM__ */ |
314 | 5011 |
%}. |
3705 | 5012 |
"/ arrive here, for bad base, |
5013 |
"/ or when having memory problems (i.e. no space for string) ... |
|
5014 |
||
5015 |
"/ |
|
5016 |
"/ fall back for seldom used bases |
|
20330 | 5017 |
"/ Notice: cannot use super>>printStringRadix: here, |
3705 | 5018 |
"/ since that would lead to endless recursion ... |
5019 |
"/ (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
|
5020 |
|
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5021 |
s := WriteStream on:(String new:10). |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5022 |
super printOn:s base:base. |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5023 |
^ s contents. |
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5024 |
|
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5025 |
" |
20333 | 5026 |
127 printStringRadix:16 |
5027 |
127 printStringRadix:-16 |
|
5028 |
-127 printStringRadix:16 |
|
5029 |
-127 printStringRadix:-16 |
|
8908 | 5030 |
123 printStringRadix:12 |
5031 |
123 printStringRadix:10 |
|
5032 |
123 printStringRadix:8 |
|
5033 |
123 printStringRadix:3 |
|
5034 |
123 printStringRadix:2 |
|
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5035 |
123 printStringRadix:1 |
18592 | 5036 |
35 printStringRadix:36 |
5037 |
123 printStringRadix:37 |
|
3705 | 5038 |
|
8908 | 5039 |
-127 printStringRadix:16 |
5040 |
-123 printStringRadix:12 |
|
5041 |
-123 printStringRadix:10 |
|
5042 |
-123 printStringRadix:8 |
|
5043 |
-123 printStringRadix:3 |
|
5044 |
-123 printStringRadix:2 |
|
20330 | 5045 |
|
20333 | 5046 |
16r3FFFFFFF printStringRadix:16 |
5047 |
16r7FFFFFFF printStringRadix:16 |
|
5048 |
16rFFFFFFFF printStringRadix:16 |
|
5049 |
16r3FFFFFFFFFFFFFFF printStringRadix:16 |
|
5050 |
16r7FFFFFFFFFFFFFFF printStringRadix:16 |
|
5051 |
16rFFFFFFFFFFFFFFFF printStringRadix:16 |
|
5052 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5053 |
16r-3FFFFFFF printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5054 |
16r-40000000 printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5055 |
16r-7FFFFFFF printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5056 |
16r-80000000 printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5057 |
16r-FFFFFFFF printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5058 |
16r-3FFFFFFFFFFFFFFF printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5059 |
16r-7FFFFFFFFFFFFFFF printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5060 |
16r-FFFFFFFFFFFFFFFF printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5061 |
16r-4000000000000000 printStringRadix:16 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5062 |
16r-8000000000000000 printStringRadix:16 |
3189
f07145cc8ef7
Have to call "super #printOn:base:" in #printString.
Stefan Vogel <sv@exept.de>
parents:
3156
diff
changeset
|
5063 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5064 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5065 |
"Modified: / 19-09-2017 / 16:34:40 / stefan" |
1 | 5066 |
! |
5067 |
||
5068 |
printfPrintString:formatString |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5069 |
<unsave> |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5070 |
|
7746 | 5071 |
"non-standard, but sometimes useful. |
1 | 5072 |
return a printed representation of the receiver |
15259 | 5073 |
as specified by formatString, which is defined by the C-function 'printf'. |
8908 | 5074 |
No checking for string overrun - the resulting string |
62 | 5075 |
must be shorter than 256 chars or else ... |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5076 |
|
2781 | 5077 |
Notice that a conversion may not be portable; for example, |
5078 |
to correctly convert an int on a 64-bit alpha, a %ld is required, |
|
14632 | 5079 |
on 64bit mingw or visualc, %lld is required, |
5080 |
while other systems may be happy with a %d. |
|
15259 | 5081 |
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
|
5082 |
Use at your own risk (if at all). |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5083 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5084 |
This method is NONSTANDARD and may be removed without notice; |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5085 |
it is provided to allow special conversions in very special situations. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5086 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5087 |
WARNING: 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
|
5088 |
Please use the printf: method, which is safe as it is completely implemented in Smalltalk." |
1 | 5089 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5090 |
%{ /* STACK: 8000 */ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5091 |
#ifndef __SCHTEAM__ |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5092 |
char buffer[1024]; |
314 | 5093 |
OBJ s; |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
5094 |
int len; |
1 | 5095 |
|
12482
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
5096 |
if (__isStringLike(formatString)) { |
20339 | 5097 |
/* |
5098 |
* actually only needed on sparc: since thisContext is |
|
5099 |
* in a global register, which gets destroyed by printf, |
|
5100 |
* manually save it here - very stupid ... |
|
5101 |
*/ |
|
5102 |
__BEGIN_PROTECT_REGISTERS__ |
|
5103 |
||
5104 |
len = snprintf(buffer, sizeof(buffer), __stringVal(formatString), __intVal(self)); |
|
5105 |
||
5106 |
__END_PROTECT_REGISTERS__ |
|
5107 |
||
5108 |
if (len < 0) goto fail; |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5109 |
if (len >= sizeof(buffer)) goto fail; |
20339 | 5110 |
|
5111 |
s = __MKSTRING_L(buffer, len); |
|
5112 |
if (s != nil) { |
|
5113 |
RETURN (s); |
|
5114 |
} |
|
1 | 5115 |
} |
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
5116 |
fail: ; |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5117 |
#endif /* not __SCHTEAM__ */ |
314 | 5118 |
%}. |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5119 |
^ super printfPrintString:formatString |
1 | 5120 |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
5121 |
" |
20339 | 5122 |
123 printfPrintString:'%%d -> %d' |
5123 |
123 printfPrintString:'%%6d -> %6d' |
|
5124 |
123 printfPrintString:'%%x -> %x' |
|
5125 |
123 printfPrintString:'%%4x -> %4x' |
|
5126 |
123 printfPrintString:'%%04x -> %04x' |
|
8417
dcba76518224
Security: use snprintf() instead of sprintf() to avoid overwrites.
Stefan Vogel <sv@exept.de>
parents:
8408
diff
changeset
|
5127 |
" |
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5128 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5129 |
"Modified: / 03-07-2017 / 15:07:37 / cg" |
1 | 5130 |
! ! |
701 | 5131 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
5132 |
!SmallInteger methodsFor:'private'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
5133 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5134 |
numberOfDigits:n8BitDigits |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5135 |
"initialize the instance to store n8BitDigits. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5136 |
It is a no-op for SmallIntegers." |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5137 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5138 |
^ self. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5139 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5140 |
"Created: / 01-11-2018 / 11:29:45 / Stefan Vogel" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5141 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5142 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5143 |
numberOfDigits:n8BitDigits sign:sign |
18382 | 5144 |
"private: for protocol completeness with LargeIntegers. |
18897 | 5145 |
Returns a smallInteger with my absValue and the sign of the argument. |
5146 |
The method's name may be misleading: the receiver is not changed, |
|
5147 |
but a new number is returned." |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
5148 |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5149 |
^ self setSign:sign. |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5150 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5151 |
"Created: / 01-11-2018 / 12:15:17 / Stefan Vogel" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
5152 |
! ! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
829
diff
changeset
|
5153 |
|
16294 | 5154 |
!SmallInteger methodsFor:'special modulo arithmetic'! |
5155 |
||
5156 |
plus32:aNumber |
|
5157 |
"return the sum of the receiver and the argument, as SmallInteger. |
|
5158 |
The argument must be another SmallInteger. |
|
5159 |
If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned. |
|
5160 |
This is of course not always correct, but allows for C/Java behavior to be emulated." |
|
5161 |
||
5162 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5163 |
#ifndef __SCHTEAM__ |
16294 | 5164 |
INT sum; |
5165 |
||
5166 |
sum = __unsignedLongIntVal(self) + __unsignedLongIntVal(aNumber); |
|
18227 | 5167 |
# if __POINTER_SIZE__ == 8 |
16294 | 5168 |
sum &= 0xFFFFFFFFL; |
18227 | 5169 |
# endif |
16294 | 5170 |
RETURN ( __MKUINT(sum)); |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5171 |
#endif /* not __SCHTEAM__ */ |
16294 | 5172 |
%}. |
5173 |
self primitiveFailed |
|
5174 |
||
5175 |
" |
|
5176 |
16r7FFFFFFF + 1 2147483648 |
|
5177 |
16r7FFFFFFF plus32: 1 |
|
5178 |
" |
|
5179 |
! |
|
5180 |
||
5181 |
plus:aNumber |
|
5182 |
"return the sum of the receiver and the argument, as SmallInteger. |
|
5183 |
The argument must be another SmallInteger. |
|
5184 |
If the result overflows the smallInteger range, the value modulo the |
|
5185 |
smallInteger range is returned (i.e. the low bits of the sum). |
|
5186 |
This is of course not always correct, but some code does a modulo anyway |
|
5187 |
and can therefore speed things up by not going through LargeIntegers." |
|
5188 |
||
5189 |
%{ /* NOCONTEXT */ |
|
18255 | 5190 |
#ifdef __SCHTEAM__ |
5191 |
{ |
|
20339 | 5192 |
long myValue = self.longValue(); |
5193 |
long otherValue = aNumber.longValue(); |
|
5194 |
long sum = myValue + otherValue; |
|
5195 |
return context._RETURN( STInteger._new(sum) ); |
|
18255 | 5196 |
} |
5197 |
/* NOT REACHED */ |
|
5198 |
#else |
|
16294 | 5199 |
if (__isSmallInteger(aNumber)) { |
20339 | 5200 |
INT sum; |
5201 |
||
5202 |
sum = __intVal(self) + __intVal(aNumber); |
|
5203 |
if (!__ISVALIDINTEGER(sum)) { |
|
5204 |
/* keep the sign */ |
|
5205 |
sum %= _MAX_INT; |
|
5206 |
} |
|
5207 |
RETURN ( __mkSmallInteger(sum)); |
|
16294 | 5208 |
} |
18227 | 5209 |
#endif |
16294 | 5210 |
%}. |
5211 |
self primitiveFailed |
|
5212 |
||
5213 |
" |
|
20339 | 5214 |
5 plus:-1 |
5215 |
5 plus:1 |
|
5216 |
1 plus:-5 |
|
5217 |
self maxVal plus:1 |
|
5218 |
self maxVal + 1 |
|
16294 | 5219 |
" |
5220 |
! |
|
5221 |
||
5222 |
subtract:aNumber |
|
5223 |
"return the difference of the receiver and the argument, as SmallInteger. |
|
5224 |
The argument must be another SmallInteger. |
|
5225 |
If the result overflows the smallInteger range, the value modulo the |
|
5226 |
smallInteger range is returned (i.e. the low bits of the sum). |
|
5227 |
This is of course not always correct, but some code does a modulo anyway |
|
5228 |
and can therefore speed things up by not going through LargeIntegers." |
|
5229 |
||
5230 |
%{ /* NOCONTEXT */ |
|
18255 | 5231 |
#ifdef __SCHTEAM__ |
5232 |
{ |
|
20339 | 5233 |
long myValue = self.longValue(); |
5234 |
long otherValue = aNumber.longValue(); |
|
5235 |
long diference = myValue - otherValue; |
|
5236 |
return context._RETURN( STInteger._new(diference) ); |
|
18255 | 5237 |
} |
5238 |
/* NOT REACHED */ |
|
5239 |
#else |
|
16294 | 5240 |
|
5241 |
if (__isSmallInteger(aNumber)) { |
|
20339 | 5242 |
INT diff; |
5243 |
||
5244 |
diff = __intVal(self) - __intVal(aNumber); |
|
5245 |
if (!__ISVALIDINTEGER(diff)) { |
|
5246 |
/* keep the sign */ |
|
5247 |
diff %= _MAX_INT; |
|
5248 |
} |
|
5249 |
RETURN ( __mkSmallInteger(diff)); |
|
16294 | 5250 |
} |
18227 | 5251 |
#endif |
16294 | 5252 |
%}. |
5253 |
self primitiveFailed |
|
5254 |
||
5255 |
" |
|
20339 | 5256 |
-1 subtract:5 |
5257 |
5 subtract:1 |
|
5258 |
1 subtract:-5 |
|
5259 |
self minVal subtract:1 |
|
5260 |
self minVal - 1 |
|
16294 | 5261 |
" |
5262 |
! |
|
5263 |
||
5264 |
times:aNumber |
|
5265 |
"return the product of the receiver and the argument, as SmallInteger. |
|
5266 |
The argument must be another SmallInteger. |
|
5267 |
If the result overflows the smallInteger range, the value modulo the |
|
5268 |
smallInteger range is returned (i.e. the low bits of the product). |
|
5269 |
This is of course not always correct, but some code does a modulo anyway |
|
5270 |
and can therefore speed things up by not going through LargeIntegers." |
|
5271 |
||
5272 |
%{ /* NOCONTEXT */ |
|
18255 | 5273 |
#ifdef __SCHTEAM__ |
5274 |
{ |
|
20339 | 5275 |
long myValue = self.longValue(); |
5276 |
long otherValue = aNumber.longValue(); |
|
5277 |
long product = myValue * otherValue; |
|
5278 |
return context._RETURN( STInteger._new(product) ); |
|
18255 | 5279 |
} |
5280 |
/* NOT REACHED */ |
|
5281 |
#else |
|
16294 | 5282 |
INT myValue, otherValue; |
5283 |
unsigned INT productLow, productHi; |
|
5284 |
int negative; |
|
5285 |
||
5286 |
# define low16Bits(foo) ((foo) & 0xFFFF) |
|
5287 |
# define hi16Bits(foo) ((foo) >> 16) |
|
5288 |
# define low32Bits(foo) ((foo) & 0xFFFFFFFFL) |
|
5289 |
# define hi32Bits(foo) ((foo) >> 32) |
|
5290 |
||
5291 |
/* |
|
5292 |
* can we use long long arithmetic ? |
|
5293 |
* |
|
5294 |
* long-long arithmetic seems to be buggy with some systems |
|
5295 |
* (took me a while to find this out :-( |
|
5296 |
* (try 10000 * 10000) |
|
5297 |
*/ |
|
18227 | 5298 |
# if defined(__sparc__) && defined(__GNUC__) && (__GNUC__ >= 2) |
5299 |
# define USE_LONGLONG_FOR_MUL |
|
5300 |
# endif |
|
5301 |
||
5302 |
# if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 2) |
|
5303 |
# define USE_LONGLONG_FOR_MUL |
|
5304 |
# endif |
|
16294 | 5305 |
|
5306 |
if (__isSmallInteger(aNumber)) { |
|
20339 | 5307 |
myValue = __intVal(self); |
5308 |
otherValue = __intVal(aNumber); |
|
16294 | 5309 |
|
18227 | 5310 |
# if defined(USE_LONGLONG_FOR_MUL) |
20339 | 5311 |
{ |
18227 | 5312 |
# if defined(__alpha__) && !defined(__alpha64__) |
5313 |
# define LONGLONG INT64 |
|
5314 |
# else |
|
5315 |
# define LONGLONG long long |
|
5316 |
# endif |
|
20339 | 5317 |
LONGLONG product; |
5318 |
||
5319 |
product = (LONGLONG)myValue * (LONGLONG)otherValue; |
|
5320 |
if (product < 0) { |
|
5321 |
RETURN ( __mkSmallInteger(-(INT)(-product & _MAX_INT))); |
|
5322 |
} |
|
5323 |
RETURN ( __mkSmallInteger((INT)(product & _MAX_INT))); |
|
5324 |
} |
|
18227 | 5325 |
# else /* no long-long */ |
20339 | 5326 |
negative = 1; |
5327 |
if (myValue < 0) { |
|
5328 |
negative = -1; |
|
5329 |
myValue = -myValue; |
|
5330 |
} |
|
5331 |
if (otherValue < 0) { |
|
5332 |
negative = -negative; |
|
5333 |
otherValue = -otherValue; |
|
5334 |
} |
|
16294 | 5335 |
|
18227 | 5336 |
# if defined(__GNUC__) && defined(__mc68k__) |
20339 | 5337 |
asm ("mulu%.l %3,%1:%0" |
5338 |
: "=d" ((unsigned long)(productLow)), |
|
5339 |
"=d" ((unsigned long)(productHi)) |
|
5340 |
: "%0" ((unsigned long)(myValue)), |
|
5341 |
"dmi" ((unsigned long)(otherValue))); |
|
18227 | 5342 |
# else |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
5343 |
# if defined (__GNUC__) && defined(__x86__) |
20339 | 5344 |
asm ("mull %3" |
5345 |
: "=a" ((unsigned long)(productLow)), |
|
5346 |
"=d" ((unsigned long)(productHi)) |
|
5347 |
: "%0" ((unsigned long)(myValue)), |
|
5348 |
"rm" ((unsigned long)(otherValue))); |
|
18227 | 5349 |
# else |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
5350 |
# if defined(__win32__) && defined(__BORLANDC__) |
20339 | 5351 |
asm { |
5352 |
mov eax, myValue |
|
5353 |
mov edx, otherValue |
|
5354 |
mul edx |
|
5355 |
mov productLow, eax |
|
5356 |
mov productHi, edx |
|
5357 |
} |
|
18227 | 5358 |
# else /* generic */ |
20339 | 5359 |
{ |
5360 |
unsigned INT pHH, pHL, pLH, pLL; |
|
5361 |
unsigned INT low1, low2, hi1, hi2; |
|
5362 |
unsigned INT t; |
|
5363 |
||
5364 |
/* unsigned multiply myValue * otherValue -> productHi, productLow |
|
5365 |
* |
|
5366 |
* this is too slow: |
|
5367 |
* since most machines can do 32*32 to 64 bit multiply, |
|
5368 |
* (or at least 32*32 with Overflow check) |
|
5369 |
* - need more assembler (inline) functions here |
|
5370 |
*/ |
|
18227 | 5371 |
# if __POINTER_SIZE__ == 8 |
20339 | 5372 |
low1 = low32Bits((unsigned INT)myValue); |
5373 |
hi1 = hi32Bits((unsigned INT)myValue); |
|
5374 |
low2 = low32Bits((unsigned INT)otherValue); |
|
5375 |
hi2 = hi32Bits((unsigned INT)otherValue); |
|
20333 | 5376 |
# undef LLMASK |
5377 |
# define LLMASK 0xC000000000000000LL |
|
18227 | 5378 |
# else |
20339 | 5379 |
low1 = low16Bits((unsigned INT)myValue); |
5380 |
hi1 = hi16Bits((unsigned INT)myValue); |
|
5381 |
low2 = low16Bits((unsigned INT)otherValue); |
|
5382 |
hi2 = hi16Bits((unsigned INT)otherValue); |
|
18227 | 5383 |
# define LLMASK 0xC0000000 |
5384 |
# endif |
|
16294 | 5385 |
|
20339 | 5386 |
pLH = low1 * hi2; |
5387 |
pHL = hi1 * low2; |
|
5388 |
pLL = low1 * low2; |
|
5389 |
pHH = hi1 * hi2; |
|
5390 |
||
5391 |
/* |
|
5392 |
* the common case ... |
|
5393 |
*/ |
|
5394 |
if ((pHL == 0) |
|
5395 |
&& (pLH == 0) |
|
5396 |
&& (pHH == 0) |
|
5397 |
&& ((pLL & LLMASK) == 0)) { |
|
5398 |
if (negative < 0) { |
|
5399 |
RETURN ( __mkSmallInteger(- ((INT)pLL)) ); |
|
5400 |
} |
|
5401 |
RETURN ( __mkSmallInteger((INT)pLL) ); |
|
5402 |
} |
|
5403 |
||
5404 |
/* |
|
5405 |
* pHH |--------|--------| |
|
5406 |
* pLH |--------|--------| |
|
5407 |
* pHL |--------|--------| |
|
5408 |
* pLL |--------|--------| |
|
5409 |
*/ |
|
16294 | 5410 |
|
18227 | 5411 |
# if __POINTER_SIZE__ == 8 |
20339 | 5412 |
t = low32Bits(pLH) + low32Bits(pHL) + hi32Bits(pLL); |
5413 |
productLow = (t << 32) + low32Bits(pLL); |
|
5414 |
productHi = pHH + hi32Bits(t) + hi32Bits(pHL) + hi32Bits(pLH); |
|
18227 | 5415 |
# else |
20339 | 5416 |
t = low16Bits(pLH) + low16Bits(pHL) + hi16Bits(pLL); |
5417 |
productLow = (t << 16) + low16Bits(pLL); |
|
5418 |
productHi = pHH + hi16Bits(t) + hi16Bits(pHL) + hi16Bits(pLH); |
|
18227 | 5419 |
# endif |
20339 | 5420 |
} |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
5421 |
# endif /* ! __win32__ */ |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
19601
diff
changeset
|
5422 |
# endif /* ! (__GNUC__ && __x86__) */ |
18227 | 5423 |
# endif /* ! (__GNUC__ && __mc68k__) */ |
16294 | 5424 |
|
20339 | 5425 |
if (negative < 0) { |
5426 |
RETURN ( __mkSmallInteger(-(INT)(productLow & _MAX_INT))); |
|
5427 |
} |
|
5428 |
RETURN ( __mkSmallInteger((INT)(productLow & _MAX_INT))); |
|
18227 | 5429 |
# endif /* ! USE_LONGLONG */ |
16294 | 5430 |
} |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5431 |
#endif /* not __SCHTEAM__ */ |
16294 | 5432 |
%}. |
5433 |
||
5434 |
self primitiveFailed |
|
5435 |
||
5436 |
" |
|
20339 | 5437 |
5 times:-1 |
5438 |
5 times:1 |
|
5439 |
self maxVal-1 times:2 |
|
5440 |
self maxVal-1 times:-2 |
|
5441 |
self maxVal-1 * 2 bitAnd:16r3fffffff |
|
16294 | 5442 |
" |
5443 |
! ! |
|
5444 |
||
5445 |
!SmallInteger methodsFor:'special modulo bit operators'! |
|
5446 |
||
5447 |
bitInvert32 |
|
5448 |
"return the value of the receiver with all bits inverted in 32bit signed int space |
|
5449 |
(changes the sign)" |
|
5450 |
||
5451 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5452 |
#ifndef __SCHTEAM__ |
16294 | 5453 |
unsigned INT v; |
5454 |
||
5455 |
v = __intVal(self); |
|
5456 |
v = ~v; |
|
18227 | 5457 |
# if __POINTER_SIZE__ == 8 |
16294 | 5458 |
v &= 0xFFFFFFFFL; |
18227 | 5459 |
# endif |
5460 |
RETURN ( __MKUINT(v) ); |
|
16294 | 5461 |
#endif |
5462 |
%}. |
|
5463 |
^ self primitiveFailed |
|
5464 |
||
5465 |
" |
|
5466 |
1 bitInvert32 |
|
5467 |
16r40000000 bitInvert32 |
|
5468 |
16r80000000 bitInvert32 |
|
5469 |
" |
|
5470 |
! |
|
5471 |
||
5472 |
bitRotate32:shiftCount |
|
5473 |
"return the value of the receiver rotated by shiftCount bits, |
|
5474 |
but only within 32 bits, rotating left for positive, right for negative counts. |
|
5475 |
Rotates through the sign bit. |
|
5476 |
Useful for crypt algorithms, or to emulate C/Java semantics." |
|
5477 |
||
5478 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5479 |
#ifndef __SCHTEAM__ |
16294 | 5480 |
|
5481 |
unsigned INT bits; |
|
5482 |
int count; |
|
5483 |
||
5484 |
if (__isSmallInteger(shiftCount)) { |
|
20339 | 5485 |
count = __intVal(shiftCount); |
5486 |
count = count % 32; |
|
5487 |
||
5488 |
bits = __intVal(self); |
|
5489 |
if (count > 0) { |
|
5490 |
bits = (bits << count) | (bits >> (32-count)); |
|
5491 |
} else { |
|
5492 |
bits = (bits >> (-count)) | (bits << (32-(-count))); |
|
5493 |
} |
|
18227 | 5494 |
# if __POINTER_SIZE__ == 8 |
20339 | 5495 |
bits &= 0xFFFFFFFFL; |
18227 | 5496 |
# endif |
20339 | 5497 |
RETURN (__MKUINT(bits)); |
16294 | 5498 |
} |
18227 | 5499 |
#endif |
16294 | 5500 |
%}. |
5501 |
^ self primitiveFailed |
|
5502 |
||
5503 |
" |
|
5504 |
128 rotate32:1 |
|
5505 |
||
5506 |
1 rotate32:1 |
|
5507 |
1 rotate32:2 |
|
5508 |
1 rotate32:31 |
|
5509 |
1 rotate32:32 |
|
5510 |
||
5511 |
1 rotate32:-1 |
|
5512 |
1 rotate32:-2 |
|
5513 |
1 rotate32:-3 |
|
5514 |
1 rotate32:-32 |
|
5515 |
" |
|
5516 |
! |
|
5517 |
||
5518 |
bitShift32:shiftCount |
|
5519 |
"return the value of the receiver shifted by shiftCount bits, |
|
5520 |
but only within 32 bits, shifting into/out-of the sign bit. |
|
5521 |
May be useful for communication interfaces, to create ST-numbers |
|
5522 |
from a signed 32bit int value given as individual bytes, |
|
5523 |
or to emulate C/Java semantics." |
|
5524 |
||
5525 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5526 |
#ifndef __SCHTEAM__ |
16294 | 5527 |
|
5528 |
INT bits, count; |
|
5529 |
||
5530 |
if (__isSmallInteger(shiftCount)) { |
|
20339 | 5531 |
count = __intVal(shiftCount); |
5532 |
if (count >= 32) { |
|
5533 |
RETURN (__mkSmallInteger(0)); |
|
5534 |
} |
|
5535 |
||
5536 |
bits = __intVal(self); |
|
5537 |
if (count > 0) { |
|
5538 |
bits = bits << count; |
|
5539 |
} else { |
|
5540 |
bits = bits >> (-count); |
|
5541 |
} |
|
18227 | 5542 |
# if __POINTER_SIZE__ == 8 |
20339 | 5543 |
bits &= 0xFFFFFFFFL; |
18227 | 5544 |
# endif |
20339 | 5545 |
RETURN (__MKINT(bits)); |
16294 | 5546 |
} |
18227 | 5547 |
#endif |
16294 | 5548 |
%}. |
5549 |
^ self primitiveFailed |
|
5550 |
||
5551 |
" |
|
5552 |
128 bitShift:24 |
|
5553 |
128 bitShift32:24 |
|
5554 |
||
5555 |
1 bitShift:31 |
|
5556 |
1 bitShift32:31 |
|
5557 |
" |
|
5558 |
! |
|
5559 |
||
5560 |
bitXor32:aNumber |
|
5561 |
"return the xor of the receiver and the argument. |
|
5562 |
The argument must be another SmallInteger or a 4-byte LargeInteger. |
|
5563 |
If the result overflows the 32 bit range, the value modulo 16rFFFFFFFF is returned. |
|
5564 |
This is of course not always correct, but allows for C/Java behavior to be emulated." |
|
5565 |
||
5566 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5567 |
#ifndef __SCHTEAM__ |
16294 | 5568 |
INT rslt; |
5569 |
||
5570 |
rslt = __unsignedLongIntVal(self) ^ __unsignedLongIntVal(aNumber); |
|
18227 | 5571 |
# if __POINTER_SIZE__ == 8 |
16294 | 5572 |
rslt &= 0xFFFFFFFFL; |
18227 | 5573 |
# endif |
5574 |
RETURN ( __MKUINT(rslt)); |
|
16294 | 5575 |
#endif |
5576 |
%}. |
|
5577 |
self primitiveFailed |
|
5578 |
||
5579 |
" |
|
5580 |
16r7FFFFFFF bitXor: 16r80000000 4294967295 |
|
5581 |
16r7FFFFFFF bitXor32: 16r80000000 |
|
5582 |
" |
|
5583 |
! |
|
5584 |
||
5585 |
unsignedBitShift32:shiftCount |
|
5586 |
"return the value of the receiver shifted by shiftCount bits, |
|
5587 |
but only within 32 unsigned bits. |
|
5588 |
May be useful for communication interfaces, to create ST-numbers |
|
5589 |
from an unsigned 32bit int value given as individual byte, |
|
5590 |
or to emulate C/Java semantics." |
|
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__ |
16294 | 5594 |
|
5595 |
unsigned INT bits; |
|
5596 |
INT count; |
|
5597 |
||
5598 |
if (__isSmallInteger(shiftCount)) { |
|
20339 | 5599 |
count = __intVal(shiftCount); |
5600 |
if (count >= 32) { |
|
5601 |
RETURN (__mkSmallInteger(0)); |
|
5602 |
} |
|
5603 |
||
5604 |
bits = __intVal(self); |
|
5605 |
if (count > 0) { |
|
5606 |
bits = bits << count; |
|
5607 |
} else { |
|
5608 |
bits = bits >> (-count); |
|
5609 |
} |
|
18227 | 5610 |
# if __POINTER_SIZE__ == 8 |
20339 | 5611 |
bits &= 0xFFFFFFFFL; |
18227 | 5612 |
# endif |
20339 | 5613 |
RETURN (__MKUINT(bits)); |
16294 | 5614 |
} |
18227 | 5615 |
#endif |
16294 | 5616 |
%}. |
5617 |
^ self primitiveFailed |
|
5618 |
||
5619 |
" |
|
5620 |
128 unsignedBitShift:24 |
|
5621 |
128 unsignedBitShift32:24 |
|
5622 |
||
5623 |
1 unsignedBitShift:31 |
|
5624 |
1 unsignedBitShift32:31 |
|
5625 |
||
5626 |
-1 unsignedBitShift32:-1 |
|
5627 |
-1 unsignedBitShift32:1 |
|
5628 |
" |
|
5629 |
! ! |
|
5630 |
||
701 | 5631 |
!SmallInteger methodsFor:'testing'! |
5632 |
||
5633 |
between:min and:max |
|
18395 | 5634 |
"return true if the receiver is greater than or equal to the argument min |
5635 |
and less than or equal to the argument max. |
|
701 | 5636 |
- reimplemented here for speed" |
5637 |
||
5638 |
%{ /* NOCONTEXT */ |
|
18395 | 5639 |
#ifdef __SCHTEAM__ |
5640 |
if (STObject.bothSmallInteger(min, max)) { |
|
20339 | 5641 |
long myVal = ((STInteger)self).longValue(); |
5642 |
||
5643 |
if (myVal >= ((STInteger)min).longValue()) { |
|
5644 |
if (myVal <= ((STInteger)max).longValue()) { |
|
5645 |
return __c__._RETURN_true(); |
|
5646 |
} |
|
5647 |
} |
|
5648 |
return __c__._RETURN_false(); |
|
18395 | 5649 |
} |
5650 |
#else /* not SCHTEAM */ |
|
701 | 5651 |
if (__bothSmallInteger(min, max)) { |
18227 | 5652 |
# if TAG_INT == 1 |
20339 | 5653 |
// tag bit does not change the magnitude order |
5654 |
if ((INT)self < (INT)(min)) { |
|
5655 |
RETURN ( false ); |
|
5656 |
} |
|
5657 |
if ((INT)self > (INT)(max)) { |
|
5658 |
RETURN ( false ); |
|
5659 |
} |
|
5660 |
RETURN ( true ); |
|
18227 | 5661 |
# else |
20339 | 5662 |
REGISTER INT selfVal; |
5663 |
||
5664 |
selfVal = __intVal(self); |
|
5665 |
if (selfVal < __intVal(min)) { |
|
5666 |
RETURN ( false ); |
|
5667 |
} |
|
5668 |
if (selfVal > __intVal(max)) { |
|
5669 |
RETURN ( false ); |
|
5670 |
} |
|
5671 |
RETURN ( true ); |
|
18227 | 5672 |
# endif |
5673 |
} |
|
18289 | 5674 |
#endif /* not SCHTEAM */ |
2216 | 5675 |
%}. |
701 | 5676 |
(self < min) ifTrue:[^ false]. |
18395 | 5677 |
(max < self) ifTrue:[^ false]. |
701 | 5678 |
^ true |
5679 |
! |
|
5680 |
||
5681 |
even |
|
5682 |
"return true, if the receiver is even" |
|
5683 |
||
5684 |
%{ /* NOCONTEXT */ |
|
18904 | 5685 |
#ifdef __SCHTEAM__ |
5686 |
return __c__._RETURN( self.isEven() ? STObject.True : STObject.False ); |
|
5687 |
/* NOTREACHED */ |
|
5688 |
#else |
|
2216 | 5689 |
RETURN ( ((INT)self & (INT)__MASKSMALLINT(1)) ? false : true ); |
18904 | 5690 |
#endif /* not SCHTEAM */ |
5412 | 5691 |
%}. |
5692 |
^ super even |
|
701 | 5693 |
! |
5694 |
||
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5695 |
isImmediate |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5696 |
"return true if I am an immediate object |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5697 |
i.e. I am represented in the pointer itself and |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5698 |
no real object header/storage is used by me." |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5699 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5700 |
^ true |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5701 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5702 |
"Created: / 18-09-2018 / 14:48:22 / Stefan Vogel" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5703 |
"Modified (comment): / 27-05-2019 / 15:38:29 / Claus Gittinger" |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5704 |
! |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5705 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5706 |
isPowerOf2 |
13386 | 5707 |
"return true, if the receiver is a power of 2" |
5708 |
||
19057 | 5709 |
"/ 0 is not a power of two, because ld(0) is -INF |
5710 |
^ ((self bitAnd:(self - 1)) == 0) and:[self ~~ 0] |
|
13386 | 5711 |
|
5712 |
" |
|
25426
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5713 |
0 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5714 |
1 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5715 |
2 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5716 |
3 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5717 |
4 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5718 |
5 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5719 |
16r8000000000000000 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5720 |
16r8000000000000001 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5721 |
16r40000000 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5722 |
16r80000000 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5723 |
16r100000000 isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5724 |
|
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5725 |
10000 factorial isPowerOf2 |
963f86568b2d
Cherry-picked `Number`, `Integer`, `SmallInteger` and `LargeInteger` from a6b6dda4caff
Jan Vrany <jan.vrany@labware.com>
parents:
23107
diff
changeset
|
5726 |
|n| n := 10000 factorial. Time millisecondsToRun:[10000 timesRepeat:[ n isPowerOf2]] |
13386 | 5727 |
" |
5728 |
! |
|
5729 |
||
701 | 5730 |
negative |
5731 |
"return true, if the receiver is less than zero |
|
5732 |
reimplemented here for speed" |
|
5733 |
||
5734 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5735 |
#ifdef __SCHTEAM__ |
18904 | 5736 |
return context._RETURN( self.isLt_0() ? STObject.True : STObject.False); |
18236 | 5737 |
/* NOTREACHED */ |
5738 |
#else |
|
18227 | 5739 |
# if TAG_INT == 1 |
701 | 5740 |
/* tag bit does not change sign */ |
5741 |
RETURN ( ((INT)(self) < 0) ? true : false ); |
|
18227 | 5742 |
# else |
2216 | 5743 |
RETURN ( (__intVal(self) < 0) ? true : false ); |
18227 | 5744 |
# endif |
701 | 5745 |
#endif |
5412 | 5746 |
%}. |
5747 |
^ self < 0 |
|
701 | 5748 |
! |
5749 |
||
14693 | 5750 |
nextPowerOf2 |
5751 |
"return the power of 2 at or above the receiver. |
|
18904 | 5752 |
Useful for padding. |
19060 | 5753 |
Notice, that for a powerOf2, the receiver is returned. |
19263 | 5754 |
Also notice, that (because it is used for padding), |
19060 | 5755 |
0 is returned for zero." |
14693 | 5756 |
|
5757 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5758 |
#ifndef __SCHTEAM__ |
14693 | 5759 |
INT x; |
5760 |
||
5761 |
x = __intVal(self) - 1; |
|
5762 |
x |= (x >> 1); |
|
5763 |
x |= (x >> 2); |
|
5764 |
x |= (x >> 4); |
|
5765 |
x |= (x >> 8); |
|
5766 |
x |= (x >> 16); |
|
18227 | 5767 |
# if __POINTER_SIZE__ == 8 |
14693 | 5768 |
x |= (x >> 32); |
18227 | 5769 |
# endif |
5770 |
RETURN (__MKINT(x + 1)); |
|
14693 | 5771 |
#endif |
18227 | 5772 |
%}. |
5773 |
^ super nextPowerOf2 |
|
5774 |
||
14693 | 5775 |
" |
18904 | 5776 |
0 nextPowerOf2 |
14693 | 5777 |
1 nextPowerOf2 |
5778 |
2 nextPowerOf2 |
|
5779 |
3 nextPowerOf2 |
|
5780 |
4 nextPowerOf2 |
|
5781 |
5 nextPowerOf2 |
|
5782 |
6 nextPowerOf2 |
|
5783 |
7 nextPowerOf2 |
|
5784 |
8 nextPowerOf2 |
|
19060 | 5785 |
9 nextPowerOf2 |
14693 | 5786 |
|
5787 |
22 nextPowerOf2 |
|
21107 | 5788 |
32 nextPowerOf2 |
5789 |
16rFFFF nextPowerOf2 = 16r10000 |
|
5790 |
16rFFFFFFFF nextPowerOf2 = 16r100000000 |
|
5791 |
16r1FFFFFFFFFFFFFFF nextPowerOf2 = 16r2000000000000000 |
|
5792 |
16r3FFFFFFFFFFFFFFF nextPowerOf2 = 16r4000000000000000 |
|
5793 |
16r7FFFFFFFFFFFFFFF nextPowerOf2 = 16r8000000000000000 |
|
5794 |
16rFFFFFFFFFFFFFFFF nextPowerOf2 = 16r10000000000000000 |
|
18227 | 5795 |
10 factorial nextPowerOf2 |
5796 |
20 factorial nextPowerOf2 |
|
5797 |
100 factorial nextPowerOf2 |
|
14693 | 5798 |
" |
5799 |
! |
|
5800 |
||
701 | 5801 |
odd |
5802 |
"return true, if the receiver is odd" |
|
5803 |
||
5804 |
%{ /* NOCONTEXT */ |
|
18904 | 5805 |
#ifdef __SCHTEAM__ |
5806 |
return __c__._RETURN( self.isEven() ? STObject.False : STObject.True ); |
|
5807 |
/* NOTREACHED */ |
|
5808 |
#else |
|
2216 | 5809 |
RETURN ( ((INT)self & (INT)__MASKSMALLINT(1)) ? true : false ); |
18227 | 5810 |
#endif |
5412 | 5811 |
%}. |
5812 |
^ super odd |
|
701 | 5813 |
! |
5814 |
||
13913 | 5815 |
parityOdd |
5816 |
"return true, if an odd number of bits are set in the receiver, false otherwise. |
|
5817 |
(i.e. true for odd parity) |
|
5818 |
Undefined for negative values (smalltalk does not require the machine to use 2's complement)" |
|
5819 |
||
5820 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5821 |
#ifndef __SCHTEAM__ |
13913 | 5822 |
|
5823 |
// tricky, but very fast (google for it, to understand) |
|
18897 | 5824 |
unsigned INT v = __intVal(self); |
5825 |
||
5826 |
# if __POINTER_SIZE__ == 8 |
|
5827 |
v ^= v >> 32; |
|
5828 |
# endif |
|
13913 | 5829 |
v ^= v >> 16; |
5830 |
v ^= v >> 8; |
|
5831 |
v ^= v >> 4; |
|
5832 |
v &= 0xf; |
|
5833 |
RETURN ( ( (0x6996 >> v) & 1 ) ? true : false ); |
|
5834 |
#endif |
|
5835 |
%}. |
|
5836 |
^ super parityOdd |
|
5837 |
||
5838 |
" |
|
20339 | 5839 |
self assert: |
5840 |
(((0 to:255) collect:[:i | i parityOdd ifTrue:1 ifFalse:0]) |
|
5841 |
asByteArray collect:[:c | c + $0 asciiValue]) asString |
|
5842 |
= |
|
5843 |
'0110100110010110100101100110100110010110011010010110100110010110100101100110100101101001100101100110100110010110100101100110100110010110011010010110100110010110011010011001011010010110011010010110100110010110100101100110100110010110011010010110100110010110' |
|
5844 |
||
5845 |
self assert:(16r0FFFFFFF parityOdd = 16r0FFFFFFF bitCount odd). |
|
5846 |
self assert:(16r1FFFFFFF parityOdd = 16r1FFFFFFF bitCount odd). |
|
5847 |
self assert:(16r3FFFFFFF parityOdd = 16r3FFFFFFF bitCount odd). |
|
5848 |
self assert:(16r7FFFFFFF parityOdd = 16r7FFFFFFF bitCount odd). |
|
5849 |
self assert:(16rFFFFFFFF parityOdd = 16rFFFFFFFF bitCount odd). |
|
5850 |
self assert:(16r3FFFFFFFFFFFFFFF parityOdd = 16r3FFFFFFFFFFFFFFF bitCount odd). |
|
5851 |
self assert:(16r7FFFFFFFFFFFFFFF parityOdd = 16r7FFFFFFFFFFFFFFF bitCount odd). |
|
5852 |
self assert:(16rFFFFFFFFFFFFFFFF parityOdd = 16rFFFFFFFFFFFFFFFF bitCount odd). |
|
13913 | 5853 |
" |
5854 |
||
5855 |
"Modified (comment): / 09-01-2012 / 19:55:37 / cg" |
|
5856 |
! |
|
5857 |
||
701 | 5858 |
positive |
18864 | 5859 |
"return true, if the receiver is greater or equal to zero (not negative) |
701 | 5860 |
reimplemented here for speed" |
5861 |
||
5862 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5863 |
#ifdef __SCHTEAM__ |
18904 | 5864 |
return context._RETURN( self.isGe_0() ? STObject.True : STObject.False); |
5865 |
/* NOTREACHED */ |
|
18236 | 5866 |
#else |
18227 | 5867 |
|
5868 |
# if TAG_INT == 1 |
|
701 | 5869 |
/* tag bit does not change sign */ |
5870 |
RETURN ( ((INT)(self) >= 0) ? true : false ); |
|
18227 | 5871 |
# else |
2216 | 5872 |
RETURN ( (__intVal(self) >= 0) ? true : false ); |
18227 | 5873 |
# endif |
701 | 5874 |
#endif |
5412 | 5875 |
%}. |
5876 |
^ super positive |
|
701 | 5877 |
! |
5878 |
||
5879 |
sign |
|
7471 | 5880 |
"return the sign of the receiver (-1, 0 or 1). |
701 | 5881 |
reimplemented here for speed" |
5882 |
||
5883 |
%{ /* NOCONTEXT */ |
|
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18236
diff
changeset
|
5884 |
#ifndef __SCHTEAM__ |
18760 | 5885 |
# if TAG_INT <= 1 /* tag in low bit */ |
5886 |
RETURN ( __mkSmallInteger( (((INT)self) > TAG_INT) - (((INT)self) < 0) ) ); |
|
5887 |
# else |
|
1133 | 5888 |
INT val = __intVal(self); |
701 | 5889 |
|
18760 | 5890 |
RETURN ( __mkSmallInteger( (val > 0) - (val < 0) ) ); |
5891 |
# endif |
|
18227 | 5892 |
#endif |
5412 | 5893 |
%}. |
5894 |
^ super sign |
|
18760 | 5895 |
|
5896 |
" |
|
5897 |
-5 sign |
|
5898 |
-1 sign |
|
5899 |
0 sign |
|
5900 |
1 sign |
|
5901 |
5 sign |
|
5902 |
" |
|
701 | 5903 |
! |
5904 |
||
5905 |
strictlyPositive |
|
5906 |
"return true, if the receiver is greater than zero |
|
5907 |
reimplemented here for speed" |
|
5908 |
||
5909 |
%{ /* NOCONTEXT */ |
|
18904 | 5910 |
#ifdef __SCHTEAM__ |
5911 |
return context._RETURN( self.isGt_0() ? STObject.True : STObject.False); |
|
5912 |
/* NOTREACHED */ |
|
5913 |
#else |
|
18227 | 5914 |
|
5915 |
# if TAG_INT == 1 |
|
701 | 5916 |
/* tag bit does not change sign */ |
17689 | 5917 |
RETURN ( ((INT)(self) > (INT)(__mkSmallInteger(0))) ? true : false ); |
18227 | 5918 |
# else |
2216 | 5919 |
RETURN ( (__intVal(self) > 0) ? true : false ); |
18227 | 5920 |
# endif |
701 | 5921 |
#endif |
5412 | 5922 |
%}. |
5923 |
^ super strictlyPositive |
|
17689 | 5924 |
|
5925 |
" |
|
18227 | 5926 |
0 strictlyPositive |
5927 |
1 strictlyPositive |
|
5928 |
-1 strictlyPositive |
|
17689 | 5929 |
" |
701 | 5930 |
! ! |
5931 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1716
diff
changeset
|
5932 |
!SmallInteger class methodsFor:'documentation'! |
701 | 5933 |
|
5934 |
version |
|
18592 | 5935 |
^ '$Header$' |
12482
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
5936 |
! |
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
5937 |
|
41f0acde1b79
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11923
diff
changeset
|
5938 |
version_CVS |
18592 | 5939 |
^ '$Header$' |
701 | 5940 |
! ! |
14755 | 5941 |
|
15259 | 5942 |
|
5943 |
SmallInteger initialize! |