author | Stefan Vogel <sv@exept.de> |
Wed, 15 Apr 2020 20:18:14 +0200 | |
changeset 25357 | cec6ca6e3af4 |
parent 25215 | 9bbaac312afe |
permissions | -rw-r--r-- |
25149 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1988 by Claus Gittinger |
19078 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
15 |
|
17488 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
Number subclass:#Integer |
19327 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'BCDConversionErrorSignal PrimeCache' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Magnitude-Numbers' |
|
1 | 23 |
! |
24 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
25 |
Object subclass:#ModuloNumber |
19327 | 26 |
instanceVariableNames:'modulus reciprocal shift' |
27 |
classVariableNames:'' |
|
28 |
poolDictionaries:'' |
|
29 |
privateIn:Integer |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
30 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
31 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
32 |
!Integer class methodsFor:'documentation'! |
88 | 33 |
|
34 |
copyright |
|
35 |
" |
|
36 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
19078 | 37 |
All Rights Reserved |
1 | 38 |
|
88 | 39 |
This software is furnished under a license and may be used |
40 |
only in accordance with the terms of that license and with the |
|
41 |
inclusion of the above copyright notice. This software may not |
|
42 |
be provided or otherwise made available to, or used by, any |
|
43 |
other person. No title to or ownership of the software is |
|
44 |
hereby transferred. |
|
45 |
" |
|
46 |
! |
|
1 | 47 |
|
88 | 48 |
documentation |
49 |
" |
|
50 |
abstract superclass for all integer numbers. |
|
51 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 52 |
|
1892 | 53 |
Mixed mode arithmetic: |
22071 | 54 |
int <op> int -> int |
55 |
int <op> fraction -> fraction |
|
56 |
int <op> float -> float |
|
57 |
int <op> fix -> fix; scale is fix's scale |
|
1892 | 58 |
|
1295 | 59 |
[author:] |
19078 | 60 |
Claus Gittinger |
1556 | 61 |
|
62 |
[see also:] |
|
19078 | 63 |
Number |
64 |
LargeInteger SmallInteger |
|
65 |
Float ShortFloat Fraction FixedPoint |
|
88 | 66 |
" |
67 |
! ! |
|
1 | 68 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
69 |
!Integer class methodsFor:'instance creation'! |
92 | 70 |
|
3044 | 71 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
72 |
"Squeak compatibility: |
|
73 |
Return an Integer given four value bytes. |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
74 |
The returned integer is either a Small- or a LargeInteger |
15241 | 75 |
(on 32bit systems - on 64bit systems, it will be always a SmallInteger)" |
3044 | 76 |
|
77 |
|t| |
|
78 |
||
79 |
t := b4. |
|
80 |
t := (t bitShift:8) + b3. |
|
81 |
t := (t bitShift:8) + b2. |
|
82 |
^ (t bitShift:8) + b1. |
|
83 |
||
84 |
" |
|
85 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
86 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
87 |
" |
|
88 |
||
89 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
90 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
91 |
! |
|
92 |
||
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
93 |
fastFromString:aString at:startIndex |
11587 | 94 |
"return the next unsigned Integer from the string |
4610 | 95 |
as a decimal number, starting at startIndex. |
19276 | 96 |
The number must be in the native machine's int range |
23944 | 97 |
(i.e. 64bit or 32bit depending on the cpu's native pointer size); |
98 |
However, for portability, only use it for 32bit numbers (uint32s). |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
99 |
No spaces are skipped. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
100 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
101 |
This is a specially tuned entry (using a low-level C-call), which |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
102 |
returns garbage if the argument string is not a small integer number. |
11945 | 103 |
It has been added to allow high speed string decomposition into numbers, |
104 |
especially for mass-data." |
|
105 |
||
106 |
%{ /* NOCONTEXT */ |
|
12480
01a98f46e297
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12422
diff
changeset
|
107 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
19078 | 108 |
char *cp = (char *)(__stringVal(aString)); |
109 |
int idx = __intVal(startIndex) - 1; |
|
110 |
unsigned INT val; |
|
111 |
||
112 |
if ((unsigned)idx < __stringSize(aString)) { |
|
23944 | 113 |
extern int atoi(); |
114 |
extern long atol(); |
|
19276 | 115 |
|
116 |
val = atol(cp + idx); |
|
19078 | 117 |
if (val <= _MAX_INT) { |
118 |
RETURN(__mkSmallInteger(val)); |
|
119 |
} |
|
120 |
RETURN (__MKUINT(val)); |
|
121 |
} |
|
11945 | 122 |
} |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
123 |
%}. |
11945 | 124 |
self primitiveFailed. |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
125 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
126 |
" |
11587 | 127 |
Integer fastFromString:'12345' at:1 |
128 |
Integer fastFromString:'12345' at:2 |
|
129 |
Integer fastFromString:'12345' at:3 |
|
130 |
Integer fastFromString:'12345' at:4 |
|
131 |
Integer fastFromString:'12345' at:5 |
|
132 |
Integer fastFromString:'1234512345' at:1 |
|
133 |
Integer fastFromString:'2147483647' at:1 |
|
23944 | 134 |
Integer fastFromString:'-12345' at:1 |
11587 | 135 |
|
136 |
Integer fastFromString:'4294967295' at:1 |
|
137 |
Integer fastFromString:'12345' at:6 |
|
138 |
Integer fastFromString:'12345' at:0 |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
139 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
140 |
Time millisecondsToRun:[ |
19078 | 141 |
100000 timesRepeat:[ |
142 |
Integer readFrom:'12345' |
|
143 |
] |
|
11587 | 144 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
145 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
146 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
147 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
148 |
Time millisecondsToRun:[ |
19078 | 149 |
100000 timesRepeat:[ |
150 |
Integer fastFromString:'12345' at:1 |
|
151 |
] |
|
11587 | 152 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
153 |
" |
23944 | 154 |
|
155 |
"Modified (comment): / 20-03-2019 / 13:44:23 / Claus Gittinger" |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
156 |
! |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
157 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
158 |
fromBCDBytes:aByteArray |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
159 |
"given a byteArray in BCD format, return an appropriate integer. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
160 |
The byteArray must contain the BCD encoded decimal string, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
161 |
starting with the most significant digits. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
162 |
This conversion is useful for some communication protocols, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
163 |
or control systems, which represent big numbers this way... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
164 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
165 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
166 |
|val| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
167 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
168 |
val := 0. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
169 |
aByteArray do:[:twoDigits | |
19078 | 170 |
|hi lo| |
171 |
||
172 |
hi := (twoDigits bitShift:-4) bitAnd:16r0F. |
|
173 |
lo := twoDigits bitAnd:16r0F. |
|
174 |
val := (val * 100) + (hi * 10) + lo |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
175 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
176 |
^ val |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
177 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
178 |
" |
11587 | 179 |
Integer fromBCDBytes:#[16r12 16r34 16r56] |
180 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78] |
|
181 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
182 |
Integer fromBCDBytes:#[16r98 16r76 16r54] |
|
183 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32] |
|
184 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
185 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
186 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
187 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
188 |
|
5171 | 189 |
fromSwappedBCDBytes:aByteArray |
190 |
"given a byteArray in BCD format, return an appropriate integer. |
|
191 |
The byteArray must contain the BCD encoded decimal string, |
|
192 |
starting with the LEAST significant digits. |
|
193 |
This conversion is useful for some communication protocols, |
|
194 |
or control systems (e.g. SMC), which represent big numbers this way... |
|
195 |
" |
|
196 |
||
197 |
|val| |
|
198 |
||
199 |
val := 0. |
|
200 |
aByteArray do:[:twoDigits | |
|
19078 | 201 |
|hi lo| |
202 |
||
203 |
lo := (twoDigits bitShift:-4) bitAnd:16r0F. |
|
204 |
hi := twoDigits bitAnd:16r0F. |
|
205 |
lo <= 9 ifTrue:[ |
|
206 |
val := (val * 100) + (hi * 10) + lo |
|
207 |
] ifFalse:[ |
|
208 |
"16rF is used to encode an odd number of digits" |
|
209 |
val := (val * 10) + hi. |
|
210 |
]. |
|
5171 | 211 |
]. |
212 |
^ val |
|
213 |
||
214 |
" |
|
11587 | 215 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56] |
216 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16rF6] |
|
217 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78] |
|
218 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
219 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54] |
|
220 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32] |
|
221 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
5171 | 222 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
223 |
" |
|
224 |
! |
|
225 |
||
92 | 226 |
new:numberOfBytes neg:negative |
227 |
"for ST-80 compatibility: |
|
228 |
Return an empty Integer (uninitialized value) with space for |
|
229 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 230 |
negative specifies if the result should be a negative number. |
231 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 232 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
233 |
^ LargeInteger basicNew |
19078 | 234 |
numberOfDigits:numberOfBytes |
235 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
|
701 | 236 |
! |
237 |
||
11587 | 238 |
readFrom:aStringOrStream |
20329 | 239 |
"return the next Integer from the (character-)stream aStream |
11587 | 240 |
as decimal number. |
241 |
||
242 |
NOTICE: |
|
701 | 243 |
This behaves different from the default readFrom:, in returning |
244 |
0 (instead of raising an error) in case no number can be read. |
|
245 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
246 |
- depending on the upcoming ANSI standard, this may change." |
|
247 |
||
248 |
^ self readFrom:aStringOrStream onError:0 |
|
249 |
||
250 |
" |
|
11587 | 251 |
Integer readFrom:(ReadStream on:'foobar') |
22460 | 252 |
Integer readFrom:(ReadStream on:'123foobar') |
11587 | 253 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
701 | 254 |
" |
22460 | 255 |
|
256 |
"Modified (comment): / 17-01-2018 / 18:27:26 / stefan" |
|
701 | 257 |
! |
258 |
||
21636 | 259 |
readFrom:aStringOrStream allowRadix:allowRadix onError:exceptionBlock |
701 | 260 |
"return the next Integer from the (character-)stream aStream, |
21637 | 261 |
possibly handling initial XXr for arbitrary radix numbers and initial sign. |
701 | 262 |
Also, all initial whitespace is skipped. |
263 |
If the string does not represent a valid integer number, |
|
264 |
return the value of exceptionBlock." |
|
265 |
||
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
266 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
267 |
|
7093
60bad14d38fc
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
6907
diff
changeset
|
268 |
Error handle:[:ex | |
19078 | 269 |
^ exceptionBlock value |
1700 | 270 |
] do:[ |
25145 | 271 |
|str nextChar negative radix| |
19078 | 272 |
|
273 |
str := aStringOrStream readStream. |
|
274 |
||
275 |
nextChar := str skipSeparators. |
|
276 |
(nextChar == $-) ifTrue:[ |
|
277 |
negative := true. |
|
278 |
str next. |
|
279 |
nextChar := str peekOrNil |
|
280 |
] ifFalse:[ |
|
281 |
negative := false |
|
282 |
]. |
|
283 |
(nextChar isNil or:[nextChar isDigit not]) ifTrue:[ |
|
284 |
" |
|
285 |
the string does not represent an integer |
|
286 |
" |
|
287 |
^ exceptionBlock value |
|
288 |
]. |
|
25145 | 289 |
(allowRadix and:[nextChar == $0]) ifTrue:[ |
290 |
nextChar := str nextPeekOrNil. |
|
291 |
((nextChar == $x) or:[ nextChar == $X]) ifTrue:[ |
|
292 |
radix := 16. |
|
293 |
nextChar := str nextPeekOrNil. |
|
294 |
] ifFalse:[ |
|
295 |
((nextChar == $b) or:[ nextChar == $B]) ifTrue:[ |
|
296 |
radix := 2. |
|
297 |
nextChar := str nextPeekOrNil. |
|
298 |
] ifFalse:[ |
|
299 |
((nextChar == $o) or:[ nextChar == $O]) ifTrue:[ |
|
300 |
radix := 8. |
|
301 |
nextChar := str nextPeekOrNil. |
|
302 |
] ifFalse:[ |
|
25146 | 303 |
(nextChar notNil and:[nextChar isDigit]) ifFalse:[ |
25145 | 304 |
^ 0 |
305 |
]. |
|
306 |
value := self readFrom:str radix:10 |
|
307 |
]. |
|
308 |
] |
|
19078 | 309 |
]. |
25145 | 310 |
] ifFalse:[ |
311 |
value := self readFrom:str radix:10. |
|
20332 | 312 |
nextChar := str peekOrNil. |
25145 | 313 |
(allowRadix and:[((nextChar == $r) or:[ nextChar == $R])]) ifTrue:[ |
314 |
"-xxr<number> is invalid; should be xxr-<val>" |
|
315 |
||
316 |
negative ifTrue:[ |
|
317 |
Logger info:'Integer [warning]: invalid (negative) radix; please use xxr-<val> instead of -xxr<number>'. |
|
318 |
"/ negative := false |
|
319 |
]. |
|
320 |
nextChar := str nextPeekOrNil. |
|
321 |
radix := value. |
|
322 |
]. |
|
323 |
]. |
|
324 |
radix notNil ifTrue:[ |
|
20332 | 325 |
nextChar == $- ifTrue:[ |
326 |
negative ifTrue:[ |
|
25145 | 327 |
Logger info:'Integer [warning]: invalid double sign; please use xxr-<val> instead of -xxr<number>'. |
20332 | 328 |
]. |
329 |
negative := true. |
|
330 |
str next. |
|
331 |
]. |
|
25145 | 332 |
value := self readFrom:str radix:radix |
19078 | 333 |
]. |
334 |
negative ifTrue:[ |
|
335 |
value := value negated |
|
336 |
]. |
|
701 | 337 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
338 |
^ value |
701 | 339 |
|
340 |
" |
|
25145 | 341 |
Integer readFrom:'12345' onError:['wrong'] |
342 |
Integer readFrom:'-12345' onError:['wrong'] |
|
343 |
Integer readFrom:'+12345' onError:['wrong'] |
|
344 |
Integer readFrom:'16rFFFF' onError:['wrong'] |
|
345 |
Integer readFrom:'12345.1234' onError:['wrong'] |
|
346 |
Integer readFrom:'foo' onError:['wrong'] |
|
347 |
Integer readFrom:'foo' |
|
348 |
Integer readFrom:'0x123' |
|
701 | 349 |
" |
350 |
||
21636 | 351 |
"Created: / 09-03-2017 / 16:26:28 / cg" |
25146 | 352 |
"Modified: / 17-12-2019 / 14:40:11 / Stefan Reise" |
21636 | 353 |
! |
354 |
||
355 |
readFrom:aStringOrStream onError:exceptionBlock |
|
356 |
"return the next Integer from the (character-)stream aStream, |
|
357 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
358 |
Also, all initial whitespace is skipped. |
|
359 |
If the string does not represent a valid integer number, |
|
360 |
return the value of exceptionBlock." |
|
361 |
||
362 |
^ self readFrom:aStringOrStream allowRadix:true onError:exceptionBlock |
|
363 |
||
364 |
" |
|
365 |
Integer readFrom:'12345' onError:['wrong'] |
|
366 |
Integer readFrom:'-12345' onError:['wrong'] |
|
367 |
Integer readFrom:'+12345' onError:['wrong'] |
|
368 |
Integer readFrom:'16rFFFF' onError:['wrong'] |
|
369 |
Integer readFrom:'12345.1234' onError:['wrong'] |
|
370 |
Integer readFrom:'foo' onError:['wrong'] |
|
371 |
Integer readFrom:'foo' |
|
24683 | 372 |
Integer readFrom:'0xFFFF' onError:['wrong'] |
21636 | 373 |
|
374 |
Integer readFrom:'16rFFFF' allowRadix:false onError:['wrong'] |
|
375 |
" |
|
376 |
||
377 |
"Created: / 16-11-1995 / 22:48:59 / cg" |
|
378 |
"Modified (comment): / 09-03-2017 / 16:27:14 / cg" |
|
701 | 379 |
! |
380 |
||
381 |
readFrom:aStringOrStream radix:radix |
|
25149 | 382 |
"return the next possibly signed Integer from the (character-)stream aStream in radix; |
383 |
(assumes that any initial XXr has already been read). |
|
701 | 384 |
No whitespace-skipping is done. |
385 |
Returns 0 if no number available. |
|
386 |
||
11587 | 387 |
NOTICE: |
701 | 388 |
This behaves different from the default readFrom:, in returning |
389 |
0 (instead of raising an error) in case no number can be read. |
|
390 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
391 |
- depending on the upcoming ANSI standard, this may change." |
|
392 |
||
393 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
394 |
! |
|
395 |
||
396 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
25149 | 397 |
"return the next possibly signed Integer from the (character-)stream aStream in radix; |
398 |
(assumes that any initial XXr has already been read). |
|
701 | 399 |
No whitespace-skipping is done. |
400 |
Returns the value of exceptionBlock, if no number is available." |
|
401 |
||
25149 | 402 |
|str nextChar| |
701 | 403 |
|
404 |
str := aStringOrStream readStream. |
|
405 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
406 |
nextChar := str peekOrNil. |
25149 | 407 |
nextChar == $- ifTrue:[ |
408 |
str next. |
|
409 |
^ self readUnsignedFrom:str radix:radix sign:-1 onError:exceptionBlock |
|
701 | 410 |
]. |
25149 | 411 |
^ self readUnsignedFrom:str radix:radix sign:1 onError:exceptionBlock |
701 | 412 |
|
11945 | 413 |
" |
11587 | 414 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
415 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
416 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
417 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
418 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
419 |
Integer readFrom:'gg' radix:10 onError:0 |
|
20130 | 420 |
Integer readFrom:'' radix:10 onError:'wrong' |
421 |
||
422 |
|s| |
|
423 |
s := String new:1000 withAll:$1. |
|
11654 | 424 |
Time millisecondsToRun:[ |
19078 | 425 |
1000 timesRepeat:[ |
20130 | 426 |
s asInteger |
19078 | 427 |
] |
11945 | 428 |
] |
701 | 429 |
" |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
430 |
|
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
431 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
6635 | 432 |
! |
433 |
||
6673 | 434 |
readFromRomanString:aStringOrStream |
435 |
"convert a string or stream containing a roman representation into an integer. |
|
11125 | 436 |
Raises a RomanNumberFormatError, if the inputs format is completely wrong. |
21429 | 437 |
Raises BadRomanNumberFormatError if it's wrong, but could be parsed. |
6682 | 438 |
Notifies via NaiveRomanNumberFormatNotification, if its a bit wrong (naive format). |
439 |
Will read both real and naive roman numbers (see printRomanOn: vs. printRomanOn:naive:), |
|
20378 | 440 |
however, a notification is raised for naive numbers (catch it if you are interested in it)." |
6635 | 441 |
|
11587 | 442 |
|romanValues s c val digitVal prevDigitVal countSame delta |
6635 | 443 |
stopOnSeparator finish| |
444 |
||
11587 | 445 |
romanValues := Dictionary |
19078 | 446 |
withKeys:'MDCLXVI' "/ #($M $D $C $L $X $V $I) |
447 |
andValues:#(1000 500 100 50 10 5 1). |
|
6635 | 448 |
|
449 |
(stopOnSeparator := aStringOrStream isStream) ifFalse:[ |
|
19078 | 450 |
s := aStringOrStream readStream. |
6635 | 451 |
]. |
11587 | 452 |
s atEnd ifTrue:[ |
19078 | 453 |
^ RomanNumberFormatError raiseErrorString:'empty string' |
6635 | 454 |
]. |
455 |
val := 0. |
|
456 |
prevDigitVal := 99999. |
|
457 |
countSame := 1. |
|
458 |
finish := false. |
|
459 |
||
460 |
[s atEnd or:[finish]] whileFalse:[ |
|
19078 | 461 |
c := s next asUppercase. |
462 |
c isSeparator ifTrue:[ |
|
463 |
stopOnSeparator ifFalse:[ |
|
464 |
^ RomanNumberFormatError raiseErrorString:'garbage at the end' |
|
465 |
]. |
|
466 |
finish := true. |
|
467 |
] ifFalse:[ |
|
468 |
digitVal := romanValues at:c ifAbsent:nil. |
|
469 |
digitVal isNil ifTrue:[ |
|
470 |
^ RomanNumberFormatError raiseErrorString:'invalid character' |
|
471 |
]. |
|
472 |
||
473 |
digitVal = prevDigitVal ifTrue:[ |
|
474 |
( #( 1 10 100 1000) includes:digitVal) ifFalse:[ |
|
475 |
^ RomanNumberFormatError raiseErrorString:'character may not be repeated' |
|
476 |
]. |
|
477 |
val := val + digitVal. |
|
478 |
countSame := countSame + 1. |
|
479 |
countSame >= 4 ifTrue:[ |
|
480 |
digitVal ~= 1000 ifTrue:[ |
|
481 |
countSame > 4 ifTrue:[ |
|
482 |
"/ this is a bad roman number (such as MCCCCCCCCXXXXXXII); |
|
483 |
"/ Its not correct, but sometimes encountered on buildings. |
|
484 |
"/ If you do not want to be too picky, |
|
485 |
"/ provide a proceeding handler in order to proceed the conversion. |
|
486 |
BadRomanNumberFormatError raiseRequestErrorString:'more than 4 occurrences of same character' |
|
487 |
] ifFalse:[ |
|
488 |
"/ this is a naive roman number (such as VIIII); |
|
489 |
"/ Its not correct, but very often encountered (especially as page numbers). |
|
490 |
"/ The notification below normally goes unnoticed, unless some input validator |
|
491 |
"/ wants to be very picky, and treat this as an error. |
|
492 |
"/ To do so, provide a handler for NaiveRomanNumberFormatNotification. |
|
493 |
NaiveRomanNumberFormatNotification raiseRequestErrorString:'more than 3 occurrences of same character'. |
|
494 |
] |
|
495 |
] |
|
496 |
]. |
|
497 |
] ifFalse:[ |
|
498 |
digitVal < prevDigitVal ifTrue:[ |
|
499 |
val := val + digitVal. |
|
500 |
] ifFalse:[ |
|
501 |
countSame == 1 ifFalse:[ |
|
502 |
^ RomanNumberFormatError raiseErrorString:'invalid character combination' |
|
503 |
]. |
|
504 |
delta := digitVal - prevDigitVal. |
|
505 |
( #( 4 9 40 90 400 900) includes:delta) ifFalse:[ |
|
506 |
^ RomanNumberFormatError raiseErrorString:'invalid character combination' |
|
507 |
]. |
|
508 |
val := val - prevDigitVal. |
|
509 |
val := val + delta. |
|
510 |
digitVal := prevDigitVal - 0.1. "/ trick: prevent prevDigit from arriving again. |
|
511 |
]. |
|
512 |
countSame := 1. |
|
513 |
]. |
|
514 |
prevDigitVal := digitVal. |
|
515 |
]. |
|
6635 | 516 |
]. |
517 |
"/ val > 5000 ifTrue:[ |
|
20622 | 518 |
"/ ^ RomanNumberFormatError raiseErrorString:'number out of range (1..5000)' |
11587 | 519 |
"/ ]. |
6635 | 520 |
^ val. |
521 |
||
522 |
" |
|
11587 | 523 |
Integer readFromRomanString:'I' |
524 |
Integer readFromRomanString:'II' |
|
525 |
Integer readFromRomanString:'III' |
|
526 |
Integer readFromRomanString:'IV' |
|
527 |
Integer readFromRomanString:'clix' |
|
528 |
Integer readFromRomanString:'MIX' |
|
529 |
Integer readFromRomanString:'MCMXCIX' |
|
6673 | 530 |
|
6682 | 531 |
Naive cases (which are accepted): |
11587 | 532 |
Integer readFromRomanString:'IIII' |
533 |
Integer readFromRomanString:'VIIII' |
|
534 |
Integer readFromRomanString:'CLXXXXVIIII' |
|
6682 | 535 |
|
536 |
Error case (not proceedable): |
|
11587 | 537 |
Integer readFromRomanString:'LC' |
6682 | 538 |
|
539 |
Error case (proceedable): |
|
11587 | 540 |
Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII' |
6682 | 541 |
|
542 |
BadRomanNumberFormatError ignoreIn:[ |
|
19078 | 543 |
Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII' |
11587 | 544 |
] |
6682 | 545 |
" |
546 |
||
547 |
||
548 |
"naive cases: |
|
549 |
#( |
|
19078 | 550 |
'MCMXCIX' 1999 |
551 |
'MCMXCVIIII' 1999 |
|
552 |
'MCMLXXXXIX' 1999 |
|
553 |
'MDCCCCXCIX' 1999 |
|
554 |
'MDCCCCXCVIIII' 1999 |
|
555 |
'MDCCCCLXXXXIX' 1999 |
|
556 |
'MDCCCCLXXXXVIIII' 1999 |
|
6682 | 557 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 558 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6682 | 559 |
] |
6635 | 560 |
" |
561 |
||
6673 | 562 |
|
6635 | 563 |
"error cases: |
11587 | 564 |
#( |
19078 | 565 |
'XIIX' |
566 |
'VV' |
|
567 |
'VVV' |
|
568 |
'XXL' |
|
569 |
'XLX' |
|
570 |
'LC' |
|
571 |
'LL' |
|
572 |
'DD' |
|
6635 | 573 |
) do:[:badString | |
19078 | 574 |
(Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt]. |
6635 | 575 |
] |
576 |
" |
|
577 |
||
578 |
"good cases: |
|
579 |
#( 'I' 1 |
|
19078 | 580 |
'II' 2 |
581 |
'III' 3 |
|
582 |
'IV' 4 |
|
583 |
'V' 5 |
|
584 |
'VI' 6 |
|
585 |
'VII' 7 |
|
586 |
'VIII' 8 |
|
587 |
'IX' 9 |
|
588 |
'X' 10 |
|
589 |
'XI' 11 |
|
590 |
'XII' 12 |
|
591 |
'XIII' 13 |
|
592 |
'XIV' 14 |
|
593 |
'XV' 15 |
|
594 |
'XVI' 16 |
|
595 |
'XVII' 17 |
|
596 |
'XVIII' 18 |
|
597 |
'XIX' 19 |
|
598 |
'XX' 20 |
|
599 |
'XXX' 30 |
|
600 |
'L' 50 |
|
601 |
'XL' 40 |
|
602 |
'LX' 60 |
|
603 |
'LXX' 70 |
|
604 |
'LXXX' 80 |
|
605 |
'CXL' 140 |
|
606 |
'CL' 150 |
|
607 |
'CLX' 160 |
|
608 |
'MMM' 3000 |
|
609 |
'MMMM' 4000 |
|
610 |
'MMMMCMXCIX' 4999 |
|
611 |
'MMMMMMMMMCMXCIX' 9999 |
|
6635 | 612 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 613 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6635 | 614 |
] |
615 |
" |
|
616 |
||
617 |
" |
|
618 |
1 to:9999 do:[:n | |
|
19078 | 619 |
|romanString| |
620 |
||
621 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
622 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 623 |
] |
624 |
" |
|
21429 | 625 |
|
626 |
"Modified (comment): / 13-02-2017 / 20:23:02 / cg" |
|
6673 | 627 |
! |
628 |
||
629 |
readFromRomanString:aStringOrStream onError:exceptionalValue |
|
630 |
"convert a string or stream containing a roman representation into an integer. |
|
11587 | 631 |
Raises an exception, if the inputs format is wrong. |
632 |
Does allow reading of naive (more than 3 in a row) and |
|
6682 | 633 |
bad (not using L and D) roman numbers. |
634 |
(Such numbers can be seen on some medevial buildings. " |
|
6673 | 635 |
|
21386 | 636 |
^ RomanNumberFormatError |
637 |
handle:[:ex | exceptionalValue value ] |
|
638 |
do:[ self readFromRomanString:aStringOrStream ]. |
|
6673 | 639 |
|
640 |
||
641 |
" |
|
11587 | 642 |
Integer readFromRomanString:'I' onError:nil |
643 |
Integer readFromRomanString:'II' onError:nil |
|
644 |
Integer readFromRomanString:'III' onError:nil |
|
645 |
Integer readFromRomanString:'IV' onError:nil |
|
646 |
Integer readFromRomanString:'clix' onError:nil |
|
647 |
Integer readFromRomanString:'MCMXCIX' onError:nil |
|
6673 | 648 |
|
6682 | 649 |
Naive cases (which are accepted): |
11587 | 650 |
Integer readFromRomanString:'IIII' onError:nil |
651 |
Integer readFromRomanString:'VIIII' onError:nil |
|
652 |
Integer readFromRomanString:'CLXXXXVIIII' onError:nil |
|
6682 | 653 |
|
654 |
Error cases: |
|
21386 | 655 |
Integer readFromRomanString:'LC' onError:nil |
6673 | 656 |
" |
657 |
||
658 |
"error cases: |
|
11587 | 659 |
#( |
19078 | 660 |
'XIIX' |
661 |
'VV' |
|
662 |
'VVV' |
|
663 |
'XXL' |
|
664 |
'XLX' |
|
665 |
'LC' |
|
666 |
'LL' |
|
667 |
'DD' |
|
6673 | 668 |
) do:[:badString | |
19078 | 669 |
(Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt]. |
6673 | 670 |
] |
671 |
" |
|
672 |
||
6682 | 673 |
"naive (but handled) cases: |
11587 | 674 |
#( |
19078 | 675 |
'IIII' 4 |
676 |
'VIIII' 9 |
|
677 |
'XIIII' 14 |
|
678 |
'XVIIII' 19 |
|
6682 | 679 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 680 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6682 | 681 |
] |
682 |
" |
|
683 |
||
6673 | 684 |
"good cases: |
685 |
#( 'I' 1 |
|
19078 | 686 |
'II' 2 |
687 |
'III' 3 |
|
688 |
'IV' 4 |
|
689 |
'V' 5 |
|
690 |
'VI' 6 |
|
691 |
'VII' 7 |
|
692 |
'VIII' 8 |
|
693 |
'IX' 9 |
|
694 |
'X' 10 |
|
695 |
'XI' 11 |
|
696 |
'XII' 12 |
|
697 |
'XIII' 13 |
|
698 |
'XIV' 14 |
|
699 |
'XV' 15 |
|
700 |
'XVI' 16 |
|
701 |
'XVII' 17 |
|
702 |
'XVIII' 18 |
|
703 |
'XIX' 19 |
|
704 |
'XX' 20 |
|
705 |
'XXX' 30 |
|
706 |
'L' 50 |
|
707 |
'XL' 40 |
|
708 |
'LX' 60 |
|
709 |
'LXX' 70 |
|
710 |
'LXXX' 80 |
|
711 |
'CXL' 140 |
|
712 |
'CL' 150 |
|
713 |
'CLX' 160 |
|
714 |
'MMM' 3000 |
|
715 |
'MMMM' 4000 |
|
716 |
'MMMMCMXCIX' 4999 |
|
717 |
'MMMMMMMMMCMXCIX' 9999 |
|
6673 | 718 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 719 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6673 | 720 |
] |
721 |
" |
|
722 |
||
723 |
" |
|
724 |
1 to:9999 do:[:n | |
|
19078 | 725 |
|romanString| |
726 |
||
727 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
728 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 729 |
] |
730 |
" |
|
731 |
||
732 |
"reading naive numbers: |
|
733 |
||
734 |
1 to:9999 do:[:n | |
|
19078 | 735 |
|romanString| |
736 |
||
737 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:true]. |
|
738 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6635 | 739 |
] |
740 |
" |
|
21386 | 741 |
|
742 |
"Modified (comment): / 08-02-2017 / 19:07:00 / stefan" |
|
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
743 |
! |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
744 |
|
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
745 |
readFromString:aString radix:base onError:exceptionBlock |
20329 | 746 |
"return the next UNSIGNED Integer from the (character-)aString in radix; |
747 |
(assumes that the initial XXr has already been read). |
|
748 |
No whitespace-skipping is done. |
|
749 |
Expects that NO garbage is at the end of the string. |
|
750 |
Returns the value from exceptionBlock, if no valid integer is in the string." |
|
751 |
||
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
752 |
|str val| |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
753 |
|
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
754 |
str := ReadStream on:aString. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
755 |
val := self readFrom:str radix:base onError:[^ exceptionBlock value]. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
756 |
str atEnd ifFalse:[ ^ exceptionBlock value]. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
757 |
^ val |
20329 | 758 |
|
759 |
" |
|
760 |
Integer readFromString:'1234' radix:10 onError:[nil] |
|
761 |
Integer readFromString:'-1234' radix:10 onError:[nil] - I only read unsigned numbers |
|
762 |
Integer readFromString:' 1234' radix:10 onError:[nil] - I do not skip whitespace |
|
763 |
Integer readFromString:'1234 ' radix:10 onError:[nil] - I do not accept anything after the number |
|
764 |
||
765 |
" |
|
25149 | 766 |
! |
767 |
||
768 |
readUnsignedFrom:aStringOrStream radix:radix |
|
769 |
"return the next UNSIGNED Integer from the (character-)stream aStream in radix; |
|
770 |
(assumes that any initial XXr or sign has already been read). |
|
771 |
No whitespace-skipping is done. |
|
772 |
Returns 0 if no number available. |
|
773 |
||
774 |
NOTICE: |
|
775 |
This behaves different from the default readFrom:, in returning |
|
776 |
0 (instead of raising an error) in case no number can be read. |
|
777 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
778 |
- depending on the upcoming ANSI standard, this may change." |
|
779 |
||
780 |
^ self readUnsignedFrom:aStringOrStream radix:radix sign:1 onError:0 |
|
781 |
! |
|
782 |
||
783 |
readUnsignedFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
784 |
"return the next UNSIGNED Integer from the (character-)stream aStream in radix; |
|
785 |
(assumes that any initial XXr or sign has already been read). |
|
786 |
No whitespace-skipping is done. |
|
787 |
Returns the value of exceptionBlock, if no number is available." |
|
788 |
||
789 |
^ self readUnsignedFrom:aStringOrStream radix:radix sign:1 onError:exceptionBlock |
|
790 |
||
791 |
" |
|
792 |
Integer readUnsignedFrom:(ReadStream on:'12345') radix:10 |
|
793 |
Integer readUnsignedFrom:(ReadStream on:'FFFF') radix:16 |
|
794 |
Integer readUnsignedFrom:(ReadStream on:'1010') radix:2 |
|
795 |
Integer readUnsignedFrom:(ReadStream on:'foobar') radix:10 |
|
796 |
Integer readUnsignedFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
797 |
Integer readUnsignedFrom:'gg' radix:10 onError:0 |
|
798 |
Integer readUnsignedFrom:'' radix:10 onError:'wrong' |
|
799 |
||
800 |
|s| |
|
801 |
s := String new:1000 withAll:$1. |
|
802 |
Time millisecondsToRun:[ |
|
803 |
1000 timesRepeat:[ |
|
804 |
s asInteger |
|
805 |
] |
|
806 |
] |
|
807 |
" |
|
808 |
||
809 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
|
810 |
! |
|
811 |
||
812 |
readUnsignedFrom:aStringOrStream radix:radix sign:sign |
|
813 |
"return the next UNSIGNED Integer from the (character-)stream aStream in radix; |
|
814 |
(assumes that any initial XXr or sign has already been read). |
|
815 |
No whitespace-skipping is done. |
|
816 |
Returns 0 if no number available. |
|
817 |
||
818 |
NOTICE: |
|
819 |
This behaves different from the default readFrom:, in returning |
|
820 |
0 (instead of raising an error) in case no number can be read. |
|
821 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
822 |
- depending on the upcoming ANSI standard, this may change." |
|
823 |
||
824 |
^ self readUnsignedFrom:aStringOrStream radix:radix sign:sign onError:0 |
|
825 |
! |
|
826 |
||
827 |
readUnsignedFrom:aStringOrStream radix:radix sign:sign onError:exceptionBlock |
|
828 |
"return the next UNSIGNED Integer from the (character-)stream aStream in radix; |
|
829 |
(assumes that any initial XXr or sign has already been read). |
|
830 |
No whitespace-skipping is done. |
|
831 |
Returns the value of exceptionBlock, if no number is available." |
|
832 |
||
833 |
|str nextChar value ret |
|
834 |
r "{ Class: SmallInteger }" |
|
835 |
r2 "{ Class: SmallInteger }" |
|
836 |
r3 "{ Class: SmallInteger }" |
|
837 |
r4 "{ Class: SmallInteger }" |
|
838 |
digit1 digit2 digit3 digit4 | |
|
839 |
||
840 |
str := aStringOrStream readStream. |
|
841 |
||
842 |
nextChar := str peekOrNil. |
|
843 |
(nextChar isNil or:[(value := nextChar digitValueRadix:radix) isNil]) ifTrue:[ |
|
844 |
^ exceptionBlock value |
|
845 |
]. |
|
846 |
||
847 |
"/ OLD code |
|
848 |
"/ [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
|
849 |
"/ str next. |
|
850 |
"/ value := value * radix + nextChar digitValue. |
|
851 |
"/ nextChar := str peekOrNil. |
|
852 |
"/ ]. |
|
853 |
"/ ^ value. |
|
854 |
||
855 |
"/ the code below does the same, but is much faster, if the |
|
856 |
"/ converted number is large |
|
857 |
"/ (requires only half as many LargeInt multiplications and additions) |
|
858 |
"/ It should not be slower for smallIntegers. |
|
859 |
||
860 |
r := radix. |
|
861 |
r2 := r * r. |
|
862 |
r4 := r2 * r2. |
|
863 |
||
864 |
[ |
|
865 |
nextChar := str nextPeekOrNil. |
|
866 |
nextChar notNil and:[(digit1 := nextChar digitValueRadix:r) notNil] |
|
867 |
] whileTrue:[ |
|
868 |
"/ read 4 chars and pre-compute their value to avoid largeInt operations. |
|
869 |
||
870 |
nextChar := str nextPeekOrNil. |
|
871 |
(nextChar isNil or:[(digit2 := nextChar digitValueRadix:r) isNil]) ifTrue:[ |
|
872 |
ret := (value * r) + digit1. |
|
873 |
sign == -1 ifTrue:[^ ret negated]. |
|
874 |
^ ret |
|
875 |
]. |
|
876 |
||
877 |
nextChar := str nextPeekOrNil. |
|
878 |
(nextChar isNil or:[(digit3 := nextChar digitValueRadix:r) isNil]) ifTrue:[ |
|
879 |
ret := (value * r2) + ((digit1*r) + digit2) . |
|
880 |
sign == -1 ifTrue:[^ ret negated]. |
|
881 |
^ ret |
|
882 |
]. |
|
883 |
||
884 |
nextChar := str nextPeekOrNil. |
|
885 |
(nextChar isNil or:[ (digit4 := nextChar digitValueRadix:r) isNil]) ifTrue:[ |
|
886 |
r3 := r2 * r. |
|
887 |
ret := (value * r3) + ((((digit1*r) + digit2)*r) + digit3). |
|
888 |
sign == -1 ifTrue:[^ ret negated]. |
|
889 |
^ ret |
|
890 |
]. |
|
891 |
||
892 |
value := (value * r4) + ((((((digit1*r) + digit2)*r) + digit3)*r) + digit4). |
|
893 |
]. |
|
894 |
sign == -1 ifTrue:[^ value negated]. |
|
895 |
^ value |
|
896 |
||
897 |
" |
|
898 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
899 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
900 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
901 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
902 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
903 |
Integer readFrom:'gg' radix:10 onError:0 |
|
904 |
Integer readFrom:'' radix:10 onError:'wrong' |
|
905 |
||
906 |
|s| |
|
907 |
s := String new:1000 withAll:$1. |
|
908 |
Time millisecondsToRun:[ |
|
909 |
1000 timesRepeat:[ |
|
910 |
s asInteger |
|
911 |
] |
|
912 |
] |
|
913 |
" |
|
914 |
||
915 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
|
701 | 916 |
! ! |
917 |
||
9141 | 918 |
!Integer class methodsFor:'Compatibility-Squeak'! |
919 |
||
920 |
readFrom:aStringOrStream base:aBase |
|
921 |
"for squeak compatibility" |
|
922 |
||
923 |
^ self readFrom:aStringOrStream radix:aBase |
|
924 |
! ! |
|
925 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
926 |
!Integer class methodsFor:'Signal constants'! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
927 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
928 |
bcdConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
929 |
"return the signal which is raised when bcd conversion fails |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
930 |
(i.e. when trying to decode an invalid BCD number)" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
931 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
932 |
^ BCDConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
933 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
934 |
"Modified: / 15.11.1999 / 20:35:20 / cg" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
935 |
! ! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
936 |
|
25215 | 937 |
|
938 |
||
7444 | 939 |
!Integer class methodsFor:'class initialization'! |
940 |
||
941 |
initialize |
|
942 |
BCDConversionErrorSignal isNil ifTrue:[ |
|
19078 | 943 |
BCDConversionErrorSignal := ConversionError newSignal. |
944 |
BCDConversionErrorSignal nameClass:self message:#bcdConversionErrorSignal. |
|
945 |
BCDConversionErrorSignal notifierString:'bcd conversion error'. |
|
7444 | 946 |
]. |
947 |
||
948 |
"Modified: / 15.11.1999 / 20:36:04 / cg" |
|
949 |
! ! |
|
950 |
||
21822 | 951 |
!Integer class methodsFor:'coercing & converting'! |
952 |
||
953 |
coerce:aNumber |
|
25010 | 954 |
"convert the argument aNumber into an instance of the receiver (class) and return it." |
21822 | 955 |
|
956 |
^ aNumber asInteger |
|
957 |
! ! |
|
958 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
959 |
!Integer class methodsFor:'constants'! |
701 | 960 |
|
7727 | 961 |
epsilon |
21903 | 962 |
"return the maximum relative spacing of instances of mySelf |
963 |
(i.e. the value-delta of the least significant bit)" |
|
7727 | 964 |
|
965 |
"don't know, what to really return here. |
|
8635 | 966 |
Returning 1 gives stupid values when doing some taylor series approximations |
967 |
(although it is correct)" |
|
7727 | 968 |
|
969 |
^ Float epsilon. |
|
8635 | 970 |
|
971 |
" |
|
11587 | 972 |
2 sqrt_withAccuracy:(Integer epsilon) |
973 |
2 sqrt_withAccuracy:1 |
|
8635 | 974 |
" |
21903 | 975 |
|
976 |
"Modified (comment): / 22-06-2017 / 13:44:17 / cg" |
|
7727 | 977 |
! |
978 |
||
701 | 979 |
unity |
1555 | 980 |
"return the neutral element for multiplication (1)" |
701 | 981 |
|
982 |
^ 1 |
|
1555 | 983 |
|
984 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 985 |
! |
986 |
||
987 |
zero |
|
1555 | 988 |
"return the neutral element for addition (0)" |
701 | 989 |
|
990 |
^ 0 |
|
1555 | 991 |
|
992 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 993 |
! ! |
994 |
||
25215 | 995 |
|
9125 | 996 |
!Integer class methodsFor:'prime numbers'! |
997 |
||
11520 | 998 |
flushPrimeCache |
18865 | 999 |
"cleanup after using a primeCache. |
1000 |
See comment in initializePrimeCacheUpTo:limit" |
|
11520 | 1001 |
|
1002 |
PrimeCache := nil. |
|
1003 |
||
1004 |
" |
|
1005 |
Integer initializePrimeCacheUpTo:1000000 |
|
1006 |
Integer flushPrimeCache. |
|
1007 |
" |
|
1008 |
! |
|
1009 |
||
1010 |
initializePrimeCacheUpTo:limit |
|
11582 | 1011 |
"if many operations are to be done using primes, we can keep them around... |
1012 |
You will need n/8/2 bytes to keep fast info about primes up to n |
|
1013 |
(i.e. 100Mb is good for primes up to 1.6*10^9)" |
|
11520 | 1014 |
|
1015 |
|bits| |
|
1016 |
||
11563
a91c8d348760
could have returned an uncompressed largeint after bitShift
Claus Gittinger <cg@exept.de>
parents:
11521
diff
changeset
|
1017 |
PrimeCache := nil. |
11582 | 1018 |
bits := BooleanArray new:limit//2. |
11520 | 1019 |
self primesUpTo:limit do:[:p | |
19078 | 1020 |
bits at:p//2 put:true |
11520 | 1021 |
]. |
1022 |
PrimeCache := bits. |
|
1023 |
||
1024 |
" |
|
11521 | 1025 |
Integer initializePrimeCacheUpTo:1000000. |
11654 | 1026 |
Integer initializePrimeCacheUpTo:10000000. |
1027 |
Integer initializePrimeCacheUpTo:100000000. |
|
11563
a91c8d348760
could have returned an uncompressed largeint after bitShift
Claus Gittinger <cg@exept.de>
parents:
11521
diff
changeset
|
1028 |
Integer initializePrimeCacheUpTo:1000000000. |
11521 | 1029 |
Integer flushPrimeCache. |
1030 |
" |
|
1031 |
||
1032 |
" |
|
1033 |
Integer flushPrimeCache. |
|
1034 |
Transcript showCR:( |
|
19078 | 1035 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11521 | 1036 |
). |
1037 |
Integer initializePrimeCacheUpTo:100000. |
|
1038 |
Transcript showCR:( |
|
19078 | 1039 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11521 | 1040 |
). |
11520 | 1041 |
Integer flushPrimeCache. |
1042 |
" |
|
1043 |
! |
|
1044 |
||
9125 | 1045 |
largePrimesUpTo: max do: aBlock |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1046 |
"Evaluate aBlock with all primes up and including maxValue. |
9125 | 1047 |
The Algorithm is adapted from http://www.rsok.com/~jrm/printprimes.html |
11587 | 1048 |
It encodes prime numbers much more compactly than #primesUpTo: |
9125 | 1049 |
38.5 integer per byte (2310 numbers per 60 byte) allow for some fun large primes. |
1050 |
(all primes up to SmallInteger maxVal can be computed within ~27MB of memory; |
|
1051 |
the regular #primesUpTo: would require 4 *GIGA*bytes). |
|
1052 |
Note: The algorithm could be re-written to produce the first primes (which require |
|
1053 |
the longest time to sieve) faster but only at the cost of clarity." |
|
1054 |
||
1055 |
| limit flags maskBitIndex bitIndex maskBit byteIndex index primesUpTo2310 indexLimit | |
|
1056 |
||
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1057 |
limit := max asInteger. |
9125 | 1058 |
indexLimit := max sqrt truncated + 1. |
1059 |
"Create the array of flags." |
|
1060 |
flags := ByteArray new: (limit + 2309) // 2310 * 60 + 60. |
|
1061 |
flags atAllPut: 16rFF. "set all to true" |
|
1062 |
||
1063 |
"Compute the primes up to 2310" |
|
1064 |
primesUpTo2310 := self primesUpTo: 2310. |
|
1065 |
||
1066 |
"Create a mapping from 2310 integers to 480 bits (60 byte)" |
|
1067 |
maskBitIndex := Array new: 2310. |
|
1068 |
bitIndex := -1. "for pre-increment" |
|
1069 |
maskBitIndex at: 1 put: (bitIndex := bitIndex + 1). |
|
1070 |
maskBitIndex at: 2 put: (bitIndex := bitIndex + 1). |
|
1071 |
||
1072 |
1 to: 5 do:[:i| aBlock value: (primesUpTo2310 at: i)]. |
|
1073 |
||
1074 |
index := 6. |
|
1075 |
2 to: 2309 do:[:n| |
|
19078 | 1076 |
[(primesUpTo2310 at: index) < n] |
1077 |
whileTrue:[index := index + 1]. |
|
1078 |
n = (primesUpTo2310 at: index) ifTrue:[ |
|
1079 |
maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1). |
|
1080 |
] ifFalse:[ |
|
1081 |
"if modulo any of the prime factors of 2310, then could not be prime" |
|
1082 |
(n \\ 2 = 0 or:[n \\ 3 = 0 or:[n \\ 5 = 0 or:[n \\ 7 = 0 or:[n \\ 11 = 0]]]]) |
|
1083 |
ifTrue:[maskBitIndex at: n+1 put: 0] |
|
1084 |
ifFalse:[maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1)]. |
|
1085 |
]. |
|
9125 | 1086 |
]. |
1087 |
||
1088 |
"Now the real work begins... |
|
1089 |
Start with 13 since multiples of 2,3,5,7,11 are handled by the storage method; |
|
1090 |
increment by 2 for odd numbers only." |
|
1091 |
13 to: limit by: 2 do:[:n| |
|
19078 | 1092 |
(maskBit := maskBitIndex at: (n \\ 2310 + 1)) = 0 ifFalse:["not a multiple of 2,3,5,7,11" |
1093 |
byteIndex := n // 2310 * 60 + (maskBit-1 bitShift: -3) + 1. |
|
1094 |
bitIndex := 1 bitShift: (maskBit bitAnd: 7). |
|
1095 |
((flags at: byteIndex) bitAnd: bitIndex) = 0 ifFalse:["not marked -- n is prime" |
|
1096 |
aBlock value: n. |
|
1097 |
"Start with n*n since any integer < n has already been sieved |
|
1098 |
(e.g., any multiple of n with a number k < n has been cleared |
|
1099 |
when k was sieved); add 2 * i to avoid even numbers and |
|
1100 |
mark all multiples of this prime. Note: n < indexLimit below |
|
1101 |
limits running into LargeInts -- nothing more." |
|
1102 |
n < indexLimit ifTrue:[ |
|
1103 |
index := n * n. |
|
1104 |
(index bitAnd: 1) = 0 ifTrue:[index := index + n]. |
|
1105 |
[index <= limit] whileTrue:[ |
|
1106 |
(maskBit := maskBitIndex at: (index \\ 2310 + 1)) = 0 ifFalse:[ |
|
1107 |
byteIndex := (index // 2310 * 60) + (maskBit-1 bitShift: -3) + 1. |
|
1108 |
maskBit := 255 - (1 bitShift: (maskBit bitAnd: 7)). |
|
1109 |
flags at: byteIndex put: ((flags at: byteIndex) bitAnd: maskBit). |
|
1110 |
]. |
|
1111 |
index := index + (2 * n)]. |
|
1112 |
]. |
|
1113 |
]. |
|
1114 |
]. |
|
9125 | 1115 |
]. |
1116 |
||
1117 |
" |
|
1118 |
Integer largePrimesUpTo:1000000 do:[:i | i > 900000 ifTrue:[self halt] ] |
|
1119 |
(Integer primesUpTo:1000000) inspect |
|
1120 |
" |
|
1121 |
! |
|
1122 |
||
11582 | 1123 |
primeCacheSize |
18865 | 1124 |
"see comment in initializePrimeCacheUpTo:limit" |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1125 |
|
11582 | 1126 |
^ PrimeCache size * 2 |
1127 |
||
1128 |
" |
|
1129 |
Integer initializePrimeCacheUpTo:1000. |
|
1130 |
Integer initializePrimeCacheUpTo:1000000. |
|
1131 |
Integer initializePrimeCacheUpTo:1000000000. |
|
1132 |
Integer flushPrimeCache. |
|
1133 |
" |
|
1134 |
||
1135 |
" |
|
1136 |
Integer flushPrimeCache. |
|
1137 |
Transcript showCR:( |
|
19078 | 1138 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11582 | 1139 |
). |
1140 |
Integer initializePrimeCacheUpTo:100000. |
|
1141 |
Transcript showCR:( |
|
19078 | 1142 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11582 | 1143 |
). |
1144 |
Integer flushPrimeCache. |
|
1145 |
" |
|
1146 |
! |
|
1147 |
||
19128 | 1148 |
primesUpTo5000 |
1149 |
"return a table of primes up to 5000. |
|
1150 |
Primes are heavily used to compute good container sizes in Set and Dictionary, |
|
1151 |
and in some cryprographic algorithms." |
|
11520 | 1152 |
|
1153 |
^ #( |
|
19078 | 1154 |
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 |
1155 |
73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 |
|
1156 |
173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 |
|
1157 |
277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 |
|
1158 |
397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 |
|
1159 |
509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 |
|
1160 |
641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 |
|
1161 |
761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 |
|
1162 |
887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 |
|
1163 |
||
1164 |
1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 |
|
1165 |
1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 |
|
1166 |
1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 |
|
1167 |
1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 |
|
1168 |
1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 |
|
1169 |
1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 |
|
1170 |
1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 |
|
1171 |
1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 |
|
1172 |
1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 |
|
19128 | 1173 |
|
1174 |
2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 |
|
1175 |
2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 |
|
1176 |
2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 |
|
1177 |
2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 |
|
1178 |
2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 |
|
1179 |
2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 |
|
1180 |
2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 |
|
1181 |
2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 |
|
1182 |
2939 2953 2957 2963 2969 2971 2999 |
|
1183 |
||
1184 |
3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 |
|
1185 |
3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 |
|
1186 |
3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 |
|
1187 |
3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 |
|
1188 |
3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 |
|
1189 |
3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 |
|
1190 |
3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 |
|
1191 |
3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 |
|
1192 |
||
1193 |
4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 |
|
1194 |
4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 |
|
1195 |
4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 |
|
1196 |
4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 |
|
1197 |
4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 |
|
1198 |
4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 |
|
1199 |
4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 |
|
1200 |
4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999 |
|
19078 | 1201 |
). |
11520 | 1202 |
! |
1203 |
||
9125 | 1204 |
primesUpTo: max |
15578 | 1205 |
"Return a list of prime integers up to and including the given integer." |
9125 | 1206 |
|
19231 | 1207 |
|cls| |
1208 |
||
20646 | 1209 |
"/ sigh: IntegerArray is in libbasic2... |
1210 |
(IntegerArray notNil |
|
1211 |
and:[ max < IntegerArray maxVal]) ifTrue:[ |
|
19231 | 1212 |
cls := IntegerArray. |
1213 |
] ifFalse:[ |
|
1214 |
cls := Array. |
|
1215 |
]. |
|
1216 |
||
1217 |
^ cls streamContents:[:s| self primesUpTo: max do:[:prime| s nextPut: prime]] |
|
9125 | 1218 |
|
1219 |
" |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1220 |
Integer primesUpTo: 100 |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1221 |
Integer primesUpTo: 13 |
11587 | 1222 |
(Integer primesUpTo: 100) select:[:p | p between:10 and:99] |
9125 | 1223 |
" |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1224 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1225 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1226 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1227 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1228 |
N := 1000. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1229 |
p := 1. |
11587 | 1230 |
a := (1 to:1000) |
19078 | 1231 |
collect:[:i | p := p nextPrime. p ] |
1232 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1233 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1234 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1235 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1236 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1237 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1238 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1239 |
N := 1000 nextPrime. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1240 |
p := 1. |
11587 | 1241 |
a := (1 to:1000) |
19078 | 1242 |
collect:[:i | p := p nextPrime. p ] |
1243 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1244 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1245 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1246 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1247 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1248 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1249 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1250 |
N := 1000 nextPrime-1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1251 |
p := 1. |
11587 | 1252 |
a := (1 to:1000) |
19078 | 1253 |
collect:[:i | p := p nextPrime. p ] |
1254 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1255 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1256 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1257 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1258 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1259 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1260 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1261 |
N := 100000. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1262 |
p := 1. |
11587 | 1263 |
a := (1 to:N) |
19078 | 1264 |
collect:[:i | p := p nextPrime. p ] |
1265 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1266 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1267 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1268 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1269 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1270 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1271 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1272 |
N := 100000 nextPrime. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1273 |
p := 1. |
11587 | 1274 |
a := (1 to:N) |
19078 | 1275 |
collect:[:i | p := p nextPrime. p ] |
1276 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1277 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1278 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1279 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1280 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1281 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1282 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1283 |
N := 100000 nextPrime-1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1284 |
p := 1. |
11587 | 1285 |
a := (1 to:N) |
19078 | 1286 |
collect:[:i | p := p nextPrime. p ] |
1287 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1288 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1289 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1290 |
" |
9125 | 1291 |
! |
1292 |
||
1293 |
primesUpTo: max do: aBlock |
|
18865 | 1294 |
"Compute aBlock with all prime integers up to and including the given integer. |
1295 |
See comment in initializePrimeCacheUpTo:limit" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1296 |
|
19128 | 1297 |
| limit |
19131 | 1298 |
iLimit "{ Class: SmallInteger }" |
19128 | 1299 |
flags |
19131 | 1300 |
prime "{ Class: SmallInteger }" |
1301 |
k "{ Class: SmallInteger }" |
|
19128 | 1302 |
wellKnownPrimes| |
1303 |
||
1304 |
wellKnownPrimes := self primesUpTo5000. |
|
1305 |
max <= (wellKnownPrimes last+1) ifTrue:[ |
|
1306 |
wellKnownPrimes do:[:p | |
|
19078 | 1307 |
p > max ifTrue:[^ self]. |
1308 |
aBlock value:p. |
|
1309 |
]. |
|
1310 |
^ self. |
|
11521 | 1311 |
]. |
19128 | 1312 |
|
11582 | 1313 |
max <= self primeCacheSize ifTrue:[ |
19078 | 1314 |
aBlock value:2. |
1315 |
3 to:max by:2 do:[:p | |
|
1316 |
(PrimeCache at:p//2) ifTrue:[ |
|
1317 |
aBlock value:p |
|
1318 |
]. |
|
1319 |
]. |
|
1320 |
^ self. |
|
11520 | 1321 |
]. |
1322 |
||
9125 | 1323 |
limit := max asInteger - 1. |
11587 | 1324 |
"Fall back into #largePrimesUpTo:do: if we'd require more than 100k of memory; |
9125 | 1325 |
the alternative will only requre 1/154th of the amount we need here and is almost as fast." |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1326 |
limit > 25000 ifTrue:[^ self largePrimesUpTo: max do: aBlock]. |
11492 | 1327 |
|
19128 | 1328 |
iLimit := limit. |
1329 |
||
11492 | 1330 |
"/ sieve, on the fly |
19128 | 1331 |
flags := ByteArray new: iLimit. |
1332 |
aBlock value: 2. |
|
1333 |
||
1334 |
2 to: iLimit by:2 do: [:i | |
|
1335 |
(flags at: i) == 0 ifTrue: [ |
|
19078 | 1336 |
prime := i + 1. |
1337 |
k := i + prime. |
|
19128 | 1338 |
[k <= iLimit] whileTrue: [ |
1339 |
flags at: k put: 1. |
|
19078 | 1340 |
k := k + prime |
1341 |
]. |
|
1342 |
aBlock value: prime |
|
1343 |
] |
|
9125 | 1344 |
]. |
1345 |
||
1346 |
" |
|
1347 |
Integer primesUpTo: 100 |
|
19128 | 1348 |
Integer primesUpTo:20000 do:[:p | ] |
9125 | 1349 |
" |
1350 |
! ! |
|
1351 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1352 |
!Integer class methodsFor:'queries'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1353 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1354 |
hasSharedInstances |
25031 | 1355 |
"return true if this class can share instances when stored binary, |
1356 |
that is, instances with the same value can be stored by reference. |
|
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1357 |
Although not always shared (LargeIntegers), these should be treated |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1358 |
so, to be independent of the number of bits in a SmallInt" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1359 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1360 |
^ true |
8892 | 1361 |
! |
1362 |
||
1363 |
isAbstract |
|
11223 | 1364 |
"Return if this class is an abstract class. |
1365 |
True is returned for Integer here; false for subclasses. |
|
19455 | 1366 |
Abstract subclasses must redefine this again." |
11223 | 1367 |
|
8892 | 1368 |
^ self == Integer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1369 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1370 |
|
25215 | 1371 |
|
7261 | 1372 |
!Integer methodsFor:'Compatibility-Dolphin'! |
6249 | 1373 |
|
1374 |
highWord |
|
1375 |
"return the high 16 bits of a 32 bit value" |
|
1376 |
||
1377 |
^ self bitShift:-16 |
|
1378 |
||
1379 |
" |
|
11587 | 1380 |
(16r12345678 highWord) hexPrintString |
1381 |
(16r12345678 lowWord) hexPrintString |
|
6249 | 1382 |
" |
1383 |
! |
|
1384 |
||
1385 |
lowWord |
|
1386 |
"return the low 16 bits of a 32 bit value" |
|
1387 |
||
1388 |
^ self bitAnd:16rFFFF |
|
1389 |
||
1390 |
" |
|
11587 | 1391 |
(16r12345678 lowWord) hexPrintString |
1392 |
(16r12345678 highWord) hexPrintString |
|
6249 | 1393 |
" |
1394 |
! |
|
1395 |
||
1396 |
mask:integerMask set:aBoolean |
|
1397 |
"Answer the result of setting/resetting the specified mask in the receiver." |
|
1398 |
||
11587 | 1399 |
^ aBoolean |
19078 | 1400 |
ifTrue: [self bitOr:integerMask] |
1401 |
ifFalse: [self bitClear:integerMask] |
|
6249 | 1402 |
|
1403 |
"turn on the 1-bit: |
|
19078 | 1404 |
|v| |
1405 |
||
1406 |
v := 2r0100. |
|
1407 |
v mask:1 set:true |
|
6249 | 1408 |
|
1409 |
turn off the 1-bit: |
|
19078 | 1410 |
|v| |
1411 |
||
1412 |
v := 2r0101. |
|
1413 |
v mask:1 set:false |
|
6249 | 1414 |
" |
6875 | 1415 |
! |
1416 |
||
17248 | 1417 |
maskClear:aMaskInteger |
1418 |
"return an integer with all bits cleared which are set in aMaskInteger. |
|
1419 |
An alias for bitClear: for compatibility." |
|
1420 |
||
1421 |
^ self bitClear:aMaskInteger |
|
1422 |
||
1423 |
" |
|
1424 |
3 maskClear:1 |
|
1425 |
" |
|
1426 |
! |
|
1427 |
||
1428 |
maskSet:aMaskInteger |
|
1429 |
"return an integer with all bits set which are set in aMaskInteger. |
|
1430 |
An alias for bitSet: for compatibility." |
|
1431 |
||
1432 |
^ self bitOr:aMaskInteger |
|
1433 |
! |
|
1434 |
||
6875 | 1435 |
printStringRadix:aRadix padTo:sz |
11936 | 1436 |
"return a printed representation of the receiver in a given radix, |
15765 | 1437 |
padded with zeros (at the left) up to size. |
11936 | 1438 |
If the printString is longer than size, |
1439 |
it is returned unchanged (i.e. not truncated). |
|
1440 |
See also printStringRadix:size:fill:" |
|
1441 |
||
6875 | 1442 |
^ self printStringRadix:aRadix size:sz fill:$0 |
1443 |
||
1444 |
" |
|
11945 | 1445 |
1024 printStringRadix:16 padTo:4 |
1446 |
16rABCD printStringRadix:16 padTo:3 |
|
1447 |
1024 printStringRadix:2 padTo:16 |
|
1448 |
1024 printStringRadix:16 padTo:8 |
|
6875 | 1449 |
" |
6907 | 1450 |
! |
1451 |
||
1452 |
| aNumber |
|
11936 | 1453 |
"return the bitwise-or of the receiver and the argument, anInteger. |
14448 | 1454 |
Same as bitOr: - added for compatibility with Dolphin Smalltalk. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1455 |
Notice: |
19093 | 1456 |
PLEASE DO NOT USE | for integers in new code; it makes the code harder |
1457 |
to understand, as it may be not obvious, whether a boolean-or a bitWise-or is intended. |
|
1458 |
For integers, use bitOr: to make the intention explicit. |
|
19094 | 1459 |
Also, consider using or: for booleans, which is does not evaluate the right part if the left is true." |
11936 | 1460 |
|
6907 | 1461 |
^ self bitOr:aNumber |
1462 |
||
1463 |
" |
|
11587 | 1464 |
14 | 1 |
1465 |
9 & 8 |
|
6907 | 1466 |
" |
6249 | 1467 |
! ! |
1468 |
||
7261 | 1469 |
!Integer methodsFor:'Compatibility-Squeak'! |
4867 | 1470 |
|
24409 | 1471 |
anyBitOfMagnitudeFrom:startBitIndex to:stopBitIndexArg |
1472 |
"Tests for any magnitude bits in the interval from start to stopArg." |
|
1473 |
||
1474 |
| magnitude firstDigitIx lastDigitIx rightShift leftShift stopBitIndex | |
|
1475 |
||
1476 |
"/ <primitive: 'primAnyBitFromTo' module:'LargeIntegers'> |
|
1477 |
||
1478 |
startBitIndex < 1 | (stopBitIndexArg < 1) ifTrue: [^ self error: 'out of range']. |
|
1479 |
magnitude := self abs. |
|
1480 |
stopBitIndex := stopBitIndexArg min: magnitude highBit. |
|
1481 |
startBitIndex > stopBitIndex ifTrue: [^ false]. |
|
1482 |
||
1483 |
firstDigitIx := (startBitIndex - 1) // 8 + 1. |
|
1484 |
lastDigitIx := (stopBitIndex - 1) // 8 + 1. |
|
1485 |
rightShift := ((startBitIndex - 1) \\ 8) negated. |
|
1486 |
leftShift := 7 - ((stopBitIndex - 1) \\ 8). |
|
1487 |
firstDigitIx = lastDigitIx ifTrue: [ |
|
1488 |
| digit mask | |
|
1489 |
mask := (255 bitShift: rightShift negated) |
|
24607 | 1490 |
bitAnd: (255 bitShift: leftShift negated). |
24409 | 1491 |
digit := magnitude digitAt: firstDigitIx. |
1492 |
^ (digit bitAnd: mask) ~= 0 |
|
1493 |
]. |
|
1494 |
((magnitude digitAt: firstDigitIx) bitShift: rightShift) ~= 0 ifTrue: [^ true]. |
|
1495 |
firstDigitIx + 1 to: lastDigitIx - 1 do: [:ix | |
|
1496 |
(magnitude digitAt: ix) ~= 0 ifTrue: [^ true] |
|
1497 |
]. |
|
1498 |
(((magnitude digitAt: lastDigitIx) bitShift: leftShift) bitAnd: 255) ~= 0 ifTrue: [^ true]. |
|
1499 |
^ false |
|
1500 |
||
1501 |
"Created: / 27-05-2019 / 08:37:08 / Claus Gittinger" |
|
1502 |
! |
|
1503 |
||
12122 | 1504 |
asByteArray |
15937 | 1505 |
"return my hexBytes in MSB. |
1506 |
Do not use: |
|
19078 | 1507 |
This is a very stupid squeak-compatibility method, |
21993 | 1508 |
as normally, you'd expect the bytes to be ordered in the machine's native order" |
12147 | 1509 |
|
15567 | 1510 |
^ self digitBytesMSB |
21993 | 1511 |
|
1512 |
"Modified (comment): / 11-07-2017 / 16:13:03 / mawalch" |
|
12122 | 1513 |
! |
1514 |
||
12147 | 1515 |
asByteArrayOfSize:size |
1516 |
"return my hexBytes in MSB, optionally padded at the left with zeros" |
|
1517 |
||
12122 | 1518 |
"((( |
19078 | 1519 |
| repeats number | |
1520 |
repeats := 1000000. |
|
1521 |
number := 123456789123456789123456789123456789123456789123456789. |
|
1522 |
[repeats timesRepeat: (number asByteArrayOfSize: 1024) ] timeToRun. |
|
12122 | 1523 |
)))" |
1524 |
||
15567 | 1525 |
| bytes bytesSize| |
1526 |
||
1527 |
bytes := self digitBytesMSB. |
|
1528 |
bytesSize := bytes size. |
|
1529 |
size < bytesSize ifTrue: [ |
|
19078 | 1530 |
^ ConversionError raiseRequestWith:self errorString:'number too big for ', size asString |
14796 | 1531 |
]. |
15567 | 1532 |
^ (ByteArray new:size) |
19078 | 1533 |
replaceFrom:size-bytesSize+1 to:size with:bytes startingAt:1. |
12147 | 1534 |
|
1535 |
" |
|
1536 |
123 asByteArrayOfSize:1 #[123] |
|
1537 |
123 asByteArrayOfSize:2 #[0 123] |
|
1538 |
123 asByteArrayOfSize:4 #[0 0 0 123] |
|
1539 |
||
1540 |
255 asByteArrayOfSize:1 #[255] |
|
1541 |
||
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1542 |
256 asByteArrayOfSize:1 |
12147 | 1543 |
256 asByteArrayOfSize:2 |
1544 |
256 asByteArrayOfSize:4 |
|
22567 | 1545 |
256 asByteArrayOfSize:8 |
12147 | 1546 |
" |
12122 | 1547 |
! |
1548 |
||
8573 | 1549 |
atRandom:aRandomGenerator |
15456 | 1550 |
"return a random number between 1 and myself" |
8573 | 1551 |
|
1552 |
self < 1 ifTrue:[^ self]. |
|
1553 |
^ aRandomGenerator nextIntegerBetween:1 and:self |
|
1554 |
||
1555 |
" |
|
1556 |
100 atRandom:(Random new) |
|
1557 |
1000 atRandom:(Random new) |
|
1558 |
" |
|
1559 |
! |
|
1560 |
||
22600 | 1561 |
bitShiftMagnitude:shift |
1562 |
^ self bitShift:shift. |
|
1563 |
||
1564 |
" |
|
1565 |
-1 bitShiftMagnitude:1 |
|
1566 |
-2 bitShift:-1 |
|
1567 |
-2 bitShift:-1 |
|
1568 |
" |
|
1569 |
! |
|
1570 |
||
14385
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1571 |
printLeftPaddedWith:padChar to:size base:base |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1572 |
"prints left-padded" |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1573 |
|
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1574 |
^ (self printStringRadix:base) leftPaddedTo:size with:padChar |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1575 |
|
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1576 |
" |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1577 |
1234 printPaddedWith:$0 to:4 base:16 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1578 |
1234 printLeftPaddedWith:$0 to:4 base:16 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1579 |
128 printLeftPaddedWith:$0 to:2 base:16 |
14385
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1580 |
" |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1581 |
! |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1582 |
|
11858 | 1583 |
printPaddedWith:padChar to:size base:base |
14385
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1584 |
"attention: prints right-padded; see printLeftPadded." |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1585 |
|
11858 | 1586 |
^ (self printStringRadix:base) paddedTo:size with:padChar |
1587 |
||
1588 |
" |
|
1589 |
1234 printPaddedWith:$0 to:4 base:16 |
|
1590 |
" |
|
1591 |
! |
|
1592 |
||
4376 | 1593 |
printStringBase:base |
4867 | 1594 |
"return my printString in a base; |
1595 |
same as printStringRadix:" |
|
1596 |
||
4376 | 1597 |
^ self printStringRadix:base |
12284
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1598 |
|
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1599 |
" |
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1600 |
1234 printStringBase:16 |
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1601 |
" |
9068 | 1602 |
! |
1603 |
||
9125 | 1604 |
printStringHex |
1605 |
"return my printString in base 16; |
|
1606 |
same as printStringRadix:" |
|
1607 |
||
1608 |
^ self printStringRadix:16 |
|
1609 |
||
1610 |
" |
|
1611 |
4096 printStringHex |
|
1612 |
" |
|
1613 |
! |
|
1614 |
||
9068 | 1615 |
printStringRoman |
9069 | 1616 |
"return my roman printString; |
1617 |
almost the same as romanPrintString:" |
|
1618 |
||
9068 | 1619 |
"funny - although the romans did not have negative numbers - squeak has" |
1620 |
self negative ifTrue:[ |
|
19078 | 1621 |
^ '-' , self negated romanPrintString |
9068 | 1622 |
]. |
1623 |
^ self romanPrintString |
|
12280 | 1624 |
! |
1625 |
||
1626 |
raisedTo:exp modulo:mod |
|
1627 |
^ self raisedTo:exp mod:mod |
|
4376 | 1628 |
! ! |
1629 |
||
7261 | 1630 |
!Integer methodsFor:'Compatibility-V''Age'! |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1631 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1632 |
<< aNumber |
7155 | 1633 |
"V'Age compatibility: left shift" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1634 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1635 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1636 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1637 |
" |
11587 | 1638 |
1 << 5 |
1639 |
64 << -5 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1640 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1641 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1642 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1643 |
>> aNumber |
7155 | 1644 |
"V'Age compatibility: right shift" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1645 |
|
22213 | 1646 |
^ self rightShift:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1647 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1648 |
" |
11587 | 1649 |
1 >> -5 |
1650 |
64 >> 5 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1651 |
" |
22213 | 1652 |
|
1653 |
"Modified: / 25-08-2017 / 12:32:47 / cg" |
|
1 | 1654 |
! ! |
1655 |
||
25039 | 1656 |
|
25215 | 1657 |
|
4423 | 1658 |
!Integer methodsFor:'bcd conversion'! |
1659 |
||
1660 |
decodeFromBCD |
|
1661 |
"return a number representing the value of the BCD encoded receiver." |
|
1662 |
||
4424 | 1663 |
|v rslt multiplier nibble| |
4423 | 1664 |
|
1665 |
v := self. |
|
1666 |
rslt := 0. |
|
1667 |
multiplier := 1. |
|
1668 |
[v > 0] whileTrue:[ |
|
19078 | 1669 |
nibble := v bitAnd:16r0F. |
1670 |
nibble > 9 ifTrue:[ |
|
1671 |
^ BCDConversionErrorSignal |
|
1672 |
raiseRequestWith:self |
|
1673 |
errorString:'bad BCD coded value' |
|
1674 |
]. |
|
1675 |
rslt := rslt + (nibble * multiplier). |
|
1676 |
multiplier := multiplier * 10. |
|
1677 |
v := v bitShift:-4. |
|
4423 | 1678 |
]. |
1679 |
^ rslt |
|
1680 |
||
1681 |
" |
|
4949 | 1682 |
16r1234567890123 decodeFromBCD |
11587 | 1683 |
16r1073741823 decodeFromBCD |
1684 |
16r1073741824 decodeFromBCD |
|
1685 |
16r1073741825 decodeFromBCD |
|
1686 |
||
1687 |
16r55 decodeFromBCD |
|
1688 |
16r127 decodeFromBCD |
|
1689 |
16r800000 decodeFromBCD |
|
1690 |
16r8000000 decodeFromBCD |
|
1691 |
16r80000000 decodeFromBCD |
|
1692 |
16r800000000 decodeFromBCD |
|
1693 |
16r127567890 decodeFromBCD |
|
1694 |
16r1234567890 decodeFromBCD |
|
1695 |
||
1696 |
16r5A decodeFromBCD |
|
1697 |
16rFF decodeFromBCD |
|
4423 | 1698 |
" |
1699 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1700 |
"Modified: / 15.11.1999 / 20:37:20 / cg" |
4423 | 1701 |
! |
1702 |
||
1703 |
encodeAsBCD |
|
1704 |
"return a BCD encoded number representing the same value as the |
|
1705 |
receiver." |
|
1706 |
||
1707 |
|v rslt shift| |
|
1708 |
||
1709 |
v := self. |
|
1710 |
rslt := shift := 0. |
|
1711 |
[v > 0] whileTrue:[ |
|
19078 | 1712 |
rslt := rslt + ((v \\ 10) bitShift:shift). |
1713 |
shift := shift + 4. |
|
1714 |
v := v // 10. |
|
4423 | 1715 |
]. |
1716 |
^ rslt |
|
1717 |
||
1718 |
" |
|
11587 | 1719 |
55 encodeAsBCD hexPrintString |
1720 |
127 encodeAsBCD hexPrintString |
|
1721 |
127 encodeAsBCD hexPrintString |
|
1722 |
8912345 encodeAsBCD hexPrintString |
|
1723 |
89123456 encodeAsBCD hexPrintString |
|
1724 |
891234567 encodeAsBCD hexPrintString |
|
1725 |
900000000 encodeAsBCD hexPrintString |
|
1726 |
1073741823 encodeAsBCD hexPrintString |
|
1727 |
1073741824 encodeAsBCD hexPrintString |
|
1728 |
1073741825 encodeAsBCD hexPrintString |
|
1729 |
1891234567 encodeAsBCD hexPrintString |
|
1730 |
8912345678 encodeAsBCD hexPrintString |
|
1731 |
1234567890 encodeAsBCD hexPrintString |
|
4423 | 1732 |
" |
1733 |
||
1734 |
! ! |
|
1735 |
||
69 | 1736 |
!Integer methodsFor:'bit operators'! |
1737 |
||
17248 | 1738 |
allMask:aMaskInteger |
1739 |
"return true if all 1-bits in aMaskInteger are also 1 in the receiver" |
|
1740 |
||
1741 |
^ (self bitAnd:aMaskInteger) == aMaskInteger |
|
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1742 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1743 |
"2r00001111 allMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1744 |
"2r00001111 allMask:2r00011110" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1745 |
"2r00001111 allMask:2r00000000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1746 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1747 |
|
17248 | 1748 |
anyMask:aMaskInteger |
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1749 |
"return true if any 1-bits in anInteger is also 1 in the receiver. |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1750 |
(somewhat incorrect, if the mask is zero)" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1751 |
|
17248 | 1752 |
^ (self bitAnd:aMaskInteger) ~~ 0 |
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1753 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1754 |
"2r00001111 anyMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1755 |
"2r00001111 anyMask:2r11110000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1756 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1757 |
|
17248 | 1758 |
bitAnd:aMaskInteger |
357 | 1759 |
"return the bitwise-and of the receiver and the argument, anInteger. |
1760 |
This is a general and slow implementation, walking over the bytes of |
|
1761 |
the receiver and the argument." |
|
69 | 1762 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1763 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1764 |
result byte| |
69 | 1765 |
|
17248 | 1766 |
aMaskInteger isInteger ifFalse:[ |
19078 | 1767 |
^ aMaskInteger bitAndFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1768 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1769 |
|
17248 | 1770 |
n := (aMaskInteger digitLength) min:(self digitLength). |
92 | 1771 |
result := self class basicNew numberOfDigits:n. |
69 | 1772 |
|
1773 |
1 to:n do:[:index | |
|
19078 | 1774 |
byte := (aMaskInteger digitAt:index) bitAnd:(self digitAt:index). |
1775 |
result digitAt:index put:byte. |
|
69 | 1776 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1777 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
19078 | 1778 |
^ result compressed |
92 | 1779 |
]. |
1780 |
^ result |
|
357 | 1781 |
|
1782 |
" |
|
11587 | 1783 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
1784 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
1785 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
1786 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
357 | 1787 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1788 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1789 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 1790 |
! |
1791 |
||
17248 | 1792 |
bitClear:aMaskInteger |
19071 | 1793 |
"return the bitwise-and of the receiver and the complement of the argument, anInteger, |
16379 | 1794 |
returning the receiver with bits of the argument cleared. |
19071 | 1795 |
(i.e. the same as self bitAnd:aMaskInteger bitInvert). |
3884 | 1796 |
This is a general and slow implementation, walking over the bytes of |
1797 |
the receiver and the argument." |
|
1798 |
||
1799 |
|n "{ Class: SmallInteger }" |
|
1800 |
result byte| |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1801 |
|
17248 | 1802 |
n := (aMaskInteger digitLength) max:(self digitLength). |
3884 | 1803 |
result := self class basicNew numberOfDigits:n. |
1804 |
||
1805 |
1 to:n do:[:index | |
|
19078 | 1806 |
byte := (self digitAt:index) bitClear:(aMaskInteger digitAt:index). |
1807 |
result digitAt:index put:byte. |
|
3884 | 1808 |
]. |
1809 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
19078 | 1810 |
^ result compressed |
3884 | 1811 |
]. |
1812 |
^ result |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1813 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1814 |
|
11492 | 1815 |
bitCount |
1816 |
"return the number of 1-bits in the receiver" |
|
1817 |
||
1818 |
|n "{ Class: SmallInteger }" |
|
1819 |
cnt byte| |
|
1820 |
||
1821 |
n := self digitLength. |
|
1822 |
cnt := 0. |
|
1823 |
||
1824 |
1 to:n do:[:index | |
|
19078 | 1825 |
byte := self digitAt:index. |
1826 |
cnt := cnt + (byte bitCount) |
|
11492 | 1827 |
]. |
1828 |
^ cnt |
|
1829 |
||
1830 |
" |
|
11587 | 1831 |
2r100000000000000000000000000000000000000000000000000000000001 bitCount |
11492 | 1832 |
2r111111111111111111111111111111111111111111111111111111111111111111 bitCount |
13914 | 1833 |
100 factorial bitCount -> 207 |
1834 |
1000 factorial bitCount -> 3788 |
|
11492 | 1835 |
" |
13914 | 1836 |
|
1837 |
"Modified (comment): / 09-01-2012 / 19:51:00 / cg" |
|
11492 | 1838 |
! |
1839 |
||
22219 | 1840 |
bitDeinterleave:n |
1841 |
"extract count integers from an n-way Morton number as a vector; |
|
22222 | 1842 |
This is the inverse operation from bitInterleave: - see comment there. |
22219 | 1843 |
i.e. if count is 3, |
1844 |
and the receiver's bits are |
|
1845 |
cN bN aN ... c2 b2 a2 c1 b1 a1 c0 b0 a0 |
|
1846 |
then the result will be a vector containing the numbers a,b,c with bits: |
|
1847 |
aN ... a2 a1 a0 |
|
1848 |
bN ... b2 b1 b0 |
|
22222 | 1849 |
cN ... c2 c1 c0." |
22219 | 1850 |
|
1851 |
|v shift tuple| |
|
1852 |
||
1853 |
tuple := Array new:n withAll:0. |
|
1854 |
||
1855 |
shift := 0. |
|
1856 |
v := self. |
|
1857 |
[ v > 0 ] whileTrue:[ |
|
1858 |
1 to:n do:[:i | |
|
1859 |
tuple at:i put:((tuple at:i) bitOr:((v bitAnd:1) bitShift:shift)). |
|
1860 |
v := v rightShift:1. |
|
1861 |
]. |
|
1862 |
shift := shift + 1. |
|
1863 |
]. |
|
1864 |
^ tuple |
|
1865 |
||
1866 |
" |
|
1867 |
(2r1100 bitInterleaveWith:2r1001) -> 2r11100001 |
|
1868 |
(2r11000110 bitInterleaveWith:2r10011100 and:2r10100101) -> 2r111100001010010111100001. |
|
1869 |
||
1870 |
2r11100001 bitDeinterleave:2 |
|
1871 |
||
1872 |
(2r11000110 bitInterleaveWith:2r10011100 and:2r10100101) |
|
1873 |
(198 bitInterleaveWith:156 and:165) bitDeinterleave:3 |
|
1874 |
" |
|
1875 |
||
1876 |
"Created: / 28-08-2017 / 15:02:31 / cg" |
|
22222 | 1877 |
"Modified (comment): / 28-08-2017 / 18:45:21 / cg" |
22219 | 1878 |
! |
1879 |
||
1880 |
bitInterleaveWith:anInteger |
|
1881 |
"generate a Morton number (-> https://en.wikipedia.org/wiki/Morton_number_(number_theory)) |
|
1882 |
by interleaving bits of the receiver (at odd positions if counting from 1) |
|
1883 |
with bits of the argument (at even bit positions). |
|
1884 |
||
1885 |
Thus, if the bits of the receiver are |
|
1886 |
aN ... a2 a1 a0 |
|
1887 |
and those of the argument are: |
|
1888 |
bN ... b2 b1 b0 |
|
1889 |
the result is |
|
1890 |
bN aN ... b2 a2 b1 a1 b0 a0. |
|
1891 |
||
1892 |
Morton numbers are great to linearize 2D coordinates |
|
1893 |
eg. to sort 2D points by distances" |
|
1894 |
||
1895 |
"/ a naive and slow fallback here, using a small map to process cunks of 4 bits |
|
1896 |
||
1897 |
|a b shift ma mb val| |
|
1898 |
||
1899 |
a := self. |
|
1900 |
b := anInteger. |
|
24456 | 1901 |
self assert:(a >= 0). |
1902 |
self assert:(b >= 0). |
|
1903 |
||
22219 | 1904 |
val := 0. |
1905 |
shift := 0. |
|
1906 |
[ (a == 0) and:[b == 0]] whileFalse:[ |
|
1907 |
"/ strip off 4 bits from each... |
|
1908 |
"/ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 |
|
1909 |
mb := #[ 2r00000000 2r00000010 2r00001000 2r00001010 2r00100000 2r00100010 2r00101000 2r00101010 2r10000000 2r10000010 2r10001000 2r10001010 2r10100000 2r10100010 2r10101000 2r10101010 ] |
|
1910 |
at:((b bitAnd:2r1111)+1). |
|
1911 |
ma := #[ 2r00000000 2r00000001 2r00000100 2r00000101 2r00010000 2r00010001 2r00010100 2r00010101 2r01000000 2r01000001 2r01000100 2r01000101 2r01010000 2r01010001 2r01010100 2r01010101 ] |
|
1912 |
at:((a bitAnd:2r1111)+1). |
|
1913 |
val := val bitOr:((ma bitOr:mb) bitShift:shift). |
|
1914 |
shift := shift + 8. |
|
1915 |
a := a rightShift:4. |
|
1916 |
b := b rightShift:4. |
|
1917 |
]. |
|
1918 |
^ val. |
|
1919 |
||
1920 |
" |
|
1921 |
(2r11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1922 |
bitInterleaveWith:2r10010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) |
|
1923 |
-> 2r1101001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1924 |
||
1925 |
(2r11000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1926 |
bitInterleaveWith:2r10010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) |
|
1927 |
-> 2r1101001000010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1928 |
" |
|
1929 |
||
1930 |
"Created: / 28-08-2017 / 14:33:08 / cg" |
|
22222 | 1931 |
"Modified: / 28-08-2017 / 19:19:31 / cg" |
24456 | 1932 |
"Modified: / 22-07-2019 / 19:43:04 / Claus Gittinger" |
22219 | 1933 |
! |
1934 |
||
1935 |
bitInterleaveWith:integer1 and:integer2 |
|
1936 |
"generate a Morton3 number (-> https://en.wikipedia.org/wiki/Morton_number_(number_theory)) |
|
1937 |
by interleaving bits of the receiver with bits of the arguments. |
|
1938 |
Thus, if the bits of the receiver are |
|
1939 |
aN ... a2 a1 a0 |
|
1940 |
and those of the integer1 are: |
|
1941 |
bN ... b2 b1 b0 |
|
1942 |
and those of the integer2 are: |
|
1943 |
cN ... c2 c1 c0 |
|
1944 |
the result is |
|
1945 |
cN bN aN ... c2 b2 a2 c1 b1 a1 c0 b0 a0. |
|
1946 |
||
1947 |
Morton3 numbers are great to linearize 3D coordinates |
|
1948 |
eg. to sort 3D points by distances" |
|
1949 |
||
1950 |
|a b c shift ma mb mc val| |
|
1951 |
||
1952 |
a := self. |
|
1953 |
b := integer1. |
|
1954 |
c := integer2. |
|
22469 | 1955 |
self assert:(a >= 0). |
1956 |
self assert:(b >= 0). |
|
1957 |
self assert:(c >= 0). |
|
1958 |
||
22219 | 1959 |
val := 0. |
1960 |
shift := 0. |
|
22469 | 1961 |
[ a ~~ 0 or:[b ~~ 0 or:[c ~~ 0]] ] whileTrue:[ |
22219 | 1962 |
"/ strip off 4 bits from each... |
1963 |
"/ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 |
|
1964 |
mc := #( 2r000000000000 2r000000000100 2r000000100000 2r000000100100 2r000100000000 2r000100000100 2r000100100000 2r000100100100 2r100000000000 2r100000000100 2r100000100000 2r100000100100 2r100100000000 2r100100000100 2r100100100000 2r100100100100 ) |
|
1965 |
at:((c bitAnd:2r1111)+1). |
|
1966 |
mb := #( 2r000000000000 2r000000000010 2r000000010000 2r000000010010 2r000010000000 2r000010000010 2r000010010000 2r000010010010 2r010000000000 2r010000000010 2r010000010000 2r010000010010 2r010010000000 2r010010000010 2r010010010000 2r010010010010 ) |
|
1967 |
at:((b bitAnd:2r1111)+1). |
|
1968 |
ma := #( 2r000000000000 2r000000000001 2r000000001000 2r000000001001 2r000001000000 2r000001000001 2r000001001000 2r000001001001 2r001000000000 2r001000000001 2r001000001000 2r001000001001 2r001001000000 2r001001000001 2r001001001000 2r001001001001 ) |
|
1969 |
at:((a bitAnd:2r1111)+1). |
|
1970 |
val := val bitOr:(((ma bitOr:mb) bitOr:mc) bitShift:shift). |
|
1971 |
shift := shift + 12. |
|
1972 |
a := a rightShift:4. |
|
1973 |
b := b rightShift:4. |
|
1974 |
c := c rightShift:4. |
|
1975 |
]. |
|
1976 |
^ val. |
|
1977 |
||
1978 |
" |
|
1979 |
(2r1100 bitInterleaveWith:2r1001 and:2r1010) printStringRadix:2 -> '111 001 100 010' |
|
22222 | 1980 |
|
22219 | 1981 |
(2r11000110 bitInterleaveWith:2r10011100 and:2r10100101) printStringRadix:2 -> '111 001 100 010 010 111 001 100' |
22222 | 1982 |
(1 bitInterleaveWith:1 and:16) |
1983 |
||
1984 |
((1<<31) bitInterleaveWith:(1<<31) and:(1<<31)) bitDeinterleave:3 |
|
1985 |
((1<<31) bitInterleaveWith:(1<<63) and:(1<<95)) bitDeinterleave:3 |
|
22219 | 1986 |
" |
1987 |
||
1988 |
"Created: / 28-08-2017 / 14:33:04 / cg" |
|
22222 | 1989 |
"Modified: / 28-08-2017 / 19:19:25 / cg" |
22469 | 1990 |
"Modified: / 19-01-2018 / 11:29:04 / stefan" |
22219 | 1991 |
! |
1992 |
||
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1993 |
bitInvert |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1994 |
"return a new integer, where all bits are complemented. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1995 |
This does not really make sense for negative largeIntegers, |
12255 | 1996 |
since the digits are stored as absolute value. |
1997 |
Q: is this specified in a language standard ?" |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1998 |
|
4614 | 1999 |
"/ ^ -1 - self |
2000 |
||
15841 | 2001 |
|n "{ Class: SmallInteger }" |
2002 |
byte "{ Class: SmallInteger }" |
|
2003 |
result| |
|
4614 | 2004 |
|
2005 |
n := self digitLength. |
|
2006 |
result := self class basicNew numberOfDigits:n. |
|
2007 |
||
2008 |
1 to:n do:[:index | |
|
19078 | 2009 |
byte := self digitAt:index. |
2010 |
byte := byte bitInvert bitAnd:16rFF. |
|
2011 |
result digitAt:index put:byte. |
|
4614 | 2012 |
]. |
15841 | 2013 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
19078 | 2014 |
"if last byte is zero we can normalize" |
2015 |
^ result compressed |
|
4614 | 2016 |
]. |
2017 |
^ result |
|
2018 |
||
19068 | 2019 |
" |
2020 |
16rff bitInvert bitAnd:16rff |
|
2021 |
16rffffffff bitInvert |
|
2022 |
16rff00ff00 bitInvert hexPrintString |
|
2023 |
" |
|
2024 |
! |
|
2025 |
||
2026 |
bitInvertByte |
|
2027 |
"return a new integer, where the low 8 bits are masked and complemented. |
|
2028 |
This returns an unsigned version of what bitInvert would return. |
|
2029 |
(i.e. same as self bitInvert bitAnd:16rFF)" |
|
2030 |
||
2031 |
^ (self digitAt:1) bitInvert bitAnd:16rFF |
|
2032 |
||
2033 |
" |
|
2034 |
16rff bitInvert |
|
2035 |
16rff bitInvertByte |
|
2036 |
" |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2037 |
! |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
2038 |
|
17248 | 2039 |
bitOr:aMaskInteger |
701 | 2040 |
"return the bitwise-or of the receiver and the argument, anInteger. |
2041 |
This is a general and slow implementation, walking over the bytes of |
|
23118 | 2042 |
the receiver and the argument. |
2043 |
It is redefined in concrete subclasses (especially SmallInteger) for performance." |
|
701 | 2044 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
2045 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
2046 |
result byte| |
701 | 2047 |
|
17248 | 2048 |
aMaskInteger isInteger ifFalse:[ |
19078 | 2049 |
^ aMaskInteger bitOrFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2050 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2051 |
|
17248 | 2052 |
n := (aMaskInteger digitLength) max:(self digitLength). |
701 | 2053 |
result := self class basicNew numberOfDigits:n. |
2054 |
||
2055 |
1 to:n do:[:index | |
|
19078 | 2056 |
byte := (aMaskInteger digitAt:index) bitOr:(self digitAt:index). |
2057 |
result digitAt:index put:byte. |
|
701 | 2058 |
]. |
2059 |
"/ no need to normalize - if the operands were correct |
|
2060 |
"/ byte == 0 ifTrue:[ |
|
23496 | 2061 |
"/ ^ result compressed |
701 | 2062 |
"/ ]. |
2063 |
^ result |
|
23118 | 2064 |
|
23496 | 2065 |
" |
2066 |
16rFFFFFFFFFFFFFFFFFFFF0 bitOr:1.0 |
|
2067 |
16rFFFFFFFFFFFFFFFFFFFF0 bitOr:1.0s1 |
|
2068 |
16rFFFFFFFFFFFFFFFFFFFF0 bitOr:1.1 |
|
2069 |
" |
|
2070 |
||
23118 | 2071 |
"Modified (comment): / 20-06-2018 / 14:47:43 / Claus Gittinger" |
23496 | 2072 |
"Modified (comment): / 01-11-2018 / 12:10:27 / Stefan Vogel" |
2073 |
! |
|
2074 |
||
2075 |
bitReversed |
|
2076 |
"swap (i.e. reverse) bits in an integer |
|
23994 | 2077 |
i.e. a.b.c.d....x.y.z -> z.y.x...b.a.d.c. |
2078 |
Warning: |
|
2079 |
do not use this without care: it depends on the machine's |
|
2080 |
word size; i.e. a 64bit machine will return a different result as a 32bit machine. |
|
2081 |
Better use one of the bitReversedXX methods. |
|
2082 |
This my vanish or be replaced by something better" |
|
23496 | 2083 |
|
2084 |
|n "{ Class: SmallInteger }" |
|
2085 |
result byte| |
|
2086 |
||
2087 |
n := self digitLength. |
|
2088 |
result := self class basicNew numberOfDigits:n. |
|
2089 |
||
2090 |
1 to:n do:[:index | |
|
2091 |
byte := (self digitAt:index) bitReversed8. |
|
2092 |
result digitAt:n+1-index put:byte. |
|
2093 |
]. |
|
23992 | 2094 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
23496 | 2095 |
^ result compressed |
2096 |
]. |
|
2097 |
||
2098 |
^ result |
|
2099 |
||
2100 |
" |
|
2101 |
2r1001 asLargeInteger bitReversed printStringRadix:2 |
|
2102 |
2r10011101 asLargeInteger bitReversed printStringRadix:2 |
|
2103 |
2r111110011101 asLargeInteger bitReversed printStringRadix:2 |
|
2104 |
2r11111001110100000000000000000000000000000000000000000001 bitReversed printStringRadix:2 |
|
2105 |
-1 asLargeInteger bitReversed printStringRadix:2 |
|
2106 |
" |
|
2107 |
||
2108 |
"Created: / 01-11-2018 / 11:22:42 / Stefan Vogel" |
|
23994 | 2109 |
"Modified (comment): / 27-03-2019 / 18:28:42 / Claus Gittinger" |
23496 | 2110 |
! |
2111 |
||
2112 |
bitReversed16 |
|
2113 |
"swap (i.e. reverse) the low 16 bits in an integer |
|
24005 | 2114 |
the high bits are ignored and cleared in the result |
23496 | 2115 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
2116 |
||
23999 | 2117 |
|rslt| |
2118 |
||
2119 |
rslt := (self digitAt:2) bitReversed8. |
|
2120 |
rslt := rslt bitOr:((self digitAt:1) bitReversed8 bitShift:8). |
|
2121 |
^ rslt. |
|
2122 |
||
2123 |
" |
|
2124 |
16r8000 bitReversed16 |
|
2125 |
16r87654321 bitReversed16 printStringRadix:2 |
|
2126 |
16rFEDCBA987654321 bitReversed16 printStringRadix:2 |
|
23496 | 2127 |
" |
2128 |
||
2129 |
"Created: / 01-11-2018 / 11:34:51 / Stefan Vogel" |
|
24005 | 2130 |
"Modified (comment): / 27-03-2019 / 15:14:45 / stefan" |
23496 | 2131 |
! |
2132 |
||
2133 |
bitReversed32 |
|
2134 |
"swap (i.e. reverse) the low 32 bits in an integer |
|
24005 | 2135 |
the high bits are ignored and cleared in the result |
23496 | 2136 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
2137 |
||
23999 | 2138 |
|rslt| |
2139 |
||
2140 |
rslt := (self digitAt:4) bitReversed8. |
|
2141 |
rslt := rslt bitOr:((self digitAt:3) bitReversed8 bitShift:8). |
|
2142 |
rslt := rslt bitOr:((self digitAt:2) bitReversed8 bitShift:16). |
|
2143 |
rslt := rslt bitOr:((self digitAt:1) bitReversed8 bitShift:24). |
|
2144 |
^ rslt. |
|
2145 |
||
2146 |
" |
|
2147 |
16r80000000 bitReversed32 |
|
23496 | 2148 |
2r11111001110100000000000000000000000000000000000000000001 bitReversed32 printStringRadix:2 |
2149 |
-1 asLargeInteger bitReversed32 printStringRadix:2 |
|
2150 |
" |
|
2151 |
||
2152 |
"Created: / 01-11-2018 / 11:35:54 / Stefan Vogel" |
|
24005 | 2153 |
"Modified (comment): / 27-03-2019 / 15:14:39 / stefan" |
23496 | 2154 |
! |
2155 |
||
23992 | 2156 |
bitReversed64 |
24005 | 2157 |
"swap (i.e. reverse) the low 64 bits in an integer |
2158 |
the high bits are ignored and cleared in the result |
|
23992 | 2159 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
2160 |
||
23999 | 2161 |
|rslt| |
2162 |
||
2163 |
rslt := (self digitAt:8) bitReversed8. |
|
2164 |
rslt := rslt bitOr:((self digitAt:7) bitReversed8 bitShift:8). |
|
2165 |
rslt := rslt bitOr:((self digitAt:6) bitReversed8 bitShift:16). |
|
2166 |
rslt := rslt bitOr:((self digitAt:5) bitReversed8 bitShift:24). |
|
2167 |
rslt := rslt bitOr:((self digitAt:4) bitReversed8 bitShift:32). |
|
2168 |
rslt := rslt bitOr:((self digitAt:3) bitReversed8 bitShift:40). |
|
2169 |
rslt := rslt bitOr:((self digitAt:2) bitReversed8 bitShift:48). |
|
2170 |
rslt := rslt bitOr:((self digitAt:1) bitReversed8 bitShift:56). |
|
2171 |
^ rslt. |
|
2172 |
||
2173 |
" |
|
2174 |
16r80000000 bitReversed32 |
|
2175 |
16r80000000 bitReversed64 |
|
2176 |
16r8000000000000000 bitReversed64 |
|
2177 |
2r11111001110100000000000000000000000000000000000000000001 bitReversed32 printStringRadix:2 |
|
2178 |
-1 asLargeInteger bitReversed32 printStringRadix:2 |
|
2179 |
" |
|
2180 |
||
2181 |
"Created: / 01-11-2018 / 11:35:54 / Stefan Vogel" |
|
24005 | 2182 |
"Modified (comment): / 27-03-2019 / 15:14:33 / stefan" |
23992 | 2183 |
! |
2184 |
||
23496 | 2185 |
bitReversed8 |
2186 |
"swap (i.e. reverse) the low 8 bits in an integer |
|
24005 | 2187 |
the high bits are ignored and cleared in the result |
23496 | 2188 |
i.e. xxx.a.b.c.d....x.y.z -> 000.z.y.x...b.a.d.c." |
2189 |
||
2190 |
^ (self digitAt:1) bitReversed8. |
|
2191 |
||
2192 |
" |
|
2193 |
2r1001 asLargeInteger bitReversed8 printStringRadix:2 |
|
2194 |
2r10011101 asLargeInteger bitReversed8 printStringRadix:2 |
|
2195 |
2r111110011101 asLargeInteger bitReversed8 printStringRadix:2 |
|
2196 |
2r11111001110100000000000000000000000000000000000000000001 bitReversed8 printStringRadix:2 |
|
2197 |
-1 asLargeInteger bitReversed8 printStringRadix:2 |
|
2198 |
" |
|
2199 |
||
2200 |
"Created: / 01-11-2018 / 11:32:58 / Stefan Vogel" |
|
24005 | 2201 |
"Modified (comment): / 27-03-2019 / 15:14:54 / stefan" |
701 | 2202 |
! |
2203 |
||
85 | 2204 |
bitShift:shiftCount |
2205 |
"return the value of the receiver shifted by shiftCount bits; |
|
2206 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 2207 |
|
11587 | 2208 |
Notice: the result of bitShift: on negative receivers is not |
19078 | 2209 |
defined in the language standard (since the implementation |
2210 |
is free to choose any internal representation for integers) |
|
2211 |
However, ST/X preserves the sign." |
|
11587 | 2212 |
|
2213 |
|result |
|
92 | 2214 |
prev "{ Class: SmallInteger }" |
2215 |
next "{ Class: SmallInteger }" |
|
2216 |
byte "{ Class: SmallInteger }" |
|
359 | 2217 |
byte2 "{ Class: SmallInteger }" |
92 | 2218 |
bitShift "{ Class: SmallInteger }" |
2219 |
revShift "{ Class: SmallInteger }" |
|
2220 |
digitShift "{ Class: SmallInteger }" |
|
3044 | 2221 |
nn "{ Class: SmallInteger }" |
2222 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 2223 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2224 |
shiftCount isInteger ifFalse:[ |
19078 | 2225 |
^ shiftCount bitShiftFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2226 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2227 |
|
85 | 2228 |
shiftCount > 0 ifTrue:[ |
19078 | 2229 |
"left shift" |
2230 |
||
19874 | 2231 |
nDigits := self digitLength. |
19078 | 2232 |
digitShift := shiftCount // 8. |
2233 |
bitShift := shiftCount \\ 8. |
|
2234 |
||
2235 |
" |
|
2236 |
modulo 8 shifts can be done faster ... |
|
2237 |
" |
|
2238 |
bitShift == 0 ifTrue:[ |
|
19874 | 2239 |
nn := nDigits + digitShift. |
2240 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
|
2241 |
result digitBytes replaceFrom:(digitShift + 1) to:nn with:self digitBytes. |
|
19078 | 2242 |
" |
2243 |
no normalize needed, since receiver was already normalized |
|
2244 |
" |
|
2245 |
^ result |
|
2246 |
]. |
|
2247 |
||
2248 |
" |
|
2249 |
less-than-8 shifts can be done faster ... |
|
2250 |
" |
|
2251 |
digitShift == 0 ifTrue:[ |
|
19874 | 2252 |
nn := nDigits+1. |
19078 | 2253 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
2254 |
prev := 0. |
|
19874 | 2255 |
1 to:nDigits do:[:index | |
19078 | 2256 |
byte := self digitAt:index. |
2257 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
2258 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
2259 |
prev := byte bitShift:-8. |
|
2260 |
]. |
|
2261 |
result digitAt:nn put:prev. |
|
2262 |
" |
|
2263 |
might have stored a 0-byte ... |
|
2264 |
" |
|
2265 |
prev == 0 ifTrue:[ |
|
2266 |
^ result compressed |
|
2267 |
]. |
|
2268 |
^ result. |
|
2269 |
]. |
|
2270 |
||
2271 |
" |
|
2272 |
slow case ... |
|
2273 |
" |
|
19874 | 2274 |
nn := nDigits + digitShift + 1. |
2275 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
|
19078 | 2276 |
byte := self digitAt:1. |
2277 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
2278 |
result digitAt:(digitShift + 1) put:byte. |
|
2279 |
revShift := -8 + bitShift. |
|
2280 |
2 to:nDigits do:[:index | |
|
2281 |
byte := self digitAt:index. |
|
2282 |
byte2 := self digitAt:index-1. |
|
2283 |
byte := byte bitShift:bitShift. |
|
2284 |
byte2 := byte2 bitShift:revShift. |
|
2285 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
|
2286 |
result digitAt:(index + digitShift) put:byte. |
|
2287 |
]. |
|
2288 |
byte2 := self digitAt:nDigits. |
|
2289 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
|
2290 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
|
2291 |
" |
|
2292 |
might have stored a 0-byte ... |
|
2293 |
" |
|
2294 |
byte2 == 0 ifTrue:[ |
|
2295 |
^ result compressed |
|
2296 |
]. |
|
2297 |
^ result |
|
92 | 2298 |
]. |
2299 |
||
2300 |
shiftCount < 0 ifTrue:[ |
|
19078 | 2301 |
"right shift" |
2302 |
||
19874 | 2303 |
nDigits := self digitLength. |
19078 | 2304 |
digitShift := shiftCount negated // 8. |
2305 |
bitShift := shiftCount negated \\ 8. |
|
19874 | 2306 |
|
2307 |
digitShift >= nDigits ifTrue:[ |
|
19078 | 2308 |
^ 0 |
2309 |
]. |
|
2310 |
||
2311 |
" |
|
2312 |
modulo 8 shifts can be done faster ... |
|
2313 |
" |
|
2314 |
bitShift == 0 ifTrue:[ |
|
19874 | 2315 |
nn := nDigits-digitShift. |
2316 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
|
2317 |
result digitBytes replaceFrom:1 to:nn with:self digitBytes startingAt:(digitShift + 1) . |
|
2318 |
nn <= SmallInteger maxBytes ifTrue:[ |
|
19078 | 2319 |
^ result compressed |
2320 |
]. |
|
2321 |
^ result |
|
2322 |
]. |
|
2323 |
||
2324 |
" |
|
2325 |
less-than-8 shifts can be done faster ... |
|
2326 |
" |
|
2327 |
digitShift == 0 ifTrue:[ |
|
19874 | 2328 |
result := self class basicNew numberOfDigits:nDigits sign:self sign. |
19078 | 2329 |
prev := 0. |
2330 |
bitShift := bitShift negated. |
|
2331 |
revShift := 8 + bitShift. |
|
19874 | 2332 |
nDigits to:1 by:-1 do:[:index | |
19078 | 2333 |
byte := self digitAt:index. |
2334 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
2335 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
2336 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
2337 |
prev := next. |
|
2338 |
]. |
|
2339 |
^ result compressed |
|
2340 |
]. |
|
2341 |
||
2342 |
" |
|
2343 |
slow case ... |
|
2344 |
" |
|
19874 | 2345 |
nn := nDigits-digitShift. |
19078 | 2346 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
2347 |
||
2348 |
prev := 0. |
|
2349 |
bitShift := bitShift negated. |
|
2350 |
revShift := 8 + bitShift. |
|
2351 |
nn := digitShift + 1. |
|
19874 | 2352 |
nDigits to:nn by:-1 do:[:index | |
19078 | 2353 |
byte := self digitAt:index. |
2354 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
2355 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
2356 |
result digitAt:(index - digitShift) put:byte. |
|
2357 |
prev := next. |
|
2358 |
]. |
|
2359 |
"the last stored byte ..." |
|
2360 |
^ result compressed |
|
85 | 2361 |
]. |
92 | 2362 |
|
2363 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2364 |
|
3638 | 2365 |
"Modified: / 8.7.1998 / 12:45:24 / cg" |
4151 | 2366 |
"Modified: / 5.5.1999 / 16:05:05 / stefan" |
701 | 2367 |
! |
1 | 2368 |
|
17248 | 2369 |
bitTest:aMaskInteger |
5782 | 2370 |
"return true, if any bit from aMask is set in the receiver. |
2371 |
I.e. true, if the bitwise-AND of the receiver and the argument, anInteger |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2372 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2373 |
This is a general and slow implementation, walking over the bytes of |
23118 | 2374 |
the receiver and the argument. |
2375 |
It is redefined in concrete subclasses (especially SmallInteger) for performance." |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2376 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2377 |
|n "{ Class: SmallInteger }" |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2378 |
byte| |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2379 |
|
17248 | 2380 |
n := (aMaskInteger digitLength) min:(self digitLength). |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2381 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2382 |
1 to:n do:[:index | |
19078 | 2383 |
byte := (aMaskInteger digitAt:index) bitAnd:(self digitAt:index). |
2384 |
byte ~~ 0 ifTrue:[^ true]. |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2385 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2386 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2387 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2388 |
" |
11587 | 2389 |
16r112233445566778899 bitTest:16rFF |
2390 |
16r112233445566778800 bitTest:16rFF |
|
2391 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
|
2392 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
|
2393 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
|
2394 |
16r1234567800000000 bitTest:16r8000000000000000 |
|
2395 |
16r8765432100000000 bitTest:16r8000000000000000 |
|
2396 |
16r12345678 bitTest:16r80000000 |
|
2397 |
16r87654321 bitTest:16r80000000 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2398 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2399 |
|
23118 | 2400 |
"Modified: / 06-06-1999 / 15:10:33 / cg" |
2401 |
"Modified (comment): / 20-06-2018 / 14:47:28 / Claus Gittinger" |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2402 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2403 |
|
701 | 2404 |
bitXor:anInteger |
2405 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
2406 |
This is a general and slow implementation, walking over the bytes of |
|
23118 | 2407 |
the receiver and the argument. |
2408 |
It is redefined in concrete subclasses (especially SmallInteger) for performance." |
|
701 | 2409 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
2410 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
2411 |
result byte| |
701 | 2412 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2413 |
anInteger isInteger ifFalse:[ |
19078 | 2414 |
^ anInteger bitXorFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2415 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2416 |
|
701 | 2417 |
n := (anInteger digitLength) max:(self digitLength). |
2418 |
result := self class basicNew numberOfDigits:n. |
|
1 | 2419 |
|
701 | 2420 |
1 to:n do:[:index | |
19078 | 2421 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
2422 |
result digitAt:index put:byte. |
|
701 | 2423 |
]. |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
2424 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
19078 | 2425 |
^ result compressed |
701 | 2426 |
]. |
2427 |
^ result |
|
1 | 2428 |
|
701 | 2429 |
" |
11587 | 2430 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
701 | 2431 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
2432 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
11587 | 2433 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
701 | 2434 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2435 |
|
23118 | 2436 |
"Modified: / 05-11-1996 / 14:06:40 / cg" |
2437 |
"Modified (comment): / 20-06-2018 / 14:47:55 / Claus Gittinger" |
|
1 | 2438 |
! |
2439 |
||
25215 | 2440 |
bitXored:anInteger |
2441 |
"subclasses may redefine this to possibly change the receiver |
|
2442 |
and return an possibly unnormalized Integer. |
|
2443 |
Use to speed up cryptographic computations." |
|
2444 |
||
2445 |
^ self bitXor:anInteger |
|
2446 |
||
2447 |
"Created: / 29-01-2020 / 17:54:19 / Stefan Vogel" |
|
2448 |
! |
|
2449 |
||
12670 | 2450 |
changeMask:mask to:aBooleanOrNumber |
2451 |
"return a new number where the specified mask-bit is on or off, |
|
2452 |
depending on aBooleanOrNumber. |
|
14404
49a54ea47bf8
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14403
diff
changeset
|
2453 |
The method's name may be misleading: the receiver is not changed, |
12670 | 2454 |
but a new number is returned. Should be named #withMask:changedTo:" |
2455 |
||
2456 |
(aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[ |
|
19078 | 2457 |
^ self bitClear:mask |
12670 | 2458 |
]. |
2459 |
^ self bitOr:mask |
|
2460 |
||
2461 |
" |
|
24881 | 2462 |
(16r3fffffff changeMask:16r80 to:0) hexPrintString -> '3FFFFF7F' |
2463 |
(16r3fff0000 changeMask:16r80 to:1) hexPrintString -> '3FFF0080' |
|
2464 |
(16r3fffffFF changeMask:16rFF to:0) hexPrintString -> '3FFFFF00' |
|
2465 |
(16r3fff0000 changeMask:16rFF to:1) hexPrintString -> '3FFF00FF' |
|
2466 |
(16r3fffffFF changeMask:16rFF to:false) hexPrintString -> '3FFFFF00' |
|
2467 |
(16r3fff0000 changeMask:16rFF to:true) hexPrintString -> '3FFF00FF' |
|
12670 | 2468 |
" |
2469 |
! |
|
2470 |
||
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2471 |
even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2472 |
"return true if the receiver is even" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2473 |
|
9401 | 2474 |
^ (self bitAt:1) == 0 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2475 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2476 |
" |
11587 | 2477 |
16r112233445566778899 even |
2478 |
16r112233445566778800 even |
|
2479 |
1 even |
|
2480 |
2 even |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2481 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2482 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2483 |
"Created: / 6.6.1999 / 15:00:40 / cg" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2484 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2485 |
|
701 | 2486 |
highBit |
19600 | 2487 |
"return the bitIndex of the highest bit set. |
2488 |
The returned bitIndex starts at 1 for the least significant bit. |
|
24167 | 2489 |
Returns 0 if no bit is set. |
2490 |
Notice for negative numbers, the returned value is undefined (actually: nonsense), |
|
24287 | 2491 |
because for 2's complement representation, conceptionally all high bits are 1. |
2492 |
But because we use a sign-magnitude representation, you'll get the high bit of |
|
2493 |
the absolute magnitude." |
|
701 | 2494 |
|
2495 |
|byteNr highByte| |
|
2496 |
||
2497 |
byteNr := self digitLength. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2498 |
byteNr == 0 ifTrue:[ |
19078 | 2499 |
^ 0 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2500 |
]. |
701 | 2501 |
highByte := self digitAt:byteNr. |
2502 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 2503 |
|
701 | 2504 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2505 |
0 highBit |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2506 |
-1 highBit |
11587 | 2507 |
(1 bitShift:1) highBit |
2508 |
(1 bitShift:30) highBit |
|
2509 |
(1 bitShift:31) highBit |
|
2510 |
(1 bitShift:32) highBit |
|
2511 |
(1 bitShift:33) highBit |
|
2512 |
(1 bitShift:64) highBit |
|
2513 |
(1 bitShift:1000) highBit |
|
2514 |
(1 bitShift:1000) negated highBit |
|
2515 |
((1 bitShift:64)-1) highBit |
|
701 | 2516 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2517 |
|
24167 | 2518 |
"Modified: / 03-05-1999 / 09:20:57 / stefan" |
24287 | 2519 |
"Modified (comment): / 08-06-2019 / 02:06:47 / Claus Gittinger" |
2429 | 2520 |
! |
2521 |
||
24192 | 2522 |
highBitOfMagnitude |
2523 |
"return the high bit index of my magnitude bits" |
|
2524 |
||
2525 |
| magnitude | |
|
2526 |
||
2527 |
magnitude := self abs. |
|
2528 |
^ magnitude highBit |
|
2529 |
||
2530 |
" |
|
2531 |
17 highBit -> 5 |
|
2532 |
-17 highBit -> 63 (actually undefined) |
|
2533 |
-17 highBitOfMagnitude -> 5 |
|
2534 |
" |
|
2535 |
||
2536 |
"Created: / 27-05-2019 / 08:46:49 / Claus Gittinger" |
|
2537 |
! |
|
2538 |
||
16071 | 2539 |
leftShift:shiftCount |
2540 |
"return the value of the receiver shifted left by shiftCount bits; |
|
2541 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
2542 |
||
2543 |
Notice: the result of bitShift: on negative receivers is not |
|
19078 | 2544 |
defined in the language standard (since the implementation |
2545 |
is free to choose any internal representation for integers) |
|
2546 |
However, ST/X preserves the sign." |
|
16071 | 2547 |
|
2548 |
^ self bitShift:shiftCount |
|
2549 |
||
2550 |
" |
|
2551 |
16r100000000 leftShift:1 |
|
2552 |
16r100000000 negated leftShift:1 |
|
2553 |
" |
|
2554 |
! |
|
2555 |
||
2429 | 2556 |
lowBit |
2557 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
11587 | 2558 |
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:
6358
diff
changeset
|
2559 |
Returns 0 if no bit is set." |
2429 | 2560 |
|
2561 |
|maxBytes "{ Class: SmallInteger }" |
|
2562 |
byte| |
|
2563 |
||
2564 |
maxBytes := self digitLength. |
|
2565 |
1 to:maxBytes do:[:byteIndex | |
|
19078 | 2566 |
byte := self digitAt:byteIndex. |
2567 |
byte ~~ 0 ifTrue:[ |
|
2568 |
^ (byteIndex-1)*8 + (byte lowBit) |
|
2569 |
]. |
|
2429 | 2570 |
]. |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
2571 |
^ 0 "/ should not happen |
2429 | 2572 |
|
2573 |
" |
|
11587 | 2574 |
0 lowBit |
2575 |
1 lowBit |
|
2576 |
(1 bitShift:1) lowBit |
|
2577 |
(1 bitShift:1) highBit |
|
2578 |
(1 bitShift:30) lowBit |
|
2579 |
(1 bitShift:30) highBit |
|
2580 |
(1 bitShift:31) lowBit |
|
2581 |
(1 bitShift:31) highBit |
|
2582 |
(1 bitShift:32) lowBit |
|
2583 |
(1 bitShift:32) highBit |
|
2584 |
(1 bitShift:33) lowBit |
|
2585 |
(1 bitShift:33) highBit |
|
2586 |
(1 bitShift:64) lowBit |
|
2587 |
(1 bitShift:64) highBit |
|
2588 |
(1 bitShift:1000) lowBit |
|
2589 |
(1 bitShift:1000) highBit |
|
2590 |
((1 bitShift:64)-1) lowBit |
|
2591 |
((1 bitShift:64)-1) highBit |
|
2429 | 2592 |
" |
2593 |
||
2594 |
"Modified: 1.3.1997 / 16:54:23 / cg" |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2595 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2596 |
|
17248 | 2597 |
noMask:aMaskInteger |
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2598 |
"return true if no 1-bit in anInteger is 1 in the receiver" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2599 |
|
17248 | 2600 |
^ (self bitAnd:aMaskInteger) == 0 |
2601 |
||
2602 |
" |
|
2603 |
2r00001111 noMask:2r00000001 |
|
2604 |
2r00001111 noMask:2r11110000 |
|
2605 |
" |
|
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2606 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2607 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2608 |
odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2609 |
"return true if the receiver is odd" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2610 |
|
9401 | 2611 |
^ (self bitAt:1) == 1 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2612 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2613 |
" |
11587 | 2614 |
16r112233445566778899 odd |
2615 |
16r112233445566778800 odd |
|
2616 |
1 odd |
|
2617 |
2 odd |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2618 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2619 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2620 |
"Created: / 6.6.1999 / 15:00:55 / cg" |
5209 | 2621 |
! |
2622 |
||
7155 | 2623 |
rightShift:shiftCount |
2624 |
"return the value of the receiver shifted right by shiftCount bits; |
|
2625 |
rightShift if shiftCount > 0; leftShift otherwise. |
|
2626 |
||
11587 | 2627 |
Notice: the result of bitShift: on negative receivers is not |
19078 | 2628 |
defined in the language standard (since the implementation |
2629 |
is free to choose any internal representation for integers) |
|
2630 |
However, ST/X preserves the sign." |
|
7155 | 2631 |
|
2632 |
^ self bitShift:(shiftCount negated) |
|
2633 |
||
2634 |
" |
|
11587 | 2635 |
16r100000000 rightShift:1 |
2636 |
16r100000000 negated rightShift:1 |
|
2637 |
||
2638 |
16r100000000 rightShift:2 |
|
2639 |
16r100000000 negated rightShift:2 |
|
2640 |
||
2641 |
16r100000000 rightShift:3 |
|
2642 |
16r100000000 negated rightShift:3 |
|
2643 |
||
2644 |
((16r100000000 rightShift:1) rightShift:1) rightShift:1 |
|
2645 |
((16r100000000 negated rightShift:1) rightShift:1) rightShift:1 |
|
7155 | 2646 |
" |
19053 | 2647 |
! ! |
2648 |
||
2649 |
!Integer methodsFor:'bit operators - indexed'! |
|
2650 |
||
2651 |
bitAt:index |
|
2652 |
"return the value of the index's bit (index starts at 1) as 0 or 1. |
|
2653 |
Notice: the result of bitAt: on negative receivers is not |
|
19078 | 2654 |
defined in the language standard (since the implementation |
2655 |
is free to choose any internal representation for integers)" |
|
19053 | 2656 |
|
25215 | 2657 |
|i "{Class: SmallInteger}" |
2658 |
d "{Class: SmallInteger}"| |
|
19053 | 2659 |
|
2660 |
i := index - 1. |
|
25215 | 2661 |
d := self digitAt:(i // 8 + 1). "/ #digitAt: raises an error if i < 0 |
2662 |
d == 0 ifTrue:[ |
|
2663 |
^ 0. |
|
19053 | 2664 |
]. |
25215 | 2665 |
^ d bitAt:(i \\ 8 + 1) |
19053 | 2666 |
|
2667 |
" |
|
2668 |
1 bitAt:1 => 1 |
|
2669 |
1 bitAt:2 => 0 |
|
2670 |
1 bitAt:0 index error |
|
2671 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
|
2672 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
|
25215 | 2673 |
2r1000000000000000000000000000000000000000000000000000 bitAt:1 => 0 |
19053 | 2674 |
|
2675 |
(1 bitShift:1000) bitAt:1000 => 0 |
|
2676 |
(1 bitShift:1000) bitAt:1001 => 1 |
|
2677 |
(1 bitShift:1000) bitAt:1002 => 0 |
|
2678 |
||
2679 |
(1 bitShift:30) bitAt:30 |
|
2680 |
(1 bitShift:30) bitAt:31 |
|
2681 |
(1 bitShift:30) bitAt:32 |
|
2682 |
(1 bitShift:31) bitAt:31 |
|
2683 |
(1 bitShift:31) bitAt:32 |
|
2684 |
(1 bitShift:31) bitAt:33 |
|
2685 |
(1 bitShift:32) bitAt:32 |
|
2686 |
(1 bitShift:32) bitAt:33 |
|
2687 |
(1 bitShift:32) bitAt:34 |
|
2688 |
(1 bitShift:64) bitAt:64 |
|
2689 |
(1 bitShift:64) bitAt:65 |
|
2690 |
(1 bitShift:64) bitAt:66 |
|
2691 |
" |
|
25215 | 2692 |
|
2693 |
"Modified (comment): / 29-01-2020 / 18:21:17 / Stefan Vogel" |
|
19053 | 2694 |
! |
2695 |
||
24344 | 2696 |
bitAt:anIntegerIndex put:aBit |
2697 |
"the name is a bit misleading: this returns a NEW integer with the corresponding |
|
2698 |
bit either set or cleared. |
|
2699 |
Indexing starts with 1" |
|
2700 |
||
2701 |
aBit == 0 ifTrue:[ |
|
2702 |
^ self clearBit:anIntegerIndex |
|
2703 |
]. |
|
2704 |
^ self setBit:anIntegerIndex |
|
2705 |
||
24345 | 2706 |
" |
2707 |
2r1100 bitAt:1 put:1 |
|
2708 |
2r1101 bitAt:1 put:0 |
|
2709 |
" |
|
2710 |
||
24344 | 2711 |
"Created: / 23-06-2019 / 21:55:15 / Claus Gittinger" |
2712 |
! |
|
2713 |
||
19053 | 2714 |
bitIndicesOfOneBitsDo:aBlock |
2715 |
"evaluate aBlock for all indices of a 1-bit, starting with the index of the lowest bit. |
|
2716 |
The index for the least significant bit is 1." |
|
2717 |
||
2718 |
1 to:self digitLength do:[:i8 | |
|
19078 | 2719 |
|byte| |
2720 |
||
2721 |
byte := self digitAt:i8. |
|
2722 |
byte ~~ 0 ifTrue:[ |
|
2723 |
1 to:8 do:[:i | |
|
2724 |
(byte bitAt:i) == 1 ifTrue:[ |
|
2725 |
aBlock value:(((i8-1)*8) + i). |
|
2726 |
]. |
|
2727 |
]. |
|
2728 |
] |
|
19053 | 2729 |
]. |
2730 |
||
2731 |
" |
|
2732 |
1 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i]. |
|
2733 |
2 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2734 |
4 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2735 |
12 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2736 |
127 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2737 |
" |
|
2738 |
! |
|
2739 |
||
2740 |
bitIndicesOfOneBitsReverseDo:aBlock |
|
2741 |
"evaluate aBlock for all indices of a 1-bit, starting with the index of the highest |
|
2742 |
and ending with the lowest bit. |
|
2743 |
The index for the least significant bit is 1." |
|
2744 |
||
2745 |
self digitLength downTo:1 do:[:i8 | |
|
19078 | 2746 |
|byte| |
2747 |
||
2748 |
byte := self digitAt:i8. |
|
2749 |
byte ~~ 0 ifTrue:[ |
|
2750 |
8 downTo:1 do:[:i | |
|
2751 |
(byte bitAt:i) == 1 ifTrue:[ |
|
2752 |
aBlock value:(((i8-1)*8) + i). |
|
2753 |
]. |
|
2754 |
]. |
|
2755 |
] |
|
19053 | 2756 |
]. |
2757 |
||
2758 |
" |
|
2759 |
1 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i]. |
|
2760 |
2 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2761 |
4 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2762 |
12 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2763 |
127 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2764 |
" |
|
2765 |
! |
|
2766 |
||
2767 |
changeBit:index to:aBooleanOrNumber |
|
2768 |
"return a new number where the specified bit is on or off, |
|
2769 |
depending on aBooleanOrNumber. |
|
2770 |
Bits are counted from 1 starting with the least significant. |
|
2771 |
The method's name may be misleading: the receiver is not changed, |
|
2772 |
but a new number is returned. Should be named #withBit:changedTo:" |
|
2773 |
||
2774 |
(aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[ |
|
19078 | 2775 |
^ self clearBit:index |
19053 | 2776 |
]. |
2777 |
^ self setBit:index |
|
2778 |
||
2779 |
" |
|
2780 |
(16r3fffffff changeBit:31 to:1) hexPrintString |
|
2781 |
(16r3fffffff asLargeInteger setBit:31) hexPrintString |
|
2782 |
" |
|
2783 |
! |
|
2784 |
||
2785 |
clearBit:index |
|
2786 |
"return a new integer where the specified bit is off. |
|
2787 |
Bits are counted from 1 starting with the least significant. |
|
2788 |
The method's name may be misleading: the receiver is not changed, |
|
2789 |
but a new number is returned. Should be named #withBitCleared:" |
|
2790 |
||
2791 |
|n "{ Class: SmallInteger }" |
|
2792 |
byteIndex "{ Class: SmallInteger }" |
|
2793 |
bitIndex "{ Class: SmallInteger }" |
|
2794 |
result byte| |
|
2795 |
||
2796 |
index <= 0 ifTrue:[ |
|
19078 | 2797 |
^ SubscriptOutOfBoundsSignal |
2798 |
raiseRequestWith:index |
|
2799 |
errorString:'bit index out of bounds' |
|
19053 | 2800 |
]. |
2801 |
byteIndex := ((index - 1) // 8) + 1. |
|
2802 |
n := self digitLength. |
|
2803 |
byteIndex > n ifTrue:[ |
|
19078 | 2804 |
^ self |
19053 | 2805 |
]. |
2806 |
||
2807 |
result := self simpleDeepCopy. |
|
2808 |
bitIndex := ((index - 1) \\ 8) + 1. |
|
2809 |
byte := (result digitAt:byteIndex) clearBit:bitIndex. |
|
2810 |
result digitAt:byteIndex put:byte. |
|
2811 |
(byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[ |
|
19078 | 2812 |
^ result compressed |
19053 | 2813 |
]. |
2814 |
^ result |
|
2815 |
||
2816 |
" |
|
2817 |
3111111111 clearBit:1 |
|
2818 |
" |
|
2819 |
||
2820 |
"Modified: / 28.7.1998 / 18:35:50 / cg" |
|
2821 |
! |
|
2822 |
||
2823 |
invertBit:index |
|
2824 |
"return a new number where the specified bit is inverted. |
|
2825 |
Bits are counted from 1 starting with the least significant. |
|
2826 |
The method's name may be misleading: the receiver is not changed, |
|
2827 |
but a new number is returned. Should be named #withBitInverted:" |
|
2828 |
||
2829 |
index <= 0 ifTrue:[ |
|
19078 | 2830 |
^ SubscriptOutOfBoundsSignal |
2831 |
raiseRequestWith:index |
|
2832 |
errorString:'index out of bounds' |
|
19053 | 2833 |
]. |
2834 |
^ self bitXor:(1 bitShift:index-1) |
|
2835 |
||
2836 |
" |
|
2837 |
0 invertBit:3 => 4 (2r100) |
|
2838 |
0 invertBit:48 => 140737488355328 (2r1000.....000) |
|
2839 |
((0 invertBit:99) invertBit:100) printStringRadix:2 |
|
2840 |
" |
|
2841 |
! |
|
2842 |
||
2843 |
isBitClear:index |
|
2844 |
"return true if the index' bit is clear; false otherwise. |
|
2845 |
Bits are counted from 1 starting with the least significant." |
|
2846 |
||
2847 |
^ (self bitAt:index) == 0 |
|
2848 |
||
2849 |
" |
|
2850 |
5 isBitClear:1 => false |
|
2851 |
5 isBitClear:2 => true |
|
2852 |
5 isBitClear:3 => false |
|
2853 |
5 isBitClear:4 => true |
|
2854 |
5 isBitClear:10000 => true |
|
2855 |
2r0101 isBitClear:2 => true |
|
2856 |
2r0101 isBitClear:1 => false |
|
2857 |
2r0101 isBitClear:0 index error |
|
2858 |
" |
|
2859 |
! |
|
2860 |
||
2861 |
isBitSet:index |
|
2862 |
"return true if the index' bit is set; false otherwise. |
|
2863 |
Bits are counted from 1 starting with the least significant." |
|
2864 |
||
2865 |
^ (self bitAt:index) ~~ 0 |
|
2866 |
||
2867 |
" |
|
2868 |
5 isBitSet:3 => true |
|
2869 |
2r0101 isBitSet:2 => false |
|
2870 |
2r0101 isBitSet:1 => true |
|
2871 |
2r0101 isBitSet:0 index error |
|
2872 |
" |
|
7155 | 2873 |
! |
2874 |
||
5209 | 2875 |
setBit:index |
8937 | 2876 |
"return a new integer, where the specified bit is on. |
5209 | 2877 |
Bits are counted from 1 starting with the least significant. |
14404
49a54ea47bf8
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14403
diff
changeset
|
2878 |
The method's name may be misleading: the receiver is not changed, |
11499 | 2879 |
but a new number is returned. Should be named #withBitSet:" |
5209 | 2880 |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
2881 |
index <= 0 ifTrue:[ |
19078 | 2882 |
^ SubscriptOutOfBoundsSignal |
2883 |
raiseRequestWith:index |
|
2884 |
errorString:'index out of bounds' |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
2885 |
]. |
5209 | 2886 |
^ self bitOr:(1 bitShift:index-1) |
2887 |
||
2888 |
" |
|
2889 |
0 setBit:3 => 4 (2r100) |
|
2890 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
|
11587 | 2891 |
((0 setBit:99) setBit:100) printStringRadix:2 |
5209 | 2892 |
" |
701 | 2893 |
! ! |
2894 |
||
3890 | 2895 |
!Integer methodsFor:'byte access'! |
2896 |
||
12784 | 2897 |
byteAt:anIndex |
2898 |
"compatibility with ByteArrays etc." |
|
2899 |
||
2900 |
^ self digitAt:anIndex |
|
2901 |
||
2902 |
" |
|
19078 | 2903 |
12345678 byteAt:2 |
2904 |
12345678 digitBytes at:2 |
|
2905 |
||
2906 |
-12345678 byteAt:2 |
|
2907 |
-12345678 digitBytes at:2 |
|
12784 | 2908 |
" |
2909 |
! |
|
2910 |
||
19130 | 2911 |
byteSwapped32 |
2912 |
"a fallback, in case unimplemented in concrete classes. |
|
2913 |
Not actually used" |
|
2914 |
||
2915 |
^ ((self digitAt:1) bitShift:24) |
|
2916 |
+ ((self digitAt:2) bitShift:16) |
|
2917 |
+ ((self digitAt:3) bitShift:8) |
|
2918 |
+ (self digitAt:4) |
|
2919 |
||
2920 |
" |
|
2921 |
16r12345678901234567890 byteSwapped32 |
|
2922 |
" |
|
2923 |
! |
|
2924 |
||
2925 |
byteSwapped64 |
|
2926 |
"a fallback, in case unimplemented in concrete classes. |
|
2927 |
Not actually used" |
|
2928 |
||
2929 |
^ ((self digitAt:1) bitShift:56) |
|
2930 |
+ ((self digitAt:2) bitShift:48) |
|
2931 |
+ ((self digitAt:3) bitShift:40) |
|
2932 |
+ ((self digitAt:4) bitShift:32) |
|
2933 |
+ ((self digitAt:5) bitShift:24) |
|
2934 |
+ ((self digitAt:6) bitShift:16) |
|
2935 |
+ ((self digitAt:7) bitShift:8) |
|
2936 |
+ (self digitAt:8) |
|
2937 |
||
2938 |
" |
|
2939 |
16r1234567890123456789 byteSwapped64 hexPrintString |
|
2940 |
" |
|
2941 |
! |
|
2942 |
||
3890 | 2943 |
digitByteLength |
19327 | 2944 |
<resource: #obsolete> |
19316 | 2945 |
"return the number bytes required for a 2's complement |
2946 |
binary representation of this Integer." |
|
2947 |
||
2948 |
self obsoleteMethodWarning:'use signedDigitLength - attention: return value fixed'. |
|
2949 |
^ self signedDigitLength |
|
2950 |
||
2951 |
" |
|
2952 |
-127 digitByteLength |
|
2953 |
-128 digitByteLength |
|
11587 | 2954 |
-129 digitByteLength |
19315 | 2955 |
-32769 digitByteLength |
2956 |
32768 digitByteLength |
|
3890 | 2957 |
" |
10488 | 2958 |
! |
2959 |
||
2960 |
digitBytes |
|
18851 | 2961 |
"return a byteArray filled with the receiver's bits |
10488 | 2962 |
(8 bits of the absolute value per element), |
2963 |
least significant byte is first" |
|
2964 |
||
2965 |
^ self subclassResponsibility |
|
2966 |
! |
|
2967 |
||
15567 | 2968 |
digitBytesMSB |
18851 | 2969 |
"return a byteArray filled with the receiver's bits |
15567 | 2970 |
(8 bits of the absolute value per element), |
2971 |
most significant byte is first" |
|
2972 |
||
2973 |
^ self subclassResponsibility |
|
2974 |
! |
|
2975 |
||
10488 | 2976 |
digitBytesMSB:msbFlag |
18851 | 2977 |
"return a byteArray filled with the receiver's bits |
10488 | 2978 |
(8 bits of the absolute value per element), |
2979 |
if msbflag = true, most significant byte is first, |
|
2980 |
otherwise least significant byte is first" |
|
2981 |
||
2982 |
msbFlag ifTrue:[ |
|
19078 | 2983 |
^ self digitBytesMSB. |
10488 | 2984 |
]. |
2985 |
^ self digitBytes |
|
2986 |
||
2987 |
" |
|
2988 |
16r12 digitBytesMSB:true |
|
2989 |
16r1234 digitBytesMSB:true |
|
2990 |
16r1234 digitBytesMSB:false |
|
2991 |
16r12345678 digitBytesMSB:true |
|
2992 |
16r12345678 digitBytesMSB:false |
|
2993 |
" |
|
14166 | 2994 |
! |
2995 |
||
19316 | 2996 |
signedDigitLength |
2997 |
"return the number bytes required for a 2's complement |
|
2998 |
binary representation of this Integer. |
|
2999 |
I.e. the number of bytes from which we have to sign extent the highest bit" |
|
3000 |
||
3001 |
|absLen "{ Class: SmallInteger }" | |
|
3002 |
||
3003 |
self >= 0 ifTrue:[ |
|
3004 |
absLen := self digitLength. |
|
3005 |
(self digitByteAt:absLen) >= 16r80 ifTrue:[ |
|
3006 |
^ absLen + 1. |
|
3007 |
]. |
|
3008 |
^ absLen. |
|
3009 |
]. |
|
3010 |
||
3011 |
absLen := self negated digitLength. |
|
3012 |
(self digitByteAt:absLen) < 16r80 ifTrue:[ |
|
3013 |
^ absLen + 1 |
|
3014 |
]. |
|
3015 |
^ absLen |
|
3016 |
||
3017 |
" |
|
3018 |
0 signedDigitLength |
|
3019 |
1 signedDigitLength |
|
3020 |
126 signedDigitLength |
|
3021 |
127 signedDigitLength |
|
3022 |
128 signedDigitLength |
|
3023 |
||
3024 |
255 signedDigitLength |
|
3025 |
256 signedDigitLength |
|
3026 |
257 signedDigitLength |
|
3027 |
||
3028 |
32767 signedDigitLength |
|
3029 |
32768 signedDigitLength |
|
3030 |
||
3031 |
-1 signedDigitLength |
|
3032 |
-127 signedDigitLength |
|
3033 |
-128 signedDigitLength |
|
3034 |
-129 signedDigitLength |
|
3035 |
||
3036 |
-32767 signedDigitLength |
|
3037 |
-32768 signedDigitLength |
|
3038 |
-32769 signedDigitLength |
|
3039 |
" |
|
3040 |
! |
|
3041 |
||
14166 | 3042 |
swapBytes |
16886 | 3043 |
"swap bytes pair-wise in a positive integer |
3044 |
i.e. a.b.c.d -> b.a.d.c |
|
3045 |
Swapping of negative integers is undefined and therefore not supported." |
|
16884 | 3046 |
|
3047 |
|digitBytes| |
|
3048 |
||
3049 |
self negative ifTrue:[ |
|
19078 | 3050 |
RangeError raiseWith:self errorString:'negative numbers are not supported in #swapBytes'. |
16884 | 3051 |
]. |
3052 |
digitBytes := self digitBytes. |
|
3053 |
digitBytes size odd ifTrue:[ |
|
19078 | 3054 |
"ByteArray<<#swapBytes needs even number of bytes. |
3055 |
Add 0 to the most significant position (the end)" |
|
3056 |
digitBytes := digitBytes copyWith:0. |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3057 |
|
16884 | 3058 |
]. |
3059 |
^ (LargeInteger digitBytes:digitBytes swapBytes) compressed |
|
14166 | 3060 |
|
3061 |
" |
|
19078 | 3062 |
16rFFEE2211 swapBytes hexPrintString |
3063 |
16rFFEEAA2211 swapBytes hexPrintString |
|
3064 |
16r2211 swapBytes hexPrintString |
|
3065 |
16rFF3FFFFF swapBytes |
|
3066 |
self assert:(SmallInteger maxVal swapBytes swapBytes == SmallInteger maxVal) |
|
14166 | 3067 |
" |
3890 | 3068 |
! ! |
3069 |
||
5238 | 3070 |
!Integer methodsFor:'coercing & converting'! |
701 | 3071 |
|
1888 | 3072 |
asFixedPoint |
3073 |
"return the receiver as a fixedPoint number" |
|
3074 |
||
6640
2de8925ad2cd
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6639
diff
changeset
|
3075 |
^ FixedPoint basicNew |
19078 | 3076 |
setNumerator:self denominator:1 scale:1 |
1888 | 3077 |
|
3078 |
" |
|
11587 | 3079 |
100 asFixedPoint |
3080 |
100 asFixedPoint + 0.1 asFixedPoint |
|
1888 | 3081 |
" |
3082 |
||
3083 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
3084 |
! |
|
3085 |
||
2141 | 3086 |
asFixedPoint:scale |
3087 |
"return the receiver as fixedPoint number, with the given number |
|
3088 |
of post-decimal-point digits." |
|
3089 |
||
6639
d88a51b081ae
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6635
diff
changeset
|
3090 |
^ FixedPoint basicNew |
19078 | 3091 |
setNumerator:self denominator:1 scale:scale |
2141 | 3092 |
|
3093 |
" |
|
11587 | 3094 |
100 asFixedPoint:2 |
3095 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
2141 | 3096 |
" |
3097 |
||
3098 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
3099 |
! |
|
3100 |
||
7444 | 3101 |
asFloat |
3102 |
"return a Float with same value as myself. |
|
11240 | 3103 |
Since floats have a limited precision, you usually loose bits when doing this." |
7444 | 3104 |
|
3105 |
^ Float fromInteger:self |
|
3106 |
||
3107 |
" |
|
11587 | 3108 |
1234567890 asFloat |
3109 |
1234567890 asFloat asInteger |
|
3110 |
12345678901234567890 asFloat |
|
3111 |
12345678901234567890 asFloat asInteger |
|
7444 | 3112 |
" |
3113 |
! |
|
3114 |
||
701 | 3115 |
asFraction |
3116 |
"return a Fraction with same value as receiver" |
|
3117 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
3118 |
^ Fraction basicNew setNumerator:self denominator:1 |
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
3119 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
3120 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 3121 |
! |
3122 |
||
701 | 3123 |
asInteger |
11587 | 3124 |
"return the receiver truncated towards zero - |
1 | 3125 |
for integers this is self" |
3126 |
||
3127 |
^ self |
|
1337 | 3128 |
! |
3129 |
||
7444 | 3130 |
asLargeFloat |
3131 |
"return a LargeFloat with same value as myself. |
|
23053 | 3132 |
If the largeFloat class is not present, a regular float is returned" |
3133 |
||
23055 | 3134 |
^ (LargeFloat ? LongFloat ? Float) fromInteger:self |
7444 | 3135 |
|
3136 |
" |
|
11587 | 3137 |
1234567890 asLargeFloat |
3138 |
1234567890 asLargeFloat asInteger |
|
3139 |
12345678901234567890 asLargeFloat |
|
3140 |
12345678901234567890 asLargeFloat asInteger |
|
7444 | 3141 |
" |
23053 | 3142 |
|
23055 | 3143 |
"Modified: / 07-06-2018 / 16:28:16 / Claus Gittinger" |
7444 | 3144 |
! |
3145 |
||
24153 | 3146 |
asLargeFloatPrecision:n |
3147 |
"return a LargeFloat with same value as myself. |
|
3148 |
If the largeFloat class is not present, a regular float is returned" |
|
3149 |
||
3150 |
LargeFloat isNil ifTrue:[^ (LongFloat ? Float) fromInteger:self]. |
|
3151 |
^ LargeFloat fromInteger:self precision:n |
|
3152 |
||
3153 |
" |
|
3154 |
1234567890 asLargeFloatPrecision:10 |
|
3155 |
12345678901234567890 asLargeFloatPrecision:10 |
|
3156 |
" |
|
3157 |
||
3158 |
"Created: / 26-05-2019 / 03:56:50 / Claus Gittinger" |
|
3159 |
! |
|
3160 |
||
7444 | 3161 |
asLongFloat |
3162 |
"return a LongFloat with same value as myself. |
|
3163 |
Since longFloats have a limited precision, you usually loose bits when |
|
3164 |
doing this." |
|
3165 |
||
3166 |
^ LongFloat fromInteger:self |
|
3167 |
||
3168 |
" |
|
11587 | 3169 |
1234567890 asFloat |
3170 |
1234567890 asFloat asInteger |
|
3171 |
12345678901234567890 asFloat |
|
3172 |
12345678901234567890 asFloat asInteger |
|
7444 | 3173 |
" |
3174 |
! |
|
3175 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3176 |
asModuloNumber |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3177 |
"return a precomputed modulo number" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3178 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3179 |
^ ModuloNumber modulus:self. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3180 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3181 |
"Created: / 3.5.1999 / 14:48:03 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3182 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3183 |
|
7444 | 3184 |
asShortFloat |
3185 |
"return a ShortFloat with same value as receiver" |
|
3186 |
||
3187 |
^ ShortFloat fromInteger:self |
|
3188 |
! |
|
3189 |
||
21822 | 3190 |
coerce:aNumber |
3191 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
3192 |
||
3193 |
^ aNumber asInteger |
|
3194 |
! |
|
3195 |
||
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3196 |
signExtended24BitValue |
19257 | 3197 |
"return an integer from sign-extending the 24'th bit. |
19062 | 3198 |
i.e. interprets the lowest 24 bits as a signed integer, |
3199 |
ignoring higher bits. |
|
3200 |
This may be useful for communication interfaces" |
|
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3201 |
|
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3202 |
^ (self bitAnd:16rFFFFFF) signExtended24BitValue |
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3203 |
|
19061 | 3204 |
" |
3205 |
16r800000 signExtended24BitValue |
|
3206 |
16r7FFFFF signExtended24BitValue |
|
3207 |
16rFFFFFF signExtended24BitValue |
|
3208 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3209 |
|
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3210 |
"Modified: / 07-05-1996 / 09:31:57 / cg" |
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3211 |
"Created: / 05-03-2012 / 14:37:55 / cg" |
19257 | 3212 |
"Modified (comment): / 26-02-2016 / 19:39:52 / cg" |
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3213 |
! |
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
3214 |
|
1337 | 3215 |
signExtendedByteValue |
19257 | 3216 |
"return an integer from sign-extending the 8'th bit. |
19062 | 3217 |
i.e. interprets the lowest 8 bits as a signed integer, |
3218 |
ignoring higher bits. |
|
3219 |
This may be useful for communication interfaces" |
|
1337 | 3220 |
|
3221 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
3222 |
||
19061 | 3223 |
" |
3224 |
16r80 signExtendedByteValue |
|
3225 |
16r7F signExtendedByteValue |
|
3226 |
16rFF signExtendedByteValue |
|
3227 |
" |
|
3228 |
||
19257 | 3229 |
"Created: / 07-05-1996 / 09:31:52 / cg" |
3230 |
"Modified (comment): / 26-02-2016 / 19:39:46 / cg" |
|
1337 | 3231 |
! |
3232 |
||
19062 | 3233 |
signExtendedFromBit:bitNr |
19257 | 3234 |
"return an integer from sign-extending the n'th bit. |
19062 | 3235 |
i.e. interprets the lowest n bits as a signed integer, |
3236 |
ignoring higher bits. |
|
19257 | 3237 |
The bit numbering is 1-based (i.e. the lowest bit has bitNr 1) |
22073 | 3238 |
This may be useful for communication interfaces. |
3239 |
(kind of the reverse operation to asUnsigned:)." |
|
19062 | 3240 |
|
22070 | 3241 |
^ self signExtendedFromMaskBit:(1 bitShift:bitNr-1) |
3242 |
||
3243 |
" |
|
3244 |
2r111000111 signExtendedFromBit:3 -> 2r11111....111 -> -1 |
|
3245 |
2r111000110 signExtendedFromBit:3 -> 2r11111....110 -> -2 |
|
3246 |
2r111000101 signExtendedFromBit:3 -> 2r11111....101 -> -3 |
|
3247 |
2r111000100 signExtendedFromBit:3 -> 2r11111....100 -> -4 |
|
3248 |
2r111000000 signExtendedFromBit:3 -> 2r00000....000 -> 0 |
|
3249 |
2r111000011 signExtendedFromBit:3 -> 2r00000....011 -> 3 |
|
3250 |
||
3251 |
16r800008 signExtendedFromBit:4 -> -8 |
|
3252 |
16r7FFF07 signExtendedFromBit:4 -> 7 |
|
3253 |
16r7FFF0F signExtendedFromBit:4 -> -1 |
|
3254 |
||
3255 |
16rFFFFFF signExtendedFromBit:8 -> -1 |
|
3256 |
16rFFFF7F signExtendedFromBit:8 -> 127 |
|
3257 |
16rFFFF80 signExtendedFromBit:8 -> -128 |
|
3258 |
" |
|
3259 |
||
22073 | 3260 |
"Modified (comment): / 21-07-2017 / 15:13:55 / cg" |
22070 | 3261 |
! |
3262 |
||
3263 |
signExtendedFromMaskBit:highBitMask |
|
3264 |
"return an integer from sign-extending the bit defined by highMaskBit, |
|
3265 |
which MUST be a single bit (otherwise, you'll get garbage). |
|
3266 |
i.e. interprets the lowest n bits as a signed integer, |
|
3267 |
ignoring higher bits. |
|
3268 |
This may be useful for communication interfaces and to expand bitfields into |
|
3269 |
signed values" |
|
3270 |
||
19062 | 3271 |
|masked| |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3272 |
|
22070 | 3273 |
masked := self bitAnd:(highBitMask-1). |
3274 |
(self bitTest:highBitMask) ifTrue:[ |
|
3275 |
^ masked - highBitMask |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3276 |
]. |
19062 | 3277 |
^ masked |
3278 |
||
3279 |
" |
|
22070 | 3280 |
2r111000111 signExtendedFromMaskBit:2r100 -> 2r11111....111 -> -1 |
3281 |
2r111000110 signExtendedFromMaskBit:2r100 -> 2r11111....110 -> -2 |
|
3282 |
2r111000101 signExtendedFromMaskBit:2r100 -> 2r11111....101 -> -3 |
|
3283 |
2r111000100 signExtendedFromMaskBit:2r100 -> 2r11111....100 -> -4 |
|
3284 |
2r111000000 signExtendedFromMaskBit:2r100 -> 2r00000....000 -> 0 |
|
3285 |
2r111000011 signExtendedFromMaskBit:2r100 -> 2r00000....011 -> 3 |
|
3286 |
||
3287 |
16r800008 signExtendedFromMaskBit:2r1000 -> -8 |
|
3288 |
16r7FFF07 signExtendedFromMaskBit:2r1000 -> 7 |
|
3289 |
16r7FFF0F signExtendedFromMaskBit:2r1000 -> -1 |
|
3290 |
||
3291 |
16rFFFFFF signExtendedFromMaskBit:2r10000000 -> -1 |
|
3292 |
16rFFFF7F signExtendedFromMaskBit:2r10000000 -> 127 |
|
3293 |
16rFFFF80 signExtendedFromMaskBit:2r10000000 -> -128 |
|
3294 |
" |
|
3295 |
||
3296 |
"Created: / 21-07-2017 / 14:52:12 / cg" |
|
19062 | 3297 |
! |
3298 |
||
19061 | 3299 |
signExtendedLongLongValue |
19257 | 3300 |
"return an integer from sign-extending the 64'th bit. |
19062 | 3301 |
i.e. interprets the lowest 64 bits as a signed integer, |
3302 |
ignoring higher bits. |
|
3303 |
This may be useful for communication interfaces" |
|
19061 | 3304 |
|
3305 |
(self bitTest:16r8000000000000000) ifTrue:[ |
|
19078 | 3306 |
^ (self bitAnd:16rFFFFFFFFFFFFFFFF)-16r10000000000000000 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3307 |
]. |
19061 | 3308 |
^ (self bitAnd:16r7FFFFFFFFFFFFFFF) |
3309 |
||
3310 |
" |
|
19062 | 3311 |
16r1238000000000000000 signExtendedLongLongValue |
3312 |
16r1237FFFFFFFFFFFFFFF signExtendedLongLongValue |
|
3313 |
16r123FFFFFFFFFFFFFFFF signExtendedLongLongValue |
|
19061 | 3314 |
" |
19257 | 3315 |
|
3316 |
"Modified (comment): / 26-02-2016 / 19:38:55 / cg" |
|
19061 | 3317 |
! |
3318 |
||
15084 | 3319 |
signExtendedLongValue |
19257 | 3320 |
"return an integer from sign-extending the 32'th bit. |
19062 | 3321 |
i.e. interprets the lowest 32 bits as a signed integer, |
3322 |
ignoring higher bits. |
|
3323 |
This may be useful for communication interfaces" |
|
15084 | 3324 |
|
19061 | 3325 |
(self bitTest:16r80000000) ifTrue:[ |
19078 | 3326 |
^ (self bitAnd:16rFFFFFFFF) - 16r100000000 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3327 |
]. |
19061 | 3328 |
^ (self bitAnd:16r7FFFFFFF) |
3329 |
||
3330 |
" |
|
3331 |
16r80000000 signExtendedLongValue |
|
3332 |
16r7FFFFFFF signExtendedLongValue |
|
3333 |
16rFFFFFFFF signExtendedLongValue |
|
3334 |
" |
|
19257 | 3335 |
|
3336 |
"Modified (comment): / 26-02-2016 / 19:39:09 / cg" |
|
15084 | 3337 |
! |
3338 |
||
1337 | 3339 |
signExtendedShortValue |
19257 | 3340 |
"return an integer from sign-extending the 16'th bit. |
19062 | 3341 |
i.e. interprets the lowest 16 bits as a signed integer, |
3342 |
ignoring higher bits. |
|
3343 |
This may be useful for communication interfaces" |
|
1337 | 3344 |
|
3345 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
3346 |
||
19061 | 3347 |
" |
3348 |
16r8000 signExtendedShortValue |
|
3349 |
16r7FFF signExtendedShortValue |
|
3350 |
16rFFFF signExtendedShortValue |
|
19062 | 3351 |
|
3352 |
16r1238000 signExtendedShortValue |
|
3353 |
16r1237FFF signExtendedShortValue |
|
3354 |
16r123FFFF signExtendedShortValue |
|
19061 | 3355 |
" |
3356 |
||
19257 | 3357 |
"Modified: / 07-05-1996 / 09:31:57 / cg" |
3358 |
"Modified (comment): / 26-02-2016 / 19:39:37 / cg" |
|
24244 | 3359 |
! |
3360 |
||
3361 |
zigZagDecodedValue |
|
3362 |
"zigzag encoding maps values with small absolute values |
|
3363 |
into relatively small unsigned integer numbers in the same range. |
|
3364 |
i.e. [minInt .. maxInt] is mapped into [0 .. maxUINT], |
|
24247 | 3365 |
where small magnitudes generate small encodings. |
3366 |
Zigzag is used eg. by google's protocol buffer encoding" |
|
24244 | 3367 |
|
3368 |
|t| |
|
3369 |
||
3370 |
t := self bitShift:-1. |
|
3371 |
(self odd) ifTrue:[^ t negated - 1]. |
|
3372 |
^ t |
|
3373 |
||
3374 |
" |
|
3375 |
0 zigZagEncoded64BitValue zigZagDecodedValue -> 0 |
|
3376 |
-1 zigZagEncoded64BitValue zigZagDecodedValue -> -1 |
|
3377 |
1 zigZagEncoded64BitValue zigZagDecodedValue -> 1 |
|
3378 |
-2 zigZagEncoded64BitValue zigZagDecodedValue -> -2 |
|
3379 |
2 zigZagEncoded64BitValue zigZagDecodedValue -> 2 |
|
3380 |
-2147483647 zigZagEncoded64BitValue zigZagDecodedValue -> -2147483647 |
|
3381 |
2147483647 zigZagEncoded64BitValue zigZagDecodedValue -> 2147483647 |
|
3382 |
-2147483648 zigZagEncoded64BitValue zigZagDecodedValue -> -2147483648 |
|
3383 |
2147483648 zigZagEncoded64BitValue zigZagDecodedValue -> 2147483648 |
|
3384 |
||
3385 |
-4611686018427387903 zigZagEncoded64BitValue zigZagDecodedValue -> -4611686018427387903 |
|
3386 |
4611686018427387903 zigZagEncoded64BitValue zigZagDecodedValue -> 4611686018427387903 |
|
3387 |
||
3388 |
-4611686018427387904 zigZagEncoded64BitValue zigZagDecodedValue -> -4611686018427387904 |
|
3389 |
4611686018427387904 zigZagEncoded64BitValue zigZagDecodedValue -> 4611686018427387904 |
|
3390 |
||
3391 |
-4611686018427387905 zigZagEncoded64BitValue zigZagDecodedValue -> -4611686018427387905 |
|
3392 |
4611686018427387905 zigZagEncoded64BitValue zigZagDecodedValue -> 4611686018427387905 |
|
3393 |
||
3394 |
-9223372036854775807 zigZagEncoded64BitValue zigZagDecodedValue -> -9223372036854775807 |
|
3395 |
9223372036854775807 zigZagEncoded64BitValue zigZagDecodedValue -> 9223372036854775807 |
|
3396 |
||
3397 |
-9223372036854775808 zigZagEncoded64BitValue zigZagDecodedValue -> -9223372036854775808 |
|
3398 |
||
3399 |
-- out of range |
|
3400 |
9223372036854775808 zigZagEncoded64BitValue zigZagDecodedValue |
|
3401 |
" |
|
3402 |
||
3403 |
"Created: / 04-06-2019 / 00:48:47 / Claus Gittinger" |
|
3404 |
! |
|
3405 |
||
3406 |
zigZagEncoded32BitValue |
|
3407 |
"zigzag encoding maps values with small absolute values |
|
3408 |
into relatively small unsigned integer numbers in the same range. |
|
3409 |
i.e. [minInt .. maxInt] is mapped into [0 .. maxUINT], |
|
24247 | 3410 |
where small magnitudes generate small encodings. |
3411 |
Zigzag is used eg. by google's protocol buffer encoding" |
|
24244 | 3412 |
|
3413 |
|t| |
|
3414 |
||
3415 |
t := self bitShift:1. |
|
3416 |
self negative ifTrue:[t := t negated - 1]. |
|
3417 |
^ t bitAnd:16rFFFFFFFF |
|
3418 |
||
3419 |
" |
|
3420 |
0 zigZagEncoded32BitValue -> 0 |
|
3421 |
-1 zigZagEncoded32BitValue -> 1 |
|
3422 |
1 zigZagEncoded32BitValue -> 2 |
|
3423 |
-2 zigZagEncoded32BitValue -> 3 |
|
3424 |
2 zigZagEncoded32BitValue -> 4 |
|
3425 |
-2147483647 zigZagEncoded32BitValue -> 4294967293 |
|
3426 |
16r7FFFFFFF zigZagEncoded32BitValue -> 4294967294 |
|
3427 |
16r-80000000 zigZagEncoded32BitValue -> 4294967295 |
|
3428 |
||
3429 |
-- out of range: |
|
3430 |
2147483648 zigZagEncoded32BitValue -> 4294967296 |
|
3431 |
" |
|
3432 |
||
3433 |
"Created: / 03-06-2019 / 17:37:53 / Claus Gittinger" |
|
3434 |
"Modified: / 03-06-2019 / 19:13:55 / Claus Gittinger" |
|
24247 | 3435 |
"Modified (comment): / 04-06-2019 / 01:11:25 / Claus Gittinger" |
24244 | 3436 |
! |
3437 |
||
3438 |
zigZagEncoded64BitValue |
|
3439 |
"zigzag encoding maps values with small absolute values |
|
3440 |
into relatively small unsigned integer numbers in the same range. |
|
3441 |
i.e. [minInt .. maxInt] is mapped into [0 .. maxUINT], |
|
24247 | 3442 |
where small magnitudes generate small encodings. |
3443 |
Zigzag is used eg. by google's protocol buffer encoding" |
|
24244 | 3444 |
|
3445 |
|t| |
|
3446 |
||
3447 |
t := self bitShift:1. |
|
3448 |
self negative ifTrue:[t := t negated - 1]. |
|
3449 |
^ t bitAnd:16rFFFFFFFFFFFFFFFF |
|
3450 |
||
3451 |
" |
|
3452 |
0 zigZagEncoded64BitValue -> 0 |
|
3453 |
-1 zigZagEncoded64BitValue -> 1 |
|
3454 |
1 zigZagEncoded64BitValue -> 2 |
|
3455 |
-2 zigZagEncoded64BitValue -> 3 |
|
3456 |
2 zigZagEncoded64BitValue -> 4 |
|
3457 |
-2147483647 zigZagEncoded64BitValue -> 4294967293 |
|
3458 |
2147483647 zigZagEncoded64BitValue -> 4294967294 |
|
3459 |
-2147483648 zigZagEncoded64BitValue -> 4294967295 |
|
3460 |
2147483648 zigZagEncoded64BitValue -> 4294967296 |
|
3461 |
||
3462 |
-16r3FFFFFFFFFFFFFFF zigZagEncoded64BitValue 9223372036854775805 |
|
3463 |
16r3FFFFFFFFFFFFFFF zigZagEncoded64BitValue 9223372036854775806 |
|
3464 |
||
3465 |
-16r4000000000000000 zigZagEncoded64BitValue 9223372036854775807 |
|
3466 |
16r4000000000000000 zigZagEncoded64BitValue 9223372036854775808 |
|
3467 |
||
3468 |
-16r4000000000000001 zigZagEncoded64BitValue 9223372036854775809 |
|
3469 |
16r4000000000000001 zigZagEncoded64BitValue 9223372036854775810 |
|
3470 |
||
3471 |
-16r7FFFFFFFFFFFFFFF zigZagEncoded64BitValue 18446744073709551613 |
|
3472 |
16r7FFFFFFFFFFFFFFF zigZagEncoded64BitValue 18446744073709551614 |
|
3473 |
||
3474 |
-16r8000000000000000 zigZagEncoded64BitValue 18446744073709551615 |
|
3475 |
||
3476 |
-- out of range |
|
3477 |
16r8000000000000000 zigZagEncoded64BitValue |
|
3478 |
" |
|
3479 |
||
3480 |
"Created: / 03-06-2019 / 17:42:02 / Claus Gittinger" |
|
3481 |
"Modified: / 03-06-2019 / 19:12:37 / Claus Gittinger" |
|
24247 | 3482 |
"Modified (comment): / 04-06-2019 / 01:11:32 / Claus Gittinger" |
1 | 3483 |
! ! |
3484 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3485 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3486 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3487 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3488 |
"redefined to return smallInteger hashValues" |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3489 |
|
19553 | 3490 |
^ self bitAnd:SmallInteger maxVal. |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3491 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3492 |
" |
19078 | 3493 |
-20000000000000 hash |
3494 |
20000000000000 hash |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3495 |
" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3496 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3497 |
"Created: / 14.11.1996 / 12:12:27 / cg" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3498 |
"Modified: / 24.2.1998 / 10:07:29 / stefan" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3499 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3500 |
|
16293 | 3501 |
|
12978 | 3502 |
!Integer methodsFor:'dependents access'! |
3503 |
||
3504 |
addDependent:anObject |
|
23556 | 3505 |
"It doesn't make sense to add dependents to a shared instance. |
3506 |
Silently ignore ..." |
|
3507 |
||
3508 |
"/ Transcript show:'*** trying to make dependent on an integer: '. |
|
3509 |
"/ thisContext sender printOn:Transcript. Transcript cr. |
|
12978 | 3510 |
|
3511 |
"Created: / 28-07-2010 / 20:29:00 / cg" |
|
23556 | 3512 |
"Modified: / 03-12-2018 / 17:48:53 / Stefan Vogel" |
3513 |
! |
|
3514 |
||
3515 |
onChangeSend:selector to:someOne |
|
3516 |
"It doesn't make sense to add dependents to a shared instance. |
|
3517 |
Silently ignore ..." |
|
3518 |
||
3519 |
"/ Transcript show:'*** trying to make dependent on an integer: '. |
|
3520 |
"/ thisContext sender printOn:Transcript. Transcript cr. |
|
3521 |
||
3522 |
"Created: / 30-11-2018 / 18:02:50 / Stefan Vogel" |
|
3523 |
"Modified (comment): / 03-12-2018 / 17:49:05 / Stefan Vogel" |
|
12978 | 3524 |
! ! |
3525 |
||
701 | 3526 |
!Integer methodsFor:'double dispatching'! |
3527 |
||
3528 |
differenceFromFraction:aFraction |
|
3529 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 3530 |
|
701 | 3531 |
|d| |
1 | 3532 |
|
701 | 3533 |
d := aFraction denominator. |
7356 | 3534 |
^ aFraction class |
19078 | 3535 |
numerator:(aFraction numerator - (self * d)) |
3536 |
denominator:d |
|
2793 | 3537 |
|
3538 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 3539 |
! |
3540 |
||
11517 | 3541 |
differenceFromTimestamp:aTimestamp |
3542 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
3543 |
before aTimestamp" |
|
3544 |
||
3545 |
^ aTimestamp subtractSeconds:self. |
|
3546 |
||
3547 |
" |
|
11587 | 3548 |
Timestamp now subtractSeconds:100 |
3549 |
100 differenceFromTimestamp:Timestamp now |
|
11517 | 3550 |
" |
3551 |
! |
|
3552 |
||
7356 | 3553 |
equalFromFraction:aFraction |
3554 |
"that should never be invoked, as fractions are always normalized to integers |
|
3555 |
if resulting from an arithmetic operation. |
|
3556 |
However, this implementation is for subclasses (i.e. fixed point) and also |
|
3557 |
allows comparing unnormalized fractions as might appear within the fraction class" |
|
3558 |
||
3559 |
|denominator numerator| |
|
3560 |
||
3561 |
denominator := aFraction denominator. |
|
3562 |
numerator := aFraction numerator. |
|
3563 |
(denominator == 1) ifFalse:[ |
|
19078 | 3564 |
^ numerator = (self * denominator) |
7356 | 3565 |
]. |
3566 |
^ numerator = self |
|
3567 |
! |
|
3568 |
||
701 | 3569 |
productFromFraction:aFraction |
3570 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 3571 |
|
7356 | 3572 |
^ aFraction class |
19078 | 3573 |
numerator:(self * aFraction numerator) |
3574 |
denominator:aFraction denominator |
|
2793 | 3575 |
|
3576 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 3577 |
! |
3578 |
||
701 | 3579 |
quotientFromFraction:aFraction |
7444 | 3580 |
"Return the quotient of the argument, aFraction and the receiver. |
3581 |
Sent when aFraction does not know how to divide by the receiver." |
|
701 | 3582 |
|
7356 | 3583 |
^ aFraction class |
19078 | 3584 |
numerator:aFraction numerator |
3585 |
denominator:(self * aFraction denominator) |
|
2793 | 3586 |
|
3587 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 3588 |
! |
1 | 3589 |
|
701 | 3590 |
sumFromFraction:aFraction |
3591 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
3592 |
||
3593 |
|d| |
|
3594 |
||
3595 |
d := aFraction denominator. |
|
7356 | 3596 |
^ aFraction class |
19078 | 3597 |
numerator:(aFraction numerator + (self * d)) |
3598 |
denominator:d |
|
2793 | 3599 |
|
3600 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
10681 | 3601 |
! |
3602 |
||
3603 |
sumFromTimestamp:aTimestamp |
|
3604 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
3605 |
after aTimestamp" |
|
3606 |
||
3607 |
^ aTimestamp addSeconds:self. |
|
3608 |
||
3609 |
" |
|
3610 |
Timestamp now addSeconds:100 |
|
3611 |
100 sumFromTimestamp:Timestamp now |
|
3612 |
" |
|
1 | 3613 |
! ! |
3614 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3615 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3616 |
|
11587 | 3617 |
gcd_helper:anInteger |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3618 |
"a helper for the greatest common divisor of the receiver and anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3619 |
Knuth's algorithm for large positive integers, with receiver being |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3620 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3621 |
|
19251 | 3622 |
| a b aLowBit bLowBit shift t | |
11496 | 3623 |
|
3624 |
a := self. |
|
3625 |
b := anInteger. |
|
3626 |
||
19251 | 3627 |
aLowBit := a lowBit - 1. |
3628 |
bLowBit := b lowBit - 1. |
|
3629 |
shift := aLowBit min:bLowBit. |
|
22213 | 3630 |
b := b rightShift:bLowBit. |
11496 | 3631 |
[a = 0] whileFalse:[ |
22213 | 3632 |
a := a rightShift:aLowBit. |
19078 | 3633 |
a < b ifTrue:[ |
3634 |
t := a. a := b. b := t |
|
3635 |
]. |
|
3636 |
a := a - b. |
|
19251 | 3637 |
aLowBit := a lowBit - 1. |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3638 |
]. |
11496 | 3639 |
^ b bitShift:shift |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3640 |
|
22213 | 3641 |
"Created: / 01-03-1997 / 16:38:17 / cg" |
3642 |
"Modified: / 25-08-2017 / 12:33:09 / cg" |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3643 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3644 |
|
10027 | 3645 |
|
12565 | 3646 |
!Integer methodsFor:'iteration'! |
3647 |
||
23594 | 3648 |
timesCollect:aBlock |
3649 |
"syntactic sugar; same as (1 to:self) collect:aBlock" |
|
3650 |
||
3651 |
^ (1 to:self) collect:aBlock |
|
3652 |
||
3653 |
" |
|
3654 |
10 timesCollect:[:i | i squared] |
|
3655 |
" |
|
3656 |
||
3657 |
"Created: / 09-01-2019 / 17:35:14 / Claus Gittinger" |
|
3658 |
! |
|
3659 |
||
12565 | 3660 |
to:stop collect:aBlock |
16347 | 3661 |
"syntactic sugar; same as (self to:stop) collect:aBlock" |
12565 | 3662 |
|
23594 | 3663 |
^ (self to:stop) collect:aBlock as:Array |
12565 | 3664 |
|
3665 |
" |
|
3666 |
1 to:10 collect:[:i | i squared] |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3667 |
10 to:20 collect:[:i | i squared] |
12565 | 3668 |
(10 to:20) collect:[:i | i squared] |
3669 |
" |
|
23594 | 3670 |
|
3671 |
"Modified (format): / 09-01-2019 / 17:51:04 / Claus Gittinger" |
|
3672 |
! |
|
3673 |
||
3674 |
to:stop collect:aBlock as:collectionClass |
|
3675 |
"syntactic sugar; same as (self to:stop) collect:aBlock" |
|
3676 |
||
3677 |
^ (self to:stop) collect:aBlock as:collectionClass |
|
3678 |
||
3679 |
" |
|
3680 |
1 to:10 collect:[:i | i squared] as:Set |
|
3681 |
" |
|
3682 |
||
3683 |
"Created: / 09-01-2019 / 17:51:17 / Claus Gittinger" |
|
12565 | 3684 |
! ! |
3685 |
||
1 | 3686 |
!Integer methodsFor:'misc math'! |
3687 |
||
701 | 3688 |
acker:n |
1560 | 3689 |
"return the value of acker(self, n). |
18851 | 3690 |
;-) Do not try with receivers > 3" |
701 | 3691 |
|
3692 |
(self == 0) ifTrue:[^ n + 1]. |
|
3693 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
3694 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
3695 |
||
3696 |
" |
|
11587 | 3697 |
3 acker:2 |
3698 |
3 acker:7 |
|
701 | 3699 |
" |
1560 | 3700 |
|
3701 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 3702 |
! |
3703 |
||
11491 | 3704 |
binco:kIn |
13023 | 3705 |
"an alternative name for the binomial coefficient for squeak compatibility" |
3706 |
||
11491 | 3707 |
^ self binomialCoefficient:kIn |
13023 | 3708 |
|
3709 |
"Modified: / 17-08-2010 / 17:29:07 / cg" |
|
11491 | 3710 |
! |
3711 |
||
19040 | 3712 |
binomialCoefficient:k |
3713 |
"The binomial coefficient (n over k) |
|
11486 | 3714 |
|
11476 | 3715 |
/ n \ with self being n, and 0 <= k <= n. |
3716 |
\ k / |
|
3717 |
||
11587 | 3718 |
is the number of ways of picking k unordered outcomes from n possibilities, |
3719 |
also known as a combination or combinatorial number. |
|
11486 | 3720 |
Sometimes also called C(n,k) (for choose k from n) |
3721 |
||
3722 |
binCo is defined as: |
|
19078 | 3723 |
n!! |
11476 | 3724 |
---------- |
3725 |
k!! (n-k)!! |
|
3726 |
||
3727 |
but there is a faster, recursive formula: |
|
3728 |
||
11587 | 3729 |
/ n \ = / n - 1 \ + / n - 1 \ |
11476 | 3730 |
\ k / \ k - 1 / \ k / |
3731 |
||
3732 |
with: |
|
3733 |
||
11587 | 3734 |
/ n \ = / n \ = 1 |
3735 |
\ 0 / \ n / |
|
11476 | 3736 |
" |
3737 |
||
19040 | 3738 |
|kRun acc| |
3739 |
||
3740 |
k > self ifTrue:[^ 0]. |
|
3741 |
||
3742 |
kRun := k. |
|
3743 |
kRun > (self / 2) ifTrue:[ |
|
19078 | 3744 |
"/ symmetry |
3745 |
kRun := self - kRun. |
|
11476 | 3746 |
]. |
3747 |
||
3748 |
acc := 1. |
|
19040 | 3749 |
1 to:kRun do:[:i | |
19078 | 3750 |
acc := acc * (self - kRun + i) / i. |
11476 | 3751 |
]. |
3752 |
^ acc |
|
3753 |
||
3754 |
" |
|
11587 | 3755 |
(7 binomialCoefficient:3) |
3756 |
(10 binomialCoefficient:5) |
|
3757 |
(100 binomialCoefficient:5) |
|
3758 |
(1000 binomialCoefficient:5) |
|
24697 | 3759 |
(100000 binomialCoefficient:100000) |
3760 |
(0 binomialCoefficient:0) |
|
11486 | 3761 |
|
11476 | 3762 |
TestCase assert: (10 binomialCoefficient:5) = (10 factorial / (5 factorial * 5 factorial)) |
11587 | 3763 |
TestCase assert: (100 binomialCoefficient:78) = (100 factorial / (78 factorial * (100-78) factorial)) |
3764 |
TestCase assert: (1000 binomialCoefficient:5) = (1000 factorial / (5 factorial * (1000-5) factorial)) |
|
3765 |
TestCase assert: (10000 binomialCoefficient:78) = (10000 factorial / (78 factorial * (10000-78) factorial)) |
|
11486 | 3766 |
|
3767 |
Time millisecondsToRun:[ (10000 binomialCoefficient:78) ] -> 0 |
|
19252 | 3768 |
Time millisecondsToRun:[ (10000 factorial / (78 factorial * (10000-78) factorial)) ] -> 130 |
11486 | 3769 |
|
11476 | 3770 |
" |
3771 |
! |
|
3772 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3773 |
divMod:aNumber |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3774 |
"return an array filled with |
19078 | 3775 |
(self // aNumber) and (self \\ aNumber). |
17037 | 3776 |
The returned remainder has the same sign as aNumber. |
3777 |
The following is always true: |
|
19078 | 3778 |
(receiver // something) * something + (receiver \\ something) = receiver |
17037 | 3779 |
|
3780 |
Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3781 |
Especially surprising: |
19078 | 3782 |
-1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, |
3783 |
and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). |
|
3784 |
-10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, |
|
3785 |
and -4 * 4 gives -12, so we need to add 2 to get the original -10. |
|
17037 | 3786 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3787 |
This may be redefined in some integer classes for |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3788 |
more performance (where the remainder is generated as a side effect of division)" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3789 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3790 |
^ Array |
19078 | 3791 |
with:(self // aNumber) |
3792 |
with:(self \\ aNumber) |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3793 |
|
11587 | 3794 |
" |
17037 | 3795 |
10 divMod:3 -> #(3 1) because 3*3 + 1 = 10 |
3796 |
10 divMod:-3 -> #(-4 -2) because -4*-3 + (-2) = 10 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3797 |
-10 divMod:3 -> #(-4 2) because -4*-3 + 2 = -10 |
17037 | 3798 |
-10 divMod:-3 -> #(3 -1) because -3*3 + (-1) = -10 |
3799 |
||
3800 |
1000000000000000000000 divMod:3 -> #(333333333333333333333 1) |
|
3801 |
1000000000000000000000 divMod:-3 -> #(-333333333333333333334 -2) |
|
3802 |
-1000000000000000000000 divMod:3 -> #(-333333333333333333334 2) |
|
3803 |
-1000000000000000000000 divMod:-3 -> #(333333333333333333333 -1) |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3804 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3805 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3806 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3807 |
"Modified: 29.10.1996 / 21:18:58 / cg" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3808 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3809 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3810 |
extendedEuclid:tb |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3811 |
"return the solution of 'ax + by = gcd(a,b)'. |
7356 | 3812 |
An array containing x, y and gcd(a,b) is returned." |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3813 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3814 |
|a b gcd gcd1 u u1 v v1 tmp t swap shift "{SmallInteger}"| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3815 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3816 |
self < tb ifTrue:[ |
19078 | 3817 |
a := self. |
3818 |
b := tb. |
|
3819 |
swap := false. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3820 |
] ifFalse:[ |
19078 | 3821 |
a := tb. |
3822 |
b := self. |
|
3823 |
swap := true. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3824 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3825 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3826 |
shift := ((a lowBit) min:(b lowBit))-1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3827 |
shift > 0 ifTrue:[ |
19078 | 3828 |
tmp := shift negated. |
3829 |
a := a bitShift:tmp. |
|
3830 |
b := b bitShift:tmp. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3831 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3832 |
|
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
3833 |
gcd := a. |
11587 | 3834 |
gcd1 := b. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3835 |
u := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3836 |
u1 := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3837 |
v := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3838 |
v1 := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3839 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3840 |
[ |
19251 | 3841 |
"/ The following condition is true: |
3842 |
"/ (a * u1) + (b * v1) ~= gcd1 ifTrue:[self halt]. |
|
19078 | 3843 |
t := gcd1 divMod:gcd. |
3844 |
gcd1 := gcd. |
|
3845 |
gcd := t at:2. |
|
3846 |
t := t at:1. |
|
3847 |
tmp := v. |
|
19251 | 3848 |
"/ v1 - (v * t) - v1 + (v * t) ~= 0 ifTrue:[self halt]. |
19078 | 3849 |
v := v1 - (v * t). |
3850 |
v1 := tmp. |
|
3851 |
tmp := u. |
|
19251 | 3852 |
"/ u1 - (u * t) - u1 + (u * t) ~= 0 ifTrue:[self halt]. |
19078 | 3853 |
u := u1 - (u * t). |
3854 |
u1 := tmp. |
|
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
3855 |
gcd > 0] whileTrue. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3856 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3857 |
shift > 0 ifTrue:[ |
19078 | 3858 |
gcd1 := gcd1 bitShift:shift. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3859 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3860 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3861 |
swap ifTrue:[ |
19078 | 3862 |
^ Array with:v1 with:u1 with:gcd1. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3863 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3864 |
^ Array with:u1 with:v1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3865 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3866 |
|
11587 | 3867 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3868 |
14 extendedEuclid:5 |
11587 | 3869 |
14 extendedEuclid:2 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3870 |
25 extendedEuclid:15 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3871 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3872 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3873 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3874 |
"Modified: / 18.11.1999 / 16:19:24 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3875 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3876 |
|
1 | 3877 |
factorial |
19251 | 3878 |
"return fac(self) (i.e. 1*2*3...*self). |
19161 | 3879 |
This chooses a good algorithm, based on the receiver. |
19337 | 3880 |
Some heuristics here, which has to do with the speed of largeInteger arithmetic." |
19161 | 3881 |
|
3882 |
(self <= 20) ifTrue:[ |
|
19078 | 3883 |
self < 0 ifTrue:[ |
3884 |
"/ |
|
3885 |
"/ requested factorial of a negative number |
|
3886 |
"/ |
|
3887 |
^ self class |
|
3888 |
raise:#domainErrorSignal |
|
3889 |
receiver:self |
|
3890 |
selector:#factorial |
|
3891 |
arguments:#() |
|
3892 |
errorString:'factorial of negative number' |
|
3893 |
]. |
|
19161 | 3894 |
^ #(1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800 |
3895 |
479001600 6227020800 87178291200 1307674368000 20922789888000 |
|
3896 |
355687428096000 6402373705728000 121645100408832000 |
|
3897 |
2432902008176640000) at:self+1 |
|
3898 |
]. |
|
3899 |
||
19163 | 3900 |
"/ self < 80000 ifTrue:[ |
3901 |
"/ ^ self factorialHalf |
|
3902 |
"/ ]. |
|
19161 | 3903 |
^ self factorialEvenOdd |
3904 |
||
3905 |
" |
|
3906 |
10 factorial |
|
3907 |
100 factorial |
|
3908 |
1000 factorial |
|
3909 |
10000 factorial |
|
3910 |
100000 factorial |
|
3911 |
200000 factorial |
|
3912 |
300000 factorial |
|
3913 |
1000000 factorial |
|
3914 |
||
3915 |
Time millisecondsToRun:[10000 factorial]40 |
|
3916 |
Time millisecondsToRun:[100000 factorial]3220 |
|
3917 |
Time millisecondsToRun:[1000000 factorial]357120 |
|
19164 | 3918 |
|
3919 |
#(factorialIter factorialHalf factorialEvenOdd factorial) |
|
3920 |
do:[:sel | |
|
3921 |
#( (10000 10) |
|
3922 |
(20000 10) |
|
3923 |
(50000 10) |
|
3924 |
(70000 10) |
|
3925 |
(100000 5) |
|
3926 |
(200000 3) |
|
3927 |
(300000 3) |
|
3928 |
(400000 3)) pairsDo:[:n :repeat | |
|
3929 |
|times| |
|
3930 |
times := (1 to:repeat) collect:[:i | |
|
3931 |
Time millisecondsToRun:[ n perform:sel] |
|
3932 |
]. |
|
3933 |
||
3934 |
Transcript printf:'%12s %6d: %5d\n' with:sel with:n with:times min |
|
3935 |
] |
|
3936 |
]. |
|
3937 |
||
3938 |
factorialIter 10000: 30 |
|
3939 |
factorialIter 20000: 130 |
|
3940 |
factorialIter 50000: 790 |
|
3941 |
factorialIter 70000: 1710 |
|
3942 |
factorialIter 100000: 4880 |
|
3943 |
factorialIter 200000: 24980 |
|
3944 |
factorialIter 300000: 60060 |
|
3945 |
factorialIter 400000: 112310 |
|
3946 |
factorialHalf 10000: 20 |
|
3947 |
factorialHalf 20000: 100 |
|
3948 |
factorialHalf 50000: 690 |
|
3949 |
factorialHalf 70000: 1430 |
|
3950 |
factorialHalf 100000: 3220 |
|
3951 |
factorialHalf 200000: 28340 |
|
3952 |
factorialHalf 300000: 68740 |
|
3953 |
factorialHalf 400000: 127490 |
|
3954 |
factorialEvenOdd 10000: 10 |
|
3955 |
factorialEvenOdd 20000: 60 |
|
3956 |
factorialEvenOdd 50000: 390 |
|
3957 |
factorialEvenOdd 70000: 810 |
|
3958 |
factorialEvenOdd 100000: 2020 |
|
3959 |
factorialEvenOdd 200000: 9960 |
|
3960 |
factorialEvenOdd 300000: 24480 |
|
3961 |
factorialEvenOdd 400000: 45340 |
|
3962 |
factorial 10000: 20 |
|
3963 |
factorial 20000: 100 |
|
3964 |
factorial 50000: 680 |
|
3965 |
factorial 70000: 1400 |
|
3966 |
factorial 100000: 2040 |
|
3967 |
factorial 200000: 10130 |
|
3968 |
factorial 300000: 24670 |
|
19161 | 3969 |
" |
3970 |
! |
|
3971 |
||
3972 |
factorialEvenOdd |
|
22166 | 3973 |
"a recursive odd-even algorithm, which processes smaller largeInts in the loop. |
3974 |
Because multiplication is an O(n^2) algorithm, there is a threshold from which |
|
3975 |
more but smaller multiplications makes a noticable difference" |
|
19161 | 3976 |
|
3977 |
|pO i s2 t stop| |
|
3978 |
||
3979 |
(self <= 20) ifTrue:[ |
|
3980 |
^ #(1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800 |
|
3981 |
479001600 6227020800 87178291200 1307674368000 20922789888000 |
|
3982 |
355687428096000 6402373705728000 121645100408832000 |
|
3983 |
2432902008176640000) at:self+1 |
|
3984 |
]. |
|
3985 |
||
3986 |
"/ |
|
3987 |
"/ 3 * 4 * 5 * 6 *7 * 8 .... * n |
|
3988 |
"/ odd numbers: |
|
3989 |
"/ 3 5 7 9 ... n |
|
19251 | 3990 |
"/ even numbers: |
19161 | 3991 |
"/ 2 4 6 8 ... n |
3992 |
"/ 1 2 3 4 ... n//2 |
|
3993 |
"/ is (n/2)!! << n-1 |
|
3994 |
||
3995 |
pO := 1. |
|
3996 |
i := 3. |
|
3997 |
||
3998 |
"/ odds only in pairs as |
|
3999 |
"/ i * (i + 2) |
|
4000 |
"/ to get to the next pair, |
|
4001 |
"/ (i+4)(i+6) |
|
4002 |
"/ we add: 8i + 24 |
|
4003 |
"/ (i+4)(i+6)-(i*(i+2)) |
|
4004 |
"/ i^2 + 10i + 24 - i^2 - 2i |
|
4005 |
"/ 8i + 24 |
|
4006 |
stop := self-2. |
|
4007 |
t := i*(i+2). |
|
4008 |
[i <= stop] whileTrue:[ |
|
4009 |
"/ odd*next odd |
|
4010 |
pO := pO * t. |
|
19163 | 4011 |
t := t + ((i*8) + 24). |
19161 | 4012 |
i := i + 4. |
4013 |
]. |
|
4014 |
||
4015 |
[i <= self] whileTrue:[ |
|
4016 |
"/ odd |
|
4017 |
pO := pO * i. |
|
4018 |
i := i + 2. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
4019 |
]. |
19161 | 4020 |
|
4021 |
"/ the factorial of the evens... |
|
4022 |
s2 := (self//2). |
|
4023 |
^ (s2 factorialEvenOdd * pO) << s2. |
|
4024 |
||
4025 |
" |
|
4026 |
(6 to:2000) conform:[:i | i factorialIter = i factorialEvenOdd] |
|
4027 |
||
4028 |
Time millisecondsToRun:[20000 factorialIter] |
|
4029 |
Time millisecondsToRun:[50000 factorialIter] |
|
4030 |
Time millisecondsToRun:[70000 factorialIter] |
|
4031 |
Time millisecondsToRun:[100000 factorialIter] |
|
23646 | 4032 |
Time millisecondsToRun:[200000 factorialIter] 16190 |
19161 | 4033 |
|
4034 |
Time millisecondsToRun:[20000 factorialEvenOdd] |
|
4035 |
Time millisecondsToRun:[50000 factorialEvenOdd] |
|
4036 |
Time millisecondsToRun:[70000 factorialEvenOdd] |
|
4037 |
Time millisecondsToRun:[100000 factorialEvenOdd] |
|
23646 | 4038 |
Time millisecondsToRun:[200000 factorialEvenOdd] 2910 |
19161 | 4039 |
" |
22166 | 4040 |
|
4041 |
"Modified (comment): / 05-07-2017 / 16:41:41 / cg" |
|
23646 | 4042 |
"Modified (comment): / 26-01-2019 / 08:48:12 / Claus Gittinger" |
19161 | 4043 |
! |
4044 |
||
4045 |
factorialHalf |
|
19251 | 4046 |
"an algorithm, which does it with half the number of multiplications. |
19161 | 4047 |
this is faster than factorialPM to roughly 60000." |
4048 |
||
4049 |
|p i d| |
|
4050 |
||
4051 |
i := self. |
|
4052 |
self odd ifTrue:[ |
|
4053 |
i := i - 1. |
|
4054 |
]. |
|
20269 | 4055 |
|
19161 | 4056 |
p := i. |
4057 |
d := i - 2. |
|
4058 |
[d >= 2] whileTrue:[ |
|
4059 |
i := i + d. |
|
4060 |
p := p * i. |
|
4061 |
d := d - 2. |
|
4062 |
]. |
|
4063 |
self odd ifTrue:[ |
|
4064 |
p := p * self |
|
20269 | 4065 |
]. |
19161 | 4066 |
^ p |
4067 |
||
4068 |
" |
|
4069 |
10 factorial 3628800 |
|
4070 |
10 factorialHalf 3628800 |
|
20269 | 4071 |
|
19161 | 4072 |
11 factorial 39916800 |
4073 |
11 factorialHalf 39916800 |
|
4074 |
||
4075 |
12 factorial 479001600 |
|
20269 | 4076 |
12 factorialHalf 479001600 |
19161 | 4077 |
|
4078 |
10000 factorial = 10000 factorialHalf |
|
4079 |
||
4080 |
(6 to:2000) conform:[:i | i factorialIter = i factorialHalf] |
|
20269 | 4081 |
|
19161 | 4082 |
Time microsecondsToRun:[30 factorialIter] |
4083 |
Time microsecondsToRun:[30 factorialHalf] |
|
4084 |
Time microsecondsToRun:[50 factorialIter] |
|
4085 |
Time microsecondsToRun:[50 factorialHalf] |
|
4086 |
Time microsecondsToRun:[75 factorialIter] |
|
4087 |
Time microsecondsToRun:[75 factorialHalf] |
|
4088 |
Time microsecondsToRun:[100 factorialIter] |
|
4089 |
Time microsecondsToRun:[100 factorialHalf] |
|
4090 |
Time microsecondsToRun:[500 factorialIter] |
|
4091 |
Time microsecondsToRun:[500 factorialHalf] |
|
4092 |
Time microsecondsToRun:[1000 factorialIter] |
|
4093 |
Time microsecondsToRun:[1000 factorialHalf] |
|
4094 |
Time microsecondsToRun:[2000 factorialIter] |
|
4095 |
Time microsecondsToRun:[2000 factorialHalf] |
|
4096 |
||
4097 |
Time microsecondsToRun:[500 factorial]118 120 120 |
|
4098 |
Time microsecondsToRun:[1000 factorial]339 355 406 |
|
4099 |
Time microsecondsToRun:[5000 factorial]15703 13669 7715 |
|
4100 |
Time millisecondsToRun:[10000 factorial]40 30 50 |
|
4101 |
Time millisecondsToRun:[20000 factorial]140 150 150 |
|
4102 |
Time millisecondsToRun:[40000 factorial]600 570 560 570 |
|
4103 |
Time millisecondsToRun:[60000 factorial]1220 1240 1340 |
|
4104 |
Time millisecondsToRun:[80000 factorial]2600 2580 2540 |
|
4105 |
Time millisecondsToRun:[100000 factorial]4680 4810 5280 |
|
4106 |
Time millisecondsToRun:[120000 factorial]8100 8010 7920 |
|
4107 |
Time millisecondsToRun:[150000 factorial]13830 14040 13360 |
|
20269 | 4108 |
Time millisecondsToRun:[200000 factorial]23880 23740 |
19161 | 4109 |
|
4110 |
Time microsecondsToRun:[500 factorialHalf]150 142 192 |
|
4111 |
Time microsecondsToRun:[1000 factorialHalf]383 527 684 |
|
4112 |
Time microsecondsToRun:[5000 factorialHalf]6654 9221 4629 |
|
4113 |
Time millisecondsToRun:[10000 factorialHalf]20 30 20 |
|
4114 |
Time millisecondsToRun:[20000 factorialHalf]110 110 110 |
|
4115 |
Time millisecondsToRun:[40000 factorialHalf]490 490 490 |
|
4116 |
Time millisecondsToRun:[60000 factorialHalf]1100 1090 1070 |
|
4117 |
Time millisecondsToRun:[80000 factorialHalf]1920 1920 1880 |
|
4118 |
Time millisecondsToRun:[100000 factorialHalf]3030 3010 3000 |
|
4119 |
Time millisecondsToRun:[120000 factorialHalf]4830 4770 4760 |
|
4120 |
Time millisecondsToRun:[150000 factorialHalf]14510 13940 13900 |
|
20269 | 4121 |
Time millisecondsToRun:[200000 factorialHalf]28730 28160 |
19161 | 4122 |
" |
4123 |
! |
|
4124 |
||
4125 |
factorialIter |
|
4126 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
|
4127 |
This is slightly faster than the recursive algorithm, and does not |
|
4128 |
suffer from stack overflow problems (with big receivers)" |
|
4129 |
||
4130 |
|p i| |
|
4131 |
||
1560 | 4132 |
p := 2. |
2745 | 4133 |
i := 3. |
4134 |
[i <= self] whileTrue:[ |
|
19078 | 4135 |
p := p * i. |
4136 |
i := i + 1. |
|
1560 | 4137 |
]. |
4138 |
^ p |
|
4139 |
||
11587 | 4140 |
" |
1560 | 4141 |
10 factorial |
4142 |
1000 factorial |
|
4143 |
10000 factorial |
|
4144 |
10000 factorialR |
|
4145 |
||
11587 | 4146 |
Time millisecondsToRun:[2000 factorial] |
4147 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 4148 |
-1 factorial |
1560 | 4149 |
" |
4150 |
||
10027 | 4151 |
"Modified: / 04-10-2006 / 14:31:12 / cg" |
1560 | 4152 |
! |
4153 |
||
4154 |
factorialR |
|
3908 | 4155 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm. |
11496 | 4156 |
|
3908 | 4157 |
This is included to demonstration purposes - if you really need |
19251 | 4158 |
factorial numbers, use the tuned #factorial, which is |
3908 | 4159 |
faster and does not suffer from stack overflow problems (with big receivers)." |
1 | 4160 |
|
356 | 4161 |
(self >= 2) ifTrue:[ |
19078 | 4162 |
^ self * (self - 1) factorialR |
1 | 4163 |
]. |
356 | 4164 |
^ 1 |
212 | 4165 |
|
4166 |
" |
|
1560 | 4167 |
10 factorialR |
4168 |
1000 factorialR |
|
4158 | 4169 |
Time millisecondsToRun:[10000 factorial] |
212 | 4170 |
" |
1560 | 4171 |
|
3908 | 4172 |
"Created: / 18.7.1996 / 12:48:36 / cg" |
4158 | 4173 |
"Modified: / 8.5.1999 / 18:40:13 / cg" |
1 | 4174 |
! |
4175 |
||
11896 | 4176 |
fib |
4177 |
"compute the fibionacci number for the receiver. |
|
19078 | 4178 |
fib(0) := 0 |
4179 |
fib(1) := 1 |
|
4180 |
fib(n) := fib(n-1) + fib(n-2)" |
|
11896 | 4181 |
|
4182 |
self <= 0 ifTrue:[ |
|
19078 | 4183 |
self == 0 ifTrue:[^ 0]. |
11896 | 4184 |
]. |
4185 |
^ self fib_helper |
|
4186 |
||
4187 |
" |
|
11945 | 4188 |
30 fib |
11896 | 4189 |
60 fib |
4190 |
1000 fib |
|
4191 |
" |
|
4192 |
! |
|
4193 |
||
4194 |
fib_helper |
|
4195 |
"compute the fibionacci number for the receiver. |
|
4196 |
||
19078 | 4197 |
Fib(n) = Fib(n-1) + Fib(n-2) |
13023 | 4198 |
|
4199 |
Knuth: |
|
19078 | 4200 |
Fib(n+m) = Fib(m) * Fib(n+1) + Fib(m-1) * Fib(n) |
13023 | 4201 |
|
4202 |
This is about 3 times faster than fib_iterative. |
|
11896 | 4203 |
" |
4204 |
||
4205 |
|fibUsingDict dict| |
|
4206 |
||
4207 |
dict := Dictionary new:100. |
|
4208 |
||
4209 |
fibUsingDict := [:x | |
|
19078 | 4210 |
|n fib fibN fibNp1 fibNm1 fibXm1 fibXm2 fibXp1| |
4211 |
||
4212 |
x <= 30 ifTrue:[ |
|
4213 |
"/ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
4214 |
fib := #(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 |
|
4215 |
) at:x |
|
4216 |
] ifFalse:[ |
|
4217 |
fib := dict at:x ifAbsent:nil. |
|
4218 |
fib isNil ifTrue:[ |
|
4219 |
fibXm1 := dict at:(x-1) ifAbsent:nil. |
|
4220 |
fibXm1 notNil ifTrue:[ |
|
4221 |
fibXm2 := dict at:(x-2) ifAbsent:nil. |
|
4222 |
fibXm2 notNil ifTrue:[ |
|
4223 |
fib := fibXm1 + fibXm2. |
|
4224 |
] ifFalse:[ |
|
4225 |
fibXp1 := dict at:(x+1) ifAbsent:nil. |
|
4226 |
fibXp1 notNil ifTrue:[ |
|
4227 |
fib := fibXp1 - fibXm1. |
|
4228 |
] |
|
4229 |
] |
|
4230 |
]. |
|
4231 |
||
4232 |
fib isNil ifTrue:[ |
|
4233 |
n := x // 2. |
|
4234 |
x odd ifTrue:[ |
|
4235 |
"/ m is set to n+1; therefore: |
|
4236 |
"/ Fib(x) = Fib(n+n+1) ; x odd; setting n = (x-1)/2 |
|
4237 |
"/ using Knuth: |
|
4238 |
"/ Fib(n+n+1) = Fib(n+1) * Fib(n+1) + Fib(n+1-1) * Fib(n) |
|
4239 |
"/ = (Fib(n+1) ^ 2) + (Fib(n) ^ 2) |
|
4240 |
fibN := fibUsingDict value:n. |
|
4241 |
fibNp1 := fibUsingDict value:(n+1). |
|
4242 |
fib := fibN squared + fibNp1 squared |
|
4243 |
] ifFalse:[ |
|
4244 |
"/ as |
|
4245 |
"/ Fib(n+1) = Fib(n) + Fib(n-1) |
|
4246 |
"/ therefore: |
|
4247 |
"/ Fib(n) = Fib(n+1) - Fib(n-1) |
|
4248 |
"/ and, since n is even, n+1 and n-1 are odd, and can be computed as above. |
|
4249 |
"/ This gives us: |
|
4250 |
"/ Fib(x) = Fib(x+1) - Fib(x-1) ; x even; setting n = x/2 |
|
4251 |
"/ = Fib(n+n+1) - Fib(n+n-1) |
|
4252 |
"/ = Fib(n+n+1) - Fib((n-1)+(n-1)+1) |
|
4253 |
"/ = ((Fib(n+1)^2) + (Fib(n)^2)) - ((Fib((n-1)+1)^2) + (Fib((n-1))^2)) |
|
4254 |
"/ = (Fib(n+1)^2) + (Fib(n)^2) - (Fib(n)^2) - (Fib((n-1))^2) |
|
4255 |
"/ = (Fib(n+1)^2) - (Fib((n-1))^2) |
|
4256 |
fibNm1 := fibUsingDict value:(n-1). |
|
4257 |
fibNp1 := fibUsingDict value:(n+1). |
|
4258 |
fib := fibNp1 squared - fibNm1 squared |
|
4259 |
]. |
|
4260 |
]. |
|
4261 |
dict at:x put:fib. |
|
4262 |
] |
|
4263 |
]. |
|
4264 |
fib |
|
11896 | 4265 |
]. |
4266 |
||
4267 |
^ fibUsingDict value:self |
|
4268 |
||
4269 |
"the running time is mostly dictated by the LargeInteger multiplication performance... |
|
25149 | 4270 |
(therefore, we get O(n²) execution times, even for a linear number of multiplications) |
11896 | 4271 |
|
4272 |
Time millisecondsToRun:[50000 fib_iterative] 312 (DUO 1.7Ghz CPU) |
|
4273 |
Time millisecondsToRun:[50000 fib_helper] 109 |
|
4274 |
||
11945 | 4275 |
Time millisecondsToRun:[100000 fib_iterative] 1248 |
11896 | 4276 |
Time millisecondsToRun:[100000 fib_helper] 374 |
4277 |
||
11945 | 4278 |
Time millisecondsToRun:[200000 fib_iterative] 4758 |
11896 | 4279 |
Time millisecondsToRun:[200000 fib_helper] 1544 |
4280 |
||
11945 | 4281 |
Time millisecondsToRun:[400000 fib_iterative] 18892 |
11896 | 4282 |
Time millisecondsToRun:[400000 fib_helper] 6084 |
4283 |
||
4284 |
1 to:100 do:[:i | self assert:(i fib_iterative = i fib_helper) ] |
|
4285 |
1 to:100 do:[:i | self assert:(i fib_iterative = i fib) ] |
|
4286 |
" |
|
13023 | 4287 |
|
4288 |
"Modified: / 17-08-2010 / 17:29:34 / cg" |
|
11896 | 4289 |
! |
4290 |
||
11587 | 4291 |
gcd:anInteger |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
4292 |
"return the greatest common divisor of the receiver and anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
4293 |
Euclids & Knuths algorithm." |
1 | 4294 |
|
11496 | 4295 |
|a b t| |
4296 |
||
4297 |
a := self abs. |
|
4298 |
b := anInteger abs. |
|
4299 |
||
4300 |
a < b ifTrue:[ |
|
19078 | 4301 |
t := a. |
4302 |
a := b. |
|
4303 |
b := t. |
|
1 | 4304 |
]. |
52 | 4305 |
|
11496 | 4306 |
b = 0 ifTrue: [^ a]. |
4307 |
a := a \\ b. |
|
4308 |
a = 0 ifTrue:[^ b]. |
|
4309 |
^ b gcd_helper:a |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
4310 |
|
11587 | 4311 |
" |
4312 |
3141589999999999 gcd:1000000000000000 |
|
11496 | 4313 |
|
4314 |
Time millisecondsToRun:[ |
|
19078 | 4315 |
10000 timesRepeat:[ |
4316 |
123456789012345678901234567890 gcd: 9876543210987654321 |
|
4317 |
] |
|
11496 | 4318 |
] |
3060 | 4319 |
" |
4320 |
||
4321 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 4322 |
! |
4323 |
||
25215 | 4324 |
gfMul2_128:factorIn |
4325 |
"Galoise multiplication inf GF(2^128). This is defined by 1 + a + a^2 + a^7 + a^128. |
|
4326 |
Candidate for faste implementation - especially for Intel processors: |
|
4327 |
https://www.intel.cn/content/dam/www/public/us/en/documents/white-papers/carry-less-multiplication-instruction-in-gcm-mode-paper.pdf" |
|
4328 |
||
4329 |
|factor result| |
|
4330 |
||
4331 |
factor := factorIn. |
|
4332 |
result := 0. |
|
4333 |
||
4334 |
128 to:1 by:-1 do:[:i| |
|
4335 |
(self bitAt:i) == 1 ifTrue:[ |
|
4336 |
result := result bitXored:factor. |
|
4337 |
]. |
|
4338 |
factor odd ifTrue:[ |
|
4339 |
factor := (factor rightShift:1) bitXored:16rE1000000000000000000000000000000. |
|
4340 |
] ifFalse:[ |
|
4341 |
factor := factor rightShift:1. |
|
4342 |
] |
|
4343 |
]. |
|
4344 |
^ result compressed. |
|
4345 |
||
4346 |
"<<EOC |
|
4347 |
#( |
|
4348 |
(16r0388DACE60B6A392F328C2B971B2FE78 16r66E94BD4EF8A2C3B884CFA59CA342B2E 16r5E2EC746917062882C85B0685353DEB7) |
|
4349 |
) triplesDo:[:x :y :result| |
|
4350 |
|square| |
|
4351 |
self assert:(x gfMul2_128:y) = result. |
|
4352 |
self assert:(y gfMul2_128:x) = result. |
|
4353 |
"/ check distributive law (#bitXor is addition in GF): |
|
4354 |
self assert:(x gfMul2_128:(y bitXor:result)) = ((x gfMul2_128:y) bitXor:(x gfMul2_128:result)). |
|
4355 |
||
4356 |
"/ check, that order is 2^128-1: |
|
4357 |
square := x. |
|
4358 |
128 timesRepeat:[ |
|
4359 |
square := square gfMul2_128:square. |
|
4360 |
]. |
|
4361 |
self assert:square = x. |
|
4362 |
]. |
|
4363 |
||
4364 |
#( |
|
4365 |
(1 2 3) |
|
4366 |
(111 333 444) |
|
4367 |
(555777 888999 33333333333333333333333333333333333) |
|
4368 |
) triplesDo:[:x :y :z| |
|
4369 |
self assert:(x gfMul2_128:y) = (y gfMul2_128:x). |
|
4370 |
"/ check distributive law (#bitXor is addition in GF): |
|
4371 |
self assert:(x gfMul2_128:(y bitXor:z)) = ((x gfMul2_128:y) bitXor:(x gfMul2_128:z)) |
|
4372 |
]. |
|
4373 |
EOC" |
|
4374 |
||
4375 |
"Created: / 29-01-2020 / 15:16:49 / Stefan Vogel" |
|
4376 |
"Modified: / 29-01-2020 / 17:57:15 / Stefan Vogel" |
|
4377 |
! |
|
4378 |
||
21938 | 4379 |
integerLog10 |
4380 |
"return the floor of log10 of the receiver. |
|
4381 |
This is the same as (self log:10) floor. |
|
4382 |
Used to find out the number of digits needed |
|
4383 |
to print a number/and for conversion to a LargeInteger." |
|
4384 |
||
4385 |
^ self asFloat log10 floor |
|
4386 |
||
4387 |
" |
|
4388 |
10 integerLog10 |
|
4389 |
1000 integerLog10 |
|
4390 |
||
4391 |
10000000000000000.0 log:10 |
|
4392 |
10000000000000000 integerLog10 |
|
4393 |
100000000000000000 integerLog10 |
|
4394 |
1000000000000000000 integerLog10 |
|
4395 |
10000000000000000000 integerLog10 |
|
4396 |
100000000000000000000 integerLog10 |
|
4397 |
1000000000000000000000 integerLog10 -> 21 |
|
4398 |
1000000000000000000000000000000 integerLog10 -> 30 |
|
4399 |
10000000000000000000000000000000000000000 integerLog10 -> 40 |
|
4400 |
||
4401 |
1 to:10000 by:10 do:[:i | |
|
4402 |
self assert:(i factorial printString size == (i factorial integerLog10+1)) |
|
4403 |
]. |
|
4404 |
21 factorial printString size |
|
4405 |
21 factorial integerLog10 |
|
4406 |
51 factorial printString size |
|
4407 |
51 factorial integerLog10 |
|
4408 |
" |
|
4409 |
||
4410 |
"Created: / 02-07-2017 / 01:20:49 / cg" |
|
4411 |
"Modified: / 02-07-2017 / 10:16:40 / cg" |
|
4412 |
! |
|
4413 |
||
14097 | 4414 |
integerLog2 |
14491 | 4415 |
"return the floor of log2 of the receiver. |
4416 |
This is the same as (self log:2) floor." |
|
14097 | 4417 |
|
4418 |
self <= 0 ifTrue:[ |
|
19078 | 4419 |
^ self class |
4420 |
raise:#domainErrorSignal |
|
4421 |
receiver:self |
|
4422 |
selector:#integerLog2 |
|
4423 |
arguments:#() |
|
4424 |
errorString:'logarithm of negative integer' |
|
14097 | 4425 |
]. |
4426 |
^ self highBit - 1. |
|
4427 |
||
4428 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4429 |
2 log:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4430 |
2 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4431 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4432 |
3 log:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4433 |
3 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4434 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4435 |
4 log:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4436 |
4 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4437 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4438 |
64 integerLog2 |
14097 | 4439 |
100 integerLog2 |
4440 |
100 log:2 |
|
4441 |
999 integerLog2 |
|
4442 |
999 log:2 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4443 |
120000 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4444 |
120000 log:2 |
14097 | 4445 |
-1 integerLog2 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4446 |
50 factorial integerLog2 |
14767 | 4447 |
50 factorial log:2 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4448 |
1000 factorial integerLog2 |
14767 | 4449 |
1000 factorial log:2 -- float error!! |
14097 | 4450 |
" |
4451 |
! |
|
4452 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4453 |
integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4454 |
"return an integer representing 1/self * 2**n. |
11896 | 4455 |
Where an integer is one bit longer than self. |
4456 |
This is a helper for modulu numbers" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4457 |
|
19231 | 4458 |
|b "{Class: SmallInteger}" rem result digitBytes| |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4459 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4460 |
b := self highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4461 |
rem := 1 bitShift:b. |
10488 | 4462 |
result := LargeInteger basicNew numberOfDigits:(b // 8)+1. |
19231 | 4463 |
digitBytes := result digitBytes. |
4464 |
b+1 to:1 by:-1 do:[:idx| |
|
19078 | 4465 |
rem >= self ifTrue:[ |
19231 | 4466 |
rem := rem - self. |
4467 |
digitBytes bitSetAt:idx. |
|
19078 | 4468 |
]. |
4469 |
rem := rem mul2. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4470 |
]. |
11587 | 4471 |
^ result compressed. |
4472 |
||
4473 |
" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4474 |
333 integerReciprocal (2 raisedTo:18) // 333 |
11587 | 4475 |
393 integerReciprocal |
11945 | 4476 |
8 integerReciprocal |
4477 |
15 integerReciprocal |
|
11587 | 4478 |
15112233445566 integerReciprocal |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4479 |
10239552311579 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4480 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4481 |
|
13023 | 4482 |
"Modified: / 03-05-1999 / 14:27:18 / stefan" |
4483 |
"Modified: / 17-08-2010 / 17:30:22 / cg" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4484 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4485 |
|
12588 | 4486 |
integerSqrt |
21931 | 4487 |
"return the largest integer which is less or equal to the receiver's square root. |
4488 |
For large integers, this provides better results than the float sqrt method |
|
4489 |
(which actually fails for very large numbers) |
|
12586
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
4490 |
This might be needed for some number theoretic problems with large numbers |
21931 | 4491 |
(and also in cryptography). |
4492 |
Uses Newton's method" |
|
12586
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
4493 |
|
24868 | 4494 |
|guess| |
12581 | 4495 |
|
18851 | 4496 |
self negative ifTrue:[ |
19078 | 4497 |
^ self class |
4498 |
raise:#imaginaryResultSignal |
|
4499 |
receiver:self |
|
4500 |
selector:#integerSqrt |
|
4501 |
arguments:#() |
|
4502 |
errorString:'bad (negative) receiver in sqrt' |
|
18851 | 4503 |
]. |
19078 | 4504 |
self == 0 ifTrue:[^ 0]. |
20269 | 4505 |
|
12582
7afa0bb19bbe
comment/format in: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12581
diff
changeset
|
4506 |
guess := (1 bitShift:(self highBit // 2)). |
24868 | 4507 |
^ self integerSqrtWithGuess:guess |
4508 |
||
4509 |
" |
|
4510 |
333 sqrt -> 18.2482875908947 |
|
4511 |
324 sqrt -> 18.0 |
|
4512 |
323 sqrt -> 17.9722007556114 |
|
4513 |
||
4514 |
333 integerSqrt -> 18 |
|
4515 |
325 integerSqrt -> 18 |
|
4516 |
324 integerSqrt -> 18 |
|
4517 |
323 integerSqrt -> 17 |
|
4518 |
||
4519 |
10239552004900 integerSqrt |
|
4520 |
10239552004900 sqrt |
|
4521 |
10239552311579 integerSqrt |
|
4522 |
10239552311579 sqrt |
|
4523 |
||
4524 |
5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt squared |
|
4525 |
5397346292805549782720214077673687804022210808238353958670041357153884304 sqrt squared |
|
4526 |
||
4527 |
5397346292805549782720214077673687806275517530364350655459511599582614290 integerSqrt |
|
4528 |
5397346292805549782720214077673687806275517530364350655459511599582614290 sqrt |
|
4529 |
1000 factorial integerSqrt |
|
4530 |
||
4531 |
1000 factorial - 1000 factorial integerSqrt squared |
|
4532 |
1000 factorial - (1000 factorial integerSqrt + 1) squared |
|
4533 |
1000 factorial between:(1000 factorial integerSqrt squared) and:((1000 factorial integerSqrt + 1) squared) |
|
4534 |
" |
|
4535 |
||
4536 |
"Modified (comment): / 25-07-2017 / 17:52:14 / cg" |
|
4537 |
! |
|
4538 |
||
4539 |
integerSqrtWithGuess:initialGuess |
|
4540 |
"return the largest integer which is less or equal to the receiver's square root. |
|
4541 |
For large integers, this provides better results than the float sqrt method |
|
4542 |
(which actually fails for very large numbers) |
|
4543 |
This might be needed for some number theoretic problems with large numbers |
|
4544 |
(and also in cryptography). |
|
4545 |
Uses Newton's method" |
|
4546 |
||
4547 |
|guess prevGuess guessSquared| |
|
4548 |
||
4549 |
guess := initialGuess. |
|
4550 |
prevGuess := nil. |
|
12581 | 4551 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4552 |
[ |
19078 | 4553 |
prevGuess ~= guess |
4554 |
and:[ ((guessSquared := guess squared) - self) abs >= guess ] |
|
12581 | 4555 |
] whileTrue:[ |
19078 | 4556 |
prevGuess := guess. |
4557 |
guess := (guess + (self / guess)) // 2. |
|
12581 | 4558 |
]. |
12586
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
4559 |
guessSquared > self ifTrue:[ |
19078 | 4560 |
guess := guess - 1. |
12581 | 4561 |
]. |
12585
e8ce2be6049a
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12582
diff
changeset
|
4562 |
"/ self assert:(guess squared <= self). |
e8ce2be6049a
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12582
diff
changeset
|
4563 |
"/ self assert:((guess + 1) squared > self). |
12581 | 4564 |
|
4565 |
^ guess. |
|
4566 |
||
4567 |
" |
|
22107 | 4568 |
333 sqrt -> 18.2482875908947 |
4569 |
324 sqrt -> 18.0 |
|
4570 |
323 sqrt -> 17.9722007556114 |
|
4571 |
||
4572 |
333 integerSqrt -> 18 |
|
4573 |
325 integerSqrt -> 18 |
|
4574 |
324 integerSqrt -> 18 |
|
4575 |
323 integerSqrt -> 17 |
|
21931 | 4576 |
|
4577 |
10239552004900 integerSqrt |
|
4578 |
10239552004900 sqrt |
|
12588 | 4579 |
10239552311579 integerSqrt |
21931 | 4580 |
10239552311579 sqrt |
4581 |
||
4582 |
5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt squared |
|
4583 |
5397346292805549782720214077673687804022210808238353958670041357153884304 sqrt squared |
|
4584 |
||
12588 | 4585 |
5397346292805549782720214077673687806275517530364350655459511599582614290 integerSqrt |
21931 | 4586 |
5397346292805549782720214077673687806275517530364350655459511599582614290 sqrt |
18851 | 4587 |
1000 factorial integerSqrt |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4588 |
|
18851 | 4589 |
1000 factorial - 1000 factorial integerSqrt squared |
4590 |
1000 factorial - (1000 factorial integerSqrt + 1) squared |
|
21931 | 4591 |
1000 factorial between:(1000 factorial integerSqrt squared) and:((1000 factorial integerSqrt + 1) squared) |
12581 | 4592 |
" |
21931 | 4593 |
|
22107 | 4594 |
"Modified (comment): / 25-07-2017 / 17:52:14 / cg" |
12581 | 4595 |
! |
4596 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4597 |
inverseMod:n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4598 |
"find the modular inverse for myself to n. |
11896 | 4599 |
This is defined as the solution of: '1 = (self * x) mod n. |
4600 |
This is a helper for modulu numbers" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4601 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4602 |
|e ret| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4603 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
4604 |
"the following expression returns #(x y (self gcd:n)), the solution of the equation |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
4605 |
(self * x) + (n * y) = self gcd:n" |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
4606 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4607 |
e := self extendedEuclid:n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4608 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4609 |
(e at:3) == 1 ifTrue:[ |
19078 | 4610 |
ret := e at:1. |
4611 |
ret negative ifTrue:[ |
|
4612 |
^ ret + n |
|
4613 |
]. |
|
4614 |
^ ret. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4615 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4616 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4617 |
^ 0 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4618 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4619 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4620 |
14 inverseMod:5 -> 4 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4621 |
5 inverseMod:14 -> 3 |
11587 | 4622 |
14 inverseMod:11 -> 4 (4 * 14) \\ 11 |
4623 |
11 inverseMod:14 -> 9 (9 * 11) \\ 14 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4624 |
79 inverseMod:3220 -> 1019 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4625 |
3220 inverseMod:79 -> 54 (54 * 3220) \\ 79 |
11587 | 4626 |
1234567891 inverseMod:1111111111119 |
19078 | 4627 |
-> 148726663534 (148726663534*1234567891) \\ 1111111111119 |
11587 | 4628 |
|
4629 |
||
4630 |
14 extendedEuclid:11 |
|
4631 |
5 extendedEuclid:14 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4632 |
14 extendedEuclid:2 |
11587 | 4633 |
3220 extendedEuclid:79 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4634 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4635 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4636 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
4637 |
"Modified: / 18.11.1999 / 16:21:37 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4638 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4639 |
|
1 | 4640 |
lcm:anInteger |
52 | 4641 |
"return the least common multiple (using gcd:)" |
4642 |
||
4643 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
4644 |
||
92 | 4645 |
" |
4646 |
65 lcm:15 |
|
4647 |
3 lcm:15 |
|
4648 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4649 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4650 |
|
11496 | 4651 |
primeFactors |
4652 |
"return a collection of prime factors of the receiver. |
|
4653 |
For prime numbers, an empty collection is returned. |
|
13021 | 4654 |
Can take a long time for big numbers" |
11496 | 4655 |
|
11508 | 4656 |
^ self primeFactorsUpTo:nil |
4657 |
||
4658 |
" |
|
4659 |
2 to:10000 do:[:n | |
|
19078 | 4660 |
self assert:((n isPrime and:[ n primeFactors isEmpty]) |
4661 |
or:[ n isPrime not and:[n primeFactors product = n]]) |
|
11508 | 4662 |
] |
4663 |
3 to:10000 do:[:n | |
|
19078 | 4664 |
self assert:(n factorial primeFactors product = n factorial) |
11508 | 4665 |
] |
4666 |
||
11587 | 4667 |
13195 primeFactors |
4668 |
12 primeFactors |
|
4669 |
2 primeFactors |
|
4670 |
3 primeFactors |
|
4671 |
5 primeFactors |
|
4672 |
14 primeFactors |
|
4673 |
13423453625634765 primeFactors |
|
4674 |
13423453625634765 isPrime |
|
4675 |
13423453625634765 gcd:(3 * 5 * 19 * 29) |
|
4676 |
13423453625634765 / 8265 |
|
4677 |
1624132320101 isPrime |
|
11508 | 4678 |
1624132320101 gcd: 8265 |
4679 |
||
11587 | 4680 |
1000000 primeFactors |
4681 |
100000000 primeFactors |
|
4682 |
1000000000 primeFactors |
|
11508 | 4683 |
|
4684 |
Time millisecondsToRun:[ |
|
19078 | 4685 |
1000 timesRepeat:[ |
4686 |
10000000000000000000000000000000000000 primeFactors |
|
4687 |
] |
|
11508 | 4688 |
] 421 |
4689 |
" |
|
13021 | 4690 |
|
4691 |
"Modified: / 17-08-2010 / 17:27:33 / cg" |
|
11508 | 4692 |
! |
4693 |
||
4694 |
primeFactorsUpTo:limitArgOrNil |
|
4695 |
"return a collection of prime factors of the receiver. |
|
4696 |
For prime numbers, an empty collection is returned. |
|
11587 | 4697 |
Can take a long time for big numbers |
13021 | 4698 |
(win a nobel price, if you find something quick (*) |
4699 |
||
4700 |
(*):which does not mean that the code below is optimal - far from it !!" |
|
11508 | 4701 |
|
18865 | 4702 |
"See comment in initializePrimeCacheUpTo:limit" |
4703 |
||
19128 | 4704 |
|rest n factors limit wellKnownPrimes lastPrime checkThisFactor nextTry| |
11496 | 4705 |
|
4706 |
factors := Bag new. |
|
4707 |
rest := self. |
|
11508 | 4708 |
limit := (rest // 2). |
4709 |
limitArgOrNil notNil ifTrue:[ |
|
19078 | 4710 |
limit := limit min:limitArgOrNil. |
11508 | 4711 |
]. |
11496 | 4712 |
|
4713 |
"/ try to get the number down fast: |
|
4714 |
n := rest lowBit. |
|
4715 |
n ~~ 1 ifTrue:[ |
|
19078 | 4716 |
self == 2 ifTrue:[^ #() ]. |
4717 |
factors add:2 withOccurrences:(n-1). |
|
4718 |
rest := rest rightShift:(n-1). |
|
11496 | 4719 |
]. |
4720 |
||
20269 | 4721 |
checkThisFactor := |
19128 | 4722 |
[:prime | |
4723 |
(prime*prime) > rest ifTrue:[ |
|
19078 | 4724 |
(rest ~~ 1 and:[factors notEmpty]) ifTrue:[ factors add:rest ]. |
4725 |
^ factors. |
|
4726 |
]. |
|
4727 |
||
4728 |
[rest \\ prime == 0] whileTrue:[ |
|
4729 |
factors add:prime. |
|
4730 |
rest := rest // prime. |
|
4731 |
rest == 1 ifTrue:[^ factors]. |
|
4732 |
]. |
|
4733 |
lastPrime := prime. |
|
4734 |
]. |
|
11496 | 4735 |
|
20269 | 4736 |
wellKnownPrimes := Integer primesUpTo5000. |
19128 | 4737 |
limit <= (wellKnownPrimes last+1) ifTrue:[ |
4738 |
wellKnownPrimes do:checkThisFactor. |
|
19078 | 4739 |
^ factors |
11496 | 4740 |
]. |
4741 |
||
4742 |
"/ actually, all of the code is duplicated; once for primes from a table, |
|
4743 |
"/ and then primes as generated on the fly. The prime generation involves a prime-test, |
|
4744 |
"/ which may slow things down quite a bit. |
|
4745 |
"/ (the primesUpTo uses a faster sieve, but can only represent primes to upTo (say) |
|
13022 | 4746 |
"/ a few millions). |
11496 | 4747 |
|
11938 | 4748 |
Integer primesUpTo:(limit min:(1000000 max:Integer primeCacheSize)) do:checkThisFactor. |
11496 | 4749 |
|
4750 |
nextTry := lastPrime + 2. |
|
4751 |
[ nextTry <= limit ] whileTrue:[ |
|
19078 | 4752 |
"/ now, we are beyond the list of pre-generated primes. |
4753 |
"/ change our strategy to: see if it divides an odd number; |
|
4754 |
"/ if so, add the divisor's prime factors. |
|
19128 | 4755 |
(nextTry*nextTry) > rest ifTrue:[ |
19078 | 4756 |
(rest ~~ 1 and:[factors notEmpty]) ifTrue:[ factors add:rest ]. |
4757 |
^ factors. |
|
4758 |
]. |
|
4759 |
[(rest \\ nextTry) == 0] whileTrue:[ |
|
4760 |
"/ can only happen relatively late after the last prime, |
|
4761 |
"/ because otherwise, the primeFactors of nextTry would already have |
|
4762 |
"/ been found as divisors. |
|
4763 |
"/ first chance is: (lastPrime + 2) squared |
|
4764 |
nextTry < lastPrime squared ifTrue:[ |
|
4765 |
"/ nextTry is a prime !! |
|
4766 |
factors add:nextTry |
|
4767 |
] ifFalse:[ |
|
4768 |
factors addAll:(nextTry primeFactors). |
|
4769 |
]. |
|
4770 |
rest := rest // nextTry. |
|
4771 |
]. |
|
4772 |
nextTry := nextTry + 2. |
|
11496 | 4773 |
]. |
4774 |
^ factors |
|
4775 |
||
4776 |
" |
|
4777 |
2 to:10000 do:[:n | |
|
19078 | 4778 |
self assert:((n isPrime and:[ n primeFactors isEmpty]) |
4779 |
or:[ n isPrime not and:[n primeFactors product = n]]) |
|
11496 | 4780 |
] |
4781 |
3 to:10000 do:[:n | |
|
19078 | 4782 |
self assert:(n factorial primeFactors product = n factorial) |
11496 | 4783 |
] |
4784 |
||
11587 | 4785 |
13195 primeFactors |
4786 |
12 primeFactors |
|
4787 |
2 primeFactors |
|
4788 |
3 primeFactors |
|
4789 |
5 primeFactors |
|
4790 |
14 primeFactors |
|
4791 |
13423453625634765 primeFactors |
|
4792 |
13423453625634765 isPrime |
|
4793 |
13423453625634765 gcd:(3 * 5 * 19 * 29) |
|
4794 |
13423453625634765 / 8265 |
|
4795 |
1624132320101 isPrime |
|
11496 | 4796 |
1624132320101 gcd: 8265 |
4797 |
||
11587 | 4798 |
1000000 primeFactors |
4799 |
100000000 primeFactors |
|
4800 |
1000000000 primeFactors |
|
11496 | 4801 |
|
4802 |
Time millisecondsToRun:[ |
|
19078 | 4803 |
1000 timesRepeat:[ |
4804 |
10000000000000000000000000000000000000 primeFactors |
|
4805 |
] |
|
11496 | 4806 |
] 421 |
4807 |
" |
|
13021 | 4808 |
|
13022 | 4809 |
"Modified: / 17-08-2010 / 17:28:05 / cg" |
11496 | 4810 |
! |
4811 |
||
4973 | 4812 |
raisedTo:exp mod:mod |
12280 | 4813 |
"return the modulo (remainder) of |
4973 | 4814 |
the receiver raised to exp (an Integer) and mod (another Integer)" |
4815 |
||
11587 | 4816 |
|result m t |
19874 | 4817 |
eI "{Class: SmallInteger}" |
5816 | 4818 |
bits "{Class: SmallInteger}"| |
4973 | 4819 |
|
4820 |
"use the addition chaining algorithm" |
|
4821 |
||
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4822 |
exp == 0 ifTrue:[ |
19078 | 4823 |
^ 1 |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4824 |
]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4825 |
exp == 1 ifTrue:[ |
19078 | 4826 |
mod isNumber ifTrue:[ |
4827 |
^ self \\ mod. |
|
4828 |
] |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4829 |
]. |
4973 | 4830 |
exp negative ifTrue:[ |
19078 | 4831 |
^ self class |
4832 |
raise:#domainErrorSignal |
|
4833 |
receiver:self |
|
4834 |
selector:#raisedTo:mod: |
|
4835 |
arguments:(Array with:exp with:mod) |
|
4836 |
errorString:'modulo arithmethic with negative exponent' |
|
4973 | 4837 |
]. |
4838 |
||
4839 |
m := mod asModuloNumber. |
|
4840 |
||
4841 |
result := 1. |
|
4842 |
t := m modulusOf:self. |
|
4843 |
||
19874 | 4844 |
(exp class == SmallInteger) ifTrue:[ |
4845 |
eI := exp. |
|
4846 |
[eI ~~ 0] whileTrue:[ |
|
4847 |
(eI bitAnd:1) == 1 ifTrue:[ |
|
4848 |
result := m modulusOf:(result * t). |
|
4849 |
]. |
|
4850 |
eI := eI bitShift:-1. |
|
4851 |
eI ~~ 0 ifTrue:[ |
|
4852 |
t := m modulusOf:(t * t). |
|
4853 |
]. |
|
4854 |
]. |
|
4855 |
^ result. |
|
4856 |
]. |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4857 |
|
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4858 |
bits := exp highBit. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4859 |
|
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4860 |
1 to:bits-1 do:[:i| |
19078 | 4861 |
(exp bitAt:i) == 1 ifTrue:[ |
4862 |
result := m modulusOf:(result * t). |
|
4863 |
]. |
|
4864 |
t := m modulusOf:(t * t). |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4865 |
]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4866 |
result := m modulusOf:(result * t). |
4973 | 4867 |
|
4868 |
^ result |
|
4869 |
||
11587 | 4870 |
" |
22561 | 4871 |
Time millisecondsToRun: [100000 timesRepeat: [12345678907 raisedTo: 3 modulo: 12345678917]] |
4872 |
||
11587 | 4873 |
2 raisedTo:2 mod:3 |
4973 | 4874 |
20000000000000 raisedTo:200 mod:190 -> 30 |
11587 | 4875 |
(20000000000000 raisedTo:200) \\ 190 |
4973 | 4876 |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4877 |
Time millisecondsToRun:[10000 timesRepeat:[ |
19874 | 4878 |
200000000000000000000000 raisedTo:65537 mod:1900000000000000000000000 |
19078 | 4879 |
] |
4880 |
] |
|
4973 | 4881 |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4882 |
Time millisecondsToRun:[1000 timesRepeat:[ |
19874 | 4883 |
(200000000000000000000000 raisedTo:65537) \\ 1900000000000000000000000 |
19078 | 4884 |
] |
4885 |
] |
|
4973 | 4886 |
" |
4887 |
||
4888 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
|
4889 |
"Modified: / 5.5.1999 / 11:18:20 / stefan" |
|
6195 | 4890 |
"Modified: / 16.11.2001 / 14:15:21 / cg" |
4973 | 4891 |
! |
4892 |
||
4893 |
raisedToCrtModP:p q:q ep:ep eq:eq u:u |
|
4894 |
"Application of the Chinese Remainder Theorem (CRT). |
|
4895 |
||
4896 |
This is a faster modexp for moduli with a known factorisation into two |
|
4897 |
relatively prime factors p and q, and an input relatively prime to the |
|
4898 |
modulus, the Chinese Remainder Theorem to do the computation mod p and |
|
4899 |
mod q, and then combine the results. This relies on a number of |
|
4900 |
precomputed values, but does not actually require the modulus n or the |
|
4901 |
exponent e. |
|
4902 |
||
4903 |
expout = expin ^ e mod (p*q). |
|
4904 |
We form this by evaluating |
|
4905 |
p2 = (expin ^ e) mod p and |
|
4906 |
q2 = (expin ^ e) mod q |
|
4907 |
and then combining the two by the CRT. |
|
4908 |
||
4909 |
Two optimisations of this are possible. First, we can reduce expin |
|
4910 |
modulo p and q before starting. |
|
4911 |
||
4912 |
Second, since we know the factorisation of p and q (trivially derived |
|
4913 |
from the factorisation of n = p*q), and expin is relatively prime to |
|
4914 |
both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m), |
|
4915 |
to throw away multiples of phi(p) or phi(q) in e. |
|
4916 |
Letting ep = e mod phi(p) and |
|
19078 | 4917 |
eq = e mod phi(q) |
4973 | 4918 |
then combining these two speedups, we only need to evaluate |
4919 |
p2 = ((expin mod p) ^ ep) mod p and |
|
4920 |
q2 = ((expin mod q) ^ eq) mod q. |
|
4921 |
||
4922 |
Now we need to apply the CRT. Starting with |
|
4923 |
expout = p2 (mod p) and |
|
4924 |
expout = q2 (mod q) |
|
4925 |
we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p, |
|
4926 |
then 0 <= expout < p*q for some 0 <= k < q. Since we want expout = q2 |
|
4927 |
(mod q), then p*k = q2-p2 (mod q). Since p and q are relatively prime, |
|
4928 |
p has a multiplicative inverse u mod q. In other words, u = 1/p (mod q). |
|
4929 |
||
4930 |
Multiplying by u on both sides gives k = u*(q2-p2) (mod q). |
|
4931 |
Since we want 0 <= k < q, we can thus find k as |
|
4932 |
k = (u * (q2-p2)) mod q. |
|
4933 |
||
4934 |
Once we have k, evaluating p2 + p * k is easy, and |
|
4935 |
that gives us the result |
|
11587 | 4936 |
" |
4973 | 4937 |
|
4938 |
|result t mp mq| |
|
4939 |
||
4940 |
mp := p asModuloNumber. |
|
19874 | 4941 |
result := self raisedTo:ep mod:mp. |
4973 | 4942 |
|
4943 |
mq := q asModuloNumber. |
|
19874 | 4944 |
t := self raisedTo:eq mod:mq. |
4973 | 4945 |
|
4946 |
"now p2 is in result, q2 in t" |
|
4947 |
||
19231 | 4948 |
t := t - result. |
4949 |
t negative ifTrue:[ |
|
19078 | 4950 |
t := t + q. |
4973 | 4951 |
]. |
19231 | 4952 |
t := t * u. |
4973 | 4953 |
t := mq modulusOf:t. |
19231 | 4954 |
t := t * p. |
4955 |
result := result + t. |
|
4973 | 4956 |
|
4957 |
^ result. |
|
4958 |
||
4959 |
"Created: / 30.4.1999 / 15:53:15 / stefan" |
|
4960 |
"Modified: / 5.5.1999 / 11:01:15 / stefan" |
|
1 | 4961 |
! ! |
4962 |
||
4963 |
!Integer methodsFor:'printing & storing'! |
|
4964 |
||
16038 | 4965 |
asBCD |
4966 |
"return an integer which represents the BCD encoded value of the receiver; |
|
4967 |
that is: each digit of its decimal representation is placed into a nibble |
|
4968 |
of the result. (aka 162 -> 0x162). |
|
4969 |
This conversion is useful for some communication protocols, |
|
16040 | 4970 |
or control systems, which represent numbers this way... |
4971 |
This fallback code is not particularily tuned or optimized for speed." |
|
16038 | 4972 |
|
4973 |
|rest twoDigits hi lo shift out| |
|
4974 |
||
4975 |
out := 0. shift := 0. |
|
4976 |
rest := self. |
|
4977 |
[rest > 0] whileTrue:[ |
|
19078 | 4978 |
twoDigits := rest \\ 100. |
4979 |
rest := rest // 100. |
|
4980 |
hi := twoDigits // 10. |
|
4981 |
lo := twoDigits \\ 10. |
|
4982 |
out := out bitOr:(((hi bitShift:4)+lo) bitShift:shift). |
|
4983 |
shift := shift + 8. |
|
16038 | 4984 |
]. |
4985 |
||
4986 |
^ out |
|
4987 |
||
4988 |
" |
|
4989 |
(100 factorial) asBCD |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4990 |
999999999 asBCD |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4991 |
100000000 asBCD |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4992 |
123456789 asBCD |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4993 |
99999999 asBCD |
16038 | 4994 |
12345678 asBCD |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4995 |
12345678 asBCD |
16038 | 4996 |
12345678 asBCD hexPrintString |
4997 |
12345678901234567890 asBCD |
|
4998 |
" |
|
4999 |
! |
|
5000 |
||
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5001 |
asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5002 |
"return a byteArray containing the receiver in BCD encoding. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5003 |
The byteArray will contain the BCD encoded decimal string, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5004 |
starting with the most significant digits first. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5005 |
This conversion is useful for some communication protocols, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5006 |
or control systems, which represent big numbers this way... |
16038 | 5007 |
This is not particularily tuned or optimized for speed." |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5008 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5009 |
|s rest twoDigits hi lo| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5010 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5011 |
self == 0 ifTrue:[ |
19078 | 5012 |
^ #[ 16r00 ] |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5013 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5014 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5015 |
"/ a very rough estimate on the final size ... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5016 |
s := (ByteArray new:(self digitLength * 2)) writeStream. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5017 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5018 |
rest := self. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5019 |
[rest > 0] whileTrue:[ |
19078 | 5020 |
twoDigits := rest \\ 100. |
5021 |
rest := rest // 100. |
|
5022 |
hi := twoDigits \\ 10. |
|
5023 |
lo := twoDigits // 10. |
|
5024 |
s nextPut:(lo bitShift:4)+hi |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5025 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5026 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5027 |
^ s contents reverse |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5028 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5029 |
" |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5030 |
12345678 asBCDBytes |
16038 | 5031 |
12345678 asBCDBytes hexPrintString |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5032 |
12345678901234567890 asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5033 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5034 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5035 |
|
701 | 5036 |
errorPrintHex |
5037 |
"print the receiver as a hex number on the standard error stream" |
|
5038 |
||
5039 |
(self printStringRadix:16) errorPrint |
|
5040 |
! |
|
1 | 5041 |
|
701 | 5042 |
hexPrintString |
23382 | 5043 |
"return a hex string representation of the receiver. |
5044 |
Notice: this is not padded in any way" |
|
701 | 5045 |
|
5046 |
^ self printStringRadix:16 |
|
3874 | 5047 |
|
5048 |
" |
|
23382 | 5049 |
9 hexPrintString |
3874 | 5050 |
127 hexPrintString |
5051 |
-1 hexPrintString |
|
5052 |
" |
|
5053 |
||
23382 | 5054 |
"Modified: / 11-10-1998 / 01:15:43 / cg" |
5055 |
"Modified (comment): / 23-09-2018 / 03:48:57 / Claus Gittinger" |
|
701 | 5056 |
! |
5057 |
||
2408 | 5058 |
hexPrintString:size |
5059 |
"return a hex string representation of the receiver, |
|
5060 |
padded to size characters" |
|
5061 |
||
5799 | 5062 |
"/ ^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
5063 |
^ self printStringRadix:16 size:size fill:$0 |
|
5064 |
||
5065 |
" |
|
11587 | 5066 |
12345 hexPrintString:4 |
5067 |
123 hexPrintString:4 |
|
5799 | 5068 |
" |
2408 | 5069 |
|
5070 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
5071 |
! |
|
5072 |
||
701 | 5073 |
printHex |
5074 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 5075 |
|
701 | 5076 |
(self printStringRadix:16) print |
21938 | 5077 |
|
5078 |
" |
|
5079 |
16rAFFE printStringRadix:16 |
|
5080 |
" |
|
5081 |
||
5082 |
"Modified (comment): / 02-07-2017 / 10:20:59 / cg" |
|
701 | 5083 |
! |
5084 |
||
20140 | 5085 |
printOn:aStream base:base showRadix:showRadix |
5988 | 5086 |
"append a string representation of the receiver in the specified numberBase to aStream |
5087 |
(if showRadix is true, with initial XXr) |
|
20140 | 5088 |
The base argument should be between 2 and 36. |
5089 |
If it is negative, digits > 9 are printed as lowecase a-z." |
|
5090 |
||
5091 |
|absBase num s divMod mod r r2 r4 nD numN string| |
|
5092 |
||
5093 |
(base isInteger and:[absBase := base abs. absBase between:2 and:36]) ifFalse:[ |
|
5094 |
ConversionError raiseRequestWith:self errorString:' - invalid base: ', absBase printString. |
|
5095 |
absBase := 10. |
|
54 | 5096 |
]. |
5097 |
||
5988 | 5098 |
showRadix ifTrue:[ |
20140 | 5099 |
absBase printOn:aStream. |
19078 | 5100 |
aStream nextPut:$r. |
5988 | 5101 |
]. |
5102 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5103 |
(self = 0) ifTrue:[aStream nextPut:$0. ^ self]. |
20140 | 5104 |
self negative ifTrue:[ |
19078 | 5105 |
aStream nextPut:$- . |
5106 |
num := self negated. |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5107 |
] ifFalse:[ |
19078 | 5108 |
num := self. |
1 | 5109 |
]. |
92 | 5110 |
|
5111 |
" |
|
5112 |
claus: changed it from a recursive algorithm; |
|
18851 | 5113 |
(it used to trigger stack-overflow exceptions when printing 3000 factorial ...) |
92 | 5114 |
" |
18865 | 5115 |
"/ leftPart := num // base. |
5116 |
"/ (leftPart ~= 0) ifTrue:[ |
|
5117 |
"/ leftPart printOn:aStream base:base. |
|
5118 |
"/ aStream nextPut:(Character digitValue:(num \\ base). |
|
5119 |
"/ ^ self |
|
5120 |
"/ ]. |
|
5121 |
"/ aStream nextPut:(Character digitValue:num). |
|
2951 | 5122 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5123 |
"/ instead of computing the quotient and remainder |
3136
91dbf1ea13a6
print in junks of 6 digits (instead of 8 digits)
Claus Gittinger <cg@exept.de>
parents:
3135
diff
changeset
|
5124 |
"/ against radix, do it in junks of 5 or 6 digits. |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5125 |
"/ This reduces the number of LargeInt-divisions |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5126 |
"/ by that factor (turning them into smallInt divisions |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5127 |
"/ within that junk) and speeds up the conversions noticably. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
5128 |
|
20140 | 5129 |
r2 := absBase*absBase. "/ radix^2 |
18851 | 5130 |
r4 := r2*r2. "/ radix^4 |
20140 | 5131 |
absBase <= 10 ifTrue:[ |
19078 | 5132 |
r := r4*r2. "/ radix^6 (chunks of 6 digits) |
5133 |
nD := 6. |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5134 |
] ifFalse:[ |
20140 | 5135 |
r := r4*absBase. "/ radix^5 (chunks of 5 digits) |
19078 | 5136 |
nD := 5. |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5137 |
]. |
18851 | 5138 |
SmallInteger maxBits >= 63 ifTrue:[ |
24331 | 5139 |
r := r*r2. "/ radix^8 or radix^7 (chunks of 8 or 7 digits) |
19159 | 5140 |
nD := nD + 2. |
18851 | 5141 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5142 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5143 |
"get a Stream with space for the digits we are going to print. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5144 |
We need ((num log:base) ceiling) digits, which is equivalent |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5145 |
to ((num log:2)/(base log:2) ceiling) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5146 |
" |
18851 | 5147 |
s := WriteStream on:(String new:10). |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5148 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
5149 |
[num >= r] whileTrue:[ |
19078 | 5150 |
"/ |
5151 |
"/ chop off nD digits. |
|
5152 |
"/ |
|
5153 |
divMod := num divMod:r. |
|
5154 |
num := divMod at:1. |
|
5155 |
numN := divMod at:2. |
|
5156 |
||
5157 |
"/ process them |
|
5158 |
nD timesRepeat:[ |
|
20140 | 5159 |
divMod := numN divMod:absBase. |
19078 | 5160 |
numN := divMod at:1. |
5161 |
mod := divMod at:2. |
|
5162 |
s nextPut:(Character digitValue:mod). |
|
5163 |
]. |
|
2951 | 5164 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
5165 |
|
19089 | 5166 |
[num ~~ 0] whileTrue:[ |
20140 | 5167 |
divMod := num divMod:absBase. |
19078 | 5168 |
num := divMod at:1. |
5169 |
mod := divMod at:2. |
|
5170 |
s nextPut:(Character digitValue:mod). |
|
1 | 5171 |
]. |
20140 | 5172 |
string := s contents reverse. |
5173 |
base < 0 ifTrue:[ |
|
5174 |
string := string asLowercase. |
|
5175 |
]. |
|
5176 |
aStream nextPutAll:string. |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5177 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5178 |
" |
19078 | 5179 |
3000 factorial printOn:Transcript base:10 |
5180 |
10 printOn:Transcript base:3 |
|
5181 |
31 printOn:Transcript base:3 |
|
5182 |
10 printOn:Transcript base:2 |
|
5183 |
31 printOn:Transcript base:2 |
|
20140 | 5184 |
-28 printOn:Transcript base:16 |
5185 |
-28 printOn:Transcript base:-16 |
|
19078 | 5186 |
-20 printOn:Transcript base:10 |
19159 | 5187 |
Time millisecondsToRun:[10000 factorial printString] |
5188 |
'%012d' printf:{ (2 raisedTo:20) } |
|
12976
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
5189 |
" |
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
5190 |
|
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
5191 |
"Modified: / 20-01-1998 / 18:05:02 / stefan" |
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
5192 |
"Created: / 07-09-2001 / 13:51:33 / cg" |
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
5193 |
"Modified: / 02-08-2010 / 12:24:14 / cg" |
24331 | 5194 |
"Modified (comment): / 11-06-2019 / 00:07:05 / Claus Gittinger" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5195 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5196 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5197 |
printOn:aStream base:baseInteger size:sz |
15775 | 5198 |
"print a string representation of the receiver in the specified |
5199 |
base. The string is padded on the left with fillCharacter to make |
|
5200 |
its size as specified in sz." |
|
5201 |
||
5202 |
self printOn:aStream base:baseInteger size:sz fill:$0 |
|
5203 |
||
5204 |
" |
|
20268 | 5205 |
1024 printOn:Transcript base:16 size:4. |
15775 | 5206 |
1024 printOn:Transcript base:2 size:16. |
5207 |
1024 printOn:Transcript base:16 size:8. |
|
5208 |
" |
|
5209 |
! |
|
5210 |
||
7173 | 5211 |
printOn:aStream base:baseInteger size:sz fill:fillCharacter |
5212 |
"print a string representation of the receiver in the specified |
|
5213 |
base. The string is padded on the left with fillCharacter to make |
|
5214 |
its size as specified in sz." |
|
5215 |
||
20140 | 5216 |
|string actualSize| |
5217 |
||
5218 |
string := self printStringRadix:baseInteger. |
|
7173 | 5219 |
actualSize := string size. |
5220 |
actualSize < sz ifTrue:[ |
|
19078 | 5221 |
aStream next:sz-actualSize put:fillCharacter. |
7173 | 5222 |
]. |
5223 |
aStream nextPutAll:string. |
|
5224 |
||
5225 |
" |
|
20269 | 5226 |
1024 printOn:Transcript base:16 size:4 fill:$0. |
7173 | 5227 |
1024 printOn:Transcript base:2 size:16 fill:$. |
5228 |
1024 printOn:Transcript base:16 size:8 fill:Character space. |
|
5229 |
" |
|
5230 |
! |
|
5231 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5232 |
printOn:aStream radix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5233 |
"append a printed description of the receiver to aStream. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5234 |
The receiver is printed in radix base (instead of the default, 10). |
5988 | 5235 |
This method is obsoleted by #printOn:base:, which is ST-80 & ANSI compatible." |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5236 |
|
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
5237 |
<resource: #obsolete> |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
5238 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5239 |
self printOn:aStream base:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5240 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5241 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
5988 | 5242 |
"Modified: / 7.9.2001 / 13:58:29 / cg" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5243 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
5244 |
|
6635 | 5245 |
printRomanOn:aStream |
6673 | 5246 |
"print the receiver as roman number to the receiver, aStream. |
5247 |
This converts correct (i.e. prefix notation for 4,9,40,90, etc.)." |
|
5248 |
||
5249 |
^ self printRomanOn:aStream naive:false |
|
6635 | 5250 |
|
5251 |
" |
|
6673 | 5252 |
1 to:10 do:[:i | i printRomanOn:Transcript. Transcript cr.]. |
6635 | 5253 |
1999 printRomanOn:Transcript. Transcript cr. |
5254 |
Date today year printRomanOn:Transcript. Transcript cr. |
|
5255 |
" |
|
5256 |
||
5257 |
"test all between 1 and 9999: |
|
5258 |
1 to:9999 do:[:n | |
|
19078 | 5259 |
|romanString| |
5260 |
||
5261 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
5262 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 5263 |
] |
5264 |
" |
|
5265 |
! |
|
5266 |
||
5267 |
printRomanOn:aStream naive:naive |
|
5268 |
"print the receiver as roman number to the receiver, aStream. |
|
5269 |
The naive argument controls if the conversion is |
|
6682 | 5270 |
correct (i.e. subtracting prefix notation for 4,9,40,90, etc.), |
5271 |
or naive (i.e. print 4 as IIII and 9 as VIIII); also called simple. |
|
6673 | 5272 |
The naive version is often used for page numbers in documents." |
5273 |
||
5274 |
|restValue spec| |
|
5275 |
||
5276 |
restValue := self. |
|
5277 |
restValue > 0 ifFalse:[self error:'negative roman']. |
|
5278 |
||
5279 |
naive ifTrue:[ |
|
19078 | 5280 |
spec := #( |
5281 |
" value string repeat " |
|
5282 |
1000 'M' true |
|
5283 |
500 'D' false |
|
5284 |
100 'C' true |
|
5285 |
50 'L' false |
|
5286 |
10 'X' true |
|
5287 |
5 'V' false |
|
5288 |
1 'I' true |
|
5289 |
). |
|
6673 | 5290 |
] ifFalse:[ |
19078 | 5291 |
spec := #( |
5292 |
" value string repeat " |
|
5293 |
1000 'M' true |
|
5294 |
900 'CM' false |
|
5295 |
500 'D' false |
|
5296 |
400 'CD' false |
|
5297 |
100 'C' true |
|
5298 |
90 'XC' false |
|
5299 |
50 'L' false |
|
5300 |
40 'XL' false |
|
5301 |
10 'X' true |
|
5302 |
9 'IX' false |
|
5303 |
5 'V' false |
|
5304 |
4 'IV' false |
|
5305 |
1 'I' true |
|
5306 |
). |
|
6673 | 5307 |
]. |
5308 |
||
11587 | 5309 |
spec |
19078 | 5310 |
inGroupsOf:3 |
5311 |
do:[:rValue :rString :repeatFlag | |
|
5312 |
||
5313 |
[ |
|
5314 |
(restValue >= rValue) ifTrue:[ |
|
5315 |
aStream nextPutAll:rString. |
|
5316 |
restValue := restValue - rValue. |
|
5317 |
]. |
|
5318 |
] doWhile:[ repeatFlag and:[ restValue >= rValue] ]. |
|
5319 |
]. |
|
6673 | 5320 |
|
5321 |
" |
|
5322 |
1 to:10 do:[:i | i printRomanOn:Transcript naive:false. Transcript cr.]. |
|
5323 |
1 to:10 do:[:i | i printRomanOn:Transcript naive:true. Transcript cr.]. |
|
5324 |
||
5325 |
1999 printRomanOn:Transcript. Transcript cr. |
|
5326 |
Date today year printRomanOn:Transcript. Transcript cr. |
|
5327 |
" |
|
5328 |
||
5329 |
"test all between 1 and 9999: |
|
5330 |
1 to:9999 do:[:n | |
|
19078 | 5331 |
|romanString| |
5332 |
||
5333 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:false]. |
|
5334 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 5335 |
] |
5336 |
" |
|
5337 |
||
5338 |
"test naive all between 1 and 9999: |
|
5339 |
1 to:9999 do:[:n | |
|
19078 | 5340 |
|romanString| |
5341 |
||
5342 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:true]. |
|
5343 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6635 | 5344 |
] |
5345 |
" |
|
5346 |
! |
|
5347 |
||
1 | 5348 |
printStringRadix:aRadix size:sz fill:fillCharacter |
5349 |
"return a string representation of the receiver in the specified |
|
5350 |
radix. The string is padded on the left with fillCharacter to make |
|
5351 |
its size as specified in sz." |
|
5352 |
||
2951 | 5353 |
|s actualSize| |
1 | 5354 |
|
5355 |
s := self printStringRadix:aRadix. |
|
2951 | 5356 |
actualSize := s size. |
5357 |
actualSize < sz ifTrue:[ |
|
23980 | 5358 |
^ (String new:(sz - actualSize) withAll:fillCharacter) , s |
1 | 5359 |
]. |
5360 |
^ s |
|
5361 |
||
92 | 5362 |
" |
20269 | 5363 |
1024 printStringRadix:16 size:4 fill:$0. |
92 | 5364 |
1024 printStringRadix:2 size:16 fill:$. |
5365 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
5366 |
" |
|
23980 | 5367 |
|
5368 |
"Modified: / 26-03-2019 / 11:54:52 / Claus Gittinger" |
|
216 | 5369 |
! |
5370 |
||
6635 | 5371 |
romanPrintString |
5372 |
"return a roman number representation of the receiver as a string" |
|
5373 |
||
5374 |
^ String streamContents:[:stream | self printRomanOn:stream]. |
|
5375 |
||
5376 |
" |
|
11587 | 5377 |
1999 romanPrintString. |
6635 | 5378 |
Date today year romanPrintString. |
5379 |
" |
|
1 | 5380 |
! ! |
5381 |
||
23496 | 5382 |
!Integer methodsFor:'private'! |
5383 |
||
5384 |
numberOfDigits:n8BitDigits |
|
5385 |
"initialize the instance to store n8BitDigits" |
|
5386 |
||
5387 |
^ self subclassResponsibility. |
|
5388 |
||
5389 |
"Created: / 01-11-2018 / 11:28:56 / Stefan Vogel" |
|
5390 |
! |
|
5391 |
||
5392 |
numberOfDigits:n8BitDigits sign:newSign |
|
5393 |
"initialize the instance to store n8BitDigits and sign" |
|
5394 |
||
5395 |
^ self subclassResponsibility. |
|
5396 |
||
5397 |
"Created: / 01-11-2018 / 12:12:56 / Stefan Vogel" |
|
5398 |
! |
|
5399 |
||
5400 |
setSign:aNumber |
|
5401 |
"private: for protocol completeness with LargeIntegers. |
|
5402 |
Returns a smallInteger with my absValue and the sign of the argument. |
|
5403 |
The method's name may be misleading: the receiver is not changed, |
|
5404 |
but a new number is returned." |
|
5405 |
||
5406 |
|absVal| |
|
5407 |
||
5408 |
aNumber == 0 ifTrue:[ |
|
5409 |
^ 0 |
|
5410 |
]. |
|
5411 |
absVal := self abs. |
|
5412 |
aNumber < 0 ifTrue:[ |
|
5413 |
^ absVal negated |
|
5414 |
]. |
|
5415 |
^ absVal |
|
5416 |
||
5417 |
" |
|
5418 |
-4 sign:-1 |
|
5419 |
-4 sign:0 |
|
5420 |
-4 sign:1 |
|
5421 |
-4 sign:-1 |
|
5422 |
-4 sign:0 |
|
5423 |
-4 sign:1 |
|
5424 |
" |
|
5425 |
||
5426 |
"Modified (format): / 01-11-2018 / 12:22:48 / Stefan Vogel" |
|
5427 |
! |
|
5428 |
||
5429 |
sign:aNumber |
|
5430 |
<resource: #obsolete> |
|
5431 |
"destructively change the sign of the receiver" |
|
5432 |
||
5433 |
^ self setSign:aNumber |
|
5434 |
! ! |
|
5435 |
||
701 | 5436 |
!Integer methodsFor:'queries'! |
202 | 5437 |
|
701 | 5438 |
digitAt:n |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5439 |
"return the n-th byte of the binary representation." |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5440 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5441 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5442 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5443 |
"This is a very stupid implementation, and should be redefined in |
701 | 5444 |
concrete subclasses." |
202 | 5445 |
|
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5446 |
"/ |num count| |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5447 |
"/ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5448 |
"/ num := self. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5449 |
"/ count := n. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5450 |
"/ [count > 1] whileTrue:[ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5451 |
"/ num := num // 256. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5452 |
"/ count := count - 1 |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5453 |
"/ ]. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5454 |
"/ ^ num \\ 256 |
159 | 5455 |
|
5456 |
" |
|
11587 | 5457 |
16r44332211 digitAt:1 |
5458 |
16r44332211 digitAt:2 |
|
5459 |
16r44332211 digitAt:3 |
|
5460 |
16r44332211 digitAt:4 |
|
5461 |
16r44332211 digitAt:5 |
|
5462 |
16r00332211 digitAt:4 |
|
5463 |
16r00332211 digitAt:5 |
|
159 | 5464 |
" |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5465 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5466 |
"Modified: / 26.9.2001 / 21:21:21 / cg" |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5467 |
! |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5468 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5469 |
digitByteAt:n |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5470 |
"return 8 bits of my signed value, starting at byte index. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5471 |
For positive receivers, this is the same as #digitAt:; |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5472 |
for negative ones, the actual bit representation is returned." |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5473 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5474 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5475 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5476 |
"Created: / 26.9.2001 / 21:18:43 / cg" |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
5477 |
"Modified: / 26.9.2001 / 21:20:19 / cg" |
1 | 5478 |
! |
5479 |
||
701 | 5480 |
digitLength |
19319 | 5481 |
"return the number of bytes needed for the unsigned binary representation of the receiver. |
5482 |
For negative receivers, the result is not defined by the language standard. |
|
20269 | 5483 |
This method is redefined in concrete classes |
19319 | 5484 |
- the fallback here is actually never used." |
701 | 5485 |
|
5486 |
^ (self log:256) ceiling asInteger |
|
2816 | 5487 |
|
5488 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 5489 |
! |
5490 |
||
13725 | 5491 |
exponent |
24968 | 5492 |
"return what would be the normalized float's (unbiased) exponent if I were a float. |
13725 | 5493 |
This is not for general use - it has been added for dolphin (soap) compatibility. |
24968 | 5494 |
This assumes that a float's mantissa is normalized to |
13725 | 5495 |
0.5 .. 1.0 and the number's value is: mantissa * 2^exp" |
5496 |
||
5497 |
^ self abs highBit |
|
5498 |
||
5499 |
" |
|
24968 | 5500 |
16 mantissa * (2 raisedTo:16 exponent) |
5501 |
||
13725 | 5502 |
self assert:( 1.0 exponent = 1 exponent ). |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5503 |
self assert:( 2.0 exponent = 2 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5504 |
self assert:( 3.0 exponent = 3 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5505 |
self assert:( 4.0 exponent = 4 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5506 |
self assert:( 12345.0 exponent = 12345 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5507 |
self assert:( 0.0 exponent = 0 exponent ). |
13725 | 5508 |
|
5509 |
self assert:( -1.0 exponent = -1 exponent ). |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5510 |
self assert:( -2.0 exponent = -2 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5511 |
self assert:( -3.0 exponent = -3 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5512 |
self assert:( -4.0 exponent = -4 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5513 |
self assert:( -12345.0 exponent = -12345 exponent ). |
13725 | 5514 |
" |
5515 |
! |
|
5516 |
||
701 | 5517 |
isInteger |
5518 |
"return true, if the receiver is some kind of integer number" |
|
5519 |
||
5520 |
^ true |
|
3036 | 5521 |
! |
5522 |
||
13815 | 5523 |
isLiteral |
5524 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
5525 |
(i.e. can be used in constant arrays)" |
|
5526 |
||
5527 |
^ true |
|
5528 |
! |
|
5529 |
||
18865 | 5530 |
isPerfectSquare |
5531 |
"return true if I am a perfect square. |
|
5532 |
That is a number for which the square root is an integer." |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5533 |
|
24868 | 5534 |
|intSqrt realSqrt realSqrtTruncated| |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5535 |
|
18865 | 5536 |
self strictlyPositive ifFalse:[ |
19078 | 5537 |
self == 0 ifTrue:[^ true]. |
5538 |
"/ should we raise a domain error for negative receivers? |
|
5539 |
^ false |
|
18865 | 5540 |
]. |
5541 |
||
5542 |
"/ q&d check for common small squares |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5543 |
self < 1024 ifTrue:[ |
25039 | 5544 |
self < 400 ifTrue:[ |
5545 |
self <= 100 ifTrue:[ |
|
5546 |
^ #(1 4 9 16 25 36 49 64 81 100) includes:self. |
|
5547 |
]. |
|
5548 |
^ #(121 144 169 196 225 256 289 324 361) includes:self. |
|
5549 |
]. |
|
19078 | 5550 |
^ #(400 441 484 529 576 625 676 729 784 841 900 961) includes:self. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5551 |
]. |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5552 |
|
18865 | 5553 |
"/ try powers of 2 |
24707 | 5554 |
self isPowerOf2 ifTrue:[ |
19078 | 5555 |
^ self lowBit odd |
18865 | 5556 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5557 |
|
18865 | 5558 |
"/ guess |
5559 |
realSqrt := self sqrt. |
|
5560 |
realSqrt isFinite ifTrue:[ |
|
24868 | 5561 |
realSqrtTruncated := realSqrt truncated. |
5562 |
realSqrt = realSqrtTruncated ifTrue:[ |
|
19078 | 5563 |
"/ still have to check due to rounding errors. |
24868 | 5564 |
intSqrt := realSqrtTruncated asInteger. |
5565 |
(intSqrt squared = self) ifTrue:[^ true]. |
|
19078 | 5566 |
]. |
24868 | 5567 |
"/ slow code |
5568 |
intSqrt := self integerSqrtWithGuess:realSqrtTruncated asInteger. |
|
5569 |
] ifFalse:[ |
|
5570 |
"/ slow code |
|
5571 |
intSqrt := self integerSqrt. |
|
18865 | 5572 |
]. |
5573 |
^ intSqrt squared = self |
|
5574 |
||
5575 |
" |
|
19058 | 5576 |
0 isPerfectSquare |
5577 |
3 isPerfectSquare |
|
5578 |
4 isPerfectSquare |
|
5579 |
9 isPerfectSquare |
|
18865 | 5580 |
(1 to:1000000) count:[:n | n isPerfectSquare] 1000 |
24868 | 5581 |
12345678987654321234567 isPerfectSquare |
5582 |
123123123432 squared isPerfectSquare |
|
18865 | 5583 |
(123123123432 raisedTo:7) isPerfectSquare |
24868 | 5584 |
((123456789123456789 raisedTo:7)) isPerfectSquare |
5585 |
((123456789123456789 raisedTo:7)-1) isPerfectSquare |
|
5586 |
Time microsecondsToRun:[12345678987654321234567 isPerfectSquare] |
|
5587 |
12345678987654321234567 squared isPerfectSquare |
|
24937 | 5588 |
Time microsecondsToRun:[12345678987654321234567 squared isPerfectSquare] |
18865 | 5589 |
" |
5590 |
! |
|
5591 |
||
24707 | 5592 |
isPowerOf2 |
5593 |
"return true, if the receiver is a power of 2" |
|
5594 |
||
5595 |
"redefined, because the hacker's algorithm in smallInt is much slower for large numbers" |
|
5596 |
||
5597 |
|maxBytes "{ Class: SmallInteger }"| |
|
5598 |
||
5599 |
maxBytes := self digitLength. |
|
5600 |
(self digitAt:maxBytes) isPowerOf2 ifFalse:[^ false]. |
|
5601 |
1 to:maxBytes-1 do:[:byteIndex | |
|
5602 |
(self digitAt:byteIndex) ~~ 0 ifTrue:[^ false]. |
|
5603 |
]. |
|
5604 |
^ true |
|
5605 |
||
5606 |
" |
|
5607 |
10000 factorial isPowerOf2 |
|
5608 |
|n| n := 10000 factorial. Time millisecondsToRun:[10000 timesRepeat:[ n isPowerOf2]] |
|
5609 |
" |
|
5610 |
" |
|
5611 |
(2 raisedTo:10000) isPowerOf2 |
|
5612 |
|n| n := (2 raisedTo:10000). Time millisecondsToRun:[10000 timesRepeat:[ n isPowerOf2]] |
|
5613 |
" |
|
5614 |
! |
|
5615 |
||
9134 | 5616 |
isPowerOf:p |
5617 |
"return true, if the receiver is a power of p" |
|
5618 |
||
24707 | 5619 |
p == 2 ifTrue:[ ^ self isPowerOf2]. |
9134 | 5620 |
|
5621 |
"/ the following is a q&d hack, using existing code. |
|
5622 |
^ (Integer |
|
19078 | 5623 |
readFromString:(self printStringRadix:p) |
5624 |
radix:2 |
|
24707 | 5625 |
onError:-1) isPowerOf2 |
9134 | 5626 |
|
5627 |
" |
|
19058 | 5628 |
0 isPowerOf:2 |
5629 |
1 isPowerOf:2 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5630 |
|
11587 | 5631 |
16r0000000000000000 isPowerOf:2 |
5632 |
16r0000004000000000 isPowerOf:2 |
|
5633 |
16r0000004000000001 isPowerOf:2 |
|
5634 |
||
5635 |
16r0000000000000001 isPowerOf:2 |
|
5636 |
16r0000000000000002 isPowerOf:2 |
|
5637 |
16r0000000000000004 isPowerOf:2 |
|
5638 |
16r0000000000000008 isPowerOf:2 |
|
5639 |
||
5640 |
16r0000000000000001 isPowerOf:4 |
|
5641 |
16r0000000000000002 isPowerOf:4 |
|
5642 |
16r0000000000000004 isPowerOf:4 |
|
5643 |
16r0000000000000008 isPowerOf:4 |
|
5644 |
16r0000000000000010 isPowerOf:4 |
|
5645 |
16r0000000000000020 isPowerOf:4 |
|
5646 |
||
5647 |
3r0000000000000001 isPowerOf:3 |
|
5648 |
3r0000000000000010 isPowerOf:3 |
|
5649 |
3r0000000000000100 isPowerOf:3 |
|
5650 |
3r0000000000001000 isPowerOf:3 |
|
5651 |
3r0000000000001001 isPowerOf:3 |
|
5652 |
3r0000000000002000 isPowerOf:3 |
|
5653 |
||
5654 |
10 isPowerOf:10 |
|
5655 |
20 isPowerOf:10 |
|
5656 |
100 isPowerOf:10 |
|
5657 |
110 isPowerOf:10 |
|
5658 |
200 isPowerOf:10 |
|
5659 |
1000 isPowerOf:10 |
|
5660 |
10000 isPowerOf:10 |
|
5661 |
100000 isPowerOf:10 |
|
5662 |
100001 isPowerOf:10 |
|
9134 | 5663 |
" |
13815 | 5664 |
! |
5665 |
||
5666 |
isPowerOfTwo |
|
5667 |
"return true, if the receiver is a power of 2" |
|
5668 |
||
24707 | 5669 |
^ self isPowerOf2 |
13815 | 5670 |
! |
5671 |
||
5672 |
isPrime |
|
5673 |
"return true if I am a prime Number. |
|
22114 | 5674 |
Pre-condition: I am positive. |
13815 | 5675 |
This is a q&d hack, which may need optimization if heavily used." |
5676 |
||
5677 |
|limit firstFewPrimes| |
|
5678 |
||
5679 |
self even ifTrue:[^ self == 2 ]. |
|
5680 |
self == 1 ifTrue:[^ false ]. |
|
5681 |
||
18865 | 5682 |
"/ See comment in initializePrimeCacheUpTo:limit |
5683 |
"/ Integer initializePrimeCacheUpTo:(10 raisedTo:7) |
|
5684 |
"/ Integer flushPrimeCache |
|
5685 |
"/ |
|
5686 |
"/ by default, no primeCache is used. |
|
5687 |
"/ if you do lots of number-stuff with primes, you may want to enable it with |
|
5688 |
"/ Integer initializePrimeCacheUpTo:1000000 |
|
5689 |
"/ and when done, cleanup with flushPrimeCache |
|
19128 | 5690 |
PrimeCache notNil ifTrue:[ |
5691 |
self <= (PrimeCache size*2) ifTrue:[ |
|
5692 |
^ PrimeCache at:self//2. |
|
5693 |
]. |
|
13815 | 5694 |
]. |
20269 | 5695 |
|
19128 | 5696 |
firstFewPrimes := self class primesUpTo5000. |
5697 |
limit := firstFewPrimes last + 1. |
|
5698 |
self < (limit*limit) ifTrue:[ |
|
5699 |
limit := self sqrt asInteger. |
|
20269 | 5700 |
]. |
13815 | 5701 |
firstFewPrimes do:[:p | |
19078 | 5702 |
p > limit ifTrue:[^ true]. |
24697 | 5703 |
(self \\ p) == 0 ifTrue:[ |
5704 |
"/ Transcript showCR:'divides by ',p asString. |
|
5705 |
^ false |
|
5706 |
]. |
|
13815 | 5707 |
]. |
5708 |
||
19118 | 5709 |
PrimeNumberGenerator notNil ifTrue:[ "from exept:libcrypt" |
5710 |
"speed up a lot for large primes" |
|
5711 |
^ PrimeNumberGenerator isPrime:self. |
|
5712 |
]. |
|
5713 |
||
22114 | 5714 |
"/ Not absolutely correct, but was broken much worse before. |
22118 | 5715 |
limit := self integerSqrt. |
24697 | 5716 |
(firstFewPrimes last+2) to:limit by:2 do:[:p | |
5717 |
(self \\ p) == 0 ifTrue:[ |
|
5718 |
"/ Transcript showCR:'divides by ',p asString. |
|
5719 |
^ false |
|
5720 |
]. |
|
13815 | 5721 |
]. |
5722 |
^ true |
|
5723 |
||
5724 |
" |
|
5725 |
Integer primesUpTo:1000 |
|
5726 |
(1 to:1000000) count:[:n | n isPrime] 78498 |
|
5727 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 1295 w.o firstFewPrimes |
|
5728 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 936 with firstFewPrimes (less tests) |
|
5729 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 343 with primeCache |
|
5730 |
" |
|
22114 | 5731 |
|
5732 |
"Modified (comment): / 26-07-2017 / 12:58:45 / mawalch" |
|
13815 | 5733 |
! |
5734 |
||
24968 | 5735 |
mantissa |
5736 |
"return what would be the normalized float's mantissa if I were a float. |
|
5737 |
This is not for general use - it has been added for dolphin (soap) compatibility. |
|
5738 |
This assumes that a float's mantissa is normalized to |
|
5739 |
0.5 .. 1.0 and the number's value is: mantissa * 2^exp" |
|
5740 |
||
5741 |
self < 0 ifTrue:[ |
|
5742 |
^ (self abs / (1 bitShift:(self abs highBit))) negated |
|
5743 |
]. |
|
5744 |
^ self / (1 bitShift:(self highBit)) |
|
5745 |
||
5746 |
" |
|
5747 |
16.0 mantissa |
|
5748 |
16 mantissa |
|
5749 |
16.0 exponent |
|
5750 |
16 exponent |
|
5751 |
16 mantissa * (2 raisedTo:16 exponent) |
|
5752 |
16.0 mantissa * (2 raisedTo:16.0 exponent) |
|
5753 |
-16 mantissa * (2 raisedTo:-16 exponent) |
|
5754 |
-16.0 mantissa * (2 raisedTo:-16.0 exponent) |
|
5755 |
#( 1.0 1 |
|
5756 |
2.0 2 |
|
5757 |
3.0 3 |
|
5758 |
4.0 4 |
|
5759 |
12345.0 12345 |
|
5760 |
0.0 0 |
|
5761 |
-1.0 -1 |
|
5762 |
-2.0 -2 |
|
5763 |
-3.0 -3 |
|
5764 |
-4.0 -4 |
|
5765 |
-12345.0 -12345 |
|
5766 |
) pairWiseDo:[:f :i | |
|
5767 |
self assert:( f exponent = i exponent ). |
|
5768 |
self assert:( f mantissa = i mantissa ). |
|
5769 |
self assert:( f mantissa * (2 raisedTo:f exponent))= f. |
|
5770 |
self assert:( i mantissa * (2 raisedTo:i exponent)) = i. |
|
5771 |
]. |
|
5772 |
" |
|
5773 |
! |
|
5774 |
||
23119 | 5775 |
nextMultipleOf:n |
14680 | 5776 |
"return the multiple of n at or above the receiver. |
23119 | 5777 |
(?? The name of this method may be a bit misleading, |
5778 |
as it returns the receiver iff it is already a multiple) |
|
16800 | 5779 |
Useful for padding, aligning or rounding, |
21774 | 5780 |
especially when reading aligned binary data." |
14677 | 5781 |
|
5782 |
|rest| |
|
5783 |
||
5784 |
rest := self \\ n. |
|
5785 |
rest == 0 ifTrue:[ ^ self ]. |
|
5786 |
^ self + (n - rest) |
|
5787 |
||
5788 |
" |
|
19128 | 5789 |
0 nextMultipleOf: 4 -> 0 |
5790 |
1 nextMultipleOf: 4 -> 4 |
|
5791 |
2 nextMultipleOf: 4 -> 4 |
|
20269 | 5792 |
3 nextMultipleOf: 4 -> 4 |
5793 |
4 nextMultipleOf: 4 -> 4 |
|
5794 |
5 nextMultipleOf: 4 -> 8 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5795 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5796 |
22 nextMultipleOf: 4 |
19128 | 5797 |
100 factorial nextMultipleOf: 4 |
14677 | 5798 |
" |
21774 | 5799 |
|
5800 |
"Modified (comment): / 29-05-2017 / 15:40:45 / mawalch" |
|
23119 | 5801 |
"Modified (comment): / 20-06-2018 / 15:36:33 / Claus Gittinger" |
14677 | 5802 |
! |
5803 |
||
14680 | 5804 |
nextPowerOf2 |
5805 |
"return the power of 2 at or above the receiver. |
|
18769 | 5806 |
Useful for padding. |
19058 | 5807 |
Notice, that for a powerOf2, the receiver is returned. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5808 |
Also notice, that (because it is used for padding), |
19058 | 5809 |
0 is returned for zero." |
14680 | 5810 |
|
5811 |
|x t sh| |
|
5812 |
||
5813 |
x := self - 1. |
|
5814 |
x := x bitOr: (x bitShift: -1). |
|
5815 |
x := x bitOr: (x bitShift: -2). |
|
5816 |
x := x bitOr: (x bitShift: -4). |
|
5817 |
x := x bitOr: (x bitShift: -8). |
|
5818 |
x := x bitOr: (t := x bitShift: -16). |
|
5819 |
t == 0 ifFalse:[ |
|
19078 | 5820 |
sh := -32. |
5821 |
[ |
|
5822 |
x := x bitOr: (t := x bitShift: sh). |
|
5823 |
sh := sh + sh. |
|
5824 |
] doWhile: [t ~~ 0] |
|
14680 | 5825 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5826 |
^ x + 1 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5827 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5828 |
" |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5829 |
0 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5830 |
1 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5831 |
2 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5832 |
3 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5833 |
4 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5834 |
5 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5835 |
6 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5836 |
7 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5837 |
8 nextPowerOf2 |
14680 | 5838 |
|
5839 |
22 nextPowerOf2 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5840 |
12 factorial nextPowerOf2 isPowerOf:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5841 |
100 factorial nextPowerOf2 isPowerOf:2 |
18905 | 5842 |
1000 factorial nextPowerOf2 isPowerOf:2 |
5843 |
Time millisecondsToRun:[ |
|
19078 | 5844 |
|v| |
5845 |
v := 1000 factorial. |
|
5846 |
1000 timesRepeat:[ |
|
5847 |
v nextPowerOf2 |
|
5848 |
] |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5849 |
] |
14680 | 5850 |
" |
5851 |
! |
|
5852 |
||
13815 | 5853 |
nextPrime |
5854 |
"return the next prime after the receiver" |
|
5855 |
||
5856 |
|num| |
|
5857 |
||
5858 |
num := self + 1. |
|
19015 | 5859 |
num <= 2 ifTrue:[^ 2]. |
13815 | 5860 |
num even ifTrue:[ |
19078 | 5861 |
num := num + 1 |
13815 | 5862 |
]. |
5863 |
[num isPrime] whileFalse:[ |
|
19078 | 5864 |
num := num + 2 |
13815 | 5865 |
]. |
5866 |
^ num |
|
5867 |
||
5868 |
" |
|
19015 | 5869 |
0 nextPrime |
13815 | 5870 |
1 nextPrime |
19015 | 5871 |
2 nextPrime |
13815 | 5872 |
22 nextPrime |
5873 |
37 nextPrime |
|
5874 |
36 nextPrime |
|
5875 |
3456737 nextPrime |
|
18865 | 5876 |
1000 factorial nextPrime |
13815 | 5877 |
" |
13905 | 5878 |
! |
5879 |
||
13906 | 5880 |
parityOdd |
13914 | 5881 |
"return true, if an odd number of bits are set in the receiver, false otherwise. |
13905 | 5882 |
(i.e. true for odd parity) |
5883 |
Undefined for negative values (smalltalk does not require the machine to use 2's complement)" |
|
5884 |
||
5885 |
^ self bitCount odd |
|
5886 |
||
5887 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5888 |
0 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5889 |
1 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5890 |
2 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5891 |
4 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5892 |
5 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5893 |
7 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5894 |
33 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5895 |
6 parityOdd |
13905 | 5896 |
|
5897 |
1 to:1000000 do:[:n | |
|
19078 | 5898 |
self assert:(n parityOdd = ((n printStringRadix:2) occurrencesOf:$1) odd). |
13905 | 5899 |
] |
13906 | 5900 |
|
5901 |
0 to:255 do:[:n | |
|
19078 | 5902 |
|p| |
5903 |
||
5904 |
p := |
|
5905 |
(((((((((n rightShift: 7) |
|
5906 |
bitXor: (n rightShift: 6)) |
|
5907 |
bitXor: (n rightShift: 5)) |
|
5908 |
bitXor: (n rightShift: 4)) |
|
5909 |
bitXor: (n rightShift: 3)) |
|
5910 |
bitXor: (n rightShift: 2)) |
|
5911 |
bitXor: (n rightShift: 1)) |
|
5912 |
bitXor: n) bitAnd:1) == 1. |
|
5913 |
self assert:(n parityOdd = p). |
|
13906 | 5914 |
] |
5915 |
" |
|
5916 |
||
5917 |
"Created: / 09-01-2012 / 17:18:06 / cg" |
|
701 | 5918 |
! ! |
202 | 5919 |
|
25215 | 5920 |
|
16293 | 5921 |
!Integer methodsFor:'special modulo arithmetic'! |
2458 | 5922 |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5923 |
add_32:anInteger |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5924 |
"return a C-semantic 32bit sum of the receiver and the argument. |
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5925 |
Both must be either Small- or LargeIntegers. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5926 |
Returns a signed 32bit number. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5927 |
This (nonstandard) specialized method is provided to allow simulation of |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5928 |
modulu operations with C semantics." |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5929 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5930 |
%{ /* NOCONTEXT */ |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5931 |
int val1, val2, rslt; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5932 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5933 |
if (__isSmallInteger(self)) { |
19078 | 5934 |
val1 = __intVal(self); |
12652 | 5935 |
} else { |
19078 | 5936 |
val1 = __longIntVal(self); |
5937 |
if (!val1) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5938 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5939 |
if (__isSmallInteger(anInteger)) { |
19078 | 5940 |
val2 = __intVal(anInteger); |
12652 | 5941 |
} else { |
19078 | 5942 |
val2 = __longIntVal(anInteger); |
5943 |
if (!val2) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5944 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5945 |
rslt = val1 + val2; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5946 |
RETURN(__MKINT(rslt)); |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5947 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5948 |
bad: ; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5949 |
%}. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5950 |
self primitiveFailed. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5951 |
! |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5952 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5953 |
add_32u:anInteger |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5954 |
"return a C-semantic 32bit unsigned sum of the receiver and the argument. |
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5955 |
Both must be either Small- or LargeIntegers. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5956 |
Returns an unsigned 32bit number. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5957 |
This (nonstandard) specialized method is provided to allow simulation of |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5958 |
modulu operations with C semantics." |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5959 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5960 |
%{ /* NOCONTEXT */ |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5961 |
int val1, val2, rslt; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5962 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5963 |
if (__isSmallInteger(self)) { |
19078 | 5964 |
val1 = __intVal(self); |
12652 | 5965 |
} else { |
19078 | 5966 |
val1 = __longIntVal(self); |
5967 |
if (!val1) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5968 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5969 |
if (__isSmallInteger(anInteger)) { |
19078 | 5970 |
val2 = __intVal(anInteger); |
12652 | 5971 |
} else { |
19078 | 5972 |
val2 = __longIntVal(anInteger); |
5973 |
if (!val2) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5974 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5975 |
rslt = val1 + val2; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5976 |
RETURN(__MKUINT((unsigned)rslt)); |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5977 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5978 |
bad: ; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5979 |
%}. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5980 |
self primitiveFailed. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5981 |
! |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5982 |
|
13725 | 5983 |
mul_32:anInteger |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5984 |
"return a C-semantic 32bit product of the receiver and the argument. |
13725 | 5985 |
Both must be either Small- or LargeIntegers. |
5986 |
Returns a signed 32bit number. |
|
5987 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5988 |
modulu operations with C semantics." |
|
5989 |
||
5990 |
%{ /* NOCONTEXT */ |
|
5991 |
int val1, val2, rslt; |
|
5992 |
||
5993 |
if (__isSmallInteger(self)) { |
|
19078 | 5994 |
val1 = __intVal(self); |
13725 | 5995 |
} else { |
19078 | 5996 |
val1 = __longIntVal(self); |
5997 |
if (!val1) goto bad; |
|
13725 | 5998 |
} |
5999 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6000 |
val2 = __intVal(anInteger); |
13725 | 6001 |
} else { |
19078 | 6002 |
val2 = __longIntVal(anInteger); |
6003 |
if (!val2) goto bad; |
|
13725 | 6004 |
} |
6005 |
rslt = val1 * val2; |
|
6006 |
RETURN(__MKINT(rslt)); |
|
6007 |
||
6008 |
bad: ; |
|
6009 |
%}. |
|
6010 |
self primitiveFailed. |
|
6011 |
! |
|
6012 |
||
6013 |
mul_32u:anInteger |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
6014 |
"return a C-semantic 32bit unsigned product of the receiver and the argument. |
13725 | 6015 |
Both must be either Small- or LargeIntegers. |
6016 |
Returns an unsigned 32bit number. |
|
6017 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6018 |
modulu operations with C semantics." |
|
6019 |
||
6020 |
%{ /* NOCONTEXT */ |
|
6021 |
int val1, val2, rslt; |
|
6022 |
||
6023 |
if (__isSmallInteger(self)) { |
|
19078 | 6024 |
val1 = __intVal(self); |
13725 | 6025 |
} else { |
19078 | 6026 |
val1 = __longIntVal(self); |
6027 |
if (!val1) goto bad; |
|
13725 | 6028 |
} |
6029 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6030 |
val2 = __intVal(anInteger); |
13725 | 6031 |
} else { |
19078 | 6032 |
val2 = __longIntVal(anInteger); |
6033 |
if (!val2) goto bad; |
|
13725 | 6034 |
} |
6035 |
rslt = val1 * val2; |
|
6036 |
RETURN(__MKUINT((unsigned)rslt)); |
|
6037 |
||
6038 |
bad: ; |
|
6039 |
%}. |
|
6040 |
self primitiveFailed. |
|
6041 |
! |
|
6042 |
||
6043 |
sub_32:anInteger |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
6044 |
"return a C-semantic 32bit difference of the receiver and the argument. |
13725 | 6045 |
Both must be either Small- or LargeIntegers. |
6046 |
Returns a signed 32bit number. |
|
6047 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6048 |
modulu operations with C semantics." |
|
6049 |
||
6050 |
%{ /* NOCONTEXT */ |
|
6051 |
int val1, val2, rslt; |
|
6052 |
||
6053 |
if (__isSmallInteger(self)) { |
|
19078 | 6054 |
val1 = __intVal(self); |
13725 | 6055 |
} else { |
19078 | 6056 |
val1 = __longIntVal(self); |
6057 |
if (!val1) goto bad; |
|
13725 | 6058 |
} |
6059 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6060 |
val2 = __intVal(anInteger); |
13725 | 6061 |
} else { |
19078 | 6062 |
val2 = __longIntVal(anInteger); |
6063 |
if (!val2) goto bad; |
|
13725 | 6064 |
} |
6065 |
rslt = val1 - val2; |
|
6066 |
RETURN(__MKINT(rslt)); |
|
6067 |
||
6068 |
bad: ; |
|
6069 |
%}. |
|
6070 |
self primitiveFailed. |
|
6071 |
! |
|
6072 |
||
6073 |
sub_32u:anInteger |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
6074 |
"return a C-semantic 32bit unsigned difference of the receiver and the argument. |
13725 | 6075 |
Both must be either Small- or LargeIntegers. |
6076 |
Returns an unsigned 32bit number. |
|
6077 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6078 |
modulu operations with C semantics." |
|
6079 |
||
6080 |
%{ /* NOCONTEXT */ |
|
6081 |
int val1, val2, rslt; |
|
6082 |
||
6083 |
if (__isSmallInteger(self)) { |
|
19078 | 6084 |
val1 = __intVal(self); |
13725 | 6085 |
} else { |
19078 | 6086 |
val1 = __longIntVal(self); |
6087 |
if (!val1) goto bad; |
|
13725 | 6088 |
} |
6089 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6090 |
val2 = __intVal(anInteger); |
13725 | 6091 |
} else { |
19078 | 6092 |
val2 = __longIntVal(anInteger); |
6093 |
if (!val2) goto bad; |
|
13725 | 6094 |
} |
6095 |
rslt = val1 - val2; |
|
6096 |
RETURN(__MKUINT((unsigned)rslt)); |
|
6097 |
||
6098 |
bad: ; |
|
6099 |
%}. |
|
6100 |
self primitiveFailed. |
|
6101 |
! ! |
|
6102 |
||
16293 | 6103 |
!Integer methodsFor:'special modulo bit operators'! |
6104 |
||
6105 |
asSigned32 |
|
23930 | 6106 |
"return a 32-bit integer with my bit-pattern. |
6107 |
For protocol completeness." |
|
16293 | 6108 |
|
6109 |
^ self |
|
23930 | 6110 |
|
6111 |
"Modified (comment): / 17-03-2019 / 12:08:09 / Claus Gittinger" |
|
16293 | 6112 |
! |
6113 |
||
22069 | 6114 |
asUnsigned128 |
6115 |
"return a 128-bit integer with my bit-pattern, but positive. |
|
6116 |
May be required for bit operations on the sign-bit and/or to |
|
23930 | 6117 |
convert C numbers. |
6118 |
Does not check if the value is out of range, so callers |
|
6119 |
may have to do a bitwise-and on the result" |
|
22069 | 6120 |
|
6121 |
self < 0 ifTrue:[ |
|
6122 |
^ 16r1000000000000000000000000000000000 + self |
|
6123 |
]. |
|
6124 |
^ self |
|
6125 |
||
6126 |
" |
|
6127 |
(-1 asUnsigned128) hexPrintString |
|
6128 |
1 asUnsigned128 |
|
6129 |
(SmallInteger minVal asUnsigned128) hexPrintString |
|
6130 |
(SmallInteger maxVal asUnsigned128) hexPrintString |
|
6131 |
" |
|
6132 |
||
6133 |
"Created: / 21-07-2017 / 14:44:19 / cg" |
|
23930 | 6134 |
"Modified (comment): / 17-03-2019 / 12:20:59 / Claus Gittinger" |
6135 |
! |
|
6136 |
||
6137 |
asUnsigned16 |
|
6138 |
"return a 16-bit integer with my bit-pattern, but positive. |
|
6139 |
May be required for bit operations on the sign-bit and/or to |
|
6140 |
convert C/Java numbers. |
|
6141 |
Does not check if the value is out of range, so callers |
|
6142 |
may have to do a bitwise-and on the result" |
|
6143 |
||
6144 |
self < 0 ifTrue:[ |
|
6145 |
^ 16r10000 + self |
|
6146 |
]. |
|
6147 |
^ self |
|
6148 |
||
6149 |
" |
|
6150 |
(-1 asUnsigned16) hexPrintString |
|
6151 |
1 asUnsigned16 |
|
6152 |
(SmallInteger minVal asUnsigned16) hexPrintString |
|
6153 |
(SmallInteger maxVal asUnsigned16) hexPrintString |
|
6154 |
" |
|
6155 |
||
6156 |
"Created: / 17-03-2019 / 12:07:23 / Claus Gittinger" |
|
22069 | 6157 |
! |
6158 |
||
16293 | 6159 |
asUnsigned32 |
6160 |
"return a 32-bit integer with my bit-pattern, but positive. |
|
6161 |
May be required for bit operations on the sign-bit and/or to |
|
23930 | 6162 |
convert C/Java numbers. |
6163 |
Does not check if the value is out of range, so callers |
|
6164 |
may have to do a bitwise-and on the result" |
|
16293 | 6165 |
|
6166 |
self < 0 ifTrue:[ |
|
19078 | 6167 |
^ 16r100000000 + self |
16293 | 6168 |
]. |
6169 |
^ self |
|
6170 |
||
6171 |
" |
|
6172 |
(-1 asUnsigned32) hexPrintString |
|
6173 |
1 asUnsigned32 |
|
6174 |
(SmallInteger minVal asUnsigned32) hexPrintString |
|
6175 |
(SmallInteger maxVal asUnsigned32) hexPrintString |
|
6176 |
" |
|
23930 | 6177 |
|
6178 |
"Modified (comment): / 17-03-2019 / 12:20:48 / Claus Gittinger" |
|
16293 | 6179 |
! |
6180 |
||
6181 |
asUnsigned64 |
|
6182 |
"return a 64-bit integer with my bit-pattern, but positive. |
|
6183 |
May be required for bit operations on the sign-bit and/or to |
|
23930 | 6184 |
convert C/Java numbers. |
6185 |
Does not check if the value is out of range, so callers |
|
6186 |
may have to do a bitwise-and on the result" |
|
16293 | 6187 |
|
6188 |
self < 0 ifTrue:[ |
|
19078 | 6189 |
^ 16r10000000000000000 + self |
16293 | 6190 |
]. |
6191 |
^ self |
|
6192 |
||
6193 |
" |
|
6194 |
(-1 asUnsigned64) hexPrintString |
|
6195 |
1 asUnsigned64 |
|
6196 |
(SmallInteger minVal asUnsigned64) hexPrintString |
|
6197 |
(SmallInteger maxVal asUnsigned64) hexPrintString |
|
24244 | 6198 |
|
6199 |
||
6200 |
(16r-8000000000000000 asUnsigned64) hexPrintString |
|
6201 |
||
6202 |
out of range: wrong results |
|
6203 |
(16r8000000000000000 asUnsigned64) hexPrintString |
|
6204 |
(16rFFFFFFFFFFFFFFFF asUnsigned64) hexPrintString |
|
6205 |
(16r-FFFFFFFFFFFFFFFF asUnsigned64) hexPrintString |
|
16293 | 6206 |
" |
6207 |
||
6208 |
"Created: / 26-07-2013 / 13:45:11 / cg" |
|
24244 | 6209 |
"Modified (comment): / 03-06-2019 / 18:49:12 / Claus Gittinger" |
16293 | 6210 |
! |
13725 | 6211 |
|
22072 | 6212 |
asUnsigned:numBits |
6213 |
"return a numBits integer with my bit-pattern, but positive. |
|
6214 |
May be required for bit operations on the sign-bit and/or to |
|
23930 | 6215 |
convert C/Java numbers, or to generate bitfields from signed numbers |
6216 |
(kind of the reverse operation to signExtenedFromBit:). |
|
6217 |
Does not check if the value is out of range, so callers |
|
6218 |
may have to do a bitwise-and on the result" |
|
22072 | 6219 |
|
6220 |
self < 0 ifTrue:[ |
|
6221 |
^ (1 bitShift:numBits) + self |
|
6222 |
]. |
|
6223 |
^ self |
|
6224 |
||
6225 |
" |
|
6226 |
(-1 asUnsigned:64) hexPrintString |
|
6227 |
1 asUnsigned:64 |
|
6228 |
(SmallInteger minVal asUnsigned:64) hexPrintString |
|
6229 |
(SmallInteger maxVal asUnsigned:64) hexPrintString |
|
6230 |
||
6231 |
(-1 asUnsigned:4) hexPrintString |
|
6232 |
(-7 asUnsigned:4) hexPrintString |
|
6233 |
(-8 asUnsigned:4) hexPrintString |
|
6234 |
1 asUnsigned:4 |
|
6235 |
" |
|
6236 |
||
6237 |
"Created: / 21-07-2017 / 15:11:40 / cg" |
|
23930 | 6238 |
"Modified (comment): / 17-03-2019 / 12:21:15 / Claus Gittinger" |
22072 | 6239 |
! |
6240 |
||
2458 | 6241 |
bitAnd_32:anInteger |
6242 |
"return a C-semantic 32bit locical-and of the receiver and |
|
6243 |
the argument. Both must be either Small- or LargeIntegers. |
|
12255 | 6244 |
Returns a signed 32bit number. |
2458 | 6245 |
This (nonstandard) specialized method is provided to allow simulation of |
12255 | 6246 |
bit operations with C semantics." |
6247 |
||
6248 |
%{ /* NOCONTEXT */ |
|
6249 |
int val1, val2, rslt; |
|
6250 |
||
6251 |
if (__isSmallInteger(self)) { |
|
19078 | 6252 |
val1 = __intVal(self); |
12652 | 6253 |
} else { |
19078 | 6254 |
val1 = __longIntVal(self); |
6255 |
if (!val1) goto bad; |
|
12255 | 6256 |
} |
6257 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6258 |
val2 = __intVal(anInteger); |
12652 | 6259 |
} else { |
19078 | 6260 |
val2 = __longIntVal(anInteger); |
6261 |
if (!val2) goto bad; |
|
12255 | 6262 |
} |
6263 |
rslt = val1 & val2; |
|
6264 |
RETURN(__MKINT(rslt)); |
|
6265 |
||
6266 |
bad: ; |
|
6267 |
%}. |
|
6268 |
self primitiveFailed. |
|
6269 |
! |
|
6270 |
||
6271 |
bitAnd_32u:anInteger |
|
6272 |
"return a C-semantic 32bit locical-and of the receiver and |
|
6273 |
the argument. Both must be either Small- or LargeIntegers. |
|
6274 |
Returns an unsigned 32bit number. |
|
6275 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6276 |
bit operations with C semantics." |
|
2458 | 6277 |
|
6278 |
%{ /* NOCONTEXT */ |
|
6279 |
int val1, val2, rslt; |
|
6280 |
||
6281 |
if (__isSmallInteger(self)) { |
|
19078 | 6282 |
val1 = __intVal(self); |
12652 | 6283 |
} else { |
19078 | 6284 |
val1 = __longIntVal(self); |
6285 |
if (!val1) goto bad; |
|
2458 | 6286 |
} |
6287 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6288 |
val2 = __intVal(anInteger); |
12652 | 6289 |
} else { |
19078 | 6290 |
val2 = __longIntVal(anInteger); |
6291 |
if (!val2) goto bad; |
|
2458 | 6292 |
} |
6293 |
rslt = val1 & val2; |
|
12255 | 6294 |
RETURN(__MKUINT(rslt)); |
6295 |
||
6296 |
bad: ; |
|
6297 |
%}. |
|
6298 |
self primitiveFailed. |
|
6299 |
! |
|
6300 |
||
6301 |
bitInvert_32 |
|
6302 |
"return a C-semantic 32bit complement of the receiver, |
|
6303 |
which must be either Small- or LargeIntegers. |
|
6304 |
Returns a signed 32bit number. |
|
6305 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6306 |
bit operations with C semantics." |
|
6307 |
||
6308 |
%{ /* NOCONTEXT */ |
|
6309 |
int val, rslt; |
|
6310 |
||
6311 |
if (__isSmallInteger(self)) { |
|
19078 | 6312 |
val = __intVal(self); |
12652 | 6313 |
} else { |
19078 | 6314 |
val = __longIntVal(self); |
6315 |
if (!val) goto bad; |
|
12255 | 6316 |
} |
6317 |
rslt = ~val; |
|
2458 | 6318 |
RETURN(__MKINT(rslt)); |
6319 |
||
6320 |
bad: ; |
|
6321 |
%}. |
|
6322 |
self primitiveFailed. |
|
6323 |
! |
|
6324 |
||
12255 | 6325 |
bitInvert_32u |
6326 |
"return a C-semantic 32bit complement of the receiver, |
|
6327 |
which must be either Small- or LargeIntegers. |
|
6328 |
Returns an unsigned 32bit number. |
|
6329 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6330 |
bit operations with C semantics." |
|
6331 |
||
6332 |
%{ /* NOCONTEXT */ |
|
6333 |
int val, rslt; |
|
6334 |
||
6335 |
if (__isSmallInteger(self)) { |
|
19078 | 6336 |
val = __intVal(self); |
12652 | 6337 |
} else { |
19078 | 6338 |
val = __longIntVal(self); |
6339 |
if (!val) goto bad; |
|
12255 | 6340 |
} |
6341 |
rslt = ~val; |
|
6342 |
RETURN(__MKUINT(rslt)); |
|
6343 |
||
6344 |
bad: ; |
|
6345 |
%}. |
|
6346 |
self primitiveFailed. |
|
6347 |
! |
|
6348 |
||
2458 | 6349 |
bitOr_32:anInteger |
6350 |
"return a C-semantic 32bit locical-or of the receiver and |
|
6351 |
the argument. Both must be either Small- or LargeIntegers. |
|
12255 | 6352 |
Returns a signed 32bit number. |
2458 | 6353 |
This (nonstandard) specialized method is provided to allow simulation of |
12255 | 6354 |
bit operations with C semantics." |
6355 |
||
6356 |
%{ /* NOCONTEXT */ |
|
6357 |
int val1, val2, rslt; |
|
6358 |
||
6359 |
if (__isSmallInteger(self)) { |
|
19078 | 6360 |
val1 = __intVal(self); |
12652 | 6361 |
} else { |
19078 | 6362 |
val1 = __longIntVal(self); |
6363 |
if (!val1) goto bad; |
|
12255 | 6364 |
} |
6365 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6366 |
val2 = __intVal(anInteger); |
12652 | 6367 |
} else { |
19078 | 6368 |
val2 = __longIntVal(anInteger); |
6369 |
if (!val2) goto bad; |
|
12255 | 6370 |
} |
6371 |
rslt = val1 | val2; |
|
6372 |
RETURN(__MKINT(rslt)); |
|
6373 |
||
6374 |
bad: ; |
|
6375 |
%}. |
|
6376 |
self primitiveFailed. |
|
6377 |
! |
|
6378 |
||
6379 |
bitOr_32u:anInteger |
|
6380 |
"return a C-semantic 32bit locical-or of the receiver and |
|
6381 |
the argument. Both must be either Small- or LargeIntegers. |
|
6382 |
Returns an unsigned 32bit number. |
|
6383 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6384 |
bit operations with C semantics." |
|
2458 | 6385 |
|
6386 |
%{ /* NOCONTEXT */ |
|
6387 |
int val1, val2, rslt; |
|
6388 |
||
6389 |
if (__isSmallInteger(self)) { |
|
19078 | 6390 |
val1 = __intVal(self); |
12652 | 6391 |
} else { |
19078 | 6392 |
val1 = __longIntVal(self); |
6393 |
if (!val1) goto bad; |
|
12255 | 6394 |
} |
6395 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6396 |
val2 = __intVal(anInteger); |
12652 | 6397 |
} else { |
19078 | 6398 |
val2 = __longIntVal(anInteger); |
6399 |
if (!val2) goto bad; |
|
12255 | 6400 |
} |
6401 |
rslt = val1 | val2; |
|
6402 |
RETURN(__MKUINT(rslt)); |
|
6403 |
||
6404 |
bad: ; |
|
6405 |
%}. |
|
6406 |
self primitiveFailed. |
|
6407 |
! |
|
6408 |
||
6409 |
bitXor_32:anInteger |
|
6410 |
"return a C-semantic 32bit locical-xor of the receiver and |
|
6411 |
the argument. Both must be either Small- or LargeIntegers. |
|
6412 |
Returns a signed 32bit number. |
|
6413 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6414 |
bit operations with C semantics." |
|
6415 |
||
6416 |
%{ /* NOCONTEXT */ |
|
6417 |
int val1, val2, rslt; |
|
6418 |
||
6419 |
if (__isSmallInteger(self)) { |
|
19078 | 6420 |
val1 = __intVal(self); |
12652 | 6421 |
} else { |
19078 | 6422 |
val1 = __longIntVal(self); |
6423 |
if (!val1) goto bad; |
|
2458 | 6424 |
} |
6425 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6426 |
val2 = __intVal(anInteger); |
12652 | 6427 |
} else { |
19078 | 6428 |
val2 = __longIntVal(anInteger); |
6429 |
if (!val2) goto bad; |
|
2458 | 6430 |
} |
12255 | 6431 |
rslt = val1 ^ val2; |
2458 | 6432 |
RETURN(__MKINT(rslt)); |
6433 |
||
6434 |
bad: ; |
|
6435 |
%}. |
|
6436 |
self primitiveFailed. |
|
12255 | 6437 |
! |
6438 |
||
6439 |
bitXor_32u:anInteger |
|
6440 |
"return a C-semantic 32bit locical-xor of the receiver and |
|
6441 |
the argument. Both must be either Small- or LargeIntegers. |
|
6442 |
Returns an unsigned 32bit number. |
|
6443 |
This (nonstandard) specialized method is provided to allow simulation of |
|
6444 |
bit operations with C semantics." |
|
6445 |
||
6446 |
%{ /* NOCONTEXT */ |
|
6447 |
int val1, val2, rslt; |
|
6448 |
||
6449 |
if (__isSmallInteger(self)) { |
|
19078 | 6450 |
val1 = __intVal(self); |
12652 | 6451 |
} else { |
19078 | 6452 |
val1 = __longIntVal(self); |
6453 |
if (!val1) goto bad; |
|
12255 | 6454 |
} |
6455 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 6456 |
val2 = __intVal(anInteger); |
12652 | 6457 |
} else { |
19078 | 6458 |
val2 = __longIntVal(anInteger); |
6459 |
if (!val2) goto bad; |
|
12255 | 6460 |
} |
6461 |
rslt = val1 ^ val2; |
|
6462 |
RETURN(__MKUINT(rslt)); |
|
6463 |
||
6464 |
bad: ; |
|
6465 |
%}. |
|
6466 |
self primitiveFailed. |
|
2458 | 6467 |
! ! |
6468 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6469 |
!Integer methodsFor:'tracing'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6470 |
|
4682 | 6471 |
traceInto:aRequestor level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6472 |
"double dispatch into tracer, passing my type implicitely in the selector" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6473 |
|
4682 | 6474 |
^ aRequestor traceInteger:self level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6475 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6476 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6477 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
6478 |
|
701 | 6479 |
!Integer methodsFor:'truncation & rounding'! |
6480 |
||
6481 |
ceiling |
|
1558 | 6482 |
"return the smallest integer which is larger or equal to the receiver. |
6483 |
For integers, this is the receiver itself." |
|
345 | 6484 |
|
701 | 6485 |
^ self |
1558 | 6486 |
|
6487 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 6488 |
! |
345 | 6489 |
|
701 | 6490 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6491 |
"if the receiver can be represented as a SmallInteger, return |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6492 |
a SmallInteger with my value; otherwise return self with leading |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6493 |
zeros removed. This method is redefined in LargeInteger." |
701 | 6494 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6495 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6496 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6497 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 6498 |
! |
6499 |
||
701 | 6500 |
floor |
1558 | 6501 |
"return the largest integer which is smaller or equal to the receiver. |
6502 |
For integers, this is the receiver itself." |
|
701 | 6503 |
|
6504 |
^ self |
|
1558 | 6505 |
|
6506 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 6507 |
! |
202 | 6508 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6509 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6510 |
"return a number with value from digits after the decimal point. |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6511 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6512 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6513 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6514 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6515 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6516 |
" |
11587 | 6517 |
1234.56789 fractionPart |
6518 |
1.2345e6 fractionPart |
|
6519 |
1000 fractionPart |
|
6520 |
10000000000000000 fractionPart |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6521 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6522 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6523 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6524 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6525 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6526 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6527 |
"return a number with value from digits before the decimal point. |
18851 | 6528 |
(i.e. the receiver's truncated value) |
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6529 |
Since integers have no fraction, return the receiver here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6530 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6531 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6532 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6533 |
" |
11587 | 6534 |
1234.56789 integerPart |
6535 |
1.2345e6 integerPart |
|
6536 |
1000 integerPart |
|
6537 |
10000000000000000 integerPart |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6538 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6539 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6540 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6541 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
6542 |
|
701 | 6543 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6544 |
"if the receiver can be represented as a SmallInteger, return |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6545 |
a SmallInteger with my value; otherwise return self with leading |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6546 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6547 |
This method is left for backward compatibility - it has been |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6548 |
renamed to #compressed for ST-80 compatibility." |
1 | 6549 |
|
9261 | 6550 |
<resource: #obsolete> |
6551 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6552 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6553 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
6554 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 6555 |
! |
1 | 6556 |
|
701 | 6557 |
rounded |
6558 |
"return the receiver rounded toward the next Integer - |
|
1558 | 6559 |
for integers this is the receiver itself." |
701 | 6560 |
|
6561 |
^ self |
|
1558 | 6562 |
|
6563 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 6564 |
! |
69 | 6565 |
|
701 | 6566 |
truncated |
11587 | 6567 |
"return the receiver truncated towards zero as Integer |
1558 | 6568 |
for integers this is the receiver itself." |
159 | 6569 |
|
701 | 6570 |
^ self |
1558 | 6571 |
|
6572 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 6573 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
6574 |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6575 |
!Integer methodsFor:'visiting'! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6576 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6577 |
acceptVisitor:aVisitor with:aParameter |
16723 | 6578 |
"dispatch for visitor pattern; send #visitInteger:with: to aVisitor" |
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6579 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6580 |
^ aVisitor visitInteger:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6581 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
6582 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6583 |
!Integer::ModuloNumber class methodsFor:'documentation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6584 |
|
4803 | 6585 |
copyright |
6586 |
" |
|
6587 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
19078 | 6588 |
All Rights Reserved |
4803 | 6589 |
|
6590 |
This software is furnished under a license and may be used |
|
6591 |
only in accordance with the terms of that license and with the |
|
6592 |
inclusion of the above copyright notice. This software may not |
|
6593 |
be provided or otherwise made available to, or used by, any |
|
6594 |
other person. No title to or ownership of the software is |
|
6595 |
hereby transferred. |
|
6596 |
" |
|
6597 |
||
6598 |
! |
|
6599 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6600 |
documentation |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6601 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6602 |
This is a helper class to perform fast computation of the modulus. |
4609 | 6603 |
(with big numbers, this does make a difference) |
4803 | 6604 |
|
6605 |
WARNING: this does only work with numbers which have no common |
|
19078 | 6606 |
divisor (which is true for cryptographic applications). |
6607 |
So, use this only if you know what you are doing ... |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6608 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6609 |
[author:] |
19078 | 6610 |
Stefan Vogel |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6611 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6612 |
[see also:] |
19078 | 6613 |
Integer SmallInteger LargeInsteger |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6614 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6615 |
[instance variables:] |
19078 | 6616 |
modulus the modulus |
6617 |
reciprocal reciprocal of the modulus |
|
6618 |
shift shift count to cut off some bits |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6619 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6620 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6621 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6622 |
examples |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6623 |
" |
19078 | 6624 |
[exBegin] |
6625 |
17 asModuloNumber modulusOf:38 |
|
6626 |
[exEnd] |
|
6627 |
||
6628 |
[exBegin] |
|
6629 |
38 \\ 17 |
|
6630 |
[exEnd] |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6631 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6632 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6633 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6634 |
!Integer::ModuloNumber class methodsFor:'instance creation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6635 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6636 |
modulus:anInteger |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6637 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6638 |
^ self new modulus:anInteger |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6639 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6640 |
"Created: / 3.5.1999 / 11:13:15 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6641 |
"Modified: / 3.5.1999 / 11:18:37 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6642 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6643 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6644 |
!Integer::ModuloNumber methodsFor:'accessing'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6645 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6646 |
modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6647 |
"return the modulus" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6648 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6649 |
^ modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6650 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6651 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6652 |
modulus:n |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6653 |
"set the modulus" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6654 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6655 |
modulus := n. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6656 |
reciprocal := n integerReciprocal. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6657 |
shift := n highBit negated. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6658 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6659 |
"Created: / 3.5.1999 / 10:02:39 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6660 |
"Modified: / 3.5.1999 / 14:30:49 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6661 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6662 |
|
5238 | 6663 |
!Integer::ModuloNumber methodsFor:'arithmetic'! |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6664 |
|
19125 | 6665 |
modulusOf:dividend |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6666 |
"compute the aNumber modulo myself. |
19231 | 6667 |
The shortcut works only, if dividend is < modulo * modulo |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6668 |
(When doing arithmethic modulo something). |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6669 |
Otherwise do it the long way" |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6670 |
|
19125 | 6671 |
|e t cnt abs isNegative| |
6672 |
||
6673 |
isNegative := dividend negative. |
|
6674 |
isNegative ifTrue:[ |
|
6675 |
abs := dividend negated. |
|
6676 |
] ifFalse:[ |
|
6677 |
abs := dividend. |
|
6678 |
]. |
|
19874 | 6679 |
abs < modulus ifTrue:[ |
6680 |
^ abs. |
|
6681 |
]. |
|
19125 | 6682 |
|
19231 | 6683 |
"/ self assert:dividend < (modulus * modulus) |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6684 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6685 |
"throw off low nbits(modulus)" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6686 |
|
19231 | 6687 |
e := (abs bitShift:shift) * reciprocal. |
6688 |
e := (e bitShift:shift) * modulus. |
|
19125 | 6689 |
e := abs - e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6690 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6691 |
"this subtract is done max 2 times" |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6692 |
cnt := 2. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6693 |
[(t := e - modulus) >= 0] whileTrue:[ |
19078 | 6694 |
e := t. |
6695 |
cnt == 0 ifTrue:[ |
|
6696 |
"shortcut didn't work, do it the long way" |
|
6697 |
^ e \\ modulus. |
|
6698 |
]. |
|
6699 |
cnt := cnt - 1. |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6700 |
]. |
19125 | 6701 |
isNegative ifTrue:[ |
6702 |
^ modulus - e. |
|
6703 |
]. |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6704 |
^ e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6705 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6706 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6707 |
|m| |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6708 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6709 |
m := self new modulus:7. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6710 |
m modulusOf:55. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6711 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6712 |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
6713 |
"Shortcut does not work: (size of divisor vs. dividend): |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6714 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6715 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6716 |
m := self new modulus:7. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6717 |
m modulusOf:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6718 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6719 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6720 |
"SLOW (using standard \\ operation): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6721 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6722 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6723 |
m := 123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6724 |
Time millisecondsToRun:[ |
19078 | 6725 |
100000 timesRepeat:[ |
6726 |
874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658 \\ m |
|
6727 |
] |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6728 |
] |
11587 | 6729 |
" |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6730 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6731 |
"fast (using moduloNumber with almost same-sized dividend and divisor): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6732 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6733 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6734 |
m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
19231 | 6735 |
Time millisecondsToRun:[ |
6736 |
100000 timesRepeat:[ |
|
6737 |
m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
|
6738 |
] |
|
6739 |
]. |
|
6740 |
||
6741 |
10730930127807326146398409623772237722337234475792709784029183368622308259008044569184592041059181058049458041058052 |
|
11587 | 6742 |
" |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
6743 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6744 |
"Modified: / 3.5.1999 / 14:30:32 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6745 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6746 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6747 |
!Integer::ModuloNumber methodsFor:'converting'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6748 |
|
19405 | 6749 |
asInteger |
6750 |
"return the modulus" |
|
6751 |
||
6752 |
^ modulus |
|
6753 |
! |
|
6754 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6755 |
asModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6756 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6757 |
^ self |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6758 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6759 |
"Created: / 3.5.1999 / 14:48:27 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6760 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
6761 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
6762 |
!Integer class methodsFor:'documentation'! |
701 | 6763 |
|
6764 |
version |
|
18769 | 6765 |
^ '$Header$' |
12122 | 6766 |
! |
6767 |
||
6768 |
version_CVS |
|
18769 | 6769 |
^ '$Header$' |
1 | 6770 |
! ! |
6858 | 6771 |
|
14677 | 6772 |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
6773 |
Integer initialize! |