author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 02:21:27 +0100 | |
changeset 607 | a9a526c51233 |
parent 530 | 07d0bce293c9 |
child 701 | a309e3ef7faf |
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 |
|
345 | 14 |
instanceVariableNames:'sign digitByteArray' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
77 | 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 |
||
36 |
version |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
384
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.20 1995-11-11 15:23:45 cg Exp $' |
88 | 38 |
! |
39 |
||
77 | 40 |
documentation |
41 |
" |
|
42 |
This class provides arbitrary precision integers. These are represented as: |
|
43 |
sign (-1/0/+1) and, if sign ~~ 0 |
|
44 |
a ByteArray of digits with 8 bits per element; |
|
88 | 45 |
least significant 8 bits at index 1 ... |
1 | 46 |
|
88 | 47 |
The implementation is definitely not tuned for high performance |
48 |
(some key-methods should be rewritten as primitives), |
|
77 | 49 |
but ok for now, since LargeIntegers are not used very often. |
50 |
It will be reimplemented when everything else runs fine and a need arises |
|
51 |
(or someone rewrites it and sends me the code :-). |
|
52 |
||
53 |
For real speed, its implementation should be mapped onto a tuned arbitrary |
|
88 | 54 |
precision math package (maybe even special-cased for 32 and 64 bit |
55 |
large-ints, since those are the most common). |
|
56 |
||
57 |
LargeIntegers are usually not created explicitely, but result from |
|
58 |
SmallInteger arithmetic overflowing the SmallInteger range. |
|
357 | 59 |
Also, results of LargeInteger operations are converted to back to |
60 |
SmallIntegers, when possible (see normalize). |
|
92 | 61 |
|
62 |
In contrast to ST-80, there is only one class for LargeIntegers, keeping |
|
63 |
the sign as an instance variable (ST-80 has LargePositiveInteger and |
|
64 |
LargeNegativeInteger). This may change. |
|
77 | 65 |
" |
66 |
! ! |
|
1 | 67 |
|
357 | 68 |
!LargeInteger class methodsFor:'queries'! |
69 |
||
70 |
isBuiltInClass |
|
71 |
"this class is known by the run-time-system" |
|
72 |
||
73 |
^ true |
|
74 |
! ! |
|
75 |
||
1 | 76 |
!LargeInteger class methodsFor:'instance creation'! |
77 |
||
68 | 78 |
value:aSmallInteger |
79 |
"create and return a new LargeInteger with value taken from |
|
92 | 80 |
the argument, aSmallInteger. |
81 |
Notice: this should be only used internally, since such small |
|
82 |
largeIntegers do not normally occur in the system. |
|
83 |
May change without notice." |
|
68 | 84 |
|
85 |
^ self basicNew value:aSmallInteger |
|
86 |
||
87 |
"LargeInteger value:3689" |
|
88 |
! |
|
89 |
||
1 | 90 |
new:numberOfDigits |
91 |
"catch creation message" |
|
92 |
||
93 |
self error:'LargeIntegers cannot be created with new' |
|
94 |
! |
|
95 |
||
96 |
new |
|
97 |
"catch creation message" |
|
98 |
||
99 |
self error:'LargeIntegers cannot be created with new' |
|
100 |
! |
|
101 |
||
102 |
valueLow:lowBits hi:hiBits |
|
103 |
"create and return a new LargeInteger with value taken from |
|
92 | 104 |
the two 16-bit args, where the sign of the high bits determines |
105 |
the sign of the result. |
|
106 |
The largeinteger is normalized (but not to a smallInteger). |
|
107 |
This method is called from the runtime system (+, -), |
|
108 |
when an integer result has to be converted to a Large. |
|
109 |
May change without notice." |
|
110 |
||
111 |
|newLarge| |
|
1 | 112 |
|
113 |
hiBits < 0 ifTrue:[ |
|
345 | 114 |
newLarge := self unsignedValueLow:lowBits hi:(hiBits negated). |
115 |
newLarge sign:-1. |
|
116 |
^ newLarge |
|
1 | 117 |
]. |
92 | 118 |
^ self unsignedValueLow:lowBits hi:hiBits |
2 | 119 |
! |
120 |
||
121 |
unsignedValueLow:lowBits hi:hiBits |
|
122 |
"create and return a new LargeInteger with value taken from |
|
92 | 123 |
the two 16-bit unsigned args. |
124 |
The largeinteger is normalized (but not to a smallInteger). |
|
125 |
This method is called from the runtime system (+, -), |
|
126 |
when an integer result has to be converted to a Large. |
|
127 |
May change without notice." |
|
128 |
||
129 |
|bytes b1 b2 b3 b4| |
|
130 |
||
131 |
b4 := (hiBits bitShift:-8) bitAnd:16rFF. |
|
132 |
b3 := hiBits bitAnd:16rFF. |
|
133 |
b2 := (lowBits bitShift:-8) bitAnd:16rFF. |
|
134 |
b1 := lowBits bitAnd:16rFF. |
|
2 | 135 |
|
92 | 136 |
b4 ~~ 0 ifTrue:[ |
345 | 137 |
bytes := ByteArray with:b1 with:b2 with:b3 with:b4 |
92 | 138 |
] ifFalse:[ |
345 | 139 |
b3 ~~ 0 ifTrue:[ |
140 |
bytes := ByteArray with:b1 with:b2 with:b3 |
|
141 |
] ifFalse:[ |
|
142 |
b2 ~~ 0 ifTrue:[ |
|
143 |
bytes := ByteArray with:b1 with:b2 |
|
144 |
] ifFalse:[ |
|
145 |
bytes := ByteArray with:b1 |
|
146 |
] |
|
147 |
] |
|
92 | 148 |
]. |
149 |
^ (self basicNew) setDigits:bytes |
|
1 | 150 |
! |
151 |
||
68 | 152 |
sign:s value16:ll value16:ml value16:mh value16:hh |
153 |
"create and return a new LargeInteger, with value taken from the |
|
154 |
four 16-bit unsigned value-args. |
|
155 |
This is sent internally, when a 64bit result has been created in |
|
92 | 156 |
SmallInteger multiplication or bitShift:. |
157 |
May change without notice." |
|
158 |
||
159 |
|newLarge digitBytes |
|
160 |
hhI "{ Class: SmallInteger }" |
|
161 |
mhI "{ Class: SmallInteger }" |
|
162 |
mlI "{ Class: SmallInteger }" |
|
163 |
llI "{ Class: SmallInteger }" |
|
164 |
b1 "{ Class: SmallInteger }" |
|
165 |
b2 "{ Class: SmallInteger }" |
|
166 |
b3 "{ Class: SmallInteger }" |
|
167 |
b4 "{ Class: SmallInteger }" |
|
168 |
b5 "{ Class: SmallInteger }" |
|
169 |
b6 "{ Class: SmallInteger }" |
|
170 |
b7 "{ Class: SmallInteger }" |
|
171 |
b8 "{ Class: SmallInteger }" |
|
172 |
n "{ Class: SmallInteger }"| |
|
1 | 173 |
|
92 | 174 |
" |
175 |
this will change, once arguments can have a type-constraint |
|
176 |
" |
|
177 |
hhI := hh. |
|
178 |
mhI := mh. |
|
179 |
mlI := ml. |
|
180 |
llI := ll. |
|
181 |
||
182 |
" |
|
183 |
the following code is somewhat ugly, but includes an unrolled |
|
184 |
normalization, and is thus relatively fast .... |
|
185 |
" |
|
186 |
b8 := (hh bitShift:-8) bitAnd:16rFF. |
|
187 |
b7 := hh bitAnd:16rFF. |
|
188 |
b6 := (mh bitShift:-8) bitAnd:16rFF. |
|
189 |
b5 := mh bitAnd:16rFF. |
|
190 |
b4 := (ml bitShift:-8) bitAnd:16rFF. |
|
191 |
b3 := ml bitAnd:16rFF. |
|
192 |
b2 := (ll bitShift:-8) bitAnd:16rFF. |
|
193 |
b1 := ll bitAnd:16rFF. |
|
1 | 194 |
|
92 | 195 |
b8 ~~ 0 ifTrue:[ |
345 | 196 |
n := 8 |
92 | 197 |
] ifFalse:[ |
345 | 198 |
b7 ~~ 0 ifTrue:[ |
199 |
n := 7. |
|
200 |
] ifFalse:[ |
|
201 |
b6 ~~ 0 ifTrue:[ |
|
202 |
n := 6. |
|
203 |
] ifFalse:[ |
|
204 |
b5 ~~ 0 ifTrue:[ |
|
205 |
n := 5 |
|
206 |
] ifFalse:[ |
|
207 |
b4 ~~ 0 ifTrue:[ |
|
208 |
n := 4 |
|
209 |
] ifFalse:[ |
|
210 |
b3 ~~ 0 ifTrue:[ |
|
211 |
n := 3 |
|
212 |
] ifFalse:[ |
|
213 |
b2 ~~ 0 ifTrue:[ |
|
214 |
n := 2 |
|
215 |
] ifFalse:[ |
|
216 |
n := 1 |
|
217 |
] |
|
218 |
] |
|
219 |
] |
|
220 |
] |
|
221 |
] |
|
222 |
] |
|
92 | 223 |
]. |
224 |
||
225 |
||
226 |
digitBytes := ByteArray uninitializedNew:n. |
|
227 |
digitBytes at:1 put:b1. |
|
228 |
n > 1 ifTrue:[ |
|
345 | 229 |
digitBytes at:2 put:b2. |
230 |
n > 2 ifTrue:[ |
|
231 |
digitBytes at:3 put:b3. |
|
232 |
n > 3 ifTrue:[ |
|
233 |
digitBytes at:4 put:b4. |
|
234 |
n > 4 ifTrue:[ |
|
235 |
digitBytes at:5 put:b5. |
|
236 |
n > 5 ifTrue:[ |
|
237 |
digitBytes at:6 put:b6. |
|
238 |
n > 6 ifTrue:[ |
|
239 |
digitBytes at:7 put:b7. |
|
240 |
n > 7 ifTrue:[ |
|
241 |
digitBytes at:8 put:b8. |
|
242 |
] |
|
243 |
] |
|
244 |
] |
|
245 |
] |
|
246 |
] |
|
247 |
] |
|
92 | 248 |
]. |
249 |
||
250 |
newLarge := self basicNew setDigits:digitBytes. |
|
251 |
s < 0 ifTrue:[newLarge sign:s]. |
|
68 | 252 |
^ newLarge |
1 | 253 |
! ! |
254 |
||
68 | 255 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 256 |
|
92 | 257 |
+ aNumber |
258 |
"return the sum of the receiver and the argument, aNumber" |
|
259 |
||
260 |
|otherSign| |
|
261 |
||
262 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
263 |
||
264 |
" |
|
265 |
this is the common case, adding a SmallInteger. |
|
266 |
Use a special method for this case ... |
|
267 |
" |
|
268 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 269 |
^ self sumFromInteger:aNumber |
92 | 270 |
]. |
271 |
||
272 |
" |
|
273 |
if the argument is not a largeInteger, coerce |
|
274 |
" |
|
275 |
(aNumber class == self class) ifFalse:[ |
|
345 | 276 |
^ self retry:#+ coercing:aNumber |
92 | 277 |
]. |
278 |
||
279 |
otherSign := aNumber sign. |
|
280 |
(sign > 0) ifTrue:[ |
|
345 | 281 |
"I am positive" |
282 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber]. |
|
283 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber]. |
|
284 |
^ self |
|
92 | 285 |
]. |
286 |
"I am negative" |
|
287 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self]. |
|
288 |
(otherSign < 0) ifTrue:[^ (self absPlus:aNumber) sign:-1]. |
|
289 |
^ self |
|
357 | 290 |
|
291 |
" |
|
292 |
SmallInteger maxVal |
|
293 |
SmallInteger maxVal + 1 |
|
294 |
SmallInteger maxVal + 2 |
|
295 |
SmallInteger minVal |
|
296 |
SmallInteger minVal - 1 |
|
297 |
" |
|
92 | 298 |
! |
299 |
||
300 |
- aNumber |
|
301 |
"return the difference of the receiver and the argument, aNumber" |
|
302 |
||
303 |
|otherSign result| |
|
304 |
||
305 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
306 |
||
307 |
" |
|
308 |
this is the common case, subtracting a SmallInteger. |
|
309 |
Use a special method for this case ... |
|
310 |
" |
|
311 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 312 |
sign < 0 ifTrue:[ |
313 |
aNumber > 0 ifTrue:[ |
|
314 |
result := (self absFastPlus:aNumber) sign:-1 |
|
315 |
] ifFalse:[ |
|
316 |
result := (self absFastMinus:aNumber) sign:-1 |
|
317 |
]. |
|
318 |
] ifFalse:[ |
|
319 |
aNumber > 0 ifTrue:[ |
|
320 |
result := self absFastMinus:aNumber |
|
321 |
] ifFalse:[ |
|
322 |
result := self absFastPlus:aNumber |
|
323 |
] |
|
324 |
]. |
|
325 |
^ result normalize. |
|
92 | 326 |
]. |
327 |
||
328 |
" |
|
329 |
if the argument is not a largeInteger, coerce |
|
330 |
" |
|
331 |
(aNumber class == self class) ifFalse:[ |
|
345 | 332 |
^ self retry:#- coercing:aNumber |
92 | 333 |
]. |
334 |
||
335 |
otherSign := aNumber sign. |
|
336 |
(sign > 0) ifTrue:[ |
|
345 | 337 |
"I am positive" |
338 |
(otherSign > 0) ifTrue:[^ self absMinus:aNumber]. |
|
339 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber]. |
|
340 |
^ self |
|
92 | 341 |
]. |
342 |
"I am negative" |
|
343 |
(otherSign > 0) ifTrue:[^ (self absPlus:aNumber) negated]. |
|
344 |
(otherSign < 0) ifTrue:[^ (self absMinus:aNumber) negated]. |
|
345 |
^ self |
|
346 |
||
347 |
" |
|
348 |
12345678901234567890 - 0 |
|
357 | 349 |
12345678901234567890 - 1 |
92 | 350 |
12345678901234567890 - -1 |
351 |
-12345678901234567890 - 1 |
|
352 |
-12345678901234567890 - -1 |
|
353 |
||
354 |
12345678901234567890 - 12345678901234567880 |
|
355 |
12345678901234567890 - 12345000000000000000 |
|
356 |
12345678901234567890 - -87654321098765432110 |
|
357 |
-12345678901234567890 - 87654321098765432110 |
|
358 |
-12345678901234567890 - -12345678901234567880 |
|
359 |
-12345678901234567890 - -12345678901234567980 |
|
360 |
" |
|
361 |
! |
|
362 |
||
363 |
* aNumber |
|
364 |
"return the product of the receiver and the argument, aNumber" |
|
365 |
||
366 |
|otherSign| |
|
367 |
||
368 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
369 |
||
370 |
" |
|
371 |
this is the common case, multiplying with SmallInteger. |
|
372 |
Use a special method for this case ... |
|
373 |
" |
|
374 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 375 |
^ self productFromInteger:aNumber |
92 | 376 |
]. |
377 |
||
378 |
" |
|
379 |
if the argument is not a largeInteger, coerce |
|
380 |
" |
|
381 |
(aNumber class == self class) ifFalse:[ |
|
345 | 382 |
^ self retry:#* coercing:aNumber |
92 | 383 |
]. |
384 |
||
385 |
otherSign := aNumber sign. |
|
386 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
387 |
(otherSign == 0) ifTrue:[^ 0]. |
|
388 |
^ (self absMul:aNumber) sign:-1 |
|
389 |
! |
|
390 |
||
68 | 391 |
// aNumber |
392 |
"return the quotient of the receiver and the argument, aNumber" |
|
1 | 393 |
|
394 |
|otherSign| |
|
395 |
||
396 |
otherSign := aNumber sign. |
|
397 |
||
92 | 398 |
" |
399 |
this is the common case, dividing by a SmallInteger. |
|
400 |
Use a special method for this case ... |
|
401 |
" |
|
68 | 402 |
(aNumber class == SmallInteger) ifTrue:[ |
345 | 403 |
(aNumber abs between:1 and:16r003fffff) ifTrue:[ |
404 |
sign < 0 ifTrue:[ |
|
405 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber negated) at:1]. |
|
406 |
^ ((self absFastDiv:aNumber) at:1) sign:-1 |
|
407 |
]. |
|
408 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber) at:1]. |
|
409 |
^ ((self absFastDiv:aNumber negated) at:1) sign:-1 |
|
410 |
] |
|
1 | 411 |
]. |
68 | 412 |
|
92 | 413 |
" |
414 |
if the argument is not a largeInteger, coerce |
|
415 |
" |
|
416 |
(aNumber class == self class) ifFalse:[ |
|
345 | 417 |
^ self retry:#// coercing:aNumber |
1 | 418 |
]. |
419 |
||
92 | 420 |
sign < 0 ifTrue:[ |
345 | 421 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber negated) at:1]. |
422 |
^ ((self absDiv:aNumber) at:1) sign:-1 |
|
1 | 423 |
]. |
92 | 424 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:1]. |
425 |
^ ((self absDiv:aNumber negated) at:1) sign:-1 |
|
68 | 426 |
! |
427 |
||
428 |
\\ aNumber |
|
429 |
"return the remainder of division of the receiver by the argument, aNumber" |
|
430 |
||
431 |
|otherSign| |
|
432 |
||
433 |
otherSign := aNumber sign. |
|
92 | 434 |
|
435 |
" |
|
436 |
this is the common case, dividing by a SmallInteger. |
|
437 |
Use a special method for this case ... |
|
438 |
" |
|
68 | 439 |
(aNumber class == SmallInteger) ifTrue:[ |
345 | 440 |
(aNumber abs between:1 and:16r003fffff) ifTrue:[ |
441 |
sign < 0 ifTrue:[ |
|
442 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber negated) at:2]. |
|
443 |
^ ((self absFastDiv:aNumber) at:2) sign:-1 |
|
444 |
]. |
|
445 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber) at:2]. |
|
446 |
^ ((self absFastDiv:aNumber negated) at:2) sign:-1 |
|
447 |
] |
|
68 | 448 |
]. |
449 |
||
92 | 450 |
" |
451 |
if the argument is not a largeInteger, coerce |
|
452 |
" |
|
68 | 453 |
(aNumber class == self class) ifFalse:[ |
345 | 454 |
^ self retry:#\\ coercing:aNumber |
1 | 455 |
]. |
92 | 456 |
|
457 |
sign < 0 ifTrue:[ |
|
345 | 458 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber negated) at:2]. |
459 |
^ ((self absDiv:aNumber) at:2) sign:-1 |
|
1 | 460 |
]. |
92 | 461 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:2]. |
462 |
^ ((self absDiv:aNumber negated) at:2) sign:-1 |
|
1 | 463 |
! |
464 |
||
465 |
/ aNumber |
|
54 | 466 |
"return the quotient of the receivers and the argument, aNumber" |
467 |
||
468 |
aNumber isInteger ifTrue:[ |
|
345 | 469 |
^ (Fraction numerator:self |
470 |
denominator:aNumber) reduced |
|
54 | 471 |
]. |
472 |
||
1 | 473 |
"this is a q&d hack - we loose lots of precision here ..." |
474 |
^ (self asFloat / aNumber asFloat) |
|
475 |
! |
|
476 |
||
477 |
negated |
|
92 | 478 |
"return an integer with value negated from the receivers value." |
1 | 479 |
|
480 |
|newNumber| |
|
481 |
||
482 |
(sign == 0) ifTrue:[^ 0]. |
|
92 | 483 |
|
484 |
" |
|
485 |
special case for SmallInteger minVal |
|
486 |
" |
|
487 |
sign == 1 ifTrue:[ |
|
488 |
digitByteArray size == 4 ifTrue:[ |
|
345 | 489 |
(digitByteArray at:1) == 0 ifTrue:[ |
490 |
(digitByteArray at:2) == 0 ifTrue:[ |
|
491 |
(digitByteArray at:3) == 0 ifTrue:[ |
|
492 |
(digitByteArray at:4) == 16r40 ifTrue:[ |
|
493 |
^ SmallInteger minVal |
|
494 |
]. |
|
495 |
] |
|
496 |
] |
|
497 |
] |
|
92 | 498 |
] |
499 |
]. |
|
1 | 500 |
newNumber := self shallowCopy. |
501 |
newNumber sign:(sign negated). |
|
502 |
^ newNumber |
|
503 |
! ! |
|
504 |
||
68 | 505 |
!LargeInteger methodsFor:'double dispatching'! |
506 |
||
507 |
sumFromInteger:anInteger |
|
508 |
"sent, when anInteger does not know how to add the receiver. |
|
92 | 509 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
510 |
||
511 |
|result| |
|
68 | 512 |
|
92 | 513 |
anInteger > 0 ifTrue:[ |
345 | 514 |
sign < 0 ifTrue:[ |
515 |
result := (self absFastMinus:anInteger) sign:-1 |
|
516 |
] ifFalse:[ |
|
517 |
result := self absFastPlus:anInteger |
|
518 |
] |
|
92 | 519 |
] ifFalse:[ |
345 | 520 |
anInteger == 0 ifTrue:[ |
521 |
^ self |
|
522 |
]. |
|
523 |
sign < 0 ifTrue:[ |
|
524 |
result := (self absFastPlus:anInteger abs) sign:-1 |
|
525 |
] ifFalse:[ |
|
526 |
result := self absFastMinus:anInteger |
|
527 |
] |
|
68 | 528 |
]. |
529 |
^ result normalize |
|
92 | 530 |
|
531 |
" |
|
532 |
12345678901234567890 |
|
533 |
-12345678901234567890 |
|
534 |
12345678901234567890 sumFromInteger:0 |
|
535 |
-12345678901234567890 sumFromInteger:0 |
|
536 |
12345678901234567890 sumFromInteger:1 |
|
537 |
12345678901234567890 sumFromInteger:-1 |
|
538 |
-12345678901234567890 sumFromInteger:1 |
|
539 |
-12345678901234567890 sumFromInteger:-1 |
|
540 |
" |
|
541 |
! |
|
542 |
||
543 |
differenceFromInteger:anInteger |
|
544 |
"sent, when anInteger does not know how to subtract the receiver. |
|
545 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
546 |
||
547 |
|result| |
|
548 |
||
549 |
anInteger > 0 ifTrue:[ |
|
345 | 550 |
sign > 0 ifTrue:[ |
551 |
result := (self absFastMinus:anInteger) sign:-1 |
|
552 |
] ifFalse:[ |
|
553 |
result := self absFastPlus:anInteger |
|
554 |
] |
|
92 | 555 |
] ifFalse:[ |
345 | 556 |
anInteger == 0 ifTrue:[ |
557 |
^ self negated |
|
558 |
]. |
|
559 |
sign > 0 ifTrue:[ |
|
560 |
result := (self absFastPlus:anInteger negated) sign:-1 |
|
561 |
] ifFalse:[ |
|
562 |
result := (self absFastMinus:anInteger) sign:-1 |
|
563 |
] |
|
92 | 564 |
]. |
565 |
^ result normalize |
|
566 |
||
567 |
" |
|
568 |
12345678901234567890 |
|
569 |
-12345678901234567890 |
|
570 |
12345678901234567890 differenceFromInteger:0 |
|
571 |
12345678901234567890 differenceFromInteger:1 |
|
572 |
12345678901234567890 differenceFromInteger:-1 |
|
573 |
-12345678901234567890 differenceFromInteger:1 |
|
574 |
-12345678901234567890 differenceFromInteger:-1 |
|
575 |
" |
|
68 | 576 |
! |
577 |
||
578 |
productFromInteger:anInteger |
|
579 |
"sent, when anInteger does not know how to multiply the receiver. |
|
580 |
Return the product of the receiver and the argument, aSmallInteger" |
|
581 |
||
582 |
|num result |
|
583 |
resultDigitByteArray |
|
584 |
val "{ Class: SmallInteger }" |
|
585 |
len "{ Class: SmallInteger }" |
|
586 |
carry "{ Class: SmallInteger }" |
|
85 | 587 |
prod "{ Class: SmallInteger }" |
588 |
ok| |
|
68 | 589 |
|
590 |
"multiplying by a small integer is done here" |
|
591 |
||
592 |
"trivial cases" |
|
593 |
anInteger == 0 ifTrue:[^ 0]. |
|
594 |
anInteger == 1 ifTrue:[^ self]. |
|
595 |
||
596 |
num := anInteger abs. |
|
597 |
(num > 16r3FFFFF) ifTrue:[ |
|
345 | 598 |
"if num is too big (so that multiplying by a byte could create a Large)" |
68 | 599 |
|
345 | 600 |
^ anInteger retry:#* coercing:self |
68 | 601 |
]. |
602 |
||
603 |
len := digitByteArray size. |
|
604 |
||
605 |
result := self class basicNew numberOfDigits:(len + 4). |
|
1 | 606 |
|
85 | 607 |
"used to be the following. replaced, to avoid another multiplication" |
68 | 608 |
" |
609 |
result sign:(sign * anInteger sign). |
|
610 |
" |
|
611 |
anInteger < 0 ifTrue:[ |
|
345 | 612 |
sign > 0 ifTrue:[ |
613 |
result sign:-1 |
|
614 |
]. |
|
68 | 615 |
] ifFalse:[ |
345 | 616 |
sign < 0 ifTrue:[ |
617 |
result sign:sign |
|
618 |
] |
|
68 | 619 |
]. |
620 |
||
621 |
resultDigitByteArray := result digits. |
|
622 |
||
623 |
carry := 0. |
|
624 |
val := num. |
|
625 |
||
85 | 626 |
ok := false. |
627 |
%{ |
|
250 | 628 |
if (__bothSmallInteger(len, val) |
85 | 629 |
&& __isByteArray(_INST(digitByteArray)) |
630 |
&& __isByteArray(resultDigitByteArray)) { |
|
345 | 631 |
int _l = _intVal(len); |
632 |
int _v = _intVal(val); |
|
633 |
unsigned _carry = 0; |
|
634 |
unsigned _prod; |
|
635 |
unsigned char *digitP = _ByteArrayInstPtr(_INST(digitByteArray))->ba_element; |
|
636 |
unsigned char *resultP = _ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
85 | 637 |
|
345 | 638 |
while (_l-- > 0) { |
639 |
_prod = *digitP++ * _v + _carry; |
|
640 |
*resultP++ = _prod & 0xFF; |
|
641 |
_carry = _prod >> 8; |
|
642 |
} |
|
643 |
while (_carry) { |
|
644 |
*resultP++ = _carry & 0xFF; |
|
645 |
_carry >>= 8; |
|
646 |
} |
|
647 |
ok = true; |
|
85 | 648 |
} |
649 |
%}. |
|
92 | 650 |
" |
651 |
fall back - normally not reached |
|
652 |
(could make it a primitive-failure as well) |
|
653 |
" |
|
85 | 654 |
ok ifFalse:[ |
345 | 655 |
1 to:len do:[:i | |
656 |
prod := (digitByteArray basicAt:i) * val + carry. |
|
657 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
|
658 |
carry := prod bitShift:-8. |
|
659 |
]. |
|
660 |
[carry ~~ 0] whileTrue:[ |
|
661 |
len := len + 1. |
|
662 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
|
663 |
carry := carry bitShift:-8 |
|
664 |
]. |
|
68 | 665 |
]. |
666 |
^ result normalize |
|
667 |
! ! |
|
668 |
||
669 |
!LargeInteger methodsFor:'coercing & converting'! |
|
670 |
||
671 |
coerce:aNumber |
|
672 |
"return the argument as a LargeInteger" |
|
673 |
||
674 |
^ aNumber asLargeInteger |
|
675 |
! |
|
676 |
||
677 |
value:aSmallInteger |
|
92 | 678 |
"return a new LargeInteger with value taken from a SmallInteger. |
679 |
This method will fail, if the argument is not a smallInteger." |
|
68 | 680 |
|
681 |
|absValue |
|
92 | 682 |
b1 "{ Class: SmallInteger }" |
683 |
b2 "{ Class: SmallInteger }" |
|
175 | 684 |
b3 "{ Class: SmallInteger }"| |
68 | 685 |
|
92 | 686 |
" |
687 |
could have simply created a 4-byte largeinteger and normalize |
|
688 |
it; the code below does the normalize right away, avoiding the |
|
689 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
690 |
" |
|
68 | 691 |
(aSmallInteger == 0) ifTrue: [ |
345 | 692 |
digitByteArray := #[0]. |
693 |
sign := 0. |
|
694 |
^ self |
|
68 | 695 |
]. |
696 |
(aSmallInteger < 0) ifTrue: [ |
|
345 | 697 |
sign := -1. |
698 |
absValue := aSmallInteger negated |
|
68 | 699 |
] ifFalse: [ |
345 | 700 |
sign := 1. |
701 |
absValue := aSmallInteger |
|
68 | 702 |
]. |
92 | 703 |
b1 := absValue bitAnd:16rFF. |
704 |
absValue := absValue bitShift:-8. |
|
705 |
absValue == 0 ifTrue:[ |
|
345 | 706 |
digitByteArray := ByteArray with:b1 |
92 | 707 |
] ifFalse:[ |
345 | 708 |
b2 := absValue bitAnd:16rFF. |
709 |
absValue := absValue bitShift:-8. |
|
710 |
absValue == 0 ifTrue:[ |
|
711 |
digitByteArray := ByteArray with:b1 with:b2 |
|
712 |
] ifFalse:[ |
|
713 |
b3 := absValue bitAnd:16rFF. |
|
714 |
absValue := absValue bitShift:-8. |
|
715 |
absValue == 0 ifTrue:[ |
|
716 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
717 |
] ifFalse:[ |
|
718 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:absValue |
|
719 |
] |
|
720 |
] |
|
68 | 721 |
] |
722 |
! |
|
723 |
||
724 |
asSmallInteger |
|
92 | 725 |
"return a SmallInteger with same value as myself - |
726 |
the result is invalid if the receivers value cannot |
|
727 |
be represented as a SmallInteger. |
|
728 |
Q: should we raise an exception if this happens ?" |
|
1 | 729 |
|
68 | 730 |
|value| |
731 |
||
732 |
value := 0. |
|
733 |
(sign == 0) ifFalse:[ |
|
345 | 734 |
digitByteArray reverseDo:[:aDigit | |
735 |
value := (value times:256) + aDigit |
|
736 |
]. |
|
737 |
(sign < 0) ifTrue:[ |
|
738 |
value := value negated |
|
739 |
] |
|
68 | 740 |
]. |
741 |
^ value |
|
742 |
! |
|
743 |
||
744 |
asLargeInteger |
|
745 |
"return a LargeInteger with same value as myself - thats me" |
|
746 |
||
747 |
^ self |
|
748 |
! |
|
749 |
||
750 |
asFloat |
|
92 | 751 |
"return a Float with same value as myself. |
752 |
Since floats have a limited precision, you usually loose bits when |
|
753 |
doing this." |
|
68 | 754 |
|
755 |
|newFloat| |
|
756 |
||
757 |
newFloat := 0.0. |
|
758 |
(sign == 0) ifFalse:[ |
|
345 | 759 |
digitByteArray reverseDo:[:aDigit | |
760 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
761 |
]. |
|
762 |
(sign < 0) ifTrue:[ |
|
763 |
newFloat := newFloat negated |
|
764 |
] |
|
68 | 765 |
]. |
766 |
^ newFloat |
|
92 | 767 |
|
768 |
" |
|
769 |
1234567890 asFloat |
|
770 |
12345678901234567890 asFloat |
|
771 |
12345678901234567890 asFloat asInteger |
|
772 |
" |
|
773 |
! |
|
774 |
||
775 |
normalize |
|
776 |
"if the receiver can be represented as a SmallInteger, return |
|
777 |
a SmallInteger with my value; otherwise return self with leading |
|
778 |
zeros removed" |
|
779 |
||
780 |
|index "{ Class: SmallInteger }" |
|
781 |
val "{ Class: SmallInteger }" | |
|
782 |
||
357 | 783 |
%{ /* NOCONTEXT */ |
92 | 784 |
OBJ t; |
785 |
||
357 | 786 |
if (_INST(sign) == _MKSMALLINT(0)) { |
787 |
RETURN (_MKSMALLINT(0)); |
|
788 |
} |
|
789 |
||
92 | 790 |
t = _INST(digitByteArray); |
791 |
if (__isByteArray(t)) { |
|
345 | 792 |
unsigned char *_digitBytes = _ByteArrayInstPtr(t)->ba_element; |
793 |
int _idx = _byteArraySize(t); |
|
794 |
int _val; |
|
92 | 795 |
|
345 | 796 |
while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) { |
797 |
_idx--; |
|
798 |
} |
|
799 |
switch (_idx) { |
|
800 |
case 0: |
|
801 |
RETURN (_MKSMALLINT(0)); |
|
802 |
break; |
|
92 | 803 |
|
345 | 804 |
case 1: |
805 |
_val = _digitBytes[0]; |
|
806 |
if (_INST(sign) == _MKSMALLINT(-1)) |
|
807 |
_val = -_val; |
|
808 |
RETURN (_MKSMALLINT(_val)); |
|
92 | 809 |
|
345 | 810 |
case 2: |
811 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
812 |
if (_INST(sign) == _MKSMALLINT(-1)) |
|
813 |
_val = -_val; |
|
814 |
RETURN (_MKSMALLINT(_val)); |
|
92 | 815 |
|
345 | 816 |
case 3: |
817 |
_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
818 |
if (_INST(sign) == _MKSMALLINT(-1)) |
|
819 |
_val = -_val; |
|
820 |
RETURN (_MKSMALLINT(_val)); |
|
92 | 821 |
|
345 | 822 |
case 4: |
823 |
_val = _digitBytes[3]; |
|
824 |
if (_val <= 0x40) { |
|
825 |
_val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
826 |
if (_INST(sign) == _MKSMALLINT(-1)) |
|
827 |
_val = -_val; |
|
828 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
|
829 |
RETURN (_MKSMALLINT(_val)); |
|
830 |
} |
|
831 |
} |
|
832 |
break; |
|
92 | 833 |
|
345 | 834 |
default: |
835 |
break; |
|
836 |
} |
|
92 | 837 |
} |
838 |
%}. |
|
357 | 839 |
index := digitByteArray size. |
840 |
[(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ |
|
841 |
index := index - 1 |
|
92 | 842 |
]. |
843 |
||
844 |
(index ~~ digitByteArray size) ifTrue:[ |
|
345 | 845 |
digitByteArray := digitByteArray copyFrom:1 to:index |
92 | 846 |
]. |
847 |
^ self |
|
68 | 848 |
! ! |
849 |
||
850 |
!LargeInteger methodsFor:'byte access'! |
|
851 |
||
852 |
digitLength |
|
853 |
"return the number bytes used by this Integer" |
|
854 |
||
345 | 855 |
"/ ^ digitByteArray size |
856 |
||
357 | 857 |
" |
858 |
kludge: check if there is a 0-byte ... |
|
859 |
this allows to ask unnormalized LargeIntegers |
|
860 |
to be asked for their digitLength |
|
861 |
" |
|
862 |
|l "{ Class: SmallInteger }" | |
|
345 | 863 |
|
864 |
l := digitByteArray size. |
|
865 |
[(digitByteArray at:l) == 0] whileTrue:[ |
|
866 |
l := l - 1. |
|
867 |
]. |
|
868 |
^ l |
|
68 | 869 |
! |
870 |
||
871 |
digitAt:index |
|
872 |
"return 8 bits of value, starting at byte index" |
|
873 |
||
77 | 874 |
index > digitByteArray size ifTrue:[^ 0]. |
68 | 875 |
^ digitByteArray at:index |
876 |
! |
|
877 |
||
878 |
digitAt:index put:aByte |
|
879 |
"set the 8 bits, index is a byte index" |
|
880 |
||
881 |
digitByteArray at:index put:aByte |
|
92 | 882 |
! |
883 |
||
884 |
digits |
|
885 |
"return a byteArray fille with the receivers bits |
|
886 |
(8 bits of the absolute value per element)" |
|
887 |
||
888 |
^ digitByteArray |
|
68 | 889 |
! ! |
890 |
||
891 |
!LargeInteger methodsFor:'private'! |
|
892 |
||
92 | 893 |
sign:aNumber |
894 |
sign := aNumber |
|
895 |
! |
|
896 |
||
897 |
numberOfDigits:nDigits |
|
898 |
digitByteArray := ByteArray new:nDigits. |
|
899 |
sign := 1. |
|
900 |
! |
|
68 | 901 |
|
92 | 902 |
setDigits:digits |
903 |
digitByteArray := digits. |
|
904 |
sign := 1. |
|
905 |
! |
|
906 |
||
907 |
absFastPlus:aSmallInteger |
|
908 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
|
909 |
The result is not normalized." |
|
68 | 910 |
|
92 | 911 |
|result resultDigitByteArray |
912 |
len "{ Class: SmallInteger }" |
|
913 |
index "{ Class: SmallInteger }" |
|
914 |
carry "{ Class: SmallInteger }" | |
|
915 |
||
916 |
len := digitByteArray size. |
|
917 |
||
918 |
result := self class basicNew numberOfDigits:(len + 1). |
|
919 |
resultDigitByteArray := result digits. |
|
920 |
||
921 |
index := 1. |
|
922 |
carry := aSmallInteger abs. |
|
923 |
||
924 |
[carry ~~ 0] whileTrue:[ |
|
345 | 925 |
(index <= len) ifTrue:[ |
926 |
carry := (digitByteArray basicAt:index) + carry. |
|
927 |
]. |
|
928 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
|
929 |
carry := carry bitShift:-8. |
|
930 |
index := index + 1 |
|
68 | 931 |
]. |
92 | 932 |
[index <= len] whileTrue:[ |
345 | 933 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
934 |
index := index + 1 |
|
68 | 935 |
]. |
92 | 936 |
|
937 |
^ result |
|
938 |
! |
|
939 |
||
940 |
absFastMinus:aSmallInteger |
|
941 |
"return a LargeInteger representing abs(self) - abs(theArgument). |
|
942 |
The result is not normalized." |
|
943 |
||
944 |
|result resultDigitByteArray |
|
945 |
len "{ Class: SmallInteger }" |
|
946 |
index "{ Class: SmallInteger }" |
|
947 |
borrow "{ Class: SmallInteger }" |
|
948 |
diff "{ Class: SmallInteger }" | |
|
949 |
||
950 |
len := digitByteArray size. |
|
951 |
||
952 |
result := self class basicNew numberOfDigits:(len + 1). |
|
953 |
resultDigitByteArray := result digits. |
|
954 |
||
955 |
index := 1. |
|
956 |
borrow := aSmallInteger abs. |
|
957 |
||
958 |
[borrow ~~ 0] whileTrue:[ |
|
345 | 959 |
(index <= len) ifTrue:[ |
960 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
|
961 |
borrow := borrow bitShift:-8. |
|
962 |
diff < 0 ifTrue:[ |
|
963 |
diff := diff + 256. |
|
964 |
borrow := borrow + 1. |
|
965 |
] |
|
966 |
] ifFalse:[ |
|
967 |
diff := borrow bitAnd:255. |
|
968 |
borrow := borrow bitShift:-8. |
|
969 |
]. |
|
970 |
resultDigitByteArray basicAt:index put:diff. |
|
971 |
index := index + 1 |
|
68 | 972 |
]. |
92 | 973 |
[index <= len] whileTrue:[ |
345 | 974 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
975 |
index := index + 1 |
|
68 | 976 |
]. |
92 | 977 |
|
978 |
^ result |
|
979 |
||
980 |
" |
|
981 |
12345678900000000000 absFastMinus:1 |
|
982 |
12345678900000000000 absFastMinus:1000000 |
|
983 |
12345678900000000000 absFastMinus:255 |
|
984 |
(SmallInteger maxVal + 1) absFastMinus:1 |
|
985 |
(SmallInteger minVal - 1) absFastMinus:1 |
|
986 |
" |
|
68 | 987 |
! |
988 |
||
989 |
absFastDiv:aSmallInteger |
|
990 |
"return an array with two LargeIntegers representing |
|
991 |
abs(self) // aSmallInteger and abs(self) \\ aSmallInteger" |
|
992 |
||
77 | 993 |
|tmp1 "{ Class: SmallInteger }" |
994 |
prevRest "{ Class: SmallInteger }" |
|
995 |
count "{ Class: SmallInteger }" |
|
85 | 996 |
newDigitByteArray result |
997 |
ok| |
|
68 | 998 |
|
999 |
aSmallInteger == 0 ifTrue:[ |
|
345 | 1000 |
^ DivisionByZeroSignal raise |
68 | 1001 |
]. |
1002 |
||
1003 |
"This cannot happen (if always normalized) |
|
1004 |
self < aSmallInteger ifTrue:[ |
|
345 | 1005 |
^ Array with:0 with:self |
68 | 1006 |
]. |
1007 |
" |
|
1008 |
count := digitByteArray size. |
|
1009 |
result := self class basicNew numberOfDigits:count. |
|
1010 |
newDigitByteArray := result digits. |
|
85 | 1011 |
ok := false. |
1012 |
%{ |
|
1013 |
if (__isByteArray(_INST(digitByteArray)) |
|
1014 |
&& __isByteArray(newDigitByteArray) |
|
250 | 1015 |
&& __bothSmallInteger(count, aSmallInteger)) { |
345 | 1016 |
unsigned int rest = 0; |
1017 |
int index = _intVal(count); |
|
1018 |
int divisor = _intVal(aSmallInteger); |
|
1019 |
unsigned char *digitBytes = _ByteArrayInstPtr(_INST(digitByteArray))->ba_element; |
|
1020 |
unsigned char *resultBytes = _ByteArrayInstPtr(newDigitByteArray)->ba_element; |
|
68 | 1021 |
|
345 | 1022 |
while (index > 0) { |
1023 |
unsigned int t; |
|
85 | 1024 |
|
345 | 1025 |
index--; |
1026 |
t = digitBytes[index]; |
|
1027 |
t = t | (rest << 8); |
|
1028 |
resultBytes[index] = t / divisor; |
|
1029 |
rest = t % divisor; |
|
1030 |
} |
|
1031 |
prevRest = _MKSMALLINT(rest); |
|
1032 |
ok = true; |
|
85 | 1033 |
} |
1034 |
%}. |
|
1035 |
" |
|
1036 |
slow code - not normally reached |
|
1037 |
(could also do a primitiveFailure here) |
|
1038 |
" |
|
92 | 1039 |
ok ifFalse:[ |
345 | 1040 |
prevRest := 0. |
1041 |
count to:1 by:-1 do:[:i | |
|
1042 |
tmp1 := digitByteArray at:i. |
|
1043 |
tmp1 := (tmp1 + (prevRest * 256)). |
|
1044 |
newDigitByteArray at:i put:tmp1 // aSmallInteger. |
|
1045 |
prevRest := (tmp1 \\ aSmallInteger). |
|
1046 |
] |
|
68 | 1047 |
]. |
1048 |
||
1049 |
^ Array with:(result normalize) with:prevRest |
|
1050 |
! |
|
1051 |
||
1052 |
absMul:aLargeInteger |
|
1053 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
1054 |
||
92 | 1055 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 1056 |
idx "{ Class: SmallInteger }" |
1057 |
carry "{ Class: SmallInteger }" |
|
1058 |
len1 "{ Class: SmallInteger }" |
|
1059 |
len2 "{ Class: SmallInteger }" |
|
1060 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 1061 |
prod "{ Class: SmallInteger }" |
1062 |
v "{ Class: SmallInteger }"| |
|
68 | 1063 |
|
1064 |
len1 := digitByteArray size. |
|
1065 |
otherDigitByteArray := aLargeInteger digits. |
|
1066 |
len2 := otherDigitByteArray size. |
|
1067 |
||
1068 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1). |
|
1069 |
resultDigitByteArray := result digits. |
|
92 | 1070 |
ok := false. |
1071 |
%{ |
|
1072 |
if (__isByteArray(_INST(digitByteArray)) |
|
1073 |
&& __isByteArray(otherDigitByteArray) |
|
1074 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 1075 |
&& __bothSmallInteger(len1, len2)) { |
345 | 1076 |
unsigned char *myBytes = _ByteArrayInstPtr(_INST(digitByteArray))->ba_element; |
1077 |
unsigned char *otherBytes = _ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
1078 |
unsigned char *resultBytes = _ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
1079 |
int _index1, _index2, _dstIndex, _idx; |
|
1080 |
unsigned _prod, _carry, _v; |
|
68 | 1081 |
|
345 | 1082 |
for (_index1 = 0; _index1 < _intVal(len1); _index1++) { |
1083 |
for (_index2 = 0; _index2 < _intVal(len2); _index2++) { |
|
1084 |
_dstIndex = _index1 + _index2; |
|
1085 |
_prod = myBytes[_index1] * otherBytes[_index2]; |
|
1086 |
_prod += resultBytes[_dstIndex]; |
|
1087 |
resultBytes[_dstIndex] = _prod & 0xFF; |
|
1088 |
_carry = _prod >> 8; |
|
1089 |
if (_carry) { |
|
1090 |
_idx = _dstIndex + 1; |
|
1091 |
while (_carry) { |
|
1092 |
_v = resultBytes[_idx] + _carry; |
|
1093 |
resultBytes[_idx] = _v & 0xFF; |
|
1094 |
_carry = _v >> 8; |
|
1095 |
_idx = _idx + 1; |
|
1096 |
} |
|
1097 |
} |
|
1098 |
} |
|
1099 |
} |
|
1100 |
ok = true; |
|
92 | 1101 |
} |
1102 |
%}. |
|
1103 |
ok ifFalse:[ |
|
345 | 1104 |
1 to:len1 do:[:index1 | |
1105 |
1 to:len2 do:[:index2 | |
|
1106 |
dstIndex := index1 + index2 - 1. |
|
1107 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
1108 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
1109 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
1110 |
carry := prod bitShift:-8. |
|
1111 |
carry ~~ 0 ifTrue:[ |
|
1112 |
idx := dstIndex + 1. |
|
1113 |
[carry ~~ 0] whileTrue:[ |
|
1114 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
1115 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
1116 |
carry := v bitShift:-8. |
|
1117 |
idx := idx + 1 |
|
1118 |
] |
|
1119 |
] |
|
1120 |
] |
|
1121 |
]. |
|
68 | 1122 |
]. |
1123 |
^ result normalize |
|
1124 |
! |
|
1125 |
||
1126 |
absLess:aLargeInteger |
|
1127 |
"return true, if abs(self) < abs(theArgument)" |
|
1128 |
||
1129 |
|len1 "{ Class: SmallInteger }" |
|
1130 |
len2 "{ Class: SmallInteger }" |
|
1131 |
d1 "{ Class: SmallInteger }" |
|
1132 |
d2 "{ Class: SmallInteger }" |
|
1133 |
otherDigitByteArray | |
|
1134 |
||
1135 |
len1 := digitByteArray size. |
|
1136 |
otherDigitByteArray := aLargeInteger digits. |
|
1137 |
len2 := otherDigitByteArray size. |
|
1138 |
||
1139 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
|
345 | 1140 |
len1 := len1 - 1 |
68 | 1141 |
]. |
1142 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
345 | 1143 |
len2 := len2 - 1 |
68 | 1144 |
]. |
1145 |
(len1 < len2) ifTrue:[^ true]. |
|
1146 |
(len1 > len2) ifTrue:[^ false]. |
|
1147 |
||
1148 |
[len1 > 0] whileTrue:[ |
|
345 | 1149 |
d1 := digitByteArray basicAt:len1. |
1150 |
d2 := otherDigitByteArray basicAt:len1. |
|
1151 |
(d1 < d2) ifTrue:[^ true]. |
|
1152 |
(d1 > d2) ifTrue:[^ false]. |
|
1153 |
len1 := len1 - 1 |
|
68 | 1154 |
]. |
1155 |
^ false |
|
1156 |
! |
|
1157 |
||
92 | 1158 |
absEq:aLargeInteger |
1159 |
"return true, if abs(self) = abs(theArgument)" |
|
1160 |
||
1161 |
|len1 "{ Class: SmallInteger }" |
|
1162 |
len2 "{ Class: SmallInteger }" |
|
1163 |
d1 "{ Class: SmallInteger }" |
|
1164 |
d2 "{ Class: SmallInteger }" |
|
1165 |
otherDigitByteArray | |
|
1166 |
||
1167 |
len1 := digitByteArray size. |
|
1168 |
otherDigitByteArray := aLargeInteger digits. |
|
1169 |
len2 := otherDigitByteArray size. |
|
1170 |
||
1171 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
|
345 | 1172 |
len1 := len1 - 1 |
92 | 1173 |
]. |
1174 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
345 | 1175 |
len2 := len2 - 1 |
92 | 1176 |
]. |
1177 |
(len1 ~~ len2) ifTrue:[^ false]. |
|
1178 |
[len1 > 0] whileTrue:[ |
|
345 | 1179 |
d1 := digitByteArray basicAt:len1. |
1180 |
d2 := otherDigitByteArray basicAt:len1. |
|
1181 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1182 |
len1 := len1 - 1 |
|
92 | 1183 |
]. |
1184 |
^ true |
|
1185 |
! |
|
1186 |
||
68 | 1187 |
absPlus:aLargeInteger |
1188 |
"return a LargeInteger representing abs(self) + abs(theArgument)" |
|
1189 |
||
1190 |
|result done otherDigitByteArray resultDigitByteArray |
|
1191 |
len1 "{ Class: SmallInteger }" |
|
1192 |
len2 "{ Class: SmallInteger }" |
|
1193 |
index "{ Class: SmallInteger }" |
|
1194 |
carry "{ Class: SmallInteger }" |
|
1195 |
sum "{ Class: SmallInteger }" | |
|
1196 |
||
1197 |
len1 := digitByteArray size. |
|
1198 |
otherDigitByteArray := aLargeInteger digits. |
|
1199 |
len2 := otherDigitByteArray size. |
|
1200 |
||
92 | 1201 |
result := self class basicNew numberOfDigits:((len1 max: len2) + 1). |
68 | 1202 |
resultDigitByteArray := result digits. |
1203 |
||
1204 |
index := 1. |
|
1205 |
carry := 0. |
|
1206 |
||
1207 |
done := false. |
|
1208 |
[done] whileFalse:[ |
|
345 | 1209 |
sum := carry. |
1210 |
(index <= len1) ifTrue:[ |
|
1211 |
sum := sum + (digitByteArray basicAt:index). |
|
1212 |
(index <= len2) ifTrue:[ |
|
1213 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1214 |
] |
|
1215 |
] ifFalse:[ |
|
1216 |
(index <= len2) ifTrue:[ |
|
1217 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1218 |
] ifFalse:[ |
|
1219 |
"end reached" |
|
1220 |
done := true |
|
1221 |
] |
|
1222 |
]. |
|
1223 |
(sum >= 16r100) ifTrue:[ |
|
1224 |
carry := 1. |
|
1225 |
sum := sum - 16r100 |
|
1226 |
] ifFalse:[ |
|
1227 |
carry := 0 |
|
1228 |
]. |
|
1229 |
resultDigitByteArray basicAt:index put:sum. |
|
1230 |
index := index + 1 |
|
68 | 1231 |
]. |
1232 |
^ result normalize |
|
1233 |
! |
|
1234 |
||
1235 |
absMinus:aLargeInteger |
|
1236 |
"return a LargeInteger representing abs(self) - abs(theArgument)" |
|
1237 |
||
1238 |
|result done |
|
1239 |
otherDigitByteArray resultDigitByteArray |
|
1240 |
len1 "{ Class: SmallInteger }" |
|
1241 |
len2 "{ Class: SmallInteger }" |
|
1242 |
index "{ Class: SmallInteger }" |
|
1243 |
borrow "{ Class: SmallInteger }" |
|
1244 |
diff "{ Class: SmallInteger }" |
|
1245 |
sum "{ Class: SmallInteger }" |
|
1246 |
carry "{ Class: SmallInteger }" | |
|
1247 |
||
1248 |
len1 := digitByteArray size. |
|
1249 |
otherDigitByteArray := aLargeInteger digits. |
|
1250 |
len2 := otherDigitByteArray size. |
|
1251 |
||
92 | 1252 |
result := self class basicNew numberOfDigits:((len1 max: len2) + 1). |
68 | 1253 |
resultDigitByteArray := result digits. |
1254 |
||
1255 |
index := 1. |
|
1256 |
borrow := 0. |
|
1257 |
||
1258 |
done := false. |
|
1259 |
[done] whileFalse:[ |
|
345 | 1260 |
diff := borrow. |
1261 |
(index <= len1) ifTrue:[ |
|
1262 |
diff := diff + (digitByteArray basicAt:index). |
|
1263 |
(index <= len2) ifTrue:[ |
|
1264 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1265 |
] |
|
1266 |
] ifFalse:[ |
|
1267 |
(index <= len2) ifTrue:[ |
|
1268 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1269 |
] ifFalse:[ |
|
1270 |
"end reached" |
|
1271 |
done := true |
|
1272 |
] |
|
1273 |
]. |
|
92 | 1274 |
"/ workaround for |
1275 |
"/ gcc code generator bug |
|
1276 |
"/ |
|
1277 |
||
1278 |
(diff >= 0) ifTrue:[ |
|
1279 |
borrow := 0 |
|
1280 |
] ifFalse:[ |
|
1281 |
borrow := -1. |
|
1282 |
diff := diff + 16r100 |
|
1283 |
]. |
|
1284 |
"/ (diff < 0) ifTrue:[ |
|
1285 |
"/ borrow := -1. |
|
1286 |
"/ diff := diff + 16r100 |
|
1287 |
"/ ] ifFalse:[ |
|
1288 |
"/ borrow := 0 |
|
1289 |
"/ ]. |
|
345 | 1290 |
resultDigitByteArray basicAt:index put:diff. |
1291 |
index := index + 1 |
|
68 | 1292 |
]. |
1293 |
(borrow ~~ 0) ifTrue:[ |
|
345 | 1294 |
result sign: -1. |
1295 |
carry := 0. |
|
1296 |
1 to:(index - 1) do:[:i | |
|
1297 |
sum := ((resultDigitByteArray at:i) + carry - 16r100) negated. |
|
1298 |
resultDigitByteArray at:i put:sum. |
|
1299 |
carry := 1 |
|
1300 |
] |
|
68 | 1301 |
]. |
1302 |
^ result normalize |
|
1303 |
! |
|
1304 |
||
1305 |
absDiv:anInteger |
|
1306 |
"return an array with two LargeIntegers representing |
|
1307 |
abs(self) // abs(theArgument) and abs(self) \\ abs(theArgument). |
|
1308 |
This needs a rewrite." |
|
1309 |
||
1310 |
|tmp1 tmp2 |
|
1311 |
rem |
|
1312 |
count "{ Class: SmallInteger }" |
|
1313 |
digit "{ Class: SmallInteger }" | |
|
1314 |
||
1315 |
anInteger == 0 ifTrue:[ |
|
345 | 1316 |
^ DivisionByZeroSignal raise |
68 | 1317 |
]. |
1318 |
||
1319 |
self < anInteger ifTrue:[ |
|
345 | 1320 |
^ Array with:0 with:self |
68 | 1321 |
]. |
1322 |
||
1323 |
tmp1 := self simpleDeepCopy. |
|
1324 |
tmp2 := anInteger simpleDeepCopy. |
|
1325 |
count := 0. |
|
1326 |
[tmp2 < tmp1] whileTrue:[ |
|
345 | 1327 |
tmp2 mul256. |
1328 |
count := count + 1 |
|
68 | 1329 |
]. |
1330 |
||
1331 |
tmp2 div256. |
|
1332 |
||
1333 |
rem := 0 asLargeInteger. |
|
1334 |
[count == 0] whileFalse:[ |
|
345 | 1335 |
digit := 0. |
1336 |
[tmp1 >= tmp2] whileTrue:[ |
|
1337 |
digit := digit + 1. |
|
1338 |
tmp1 := tmp1 - tmp2 |
|
1339 |
]. |
|
1340 |
rem := rem * 256 + digit. |
|
1341 |
tmp2 div256. |
|
1342 |
count := count - 1 |
|
68 | 1343 |
]. |
1344 |
^ Array with:rem with:tmp1 |
|
1345 |
! |
|
1346 |
||
1347 |
div256 |
|
1348 |
"destructively divide the receiver by 256. |
|
1349 |
private - used for division only" |
|
1350 |
||
1351 |
digitByteArray replaceFrom:1 with:digitByteArray startingAt:2. |
|
77 | 1352 |
digitByteArray at:(digitByteArray size) put:0 |
68 | 1353 |
! |
1354 |
||
1355 |
mul256 |
|
1356 |
"destructively multiply the receiver by 256. |
|
1357 |
private - used for division only" |
|
1358 |
||
1359 |
|newDigits| |
|
1360 |
||
359 | 1361 |
newDigits := ByteArray uninitializedNew:(digitByteArray size + 1). |
68 | 1362 |
newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
1363 |
newDigits at:1 put:0. |
|
1364 |
digitByteArray := newDigits |
|
1365 |
! ! |
|
1366 |
||
1367 |
!LargeInteger methodsFor:'comparing'! |
|
1368 |
||
1369 |
= aNumber |
|
1370 |
"return true, if the argument, aNumber has the same value as |
|
1371 |
the receiver" |
|
1372 |
||
1373 |
(aNumber class == self class) ifFalse:[ |
|
345 | 1374 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
1375 |
^ self retry:#= coercing:aNumber |
|
68 | 1376 |
]. |
1377 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
1378 |
^ self absEq:aNumber |
|
1379 |
! |
|
1380 |
||
1381 |
< aNumber |
|
1382 |
"return true, if the argument, aNumber is greater than the receiver" |
|
1383 |
||
1384 |
|otherSign| |
|
1385 |
||
1386 |
(aNumber class == self class) ifFalse:[ |
|
345 | 1387 |
^ self retry:#< coercing:aNumber |
68 | 1388 |
]. |
1389 |
otherSign := aNumber sign. |
|
1390 |
||
1391 |
(sign > 0) ifTrue:[ |
|
345 | 1392 |
"I am positive" |
1393 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
1394 |
^ false "aNumber is <= 0" |
|
68 | 1395 |
]. |
1396 |
(sign == 0) ifTrue:[ |
|
345 | 1397 |
(otherSign > 0) ifTrue:[^ true]. |
1398 |
^ false |
|
68 | 1399 |
]. |
1400 |
"I am negative" |
|
1401 |
(otherSign > 0) ifTrue:[^ true]. |
|
1402 |
(otherSign == 0) ifTrue:[^ true]. |
|
1403 |
^ (self absLess:aNumber) not |
|
1404 |
! |
|
1405 |
||
1406 |
> aNumber |
|
1407 |
"return true, if the argument, aNumber is less than the receiver" |
|
1408 |
||
1409 |
|otherSign| |
|
1410 |
||
1411 |
(aNumber class == self class) ifFalse:[ |
|
345 | 1412 |
^ self retry:#> coercing:aNumber |
68 | 1413 |
]. |
1414 |
otherSign := aNumber sign. |
|
1415 |
||
1416 |
(sign > 0) ifTrue:[ |
|
345 | 1417 |
"I am positive" |
1418 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
1419 |
^ true "aNumber is <= 0" |
|
68 | 1420 |
]. |
1421 |
(sign == 0) ifTrue:[ |
|
345 | 1422 |
"I am zero" |
1423 |
(otherSign > 0) ifTrue:[^ false]. |
|
1424 |
^ true |
|
68 | 1425 |
]. |
1426 |
"I am negative" |
|
1427 |
(otherSign > 0) ifTrue:[^ false]. |
|
1428 |
(otherSign == 0) ifTrue:[^ false]. |
|
1429 |
^ (self absLess:aNumber) not |
|
1 | 1430 |
! ! |
1431 |
||
1432 |
!LargeInteger methodsFor:'testing'! |
|
1433 |
||
1434 |
sign |
|
1435 |
"return the sign of the receiver" |
|
1436 |
||
1437 |
^ sign |
|
1438 |
! |
|
1439 |
||
68 | 1440 |
negative |
1441 |
"return true, if the receiver is < 0" |
|
1442 |
||
1443 |
^ (sign < 0) |
|
1444 |
! |
|
1445 |
||
1 | 1446 |
odd |
1447 |
"return true if the receiver is odd" |
|
1448 |
||
68 | 1449 |
^ (digitByteArray at:1) even |
1 | 1450 |
! |
1451 |
||
1452 |
even |
|
1453 |
"return true if the receiver is even" |
|
1454 |
||
68 | 1455 |
^ (digitByteArray at:1) even |
1 | 1456 |
! |
1457 |
||
1458 |
positive |
|
1459 |
"return true, if the receiver is >= 0" |
|
1460 |
||
1461 |
^ (sign >= 0) |
|
1462 |
! |
|
1463 |
||
1464 |
strictlyPositive |
|
1465 |
"return true, if the receiver is > 0" |
|
1466 |
||
1467 |
^ (sign > 0) |
|
1468 |
! ! |
|
1469 |
||
1470 |
!LargeInteger methodsFor:'printing & storing'! |
|
1471 |
||
54 | 1472 |
storeOn:aStream |
1473 |
"append a representation of the receiver to aStream, which can |
|
1474 |
be used to reconstruct the receiver." |
|
1 | 1475 |
|
54 | 1476 |
self printOn:aStream. |
1477 |
aStream nextPutAll:' asLargeInteger' |
|
1 | 1478 |
! ! |