author | Claus Gittinger <cg@exept.de> |
Thu, 19 Dec 1996 17:26:17 +0100 | |
changeset 2001 | 8c2b957f0cb3 |
parent 1983 | c5a2c4655d10 |
child 2267 | 123ec897aca5 |
permissions | -rw-r--r-- |
1 | 1 |
" |
68 | 2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
345 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Integer subclass:#LargeInteger |
|
1210 | 14 |
instanceVariableNames:'sign digitByteArray' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
1794 | 20 |
!LargeInteger class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
345 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
This class provides arbitrary precision integers. These are represented as: |
|
39 |
sign (-1/0/+1) and, if sign ~~ 0 |
|
40 |
a ByteArray of digits with 8 bits per element; |
|
88 | 41 |
least significant 8 bits at index 1 ... |
1 | 42 |
|
88 | 43 |
The implementation is definitely not tuned for high performance |
44 |
(some key-methods should be rewritten as primitives), |
|
77 | 45 |
but ok for now, since LargeIntegers are not used very often. |
46 |
It will be reimplemented when everything else runs fine and a need arises |
|
47 |
(or someone rewrites it and sends me the code :-). |
|
48 |
||
49 |
For real speed, its implementation should be mapped onto a tuned arbitrary |
|
88 | 50 |
precision math package (maybe even special-cased for 32 and 64 bit |
51 |
large-ints, since those are the most common). |
|
52 |
||
53 |
LargeIntegers are usually not created explicitely, but result from |
|
54 |
SmallInteger arithmetic overflowing the SmallInteger range. |
|
357 | 55 |
Also, results of LargeInteger operations are converted to back to |
56 |
SmallIntegers, when possible (see normalize). |
|
92 | 57 |
|
58 |
In contrast to ST-80, there is only one class for LargeIntegers, keeping |
|
59 |
the sign as an instance variable (ST-80 has LargePositiveInteger and |
|
60 |
LargeNegativeInteger). This may change. |
|
1295 | 61 |
|
62 |
[author:] |
|
2001 | 63 |
Claus Gittinger |
1556 | 64 |
|
65 |
[see also:] |
|
2001 | 66 |
Number |
67 |
Float Fraction FixedPoint |
|
68 |
SmallInteger |
|
77 | 69 |
" |
70 |
! ! |
|
1 | 71 |
|
1794 | 72 |
!LargeInteger class methodsFor:'instance creation'! |
1 | 73 |
|
74 |
new |
|
75 |
"catch creation message" |
|
76 |
||
77 |
self error:'LargeIntegers cannot be created with new' |
|
78 |
! |
|
79 |
||
701 | 80 |
new:numberOfDigits |
81 |
"catch creation message" |
|
2 | 82 |
|
701 | 83 |
self error:'LargeIntegers cannot be created with new' |
1 | 84 |
! |
85 |
||
701 | 86 |
value:aSmallInteger |
87 |
"create and return a new LargeInteger with value taken from |
|
88 |
the argument, aSmallInteger. |
|
89 |
Notice: this should be only used internally, since such small |
|
90 |
largeIntegers do not normally occur in the system. |
|
91 |
May change without notice." |
|
92 |
||
93 |
^ self basicNew value:aSmallInteger |
|
94 |
||
95 |
"LargeInteger value:3689" |
|
96 |
! ! |
|
97 |
||
1794 | 98 |
!LargeInteger class methodsFor:'queries'! |
701 | 99 |
|
100 |
isBuiltInClass |
|
1264 | 101 |
"return true if this class is known by the run-time-system. |
102 |
Here, true is returned." |
|
701 | 103 |
|
104 |
^ true |
|
1264 | 105 |
|
106 |
"Modified: 23.4.1996 / 15:59:21 / cg" |
|
1 | 107 |
! ! |
108 |
||
68 | 109 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 110 |
|
701 | 111 |
* aNumber |
112 |
"return the product of the receiver and the argument, aNumber" |
|
113 |
||
114 |
|otherSign| |
|
115 |
||
116 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
117 |
||
118 |
" |
|
119 |
this is the common case, multiplying with SmallInteger. |
|
120 |
Use a special method for this case ... |
|
121 |
" |
|
122 |
(aNumber class == SmallInteger) ifTrue:[ |
|
123 |
^ self productFromInteger:aNumber |
|
124 |
]. |
|
125 |
||
126 |
" |
|
127 |
if the argument is not a largeInteger, coerce |
|
128 |
" |
|
129 |
(aNumber class == self class) ifFalse:[ |
|
130 |
^ self retry:#* coercing:aNumber |
|
131 |
]. |
|
132 |
||
133 |
otherSign := aNumber sign. |
|
134 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
135 |
(otherSign == 0) ifTrue:[^ 0]. |
|
136 |
^ (self absMul:aNumber) sign:-1 |
|
137 |
! |
|
138 |
||
92 | 139 |
+ aNumber |
140 |
"return the sum of the receiver and the argument, aNumber" |
|
141 |
||
142 |
|otherSign| |
|
143 |
||
144 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
145 |
||
146 |
" |
|
147 |
this is the common case, adding a SmallInteger. |
|
148 |
Use a special method for this case ... |
|
149 |
" |
|
150 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 151 |
^ self sumFromInteger:aNumber |
92 | 152 |
]. |
153 |
||
154 |
" |
|
155 |
if the argument is not a largeInteger, coerce |
|
156 |
" |
|
157 |
(aNumber class == self class) ifFalse:[ |
|
345 | 158 |
^ self retry:#+ coercing:aNumber |
92 | 159 |
]. |
160 |
||
161 |
otherSign := aNumber sign. |
|
162 |
(sign > 0) ifTrue:[ |
|
345 | 163 |
"I am positive" |
164 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber]. |
|
165 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber]. |
|
166 |
^ self |
|
92 | 167 |
]. |
168 |
"I am negative" |
|
169 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self]. |
|
170 |
(otherSign < 0) ifTrue:[^ (self absPlus:aNumber) sign:-1]. |
|
171 |
^ self |
|
357 | 172 |
|
173 |
" |
|
174 |
SmallInteger maxVal |
|
175 |
SmallInteger maxVal + 1 |
|
176 |
SmallInteger maxVal + 2 |
|
177 |
SmallInteger minVal |
|
178 |
SmallInteger minVal - 1 |
|
179 |
" |
|
92 | 180 |
! |
181 |
||
182 |
- aNumber |
|
183 |
"return the difference of the receiver and the argument, aNumber" |
|
184 |
||
1794 | 185 |
|otherSign| |
92 | 186 |
|
187 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
188 |
||
189 |
" |
|
190 |
this is the common case, subtracting a SmallInteger. |
|
191 |
Use a special method for this case ... |
|
192 |
" |
|
193 |
(aNumber class == SmallInteger) ifTrue:[ |
|
2001 | 194 |
sign < 0 ifTrue:[ |
195 |
aNumber > 0 ifTrue:[ |
|
196 |
^ (self absFastPlus:aNumber) sign:-1 |
|
197 |
]. |
|
198 |
^ (self absFastMinus:aNumber) sign:-1 |
|
199 |
]. |
|
200 |
aNumber > 0 ifTrue:[ |
|
201 |
^ self absFastMinus:aNumber |
|
202 |
]. |
|
203 |
^ self absFastPlus:aNumber |
|
92 | 204 |
]. |
205 |
||
206 |
" |
|
207 |
if the argument is not a largeInteger, coerce |
|
208 |
" |
|
209 |
(aNumber class == self class) ifFalse:[ |
|
2001 | 210 |
^ self retry:#- coercing:aNumber |
92 | 211 |
]. |
212 |
||
213 |
otherSign := aNumber sign. |
|
214 |
(sign > 0) ifTrue:[ |
|
2001 | 215 |
"I am positive" |
216 |
(otherSign > 0) ifTrue:[^ self absMinus:aNumber]. |
|
217 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber]. |
|
218 |
^ self |
|
92 | 219 |
]. |
220 |
"I am negative" |
|
221 |
(otherSign > 0) ifTrue:[^ (self absPlus:aNumber) negated]. |
|
222 |
(otherSign < 0) ifTrue:[^ (self absMinus:aNumber) negated]. |
|
223 |
^ self |
|
224 |
||
225 |
" |
|
226 |
12345678901234567890 - 0 |
|
357 | 227 |
12345678901234567890 - 1 |
92 | 228 |
12345678901234567890 - -1 |
229 |
-12345678901234567890 - 1 |
|
230 |
-12345678901234567890 - -1 |
|
231 |
||
232 |
12345678901234567890 - 12345678901234567880 |
|
233 |
12345678901234567890 - 12345000000000000000 |
|
234 |
12345678901234567890 - -87654321098765432110 |
|
235 |
-12345678901234567890 - 87654321098765432110 |
|
236 |
-12345678901234567890 - -12345678901234567880 |
|
237 |
-12345678901234567890 - -12345678901234567980 |
|
238 |
" |
|
1794 | 239 |
|
240 |
"Modified: 20.10.1996 / 18:42:16 / cg" |
|
92 | 241 |
! |
242 |
||
701 | 243 |
/ aNumber |
1065 | 244 |
"return the quotient of the receiver and the argument, aNumber" |
92 | 245 |
|
701 | 246 |
aNumber isInteger ifTrue:[ |
247 |
^ (Fraction numerator:self |
|
248 |
denominator:aNumber) reduced |
|
92 | 249 |
]. |
250 |
||
701 | 251 |
"this is a q&d hack - we loose lots of precision here ..." |
252 |
^ (self asFloat / aNumber asFloat) |
|
92 | 253 |
! |
254 |
||
68 | 255 |
// aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
256 |
"return the quotient of the receiver and the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
257 |
The result is truncated toward negative infinity and negative, |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
258 |
if the operands signs differ." |
1 | 259 |
|
1890 | 260 |
|otherSign divMod d quo abs "{ Class: SmallInteger }" n| |
1 | 261 |
|
262 |
otherSign := aNumber sign. |
|
263 |
||
92 | 264 |
" |
265 |
this is the common case, dividing by a SmallInteger. |
|
266 |
Use a special method for this case ... |
|
267 |
" |
|
68 | 268 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 269 |
abs := aNumber. |
270 |
abs := abs abs. |
|
271 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
272 |
divMod := self absFastDiv:abs. |
|
273 |
quo := divMod at:1. |
|
274 |
(sign == otherSign) ifTrue:[^ quo]. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
275 |
|
2001 | 276 |
"/ stupid adjust ... |
277 |
(divMod at:2) == 0 ifFalse:[ |
|
278 |
^ (quo sign:-1) - 1 |
|
279 |
]. |
|
280 |
^ quo sign:-1 |
|
281 |
]. |
|
282 |
n := aNumber asLargeInteger. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
283 |
] ifFalse:[ |
2001 | 284 |
n := aNumber |
1 | 285 |
]. |
68 | 286 |
|
92 | 287 |
" |
288 |
if the argument is not a largeInteger, coerce |
|
289 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
290 |
(n class == self class) ifFalse:[ |
2001 | 291 |
^ self retry:#// coercing:aNumber |
1 | 292 |
]. |
293 |
||
1890 | 294 |
divMod := self absDiv:n. |
295 |
||
296 |
(sign == otherSign) ifTrue:[^ divMod at:1]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
297 |
|
1890 | 298 |
"/ stupid adjust for truncation ... |
299 |
quo := divMod at:1. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
300 |
(divMod at:2) == 0 ifFalse:[ |
2001 | 301 |
^ (quo sign:-1) - 1 |
1 | 302 |
]. |
1890 | 303 |
^ quo sign:-1 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
304 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
305 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
306 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
307 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
308 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
309 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
310 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
311 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
312 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
313 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
314 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
315 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
316 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
317 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
318 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
319 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
320 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
321 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
322 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
323 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
324 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
325 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
326 |
|
1890 | 327 |
"Modified: 5.11.1996 / 16:39:36 / cg" |
68 | 328 |
! |
329 |
||
330 |
\\ aNumber |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
331 |
"return the remainder of division of the receiver by the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
332 |
The sign of the result is that of aNumber." |
68 | 333 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
334 |
|abs rem| |
68 | 335 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
336 |
abs := aNumber abs. |
92 | 337 |
|
338 |
" |
|
339 |
this is the common case, dividing by a SmallInteger. |
|
340 |
Use a special method for this case ... |
|
341 |
" |
|
68 | 342 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 343 |
(abs between:1 and:16r003fffff) ifTrue:[ |
344 |
rem := (self absFastDiv:abs) at:2. |
|
345 |
] ifFalse:[ |
|
346 |
rem := (self absDiv:abs asLargeInteger) at:2 |
|
347 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
348 |
] ifFalse:[ |
2001 | 349 |
" |
350 |
if the argument is not a largeInteger, coerce |
|
351 |
" |
|
352 |
(aNumber class == self class) ifFalse:[ |
|
353 |
^ self retry:#\\ coercing:aNumber |
|
354 |
]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
355 |
|
2001 | 356 |
rem := (self absDiv:abs) at:2. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
357 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
358 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
359 |
aNumber negative ifTrue:[ |
2001 | 360 |
^ rem sign:-1 |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
361 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
362 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
363 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
364 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
365 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
366 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
367 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
368 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
369 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
370 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
371 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
372 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
373 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
374 |
|
1890 | 375 |
9000000000 \\ 7 |
376 |
-9000000000 \\ 7 |
|
377 |
9000000000 \\ -7 |
|
378 |
-9000000000 \\ -7 |
|
379 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
380 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
381 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
382 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
383 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
384 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
385 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
386 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
387 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
388 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
389 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
390 |
|
1890 | 391 |
"Modified: 5.11.1996 / 17:10:10 / cg" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
392 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
393 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
394 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
395 |
"return an array filled with self // aNumber and |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
396 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
397 |
The result is only defined for positive receiver and |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
398 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
399 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
400 |
|otherSign divMod d rem abs "{ Class: SmallInteger }" | |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
401 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
402 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
403 |
this is the common case, dividing by a SmallInteger. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
404 |
Use a special method for this case ... |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
405 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
406 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 407 |
^ self absFastDiv:aNumber abs. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
408 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
409 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
410 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
411 |
if the argument is not a largeInteger, coerce |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
412 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
413 |
(aNumber class == self class) ifTrue:[ |
2001 | 414 |
^ self absDiv:aNumber abs |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
415 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
416 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
417 |
^ super divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
418 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
419 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
420 |
9000000000 // 4000000000 => 2 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
421 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
422 |
9000000000 \\ 4000000000 => 1000000000 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
423 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
424 |
9000000000 divMod: 4000000000 => #(2 1000000000) |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
425 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
426 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
427 |
"Modified: 29.10.1996 / 20:44:23 / cg" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
428 |
"Created: 29.10.1996 / 21:22:05 / cg" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
429 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
430 |
|
1210 | 431 |
negated |
432 |
"return an integer with value negated from the receivers value." |
|
433 |
||
434 |
|newNumber| |
|
435 |
||
436 |
(sign == 0) ifTrue:[^ 0]. |
|
437 |
||
438 |
" |
|
439 |
special case for SmallInteger minVal |
|
440 |
" |
|
441 |
sign == 1 ifTrue:[ |
|
442 |
digitByteArray size == 4 ifTrue:[ |
|
443 |
(digitByteArray at:1) == 0 ifTrue:[ |
|
444 |
(digitByteArray at:2) == 0 ifTrue:[ |
|
445 |
(digitByteArray at:3) == 0 ifTrue:[ |
|
446 |
(digitByteArray at:4) == 16r40 ifTrue:[ |
|
447 |
^ SmallInteger minVal |
|
448 |
]. |
|
449 |
] |
|
450 |
] |
|
451 |
] |
|
452 |
] |
|
453 |
]. |
|
454 |
newNumber := self shallowCopy. |
|
455 |
newNumber sign:(sign negated). |
|
456 |
^ newNumber |
|
457 |
! |
|
458 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
459 |
quo:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
460 |
"return the quotient of the receiver and the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
461 |
The result is truncated toward zero (which is different from //, which |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
462 |
truncates toward negative infinity). |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
463 |
The results sign is negative if the receiver has a sign |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
464 |
different from the args sign" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
465 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
466 |
|otherSign d quo abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
467 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
468 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
469 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
470 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
471 |
this is the common case, dividing by a SmallInteger. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
472 |
Use a special method for this case ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
473 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
474 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 475 |
abs := aNumber. |
476 |
abs := abs abs. |
|
477 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
478 |
quo := (self absFastDiv:abs) at:1. |
|
479 |
(sign == otherSign) ifTrue:[^ quo]. |
|
480 |
^ quo sign:-1 |
|
481 |
] |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
482 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
483 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
484 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
485 |
if the argument is not a largeInteger, coerce |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
486 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
487 |
(aNumber class == self class) ifFalse:[ |
2001 | 488 |
^ self retry:#quo: coercing:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
489 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
490 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
491 |
sign < 0 ifTrue:[ |
2001 | 492 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber negated) at:1]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
493 |
] ifFalse:[ |
2001 | 494 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:1]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
495 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
496 |
^ ((self absDiv:aNumber) at:1) sign:-1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
497 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
498 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
499 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
500 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
501 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
502 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
503 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
504 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
505 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
506 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
507 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
508 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
509 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
510 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
511 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
512 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
513 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
514 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
515 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
516 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
517 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
518 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
519 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
520 |
"Modified: 5.11.1996 / 14:14:17 / cg" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
521 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
522 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
523 |
rem:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
524 |
"return the remainder of division of the receiver by the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
525 |
The returned remainder has the same sign as the receiver." |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
526 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
527 |
|rem abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
528 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
529 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
530 |
this is the common case, dividing by a SmallInteger. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
531 |
Use special code for this case ... |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
532 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
533 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 534 |
abs := aNumber. |
535 |
abs := abs abs. |
|
536 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
537 |
rem := (self absFastDiv:abs) at:2. |
|
538 |
] ifFalse:[ |
|
539 |
rem := (self absDiv:(abs asLargeInteger)) at:2 |
|
540 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
541 |
] ifFalse:[ |
2001 | 542 |
" |
543 |
if the argument is not a largeInteger, coerce |
|
544 |
" |
|
545 |
(aNumber class == self class) ifFalse:[ |
|
546 |
^ self retry:#\\ coercing:aNumber |
|
547 |
]. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
548 |
|
2001 | 549 |
rem := (self absDiv:aNumber) at:2 |
1 | 550 |
]. |
92 | 551 |
|
552 |
sign < 0 ifTrue:[ |
|
2001 | 553 |
^ rem sign:-1 |
1 | 554 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
555 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
556 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
557 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
558 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
559 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
560 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
561 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
562 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
563 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
564 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
565 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
566 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
567 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
568 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
569 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
570 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
571 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
572 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
573 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
574 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
575 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
576 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
577 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
578 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
579 |
"Modified: 5.11.1996 / 14:02:59 / cg" |
1 | 580 |
! ! |
581 |
||
701 | 582 |
!LargeInteger methodsFor:'byte access'! |
583 |
||
584 |
digitAt:index |
|
585 |
"return 8 bits of value, starting at byte index" |
|
586 |
||
587 |
index > digitByteArray size ifTrue:[^ 0]. |
|
588 |
^ digitByteArray at:index |
|
589 |
! |
|
590 |
||
591 |
digitAt:index put:aByte |
|
592 |
"set the 8 bits, index is a byte index" |
|
593 |
||
594 |
digitByteArray at:index put:aByte |
|
595 |
! |
|
596 |
||
597 |
digitLength |
|
598 |
"return the number bytes used by this Integer" |
|
599 |
||
600 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
601 |
check if there is a 0-byte ... |
701 | 602 |
this allows to ask unnormalized LargeIntegers |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
603 |
for their digitLength |
701 | 604 |
" |
605 |
|l "{ Class: SmallInteger }" | |
|
92 | 606 |
|
701 | 607 |
l := digitByteArray size. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
608 |
[l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ |
701 | 609 |
l := l - 1. |
610 |
]. |
|
611 |
^ l |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
612 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
613 |
"Modified: 2.2.1996 / 16:47:21 / cg" |
701 | 614 |
! |
615 |
||
616 |
digits |
|
617 |
"return a byteArray fille with the receivers bits |
|
618 |
(8 bits of the absolute value per element)" |
|
619 |
||
620 |
^ digitByteArray |
|
621 |
! ! |
|
622 |
||
623 |
!LargeInteger methodsFor:'coercing & converting'! |
|
68 | 624 |
|
701 | 625 |
asFloat |
626 |
"return a Float with same value as myself. |
|
627 |
Since floats have a limited precision, you usually loose bits when |
|
628 |
doing this." |
|
629 |
||
630 |
|newFloat| |
|
631 |
||
632 |
newFloat := 0.0. |
|
633 |
(sign == 0) ifFalse:[ |
|
634 |
digitByteArray reverseDo:[:aDigit | |
|
635 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
636 |
]. |
|
637 |
(sign < 0) ifTrue:[ |
|
638 |
newFloat := newFloat negated |
|
345 | 639 |
] |
701 | 640 |
]. |
641 |
^ newFloat |
|
642 |
||
643 |
" |
|
644 |
1234567890 asFloat |
|
645 |
12345678901234567890 asFloat |
|
646 |
12345678901234567890 asFloat asInteger |
|
647 |
" |
|
648 |
! |
|
649 |
||
650 |
asLargeInteger |
|
651 |
"return a LargeInteger with same value as myself - thats me" |
|
652 |
||
653 |
^ self |
|
654 |
! |
|
655 |
||
656 |
asSmallInteger |
|
657 |
"return a SmallInteger with same value as myself - |
|
658 |
the result is invalid if the receivers value cannot |
|
659 |
be represented as a SmallInteger. |
|
660 |
Q: should we raise an exception if this happens ?" |
|
661 |
||
662 |
|value| |
|
663 |
||
664 |
value := 0. |
|
665 |
(sign == 0) ifFalse:[ |
|
666 |
digitByteArray reverseDo:[:aDigit | |
|
667 |
value := (value times:256) + aDigit |
|
345 | 668 |
]. |
701 | 669 |
(sign < 0) ifTrue:[ |
670 |
value := value negated |
|
345 | 671 |
] |
68 | 672 |
]. |
701 | 673 |
^ value |
674 |
! |
|
675 |
||
676 |
coerce:aNumber |
|
677 |
"return the argument as a LargeInteger" |
|
678 |
||
679 |
^ aNumber asLargeInteger |
|
680 |
! |
|
681 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
682 |
compressed |
701 | 683 |
"if the receiver can be represented as a SmallInteger, return |
684 |
a SmallInteger with my value; otherwise return self with leading |
|
685 |
zeros removed" |
|
686 |
||
687 |
|index "{ Class: SmallInteger }" |
|
688 |
val "{ Class: SmallInteger }" | |
|
689 |
||
690 |
%{ /* NOCONTEXT */ |
|
691 |
OBJ t; |
|
692 |
||
1133 | 693 |
if (__INST(sign) == __MKSMALLINT(0)) { |
2001 | 694 |
RETURN (__MKSMALLINT(0)); |
701 | 695 |
} |
696 |
||
1133 | 697 |
t = __INST(digitByteArray); |
701 | 698 |
if (__isByteArray(t)) { |
2001 | 699 |
unsigned char *_digitBytes = __ByteArrayInstPtr(t)->ba_element; |
700 |
int _idx = _byteArraySize(t); |
|
701 |
int _val; |
|
701 | 702 |
|
2001 | 703 |
while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) { |
704 |
_idx--; |
|
705 |
} |
|
706 |
switch (_idx) { |
|
707 |
case 0: |
|
708 |
RETURN (__MKSMALLINT(0)); |
|
709 |
break; |
|
701 | 710 |
|
2001 | 711 |
case 1: |
712 |
_val = _digitBytes[0]; |
|
713 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
714 |
_val = -_val; |
|
715 |
RETURN (__MKSMALLINT(_val)); |
|
701 | 716 |
|
2001 | 717 |
case 2: |
718 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
719 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
720 |
_val = -_val; |
|
721 |
RETURN (__MKSMALLINT(_val)); |
|
701 | 722 |
|
2001 | 723 |
case 3: |
724 |
_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
725 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
726 |
_val = -_val; |
|
727 |
RETURN (__MKSMALLINT(_val)); |
|
701 | 728 |
|
2001 | 729 |
case 4: |
730 |
_val = _digitBytes[3]; |
|
731 |
if (_val <= 0x40) { |
|
732 |
_val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
733 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
734 |
_val = -_val; |
|
735 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
|
736 |
RETURN (__MKSMALLINT(_val)); |
|
737 |
} |
|
738 |
} |
|
739 |
break; |
|
701 | 740 |
|
2001 | 741 |
default: |
742 |
break; |
|
743 |
} |
|
701 | 744 |
} |
745 |
%}. |
|
746 |
index := digitByteArray size. |
|
747 |
[(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ |
|
2001 | 748 |
index := index - 1 |
701 | 749 |
]. |
750 |
||
751 |
(index ~~ digitByteArray size) ifTrue:[ |
|
2001 | 752 |
digitByteArray := digitByteArray copyFrom:1 to:index |
701 | 753 |
]. |
754 |
^ self |
|
755 |
! |
|
756 |
||
757 |
value:aSmallInteger |
|
758 |
"return a new LargeInteger with value taken from a SmallInteger. |
|
759 |
This method will fail, if the argument is not a smallInteger." |
|
760 |
||
761 |
|absValue |
|
762 |
b1 "{ Class: SmallInteger }" |
|
763 |
b2 "{ Class: SmallInteger }" |
|
764 |
b3 "{ Class: SmallInteger }"| |
|
92 | 765 |
|
766 |
" |
|
701 | 767 |
could have simply created a 4-byte largeinteger and normalize |
768 |
it; the code below does the normalize right away, avoiding the |
|
769 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
92 | 770 |
" |
701 | 771 |
(aSmallInteger == 0) ifTrue: [ |
2001 | 772 |
digitByteArray := ByteArray with:0. |
773 |
sign := 0. |
|
774 |
^ self |
|
701 | 775 |
]. |
776 |
(aSmallInteger < 0) ifTrue: [ |
|
2001 | 777 |
sign := -1. |
778 |
absValue := aSmallInteger negated |
|
701 | 779 |
] ifFalse: [ |
2001 | 780 |
sign := 1. |
781 |
absValue := aSmallInteger |
|
701 | 782 |
]. |
783 |
b1 := absValue bitAnd:16rFF. |
|
784 |
absValue := absValue bitShift:-8. |
|
785 |
absValue == 0 ifTrue:[ |
|
2001 | 786 |
digitByteArray := ByteArray with:b1 |
701 | 787 |
] ifFalse:[ |
2001 | 788 |
b2 := absValue bitAnd:16rFF. |
789 |
absValue := absValue bitShift:-8. |
|
790 |
absValue == 0 ifTrue:[ |
|
791 |
digitByteArray := ByteArray with:b1 with:b2 |
|
792 |
] ifFalse:[ |
|
793 |
b3 := absValue bitAnd:16rFF. |
|
794 |
absValue := absValue bitShift:-8. |
|
795 |
absValue == 0 ifTrue:[ |
|
796 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
797 |
] ifFalse:[ |
|
798 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:absValue |
|
799 |
] |
|
800 |
] |
|
701 | 801 |
] |
1890 | 802 |
|
803 |
"Modified: 5.11.1996 / 16:15:39 / cg" |
|
701 | 804 |
! ! |
805 |
||
806 |
!LargeInteger methodsFor:'comparing'! |
|
807 |
||
808 |
< aNumber |
|
809 |
"return true, if the argument, aNumber is greater than the receiver" |
|
810 |
||
811 |
|otherSign| |
|
812 |
||
813 |
(aNumber class == self class) ifFalse:[ |
|
814 |
^ self retry:#< coercing:aNumber |
|
815 |
]. |
|
816 |
otherSign := aNumber sign. |
|
817 |
||
818 |
(sign > 0) ifTrue:[ |
|
819 |
"I am positive" |
|
820 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
821 |
^ false "aNumber is <= 0" |
|
822 |
]. |
|
823 |
(sign == 0) ifTrue:[ |
|
824 |
(otherSign > 0) ifTrue:[^ true]. |
|
825 |
^ false |
|
826 |
]. |
|
827 |
"I am negative" |
|
828 |
(otherSign > 0) ifTrue:[^ true]. |
|
829 |
(otherSign == 0) ifTrue:[^ true]. |
|
830 |
^ (self absLess:aNumber) not |
|
92 | 831 |
! |
832 |
||
701 | 833 |
= aNumber |
834 |
"return true, if the argument, aNumber has the same value as |
|
835 |
the receiver" |
|
836 |
||
837 |
(aNumber class == self class) ifFalse:[ |
|
838 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
|
839 |
^ self retry:#= coercing:aNumber |
|
840 |
]. |
|
841 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
842 |
^ self absEq:aNumber |
|
843 |
! |
|
844 |
||
845 |
> aNumber |
|
846 |
"return true, if the argument, aNumber is less than the receiver" |
|
847 |
||
848 |
|otherSign| |
|
849 |
||
850 |
(aNumber class == self class) ifFalse:[ |
|
851 |
^ self retry:#> coercing:aNumber |
|
852 |
]. |
|
853 |
otherSign := aNumber sign. |
|
854 |
||
855 |
(sign > 0) ifTrue:[ |
|
856 |
"I am positive" |
|
857 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
858 |
^ true "aNumber is <= 0" |
|
859 |
]. |
|
860 |
(sign == 0) ifTrue:[ |
|
861 |
"I am zero" |
|
862 |
(otherSign > 0) ifTrue:[^ false]. |
|
863 |
^ true |
|
864 |
]. |
|
865 |
"I am negative" |
|
866 |
(otherSign > 0) ifTrue:[^ false]. |
|
867 |
(otherSign == 0) ifTrue:[^ false]. |
|
868 |
^ (self absLess:aNumber) not |
|
869 |
! ! |
|
870 |
||
871 |
!LargeInteger methodsFor:'double dispatching'! |
|
872 |
||
92 | 873 |
differenceFromInteger:anInteger |
874 |
"sent, when anInteger does not know how to subtract the receiver. |
|
875 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
876 |
||
877 |
anInteger > 0 ifTrue:[ |
|
2001 | 878 |
sign > 0 ifTrue:[ |
879 |
^ (self absFastMinus:anInteger) sign:-1 |
|
880 |
]. |
|
881 |
^ self absFastPlus:anInteger |
|
92 | 882 |
]. |
1794 | 883 |
anInteger == 0 ifTrue:[ |
2001 | 884 |
^ self negated |
1794 | 885 |
]. |
886 |
sign > 0 ifTrue:[ |
|
2001 | 887 |
^ (self absFastPlus:anInteger negated) sign:-1 |
1794 | 888 |
]. |
889 |
^ (self absFastMinus:anInteger) sign:-1 |
|
92 | 890 |
|
891 |
" |
|
892 |
12345678901234567890 |
|
893 |
-12345678901234567890 |
|
894 |
12345678901234567890 differenceFromInteger:0 |
|
895 |
12345678901234567890 differenceFromInteger:1 |
|
896 |
12345678901234567890 differenceFromInteger:-1 |
|
897 |
-12345678901234567890 differenceFromInteger:1 |
|
898 |
-12345678901234567890 differenceFromInteger:-1 |
|
899 |
" |
|
1794 | 900 |
|
901 |
"Modified: 20.10.1996 / 18:41:54 / cg" |
|
68 | 902 |
! |
903 |
||
904 |
productFromInteger:anInteger |
|
905 |
"sent, when anInteger does not know how to multiply the receiver. |
|
906 |
Return the product of the receiver and the argument, aSmallInteger" |
|
907 |
||
908 |
|num result |
|
909 |
resultDigitByteArray |
|
910 |
val "{ Class: SmallInteger }" |
|
911 |
len "{ Class: SmallInteger }" |
|
912 |
carry "{ Class: SmallInteger }" |
|
85 | 913 |
prod "{ Class: SmallInteger }" |
914 |
ok| |
|
68 | 915 |
|
916 |
"multiplying by a small integer is done here" |
|
917 |
||
918 |
"trivial cases" |
|
919 |
anInteger == 0 ifTrue:[^ 0]. |
|
920 |
anInteger == 1 ifTrue:[^ self]. |
|
921 |
||
922 |
num := anInteger abs. |
|
923 |
(num > 16r3FFFFF) ifTrue:[ |
|
2001 | 924 |
"if num is too big (so that multiplying by a byte could create a Large)" |
68 | 925 |
|
2001 | 926 |
^ anInteger retry:#* coercing:self |
68 | 927 |
]. |
928 |
||
929 |
len := digitByteArray size. |
|
930 |
||
931 |
result := self class basicNew numberOfDigits:(len + 4). |
|
1 | 932 |
|
68 | 933 |
anInteger < 0 ifTrue:[ |
2001 | 934 |
sign > 0 ifTrue:[ |
935 |
result sign:-1 |
|
936 |
]. |
|
68 | 937 |
] ifFalse:[ |
2001 | 938 |
sign < 0 ifTrue:[ |
939 |
result sign:sign |
|
940 |
] |
|
68 | 941 |
]. |
942 |
||
943 |
resultDigitByteArray := result digits. |
|
944 |
||
945 |
carry := 0. |
|
946 |
val := num. |
|
947 |
||
85 | 948 |
ok := false. |
949 |
%{ |
|
250 | 950 |
if (__bothSmallInteger(len, val) |
1133 | 951 |
&& __isByteArray(__INST(digitByteArray)) |
85 | 952 |
&& __isByteArray(resultDigitByteArray)) { |
2001 | 953 |
int _l = __intVal(len); |
954 |
int _v = __intVal(val); |
|
955 |
unsigned _carry = 0; |
|
956 |
unsigned _prod; |
|
957 |
unsigned char *digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
|
958 |
unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
959 |
|
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
960 |
#if defined(i386) /* XXX actually: LSB_FIRST */ |
2001 | 961 |
/* max: 0xFFFF * 0xFFFF -> 0xrFFFE0001 |
962 |
* + maxCarry (0xFFFF) -> 0xFFFF0000 |
|
963 |
*/ |
|
85 | 964 |
|
2001 | 965 |
if (_v <= 0xFFFF) { |
966 |
/* can do it short-wise */ |
|
967 |
while (_l > 1) { |
|
968 |
_prod = ((unsigned short *)digitP)[0] * _v + _carry; |
|
969 |
((unsigned short *)resultP)[0] = _prod & 0xFFFF; |
|
970 |
_carry = _prod >> 16; |
|
971 |
digitP += 2; |
|
972 |
resultP += 2; |
|
973 |
_l -= 2; |
|
974 |
} |
|
975 |
} |
|
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
976 |
#endif |
2001 | 977 |
{ |
978 |
while (_l-- > 0) { |
|
979 |
_prod = *digitP++ * _v + _carry; |
|
980 |
*resultP++ = _prod & 0xFF; |
|
981 |
_carry = _prod >> 8; |
|
982 |
} |
|
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
983 |
|
2001 | 984 |
while (_carry) { |
985 |
*resultP++ = _carry & 0xFF; |
|
986 |
_carry >>= 8; |
|
987 |
} |
|
988 |
} |
|
989 |
ok = true; |
|
85 | 990 |
} |
991 |
%}. |
|
92 | 992 |
" |
993 |
fall back - normally not reached |
|
994 |
(could make it a primitive-failure as well) |
|
995 |
" |
|
85 | 996 |
ok ifFalse:[ |
2001 | 997 |
1 to:len do:[:i | |
998 |
prod := (digitByteArray basicAt:i) * val + carry. |
|
999 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
|
1000 |
carry := prod bitShift:-8. |
|
1001 |
]. |
|
1002 |
[carry ~~ 0] whileTrue:[ |
|
1003 |
len := len + 1. |
|
1004 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
|
1005 |
carry := carry bitShift:-8 |
|
1006 |
]. |
|
68 | 1007 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1008 |
^ result compressed |
92 | 1009 |
! |
1010 |
||
701 | 1011 |
sumFromInteger:anInteger |
1012 |
"sent, when anInteger does not know how to add the receiver. |
|
1013 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
|
92 | 1014 |
|
701 | 1015 |
anInteger > 0 ifTrue:[ |
2001 | 1016 |
sign < 0 ifTrue:[ |
1017 |
^ (self absFastMinus:anInteger) sign:-1 |
|
1018 |
]. |
|
1019 |
^ self absFastPlus:anInteger |
|
701 | 1020 |
]. |
1794 | 1021 |
anInteger == 0 ifTrue:[ |
2001 | 1022 |
^ self |
1794 | 1023 |
]. |
1024 |
sign < 0 ifTrue:[ |
|
2001 | 1025 |
^ (self absFastPlus:anInteger abs) sign:-1 |
1794 | 1026 |
]. |
1027 |
^ self absFastMinus:anInteger |
|
92 | 1028 |
|
701 | 1029 |
" |
1030 |
12345678901234567890 |
|
1031 |
-12345678901234567890 |
|
1032 |
12345678901234567890 sumFromInteger:0 |
|
1033 |
-12345678901234567890 sumFromInteger:0 |
|
1034 |
12345678901234567890 sumFromInteger:1 |
|
1035 |
12345678901234567890 sumFromInteger:-1 |
|
1036 |
-12345678901234567890 sumFromInteger:1 |
|
1037 |
-12345678901234567890 sumFromInteger:-1 |
|
1038 |
" |
|
1794 | 1039 |
|
1040 |
"Modified: 20.10.1996 / 18:41:51 / cg" |
|
68 | 1041 |
! ! |
1042 |
||
701 | 1043 |
!LargeInteger methodsFor:'printing & storing'! |
345 | 1044 |
|
701 | 1045 |
storeOn:aStream |
1046 |
"append a representation of the receiver to aStream, which can |
|
1047 |
be used to reconstruct the receiver." |
|
68 | 1048 |
|
701 | 1049 |
self printOn:aStream. |
1050 |
aStream nextPutAll:' asLargeInteger' |
|
68 | 1051 |
! ! |
1052 |
||
1053 |
!LargeInteger methodsFor:'private'! |
|
1054 |
||
701 | 1055 |
absDiv:anInteger |
1056 |
"return an array with two LargeIntegers representing |
|
1057 |
abs(self) // abs(theArgument) and abs(self) \\ abs(theArgument). |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1058 |
Used as a helper for \\, //, rem: and quo:. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1059 |
This method needs a rewrite." |
68 | 1060 |
|
701 | 1061 |
|tmp1 tmp2 |
1890 | 1062 |
quo |
701 | 1063 |
count "{ Class: SmallInteger }" |
1064 |
digit "{ Class: SmallInteger }" | |
|
92 | 1065 |
|
701 | 1066 |
anInteger == 0 ifTrue:[ |
2001 | 1067 |
^ DivisionByZeroSignal raise |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1068 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1069 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1070 |
self = anInteger ifTrue:[ |
2001 | 1071 |
^ Array with:1 with:0 |
701 | 1072 |
]. |
92 | 1073 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1074 |
tmp1 := self simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1075 |
tmp1 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1076 |
tmp2 := anInteger simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1077 |
tmp2 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1078 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1079 |
(tmp1 < tmp2) ifTrue:[ |
2001 | 1080 |
^ Array with:0 with:tmp1 |
68 | 1081 |
]. |
92 | 1082 |
|
701 | 1083 |
count := 0. |
1890 | 1084 |
[tmp2 absLess: tmp1] whileTrue:[ |
2001 | 1085 |
tmp2 mul2. |
1086 |
count := count + 1 |
|
701 | 1087 |
]. |
1088 |
||
1890 | 1089 |
tmp2 div2. |
701 | 1090 |
|
1890 | 1091 |
quo := 0 asLargeInteger. |
1092 |
quo sign:1. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1093 |
|
701 | 1094 |
[count == 0] whileFalse:[ |
2001 | 1095 |
quo mul2. |
1096 |
(tmp1 absLess:tmp2) ifFalse:[ |
|
1097 |
quo digits at:1 put:((quo digits at:1) bitOr:1). |
|
1098 |
(tmp1 absSubtract: tmp2) ifFalse:[ |
|
1099 |
"/ difference is zero; continue shifting |
|
1100 |
count := count - 1. |
|
1101 |
[count >= 8] whileTrue:[ |
|
1102 |
quo mul256. |
|
1103 |
count := count - 8 |
|
1104 |
]. |
|
1105 |
[count == 0] whileFalse:[ |
|
1106 |
quo mul2. |
|
1107 |
count := count - 1. |
|
1108 |
]. |
|
1109 |
^ Array with:quo compressed with:tmp1 compressed |
|
1110 |
]. |
|
1111 |
]. |
|
1112 |
tmp2 div2. |
|
1113 |
count := count - 1 |
|
701 | 1114 |
]. |
1890 | 1115 |
^ Array with:quo compressed with:tmp1 compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1116 |
|
1890 | 1117 |
"Modified: 5.11.1996 / 18:40:24 / cg" |
92 | 1118 |
! |
1119 |
||
701 | 1120 |
absEq:aLargeInteger |
1121 |
"return true, if abs(self) = abs(theArgument)" |
|
92 | 1122 |
|
701 | 1123 |
|len1 "{ Class: SmallInteger }" |
1124 |
len2 "{ Class: SmallInteger }" |
|
1125 |
d1 "{ Class: SmallInteger }" |
|
1126 |
d2 "{ Class: SmallInteger }" |
|
1127 |
otherDigitByteArray | |
|
92 | 1128 |
|
701 | 1129 |
len1 := digitByteArray size. |
1130 |
otherDigitByteArray := aLargeInteger digits. |
|
1131 |
len2 := otherDigitByteArray size. |
|
92 | 1132 |
|
701 | 1133 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
1134 |
len1 := len1 - 1 |
|
1135 |
]. |
|
1136 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
1137 |
len2 := len2 - 1 |
|
68 | 1138 |
]. |
701 | 1139 |
(len1 ~~ len2) ifTrue:[^ false]. |
1140 |
[len1 > 0] whileTrue:[ |
|
1141 |
d1 := digitByteArray basicAt:len1. |
|
1142 |
d2 := otherDigitByteArray basicAt:len1. |
|
1143 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1144 |
len1 := len1 - 1 |
|
68 | 1145 |
]. |
701 | 1146 |
^ true |
68 | 1147 |
! |
1148 |
||
1890 | 1149 |
absFastDiv:aPositiveSmallInteger |
68 | 1150 |
"return an array with two LargeIntegers representing |
1890 | 1151 |
abs(self) // aPositiveSmallInteger and abs(self) \\ aPositiveSmallInteger" |
68 | 1152 |
|
77 | 1153 |
|tmp1 "{ Class: SmallInteger }" |
1154 |
prevRest "{ Class: SmallInteger }" |
|
1155 |
count "{ Class: SmallInteger }" |
|
85 | 1156 |
newDigitByteArray result |
1157 |
ok| |
|
68 | 1158 |
|
1890 | 1159 |
aPositiveSmallInteger == 0 ifTrue:[ |
2001 | 1160 |
^ DivisionByZeroSignal raise |
68 | 1161 |
]. |
1162 |
||
1163 |
"This cannot happen (if always normalized) |
|
1890 | 1164 |
self < aPositiveSmallInteger ifTrue:[ |
2001 | 1165 |
^ Array with:0 with:self |
68 | 1166 |
]. |
1167 |
" |
|
1168 |
count := digitByteArray size. |
|
1169 |
result := self class basicNew numberOfDigits:count. |
|
1170 |
newDigitByteArray := result digits. |
|
85 | 1171 |
ok := false. |
1172 |
%{ |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1173 |
OBJ __digits; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1174 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1175 |
__digits = __INST(digitByteArray); |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1176 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1177 |
if (__isByteArray(__digits) |
85 | 1178 |
&& __isByteArray(newDigitByteArray) |
1890 | 1179 |
&& __bothSmallInteger(count, aPositiveSmallInteger)) { |
2001 | 1180 |
unsigned int rest = 0; |
1181 |
int index = __intVal(count); |
|
1182 |
int index0; |
|
1183 |
unsigned divisor = __intVal(aPositiveSmallInteger); |
|
1184 |
unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element; |
|
1185 |
unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element; |
|
68 | 1186 |
|
2001 | 1187 |
index0 = index; |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1188 |
|
1890 | 1189 |
/* |
2001 | 1190 |
if (divisor < 0xFFFF) { |
1191 |
while (index > 1) { |
|
1192 |
unsigned int t; |
|
1193 |
unsigned div; |
|
85 | 1194 |
|
2001 | 1195 |
index--; |
1196 |
t = digitBytes[index]; |
|
1197 |
index--; |
|
1198 |
t = (t << 8) | digitBytes[index]; |
|
1199 |
t = t | (rest << 16); |
|
1200 |
div = t / divisor; |
|
1201 |
rest = t % divisor; |
|
1202 |
resultBytes[index+1] = (div >> 8); |
|
1203 |
resultBytes[index] = (div & 0xFF); |
|
1204 |
} |
|
1205 |
} |
|
1890 | 1206 |
*/ |
2001 | 1207 |
while (index > 0) { |
1208 |
unsigned int t; |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1209 |
|
2001 | 1210 |
index--; |
1211 |
t = digitBytes[index]; |
|
1212 |
t = t | (rest << 8); |
|
1213 |
resultBytes[index] = t / divisor; |
|
1214 |
rest = t % divisor; |
|
1215 |
} |
|
1216 |
prevRest = __MKSMALLINT(rest); |
|
1217 |
ok = true; |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1218 |
|
2001 | 1219 |
/* |
1220 |
* no need to normalize ? |
|
1221 |
*/ |
|
1222 |
if ((index0 > 4) |
|
1223 |
&& (resultBytes[index0-1])) { |
|
1224 |
RETURN ( __ARRAY_WITH2(result, prevRest)); |
|
1225 |
} |
|
85 | 1226 |
} |
1227 |
%}. |
|
1228 |
" |
|
1229 |
slow code - not normally reached |
|
1230 |
(could also do a primitiveFailure here) |
|
1231 |
" |
|
92 | 1232 |
ok ifFalse:[ |
2001 | 1233 |
self primitiveFailed |
68 | 1234 |
]. |
1235 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1236 |
^ Array with:result compressed with:prevRest |
68 | 1237 |
! |
1238 |
||
701 | 1239 |
absFastMinus:aSmallInteger |
1240 |
"return a LargeInteger representing abs(self) - abs(theArgument). |
|
1241 |
The result is not normalized." |
|
1242 |
||
1794 | 1243 |
|result resultDigitByteArray lastDigit |
701 | 1244 |
len "{ Class: SmallInteger }" |
1245 |
index "{ Class: SmallInteger }" |
|
1246 |
borrow "{ Class: SmallInteger }" |
|
1247 |
diff "{ Class: SmallInteger }" | |
|
1248 |
||
1249 |
len := digitByteArray size. |
|
1250 |
||
1251 |
result := self class basicNew numberOfDigits:(len + 1). |
|
1252 |
resultDigitByteArray := result digits. |
|
1253 |
||
1254 |
index := 1. |
|
1255 |
borrow := aSmallInteger abs. |
|
1256 |
||
1257 |
[borrow ~~ 0] whileTrue:[ |
|
2001 | 1258 |
(index <= len) ifTrue:[ |
1259 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
|
1260 |
borrow := borrow bitShift:-8. |
|
1261 |
diff < 0 ifTrue:[ |
|
1262 |
diff := diff + 256. |
|
1263 |
borrow := borrow + 1. |
|
1264 |
] |
|
1265 |
] ifFalse:[ |
|
1266 |
diff := borrow bitAnd:255. |
|
1267 |
borrow := borrow bitShift:-8. |
|
1268 |
]. |
|
1269 |
resultDigitByteArray basicAt:index put:(lastDigit := diff). |
|
1270 |
index := index + 1 |
|
701 | 1271 |
]. |
1272 |
[index <= len] whileTrue:[ |
|
2001 | 1273 |
resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index). |
1274 |
index := index + 1 |
|
701 | 1275 |
]. |
1276 |
||
1794 | 1277 |
lastDigit == 0 ifTrue:[ |
2001 | 1278 |
^ result compressed |
1794 | 1279 |
]. |
701 | 1280 |
^ result |
1281 |
||
1282 |
" |
|
1283 |
12345678900000000000 absFastMinus:1 |
|
1284 |
12345678900000000000 absFastMinus:1000000 |
|
1285 |
12345678900000000000 absFastMinus:255 |
|
1286 |
(SmallInteger maxVal + 1) absFastMinus:1 |
|
1287 |
(SmallInteger minVal - 1) absFastMinus:1 |
|
1288 |
" |
|
1794 | 1289 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1290 |
"Modified: 5.11.1996 / 14:09:16 / cg" |
701 | 1291 |
! |
1292 |
||
1293 |
absFastPlus:aSmallInteger |
|
1294 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
|
1295 |
The result is not normalized." |
|
1296 |
||
1794 | 1297 |
|result resultDigitByteArray lastDigit |
701 | 1298 |
len "{ Class: SmallInteger }" |
1299 |
index "{ Class: SmallInteger }" |
|
1300 |
carry "{ Class: SmallInteger }" | |
|
1301 |
||
1302 |
len := digitByteArray size. |
|
1303 |
||
1304 |
result := self class basicNew numberOfDigits:(len + 1). |
|
1305 |
resultDigitByteArray := result digits. |
|
1306 |
||
1307 |
index := 1. |
|
1308 |
carry := aSmallInteger abs. |
|
1309 |
||
1310 |
[carry ~~ 0] whileTrue:[ |
|
2001 | 1311 |
(index <= len) ifTrue:[ |
1312 |
carry := (digitByteArray basicAt:index) + carry. |
|
1313 |
]. |
|
1314 |
resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF). |
|
1315 |
carry := carry bitShift:-8. |
|
1316 |
index := index + 1 |
|
701 | 1317 |
]. |
1318 |
[index <= len] whileTrue:[ |
|
2001 | 1319 |
resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index). |
1320 |
index := index + 1 |
|
701 | 1321 |
]. |
1322 |
||
1794 | 1323 |
lastDigit == 0 ifTrue:[ |
2001 | 1324 |
^ result compressed |
1794 | 1325 |
]. |
701 | 1326 |
^ result |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1327 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1328 |
"Modified: 5.11.1996 / 14:09:19 / cg" |
701 | 1329 |
! |
1330 |
||
1331 |
absLess:aLargeInteger |
|
1890 | 1332 |
"return true, if abs(self) < abs(theArgument). |
1333 |
This handles unnormalized largeIntegers." |
|
701 | 1334 |
|
1890 | 1335 |
|myLen "{ Class: SmallInteger }" |
1336 |
otherLen "{ Class: SmallInteger }" |
|
701 | 1337 |
d1 "{ Class: SmallInteger }" |
1338 |
d2 "{ Class: SmallInteger }" |
|
1339 |
otherDigitByteArray | |
|
1340 |
||
1890 | 1341 |
myLen := digitByteArray size. |
701 | 1342 |
otherDigitByteArray := aLargeInteger digits. |
1890 | 1343 |
otherLen := otherDigitByteArray size. |
701 | 1344 |
|
1890 | 1345 |
[(digitByteArray basicAt:myLen) == 0] whileTrue:[ |
2001 | 1346 |
myLen := myLen - 1 |
701 | 1347 |
]. |
1890 | 1348 |
[(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[ |
2001 | 1349 |
otherLen := otherLen - 1 |
701 | 1350 |
]. |
1890 | 1351 |
(myLen < otherLen) ifTrue:[^ true]. |
1352 |
(myLen > otherLen) ifTrue:[^ false]. |
|
701 | 1353 |
|
1890 | 1354 |
[myLen > 0] whileTrue:[ |
2001 | 1355 |
d1 := digitByteArray basicAt:myLen. |
1356 |
d2 := otherDigitByteArray basicAt:myLen. |
|
1357 |
d1 == d2 ifFalse:[ |
|
1358 |
(d1 < d2) ifTrue:[^ true]. |
|
1359 |
^ false |
|
1360 |
]. |
|
1361 |
myLen := myLen - 1 |
|
701 | 1362 |
]. |
1363 |
^ false |
|
1890 | 1364 |
|
1365 |
"Modified: 5.11.1996 / 18:37:27 / cg" |
|
701 | 1366 |
! |
1367 |
||
1368 |
absMinus:aLargeInteger |
|
1369 |
"return a LargeInteger representing abs(self) - abs(theArgument)" |
|
1370 |
||
1371 |
|result done |
|
1372 |
otherDigitByteArray resultDigitByteArray |
|
1373 |
len1 "{ Class: SmallInteger }" |
|
1374 |
len2 "{ Class: SmallInteger }" |
|
1375 |
index "{ Class: SmallInteger }" |
|
1376 |
borrow "{ Class: SmallInteger }" |
|
1377 |
diff "{ Class: SmallInteger }" |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1378 |
carry "{ Class: SmallInteger }" |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1379 |
lResult| |
701 | 1380 |
|
1381 |
len1 := digitByteArray size. |
|
1382 |
otherDigitByteArray := aLargeInteger digits. |
|
1383 |
len2 := otherDigitByteArray size. |
|
1384 |
||
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1385 |
lResult := (len1 max: len2) + 1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1386 |
result := self class basicNew numberOfDigits:lResult. |
701 | 1387 |
resultDigitByteArray := result digits. |
1388 |
||
1389 |
index := 1. |
|
1390 |
borrow := 0. |
|
1391 |
||
1392 |
done := false. |
|
1393 |
[done] whileFalse:[ |
|
2001 | 1394 |
diff := borrow. |
1395 |
(index <= len1) ifTrue:[ |
|
1396 |
diff := diff + (digitByteArray basicAt:index). |
|
1397 |
(index <= len2) ifTrue:[ |
|
1398 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1399 |
] |
|
1400 |
] ifFalse:[ |
|
1401 |
(index <= len2) ifTrue:[ |
|
1402 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1403 |
] ifFalse:[ |
|
1404 |
"end reached" |
|
1405 |
done := true |
|
1406 |
] |
|
1407 |
]. |
|
701 | 1408 |
|
2001 | 1409 |
"/ workaround for |
1410 |
"/ gcc code generator bug |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1411 |
|
2001 | 1412 |
(diff >= 0) ifTrue:[ |
1413 |
borrow := 0 |
|
1414 |
] ifFalse:[ |
|
1415 |
borrow := -1. |
|
1416 |
diff := diff + 16r100 |
|
1417 |
]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1418 |
|
701 | 1419 |
"/ (diff < 0) ifTrue:[ |
1420 |
"/ borrow := -1. |
|
1421 |
"/ diff := diff + 16r100 |
|
1422 |
"/ ] ifFalse:[ |
|
1423 |
"/ borrow := 0 |
|
1424 |
"/ ]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1425 |
|
2001 | 1426 |
resultDigitByteArray basicAt:index put:diff. |
1427 |
index := index + 1 |
|
701 | 1428 |
]. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1429 |
|
701 | 1430 |
(borrow ~~ 0) ifTrue:[ |
2001 | 1431 |
"/ must generate 255's complement |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1432 |
|
2001 | 1433 |
result sign:-1. |
1434 |
[index <= lResult] whileTrue:[ |
|
1435 |
resultDigitByteArray basicAt:index put:16rFF. |
|
1436 |
index := index + 1. |
|
1437 |
]. |
|
1438 |
index := resultDigitByteArray size. |
|
1439 |
[index > 0] whileTrue:[ |
|
1440 |
resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)). |
|
1441 |
index := index -1. |
|
1442 |
]. |
|
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1443 |
|
2001 | 1444 |
index := 1. |
1445 |
carry := 1. |
|
1446 |
[carry ~~ 0] whileTrue:[ |
|
1447 |
(index <= lResult) ifTrue:[ |
|
1448 |
carry := (resultDigitByteArray basicAt:index) + carry. |
|
1449 |
]. |
|
1450 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
|
1451 |
carry := carry bitShift:-8. |
|
1452 |
index := index + 1 |
|
1453 |
]. |
|
701 | 1454 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1455 |
^ result compressed |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1456 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1457 |
"Modified: 5.11.1996 / 14:09:25 / cg" |
701 | 1458 |
! |
1459 |
||
68 | 1460 |
absMul:aLargeInteger |
1461 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
1462 |
||
92 | 1463 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 1464 |
idx "{ Class: SmallInteger }" |
1465 |
carry "{ Class: SmallInteger }" |
|
1466 |
len1 "{ Class: SmallInteger }" |
|
1467 |
len2 "{ Class: SmallInteger }" |
|
1468 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 1469 |
prod "{ Class: SmallInteger }" |
1470 |
v "{ Class: SmallInteger }"| |
|
68 | 1471 |
|
1472 |
len1 := digitByteArray size. |
|
1473 |
otherDigitByteArray := aLargeInteger digits. |
|
1474 |
len2 := otherDigitByteArray size. |
|
1475 |
||
1476 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1). |
|
1477 |
resultDigitByteArray := result digits. |
|
92 | 1478 |
ok := false. |
1479 |
%{ |
|
1133 | 1480 |
if (__isByteArray(__INST(digitByteArray)) |
92 | 1481 |
&& __isByteArray(otherDigitByteArray) |
1482 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 1483 |
&& __bothSmallInteger(len1, len2)) { |
2001 | 1484 |
unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
1485 |
unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
1486 |
unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
1487 |
int _index1, _index2, _dstIndex, _idx; |
|
1488 |
unsigned _prod, _carry, _v; |
|
68 | 1489 |
|
2001 | 1490 |
for (_index1 = 0; _index1 < __intVal(len1); _index1++) { |
1491 |
for (_index2 = 0; _index2 < __intVal(len2); _index2++) { |
|
1492 |
_dstIndex = _index1 + _index2; |
|
1493 |
_prod = myBytes[_index1] * otherBytes[_index2]; |
|
1494 |
_prod += resultBytes[_dstIndex]; |
|
1495 |
resultBytes[_dstIndex] = _prod & 0xFF; |
|
1496 |
_carry = _prod >> 8; |
|
1497 |
if (_carry) { |
|
1498 |
_idx = _dstIndex + 1; |
|
1499 |
while (_carry) { |
|
1500 |
_v = resultBytes[_idx] + _carry; |
|
1501 |
resultBytes[_idx] = _v & 0xFF; |
|
1502 |
_carry = _v >> 8; |
|
1503 |
_idx = _idx + 1; |
|
1504 |
} |
|
1505 |
} |
|
1506 |
} |
|
1507 |
} |
|
1508 |
ok = true; |
|
92 | 1509 |
} |
1510 |
%}. |
|
1511 |
ok ifFalse:[ |
|
2001 | 1512 |
1 to:len1 do:[:index1 | |
1513 |
1 to:len2 do:[:index2 | |
|
1514 |
dstIndex := index1 + index2 - 1. |
|
1515 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
1516 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
1517 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
1518 |
carry := prod bitShift:-8. |
|
1519 |
carry ~~ 0 ifTrue:[ |
|
1520 |
idx := dstIndex + 1. |
|
1521 |
[carry ~~ 0] whileTrue:[ |
|
1522 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
1523 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
1524 |
carry := v bitShift:-8. |
|
1525 |
idx := idx + 1 |
|
1526 |
] |
|
1527 |
] |
|
1528 |
] |
|
1529 |
]. |
|
68 | 1530 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1531 |
^ result compressed |
68 | 1532 |
! |
1533 |
||
1534 |
absPlus:aLargeInteger |
|
1535 |
"return a LargeInteger representing abs(self) + abs(theArgument)" |
|
1536 |
||
1537 |
|result done otherDigitByteArray resultDigitByteArray |
|
1538 |
len1 "{ Class: SmallInteger }" |
|
1539 |
len2 "{ Class: SmallInteger }" |
|
1540 |
index "{ Class: SmallInteger }" |
|
1541 |
carry "{ Class: SmallInteger }" |
|
1542 |
sum "{ Class: SmallInteger }" | |
|
1543 |
||
1544 |
len1 := digitByteArray size. |
|
1545 |
otherDigitByteArray := aLargeInteger digits. |
|
1546 |
len2 := otherDigitByteArray size. |
|
1547 |
||
92 | 1548 |
result := self class basicNew numberOfDigits:((len1 max: len2) + 1). |
68 | 1549 |
resultDigitByteArray := result digits. |
1550 |
||
1551 |
index := 1. |
|
1552 |
carry := 0. |
|
1553 |
||
1554 |
done := false. |
|
1555 |
[done] whileFalse:[ |
|
2001 | 1556 |
sum := carry. |
1557 |
(index <= len1) ifTrue:[ |
|
1558 |
sum := sum + (digitByteArray basicAt:index). |
|
1559 |
(index <= len2) ifTrue:[ |
|
1560 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1561 |
] |
|
1562 |
] ifFalse:[ |
|
1563 |
(index <= len2) ifTrue:[ |
|
1564 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1565 |
] ifFalse:[ |
|
1566 |
"end reached" |
|
1567 |
done := true |
|
1568 |
] |
|
1569 |
]. |
|
1570 |
(sum >= 16r100) ifTrue:[ |
|
1571 |
carry := 1. |
|
1572 |
sum := sum - 16r100 |
|
1573 |
] ifFalse:[ |
|
1574 |
carry := 0 |
|
1575 |
]. |
|
1576 |
resultDigitByteArray basicAt:index put:sum. |
|
1577 |
index := index + 1 |
|
68 | 1578 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1579 |
^ result compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1580 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1581 |
"Modified: 5.11.1996 / 14:09:34 / cg" |
68 | 1582 |
! |
1583 |
||
1890 | 1584 |
absSubtract:aLargeInteger |
1585 |
"destructively subtract aLargeInteger from myself. |
|
1586 |
A helper for division; only allowed for positive receiver |
|
1587 |
and argument. The receiver must be >= the argument. |
|
1588 |
The receiver must be a temporary scratch-number" |
|
1589 |
||
1590 |
|result done |
|
1591 |
otherDigitByteArray resultDigitByteArray |
|
1592 |
len1 "{ Class: SmallInteger }" |
|
1593 |
len2 "{ Class: SmallInteger }" |
|
1594 |
index "{ Class: SmallInteger }" |
|
1595 |
borrow "{ Class: SmallInteger }" |
|
1596 |
diff "{ Class: SmallInteger }" |
|
1597 |
carry "{ Class: SmallInteger }" |
|
1598 |
lResult nonZero| |
|
1599 |
||
1600 |
len1 := digitByteArray size. |
|
1601 |
otherDigitByteArray := aLargeInteger digits. |
|
1602 |
len2 := otherDigitByteArray size. |
|
1603 |
len2 > len1 ifTrue:[ |
|
2001 | 1604 |
[(otherDigitByteArray at:len2) == 0] whileTrue:[ |
1605 |
len2 := len2 - 1 |
|
1606 |
]. |
|
1607 |
len2 > len1 ifTrue:[ |
|
1608 |
self halt "/ may not be called that way |
|
1609 |
]. |
|
1890 | 1610 |
]. |
1611 |
||
1612 |
nonZero := false. |
|
1613 |
index := 1. |
|
1614 |
borrow := 0. |
|
1615 |
||
1616 |
done := false. |
|
1617 |
[index <= len1] whileTrue:[ |
|
2001 | 1618 |
diff := borrow. |
1619 |
diff := diff + (digitByteArray basicAt:index). |
|
1620 |
index <= len2 ifTrue:[ |
|
1621 |
diff := diff - (otherDigitByteArray basicAt:index). |
|
1622 |
]. |
|
1890 | 1623 |
|
2001 | 1624 |
"/ workaround for |
1625 |
"/ gcc code generator bug |
|
1890 | 1626 |
|
2001 | 1627 |
(diff >= 0) ifTrue:[ |
1628 |
borrow := 0 |
|
1629 |
] ifFalse:[ |
|
1630 |
borrow := -1. |
|
1631 |
diff := diff + 16r100 |
|
1632 |
]. |
|
1633 |
diff ~~ 0 ifTrue:[ |
|
1634 |
nonZero := true |
|
1635 |
]. |
|
1636 |
digitByteArray basicAt:index put:diff. |
|
1637 |
index := index + 1 |
|
1890 | 1638 |
]. |
1639 |
^ nonZero |
|
1640 |
||
1641 |
"Created: 5.11.1996 / 16:23:47 / cg" |
|
1642 |
"Modified: 5.11.1996 / 18:56:50 / cg" |
|
1643 |
! |
|
1644 |
||
1645 |
div2 |
|
1646 |
"destructively divide the receiver by 2. |
|
1647 |
private - used for division only. |
|
1648 |
This is worth a primitive." |
|
1649 |
||
1650 |
%{ /* NOCONTEXT */ |
|
1651 |
OBJ __digits = __INST(digitByteArray); |
|
1652 |
||
1653 |
if (__isByteArray(__digits)) { |
|
2001 | 1654 |
int __nBytes = __byteArraySize(__digits); |
1655 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
1656 |
unsigned __this, __next; |
|
1657 |
int __idx; |
|
1890 | 1658 |
|
2001 | 1659 |
if (__nBytes == 1) { |
1660 |
__bp[0] >>= 1; |
|
1661 |
RETURN (self); |
|
1662 |
} |
|
1890 | 1663 |
|
2001 | 1664 |
__idx = 1; |
1890 | 1665 |
|
2001 | 1666 |
if ((__idx+4) < __nBytes) { |
1667 |
__this = ((unsigned long *)__bp)[0]; |
|
68 | 1668 |
|
2001 | 1669 |
while ((__idx+4) < __nBytes) { |
1670 |
__next = ((unsigned long *)__bp)[1]; |
|
1671 |
__this >>= 1; |
|
1672 |
__this |= __next << 31; |
|
1673 |
((unsigned long *)__bp)[0] = __this; |
|
1674 |
__this = __next; |
|
1675 |
__bp += 4; |
|
1676 |
__idx += 4; |
|
1677 |
} |
|
1678 |
} |
|
1890 | 1679 |
|
2001 | 1680 |
__this = __bp[0]; |
1681 |
while (__idx < __nBytes) { |
|
1682 |
__next = __bp[1]; |
|
1683 |
__this >>= 1; |
|
1684 |
__this |= __next << 7; |
|
1685 |
__bp[0] = __this; |
|
1686 |
__this = __next; |
|
1687 |
__bp++; |
|
1688 |
__idx++; |
|
1689 |
} |
|
1690 |
__bp[0] = __this >> 1; |
|
1691 |
RETURN (self); |
|
1890 | 1692 |
} |
1693 |
%}. |
|
1694 |
self primitiveFailed |
|
1695 |
||
1696 |
" |
|
1697 |
100000 asLargeInteger div2 |
|
1698 |
" |
|
1699 |
||
1700 |
"Modified: 5.11.1996 / 16:12:56 / cg" |
|
1701 |
! |
|
1702 |
||
1703 |
mul2 |
|
1704 |
"destructively multiply the receiver by 2. |
|
1705 |
private - used for division only. |
|
1706 |
This is worth a primitive." |
|
1707 |
||
1708 |
|nBytes "{ Class: SmallInteger }" |
|
1709 |
b "{ Class: SmallInteger }" |
|
1710 |
t| |
|
1711 |
||
1712 |
nBytes := digitByteArray size. |
|
1713 |
||
1714 |
b := digitByteArray at:nBytes. |
|
1715 |
(b bitAnd:16r80) ~~ 0 ifTrue:[ |
|
2001 | 1716 |
"/ need another byte |
1717 |
nBytes := nBytes + 1. |
|
1718 |
t := ByteArray uninitializedNew:nBytes. |
|
1719 |
t replaceFrom:1 with:digitByteArray startingAt:1. |
|
1720 |
t at:nBytes put:0. |
|
1721 |
digitByteArray := t. |
|
1890 | 1722 |
]. |
1723 |
||
1724 |
%{ |
|
1725 |
OBJ __digits = __INST(digitByteArray); |
|
1726 |
||
1727 |
if (__isByteArray(__digits)) { |
|
2001 | 1728 |
int __nBytes = __intVal(nBytes); |
1729 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
1730 |
unsigned __carry = 0, __newCarry, __this; |
|
1731 |
int __idx; |
|
1890 | 1732 |
|
2001 | 1733 |
while (__nBytes >= 4) { |
1734 |
__this = ((unsigned long *)__bp)[0]; |
|
1735 |
__newCarry = __this >> 31; |
|
1736 |
((unsigned long *)__bp)[0] = (__this << 1) | __carry; |
|
1737 |
__carry = __newCarry; |
|
1738 |
__bp += 4; |
|
1739 |
__nBytes -= 4; |
|
1740 |
} |
|
1741 |
while (__nBytes) { |
|
1742 |
__this = __bp[0]; |
|
1743 |
__newCarry = __this >> 7; |
|
1744 |
__bp[0] = (__this << 1) | __carry; |
|
1745 |
__carry = __newCarry; |
|
1746 |
__bp ++; |
|
1747 |
__nBytes--; |
|
1748 |
} |
|
1749 |
RETURN (self); |
|
1890 | 1750 |
} |
1751 |
%}. |
|
1752 |
self primitiveFailed |
|
1753 |
||
1754 |
" |
|
1755 |
100000 asLargeInteger mul2 |
|
1756 |
" |
|
1757 |
||
1758 |
"Modified: 5.11.1996 / 16:37:32 / cg" |
|
68 | 1759 |
! |
1760 |
||
1761 |
mul256 |
|
1762 |
"destructively multiply the receiver by 256. |
|
1763 |
private - used for division only" |
|
1764 |
||
1765 |
|newDigits| |
|
1766 |
||
359 | 1767 |
newDigits := ByteArray uninitializedNew:(digitByteArray size + 1). |
68 | 1768 |
newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
1769 |
newDigits at:1 put:0. |
|
1770 |
digitByteArray := newDigits |
|
701 | 1771 |
! |
68 | 1772 |
|
701 | 1773 |
numberOfDigits:nDigits |
2001 | 1774 |
"/ digitByteArray := ByteArray uninitializedNew:nDigits. |
701 | 1775 |
digitByteArray := ByteArray new:nDigits. |
1776 |
sign := 1. |
|
68 | 1777 |
! |
1778 |
||
701 | 1779 |
setDigits:digits |
1780 |
digitByteArray := digits. |
|
1781 |
sign := 1. |
|
68 | 1782 |
! |
1783 |
||
701 | 1784 |
sign:aNumber |
1785 |
sign := aNumber |
|
1 | 1786 |
! ! |
1787 |
||
1788 |
!LargeInteger methodsFor:'testing'! |
|
1789 |
||
701 | 1790 |
even |
1791 |
"return true if the receiver is even" |
|
1 | 1792 |
|
701 | 1793 |
^ (digitByteArray at:1) even |
1 | 1794 |
! |
1795 |
||
68 | 1796 |
negative |
1797 |
"return true, if the receiver is < 0" |
|
1798 |
||
1799 |
^ (sign < 0) |
|
1800 |
! |
|
1801 |
||
1 | 1802 |
odd |
1803 |
"return true if the receiver is odd" |
|
1804 |
||
1983 | 1805 |
^ (digitByteArray at:1) odd |
1806 |
||
1807 |
"Modified: 18.11.1996 / 22:19:19 / cg" |
|
1 | 1808 |
! |
1809 |
||
1810 |
positive |
|
1811 |
"return true, if the receiver is >= 0" |
|
1812 |
||
1813 |
^ (sign >= 0) |
|
1814 |
! |
|
1815 |
||
701 | 1816 |
sign |
1817 |
"return the sign of the receiver" |
|
1818 |
||
1819 |
^ sign |
|
1820 |
! |
|
1821 |
||
1 | 1822 |
strictlyPositive |
1823 |
"return true, if the receiver is > 0" |
|
1824 |
||
1825 |
^ (sign > 0) |
|
1826 |
! ! |
|
1827 |
||
1794 | 1828 |
!LargeInteger class methodsFor:'documentation'! |
1 | 1829 |
|
701 | 1830 |
version |
2001 | 1831 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.40 1996-12-19 16:26:17 cg Exp $' |
1 | 1832 |
! ! |