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