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