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