author | Claus Gittinger <cg@exept.de> |
Mon, 12 Jan 1998 14:23:21 +0100 | |
changeset 3165 | b6bde90005a8 |
parent 3161 | 923a6931dded |
child 3178 | 649320240430 |
permissions | -rw-r--r-- |
1 | 1 |
" |
68 | 2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
345 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Integer subclass:#LargeInteger |
|
1210 | 14 |
instanceVariableNames:'sign digitByteArray' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
1794 | 20 |
!LargeInteger class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
345 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
This class provides arbitrary precision integers. These are represented as: |
|
39 |
sign (-1/0/+1) and, if sign ~~ 0 |
|
40 |
a ByteArray of digits with 8 bits per element; |
|
88 | 41 |
least significant 8 bits at index 1 ... |
1 | 42 |
|
88 | 43 |
The implementation is definitely not tuned for high performance |
44 |
(some key-methods should be rewritten as primitives), |
|
77 | 45 |
but ok for now, since LargeIntegers are not used very often. |
46 |
It will be reimplemented when everything else runs fine and a need arises |
|
47 |
(or someone rewrites it and sends me the code :-). |
|
48 |
||
49 |
For real speed, its implementation should be mapped onto a tuned arbitrary |
|
88 | 50 |
precision math package (maybe even special-cased for 32 and 64 bit |
51 |
large-ints, since those are the most common). |
|
52 |
||
53 |
LargeIntegers are usually not created explicitely, but result from |
|
54 |
SmallInteger arithmetic overflowing the SmallInteger range. |
|
357 | 55 |
Also, results of LargeInteger operations are converted to back to |
56 |
SmallIntegers, when possible (see normalize). |
|
92 | 57 |
|
58 |
In contrast to ST-80, there is only one class for LargeIntegers, keeping |
|
59 |
the sign as an instance variable (ST-80 has LargePositiveInteger and |
|
60 |
LargeNegativeInteger). This may change. |
|
1295 | 61 |
|
62 |
[author:] |
|
2001 | 63 |
Claus Gittinger |
1556 | 64 |
|
65 |
[see also:] |
|
2001 | 66 |
Number |
67 |
Float Fraction FixedPoint |
|
68 |
SmallInteger |
|
77 | 69 |
" |
2923 | 70 |
! |
71 |
||
72 |
testing |
|
73 |
" |
|
74 |
test largeInt multiplication & division |
|
75 |
(at least, test if they have not both errors which anull each other ;-) |
|
76 |
||
77 |
|last v| |
|
78 |
||
2952 | 79 |
v := last := 1. |
2923 | 80 |
2 to:3000 do:[:i | |
81 |
i printCR. |
|
2952 | 82 |
v := v * i. |
2923 | 83 |
(v / i) ~= last ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
84 |
self halt |
2923 | 85 |
]. |
86 |
last := v. |
|
87 |
] |
|
88 |
" |
|
77 | 89 |
! ! |
1 | 90 |
|
1794 | 91 |
!LargeInteger class methodsFor:'instance creation'! |
1 | 92 |
|
93 |
new |
|
94 |
"catch creation message" |
|
95 |
||
96 |
self error:'LargeIntegers cannot be created with new' |
|
97 |
! |
|
98 |
||
701 | 99 |
new:numberOfDigits |
100 |
"catch creation message" |
|
2 | 101 |
|
701 | 102 |
self error:'LargeIntegers cannot be created with new' |
1 | 103 |
! |
104 |
||
701 | 105 |
value:aSmallInteger |
106 |
"create and return a new LargeInteger with value taken from |
|
107 |
the argument, aSmallInteger. |
|
108 |
Notice: this should be only used internally, since such small |
|
109 |
largeIntegers do not normally occur in the system. |
|
110 |
May change without notice." |
|
111 |
||
112 |
^ self basicNew value:aSmallInteger |
|
113 |
||
114 |
"LargeInteger value:3689" |
|
115 |
! ! |
|
116 |
||
1794 | 117 |
!LargeInteger class methodsFor:'queries'! |
701 | 118 |
|
119 |
isBuiltInClass |
|
1264 | 120 |
"return true if this class is known by the run-time-system. |
121 |
Here, true is returned." |
|
701 | 122 |
|
123 |
^ true |
|
1264 | 124 |
|
125 |
"Modified: 23.4.1996 / 15:59:21 / cg" |
|
1 | 126 |
! ! |
127 |
||
68 | 128 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 129 |
|
701 | 130 |
* aNumber |
131 |
"return the product of the receiver and the argument, aNumber" |
|
132 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
133 |
|otherSign numberClass| |
701 | 134 |
|
135 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
136 |
||
137 |
" |
|
138 |
this is the common case, multiplying with SmallInteger. |
|
139 |
Use a special method for this case ... |
|
140 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
141 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
701 | 142 |
^ self productFromInteger:aNumber |
143 |
]. |
|
144 |
||
145 |
" |
|
146 |
if the argument is not a largeInteger, coerce |
|
147 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
148 |
(numberClass == self class) ifFalse:[ |
701 | 149 |
^ self retry:#* coercing:aNumber |
150 |
]. |
|
151 |
||
152 |
otherSign := aNumber sign. |
|
153 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
154 |
(otherSign == 0) ifTrue:[^ 0]. |
|
155 |
^ (self absMul:aNumber) sign:-1 |
|
156 |
! |
|
157 |
||
92 | 158 |
+ aNumber |
159 |
"return the sum of the receiver and the argument, aNumber" |
|
160 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
161 |
|otherSign numberClass| |
92 | 162 |
|
163 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
164 |
||
165 |
" |
|
166 |
this is the common case, adding a SmallInteger. |
|
167 |
Use a special method for this case ... |
|
168 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
169 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
345 | 170 |
^ self sumFromInteger:aNumber |
92 | 171 |
]. |
172 |
||
173 |
" |
|
174 |
if the argument is not a largeInteger, coerce |
|
175 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
176 |
(numberClass == self class) ifFalse:[ |
345 | 177 |
^ self retry:#+ coercing:aNumber |
92 | 178 |
]. |
179 |
||
180 |
otherSign := aNumber sign. |
|
181 |
(sign > 0) ifTrue:[ |
|
345 | 182 |
"I am positive" |
183 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber]. |
|
184 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber]. |
|
185 |
^ self |
|
92 | 186 |
]. |
187 |
"I am negative" |
|
188 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self]. |
|
189 |
(otherSign < 0) ifTrue:[^ (self absPlus:aNumber) sign:-1]. |
|
190 |
^ self |
|
357 | 191 |
|
192 |
" |
|
193 |
SmallInteger maxVal |
|
194 |
SmallInteger maxVal + 1 |
|
195 |
SmallInteger maxVal + 2 |
|
196 |
SmallInteger minVal |
|
197 |
SmallInteger minVal - 1 |
|
198 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
199 |
|
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
200 |
"Modified: / 9.1.1998 / 13:26:28 / cg" |
92 | 201 |
! |
202 |
||
203 |
- aNumber |
|
204 |
"return the difference of the receiver and the argument, aNumber" |
|
205 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
206 |
|otherSign numberClass| |
92 | 207 |
|
208 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
209 |
||
210 |
" |
|
211 |
this is the common case, subtracting a SmallInteger. |
|
212 |
Use a special method for this case ... |
|
213 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
214 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
215 |
sign > 0 ifTrue:[ |
2001 | 216 |
aNumber > 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
217 |
^ self absFastMinus:aNumber |
2001 | 218 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
219 |
^ self absFastPlus:aNumber |
2001 | 220 |
]. |
221 |
aNumber > 0 ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
222 |
^ (self absFastPlus:aNumber) sign:-1 |
2001 | 223 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
224 |
^ (self absFastMinus:aNumber) sign:-1 |
92 | 225 |
]. |
226 |
||
227 |
" |
|
228 |
if the argument is not a largeInteger, coerce |
|
229 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
230 |
(numberClass == self class) ifFalse:[ |
2001 | 231 |
^ self retry:#- coercing:aNumber |
92 | 232 |
]. |
233 |
||
234 |
otherSign := aNumber sign. |
|
235 |
(sign > 0) ifTrue:[ |
|
2001 | 236 |
"I am positive" |
237 |
(otherSign > 0) ifTrue:[^ self absMinus:aNumber]. |
|
238 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber]. |
|
239 |
^ self |
|
92 | 240 |
]. |
241 |
"I am negative" |
|
242 |
(otherSign > 0) ifTrue:[^ (self absPlus:aNumber) negated]. |
|
243 |
(otherSign < 0) ifTrue:[^ (self absMinus:aNumber) negated]. |
|
244 |
^ self |
|
245 |
||
246 |
" |
|
247 |
12345678901234567890 - 0 |
|
357 | 248 |
12345678901234567890 - 1 |
92 | 249 |
12345678901234567890 - -1 |
250 |
-12345678901234567890 - 1 |
|
251 |
-12345678901234567890 - -1 |
|
252 |
||
253 |
12345678901234567890 - 12345678901234567880 |
|
254 |
12345678901234567890 - 12345000000000000000 |
|
255 |
12345678901234567890 - -87654321098765432110 |
|
256 |
-12345678901234567890 - 87654321098765432110 |
|
257 |
-12345678901234567890 - -12345678901234567880 |
|
258 |
-12345678901234567890 - -12345678901234567980 |
|
259 |
" |
|
1794 | 260 |
|
261 |
"Modified: 20.10.1996 / 18:42:16 / cg" |
|
92 | 262 |
! |
263 |
||
701 | 264 |
/ aNumber |
1065 | 265 |
"return the quotient of the receiver and the argument, aNumber" |
92 | 266 |
|
701 | 267 |
aNumber isInteger ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
268 |
^ Fraction numerator:self |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
269 |
denominator:aNumber |
92 | 270 |
]. |
271 |
||
701 | 272 |
"this is a q&d hack - we loose lots of precision here ..." |
273 |
^ (self asFloat / aNumber asFloat) |
|
2793 | 274 |
|
275 |
"Modified: 28.7.1997 / 19:07:55 / cg" |
|
92 | 276 |
! |
277 |
||
68 | 278 |
// aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
279 |
"return the quotient of the receiver and the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
280 |
The result is truncated toward negative infinity and negative, |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
281 |
if the operands signs differ." |
1 | 282 |
|
2521 | 283 |
|otherSign divMod quo abs "{ Class: SmallInteger }" n| |
1 | 284 |
|
285 |
otherSign := aNumber sign. |
|
286 |
||
92 | 287 |
" |
288 |
this is the common case, dividing by a SmallInteger. |
|
289 |
Use a special method for this case ... |
|
290 |
" |
|
68 | 291 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 292 |
abs := aNumber. |
293 |
abs := abs abs. |
|
294 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
295 |
divMod := self absFastDiv:abs. |
|
296 |
quo := divMod at:1. |
|
297 |
(sign == otherSign) ifTrue:[^ quo]. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
298 |
|
2001 | 299 |
"/ stupid adjust ... |
300 |
(divMod at:2) == 0 ifFalse:[ |
|
301 |
^ (quo sign:-1) - 1 |
|
302 |
]. |
|
303 |
^ quo sign:-1 |
|
304 |
]. |
|
305 |
n := aNumber asLargeInteger. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
306 |
] ifFalse:[ |
2001 | 307 |
n := aNumber |
1 | 308 |
]. |
68 | 309 |
|
92 | 310 |
" |
311 |
if the argument is not a largeInteger, coerce |
|
312 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
313 |
(n class == self class) ifFalse:[ |
2001 | 314 |
^ self retry:#// coercing:aNumber |
1 | 315 |
]. |
316 |
||
1890 | 317 |
divMod := self absDiv:n. |
318 |
||
319 |
(sign == otherSign) ifTrue:[^ divMod at:1]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
320 |
|
1890 | 321 |
"/ stupid adjust for truncation ... |
322 |
quo := divMod at:1. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
323 |
(divMod at:2) == 0 ifFalse:[ |
2001 | 324 |
^ (quo sign:-1) - 1 |
1 | 325 |
]. |
1890 | 326 |
^ quo sign:-1 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
327 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
328 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
329 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
330 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
331 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
332 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
333 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
334 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
335 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
336 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
337 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
338 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
339 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
340 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
341 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
342 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
343 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
344 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
345 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
346 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
347 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
348 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
349 |
|
1890 | 350 |
"Modified: 5.11.1996 / 16:39:36 / cg" |
68 | 351 |
! |
352 |
||
353 |
\\ aNumber |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
354 |
"return the remainder of division of the receiver by the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
355 |
The sign of the result is that of aNumber." |
68 | 356 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
357 |
|abs rem| |
68 | 358 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
359 |
abs := aNumber abs. |
92 | 360 |
|
361 |
" |
|
362 |
this is the common case, dividing by a SmallInteger. |
|
363 |
Use a special method for this case ... |
|
364 |
" |
|
68 | 365 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 366 |
(abs between:1 and:16r003fffff) ifTrue:[ |
367 |
rem := (self absFastDiv:abs) at:2. |
|
368 |
] ifFalse:[ |
|
369 |
rem := (self absDiv:abs asLargeInteger) at:2 |
|
370 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
371 |
] ifFalse:[ |
2001 | 372 |
" |
373 |
if the argument is not a largeInteger, coerce |
|
374 |
" |
|
375 |
(aNumber class == self class) ifFalse:[ |
|
376 |
^ self retry:#\\ coercing:aNumber |
|
377 |
]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
378 |
|
2001 | 379 |
rem := (self absDiv:abs) at:2. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
380 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
381 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
382 |
aNumber negative ifTrue:[ |
2001 | 383 |
^ rem sign:-1 |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
384 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
385 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
386 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
387 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
388 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
389 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
390 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
391 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
392 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
393 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
394 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
395 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
396 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
397 |
|
1890 | 398 |
9000000000 \\ 7 |
399 |
-9000000000 \\ 7 |
|
400 |
9000000000 \\ -7 |
|
401 |
-9000000000 \\ -7 |
|
402 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
403 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
404 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
405 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
406 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
407 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
408 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
409 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
410 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
411 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
412 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
413 |
|
1890 | 414 |
"Modified: 5.11.1996 / 17:10:10 / cg" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
415 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
416 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
417 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
418 |
"return an array filled with self // aNumber and |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
419 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
420 |
The result is only defined for positive receiver and |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
421 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
422 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
423 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
424 |
this is the common case, dividing by a SmallInteger. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
425 |
Use a special method for this case ... |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
426 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
427 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 428 |
^ self absFastDiv:aNumber abs. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
429 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
430 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
431 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
432 |
if the argument is not a largeInteger, coerce |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
433 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
434 |
(aNumber class == self class) ifTrue:[ |
2001 | 435 |
^ self absDiv:aNumber abs |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
436 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
437 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
438 |
^ super divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
439 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
440 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
441 |
9000000000 // 4000000000 => 2 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
442 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
443 |
9000000000 \\ 4000000000 => 1000000000 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
444 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
445 |
9000000000 divMod: 4000000000 => #(2 1000000000) |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
446 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
447 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
448 |
"Modified: 29.10.1996 / 20:44:23 / cg" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
449 |
"Created: 29.10.1996 / 21:22:05 / cg" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
450 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
451 |
|
1210 | 452 |
negated |
453 |
"return an integer with value negated from the receivers value." |
|
454 |
||
2952 | 455 |
|newNumber sz| |
1210 | 456 |
|
457 |
(sign == 0) ifTrue:[^ 0]. |
|
458 |
||
459 |
" |
|
460 |
special case for SmallInteger minVal |
|
461 |
" |
|
462 |
sign == 1 ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
463 |
sz := digitByteArray size. |
2952 | 464 |
%{ |
465 |
if (__intVal(sz) == sizeof(INT)) { |
|
466 |
unsigned char *bp; |
|
467 |
||
468 |
bp = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))); |
|
469 |
if ((bp[sizeof(INT)-1] == 0x40) |
|
470 |
&& (bp[sizeof(INT)-2] == 0) |
|
471 |
&& (bp[sizeof(INT)-3] == 0) |
|
472 |
#ifdef alpha64 |
|
473 |
&& (bp[sizeof(INT)-4] == 0) |
|
474 |
&& (bp[sizeof(INT)-5] == 0) |
|
475 |
&& (bp[sizeof(INT)-6] == 0) |
|
476 |
&& (bp[sizeof(INT)-7] == 0) |
|
477 |
#endif |
|
478 |
) { |
|
479 |
RETURN (__MKSMALLINT(_MIN_INT)); |
|
480 |
} |
|
481 |
} |
|
482 |
%}. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
483 |
"/ sz == 4 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
484 |
"/ (digitByteArray at:1) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
485 |
"/ (digitByteArray at:2) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
486 |
"/ (digitByteArray at:3) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
487 |
"/ (digitByteArray at:4) == 16r40 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
488 |
"/ ^ SmallInteger minVal |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
489 |
"/ ]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
490 |
"/ ] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
491 |
"/ ] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
492 |
"/ ] |
2952 | 493 |
"/ ] |
1210 | 494 |
]. |
495 |
newNumber := self shallowCopy. |
|
496 |
newNumber sign:(sign negated). |
|
497 |
^ newNumber |
|
498 |
! |
|
499 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
500 |
quo:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
501 |
"return the quotient of the receiver and the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
502 |
The result is truncated toward zero (which is different from //, which |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
503 |
truncates toward negative infinity). |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
504 |
The results sign is negative if the receiver has a sign |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
505 |
different from the args sign" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
506 |
|
2521 | 507 |
|otherSign quo abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
508 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
509 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
510 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
511 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
512 |
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
|
513 |
Use a special method for this case ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
514 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
515 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 516 |
abs := aNumber. |
517 |
abs := abs abs. |
|
518 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
519 |
quo := (self absFastDiv:abs) at:1. |
|
520 |
(sign == otherSign) ifTrue:[^ quo]. |
|
521 |
^ quo sign:-1 |
|
522 |
] |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
523 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
524 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
525 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
526 |
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
|
527 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
528 |
(aNumber class == self class) ifFalse:[ |
2001 | 529 |
^ self retry:#quo: coercing:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
530 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
531 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
532 |
sign < 0 ifTrue:[ |
2001 | 533 |
(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
|
534 |
] ifFalse:[ |
2001 | 535 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:1]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
536 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
537 |
^ ((self absDiv:aNumber) at:1) sign:-1 |
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 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
540 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
541 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
542 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
543 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
544 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
545 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
546 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
547 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
548 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
549 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
550 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
551 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
552 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
553 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
554 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
555 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
556 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
557 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
558 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
559 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
560 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
561 |
"Modified: 5.11.1996 / 14:14:17 / cg" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
562 |
! |
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 |
rem:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
565 |
"return the remainder of division of the receiver by the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
566 |
The returned remainder has the same sign as the receiver." |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
567 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
568 |
|rem abs "{ Class: SmallInteger }" | |
927
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 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
571 |
this is the common case, dividing by a SmallInteger. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
572 |
Use special code for this case ... |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
573 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
574 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 575 |
abs := aNumber. |
576 |
abs := abs abs. |
|
577 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
578 |
rem := (self absFastDiv:abs) at:2. |
|
579 |
] ifFalse:[ |
|
580 |
rem := (self absDiv:(abs asLargeInteger)) at:2 |
|
581 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
582 |
] ifFalse:[ |
2001 | 583 |
" |
584 |
if the argument is not a largeInteger, coerce |
|
585 |
" |
|
586 |
(aNumber class == self class) ifFalse:[ |
|
587 |
^ self retry:#\\ coercing:aNumber |
|
588 |
]. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
589 |
|
2001 | 590 |
rem := (self absDiv:aNumber) at:2 |
1 | 591 |
]. |
92 | 592 |
|
593 |
sign < 0 ifTrue:[ |
|
2001 | 594 |
^ rem sign:-1 |
1 | 595 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
596 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
597 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
598 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
599 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
600 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
601 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
602 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
603 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
604 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
605 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
606 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
607 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
608 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
609 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
610 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
611 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
612 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
613 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
614 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
615 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
616 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
617 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
618 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
619 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
620 |
"Modified: 5.11.1996 / 14:02:59 / cg" |
1 | 621 |
! ! |
622 |
||
2855 | 623 |
!LargeInteger methodsFor:'bit operators'! |
624 |
||
625 |
lowBit |
|
626 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
627 |
starts at 1 for the least significant bit. Returns -1 if no bit is set. |
|
628 |
For negative numbers, the low bit of my absolute value is returned. |
|
629 |
Redefined here for more performance of the gcd: algorithm, which |
|
630 |
is used when big fractions are reduced." |
|
631 |
||
632 |
|sz "{ Class: SmallInteger }" |
|
633 |
byte| |
|
634 |
||
635 |
sz := digitByteArray size. |
|
636 |
1 to:sz do:[:digitIndex | |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
637 |
(byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
638 |
^ (digitIndex-1)*8 + byte lowBit |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
639 |
] |
2855 | 640 |
]. |
641 |
^ -1 |
|
642 |
||
643 |
" |
|
644 |
(1 bitShift:30) lowBit |
|
645 |
(1 bitShift:30) highBit |
|
646 |
(1 bitShift:31) lowBit |
|
647 |
(1 bitShift:31) highBit |
|
648 |
(1 bitShift:32) lowBit |
|
649 |
(1 bitShift:32) highBit |
|
650 |
(1 bitShift:33) lowBit |
|
651 |
(1 bitShift:33) highBit |
|
652 |
(1 bitShift:64) lowBit |
|
653 |
(1 bitShift:64) highBit |
|
654 |
(1 bitShift:1000) lowBit |
|
655 |
(1 bitShift:1000) highBit |
|
656 |
((1 bitShift:64)-1) lowBit |
|
657 |
((1 bitShift:64)-1) highBit |
|
658 |
" |
|
659 |
||
660 |
"Modified: 14.8.1997 / 11:55:34 / cg" |
|
661 |
! ! |
|
662 |
||
701 | 663 |
!LargeInteger methodsFor:'byte access'! |
664 |
||
665 |
digitAt:index |
|
666 |
"return 8 bits of value, starting at byte index" |
|
667 |
||
668 |
index > digitByteArray size ifTrue:[^ 0]. |
|
669 |
^ digitByteArray at:index |
|
670 |
! |
|
671 |
||
672 |
digitAt:index put:aByte |
|
673 |
"set the 8 bits, index is a byte index" |
|
674 |
||
675 |
digitByteArray at:index put:aByte |
|
676 |
! |
|
677 |
||
678 |
digitLength |
|
2816 | 679 |
"return the number bytes used by this Integer. |
680 |
For negative receivers, the digitLength of its absolute value |
|
681 |
is returned." |
|
701 | 682 |
|
683 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
684 |
check if there is a 0-byte ... |
701 | 685 |
this allows to ask unnormalized LargeIntegers |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
686 |
for their digitLength |
701 | 687 |
" |
688 |
|l "{ Class: SmallInteger }" | |
|
92 | 689 |
|
701 | 690 |
l := digitByteArray size. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
691 |
[l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
692 |
l := l - 1. |
701 | 693 |
]. |
694 |
^ l |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
695 |
|
2816 | 696 |
"Modified: 31.7.1997 / 13:18:28 / cg" |
701 | 697 |
! |
698 |
||
699 |
digits |
|
700 |
"return a byteArray fille with the receivers bits |
|
701 |
(8 bits of the absolute value per element)" |
|
702 |
||
703 |
^ digitByteArray |
|
704 |
! ! |
|
705 |
||
706 |
!LargeInteger methodsFor:'coercing & converting'! |
|
68 | 707 |
|
701 | 708 |
asFloat |
709 |
"return a Float with same value as myself. |
|
710 |
Since floats have a limited precision, you usually loose bits when |
|
711 |
doing this." |
|
712 |
||
713 |
|newFloat| |
|
714 |
||
715 |
newFloat := 0.0. |
|
716 |
(sign == 0) ifFalse:[ |
|
717 |
digitByteArray reverseDo:[:aDigit | |
|
718 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
719 |
]. |
|
720 |
(sign < 0) ifTrue:[ |
|
721 |
newFloat := newFloat negated |
|
345 | 722 |
] |
701 | 723 |
]. |
724 |
^ newFloat |
|
725 |
||
726 |
" |
|
727 |
1234567890 asFloat |
|
728 |
12345678901234567890 asFloat |
|
729 |
12345678901234567890 asFloat asInteger |
|
730 |
" |
|
731 |
! |
|
732 |
||
733 |
asLargeInteger |
|
734 |
"return a LargeInteger with same value as myself - thats me" |
|
735 |
||
736 |
^ self |
|
737 |
! |
|
738 |
||
739 |
asSmallInteger |
|
740 |
"return a SmallInteger with same value as myself - |
|
741 |
the result is invalid if the receivers value cannot |
|
742 |
be represented as a SmallInteger. |
|
743 |
Q: should we raise an exception if this happens ?" |
|
744 |
||
745 |
|value| |
|
746 |
||
747 |
value := 0. |
|
748 |
(sign == 0) ifFalse:[ |
|
749 |
digitByteArray reverseDo:[:aDigit | |
|
750 |
value := (value times:256) + aDigit |
|
345 | 751 |
]. |
701 | 752 |
(sign < 0) ifTrue:[ |
753 |
value := value negated |
|
345 | 754 |
] |
68 | 755 |
]. |
701 | 756 |
^ value |
757 |
! |
|
758 |
||
759 |
coerce:aNumber |
|
760 |
"return the argument as a LargeInteger" |
|
761 |
||
762 |
^ aNumber asLargeInteger |
|
763 |
! |
|
764 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
765 |
compressed |
701 | 766 |
"if the receiver can be represented as a SmallInteger, return |
2267 | 767 |
a SmallInteger with my value; |
768 |
otherwise remove leading zero bytes in the digitByte array |
|
769 |
and return the receiver" |
|
701 | 770 |
|
771 |
|index "{ Class: SmallInteger }" |
|
2267 | 772 |
val "{ Class: SmallInteger }" |
773 |
index0 |
|
774 |
| |
|
701 | 775 |
|
776 |
%{ /* NOCONTEXT */ |
|
777 |
OBJ t; |
|
778 |
||
1133 | 779 |
if (__INST(sign) == __MKSMALLINT(0)) { |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
780 |
RETURN (__MKSMALLINT(0)); |
701 | 781 |
} |
782 |
||
1133 | 783 |
t = __INST(digitByteArray); |
701 | 784 |
if (__isByteArray(t)) { |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
785 |
unsigned char *_digitBytes = __ByteArrayInstPtr(t)->ba_element; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
786 |
int _idx, _idx0; |
2786 | 787 |
INT _val; |
701 | 788 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
789 |
_idx = _idx0 = _byteArraySize(t); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
790 |
while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
791 |
_idx--; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
792 |
} |
2788 | 793 |
#ifdef alpha64 |
2786 | 794 |
switch (_idx) { |
795 |
case 7: |
|
796 |
_val = (_digitBytes[6]<<8); |
|
797 |
_val = (_val + _digitBytes[5]) << 8; |
|
798 |
_val = (_val + _digitBytes[4]) << 8; |
|
799 |
_val = (_val + _digitBytes[3]) << 8; |
|
800 |
_val = (_val + _digitBytes[2]) << 8; |
|
801 |
_val = (_val + _digitBytes[1]) << 8; |
|
802 |
_val += _digitBytes[0]; |
|
803 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
804 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
805 |
RETURN (__MKSMALLINT(_val)); |
2786 | 806 |
case 6: |
807 |
_val = (_digitBytes[5]<<8); |
|
808 |
_val = (_val + _digitBytes[4]) << 8; |
|
809 |
_val = (_val + _digitBytes[3]) << 8; |
|
810 |
_val = (_val + _digitBytes[2]) << 8; |
|
811 |
_val = (_val + _digitBytes[1]) << 8; |
|
812 |
_val += _digitBytes[0]; |
|
813 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
814 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
815 |
RETURN (__MKSMALLINT(_val)); |
2786 | 816 |
case 5: |
817 |
_val = (_digitBytes[4]<<8); |
|
818 |
_val = (_val + _digitBytes[3]) << 8; |
|
819 |
_val = (_val + _digitBytes[2]) << 8; |
|
820 |
_val = (_val + _digitBytes[1]) << 8; |
|
821 |
_val += _digitBytes[0]; |
|
822 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
823 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
824 |
RETURN (__MKSMALLINT(_val)); |
2786 | 825 |
case 4: |
826 |
_val = (_digitBytes[3]<<8); |
|
827 |
_val = (_val + _digitBytes[2]) << 8; |
|
828 |
_val = (_val + _digitBytes[1]) << 8; |
|
829 |
_val += _digitBytes[0]; |
|
830 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
831 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
832 |
RETURN (__MKSMALLINT(_val)); |
2786 | 833 |
case 3: |
834 |
_val = (_digitBytes[2]<<8); |
|
835 |
_val = (_val + _digitBytes[1]) << 8; |
|
836 |
_val += _digitBytes[0]; |
|
837 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
838 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
839 |
RETURN (__MKSMALLINT(_val)); |
2786 | 840 |
case 2: |
841 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
842 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
843 |
_val = -_val; |
|
844 |
RETURN (__MKSMALLINT(_val)); |
|
845 |
case 1: |
|
846 |
_val = _digitBytes[0]; |
|
847 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
848 |
_val = -_val; |
|
849 |
RETURN (__MKSMALLINT(_val)); |
|
850 |
case 0: |
|
851 |
RETURN (__MKSMALLINT(0)); |
|
852 |
||
853 |
} |
|
854 |
#else |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
855 |
if (_idx <= 4) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
856 |
if (_idx <= 2) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
857 |
if (_idx == 0) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
858 |
RETURN (__MKSMALLINT(0)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
859 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
860 |
if (_idx == 1) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
861 |
_val = _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
862 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
863 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
864 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
865 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
866 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
867 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
868 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
869 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
870 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
871 |
if (_idx == 3) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
872 |
_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
873 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
874 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
875 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
876 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
877 |
_val = _digitBytes[3]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
878 |
if (_val <= 0x40) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
879 |
_val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
880 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
881 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
882 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
883 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
884 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
885 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
886 |
} |
2786 | 887 |
#endif |
701 | 888 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
889 |
if (_idx == _idx0) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
890 |
RETURN (self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
891 |
} |
2267 | 892 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
893 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
894 |
* must copy & cut off some bytes |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
895 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
896 |
{ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
897 |
OBJ newDigits; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
898 |
OBJ oldDigits; |
701 | 899 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
900 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
901 |
* careful - there is no context here to protect |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
902 |
* the receiver ... |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
903 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
904 |
__PROTECT__(self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
905 |
__PROTECT__(__INST(digitByteArray)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
906 |
newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
907 |
__UNPROTECT__(oldDigits); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
908 |
__UNPROTECT__(self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
909 |
if (newDigits) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
910 |
bcopy(__ByteArrayInstPtr(oldDigits)->ba_element, |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
911 |
__ByteArrayInstPtr(newDigits)->ba_element, |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
912 |
_idx); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
913 |
__INST(digitByteArray) = newDigits; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
914 |
__STORE(self, newDigits); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
915 |
RETURN (self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
916 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
917 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
918 |
* allocation failed ... |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
919 |
* ... fall through to trigger the error |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
920 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
921 |
} |
701 | 922 |
} |
923 |
%}. |
|
2267 | 924 |
index0 := index := digitByteArray size. |
701 | 925 |
[(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
926 |
index := index - 1 |
701 | 927 |
]. |
928 |
||
2267 | 929 |
(index ~~ index0) ifTrue:[ |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
930 |
digitByteArray := digitByteArray copyFrom:1 to:index |
701 | 931 |
]. |
932 |
^ self |
|
933 |
! |
|
934 |
||
935 |
value:aSmallInteger |
|
936 |
"return a new LargeInteger with value taken from a SmallInteger. |
|
937 |
This method will fail, if the argument is not a smallInteger." |
|
938 |
||
939 |
|absValue |
|
940 |
b1 "{ Class: SmallInteger }" |
|
941 |
b2 "{ Class: SmallInteger }" |
|
2786 | 942 |
b3 "{ Class: SmallInteger }" |
943 |
b4 "{ Class: SmallInteger }" |
|
944 |
b5 "{ Class: SmallInteger }" |
|
945 |
b6 "{ Class: SmallInteger }" |
|
946 |
b7 "{ Class: SmallInteger }"| |
|
92 | 947 |
|
948 |
" |
|
701 | 949 |
could have simply created a 4-byte largeinteger and normalize |
950 |
it; the code below does the normalize right away, avoiding the |
|
951 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
92 | 952 |
" |
701 | 953 |
(aSmallInteger == 0) ifTrue: [ |
2001 | 954 |
digitByteArray := ByteArray with:0. |
955 |
sign := 0. |
|
956 |
^ self |
|
701 | 957 |
]. |
958 |
(aSmallInteger < 0) ifTrue: [ |
|
2001 | 959 |
sign := -1. |
960 |
absValue := aSmallInteger negated |
|
701 | 961 |
] ifFalse: [ |
2001 | 962 |
sign := 1. |
963 |
absValue := aSmallInteger |
|
701 | 964 |
]. |
965 |
b1 := absValue bitAnd:16rFF. |
|
966 |
absValue := absValue bitShift:-8. |
|
967 |
absValue == 0 ifTrue:[ |
|
2001 | 968 |
digitByteArray := ByteArray with:b1 |
701 | 969 |
] ifFalse:[ |
2001 | 970 |
b2 := absValue bitAnd:16rFF. |
971 |
absValue := absValue bitShift:-8. |
|
972 |
absValue == 0 ifTrue:[ |
|
973 |
digitByteArray := ByteArray with:b1 with:b2 |
|
974 |
] ifFalse:[ |
|
975 |
b3 := absValue bitAnd:16rFF. |
|
976 |
absValue := absValue bitShift:-8. |
|
977 |
absValue == 0 ifTrue:[ |
|
978 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
979 |
] ifFalse:[ |
|
2786 | 980 |
b4 := absValue bitAnd:16rFF. |
981 |
absValue := absValue bitShift:-8. |
|
982 |
absValue == 0 ifTrue:[ |
|
983 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 |
|
984 |
] ifFalse:[ |
|
985 |
b5 := absValue bitAnd:16rFF. |
|
986 |
absValue := absValue bitShift:-8. |
|
987 |
absValue == 0 ifTrue:[ |
|
988 |
digitByteArray := ByteArray new:5. |
|
989 |
digitByteArray at:1 put:b1. |
|
990 |
digitByteArray at:2 put:b2. |
|
991 |
digitByteArray at:3 put:b3. |
|
992 |
digitByteArray at:4 put:b4. |
|
993 |
digitByteArray at:5 put:b5. |
|
994 |
] ifFalse:[ |
|
995 |
b6 := absValue bitAnd:16rFF. |
|
996 |
absValue := absValue bitShift:-8. |
|
997 |
absValue == 0 ifTrue:[ |
|
998 |
digitByteArray := ByteArray new:6. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
999 |
digitByteArray at:1 put:b1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1000 |
digitByteArray at:2 put:b2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1001 |
digitByteArray at:3 put:b3. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1002 |
digitByteArray at:4 put:b4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1003 |
digitByteArray at:5 put:b5. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1004 |
digitByteArray at:6 put:b6. |
2786 | 1005 |
] ifFalse:[ |
1006 |
b7 := absValue bitAnd:16rFF. |
|
1007 |
absValue := absValue bitShift:-8. |
|
1008 |
absValue == 0 ifTrue:[ |
|
1009 |
digitByteArray := ByteArray new:7. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1010 |
digitByteArray at:1 put:b1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1011 |
digitByteArray at:2 put:b2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1012 |
digitByteArray at:3 put:b3. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1013 |
digitByteArray at:4 put:b4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1014 |
digitByteArray at:5 put:b5. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1015 |
digitByteArray at:6 put:b6. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1016 |
digitByteArray at:7 put:b7. |
2786 | 1017 |
] ifFalse:[ |
1018 |
digitByteArray := ByteArray new:8. |
|
1019 |
digitByteArray at:1 put:b1. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1020 |
digitByteArray at:2 put:b2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1021 |
digitByteArray at:3 put:b3. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1022 |
digitByteArray at:4 put:b4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1023 |
digitByteArray at:5 put:b5. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1024 |
digitByteArray at:6 put:b6. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1025 |
digitByteArray at:7 put:b7. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1026 |
digitByteArray at:7 put:absValue. |
2786 | 1027 |
] |
1028 |
] |
|
1029 |
] |
|
1030 |
] |
|
2001 | 1031 |
] |
1032 |
] |
|
701 | 1033 |
] |
1890 | 1034 |
|
1035 |
"Modified: 5.11.1996 / 16:15:39 / cg" |
|
701 | 1036 |
! ! |
1037 |
||
1038 |
!LargeInteger methodsFor:'comparing'! |
|
1039 |
||
1040 |
< aNumber |
|
1041 |
"return true, if the argument, aNumber is greater than the receiver" |
|
1042 |
||
1043 |
|otherSign| |
|
1044 |
||
1045 |
(aNumber class == self class) ifFalse:[ |
|
1046 |
^ self retry:#< coercing:aNumber |
|
1047 |
]. |
|
1048 |
otherSign := aNumber sign. |
|
1049 |
||
1050 |
(sign > 0) ifTrue:[ |
|
1051 |
"I am positive" |
|
1052 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
1053 |
^ false "aNumber is <= 0" |
|
1054 |
]. |
|
1055 |
(sign == 0) ifTrue:[ |
|
1056 |
(otherSign > 0) ifTrue:[^ true]. |
|
1057 |
^ false |
|
1058 |
]. |
|
1059 |
"I am negative" |
|
1060 |
(otherSign > 0) ifTrue:[^ true]. |
|
1061 |
(otherSign == 0) ifTrue:[^ true]. |
|
1062 |
^ (self absLess:aNumber) not |
|
92 | 1063 |
! |
1064 |
||
701 | 1065 |
= aNumber |
1066 |
"return true, if the argument, aNumber has the same value as |
|
1067 |
the receiver" |
|
1068 |
||
1069 |
(aNumber class == self class) ifFalse:[ |
|
1070 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
|
1071 |
^ self retry:#= coercing:aNumber |
|
1072 |
]. |
|
1073 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
1074 |
^ self absEq:aNumber |
|
1075 |
! |
|
1076 |
||
1077 |
> aNumber |
|
1078 |
"return true, if the argument, aNumber is less than the receiver" |
|
1079 |
||
1080 |
|otherSign| |
|
1081 |
||
1082 |
(aNumber class == self class) ifFalse:[ |
|
1083 |
^ self retry:#> coercing:aNumber |
|
1084 |
]. |
|
1085 |
otherSign := aNumber sign. |
|
1086 |
||
1087 |
(sign > 0) ifTrue:[ |
|
1088 |
"I am positive" |
|
1089 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
1090 |
^ true "aNumber is <= 0" |
|
1091 |
]. |
|
1092 |
(sign == 0) ifTrue:[ |
|
1093 |
"I am zero" |
|
1094 |
(otherSign > 0) ifTrue:[^ false]. |
|
1095 |
^ true |
|
1096 |
]. |
|
1097 |
"I am negative" |
|
1098 |
(otherSign > 0) ifTrue:[^ false]. |
|
1099 |
(otherSign == 0) ifTrue:[^ false]. |
|
1100 |
^ (self absLess:aNumber) not |
|
1101 |
! ! |
|
1102 |
||
1103 |
!LargeInteger methodsFor:'double dispatching'! |
|
1104 |
||
92 | 1105 |
differenceFromInteger:anInteger |
1106 |
"sent, when anInteger does not know how to subtract the receiver. |
|
1107 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
1108 |
||
1109 |
anInteger > 0 ifTrue:[ |
|
2001 | 1110 |
sign > 0 ifTrue:[ |
1111 |
^ (self absFastMinus:anInteger) sign:-1 |
|
1112 |
]. |
|
1113 |
^ self absFastPlus:anInteger |
|
92 | 1114 |
]. |
1794 | 1115 |
anInteger == 0 ifTrue:[ |
2001 | 1116 |
^ self negated |
1794 | 1117 |
]. |
1118 |
sign > 0 ifTrue:[ |
|
2001 | 1119 |
^ (self absFastPlus:anInteger negated) sign:-1 |
1794 | 1120 |
]. |
1121 |
^ (self absFastMinus:anInteger) sign:-1 |
|
92 | 1122 |
|
1123 |
" |
|
1124 |
12345678901234567890 |
|
1125 |
-12345678901234567890 |
|
1126 |
12345678901234567890 differenceFromInteger:0 |
|
1127 |
12345678901234567890 differenceFromInteger:1 |
|
1128 |
12345678901234567890 differenceFromInteger:-1 |
|
1129 |
-12345678901234567890 differenceFromInteger:1 |
|
1130 |
-12345678901234567890 differenceFromInteger:-1 |
|
1131 |
" |
|
1794 | 1132 |
|
1133 |
"Modified: 20.10.1996 / 18:41:54 / cg" |
|
68 | 1134 |
! |
1135 |
||
1136 |
productFromInteger:anInteger |
|
1137 |
"sent, when anInteger does not know how to multiply the receiver. |
|
1138 |
Return the product of the receiver and the argument, aSmallInteger" |
|
1139 |
||
1140 |
|num result |
|
1141 |
resultDigitByteArray |
|
1142 |
val "{ Class: SmallInteger }" |
|
1143 |
len "{ Class: SmallInteger }" |
|
1144 |
carry "{ Class: SmallInteger }" |
|
85 | 1145 |
prod "{ Class: SmallInteger }" |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1146 |
ok lResult| |
68 | 1147 |
|
1148 |
"multiplying by a small integer is done here" |
|
1149 |
||
1150 |
"trivial cases" |
|
1151 |
anInteger == 0 ifTrue:[^ 0]. |
|
1152 |
anInteger == 1 ifTrue:[^ self]. |
|
1153 |
||
1154 |
num := anInteger abs. |
|
1155 |
(num > 16r3FFFFF) ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1156 |
"if num is too big (so that multiplying by a byte could create a Large)" |
68 | 1157 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1158 |
^ anInteger retry:#* coercing:self |
68 | 1159 |
]. |
1160 |
||
1161 |
len := digitByteArray size. |
|
1162 |
||
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1163 |
val := num. |
2923 | 1164 |
val <= 16rFF ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1165 |
lResult := len + 1. |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1166 |
] ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1167 |
val <= 16rFFFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1168 |
lResult := len + 2 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1169 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1170 |
val <= 16rFFFFFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1171 |
lResult := len + 4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1172 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1173 |
lResult := len + 6. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1174 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1175 |
] |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1176 |
]. |
2933 | 1177 |
resultDigitByteArray := ByteArray uninitializedNew:lResult. |
2897 | 1178 |
result := self class basicNew setDigits:resultDigitByteArray. |
1 | 1179 |
|
68 | 1180 |
anInteger < 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1181 |
sign > 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1182 |
result sign:-1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1183 |
]. |
68 | 1184 |
] ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1185 |
sign < 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1186 |
result sign:sign |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1187 |
] |
68 | 1188 |
]. |
1189 |
||
85 | 1190 |
ok := false. |
1191 |
%{ |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1192 |
OBJ __digitByteArray = __INST(digitByteArray); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1193 |
|
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1194 |
if (__isSmallInteger(len) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1195 |
&& __isByteArray(__digitByteArray) |
85 | 1196 |
&& __isByteArray(resultDigitByteArray)) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1197 |
INT _l = __intVal(len); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1198 |
INT _v = __intVal(val); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1199 |
unsigned INT _carry = 0; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1200 |
unsigned INT _prod; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1201 |
unsigned char *digitP = __ByteArrayInstPtr(__digitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1202 |
unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1203 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1204 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1205 |
* skipping zeros does not help much (a few percent) on |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1206 |
* a P5 or other CPUS with a fast multiplier. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1207 |
* It may make more of a difference on CPUs with slower 0-multiply. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1208 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1209 |
while ((_l >= sizeof(INT)) && (((unsigned INT *)digitP)[0] == 0)) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1210 |
((unsigned long *)resultP)[0] = 0; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1211 |
digitP += sizeof(INT); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1212 |
resultP += sizeof(INT); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1213 |
_l -= sizeof(INT); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1214 |
} |
2923 | 1215 |
|
1216 |
#if defined(i386) || defined(alpha) /* XXX actually: LSB_FIRST */ |
|
1217 |
# if defined (__GNUC__) && defined(i386) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1218 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1219 |
* can do it long-word-wise; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1220 |
* 32*32 -> 64 multiplication |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1221 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1222 |
while (_l > 3) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1223 |
unsigned __pHi, __pLow; |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1224 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1225 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1226 |
* max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1227 |
* + maxCarry (0xFFFF.FFFF) -> 0xFFFF.FFFF.0000.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1228 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1229 |
asm ("mull %3 \n |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1230 |
addl %4,%%eax \n |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1231 |
adcl $0,%%edx" |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1232 |
: "=a" ((unsigned long)(__pLow)), |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1233 |
"=d" ((unsigned long)(__pHi)) |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1234 |
: "0" ((unsigned long)(((unsigned long *)digitP)[0])), |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1235 |
"1" ((unsigned long)(_v)), |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1236 |
"rm" ((unsigned long)(_carry)) ); |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1237 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1238 |
((unsigned long *)resultP)[0] = __pLow; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1239 |
_carry = __pHi; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1240 |
digitP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1241 |
resultP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1242 |
_l -= 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1243 |
} |
2923 | 1244 |
# else |
85 | 1245 |
|
2923 | 1246 |
# if defined(INT64) |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1247 |
if (_v <= 0xFFFFFFFFL) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1248 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1249 |
* have a 64bit int type ... good |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1250 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1251 |
UINT64 _prod64; |
2923 | 1252 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1253 |
/* have 64bit ints; can do it int-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1254 |
* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1255 |
* max: 0xFFFFFFFF * 0xFFFFFFFF -> 0xFFFFFFFE.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1256 |
* + maxCarry (0xFFFFFFFF) -> 0xFFFFFFFF.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1257 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1258 |
while (_l > 3) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1259 |
unsigned __t; |
2964 | 1260 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1261 |
__t = ((unsigned *)digitP)[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1262 |
digitP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1263 |
_prod64 = (INT64)_v; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1264 |
_prod64 *= __t; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1265 |
_prod64 += _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1266 |
((unsigned *)resultP)[0] = _prod64 /* & 0xFFFFFFFFL */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1267 |
_carry = _prod64 >> 32; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1268 |
resultP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1269 |
_l -= 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1270 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1271 |
if (_l > 1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1272 |
unsigned short __t; |
2964 | 1273 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1274 |
__t = ((unsigned short *)digitP)[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1275 |
digitP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1276 |
_prod64 = (INT64)_v; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1277 |
_prod64 *= __t; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1278 |
_prod64 += _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1279 |
((unsigned short *)resultP)[0] = _prod64 /* & 0xFFFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1280 |
_carry = _prod64 >> 16; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1281 |
resultP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1282 |
_l -= 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1283 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1284 |
if (_l > 0) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1285 |
_prod64 = *digitP++ * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1286 |
*resultP++ = _prod64 /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1287 |
_carry = _prod64 >> 8; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1288 |
_l--; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1289 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1290 |
} |
2923 | 1291 |
# else |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1292 |
if (_v <= 0xFFFF) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1293 |
/* can do it short-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1294 |
* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1295 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1296 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1297 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1298 |
while (_l > 1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1299 |
_prod = ((unsigned short *)digitP)[0] * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1300 |
((unsigned short *)resultP)[0] = _prod /* & 0xFFFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1301 |
_carry = _prod >> 16; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1302 |
digitP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1303 |
resultP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1304 |
_l -= 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1305 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1306 |
} |
3040 | 1307 |
# endif /* no INT64 */ |
1308 |
# endif /* not GNU-i386 */ |
|
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1309 |
#else /* not LSB_FIRST */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1310 |
if (_v <= 0xFFFF) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1311 |
/* can do it short-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1312 |
* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1313 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1314 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1315 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1316 |
while (_l > 1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1317 |
unsigned int t; |
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1318 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1319 |
t = (digitP[1]<<8) + digitP[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1320 |
_prod = t * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1321 |
resultP[0] = _prod /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1322 |
resultP[1] = (_prod>>8) /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1323 |
_carry = _prod >> 16; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1324 |
digitP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1325 |
resultP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1326 |
_l -= 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1327 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1328 |
} |
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1329 |
|
3040 | 1330 |
#endif /* LSB_FIRST */ |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1331 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1332 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1333 |
* rest is done byte-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1334 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1335 |
while (_l > 0) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1336 |
_prod = *digitP++ * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1337 |
*resultP++ = _prod /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1338 |
_carry = _prod >> 8; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1339 |
_l--; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1340 |
} |
2923 | 1341 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1342 |
_l = __intVal(lResult) - __intVal(len); |
2933 | 1343 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1344 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1345 |
* remaining carry |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1346 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1347 |
while (_carry) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1348 |
*resultP++ = _carry & 0xFF; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1349 |
_carry >>= 8; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1350 |
_l--; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1351 |
} |
2933 | 1352 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1353 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1354 |
* remaining zeros |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1355 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1356 |
while (_l--) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1357 |
*resultP++ = 0; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1358 |
} |
2933 | 1359 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1360 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1361 |
* need compress ? |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1362 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1363 |
if (resultP[-1]) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1364 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1365 |
* no |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1366 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1367 |
RETURN(result); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1368 |
} |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1369 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1370 |
ok = true; |
85 | 1371 |
} |
1372 |
%}. |
|
92 | 1373 |
" |
1374 |
fall back - normally not reached |
|
1375 |
(could make it a primitive-failure as well) |
|
1376 |
" |
|
85 | 1377 |
ok ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1378 |
carry := 0. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1379 |
1 to:len do:[:i | |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1380 |
prod := (digitByteArray basicAt:i) * val + carry. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1381 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1382 |
carry := prod bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1383 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1384 |
[carry ~~ 0] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1385 |
len := len + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1386 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1387 |
carry := carry bitShift:-8 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1388 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1389 |
[len < lResult] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1390 |
len := len + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1391 |
resultDigitByteArray basicAt:len put:0 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1392 |
] |
68 | 1393 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1394 |
^ result compressed |
92 | 1395 |
! |
1396 |
||
701 | 1397 |
sumFromInteger:anInteger |
1398 |
"sent, when anInteger does not know how to add the receiver. |
|
1399 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
|
92 | 1400 |
|
701 | 1401 |
anInteger > 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1402 |
sign > 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1403 |
^ self absFastPlus:anInteger |
2001 | 1404 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1405 |
^ (self absFastMinus:anInteger) sign:-1 |
701 | 1406 |
]. |
1794 | 1407 |
anInteger == 0 ifTrue:[ |
2001 | 1408 |
^ self |
1794 | 1409 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1410 |
sign > 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1411 |
^ self absFastMinus:anInteger |
1794 | 1412 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1413 |
^ (self absFastPlus:anInteger abs) sign:-1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1414 |
|
92 | 1415 |
|
701 | 1416 |
" |
1417 |
12345678901234567890 |
|
1418 |
-12345678901234567890 |
|
1419 |
12345678901234567890 sumFromInteger:0 |
|
1420 |
-12345678901234567890 sumFromInteger:0 |
|
1421 |
12345678901234567890 sumFromInteger:1 |
|
1422 |
12345678901234567890 sumFromInteger:-1 |
|
1423 |
-12345678901234567890 sumFromInteger:1 |
|
1424 |
-12345678901234567890 sumFromInteger:-1 |
|
1425 |
" |
|
1794 | 1426 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1427 |
"Modified: / 9.1.1998 / 13:27:37 / cg" |
68 | 1428 |
! ! |
1429 |
||
701 | 1430 |
!LargeInteger methodsFor:'printing & storing'! |
345 | 1431 |
|
701 | 1432 |
storeOn:aStream |
1433 |
"append a representation of the receiver to aStream, which can |
|
1434 |
be used to reconstruct the receiver." |
|
68 | 1435 |
|
701 | 1436 |
self printOn:aStream. |
1437 |
aStream nextPutAll:' asLargeInteger' |
|
68 | 1438 |
! ! |
1439 |
||
1440 |
!LargeInteger methodsFor:'private'! |
|
1441 |
||
701 | 1442 |
absDiv:anInteger |
1443 |
"return an array with two LargeIntegers representing |
|
1444 |
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
|
1445 |
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
|
1446 |
This method needs a rewrite." |
68 | 1447 |
|
701 | 1448 |
|tmp1 tmp2 |
1890 | 1449 |
quo |
2521 | 1450 |
count "{ Class: SmallInteger }" | |
92 | 1451 |
|
701 | 1452 |
anInteger == 0 ifTrue:[ |
2001 | 1453 |
^ DivisionByZeroSignal raise |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1454 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1455 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1456 |
self = anInteger ifTrue:[ |
2001 | 1457 |
^ Array with:1 with:0 |
701 | 1458 |
]. |
92 | 1459 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1460 |
tmp1 := self simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1461 |
tmp1 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1462 |
tmp2 := anInteger simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1463 |
tmp2 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1464 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1465 |
(tmp1 < tmp2) ifTrue:[ |
2001 | 1466 |
^ Array with:0 with:tmp1 |
68 | 1467 |
]. |
92 | 1468 |
|
701 | 1469 |
count := 0. |
1890 | 1470 |
[tmp2 absLess: tmp1] whileTrue:[ |
2001 | 1471 |
tmp2 mul2. |
1472 |
count := count + 1 |
|
701 | 1473 |
]. |
1474 |
||
1890 | 1475 |
tmp2 div2. |
701 | 1476 |
|
1890 | 1477 |
quo := 0 asLargeInteger. |
1478 |
quo sign:1. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1479 |
|
701 | 1480 |
[count == 0] whileFalse:[ |
2001 | 1481 |
quo mul2. |
1482 |
(tmp1 absLess:tmp2) ifFalse:[ |
|
1483 |
quo digits at:1 put:((quo digits at:1) bitOr:1). |
|
1484 |
(tmp1 absSubtract: tmp2) ifFalse:[ |
|
1485 |
"/ difference is zero; continue shifting |
|
1486 |
count := count - 1. |
|
1487 |
[count >= 8] whileTrue:[ |
|
1488 |
quo mul256. |
|
1489 |
count := count - 8 |
|
1490 |
]. |
|
1491 |
[count == 0] whileFalse:[ |
|
1492 |
quo mul2. |
|
1493 |
count := count - 1. |
|
1494 |
]. |
|
1495 |
^ Array with:quo compressed with:tmp1 compressed |
|
1496 |
]. |
|
1497 |
]. |
|
1498 |
tmp2 div2. |
|
1499 |
count := count - 1 |
|
701 | 1500 |
]. |
1890 | 1501 |
^ Array with:quo compressed with:tmp1 compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1502 |
|
1890 | 1503 |
"Modified: 5.11.1996 / 18:40:24 / cg" |
92 | 1504 |
! |
1505 |
||
701 | 1506 |
absEq:aLargeInteger |
1507 |
"return true, if abs(self) = abs(theArgument)" |
|
92 | 1508 |
|
701 | 1509 |
|len1 "{ Class: SmallInteger }" |
1510 |
len2 "{ Class: SmallInteger }" |
|
1511 |
d1 "{ Class: SmallInteger }" |
|
1512 |
d2 "{ Class: SmallInteger }" |
|
1513 |
otherDigitByteArray | |
|
92 | 1514 |
|
701 | 1515 |
len1 := digitByteArray size. |
1516 |
otherDigitByteArray := aLargeInteger digits. |
|
1517 |
len2 := otherDigitByteArray size. |
|
92 | 1518 |
|
701 | 1519 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
1520 |
len1 := len1 - 1 |
|
1521 |
]. |
|
1522 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
1523 |
len2 := len2 - 1 |
|
68 | 1524 |
]. |
701 | 1525 |
(len1 ~~ len2) ifTrue:[^ false]. |
1526 |
[len1 > 0] whileTrue:[ |
|
1527 |
d1 := digitByteArray basicAt:len1. |
|
1528 |
d2 := otherDigitByteArray basicAt:len1. |
|
1529 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1530 |
len1 := len1 - 1 |
|
68 | 1531 |
]. |
701 | 1532 |
^ true |
68 | 1533 |
! |
1534 |
||
1890 | 1535 |
absFastDiv:aPositiveSmallInteger |
68 | 1536 |
"return an array with two LargeIntegers representing |
1890 | 1537 |
abs(self) // aPositiveSmallInteger and abs(self) \\ aPositiveSmallInteger" |
68 | 1538 |
|
77 | 1539 |
|tmp1 "{ Class: SmallInteger }" |
1540 |
prevRest "{ Class: SmallInteger }" |
|
1541 |
count "{ Class: SmallInteger }" |
|
85 | 1542 |
newDigitByteArray result |
1543 |
ok| |
|
68 | 1544 |
|
1890 | 1545 |
aPositiveSmallInteger == 0 ifTrue:[ |
2001 | 1546 |
^ DivisionByZeroSignal raise |
68 | 1547 |
]. |
1548 |
||
1549 |
"This cannot happen (if always normalized) |
|
1890 | 1550 |
self < aPositiveSmallInteger ifTrue:[ |
2001 | 1551 |
^ Array with:0 with:self |
68 | 1552 |
]. |
1553 |
" |
|
1554 |
count := digitByteArray size. |
|
1555 |
result := self class basicNew numberOfDigits:count. |
|
1556 |
newDigitByteArray := result digits. |
|
85 | 1557 |
ok := false. |
1558 |
%{ |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1559 |
OBJ __digits; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1560 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1561 |
__digits = __INST(digitByteArray); |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1562 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1563 |
if (__isByteArray(__digits) |
85 | 1564 |
&& __isByteArray(newDigitByteArray) |
1890 | 1565 |
&& __bothSmallInteger(count, aPositiveSmallInteger)) { |
2786 | 1566 |
unsigned INT rest = 0; |
2001 | 1567 |
int index = __intVal(count); |
1568 |
int index0; |
|
2786 | 1569 |
unsigned INT divisor = __intVal(aPositiveSmallInteger); |
2001 | 1570 |
unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element; |
1571 |
unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element; |
|
68 | 1572 |
|
2923 | 1573 |
index0 = index - 1; |
2952 | 1574 |
|
1575 |
#ifdef i386 |
|
2001 | 1576 |
if (divisor < 0xFFFF) { |
2952 | 1577 |
if ((index & 1) == 0) { /* even number of bytes */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1578 |
while (index > 1) { |
2952 | 1579 |
unsigned INT t; |
1580 |
unsigned INT div; |
|
85 | 1581 |
|
2952 | 1582 |
# ifdef i386 /* LSB_FIRST */ |
1583 |
index -= 2; |
|
1584 |
t = *((unsigned short *)(&digitBytes[index])); |
|
1585 |
# else |
|
1586 |
index--; |
|
1587 |
t = digitBytes[index]; |
|
1588 |
index--; |
|
1589 |
t = (t << 8) | digitBytes[index]; |
|
1590 |
# endif |
|
1591 |
t = t | (rest << 16); |
|
1592 |
div = t / divisor; |
|
1593 |
rest = t % divisor; |
|
1594 |
# ifdef i386 /* LSB_FIRST */ |
|
1595 |
*((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF); |
|
1596 |
# else |
|
1597 |
resultBytes[index+1] = (div >> 8); |
|
1598 |
resultBytes[index] = (div & 0xFF); |
|
1599 |
# endif |
|
1600 |
} |
|
2001 | 1601 |
} |
1602 |
} |
|
2952 | 1603 |
#endif |
2001 | 1604 |
while (index > 0) { |
2786 | 1605 |
unsigned INT t; |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1606 |
|
2001 | 1607 |
index--; |
1608 |
t = digitBytes[index]; |
|
1609 |
t = t | (rest << 8); |
|
1610 |
resultBytes[index] = t / divisor; |
|
1611 |
rest = t % divisor; |
|
1612 |
} |
|
1613 |
prevRest = __MKSMALLINT(rest); |
|
1614 |
ok = true; |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1615 |
|
2001 | 1616 |
/* |
1617 |
* no need to normalize ? |
|
1618 |
*/ |
|
2933 | 1619 |
while ((index0 > sizeof(INT)) && (resultBytes[index0]==0)) { |
2923 | 1620 |
index0--; |
1621 |
} |
|
1622 |
||
1623 |
if ((index0 > sizeof(INT)) |
|
2001 | 1624 |
&& (resultBytes[index0-1])) { |
1625 |
RETURN ( __ARRAY_WITH2(result, prevRest)); |
|
1626 |
} |
|
85 | 1627 |
} |
1628 |
%}. |
|
1629 |
" |
|
1630 |
slow code - not normally reached |
|
1631 |
(could also do a primitiveFailure here) |
|
1632 |
" |
|
92 | 1633 |
ok ifFalse:[ |
2001 | 1634 |
self primitiveFailed |
68 | 1635 |
]. |
1636 |
||
2933 | 1637 |
^ Array with:(result compressed) with:prevRest |
68 | 1638 |
! |
1639 |
||
701 | 1640 |
absFastMinus:aSmallInteger |
1641 |
"return a LargeInteger representing abs(self) - abs(theArgument). |
|
1642 |
The result is not normalized." |
|
1643 |
||
1794 | 1644 |
|result resultDigitByteArray lastDigit |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1645 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1646 |
rsltLen "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1647 |
index "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1648 |
borrow "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1649 |
diff "{ Class: SmallInteger }" | |
701 | 1650 |
|
1651 |
len := digitByteArray size. |
|
1652 |
||
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1653 |
rsltLen := len + 1. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1654 |
result := self class basicNew numberOfDigits:rsltLen. |
701 | 1655 |
resultDigitByteArray := result digits. |
1656 |
||
1657 |
index := 1. |
|
1658 |
borrow := aSmallInteger abs. |
|
1659 |
||
1660 |
[borrow ~~ 0] whileTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1661 |
(index <= len) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1662 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1663 |
borrow := borrow bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1664 |
diff < 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1665 |
diff := diff + 256. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1666 |
borrow := borrow + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1667 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1668 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1669 |
diff := borrow bitAnd:255. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1670 |
borrow := borrow bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1671 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1672 |
resultDigitByteArray basicAt:index put:(lastDigit := diff). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1673 |
index := index + 1 |
701 | 1674 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1675 |
(index <= rsltLen) ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1676 |
[index <= len] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1677 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1678 |
index := index + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1679 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1680 |
lastDigit := 0. |
701 | 1681 |
]. |
1682 |
||
1794 | 1683 |
lastDigit == 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1684 |
^ result compressed |
1794 | 1685 |
]. |
701 | 1686 |
^ result |
1687 |
||
1688 |
" |
|
1689 |
12345678900000000000 absFastMinus:1 |
|
1690 |
12345678900000000000 absFastMinus:1000000 |
|
1691 |
12345678900000000000 absFastMinus:255 |
|
1692 |
(SmallInteger maxVal + 1) absFastMinus:1 |
|
1693 |
(SmallInteger minVal - 1) absFastMinus:1 |
|
1694 |
" |
|
1794 | 1695 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1696 |
"Modified: 24.3.1997 / 21:33:25 / cg" |
701 | 1697 |
! |
1698 |
||
1699 |
absFastPlus:aSmallInteger |
|
1700 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
|
2952 | 1701 |
The result is normalized." |
701 | 1702 |
|
1794 | 1703 |
|result resultDigitByteArray lastDigit |
2952 | 1704 |
ok |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1705 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1706 |
rsltLen "{ Class: SmallInteger }" |
701 | 1707 |
index "{ Class: SmallInteger }" |
1708 |
carry "{ Class: SmallInteger }" | |
|
1709 |
||
1710 |
len := digitByteArray size. |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1711 |
rsltLen := len + 1. |
701 | 1712 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1713 |
result := self class basicNew numberOfDigits:rsltLen. |
701 | 1714 |
resultDigitByteArray := result digits. |
1715 |
||
2952 | 1716 |
%{ |
1717 |
if (__isByteArray(__INST(digitByteArray)) |
|
1718 |
&& __isByteArray(resultDigitByteArray) |
|
1719 |
&& __isSmallInteger(aSmallInteger)) { |
|
1720 |
int __carry = __intVal(aSmallInteger); |
|
1721 |
int __index = 1; |
|
1722 |
int __len = __intVal(len); |
|
1723 |
unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element); |
|
1724 |
unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element); |
|
1725 |
int __lastDigit = 0; |
|
1726 |
||
1727 |
if (__carry < 0) { |
|
1728 |
__carry = -__carry; |
|
1729 |
} |
|
701 | 1730 |
|
2952 | 1731 |
while (__carry) { |
1732 |
if (__index <= __len) { |
|
1733 |
__carry += __src[__index-1]; |
|
1734 |
} |
|
1735 |
__dst[__index-1] = __lastDigit = __carry & 0xFF; |
|
1736 |
__carry >>= 8; |
|
1737 |
__index++; |
|
1738 |
} |
|
1739 |
if (__index <= __intVal(rsltLen)) { |
|
1740 |
while (__index <= __len) { |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1741 |
__dst[__index-1] = __src[__index-1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1742 |
__index++; |
2952 | 1743 |
} |
1744 |
__lastDigit = 0; |
|
1745 |
} |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1746 |
if (lastDigit) { |
2952 | 1747 |
RETURN (result); |
1748 |
} |
|
1749 |
ok = true; |
|
1750 |
} |
|
1751 |
%}. |
|
1752 |
||
1753 |
ok ~~ true ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1754 |
index := 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1755 |
carry := aSmallInteger abs. |
2952 | 1756 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1757 |
[carry ~~ 0] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1758 |
(index <= len) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1759 |
carry := (digitByteArray basicAt:index) + carry. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1760 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1761 |
resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1762 |
carry := carry bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1763 |
index := index + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1764 |
]. |
2952 | 1765 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1766 |
(index <= rsltLen) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1767 |
[index <= len] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1768 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1769 |
index := index + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1770 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1771 |
lastDigit := 0. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1772 |
]. |
2952 | 1773 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1774 |
lastDigit ~~ 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1775 |
^ result |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1776 |
]. |
701 | 1777 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1778 |
|
2952 | 1779 |
^ result compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1780 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1781 |
"Modified: 24.3.1997 / 21:32:41 / cg" |
701 | 1782 |
! |
1783 |
||
1784 |
absLess:aLargeInteger |
|
1890 | 1785 |
"return true, if abs(self) < abs(theArgument). |
1786 |
This handles unnormalized largeIntegers." |
|
701 | 1787 |
|
1890 | 1788 |
|myLen "{ Class: SmallInteger }" |
1789 |
otherLen "{ Class: SmallInteger }" |
|
701 | 1790 |
d1 "{ Class: SmallInteger }" |
1791 |
d2 "{ Class: SmallInteger }" |
|
1792 |
otherDigitByteArray | |
|
1793 |
||
1890 | 1794 |
myLen := digitByteArray size. |
701 | 1795 |
otherDigitByteArray := aLargeInteger digits. |
1890 | 1796 |
otherLen := otherDigitByteArray size. |
701 | 1797 |
|
1890 | 1798 |
[(digitByteArray basicAt:myLen) == 0] whileTrue:[ |
2001 | 1799 |
myLen := myLen - 1 |
701 | 1800 |
]. |
1890 | 1801 |
[(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[ |
2001 | 1802 |
otherLen := otherLen - 1 |
701 | 1803 |
]. |
1890 | 1804 |
(myLen < otherLen) ifTrue:[^ true]. |
1805 |
(myLen > otherLen) ifTrue:[^ false]. |
|
701 | 1806 |
|
1890 | 1807 |
[myLen > 0] whileTrue:[ |
2001 | 1808 |
d1 := digitByteArray basicAt:myLen. |
1809 |
d2 := otherDigitByteArray basicAt:myLen. |
|
1810 |
d1 == d2 ifFalse:[ |
|
1811 |
(d1 < d2) ifTrue:[^ true]. |
|
1812 |
^ false |
|
1813 |
]. |
|
1814 |
myLen := myLen - 1 |
|
701 | 1815 |
]. |
1816 |
^ false |
|
1890 | 1817 |
|
1818 |
"Modified: 5.11.1996 / 18:37:27 / cg" |
|
701 | 1819 |
! |
1820 |
||
1821 |
absMinus:aLargeInteger |
|
1822 |
"return a LargeInteger representing abs(self) - abs(theArgument)" |
|
1823 |
||
1824 |
|result done |
|
1825 |
otherDigitByteArray resultDigitByteArray |
|
1826 |
len1 "{ Class: SmallInteger }" |
|
1827 |
len2 "{ Class: SmallInteger }" |
|
1828 |
index "{ Class: SmallInteger }" |
|
1829 |
borrow "{ Class: SmallInteger }" |
|
1830 |
diff "{ Class: SmallInteger }" |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1831 |
carry "{ Class: SmallInteger }" |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1832 |
lResult| |
701 | 1833 |
|
1834 |
len1 := digitByteArray size. |
|
1835 |
otherDigitByteArray := aLargeInteger digits. |
|
1836 |
len2 := otherDigitByteArray size. |
|
1837 |
||
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1838 |
lResult := (len1 max: len2) + 1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1839 |
result := self class basicNew numberOfDigits:lResult. |
701 | 1840 |
resultDigitByteArray := result digits. |
1841 |
||
1842 |
index := 1. |
|
1843 |
borrow := 0. |
|
1844 |
||
1845 |
done := false. |
|
1846 |
[done] whileFalse:[ |
|
2001 | 1847 |
diff := borrow. |
1848 |
(index <= len1) ifTrue:[ |
|
1849 |
diff := diff + (digitByteArray basicAt:index). |
|
1850 |
(index <= len2) ifTrue:[ |
|
1851 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1852 |
] |
|
1853 |
] ifFalse:[ |
|
1854 |
(index <= len2) ifTrue:[ |
|
1855 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1856 |
] ifFalse:[ |
|
1857 |
"end reached" |
|
1858 |
done := true |
|
1859 |
] |
|
1860 |
]. |
|
701 | 1861 |
|
2001 | 1862 |
"/ workaround for |
1863 |
"/ gcc code generator bug |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1864 |
|
2001 | 1865 |
(diff >= 0) ifTrue:[ |
1866 |
borrow := 0 |
|
1867 |
] ifFalse:[ |
|
1868 |
borrow := -1. |
|
1869 |
diff := diff + 16r100 |
|
1870 |
]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1871 |
|
701 | 1872 |
"/ (diff < 0) ifTrue:[ |
1873 |
"/ borrow := -1. |
|
1874 |
"/ diff := diff + 16r100 |
|
1875 |
"/ ] ifFalse:[ |
|
1876 |
"/ borrow := 0 |
|
1877 |
"/ ]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1878 |
|
2001 | 1879 |
resultDigitByteArray basicAt:index put:diff. |
1880 |
index := index + 1 |
|
701 | 1881 |
]. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1882 |
|
701 | 1883 |
(borrow ~~ 0) ifTrue:[ |
2001 | 1884 |
"/ must generate 255's complement |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1885 |
|
2001 | 1886 |
result sign:-1. |
1887 |
[index <= lResult] whileTrue:[ |
|
1888 |
resultDigitByteArray basicAt:index put:16rFF. |
|
1889 |
index := index + 1. |
|
1890 |
]. |
|
1891 |
index := resultDigitByteArray size. |
|
1892 |
[index > 0] whileTrue:[ |
|
1893 |
resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)). |
|
1894 |
index := index -1. |
|
1895 |
]. |
|
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1896 |
|
2001 | 1897 |
index := 1. |
1898 |
carry := 1. |
|
1899 |
[carry ~~ 0] whileTrue:[ |
|
1900 |
(index <= lResult) ifTrue:[ |
|
1901 |
carry := (resultDigitByteArray basicAt:index) + carry. |
|
1902 |
]. |
|
1903 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
|
1904 |
carry := carry bitShift:-8. |
|
1905 |
index := index + 1 |
|
1906 |
]. |
|
701 | 1907 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1908 |
^ result compressed |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1909 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1910 |
"Modified: 5.11.1996 / 14:09:25 / cg" |
701 | 1911 |
! |
1912 |
||
68 | 1913 |
absMul:aLargeInteger |
1914 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
1915 |
||
92 | 1916 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 1917 |
idx "{ Class: SmallInteger }" |
1918 |
carry "{ Class: SmallInteger }" |
|
1919 |
len1 "{ Class: SmallInteger }" |
|
1920 |
len2 "{ Class: SmallInteger }" |
|
1921 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 1922 |
prod "{ Class: SmallInteger }" |
1923 |
v "{ Class: SmallInteger }"| |
|
68 | 1924 |
|
1925 |
len1 := digitByteArray size. |
|
1926 |
otherDigitByteArray := aLargeInteger digits. |
|
1927 |
len2 := otherDigitByteArray size. |
|
1928 |
||
1929 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1). |
|
1930 |
resultDigitByteArray := result digits. |
|
92 | 1931 |
ok := false. |
1932 |
%{ |
|
1133 | 1933 |
if (__isByteArray(__INST(digitByteArray)) |
92 | 1934 |
&& __isByteArray(otherDigitByteArray) |
1935 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 1936 |
&& __bothSmallInteger(len1, len2)) { |
2001 | 1937 |
unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
1938 |
unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
1939 |
unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
1940 |
int _index1, _index2, _dstIndex, _idx; |
|
2786 | 1941 |
unsigned INT _prod, _carry, _v; |
68 | 1942 |
|
2001 | 1943 |
for (_index1 = 0; _index1 < __intVal(len1); _index1++) { |
1944 |
for (_index2 = 0; _index2 < __intVal(len2); _index2++) { |
|
1945 |
_dstIndex = _index1 + _index2; |
|
1946 |
_prod = myBytes[_index1] * otherBytes[_index2]; |
|
1947 |
_prod += resultBytes[_dstIndex]; |
|
1948 |
resultBytes[_dstIndex] = _prod & 0xFF; |
|
1949 |
_carry = _prod >> 8; |
|
1950 |
if (_carry) { |
|
1951 |
_idx = _dstIndex + 1; |
|
1952 |
while (_carry) { |
|
1953 |
_v = resultBytes[_idx] + _carry; |
|
1954 |
resultBytes[_idx] = _v & 0xFF; |
|
1955 |
_carry = _v >> 8; |
|
1956 |
_idx = _idx + 1; |
|
1957 |
} |
|
1958 |
} |
|
1959 |
} |
|
1960 |
} |
|
1961 |
ok = true; |
|
92 | 1962 |
} |
1963 |
%}. |
|
1964 |
ok ifFalse:[ |
|
2001 | 1965 |
1 to:len1 do:[:index1 | |
1966 |
1 to:len2 do:[:index2 | |
|
1967 |
dstIndex := index1 + index2 - 1. |
|
1968 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
1969 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
1970 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
1971 |
carry := prod bitShift:-8. |
|
1972 |
carry ~~ 0 ifTrue:[ |
|
1973 |
idx := dstIndex + 1. |
|
1974 |
[carry ~~ 0] whileTrue:[ |
|
1975 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
1976 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
1977 |
carry := v bitShift:-8. |
|
1978 |
idx := idx + 1 |
|
1979 |
] |
|
1980 |
] |
|
1981 |
] |
|
1982 |
]. |
|
68 | 1983 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1984 |
^ result compressed |
68 | 1985 |
! |
1986 |
||
1987 |
absPlus:aLargeInteger |
|
1988 |
"return a LargeInteger representing abs(self) + abs(theArgument)" |
|
1989 |
||
1990 |
|result done otherDigitByteArray resultDigitByteArray |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1991 |
len1 "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1992 |
len2 "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1993 |
newLen "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1994 |
index "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1995 |
carry "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1996 |
sum "{ Class: SmallInteger }" | |
68 | 1997 |
|
1998 |
otherDigitByteArray := aLargeInteger digits. |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
1999 |
|
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2000 |
%{ |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2001 |
OBJ _digitByteArray = __INST(digitByteArray); |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2002 |
|
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2003 |
if (__isByteArray(_digitByteArray) |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2004 |
&& __isByteArray(otherDigitByteArray)) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2005 |
int _len1, _len2, _newLen; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2006 |
unsigned char *_myDigits, *_otherDigits, *_newDigits; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2007 |
int _index, _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2008 |
int _comLen; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2009 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2010 |
_len1 = __byteArraySize(_digitByteArray); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2011 |
_len2 = __byteArraySize(otherDigitByteArray); |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2012 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2013 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2014 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
68 | 2015 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2016 |
if (_len1 < _len2) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2017 |
_comLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2018 |
_newLen = _len2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2019 |
if (_otherDigits[_len2 - 1] == 0xFF) _newLen++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2020 |
} else if (_len2 < _len1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2021 |
_comLen = _len2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2022 |
_newLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2023 |
if (_myDigits[_len1 - 1] == 0xFF) _newLen++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2024 |
} else { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2025 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2026 |
* there can only be an overflow from the high bytes, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2027 |
* if their sum is >= 255 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2028 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2029 |
_newLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2030 |
if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2031 |
_newLen++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2032 |
} else { |
2850 | 2033 |
#if defined(i386) || defined(alpha) /* actually: LSB_FIRST */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2034 |
if (_newLen == sizeof(INT)) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2035 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2036 |
* two 32bit numbers, no carry - a very common case ... |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2037 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2038 |
unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2039 |
RETURN (__MKULARGEINT(_sum)); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2040 |
} |
2849 | 2041 |
#endif /* not LSB_FIRST */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2042 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2043 |
_comLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2044 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2045 |
resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen); |
68 | 2046 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2047 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2048 |
* must refetch - GC could have been invoked |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2049 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2050 |
_digitByteArray = __INST(digitByteArray); |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2051 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2052 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2053 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2054 |
_newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2055 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2056 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2057 |
* add them ... |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2058 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2059 |
_index = 1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2060 |
_carry = 0; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2061 |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2062 |
#if defined(i386) || defined(alpha) /* actually: LSB first */ |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2063 |
# ifdef INT64 |
2923 | 2064 |
/* |
2065 |
* have a 64bit integer type; |
|
2066 |
* add long-wise |
|
2067 |
*/ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2068 |
while (_index < _comLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2069 |
UINT64 _sum; |
2923 | 2070 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2071 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2072 |
_sum += *(unsigned *)(&(_myDigits[_index - 1])); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2073 |
_sum += *(unsigned *)(&(_otherDigits[_index - 1])); |
2952 | 2074 |
_carry = _sum >> 32; |
2923 | 2075 |
_sum = _sum & 0xFFFFFFFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2076 |
*(unsigned *)(&(_newDigits[_index - 1])) = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2077 |
_index += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2078 |
} |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2079 |
# endif |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2080 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2081 |
* add short-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2082 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2083 |
while (_index < _comLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2084 |
unsigned int _sum; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2085 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2086 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2087 |
_sum += *(unsigned short *)(&(_myDigits[_index - 1])); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2088 |
_sum += *(unsigned short *)(&(_otherDigits[_index - 1])); |
2952 | 2089 |
_carry = _sum >> 16; |
2923 | 2090 |
_sum = _sum & 0xFFFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2091 |
*(unsigned short *)(&(_newDigits[_index - 1])) = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2092 |
_index += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2093 |
} |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2094 |
#endif |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2095 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2096 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2097 |
* add byte |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2098 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2099 |
while (_index <= _comLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2100 |
unsigned int _sum; |
68 | 2101 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2102 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2103 |
_sum += _myDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2104 |
_sum += _otherDigits[_index - 1]; |
2952 | 2105 |
_carry = _sum >> 8; |
2923 | 2106 |
_sum = _sum & 0xFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2107 |
_newDigits[_index - 1] = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2108 |
_index++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2109 |
} |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2110 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2111 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2112 |
* rest |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2113 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2114 |
while (_index <= _newLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2115 |
unsigned int _sum; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2116 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2117 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2118 |
if (_index <= _len1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2119 |
_sum += _myDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2120 |
if (_index <= _len2) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2121 |
_sum += _otherDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2122 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2123 |
} else { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2124 |
if (_index <= _len2) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2125 |
_sum += _otherDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2126 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2127 |
} |
2952 | 2128 |
_carry = _sum >> 8; |
2923 | 2129 |
_sum = _sum & 0xFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2130 |
_newDigits[_index - 1] = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2131 |
_index++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2132 |
} |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2133 |
} |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2134 |
%}. |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2135 |
resultDigitByteArray notNil ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2136 |
result := self class basicNew. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2137 |
result setDigits:resultDigitByteArray. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2138 |
] ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2139 |
len1 := digitByteArray size. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2140 |
len2 := otherDigitByteArray size. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2141 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2142 |
"/ earlier versions estimated the newLength as: |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2143 |
"/ (len1 max:len2) + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2144 |
"/ and reduced the result. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2145 |
"/ however, if one of the addends is smaller, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2146 |
"/ the result will never require another digit, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2147 |
"/ if the highest digit of the larger addent is |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2148 |
"/ not equal to 255. Therefore, in most cases, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2149 |
"/ we can avoid the computation and resizing |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2150 |
"/ in #reduced. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2151 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2152 |
len1 < len2 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2153 |
newLen := len2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2154 |
(otherDigitByteArray at:len2) == 16rFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2155 |
newLen := newLen + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2156 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2157 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2158 |
len2 < len1 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2159 |
newLen := len1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2160 |
(digitByteArray at:len1) == 16rFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2161 |
newLen := newLen + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2162 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2163 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2164 |
newLen := len1 + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2165 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2166 |
]. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2167 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2168 |
result := self class basicNew numberOfDigits:newLen. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2169 |
resultDigitByteArray := result digits. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2170 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2171 |
index := 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2172 |
carry := 0. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2173 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2174 |
done := false. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2175 |
[done] whileFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2176 |
sum := carry. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2177 |
(index <= len1) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2178 |
sum := sum + (digitByteArray basicAt:index). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2179 |
(index <= len2) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2180 |
sum := sum + (otherDigitByteArray basicAt:index) |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2181 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2182 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2183 |
(index <= len2) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2184 |
sum := sum + (otherDigitByteArray basicAt:index) |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2185 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2186 |
"end reached" |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2187 |
done := true |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2188 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2189 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2190 |
(sum >= 16r100) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2191 |
carry := 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2192 |
sum := sum - 16r100 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2193 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2194 |
carry := 0 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2195 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2196 |
resultDigitByteArray basicAt:index put:sum. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2197 |
index := index + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2198 |
]. |
68 | 2199 |
]. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2200 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2201 |
^ result compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2202 |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2203 |
"Modified: 11.8.1997 / 03:23:37 / cg" |
68 | 2204 |
! |
2205 |
||
1890 | 2206 |
absSubtract:aLargeInteger |
2207 |
"destructively subtract aLargeInteger from myself. |
|
2208 |
A helper for division; only allowed for positive receiver |
|
2209 |
and argument. The receiver must be >= the argument. |
|
2210 |
The receiver must be a temporary scratch-number" |
|
2211 |
||
2521 | 2212 |
|done |
2213 |
otherDigitByteArray |
|
1890 | 2214 |
len1 "{ Class: SmallInteger }" |
2215 |
len2 "{ Class: SmallInteger }" |
|
2216 |
index "{ Class: SmallInteger }" |
|
2217 |
borrow "{ Class: SmallInteger }" |
|
2218 |
diff "{ Class: SmallInteger }" |
|
2521 | 2219 |
nonZero| |
1890 | 2220 |
|
2221 |
len1 := digitByteArray size. |
|
2222 |
otherDigitByteArray := aLargeInteger digits. |
|
2223 |
len2 := otherDigitByteArray size. |
|
2224 |
len2 > len1 ifTrue:[ |
|
2001 | 2225 |
[(otherDigitByteArray at:len2) == 0] whileTrue:[ |
2226 |
len2 := len2 - 1 |
|
2227 |
]. |
|
2228 |
len2 > len1 ifTrue:[ |
|
2229 |
self halt "/ may not be called that way |
|
2230 |
]. |
|
1890 | 2231 |
]. |
2232 |
||
2233 |
nonZero := false. |
|
2234 |
index := 1. |
|
2235 |
borrow := 0. |
|
2236 |
||
2237 |
done := false. |
|
2238 |
[index <= len1] whileTrue:[ |
|
2001 | 2239 |
diff := borrow. |
2240 |
diff := diff + (digitByteArray basicAt:index). |
|
2241 |
index <= len2 ifTrue:[ |
|
2242 |
diff := diff - (otherDigitByteArray basicAt:index). |
|
2243 |
]. |
|
1890 | 2244 |
|
2001 | 2245 |
"/ workaround for |
2246 |
"/ gcc code generator bug |
|
1890 | 2247 |
|
2001 | 2248 |
(diff >= 0) ifTrue:[ |
2249 |
borrow := 0 |
|
2250 |
] ifFalse:[ |
|
2251 |
borrow := -1. |
|
2252 |
diff := diff + 16r100 |
|
2253 |
]. |
|
2254 |
diff ~~ 0 ifTrue:[ |
|
2255 |
nonZero := true |
|
2256 |
]. |
|
2257 |
digitByteArray basicAt:index put:diff. |
|
2258 |
index := index + 1 |
|
1890 | 2259 |
]. |
2260 |
^ nonZero |
|
2261 |
||
2262 |
"Created: 5.11.1996 / 16:23:47 / cg" |
|
2263 |
"Modified: 5.11.1996 / 18:56:50 / cg" |
|
2264 |
! |
|
2265 |
||
2266 |
div2 |
|
2267 |
"destructively divide the receiver by 2. |
|
2268 |
private - used for division only. |
|
2269 |
This is worth a primitive." |
|
2270 |
||
2271 |
%{ /* NOCONTEXT */ |
|
2272 |
OBJ __digits = __INST(digitByteArray); |
|
2273 |
||
2274 |
if (__isByteArray(__digits)) { |
|
2001 | 2275 |
int __nBytes = __byteArraySize(__digits); |
2276 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2277 |
unsigned int __this, __next; |
2001 | 2278 |
int __idx; |
1890 | 2279 |
|
2001 | 2280 |
if (__nBytes == 1) { |
2281 |
__bp[0] >>= 1; |
|
2282 |
RETURN (self); |
|
2283 |
} |
|
1890 | 2284 |
|
2001 | 2285 |
__idx = 1; |
1890 | 2286 |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2287 |
#if defined(i386) /* XXX actually: LSB_FIRST */ |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2288 |
if (sizeof(unsigned int) == 4) { |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2289 |
if ((__idx + 4) < __nBytes) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2290 |
__this = ((unsigned int *)__bp)[0]; |
68 | 2291 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2292 |
while ((__idx + 4) < __nBytes) { |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2293 |
__next = ((unsigned int *)__bp)[1]; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2294 |
__this = (__this >> 1) /* & 0x7FFFFFF */; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2295 |
__this |= __next << 31; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2296 |
((unsigned int *)__bp)[0] = __this; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2297 |
__this = __next; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2298 |
__bp += 4; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2299 |
__idx += 4; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2300 |
} |
2001 | 2301 |
} |
2302 |
} |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2303 |
#endif |
1890 | 2304 |
|
2001 | 2305 |
__this = __bp[0]; |
2306 |
while (__idx < __nBytes) { |
|
2307 |
__next = __bp[1]; |
|
2308 |
__this >>= 1; |
|
2309 |
__this |= __next << 7; |
|
2310 |
__bp[0] = __this; |
|
2311 |
__this = __next; |
|
2312 |
__bp++; |
|
2313 |
__idx++; |
|
2314 |
} |
|
2315 |
__bp[0] = __this >> 1; |
|
2316 |
RETURN (self); |
|
1890 | 2317 |
} |
2318 |
%}. |
|
2319 |
self primitiveFailed |
|
2320 |
||
2321 |
" |
|
2322 |
100000 asLargeInteger div2 |
|
2323 |
" |
|
2324 |
||
2325 |
"Modified: 5.11.1996 / 16:12:56 / cg" |
|
2326 |
! |
|
2327 |
||
2328 |
mul2 |
|
2329 |
"destructively multiply the receiver by 2. |
|
2330 |
private - used for division only. |
|
2331 |
This is worth a primitive." |
|
2332 |
||
2333 |
|nBytes "{ Class: SmallInteger }" |
|
2334 |
b "{ Class: SmallInteger }" |
|
2335 |
t| |
|
2336 |
||
2337 |
nBytes := digitByteArray size. |
|
2338 |
||
2339 |
b := digitByteArray at:nBytes. |
|
2340 |
(b bitAnd:16r80) ~~ 0 ifTrue:[ |
|
2001 | 2341 |
"/ need another byte |
2342 |
nBytes := nBytes + 1. |
|
2343 |
t := ByteArray uninitializedNew:nBytes. |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2344 |
t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1. |
2001 | 2345 |
t at:nBytes put:0. |
2346 |
digitByteArray := t. |
|
1890 | 2347 |
]. |
2348 |
||
2349 |
%{ |
|
2350 |
OBJ __digits = __INST(digitByteArray); |
|
2351 |
||
2352 |
if (__isByteArray(__digits)) { |
|
2001 | 2353 |
int __nBytes = __intVal(nBytes); |
2354 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2355 |
unsigned int __carry = 0, __newCarry, __this; |
2001 | 2356 |
int __idx; |
1890 | 2357 |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2358 |
#if defined(i386) /* XXX actually: LSB_FIRST */ |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2359 |
if (sizeof(unsigned int) == 4) { |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2360 |
while (__nBytes >= 4) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2361 |
__this = ((unsigned int *)__bp)[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2362 |
__newCarry = (__this >> 31) /* & 1 */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2363 |
((unsigned int *)__bp)[0] = (__this << 1) | __carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2364 |
__carry = __newCarry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2365 |
__bp += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2366 |
__nBytes -= 4; |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2367 |
} |
2001 | 2368 |
} |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2369 |
#endif |
2001 | 2370 |
while (__nBytes) { |
2371 |
__this = __bp[0]; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2372 |
__newCarry = (__this >> 7) /* & 1 */; |
2001 | 2373 |
__bp[0] = (__this << 1) | __carry; |
2374 |
__carry = __newCarry; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2375 |
__bp++; |
2001 | 2376 |
__nBytes--; |
2377 |
} |
|
2378 |
RETURN (self); |
|
1890 | 2379 |
} |
2380 |
%}. |
|
2381 |
self primitiveFailed |
|
2382 |
||
2383 |
" |
|
2384 |
100000 asLargeInteger mul2 |
|
2385 |
" |
|
2386 |
||
2387 |
"Modified: 5.11.1996 / 16:37:32 / cg" |
|
68 | 2388 |
! |
2389 |
||
2390 |
mul256 |
|
2391 |
"destructively multiply the receiver by 256. |
|
2392 |
private - used for division only" |
|
2393 |
||
2394 |
|newDigits| |
|
2395 |
||
359 | 2396 |
newDigits := ByteArray uninitializedNew:(digitByteArray size + 1). |
68 | 2397 |
newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
2398 |
newDigits at:1 put:0. |
|
2399 |
digitByteArray := newDigits |
|
701 | 2400 |
! |
68 | 2401 |
|
701 | 2402 |
numberOfDigits:nDigits |
2001 | 2403 |
"/ digitByteArray := ByteArray uninitializedNew:nDigits. |
701 | 2404 |
digitByteArray := ByteArray new:nDigits. |
2405 |
sign := 1. |
|
68 | 2406 |
! |
2407 |
||
701 | 2408 |
setDigits:digits |
2409 |
digitByteArray := digits. |
|
2410 |
sign := 1. |
|
68 | 2411 |
! |
2412 |
||
701 | 2413 |
sign:aNumber |
2414 |
sign := aNumber |
|
1 | 2415 |
! ! |
2416 |
||
2417 |
!LargeInteger methodsFor:'testing'! |
|
2418 |
||
701 | 2419 |
even |
2420 |
"return true if the receiver is even" |
|
1 | 2421 |
|
701 | 2422 |
^ (digitByteArray at:1) even |
1 | 2423 |
! |
2424 |
||
68 | 2425 |
negative |
2426 |
"return true, if the receiver is < 0" |
|
2427 |
||
2428 |
^ (sign < 0) |
|
2429 |
! |
|
2430 |
||
1 | 2431 |
odd |
2432 |
"return true if the receiver is odd" |
|
2433 |
||
1983 | 2434 |
^ (digitByteArray at:1) odd |
2435 |
||
2436 |
"Modified: 18.11.1996 / 22:19:19 / cg" |
|
1 | 2437 |
! |
2438 |
||
2439 |
positive |
|
2440 |
"return true, if the receiver is >= 0" |
|
2441 |
||
2442 |
^ (sign >= 0) |
|
2443 |
! |
|
2444 |
||
701 | 2445 |
sign |
2446 |
"return the sign of the receiver" |
|
2447 |
||
2448 |
^ sign |
|
2449 |
! |
|
2450 |
||
1 | 2451 |
strictlyPositive |
2452 |
"return true, if the receiver is > 0" |
|
2453 |
||
2454 |
^ (sign > 0) |
|
2455 |
! ! |
|
2456 |
||
1794 | 2457 |
!LargeInteger class methodsFor:'documentation'! |
1 | 2458 |
|
701 | 2459 |
version |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2460 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.62 1998-01-12 13:18:47 cg Exp $' |
1 | 2461 |
! ! |