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