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