author | Claus Gittinger <cg@exept.de> |
Fri, 18 Apr 1997 21:05:22 +0200 | |
changeset 2568 | 9b7a3a08b8ca |
parent 2521 | 43b9d4fafb35 |
child 2786 | 8f05ce0a14e1 |
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 |
" |
70 |
! ! |
|
1 | 71 |
|
1794 | 72 |
!LargeInteger class methodsFor:'instance creation'! |
1 | 73 |
|
74 |
new |
|
75 |
"catch creation message" |
|
76 |
||
77 |
self error:'LargeIntegers cannot be created with new' |
|
78 |
! |
|
79 |
||
701 | 80 |
new:numberOfDigits |
81 |
"catch creation message" |
|
2 | 82 |
|
701 | 83 |
self error:'LargeIntegers cannot be created with new' |
1 | 84 |
! |
85 |
||
701 | 86 |
value:aSmallInteger |
87 |
"create and return a new LargeInteger with value taken from |
|
88 |
the argument, aSmallInteger. |
|
89 |
Notice: this should be only used internally, since such small |
|
90 |
largeIntegers do not normally occur in the system. |
|
91 |
May change without notice." |
|
92 |
||
93 |
^ self basicNew value:aSmallInteger |
|
94 |
||
95 |
"LargeInteger value:3689" |
|
96 |
! ! |
|
97 |
||
1794 | 98 |
!LargeInteger class methodsFor:'queries'! |
701 | 99 |
|
100 |
isBuiltInClass |
|
1264 | 101 |
"return true if this class is known by the run-time-system. |
102 |
Here, true is returned." |
|
701 | 103 |
|
104 |
^ true |
|
1264 | 105 |
|
106 |
"Modified: 23.4.1996 / 15:59:21 / cg" |
|
1 | 107 |
! ! |
108 |
||
68 | 109 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 110 |
|
701 | 111 |
* aNumber |
112 |
"return the product of the receiver and the argument, aNumber" |
|
113 |
||
114 |
|otherSign| |
|
115 |
||
116 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
117 |
||
118 |
" |
|
119 |
this is the common case, multiplying with SmallInteger. |
|
120 |
Use a special method for this case ... |
|
121 |
" |
|
122 |
(aNumber class == SmallInteger) ifTrue:[ |
|
123 |
^ self productFromInteger:aNumber |
|
124 |
]. |
|
125 |
||
126 |
" |
|
127 |
if the argument is not a largeInteger, coerce |
|
128 |
" |
|
129 |
(aNumber class == self class) ifFalse:[ |
|
130 |
^ self retry:#* coercing:aNumber |
|
131 |
]. |
|
132 |
||
133 |
otherSign := aNumber sign. |
|
134 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
135 |
(otherSign == 0) ifTrue:[^ 0]. |
|
136 |
^ (self absMul:aNumber) sign:-1 |
|
137 |
! |
|
138 |
||
92 | 139 |
+ aNumber |
140 |
"return the sum of the receiver and the argument, aNumber" |
|
141 |
||
142 |
|otherSign| |
|
143 |
||
144 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
145 |
||
146 |
" |
|
147 |
this is the common case, adding a SmallInteger. |
|
148 |
Use a special method for this case ... |
|
149 |
" |
|
150 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 151 |
^ self sumFromInteger:aNumber |
92 | 152 |
]. |
153 |
||
154 |
" |
|
155 |
if the argument is not a largeInteger, coerce |
|
156 |
" |
|
157 |
(aNumber class == self class) ifFalse:[ |
|
345 | 158 |
^ self retry:#+ coercing:aNumber |
92 | 159 |
]. |
160 |
||
161 |
otherSign := aNumber sign. |
|
162 |
(sign > 0) ifTrue:[ |
|
345 | 163 |
"I am positive" |
164 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber]. |
|
165 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber]. |
|
166 |
^ self |
|
92 | 167 |
]. |
168 |
"I am negative" |
|
169 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self]. |
|
170 |
(otherSign < 0) ifTrue:[^ (self absPlus:aNumber) sign:-1]. |
|
171 |
^ self |
|
357 | 172 |
|
173 |
" |
|
174 |
SmallInteger maxVal |
|
175 |
SmallInteger maxVal + 1 |
|
176 |
SmallInteger maxVal + 2 |
|
177 |
SmallInteger minVal |
|
178 |
SmallInteger minVal - 1 |
|
179 |
" |
|
92 | 180 |
! |
181 |
||
182 |
- aNumber |
|
183 |
"return the difference of the receiver and the argument, aNumber" |
|
184 |
||
1794 | 185 |
|otherSign| |
92 | 186 |
|
187 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
188 |
||
189 |
" |
|
190 |
this is the common case, subtracting a SmallInteger. |
|
191 |
Use a special method for this case ... |
|
192 |
" |
|
193 |
(aNumber class == SmallInteger) ifTrue:[ |
|
2001 | 194 |
sign < 0 ifTrue:[ |
195 |
aNumber > 0 ifTrue:[ |
|
196 |
^ (self absFastPlus:aNumber) sign:-1 |
|
197 |
]. |
|
198 |
^ (self absFastMinus:aNumber) sign:-1 |
|
199 |
]. |
|
200 |
aNumber > 0 ifTrue:[ |
|
201 |
^ self absFastMinus:aNumber |
|
202 |
]. |
|
203 |
^ self absFastPlus:aNumber |
|
92 | 204 |
]. |
205 |
||
206 |
" |
|
207 |
if the argument is not a largeInteger, coerce |
|
208 |
" |
|
209 |
(aNumber class == self class) ifFalse:[ |
|
2001 | 210 |
^ self retry:#- coercing:aNumber |
92 | 211 |
]. |
212 |
||
213 |
otherSign := aNumber sign. |
|
214 |
(sign > 0) ifTrue:[ |
|
2001 | 215 |
"I am positive" |
216 |
(otherSign > 0) ifTrue:[^ self absMinus:aNumber]. |
|
217 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber]. |
|
218 |
^ self |
|
92 | 219 |
]. |
220 |
"I am negative" |
|
221 |
(otherSign > 0) ifTrue:[^ (self absPlus:aNumber) negated]. |
|
222 |
(otherSign < 0) ifTrue:[^ (self absMinus:aNumber) negated]. |
|
223 |
^ self |
|
224 |
||
225 |
" |
|
226 |
12345678901234567890 - 0 |
|
357 | 227 |
12345678901234567890 - 1 |
92 | 228 |
12345678901234567890 - -1 |
229 |
-12345678901234567890 - 1 |
|
230 |
-12345678901234567890 - -1 |
|
231 |
||
232 |
12345678901234567890 - 12345678901234567880 |
|
233 |
12345678901234567890 - 12345000000000000000 |
|
234 |
12345678901234567890 - -87654321098765432110 |
|
235 |
-12345678901234567890 - 87654321098765432110 |
|
236 |
-12345678901234567890 - -12345678901234567880 |
|
237 |
-12345678901234567890 - -12345678901234567980 |
|
238 |
" |
|
1794 | 239 |
|
240 |
"Modified: 20.10.1996 / 18:42:16 / cg" |
|
92 | 241 |
! |
242 |
||
701 | 243 |
/ aNumber |
1065 | 244 |
"return the quotient of the receiver and the argument, aNumber" |
92 | 245 |
|
701 | 246 |
aNumber isInteger ifTrue:[ |
247 |
^ (Fraction numerator:self |
|
248 |
denominator:aNumber) reduced |
|
92 | 249 |
]. |
250 |
||
701 | 251 |
"this is a q&d hack - we loose lots of precision here ..." |
252 |
^ (self asFloat / aNumber asFloat) |
|
92 | 253 |
! |
254 |
||
68 | 255 |
// aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
256 |
"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
|
257 |
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
|
258 |
if the operands signs differ." |
1 | 259 |
|
2521 | 260 |
|otherSign divMod quo abs "{ Class: SmallInteger }" n| |
1 | 261 |
|
262 |
otherSign := aNumber sign. |
|
263 |
||
92 | 264 |
" |
265 |
this is the common case, dividing by a SmallInteger. |
|
266 |
Use a special method for this case ... |
|
267 |
" |
|
68 | 268 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 269 |
abs := aNumber. |
270 |
abs := abs abs. |
|
271 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
272 |
divMod := self absFastDiv:abs. |
|
273 |
quo := divMod at:1. |
|
274 |
(sign == otherSign) ifTrue:[^ quo]. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
275 |
|
2001 | 276 |
"/ stupid adjust ... |
277 |
(divMod at:2) == 0 ifFalse:[ |
|
278 |
^ (quo sign:-1) - 1 |
|
279 |
]. |
|
280 |
^ quo sign:-1 |
|
281 |
]. |
|
282 |
n := aNumber asLargeInteger. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
283 |
] ifFalse:[ |
2001 | 284 |
n := aNumber |
1 | 285 |
]. |
68 | 286 |
|
92 | 287 |
" |
288 |
if the argument is not a largeInteger, coerce |
|
289 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
290 |
(n class == self class) ifFalse:[ |
2001 | 291 |
^ self retry:#// coercing:aNumber |
1 | 292 |
]. |
293 |
||
1890 | 294 |
divMod := self absDiv:n. |
295 |
||
296 |
(sign == otherSign) ifTrue:[^ divMod at:1]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
297 |
|
1890 | 298 |
"/ stupid adjust for truncation ... |
299 |
quo := divMod at:1. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
300 |
(divMod at:2) == 0 ifFalse:[ |
2001 | 301 |
^ (quo sign:-1) - 1 |
1 | 302 |
]. |
1890 | 303 |
^ quo sign:-1 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
304 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
305 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
306 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
307 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
308 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
309 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
310 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
311 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
312 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
313 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
314 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
315 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
316 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
317 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
318 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
319 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
320 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
321 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
322 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
323 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
324 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
325 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
326 |
|
1890 | 327 |
"Modified: 5.11.1996 / 16:39:36 / cg" |
68 | 328 |
! |
329 |
||
330 |
\\ aNumber |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
331 |
"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
|
332 |
The sign of the result is that of aNumber." |
68 | 333 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
334 |
|abs rem| |
68 | 335 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
336 |
abs := aNumber abs. |
92 | 337 |
|
338 |
" |
|
339 |
this is the common case, dividing by a SmallInteger. |
|
340 |
Use a special method for this case ... |
|
341 |
" |
|
68 | 342 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 343 |
(abs between:1 and:16r003fffff) ifTrue:[ |
344 |
rem := (self absFastDiv:abs) at:2. |
|
345 |
] ifFalse:[ |
|
346 |
rem := (self absDiv:abs asLargeInteger) at:2 |
|
347 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
348 |
] ifFalse:[ |
2001 | 349 |
" |
350 |
if the argument is not a largeInteger, coerce |
|
351 |
" |
|
352 |
(aNumber class == self class) ifFalse:[ |
|
353 |
^ self retry:#\\ coercing:aNumber |
|
354 |
]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
355 |
|
2001 | 356 |
rem := (self absDiv:abs) at:2. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
357 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
358 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
359 |
aNumber negative ifTrue:[ |
2001 | 360 |
^ rem sign:-1 |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
361 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
362 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
363 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
364 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
365 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
366 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
367 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
368 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
369 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
370 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
371 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
372 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
373 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
374 |
|
1890 | 375 |
9000000000 \\ 7 |
376 |
-9000000000 \\ 7 |
|
377 |
9000000000 \\ -7 |
|
378 |
-9000000000 \\ -7 |
|
379 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
380 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
381 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
382 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
383 |
-900 rem: -400 |
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 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
386 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
387 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
388 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
389 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
390 |
|
1890 | 391 |
"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
|
392 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
393 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
394 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
395 |
"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
|
396 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
397 |
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
|
398 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
399 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
400 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
401 |
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
|
402 |
Use a special method for this case ... |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
403 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
404 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 405 |
^ self absFastDiv:aNumber abs. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
406 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
407 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
408 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
409 |
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
|
410 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
411 |
(aNumber class == self class) ifTrue:[ |
2001 | 412 |
^ self absDiv:aNumber abs |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
413 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
414 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
415 |
^ super divMod:aNumber |
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 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
418 |
9000000000 // 4000000000 => 2 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
419 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
420 |
9000000000 \\ 4000000000 => 1000000000 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
421 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
422 |
9000000000 divMod: 4000000000 => #(2 1000000000) |
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 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
425 |
"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
|
426 |
"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
|
427 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
428 |
|
1210 | 429 |
negated |
430 |
"return an integer with value negated from the receivers value." |
|
431 |
||
432 |
|newNumber| |
|
433 |
||
434 |
(sign == 0) ifTrue:[^ 0]. |
|
435 |
||
436 |
" |
|
437 |
special case for SmallInteger minVal |
|
438 |
" |
|
439 |
sign == 1 ifTrue:[ |
|
440 |
digitByteArray size == 4 ifTrue:[ |
|
441 |
(digitByteArray at:1) == 0 ifTrue:[ |
|
442 |
(digitByteArray at:2) == 0 ifTrue:[ |
|
443 |
(digitByteArray at:3) == 0 ifTrue:[ |
|
444 |
(digitByteArray at:4) == 16r40 ifTrue:[ |
|
445 |
^ SmallInteger minVal |
|
446 |
]. |
|
447 |
] |
|
448 |
] |
|
449 |
] |
|
450 |
] |
|
451 |
]. |
|
452 |
newNumber := self shallowCopy. |
|
453 |
newNumber sign:(sign negated). |
|
454 |
^ newNumber |
|
455 |
! |
|
456 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
457 |
quo:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
458 |
"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
|
459 |
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
|
460 |
truncates toward negative infinity). |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
461 |
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
|
462 |
different from the args sign" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
463 |
|
2521 | 464 |
|otherSign quo abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
465 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
466 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
467 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
468 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
469 |
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
|
470 |
Use a special method for this case ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
471 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
472 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 473 |
abs := aNumber. |
474 |
abs := abs abs. |
|
475 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
476 |
quo := (self absFastDiv:abs) at:1. |
|
477 |
(sign == otherSign) ifTrue:[^ quo]. |
|
478 |
^ quo sign:-1 |
|
479 |
] |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
480 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
481 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
482 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
483 |
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
|
484 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
485 |
(aNumber class == self class) ifFalse:[ |
2001 | 486 |
^ self retry:#quo: coercing:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
487 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
488 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
489 |
sign < 0 ifTrue:[ |
2001 | 490 |
(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
|
491 |
] ifFalse:[ |
2001 | 492 |
(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
|
493 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
494 |
^ ((self absDiv:aNumber) at:1) sign:-1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
495 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
496 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
497 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
498 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
499 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
500 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
501 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
502 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
503 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
504 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
505 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
506 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
507 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
508 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
509 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
510 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
511 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
512 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
513 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
514 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
515 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
516 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
517 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
518 |
"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
|
519 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
520 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
521 |
rem:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
522 |
"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
|
523 |
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
|
524 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
525 |
|rem abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
526 |
|
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 |
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
|
529 |
Use special code for this case ... |
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 |
(aNumber class == SmallInteger) ifTrue:[ |
2001 | 532 |
abs := aNumber. |
533 |
abs := abs abs. |
|
534 |
(abs between:1 and:16r003fffff) ifTrue:[ |
|
535 |
rem := (self absFastDiv:abs) at:2. |
|
536 |
] ifFalse:[ |
|
537 |
rem := (self absDiv:(abs asLargeInteger)) at:2 |
|
538 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
539 |
] ifFalse:[ |
2001 | 540 |
" |
541 |
if the argument is not a largeInteger, coerce |
|
542 |
" |
|
543 |
(aNumber class == self class) ifFalse:[ |
|
544 |
^ self retry:#\\ coercing:aNumber |
|
545 |
]. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
546 |
|
2001 | 547 |
rem := (self absDiv:aNumber) at:2 |
1 | 548 |
]. |
92 | 549 |
|
550 |
sign < 0 ifTrue:[ |
|
2001 | 551 |
^ rem sign:-1 |
1 | 552 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
553 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
554 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
555 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
556 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
557 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
558 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
559 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
560 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
561 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
562 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
563 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
564 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
565 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
566 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
567 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
568 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
569 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
570 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
571 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
572 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
573 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
574 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
575 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
576 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
577 |
"Modified: 5.11.1996 / 14:02:59 / cg" |
1 | 578 |
! ! |
579 |
||
701 | 580 |
!LargeInteger methodsFor:'byte access'! |
581 |
||
582 |
digitAt:index |
|
583 |
"return 8 bits of value, starting at byte index" |
|
584 |
||
585 |
index > digitByteArray size ifTrue:[^ 0]. |
|
586 |
^ digitByteArray at:index |
|
587 |
! |
|
588 |
||
589 |
digitAt:index put:aByte |
|
590 |
"set the 8 bits, index is a byte index" |
|
591 |
||
592 |
digitByteArray at:index put:aByte |
|
593 |
! |
|
594 |
||
595 |
digitLength |
|
596 |
"return the number bytes used by this Integer" |
|
597 |
||
598 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
599 |
check if there is a 0-byte ... |
701 | 600 |
this allows to ask unnormalized LargeIntegers |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
601 |
for their digitLength |
701 | 602 |
" |
603 |
|l "{ Class: SmallInteger }" | |
|
92 | 604 |
|
701 | 605 |
l := digitByteArray size. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
606 |
[l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ |
701 | 607 |
l := l - 1. |
608 |
]. |
|
609 |
^ l |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
610 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
611 |
"Modified: 2.2.1996 / 16:47:21 / cg" |
701 | 612 |
! |
613 |
||
614 |
digits |
|
615 |
"return a byteArray fille with the receivers bits |
|
616 |
(8 bits of the absolute value per element)" |
|
617 |
||
618 |
^ digitByteArray |
|
619 |
! ! |
|
620 |
||
621 |
!LargeInteger methodsFor:'coercing & converting'! |
|
68 | 622 |
|
701 | 623 |
asFloat |
624 |
"return a Float with same value as myself. |
|
625 |
Since floats have a limited precision, you usually loose bits when |
|
626 |
doing this." |
|
627 |
||
628 |
|newFloat| |
|
629 |
||
630 |
newFloat := 0.0. |
|
631 |
(sign == 0) ifFalse:[ |
|
632 |
digitByteArray reverseDo:[:aDigit | |
|
633 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
634 |
]. |
|
635 |
(sign < 0) ifTrue:[ |
|
636 |
newFloat := newFloat negated |
|
345 | 637 |
] |
701 | 638 |
]. |
639 |
^ newFloat |
|
640 |
||
641 |
" |
|
642 |
1234567890 asFloat |
|
643 |
12345678901234567890 asFloat |
|
644 |
12345678901234567890 asFloat asInteger |
|
645 |
" |
|
646 |
! |
|
647 |
||
648 |
asLargeInteger |
|
649 |
"return a LargeInteger with same value as myself - thats me" |
|
650 |
||
651 |
^ self |
|
652 |
! |
|
653 |
||
654 |
asSmallInteger |
|
655 |
"return a SmallInteger with same value as myself - |
|
656 |
the result is invalid if the receivers value cannot |
|
657 |
be represented as a SmallInteger. |
|
658 |
Q: should we raise an exception if this happens ?" |
|
659 |
||
660 |
|value| |
|
661 |
||
662 |
value := 0. |
|
663 |
(sign == 0) ifFalse:[ |
|
664 |
digitByteArray reverseDo:[:aDigit | |
|
665 |
value := (value times:256) + aDigit |
|
345 | 666 |
]. |
701 | 667 |
(sign < 0) ifTrue:[ |
668 |
value := value negated |
|
345 | 669 |
] |
68 | 670 |
]. |
701 | 671 |
^ value |
672 |
! |
|
673 |
||
674 |
coerce:aNumber |
|
675 |
"return the argument as a LargeInteger" |
|
676 |
||
677 |
^ aNumber asLargeInteger |
|
678 |
! |
|
679 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
680 |
compressed |
701 | 681 |
"if the receiver can be represented as a SmallInteger, return |
2267 | 682 |
a SmallInteger with my value; |
683 |
otherwise remove leading zero bytes in the digitByte array |
|
684 |
and return the receiver" |
|
701 | 685 |
|
686 |
|index "{ Class: SmallInteger }" |
|
2267 | 687 |
val "{ Class: SmallInteger }" |
688 |
index0 |
|
689 |
| |
|
701 | 690 |
|
691 |
%{ /* NOCONTEXT */ |
|
692 |
OBJ t; |
|
693 |
||
1133 | 694 |
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
|
695 |
RETURN (__MKSMALLINT(0)); |
701 | 696 |
} |
697 |
||
1133 | 698 |
t = __INST(digitByteArray); |
701 | 699 |
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
|
700 |
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
|
701 |
int _idx, _idx0; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
702 |
int _val; |
701 | 703 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
704 |
_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
|
705 |
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
|
706 |
_idx--; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
707 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
708 |
if (_idx <= 4) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
709 |
if (_idx <= 2) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
710 |
if (_idx == 0) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
711 |
RETURN (__MKSMALLINT(0)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
712 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
713 |
if (_idx == 1) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
714 |
_val = _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
715 |
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
|
716 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
717 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
718 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
719 |
_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
|
720 |
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
|
721 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
722 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
723 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
724 |
if (_idx == 3) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
725 |
_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
|
726 |
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
|
727 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
728 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
729 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
730 |
_val = _digitBytes[3]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
731 |
if (_val <= 0x40) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
732 |
_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
|
733 |
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
|
734 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
735 |
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
|
736 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
737 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
738 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
739 |
} |
701 | 740 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
741 |
if (_idx == _idx0) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
742 |
RETURN (self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
743 |
} |
2267 | 744 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
745 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
746 |
* 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
|
747 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
748 |
{ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
749 |
OBJ newDigits; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
750 |
OBJ oldDigits; |
701 | 751 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
752 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
753 |
* 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
|
754 |
* the receiver ... |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
755 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
756 |
__PROTECT__(self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
757 |
__PROTECT__(__INST(digitByteArray)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
758 |
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
|
759 |
__UNPROTECT__(oldDigits); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
760 |
__UNPROTECT__(self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
761 |
if (newDigits) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
762 |
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
|
763 |
__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
|
764 |
_idx); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
765 |
__INST(digitByteArray) = newDigits; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
766 |
__STORE(self, newDigits); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
767 |
RETURN (self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
768 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
769 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
770 |
* allocation failed ... |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
771 |
* ... 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
|
772 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
773 |
} |
701 | 774 |
} |
775 |
%}. |
|
2267 | 776 |
index0 := index := digitByteArray size. |
701 | 777 |
[(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
|
778 |
index := index - 1 |
701 | 779 |
]. |
780 |
||
2267 | 781 |
(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
|
782 |
digitByteArray := digitByteArray copyFrom:1 to:index |
701 | 783 |
]. |
784 |
^ self |
|
785 |
! |
|
786 |
||
787 |
value:aSmallInteger |
|
788 |
"return a new LargeInteger with value taken from a SmallInteger. |
|
789 |
This method will fail, if the argument is not a smallInteger." |
|
790 |
||
791 |
|absValue |
|
792 |
b1 "{ Class: SmallInteger }" |
|
793 |
b2 "{ Class: SmallInteger }" |
|
794 |
b3 "{ Class: SmallInteger }"| |
|
92 | 795 |
|
796 |
" |
|
701 | 797 |
could have simply created a 4-byte largeinteger and normalize |
798 |
it; the code below does the normalize right away, avoiding the |
|
799 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
92 | 800 |
" |
701 | 801 |
(aSmallInteger == 0) ifTrue: [ |
2001 | 802 |
digitByteArray := ByteArray with:0. |
803 |
sign := 0. |
|
804 |
^ self |
|
701 | 805 |
]. |
806 |
(aSmallInteger < 0) ifTrue: [ |
|
2001 | 807 |
sign := -1. |
808 |
absValue := aSmallInteger negated |
|
701 | 809 |
] ifFalse: [ |
2001 | 810 |
sign := 1. |
811 |
absValue := aSmallInteger |
|
701 | 812 |
]. |
813 |
b1 := absValue bitAnd:16rFF. |
|
814 |
absValue := absValue bitShift:-8. |
|
815 |
absValue == 0 ifTrue:[ |
|
2001 | 816 |
digitByteArray := ByteArray with:b1 |
701 | 817 |
] ifFalse:[ |
2001 | 818 |
b2 := absValue bitAnd:16rFF. |
819 |
absValue := absValue bitShift:-8. |
|
820 |
absValue == 0 ifTrue:[ |
|
821 |
digitByteArray := ByteArray with:b1 with:b2 |
|
822 |
] ifFalse:[ |
|
823 |
b3 := absValue bitAnd:16rFF. |
|
824 |
absValue := absValue bitShift:-8. |
|
825 |
absValue == 0 ifTrue:[ |
|
826 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
827 |
] ifFalse:[ |
|
828 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:absValue |
|
829 |
] |
|
830 |
] |
|
701 | 831 |
] |
1890 | 832 |
|
833 |
"Modified: 5.11.1996 / 16:15:39 / cg" |
|
701 | 834 |
! ! |
835 |
||
836 |
!LargeInteger methodsFor:'comparing'! |
|
837 |
||
838 |
< aNumber |
|
839 |
"return true, if the argument, aNumber is greater than the receiver" |
|
840 |
||
841 |
|otherSign| |
|
842 |
||
843 |
(aNumber class == self class) ifFalse:[ |
|
844 |
^ self retry:#< coercing:aNumber |
|
845 |
]. |
|
846 |
otherSign := aNumber sign. |
|
847 |
||
848 |
(sign > 0) ifTrue:[ |
|
849 |
"I am positive" |
|
850 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
851 |
^ false "aNumber is <= 0" |
|
852 |
]. |
|
853 |
(sign == 0) ifTrue:[ |
|
854 |
(otherSign > 0) ifTrue:[^ true]. |
|
855 |
^ false |
|
856 |
]. |
|
857 |
"I am negative" |
|
858 |
(otherSign > 0) ifTrue:[^ true]. |
|
859 |
(otherSign == 0) ifTrue:[^ true]. |
|
860 |
^ (self absLess:aNumber) not |
|
92 | 861 |
! |
862 |
||
701 | 863 |
= aNumber |
864 |
"return true, if the argument, aNumber has the same value as |
|
865 |
the receiver" |
|
866 |
||
867 |
(aNumber class == self class) ifFalse:[ |
|
868 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
|
869 |
^ self retry:#= coercing:aNumber |
|
870 |
]. |
|
871 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
872 |
^ self absEq:aNumber |
|
873 |
! |
|
874 |
||
875 |
> aNumber |
|
876 |
"return true, if the argument, aNumber is less than the receiver" |
|
877 |
||
878 |
|otherSign| |
|
879 |
||
880 |
(aNumber class == self class) ifFalse:[ |
|
881 |
^ self retry:#> coercing:aNumber |
|
882 |
]. |
|
883 |
otherSign := aNumber sign. |
|
884 |
||
885 |
(sign > 0) ifTrue:[ |
|
886 |
"I am positive" |
|
887 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
888 |
^ true "aNumber is <= 0" |
|
889 |
]. |
|
890 |
(sign == 0) ifTrue:[ |
|
891 |
"I am zero" |
|
892 |
(otherSign > 0) ifTrue:[^ false]. |
|
893 |
^ true |
|
894 |
]. |
|
895 |
"I am negative" |
|
896 |
(otherSign > 0) ifTrue:[^ false]. |
|
897 |
(otherSign == 0) ifTrue:[^ false]. |
|
898 |
^ (self absLess:aNumber) not |
|
899 |
! ! |
|
900 |
||
901 |
!LargeInteger methodsFor:'double dispatching'! |
|
902 |
||
92 | 903 |
differenceFromInteger:anInteger |
904 |
"sent, when anInteger does not know how to subtract the receiver. |
|
905 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
906 |
||
907 |
anInteger > 0 ifTrue:[ |
|
2001 | 908 |
sign > 0 ifTrue:[ |
909 |
^ (self absFastMinus:anInteger) sign:-1 |
|
910 |
]. |
|
911 |
^ self absFastPlus:anInteger |
|
92 | 912 |
]. |
1794 | 913 |
anInteger == 0 ifTrue:[ |
2001 | 914 |
^ self negated |
1794 | 915 |
]. |
916 |
sign > 0 ifTrue:[ |
|
2001 | 917 |
^ (self absFastPlus:anInteger negated) sign:-1 |
1794 | 918 |
]. |
919 |
^ (self absFastMinus:anInteger) sign:-1 |
|
92 | 920 |
|
921 |
" |
|
922 |
12345678901234567890 |
|
923 |
-12345678901234567890 |
|
924 |
12345678901234567890 differenceFromInteger:0 |
|
925 |
12345678901234567890 differenceFromInteger:1 |
|
926 |
12345678901234567890 differenceFromInteger:-1 |
|
927 |
-12345678901234567890 differenceFromInteger:1 |
|
928 |
-12345678901234567890 differenceFromInteger:-1 |
|
929 |
" |
|
1794 | 930 |
|
931 |
"Modified: 20.10.1996 / 18:41:54 / cg" |
|
68 | 932 |
! |
933 |
||
934 |
productFromInteger:anInteger |
|
935 |
"sent, when anInteger does not know how to multiply the receiver. |
|
936 |
Return the product of the receiver and the argument, aSmallInteger" |
|
937 |
||
938 |
|num result |
|
939 |
resultDigitByteArray |
|
940 |
val "{ Class: SmallInteger }" |
|
941 |
len "{ Class: SmallInteger }" |
|
942 |
carry "{ Class: SmallInteger }" |
|
85 | 943 |
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
|
944 |
ok lResult| |
68 | 945 |
|
946 |
"multiplying by a small integer is done here" |
|
947 |
||
948 |
"trivial cases" |
|
949 |
anInteger == 0 ifTrue:[^ 0]. |
|
950 |
anInteger == 1 ifTrue:[^ self]. |
|
951 |
||
952 |
num := anInteger abs. |
|
953 |
(num > 16r3FFFFF) ifTrue:[ |
|
2001 | 954 |
"if num is too big (so that multiplying by a byte could create a Large)" |
68 | 955 |
|
2001 | 956 |
^ anInteger retry:#* coercing:self |
68 | 957 |
]. |
958 |
||
959 |
len := digitByteArray size. |
|
960 |
||
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
961 |
val := num. |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
962 |
val < 16r100 ifTrue:[ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
963 |
lResult := len + 1. |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
964 |
] ifFalse:[ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
965 |
val < 16r10000 ifTrue:[ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
966 |
lResult := len + 2 |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
967 |
] ifFalse:[ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
968 |
lResult := len + 4. |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
969 |
] |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
970 |
]. |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
971 |
result := self class basicNew numberOfDigits:lResult. |
1 | 972 |
|
68 | 973 |
anInteger < 0 ifTrue:[ |
2001 | 974 |
sign > 0 ifTrue:[ |
975 |
result sign:-1 |
|
976 |
]. |
|
68 | 977 |
] ifFalse:[ |
2001 | 978 |
sign < 0 ifTrue:[ |
979 |
result sign:sign |
|
980 |
] |
|
68 | 981 |
]. |
982 |
||
983 |
resultDigitByteArray := result digits. |
|
984 |
||
985 |
carry := 0. |
|
986 |
||
85 | 987 |
ok := false. |
988 |
%{ |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
989 |
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
|
990 |
|
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
991 |
if (__isSmallInteger(len) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
992 |
&& __isByteArray(__digitByteArray) |
85 | 993 |
&& __isByteArray(resultDigitByteArray)) { |
2001 | 994 |
int _l = __intVal(len); |
995 |
int _v = __intVal(val); |
|
996 |
unsigned _carry = 0; |
|
997 |
unsigned _prod; |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
998 |
unsigned char *digitP = __ByteArrayInstPtr(__digitByteArray)->ba_element; |
2001 | 999 |
unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1000 |
|
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1001 |
#if defined(i386) /* XXX actually: LSB_FIRST */ |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1002 |
# if defined (__GNUC__) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1003 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1004 |
* can do it long-word-wise; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1005 |
* 32*32 -> 64 multiplication |
2001 | 1006 |
*/ |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1007 |
while (_l > 3) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1008 |
unsigned __pHi, __pLow; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1009 |
|
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1010 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1011 |
* max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001 |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1012 |
* + maxCarry (0xFFFF.FFFF) -> 0xFFFF.FFFF.0000.0000 |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1013 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1014 |
asm ("mull %3 \n |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1015 |
addl %4,%%eax \n |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1016 |
adcl $0,%%edx" |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1017 |
: "=a" ((unsigned long)(__pLow)), |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1018 |
"=d" ((unsigned long)(__pHi)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1019 |
: "0" ((unsigned long)(((unsigned long *)digitP)[0])), |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1020 |
"1" ((unsigned long)(_v)), |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1021 |
"rm" ((unsigned long)(_carry)) ); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1022 |
|
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1023 |
((unsigned long *)resultP)[0] = __pLow; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1024 |
_carry = __pHi; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1025 |
digitP += 4; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1026 |
resultP += 4; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1027 |
_l -= 4; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1028 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1029 |
# else |
85 | 1030 |
|
2001 | 1031 |
if (_v <= 0xFFFF) { |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1032 |
/* can do it short-wise |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1033 |
* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1034 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1035 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1036 |
*/ |
2001 | 1037 |
while (_l > 1) { |
1038 |
_prod = ((unsigned short *)digitP)[0] * _v + _carry; |
|
1039 |
((unsigned short *)resultP)[0] = _prod & 0xFFFF; |
|
1040 |
_carry = _prod >> 16; |
|
1041 |
digitP += 2; |
|
1042 |
resultP += 2; |
|
1043 |
_l -= 2; |
|
1044 |
} |
|
1045 |
} |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1046 |
# endif |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1047 |
#endif |
2001 | 1048 |
{ |
1049 |
while (_l-- > 0) { |
|
1050 |
_prod = *digitP++ * _v + _carry; |
|
1051 |
*resultP++ = _prod & 0xFF; |
|
1052 |
_carry = _prod >> 8; |
|
1053 |
} |
|
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1054 |
|
2001 | 1055 |
while (_carry) { |
1056 |
*resultP++ = _carry & 0xFF; |
|
1057 |
_carry >>= 8; |
|
1058 |
} |
|
1059 |
} |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1060 |
|
2001 | 1061 |
ok = true; |
85 | 1062 |
} |
1063 |
%}. |
|
92 | 1064 |
" |
1065 |
fall back - normally not reached |
|
1066 |
(could make it a primitive-failure as well) |
|
1067 |
" |
|
85 | 1068 |
ok ifFalse:[ |
2001 | 1069 |
1 to:len do:[:i | |
1070 |
prod := (digitByteArray basicAt:i) * val + carry. |
|
1071 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
|
1072 |
carry := prod bitShift:-8. |
|
1073 |
]. |
|
1074 |
[carry ~~ 0] whileTrue:[ |
|
1075 |
len := len + 1. |
|
1076 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
|
1077 |
carry := carry bitShift:-8 |
|
1078 |
]. |
|
68 | 1079 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1080 |
^ result compressed |
92 | 1081 |
! |
1082 |
||
701 | 1083 |
sumFromInteger:anInteger |
1084 |
"sent, when anInteger does not know how to add the receiver. |
|
1085 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
|
92 | 1086 |
|
701 | 1087 |
anInteger > 0 ifTrue:[ |
2001 | 1088 |
sign < 0 ifTrue:[ |
1089 |
^ (self absFastMinus:anInteger) sign:-1 |
|
1090 |
]. |
|
1091 |
^ self absFastPlus:anInteger |
|
701 | 1092 |
]. |
1794 | 1093 |
anInteger == 0 ifTrue:[ |
2001 | 1094 |
^ self |
1794 | 1095 |
]. |
1096 |
sign < 0 ifTrue:[ |
|
2001 | 1097 |
^ (self absFastPlus:anInteger abs) sign:-1 |
1794 | 1098 |
]. |
1099 |
^ self absFastMinus:anInteger |
|
92 | 1100 |
|
701 | 1101 |
" |
1102 |
12345678901234567890 |
|
1103 |
-12345678901234567890 |
|
1104 |
12345678901234567890 sumFromInteger:0 |
|
1105 |
-12345678901234567890 sumFromInteger:0 |
|
1106 |
12345678901234567890 sumFromInteger:1 |
|
1107 |
12345678901234567890 sumFromInteger:-1 |
|
1108 |
-12345678901234567890 sumFromInteger:1 |
|
1109 |
-12345678901234567890 sumFromInteger:-1 |
|
1110 |
" |
|
1794 | 1111 |
|
1112 |
"Modified: 20.10.1996 / 18:41:51 / cg" |
|
68 | 1113 |
! ! |
1114 |
||
701 | 1115 |
!LargeInteger methodsFor:'printing & storing'! |
345 | 1116 |
|
701 | 1117 |
storeOn:aStream |
1118 |
"append a representation of the receiver to aStream, which can |
|
1119 |
be used to reconstruct the receiver." |
|
68 | 1120 |
|
701 | 1121 |
self printOn:aStream. |
1122 |
aStream nextPutAll:' asLargeInteger' |
|
68 | 1123 |
! ! |
1124 |
||
1125 |
!LargeInteger methodsFor:'private'! |
|
1126 |
||
701 | 1127 |
absDiv:anInteger |
1128 |
"return an array with two LargeIntegers representing |
|
1129 |
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
|
1130 |
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
|
1131 |
This method needs a rewrite." |
68 | 1132 |
|
701 | 1133 |
|tmp1 tmp2 |
1890 | 1134 |
quo |
2521 | 1135 |
count "{ Class: SmallInteger }" | |
92 | 1136 |
|
701 | 1137 |
anInteger == 0 ifTrue:[ |
2001 | 1138 |
^ DivisionByZeroSignal raise |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1139 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1140 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1141 |
self = anInteger ifTrue:[ |
2001 | 1142 |
^ Array with:1 with:0 |
701 | 1143 |
]. |
92 | 1144 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1145 |
tmp1 := self simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1146 |
tmp1 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1147 |
tmp2 := anInteger simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1148 |
tmp2 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1149 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1150 |
(tmp1 < tmp2) ifTrue:[ |
2001 | 1151 |
^ Array with:0 with:tmp1 |
68 | 1152 |
]. |
92 | 1153 |
|
701 | 1154 |
count := 0. |
1890 | 1155 |
[tmp2 absLess: tmp1] whileTrue:[ |
2001 | 1156 |
tmp2 mul2. |
1157 |
count := count + 1 |
|
701 | 1158 |
]. |
1159 |
||
1890 | 1160 |
tmp2 div2. |
701 | 1161 |
|
1890 | 1162 |
quo := 0 asLargeInteger. |
1163 |
quo sign:1. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1164 |
|
701 | 1165 |
[count == 0] whileFalse:[ |
2001 | 1166 |
quo mul2. |
1167 |
(tmp1 absLess:tmp2) ifFalse:[ |
|
1168 |
quo digits at:1 put:((quo digits at:1) bitOr:1). |
|
1169 |
(tmp1 absSubtract: tmp2) ifFalse:[ |
|
1170 |
"/ difference is zero; continue shifting |
|
1171 |
count := count - 1. |
|
1172 |
[count >= 8] whileTrue:[ |
|
1173 |
quo mul256. |
|
1174 |
count := count - 8 |
|
1175 |
]. |
|
1176 |
[count == 0] whileFalse:[ |
|
1177 |
quo mul2. |
|
1178 |
count := count - 1. |
|
1179 |
]. |
|
1180 |
^ Array with:quo compressed with:tmp1 compressed |
|
1181 |
]. |
|
1182 |
]. |
|
1183 |
tmp2 div2. |
|
1184 |
count := count - 1 |
|
701 | 1185 |
]. |
1890 | 1186 |
^ 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
|
1187 |
|
1890 | 1188 |
"Modified: 5.11.1996 / 18:40:24 / cg" |
92 | 1189 |
! |
1190 |
||
701 | 1191 |
absEq:aLargeInteger |
1192 |
"return true, if abs(self) = abs(theArgument)" |
|
92 | 1193 |
|
701 | 1194 |
|len1 "{ Class: SmallInteger }" |
1195 |
len2 "{ Class: SmallInteger }" |
|
1196 |
d1 "{ Class: SmallInteger }" |
|
1197 |
d2 "{ Class: SmallInteger }" |
|
1198 |
otherDigitByteArray | |
|
92 | 1199 |
|
701 | 1200 |
len1 := digitByteArray size. |
1201 |
otherDigitByteArray := aLargeInteger digits. |
|
1202 |
len2 := otherDigitByteArray size. |
|
92 | 1203 |
|
701 | 1204 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
1205 |
len1 := len1 - 1 |
|
1206 |
]. |
|
1207 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
1208 |
len2 := len2 - 1 |
|
68 | 1209 |
]. |
701 | 1210 |
(len1 ~~ len2) ifTrue:[^ false]. |
1211 |
[len1 > 0] whileTrue:[ |
|
1212 |
d1 := digitByteArray basicAt:len1. |
|
1213 |
d2 := otherDigitByteArray basicAt:len1. |
|
1214 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1215 |
len1 := len1 - 1 |
|
68 | 1216 |
]. |
701 | 1217 |
^ true |
68 | 1218 |
! |
1219 |
||
1890 | 1220 |
absFastDiv:aPositiveSmallInteger |
68 | 1221 |
"return an array with two LargeIntegers representing |
1890 | 1222 |
abs(self) // aPositiveSmallInteger and abs(self) \\ aPositiveSmallInteger" |
68 | 1223 |
|
77 | 1224 |
|tmp1 "{ Class: SmallInteger }" |
1225 |
prevRest "{ Class: SmallInteger }" |
|
1226 |
count "{ Class: SmallInteger }" |
|
85 | 1227 |
newDigitByteArray result |
1228 |
ok| |
|
68 | 1229 |
|
1890 | 1230 |
aPositiveSmallInteger == 0 ifTrue:[ |
2001 | 1231 |
^ DivisionByZeroSignal raise |
68 | 1232 |
]. |
1233 |
||
1234 |
"This cannot happen (if always normalized) |
|
1890 | 1235 |
self < aPositiveSmallInteger ifTrue:[ |
2001 | 1236 |
^ Array with:0 with:self |
68 | 1237 |
]. |
1238 |
" |
|
1239 |
count := digitByteArray size. |
|
1240 |
result := self class basicNew numberOfDigits:count. |
|
1241 |
newDigitByteArray := result digits. |
|
85 | 1242 |
ok := false. |
1243 |
%{ |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1244 |
OBJ __digits; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1245 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1246 |
__digits = __INST(digitByteArray); |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1247 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1248 |
if (__isByteArray(__digits) |
85 | 1249 |
&& __isByteArray(newDigitByteArray) |
1890 | 1250 |
&& __bothSmallInteger(count, aPositiveSmallInteger)) { |
2001 | 1251 |
unsigned int rest = 0; |
1252 |
int index = __intVal(count); |
|
1253 |
int index0; |
|
1254 |
unsigned divisor = __intVal(aPositiveSmallInteger); |
|
1255 |
unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element; |
|
1256 |
unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element; |
|
68 | 1257 |
|
2001 | 1258 |
index0 = index; |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1259 |
|
1890 | 1260 |
/* |
2001 | 1261 |
if (divisor < 0xFFFF) { |
1262 |
while (index > 1) { |
|
1263 |
unsigned int t; |
|
1264 |
unsigned div; |
|
85 | 1265 |
|
2001 | 1266 |
index--; |
1267 |
t = digitBytes[index]; |
|
1268 |
index--; |
|
1269 |
t = (t << 8) | digitBytes[index]; |
|
1270 |
t = t | (rest << 16); |
|
1271 |
div = t / divisor; |
|
1272 |
rest = t % divisor; |
|
1273 |
resultBytes[index+1] = (div >> 8); |
|
1274 |
resultBytes[index] = (div & 0xFF); |
|
1275 |
} |
|
1276 |
} |
|
1890 | 1277 |
*/ |
2001 | 1278 |
while (index > 0) { |
1279 |
unsigned int t; |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1280 |
|
2001 | 1281 |
index--; |
1282 |
t = digitBytes[index]; |
|
1283 |
t = t | (rest << 8); |
|
1284 |
resultBytes[index] = t / divisor; |
|
1285 |
rest = t % divisor; |
|
1286 |
} |
|
1287 |
prevRest = __MKSMALLINT(rest); |
|
1288 |
ok = true; |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1289 |
|
2001 | 1290 |
/* |
1291 |
* no need to normalize ? |
|
1292 |
*/ |
|
1293 |
if ((index0 > 4) |
|
1294 |
&& (resultBytes[index0-1])) { |
|
1295 |
RETURN ( __ARRAY_WITH2(result, prevRest)); |
|
1296 |
} |
|
85 | 1297 |
} |
1298 |
%}. |
|
1299 |
" |
|
1300 |
slow code - not normally reached |
|
1301 |
(could also do a primitiveFailure here) |
|
1302 |
" |
|
92 | 1303 |
ok ifFalse:[ |
2001 | 1304 |
self primitiveFailed |
68 | 1305 |
]. |
1306 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1307 |
^ Array with:result compressed with:prevRest |
68 | 1308 |
! |
1309 |
||
701 | 1310 |
absFastMinus:aSmallInteger |
1311 |
"return a LargeInteger representing abs(self) - abs(theArgument). |
|
1312 |
The result is not normalized." |
|
1313 |
||
1794 | 1314 |
|result resultDigitByteArray lastDigit |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1315 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1316 |
rsltLen "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1317 |
index "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1318 |
borrow "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1319 |
diff "{ Class: SmallInteger }" | |
701 | 1320 |
|
1321 |
len := digitByteArray size. |
|
1322 |
||
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1323 |
rsltLen := len + 1. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1324 |
result := self class basicNew numberOfDigits:rsltLen. |
701 | 1325 |
resultDigitByteArray := result digits. |
1326 |
||
1327 |
index := 1. |
|
1328 |
borrow := aSmallInteger abs. |
|
1329 |
||
1330 |
[borrow ~~ 0] whileTrue:[ |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1331 |
(index <= len) ifTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1332 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1333 |
borrow := borrow bitShift:-8. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1334 |
diff < 0 ifTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1335 |
diff := diff + 256. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1336 |
borrow := borrow + 1. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1337 |
] |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1338 |
] ifFalse:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1339 |
diff := borrow bitAnd:255. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1340 |
borrow := borrow bitShift:-8. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1341 |
]. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1342 |
resultDigitByteArray basicAt:index put:(lastDigit := diff). |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1343 |
index := index + 1 |
701 | 1344 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1345 |
(index <= rsltLen) ifTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1346 |
[index <= len] whileTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1347 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1348 |
index := index + 1 |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1349 |
]. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1350 |
lastDigit := 0. |
701 | 1351 |
]. |
1352 |
||
1794 | 1353 |
lastDigit == 0 ifTrue:[ |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1354 |
^ result compressed |
1794 | 1355 |
]. |
701 | 1356 |
^ result |
1357 |
||
1358 |
" |
|
1359 |
12345678900000000000 absFastMinus:1 |
|
1360 |
12345678900000000000 absFastMinus:1000000 |
|
1361 |
12345678900000000000 absFastMinus:255 |
|
1362 |
(SmallInteger maxVal + 1) absFastMinus:1 |
|
1363 |
(SmallInteger minVal - 1) absFastMinus:1 |
|
1364 |
" |
|
1794 | 1365 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1366 |
"Modified: 24.3.1997 / 21:33:25 / cg" |
701 | 1367 |
! |
1368 |
||
1369 |
absFastPlus:aSmallInteger |
|
1370 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
|
1371 |
The result is not normalized." |
|
1372 |
||
1794 | 1373 |
|result resultDigitByteArray lastDigit |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1374 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1375 |
rsltLen "{ Class: SmallInteger }" |
701 | 1376 |
index "{ Class: SmallInteger }" |
1377 |
carry "{ Class: SmallInteger }" | |
|
1378 |
||
1379 |
len := digitByteArray size. |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1380 |
rsltLen := len + 1. |
701 | 1381 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1382 |
result := self class basicNew numberOfDigits:rsltLen. |
701 | 1383 |
resultDigitByteArray := result digits. |
1384 |
||
1385 |
index := 1. |
|
1386 |
carry := aSmallInteger abs. |
|
1387 |
||
1388 |
[carry ~~ 0] whileTrue:[ |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1389 |
(index <= len) ifTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1390 |
carry := (digitByteArray basicAt:index) + carry. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1391 |
]. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1392 |
resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF). |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1393 |
carry := carry bitShift:-8. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1394 |
index := index + 1 |
701 | 1395 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1396 |
|
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1397 |
(index <= rsltLen) ifTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1398 |
[index <= len] whileTrue:[ |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1399 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1400 |
index := index + 1 |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1401 |
]. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1402 |
lastDigit := 0. |
701 | 1403 |
]. |
1404 |
||
1794 | 1405 |
lastDigit == 0 ifTrue:[ |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1406 |
^ result compressed |
1794 | 1407 |
]. |
701 | 1408 |
^ result |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1409 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1410 |
"Modified: 24.3.1997 / 21:32:41 / cg" |
701 | 1411 |
! |
1412 |
||
1413 |
absLess:aLargeInteger |
|
1890 | 1414 |
"return true, if abs(self) < abs(theArgument). |
1415 |
This handles unnormalized largeIntegers." |
|
701 | 1416 |
|
1890 | 1417 |
|myLen "{ Class: SmallInteger }" |
1418 |
otherLen "{ Class: SmallInteger }" |
|
701 | 1419 |
d1 "{ Class: SmallInteger }" |
1420 |
d2 "{ Class: SmallInteger }" |
|
1421 |
otherDigitByteArray | |
|
1422 |
||
1890 | 1423 |
myLen := digitByteArray size. |
701 | 1424 |
otherDigitByteArray := aLargeInteger digits. |
1890 | 1425 |
otherLen := otherDigitByteArray size. |
701 | 1426 |
|
1890 | 1427 |
[(digitByteArray basicAt:myLen) == 0] whileTrue:[ |
2001 | 1428 |
myLen := myLen - 1 |
701 | 1429 |
]. |
1890 | 1430 |
[(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[ |
2001 | 1431 |
otherLen := otherLen - 1 |
701 | 1432 |
]. |
1890 | 1433 |
(myLen < otherLen) ifTrue:[^ true]. |
1434 |
(myLen > otherLen) ifTrue:[^ false]. |
|
701 | 1435 |
|
1890 | 1436 |
[myLen > 0] whileTrue:[ |
2001 | 1437 |
d1 := digitByteArray basicAt:myLen. |
1438 |
d2 := otherDigitByteArray basicAt:myLen. |
|
1439 |
d1 == d2 ifFalse:[ |
|
1440 |
(d1 < d2) ifTrue:[^ true]. |
|
1441 |
^ false |
|
1442 |
]. |
|
1443 |
myLen := myLen - 1 |
|
701 | 1444 |
]. |
1445 |
^ false |
|
1890 | 1446 |
|
1447 |
"Modified: 5.11.1996 / 18:37:27 / cg" |
|
701 | 1448 |
! |
1449 |
||
1450 |
absMinus:aLargeInteger |
|
1451 |
"return a LargeInteger representing abs(self) - abs(theArgument)" |
|
1452 |
||
1453 |
|result done |
|
1454 |
otherDigitByteArray resultDigitByteArray |
|
1455 |
len1 "{ Class: SmallInteger }" |
|
1456 |
len2 "{ Class: SmallInteger }" |
|
1457 |
index "{ Class: SmallInteger }" |
|
1458 |
borrow "{ Class: SmallInteger }" |
|
1459 |
diff "{ Class: SmallInteger }" |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1460 |
carry "{ Class: SmallInteger }" |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1461 |
lResult| |
701 | 1462 |
|
1463 |
len1 := digitByteArray size. |
|
1464 |
otherDigitByteArray := aLargeInteger digits. |
|
1465 |
len2 := otherDigitByteArray size. |
|
1466 |
||
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1467 |
lResult := (len1 max: len2) + 1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1468 |
result := self class basicNew numberOfDigits:lResult. |
701 | 1469 |
resultDigitByteArray := result digits. |
1470 |
||
1471 |
index := 1. |
|
1472 |
borrow := 0. |
|
1473 |
||
1474 |
done := false. |
|
1475 |
[done] whileFalse:[ |
|
2001 | 1476 |
diff := borrow. |
1477 |
(index <= len1) ifTrue:[ |
|
1478 |
diff := diff + (digitByteArray basicAt:index). |
|
1479 |
(index <= len2) ifTrue:[ |
|
1480 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1481 |
] |
|
1482 |
] ifFalse:[ |
|
1483 |
(index <= len2) ifTrue:[ |
|
1484 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
1485 |
] ifFalse:[ |
|
1486 |
"end reached" |
|
1487 |
done := true |
|
1488 |
] |
|
1489 |
]. |
|
701 | 1490 |
|
2001 | 1491 |
"/ workaround for |
1492 |
"/ gcc code generator bug |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1493 |
|
2001 | 1494 |
(diff >= 0) ifTrue:[ |
1495 |
borrow := 0 |
|
1496 |
] ifFalse:[ |
|
1497 |
borrow := -1. |
|
1498 |
diff := diff + 16r100 |
|
1499 |
]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1500 |
|
701 | 1501 |
"/ (diff < 0) ifTrue:[ |
1502 |
"/ borrow := -1. |
|
1503 |
"/ diff := diff + 16r100 |
|
1504 |
"/ ] ifFalse:[ |
|
1505 |
"/ borrow := 0 |
|
1506 |
"/ ]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1507 |
|
2001 | 1508 |
resultDigitByteArray basicAt:index put:diff. |
1509 |
index := index + 1 |
|
701 | 1510 |
]. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1511 |
|
701 | 1512 |
(borrow ~~ 0) ifTrue:[ |
2001 | 1513 |
"/ must generate 255's complement |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1514 |
|
2001 | 1515 |
result sign:-1. |
1516 |
[index <= lResult] whileTrue:[ |
|
1517 |
resultDigitByteArray basicAt:index put:16rFF. |
|
1518 |
index := index + 1. |
|
1519 |
]. |
|
1520 |
index := resultDigitByteArray size. |
|
1521 |
[index > 0] whileTrue:[ |
|
1522 |
resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)). |
|
1523 |
index := index -1. |
|
1524 |
]. |
|
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1525 |
|
2001 | 1526 |
index := 1. |
1527 |
carry := 1. |
|
1528 |
[carry ~~ 0] whileTrue:[ |
|
1529 |
(index <= lResult) ifTrue:[ |
|
1530 |
carry := (resultDigitByteArray basicAt:index) + carry. |
|
1531 |
]. |
|
1532 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
|
1533 |
carry := carry bitShift:-8. |
|
1534 |
index := index + 1 |
|
1535 |
]. |
|
701 | 1536 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1537 |
^ result compressed |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1538 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1539 |
"Modified: 5.11.1996 / 14:09:25 / cg" |
701 | 1540 |
! |
1541 |
||
68 | 1542 |
absMul:aLargeInteger |
1543 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
1544 |
||
92 | 1545 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 1546 |
idx "{ Class: SmallInteger }" |
1547 |
carry "{ Class: SmallInteger }" |
|
1548 |
len1 "{ Class: SmallInteger }" |
|
1549 |
len2 "{ Class: SmallInteger }" |
|
1550 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 1551 |
prod "{ Class: SmallInteger }" |
1552 |
v "{ Class: SmallInteger }"| |
|
68 | 1553 |
|
1554 |
len1 := digitByteArray size. |
|
1555 |
otherDigitByteArray := aLargeInteger digits. |
|
1556 |
len2 := otherDigitByteArray size. |
|
1557 |
||
1558 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1). |
|
1559 |
resultDigitByteArray := result digits. |
|
92 | 1560 |
ok := false. |
1561 |
%{ |
|
1133 | 1562 |
if (__isByteArray(__INST(digitByteArray)) |
92 | 1563 |
&& __isByteArray(otherDigitByteArray) |
1564 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 1565 |
&& __bothSmallInteger(len1, len2)) { |
2001 | 1566 |
unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
1567 |
unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
1568 |
unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
1569 |
int _index1, _index2, _dstIndex, _idx; |
|
1570 |
unsigned _prod, _carry, _v; |
|
68 | 1571 |
|
2001 | 1572 |
for (_index1 = 0; _index1 < __intVal(len1); _index1++) { |
1573 |
for (_index2 = 0; _index2 < __intVal(len2); _index2++) { |
|
1574 |
_dstIndex = _index1 + _index2; |
|
1575 |
_prod = myBytes[_index1] * otherBytes[_index2]; |
|
1576 |
_prod += resultBytes[_dstIndex]; |
|
1577 |
resultBytes[_dstIndex] = _prod & 0xFF; |
|
1578 |
_carry = _prod >> 8; |
|
1579 |
if (_carry) { |
|
1580 |
_idx = _dstIndex + 1; |
|
1581 |
while (_carry) { |
|
1582 |
_v = resultBytes[_idx] + _carry; |
|
1583 |
resultBytes[_idx] = _v & 0xFF; |
|
1584 |
_carry = _v >> 8; |
|
1585 |
_idx = _idx + 1; |
|
1586 |
} |
|
1587 |
} |
|
1588 |
} |
|
1589 |
} |
|
1590 |
ok = true; |
|
92 | 1591 |
} |
1592 |
%}. |
|
1593 |
ok ifFalse:[ |
|
2001 | 1594 |
1 to:len1 do:[:index1 | |
1595 |
1 to:len2 do:[:index2 | |
|
1596 |
dstIndex := index1 + index2 - 1. |
|
1597 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
1598 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
1599 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
1600 |
carry := prod bitShift:-8. |
|
1601 |
carry ~~ 0 ifTrue:[ |
|
1602 |
idx := dstIndex + 1. |
|
1603 |
[carry ~~ 0] whileTrue:[ |
|
1604 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
1605 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
1606 |
carry := v bitShift:-8. |
|
1607 |
idx := idx + 1 |
|
1608 |
] |
|
1609 |
] |
|
1610 |
] |
|
1611 |
]. |
|
68 | 1612 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1613 |
^ result compressed |
68 | 1614 |
! |
1615 |
||
1616 |
absPlus:aLargeInteger |
|
1617 |
"return a LargeInteger representing abs(self) + abs(theArgument)" |
|
1618 |
||
1619 |
|result done otherDigitByteArray resultDigitByteArray |
|
1620 |
len1 "{ Class: SmallInteger }" |
|
1621 |
len2 "{ Class: SmallInteger }" |
|
1622 |
index "{ Class: SmallInteger }" |
|
1623 |
carry "{ Class: SmallInteger }" |
|
1624 |
sum "{ Class: SmallInteger }" | |
|
1625 |
||
1626 |
len1 := digitByteArray size. |
|
1627 |
otherDigitByteArray := aLargeInteger digits. |
|
1628 |
len2 := otherDigitByteArray size. |
|
1629 |
||
92 | 1630 |
result := self class basicNew numberOfDigits:((len1 max: len2) + 1). |
68 | 1631 |
resultDigitByteArray := result digits. |
1632 |
||
1633 |
index := 1. |
|
1634 |
carry := 0. |
|
1635 |
||
1636 |
done := false. |
|
1637 |
[done] whileFalse:[ |
|
2001 | 1638 |
sum := carry. |
1639 |
(index <= len1) ifTrue:[ |
|
1640 |
sum := sum + (digitByteArray basicAt:index). |
|
1641 |
(index <= len2) ifTrue:[ |
|
1642 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1643 |
] |
|
1644 |
] ifFalse:[ |
|
1645 |
(index <= len2) ifTrue:[ |
|
1646 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1647 |
] ifFalse:[ |
|
1648 |
"end reached" |
|
1649 |
done := true |
|
1650 |
] |
|
1651 |
]. |
|
1652 |
(sum >= 16r100) ifTrue:[ |
|
1653 |
carry := 1. |
|
1654 |
sum := sum - 16r100 |
|
1655 |
] ifFalse:[ |
|
1656 |
carry := 0 |
|
1657 |
]. |
|
1658 |
resultDigitByteArray basicAt:index put:sum. |
|
1659 |
index := index + 1 |
|
68 | 1660 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1661 |
^ result compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1662 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1663 |
"Modified: 5.11.1996 / 14:09:34 / cg" |
68 | 1664 |
! |
1665 |
||
1890 | 1666 |
absSubtract:aLargeInteger |
1667 |
"destructively subtract aLargeInteger from myself. |
|
1668 |
A helper for division; only allowed for positive receiver |
|
1669 |
and argument. The receiver must be >= the argument. |
|
1670 |
The receiver must be a temporary scratch-number" |
|
1671 |
||
2521 | 1672 |
|done |
1673 |
otherDigitByteArray |
|
1890 | 1674 |
len1 "{ Class: SmallInteger }" |
1675 |
len2 "{ Class: SmallInteger }" |
|
1676 |
index "{ Class: SmallInteger }" |
|
1677 |
borrow "{ Class: SmallInteger }" |
|
1678 |
diff "{ Class: SmallInteger }" |
|
2521 | 1679 |
nonZero| |
1890 | 1680 |
|
1681 |
len1 := digitByteArray size. |
|
1682 |
otherDigitByteArray := aLargeInteger digits. |
|
1683 |
len2 := otherDigitByteArray size. |
|
1684 |
len2 > len1 ifTrue:[ |
|
2001 | 1685 |
[(otherDigitByteArray at:len2) == 0] whileTrue:[ |
1686 |
len2 := len2 - 1 |
|
1687 |
]. |
|
1688 |
len2 > len1 ifTrue:[ |
|
1689 |
self halt "/ may not be called that way |
|
1690 |
]. |
|
1890 | 1691 |
]. |
1692 |
||
1693 |
nonZero := false. |
|
1694 |
index := 1. |
|
1695 |
borrow := 0. |
|
1696 |
||
1697 |
done := false. |
|
1698 |
[index <= len1] whileTrue:[ |
|
2001 | 1699 |
diff := borrow. |
1700 |
diff := diff + (digitByteArray basicAt:index). |
|
1701 |
index <= len2 ifTrue:[ |
|
1702 |
diff := diff - (otherDigitByteArray basicAt:index). |
|
1703 |
]. |
|
1890 | 1704 |
|
2001 | 1705 |
"/ workaround for |
1706 |
"/ gcc code generator bug |
|
1890 | 1707 |
|
2001 | 1708 |
(diff >= 0) ifTrue:[ |
1709 |
borrow := 0 |
|
1710 |
] ifFalse:[ |
|
1711 |
borrow := -1. |
|
1712 |
diff := diff + 16r100 |
|
1713 |
]. |
|
1714 |
diff ~~ 0 ifTrue:[ |
|
1715 |
nonZero := true |
|
1716 |
]. |
|
1717 |
digitByteArray basicAt:index put:diff. |
|
1718 |
index := index + 1 |
|
1890 | 1719 |
]. |
1720 |
^ nonZero |
|
1721 |
||
1722 |
"Created: 5.11.1996 / 16:23:47 / cg" |
|
1723 |
"Modified: 5.11.1996 / 18:56:50 / cg" |
|
1724 |
! |
|
1725 |
||
1726 |
div2 |
|
1727 |
"destructively divide the receiver by 2. |
|
1728 |
private - used for division only. |
|
1729 |
This is worth a primitive." |
|
1730 |
||
1731 |
%{ /* NOCONTEXT */ |
|
1732 |
OBJ __digits = __INST(digitByteArray); |
|
1733 |
||
1734 |
if (__isByteArray(__digits)) { |
|
2001 | 1735 |
int __nBytes = __byteArraySize(__digits); |
1736 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
1737 |
unsigned __this, __next; |
|
1738 |
int __idx; |
|
1890 | 1739 |
|
2001 | 1740 |
if (__nBytes == 1) { |
1741 |
__bp[0] >>= 1; |
|
1742 |
RETURN (self); |
|
1743 |
} |
|
1890 | 1744 |
|
2001 | 1745 |
__idx = 1; |
1890 | 1746 |
|
2001 | 1747 |
if ((__idx+4) < __nBytes) { |
1748 |
__this = ((unsigned long *)__bp)[0]; |
|
68 | 1749 |
|
2001 | 1750 |
while ((__idx+4) < __nBytes) { |
1751 |
__next = ((unsigned long *)__bp)[1]; |
|
1752 |
__this >>= 1; |
|
1753 |
__this |= __next << 31; |
|
1754 |
((unsigned long *)__bp)[0] = __this; |
|
1755 |
__this = __next; |
|
1756 |
__bp += 4; |
|
1757 |
__idx += 4; |
|
1758 |
} |
|
1759 |
} |
|
1890 | 1760 |
|
2001 | 1761 |
__this = __bp[0]; |
1762 |
while (__idx < __nBytes) { |
|
1763 |
__next = __bp[1]; |
|
1764 |
__this >>= 1; |
|
1765 |
__this |= __next << 7; |
|
1766 |
__bp[0] = __this; |
|
1767 |
__this = __next; |
|
1768 |
__bp++; |
|
1769 |
__idx++; |
|
1770 |
} |
|
1771 |
__bp[0] = __this >> 1; |
|
1772 |
RETURN (self); |
|
1890 | 1773 |
} |
1774 |
%}. |
|
1775 |
self primitiveFailed |
|
1776 |
||
1777 |
" |
|
1778 |
100000 asLargeInteger div2 |
|
1779 |
" |
|
1780 |
||
1781 |
"Modified: 5.11.1996 / 16:12:56 / cg" |
|
1782 |
! |
|
1783 |
||
1784 |
mul2 |
|
1785 |
"destructively multiply the receiver by 2. |
|
1786 |
private - used for division only. |
|
1787 |
This is worth a primitive." |
|
1788 |
||
1789 |
|nBytes "{ Class: SmallInteger }" |
|
1790 |
b "{ Class: SmallInteger }" |
|
1791 |
t| |
|
1792 |
||
1793 |
nBytes := digitByteArray size. |
|
1794 |
||
1795 |
b := digitByteArray at:nBytes. |
|
1796 |
(b bitAnd:16r80) ~~ 0 ifTrue:[ |
|
2001 | 1797 |
"/ need another byte |
1798 |
nBytes := nBytes + 1. |
|
1799 |
t := ByteArray uninitializedNew:nBytes. |
|
1800 |
t replaceFrom:1 with:digitByteArray startingAt:1. |
|
1801 |
t at:nBytes put:0. |
|
1802 |
digitByteArray := t. |
|
1890 | 1803 |
]. |
1804 |
||
1805 |
%{ |
|
1806 |
OBJ __digits = __INST(digitByteArray); |
|
1807 |
||
1808 |
if (__isByteArray(__digits)) { |
|
2001 | 1809 |
int __nBytes = __intVal(nBytes); |
1810 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
1811 |
unsigned __carry = 0, __newCarry, __this; |
|
1812 |
int __idx; |
|
1890 | 1813 |
|
2001 | 1814 |
while (__nBytes >= 4) { |
1815 |
__this = ((unsigned long *)__bp)[0]; |
|
1816 |
__newCarry = __this >> 31; |
|
1817 |
((unsigned long *)__bp)[0] = (__this << 1) | __carry; |
|
1818 |
__carry = __newCarry; |
|
1819 |
__bp += 4; |
|
1820 |
__nBytes -= 4; |
|
1821 |
} |
|
1822 |
while (__nBytes) { |
|
1823 |
__this = __bp[0]; |
|
1824 |
__newCarry = __this >> 7; |
|
1825 |
__bp[0] = (__this << 1) | __carry; |
|
1826 |
__carry = __newCarry; |
|
1827 |
__bp ++; |
|
1828 |
__nBytes--; |
|
1829 |
} |
|
1830 |
RETURN (self); |
|
1890 | 1831 |
} |
1832 |
%}. |
|
1833 |
self primitiveFailed |
|
1834 |
||
1835 |
" |
|
1836 |
100000 asLargeInteger mul2 |
|
1837 |
" |
|
1838 |
||
1839 |
"Modified: 5.11.1996 / 16:37:32 / cg" |
|
68 | 1840 |
! |
1841 |
||
1842 |
mul256 |
|
1843 |
"destructively multiply the receiver by 256. |
|
1844 |
private - used for division only" |
|
1845 |
||
1846 |
|newDigits| |
|
1847 |
||
359 | 1848 |
newDigits := ByteArray uninitializedNew:(digitByteArray size + 1). |
68 | 1849 |
newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
1850 |
newDigits at:1 put:0. |
|
1851 |
digitByteArray := newDigits |
|
701 | 1852 |
! |
68 | 1853 |
|
701 | 1854 |
numberOfDigits:nDigits |
2001 | 1855 |
"/ digitByteArray := ByteArray uninitializedNew:nDigits. |
701 | 1856 |
digitByteArray := ByteArray new:nDigits. |
1857 |
sign := 1. |
|
68 | 1858 |
! |
1859 |
||
701 | 1860 |
setDigits:digits |
1861 |
digitByteArray := digits. |
|
1862 |
sign := 1. |
|
68 | 1863 |
! |
1864 |
||
701 | 1865 |
sign:aNumber |
1866 |
sign := aNumber |
|
1 | 1867 |
! ! |
1868 |
||
1869 |
!LargeInteger methodsFor:'testing'! |
|
1870 |
||
701 | 1871 |
even |
1872 |
"return true if the receiver is even" |
|
1 | 1873 |
|
701 | 1874 |
^ (digitByteArray at:1) even |
1 | 1875 |
! |
1876 |
||
68 | 1877 |
negative |
1878 |
"return true, if the receiver is < 0" |
|
1879 |
||
1880 |
^ (sign < 0) |
|
1881 |
! |
|
1882 |
||
1 | 1883 |
odd |
1884 |
"return true if the receiver is odd" |
|
1885 |
||
1983 | 1886 |
^ (digitByteArray at:1) odd |
1887 |
||
1888 |
"Modified: 18.11.1996 / 22:19:19 / cg" |
|
1 | 1889 |
! |
1890 |
||
1891 |
positive |
|
1892 |
"return true, if the receiver is >= 0" |
|
1893 |
||
1894 |
^ (sign >= 0) |
|
1895 |
! |
|
1896 |
||
701 | 1897 |
sign |
1898 |
"return the sign of the receiver" |
|
1899 |
||
1900 |
^ sign |
|
1901 |
! |
|
1902 |
||
1 | 1903 |
strictlyPositive |
1904 |
"return true, if the receiver is > 0" |
|
1905 |
||
1906 |
^ (sign > 0) |
|
1907 |
! ! |
|
1908 |
||
1794 | 1909 |
!LargeInteger class methodsFor:'documentation'! |
1 | 1910 |
|
701 | 1911 |
version |
2521 | 1912 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.44 1997-04-02 21:34:41 cg Exp $' |
1 | 1913 |
! ! |