author | Claus Gittinger <cg@exept.de> |
Wed, 02 Jun 1999 14:56:46 +0200 | |
changeset 4254 | 3846b94301b8 |
parent 4252 | 860133fce64f |
child 4255 | 73212f63b6a6 |
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 |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
43 |
The implementation is not completely tuned for high performance |
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
44 |
(more key-methods should be rewritten as primitives), |
88 | 45 |
|
46 |
LargeIntegers are usually not created explicitely, but result from |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
47 |
SmallInteger arithmetic (overflowing the SmallInteger range). |
357 | 48 |
Also, results of LargeInteger operations are converted to back to |
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
49 |
SmallIntegers, when possible (see #compressed). |
92 | 50 |
|
51 |
In contrast to ST-80, there is only one class for LargeIntegers, keeping |
|
52 |
the sign as an instance variable (ST-80 has LargePositiveInteger and |
|
53 |
LargeNegativeInteger). This may change. |
|
1295 | 54 |
|
55 |
[author:] |
|
4211 | 56 |
Claus Gittinger |
1556 | 57 |
|
58 |
[see also:] |
|
4211 | 59 |
Number |
60 |
Float Fraction FixedPoint |
|
61 |
SmallInteger |
|
77 | 62 |
" |
2923 | 63 |
! |
64 |
||
65 |
testing |
|
66 |
" |
|
67 |
test largeInt multiplication & division |
|
68 |
(at least, test if they have not both errors which anull each other ;-) |
|
69 |
||
70 |
|last v| |
|
71 |
||
2952 | 72 |
v := last := 1. |
2923 | 73 |
2 to:3000 do:[:i | |
74 |
i printCR. |
|
2952 | 75 |
v := v * i. |
2923 | 76 |
(v / i) ~= last ifTrue:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
77 |
self halt |
2923 | 78 |
]. |
79 |
last := v. |
|
80 |
] |
|
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
81 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
82 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
83 |
test addition, subtraction: |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
84 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
85 |
SmallInteger maxVal -> 1073741823 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
86 |
SmallInteger maxVal + 1 -> 1073741824 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
87 |
SmallInteger maxVal + 2 -> 1073741825 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
88 |
(SmallInteger maxVal + 2) - 2 -> 1073741823 |
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 |
SmallInteger minVal -> -1073741824 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
91 |
SmallInteger minVal - 1 -> -1073741825 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
92 |
SmallInteger minVal - 2 -> -1073741826 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
93 |
(SmallInteger minVal - 2) + 2 -> -1073741824 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
94 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
95 |
1234567890 + 10 -> 1234567900 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
96 |
1111111111 + 1111111111 -> 2222222222 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
97 |
1111111111 - 1111111111 -> 0 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
98 |
1111111111 - 2222222222 -> -1111111111 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
99 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
100 |
1111111111 * 2 -> 2222222222 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
101 |
1111111111 * -2 -> -2222222222 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
102 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
103 |
1111111111 * 1111111111 -> 1234567900987654321 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
104 |
1234567900987654321 // 1111111111 -> 1111111111 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
105 |
1234567900987654321 \\ 1111111111 -> 0 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
106 |
1234567900987654322 \\ 1111111111 -> 1 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
107 |
1234567900987654421 \\ 1111111111 -> 100 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
108 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
109 |
|
4171 | 110 |
addition: |
111 |
(16rFFFFFFF0 + 1 ) hexPrintString -> 'FFFFFFF1' |
|
112 |
(16rFFFFFFFF + 1 ) hexPrintString -> '100000000' |
|
113 |
(16rFFFFFFFF + 2 ) hexPrintString -> '100000001' |
|
114 |
(16rFFFFFFFF + 16rFFFFFF ) hexPrintString -> '100FFFFFE' |
|
115 |
(16rFFFFFFFF + 16rFFFFFFFF) hexPrintString -> '1FFFFFFFE' |
|
116 |
||
117 |
20 factorial -> 2432902008176640000 |
|
118 |
20 factorial + 10000 -> 2432902008176650000 |
|
119 |
20 factorial hexPrintString -> '21C3677C82B40000' |
|
120 |
(20 factorial + 16rFFFF) hexPrintString -> '21C3677C82B4FFFF' |
|
121 |
(20 factorial + 16rFFFFFF) hexPrintString -> '21C3677C83B3FFFF' |
|
122 |
||
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
123 |
test comparison: |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
124 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
125 |
-1234567890 > -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
126 |
-1234567890 >= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
127 |
-1234567890 < -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
128 |
-1234567890 <= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
129 |
-1234567890 = -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
130 |
-1234567890 ~= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
131 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
132 |
-1234567890 > -1234567891 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
133 |
-1234567890 >= -1234567891 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
134 |
-1234567890 < -1234567891 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
135 |
-1234567890 <= -1234567891 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
136 |
-1234567890 = -1234567891 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
137 |
-1234567890 ~= -1234567891 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
138 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
139 |
-1234567891 > -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
140 |
-1234567891 >= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
141 |
-1234567891 < -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
142 |
-1234567891 <= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
143 |
-1234567891 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
144 |
-1234567891 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
145 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
146 |
1234567890 > -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
147 |
1234567890 >= -1234567890 true |
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 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
150 |
1234567890 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
151 |
1234567890 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
152 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
153 |
-1234567890 > 1234567890 false |
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 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
157 |
-1234567890 = 1234567890 false |
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 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
160 |
1234567890 > 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
161 |
1234567890 >= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
162 |
1234567890 < 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
163 |
1234567890 <= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
164 |
1234567890 = 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
165 |
1234567890 ~= 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
166 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
167 |
-1234567890 > -10 false |
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 true |
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 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
172 |
-1234567890 ~= -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
173 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
174 |
-1234567890 > 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
175 |
-1234567890 >= 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
176 |
-1234567890 < 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
177 |
-1234567890 <= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
178 |
-1234567890 = 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
179 |
-1234567890 ~= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
180 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
181 |
-10 > -1234567890 true |
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 false |
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 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
187 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
188 |
10 > -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
189 |
10 >= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
190 |
10 < -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
191 |
10 <= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
192 |
10 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
193 |
10 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
194 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
195 |
1234567890 > -10 true |
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 false |
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 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
201 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
202 |
1234567890 > 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
203 |
1234567890 >= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
204 |
1234567890 < 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
205 |
1234567890 <= 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
206 |
1234567890 = 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
207 |
1234567890 ~= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
208 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
209 |
-10 > 1234567890 false |
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 true |
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 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
214 |
-10 ~= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
215 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
216 |
10 > 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
217 |
10 >= 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
218 |
10 < 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
219 |
10 <= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
220 |
10 = 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
221 |
10 ~= 1234567890 true |
2923 | 222 |
" |
77 | 223 |
! ! |
1 | 224 |
|
1794 | 225 |
!LargeInteger class methodsFor:'instance creation'! |
1 | 226 |
|
227 |
new |
|
228 |
"catch creation message" |
|
229 |
||
230 |
self error:'LargeIntegers cannot be created with new' |
|
231 |
! |
|
232 |
||
701 | 233 |
new:numberOfDigits |
234 |
"catch creation message" |
|
2 | 235 |
|
701 | 236 |
self error:'LargeIntegers cannot be created with new' |
1 | 237 |
! |
238 |
||
701 | 239 |
value:aSmallInteger |
240 |
"create and return a new LargeInteger with value taken from |
|
241 |
the argument, aSmallInteger. |
|
3431 | 242 |
Notice: |
4162 | 243 |
this should be only used internally, since such small |
244 |
largeIntegers do not normally occur in the system. |
|
245 |
(They are used by myself) |
|
3431 | 246 |
May change/be removed without notice." |
247 |
||
701 | 248 |
^ self basicNew value:aSmallInteger |
249 |
||
250 |
"LargeInteger value:3689" |
|
3438
2e64ef848871
oops - last change was not good
Claus Gittinger <cg@exept.de>
parents:
3431
diff
changeset
|
251 |
|
2e64ef848871
oops - last change was not good
Claus Gittinger <cg@exept.de>
parents:
3431
diff
changeset
|
252 |
"Modified: / 8.5.1998 / 21:40:41 / cg" |
701 | 253 |
! ! |
254 |
||
1794 | 255 |
!LargeInteger class methodsFor:'queries'! |
701 | 256 |
|
257 |
isBuiltInClass |
|
1264 | 258 |
"return true if this class is known by the run-time-system. |
259 |
Here, true is returned." |
|
701 | 260 |
|
261 |
^ true |
|
1264 | 262 |
|
263 |
"Modified: 23.4.1996 / 15:59:21 / cg" |
|
1 | 264 |
! ! |
265 |
||
68 | 266 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 267 |
|
701 | 268 |
* aNumber |
269 |
"return the product of the receiver and the argument, aNumber" |
|
270 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
271 |
|otherSign numberClass| |
701 | 272 |
|
273 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
274 |
||
275 |
" |
|
276 |
this is the common case, multiplying with SmallInteger. |
|
277 |
Use a special method for this case ... |
|
278 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
279 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
701 | 280 |
^ self productFromInteger:aNumber |
281 |
]. |
|
282 |
||
283 |
" |
|
284 |
if the argument is not a largeInteger, coerce |
|
285 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
286 |
(numberClass == self class) ifFalse:[ |
701 | 287 |
^ self retry:#* coercing:aNumber |
288 |
]. |
|
289 |
||
290 |
otherSign := aNumber sign. |
|
291 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
292 |
(otherSign == 0) ifTrue:[^ 0]. |
|
293 |
^ (self absMul:aNumber) sign:-1 |
|
294 |
! |
|
295 |
||
92 | 296 |
+ aNumber |
297 |
"return the sum of the receiver and the argument, aNumber" |
|
298 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
299 |
|otherSign numberClass| |
92 | 300 |
|
301 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
302 |
||
303 |
" |
|
304 |
this is the common case, adding a SmallInteger. |
|
305 |
Use a special method for this case ... |
|
306 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
307 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
308 |
^ self sumFromInteger:aNumber |
92 | 309 |
]. |
310 |
||
311 |
" |
|
312 |
if the argument is not a largeInteger, coerce |
|
313 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
314 |
(numberClass == self class) ifFalse:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
315 |
^ self retry:#+ coercing:aNumber |
92 | 316 |
]. |
317 |
||
318 |
otherSign := aNumber sign. |
|
319 |
(sign > 0) ifTrue:[ |
|
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
320 |
"I am positive" |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
321 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber sign:1]. |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
322 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber sign:1]. |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
323 |
^ self |
92 | 324 |
]. |
325 |
"I am negative" |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
326 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self sign:1]. |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
327 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber sign:-1]. |
92 | 328 |
^ self |
357 | 329 |
|
330 |
" |
|
331 |
SmallInteger maxVal |
|
332 |
SmallInteger maxVal + 1 |
|
333 |
SmallInteger maxVal + 2 |
|
334 |
SmallInteger minVal |
|
335 |
SmallInteger minVal - 1 |
|
336 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
337 |
|
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
338 |
"Modified: / 9.1.1998 / 13:26:28 / cg" |
92 | 339 |
! |
340 |
||
341 |
- aNumber |
|
342 |
"return the difference of the receiver and the argument, aNumber" |
|
343 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
344 |
|otherSign numberClass| |
92 | 345 |
|
346 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
347 |
||
348 |
" |
|
349 |
this is the common case, subtracting a SmallInteger. |
|
350 |
Use a special method for this case ... |
|
351 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
352 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
4251 | 353 |
sign > 0 ifTrue:[ |
354 |
aNumber > 0 ifTrue:[ |
|
355 |
^ self absFastMinus:aNumber sign:1 |
|
356 |
]. |
|
357 |
^ self absFastPlus:aNumber sign:1 |
|
358 |
]. |
|
359 |
aNumber > 0 ifTrue:[ |
|
360 |
^ self absFastPlus:aNumber sign:-1 |
|
361 |
]. |
|
362 |
^ self absFastMinus:aNumber sign:-1 |
|
92 | 363 |
]. |
364 |
||
365 |
" |
|
366 |
if the argument is not a largeInteger, coerce |
|
367 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
368 |
(numberClass == self class) ifFalse:[ |
4251 | 369 |
^ self retry:#- coercing:aNumber |
92 | 370 |
]. |
371 |
||
372 |
otherSign := aNumber sign. |
|
373 |
(sign > 0) ifTrue:[ |
|
4251 | 374 |
"I am positive" |
375 |
(otherSign > 0) ifTrue:[ |
|
376 |
"+large - +large" |
|
377 |
^ self absMinus:aNumber sign:1 |
|
378 |
]. |
|
379 |
(otherSign < 0) ifTrue:[ |
|
380 |
"+large - -large" |
|
381 |
^ self absPlus:aNumber sign:1 |
|
382 |
]. |
|
383 |
"should not happen" |
|
384 |
^ self |
|
92 | 385 |
]. |
386 |
"I am negative" |
|
4251 | 387 |
(otherSign > 0) ifTrue:[ |
388 |
"-large - +large" |
|
389 |
^ self absPlus:aNumber sign:-1 |
|
390 |
]. |
|
391 |
(otherSign < 0) ifTrue:[ |
|
392 |
"-large - -large" |
|
393 |
^ self absMinus:aNumber sign:-1 |
|
394 |
]. |
|
92 | 395 |
^ self |
396 |
||
397 |
" |
|
398 |
12345678901234567890 - 0 |
|
357 | 399 |
12345678901234567890 - 1 |
92 | 400 |
12345678901234567890 - -1 |
401 |
-12345678901234567890 - 1 |
|
402 |
-12345678901234567890 - -1 |
|
403 |
||
404 |
12345678901234567890 - 12345678901234567880 |
|
405 |
12345678901234567890 - 12345000000000000000 |
|
406 |
12345678901234567890 - -87654321098765432110 |
|
407 |
-12345678901234567890 - 87654321098765432110 |
|
408 |
-12345678901234567890 - -12345678901234567880 |
|
409 |
-12345678901234567890 - -12345678901234567980 |
|
410 |
" |
|
1794 | 411 |
|
412 |
"Modified: 20.10.1996 / 18:42:16 / cg" |
|
92 | 413 |
! |
414 |
||
701 | 415 |
/ aNumber |
1065 | 416 |
"return the quotient of the receiver and the argument, aNumber" |
92 | 417 |
|
701 | 418 |
aNumber isInteger ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
419 |
^ Fraction numerator:self |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
420 |
denominator:aNumber |
92 | 421 |
]. |
422 |
||
701 | 423 |
"this is a q&d hack - we loose lots of precision here ..." |
424 |
^ (self asFloat / aNumber asFloat) |
|
2793 | 425 |
|
426 |
"Modified: 28.7.1997 / 19:07:55 / cg" |
|
92 | 427 |
! |
428 |
||
68 | 429 |
// aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
430 |
"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
|
431 |
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
|
432 |
if the operands signs differ." |
1 | 433 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
434 |
|cls divMod quo abs "{ Class: SmallInteger }" n| |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
435 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
436 |
cls := aNumber class. |
1 | 437 |
|
92 | 438 |
" |
439 |
this is the common case, dividing by a SmallInteger. |
|
440 |
Use a special method for this case ... |
|
441 |
" |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
442 |
(cls == SmallInteger) ifTrue:[ |
4162 | 443 |
abs := aNumber. |
444 |
abs := abs abs. |
|
445 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
|
446 |
divMod := self absFastDivMod:abs. |
|
447 |
] ifFalse:[ |
|
448 |
n := abs asLargeInteger. |
|
449 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
450 |
] ifFalse:[ |
4162 | 451 |
" |
452 |
if the argument is not a largeInteger, coerce |
|
453 |
" |
|
454 |
(cls == self class) ifFalse:[ |
|
455 |
^ self retry:#// coercing:aNumber |
|
456 |
]. |
|
457 |
n := aNumber |
|
1 | 458 |
]. |
68 | 459 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
460 |
divMod isNil ifTrue:[ |
4162 | 461 |
divMod := self absDivMod:n. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
462 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
463 |
quo := divMod at:1. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
464 |
(sign == aNumber sign) ifFalse:[ |
4162 | 465 |
"/ adjust for truncation if negative and there is a remainder ... |
466 |
quo := quo sign:-1. |
|
467 |
(divMod at:2) == 0 ifFalse:[ |
|
468 |
^ quo - 1 |
|
469 |
]. |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
470 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
471 |
^ quo |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
472 |
|
92 | 473 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
474 |
(9000000000 // 4000000000) = (900 // 400) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
475 |
(-9000000000 // 4000000000) = (-900 // 400) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
476 |
(9000000000 // -4000000000) = (900 // -400) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
477 |
(-9000000000 // -4000000000) = (-900 // -400) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
478 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
479 |
16rfffffffff // 16r01ffffff = 2048 ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
480 |
16rfffffffff // 16r00ffffff = 4096 ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
481 |
16rfffffffff // 16r001fffff = 32768 ifFalse:[self halt]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
482 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
483 |
900 quo: 400 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
484 |
-900 quo: 400 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
485 |
900 quo: -400 |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
486 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
487 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
488 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
489 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
490 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
491 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
492 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
493 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
494 |
"Modified: / 5.11.1996 / 16:39:36 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
495 |
"Modified: / 27.4.1999 / 19:50:26 / stefan" |
68 | 496 |
! |
497 |
||
498 |
\\ aNumber |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
499 |
"Answer the integer remainder m defined by division with truncation toward |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
500 |
negative infinity. The remainder has the same sign as aNumber. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
501 |
m < |aNumber| AND there is an integer k with (k * aNumber + m) = self |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
502 |
Compare with #rem:" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
503 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
504 |
|abs rem negativeDivisor| |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
505 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
506 |
aNumber negative ifTrue:[ |
4162 | 507 |
negativeDivisor := true. |
508 |
abs := aNumber negated. |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
509 |
] ifFalse:[ |
4162 | 510 |
negativeDivisor := false. |
511 |
abs := aNumber. |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
512 |
]. |
92 | 513 |
|
514 |
" |
|
515 |
this is the common case, dividing by a SmallInteger. |
|
516 |
Use a special method for this case ... |
|
517 |
" |
|
68 | 518 |
(aNumber class == SmallInteger) ifTrue:[ |
4162 | 519 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
520 |
rem := (self absFastDivMod:abs) at:2. |
|
521 |
] ifFalse:[ |
|
522 |
rem := self absMod:abs asLargeInteger |
|
523 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
524 |
] ifFalse:[ |
4162 | 525 |
" |
526 |
if the argument is not a largeInteger, coerce |
|
527 |
" |
|
528 |
(aNumber class == self class) ifFalse:[ |
|
529 |
^ self retry:#\\ coercing:aNumber |
|
530 |
]. |
|
531 |
||
532 |
rem := self absMod:abs. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
533 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
534 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
535 |
rem = 0 ifFalse:[ |
4162 | 536 |
negativeDivisor ifTrue:[ |
537 |
rem := rem sign:-1 |
|
538 |
]. |
|
539 |
(self negative ~~ negativeDivisor) ifTrue:[ |
|
540 |
"different sign, so remainder would have been negative. |
|
541 |
rem has been rounded toward zero, this code will simulate |
|
542 |
rounding to negative infinity." |
|
543 |
||
544 |
rem := aNumber - rem. |
|
545 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
546 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
547 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
548 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
549 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
550 |
(9000000000 \\ 4000000000) = (900 \\ 400 * 10000000) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
551 |
(-9000000000 \\ 4000000000) = (-900 \\ 400 * 10000000) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
552 |
(9000000000 \\ -4000000000) = (900 \\ -400 * 10000000) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
553 |
(-9000000000 \\ -4000000000) = (-900 \\ -400 * 10000000)ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
554 |
(16000000000 \\ 4000000000) = (1600 \\ 400 * 10000000) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
555 |
(-16000000000 \\ 4000000000) = (-1600 \\ 400 * 10000000) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
556 |
(16000000000 \\ -4000000000) = (1600 \\ -400 * 10000000) ifFalse:[self halt]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
557 |
(-16000000000 \\ -4000000000) = (-1600 \\ -400 * 10000000) ifFalse:[self halt]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
558 |
|
1890 | 559 |
9000000000 \\ 7 |
560 |
-9000000000 \\ 7 |
|
561 |
9000000000 \\ -7 |
|
562 |
-9000000000 \\ -7 |
|
563 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
564 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
565 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
566 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
567 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
568 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
569 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
570 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
571 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
572 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
573 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
574 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
575 |
"Modified: / 5.11.1996 / 17:10:10 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
576 |
"Modified: / 27.4.1999 / 20:03:40 / stefan" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
577 |
! |
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 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
580 |
"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
|
581 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
582 |
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
|
583 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
584 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
585 |
|cls n| |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
586 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
587 |
cls := aNumber class. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
588 |
(cls == SmallInteger) ifTrue:[ |
4162 | 589 |
" |
590 |
this is the common case, dividing by a SmallInteger. |
|
591 |
Use a special method for this case ... |
|
592 |
" |
|
593 |
(aNumber between:1 and:16r00ffffff) ifTrue:[ |
|
594 |
^ self absFastDivMod:aNumber abs. |
|
595 |
]. |
|
596 |
n := aNumber asLargeInteger. |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
597 |
] ifFalse:[ |
4162 | 598 |
(cls == self class) ifFalse:[ |
599 |
^ super divMod:aNumber |
|
600 |
]. |
|
601 |
n := aNumber. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
602 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
603 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
604 |
^ self absDivMod:n abs |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
605 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
606 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
607 |
9000000000 // 4000000000 => 2 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
608 |
9000000000 \\ 4000000000 => 1000000000 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
609 |
9000000000 divMod: 4000000000 => #(2 1000000000) |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
610 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
611 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
612 |
"Created: / 29.10.1996 / 21:22:05 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
613 |
"Modified: / 27.4.1999 / 19:48:58 / stefan" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
614 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
615 |
|
1210 | 616 |
negated |
617 |
"return an integer with value negated from the receivers value." |
|
618 |
||
2952 | 619 |
|newNumber sz| |
1210 | 620 |
|
621 |
(sign == 0) ifTrue:[^ 0]. |
|
622 |
||
623 |
" |
|
624 |
special case for SmallInteger minVal |
|
625 |
" |
|
626 |
sign == 1 ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
627 |
sz := digitByteArray size. |
2952 | 628 |
%{ |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
629 |
int idx; |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
630 |
unsigned char *bp; |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
631 |
|
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
632 |
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
|
633 |
idx = __intVal(sz); |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
634 |
|
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
635 |
while ((idx > 1) && (bp[idx-1] == 0)) idx--; |
3431 | 636 |
|
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
637 |
if (idx == sizeof(INT)) { |
3431 | 638 |
#if defined(alpha64) |
639 |
if ( ((unsigned INT *)bp)[0] == 0x4000000000000000L) |
|
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
640 |
#else |
3431 | 641 |
# if defined(i386) /* actually: LSBFIRST */ |
642 |
if ( ((unsigned INT *)bp)[0] == 0x40000000) |
|
643 |
# else |
|
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
644 |
/* |
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
645 |
* generic code |
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
646 |
*/ |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
647 |
if ((bp[idx-1] == 0x40) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
648 |
&& (bp[idx-2] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
649 |
&& (bp[idx-3] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
650 |
&& (bp[idx-4] == 0) |
3431 | 651 |
# ifdef alpha64 |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
652 |
&& (bp[idx-5] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
653 |
&& (bp[idx-6] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
654 |
&& (bp[idx-7] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
655 |
&& (bp[idx-8] == 0) |
3431 | 656 |
# endif |
657 |
) |
|
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
658 |
# endif |
2952 | 659 |
#endif |
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
660 |
{ |
2952 | 661 |
RETURN (__MKSMALLINT(_MIN_INT)); |
662 |
} |
|
663 |
} |
|
664 |
%}. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
665 |
"/ sz == 4 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
666 |
"/ (digitByteArray at:1) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
667 |
"/ (digitByteArray at:2) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
668 |
"/ (digitByteArray at:3) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
669 |
"/ (digitByteArray at:4) == 16r40 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
670 |
"/ ^ SmallInteger minVal |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
671 |
"/ ]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
672 |
"/ ] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
673 |
"/ ] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
674 |
"/ ] |
2952 | 675 |
"/ ] |
1210 | 676 |
]. |
677 |
newNumber := self shallowCopy. |
|
678 |
newNumber sign:(sign negated). |
|
679 |
^ newNumber |
|
680 |
! |
|
681 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
682 |
quo:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
683 |
"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
|
684 |
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
|
685 |
truncates toward negative infinity). |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
686 |
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
|
687 |
different from the args sign" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
688 |
|
2521 | 689 |
|otherSign quo abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
690 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
691 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
692 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
693 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
694 |
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
|
695 |
Use a special method for this case ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
696 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
697 |
(aNumber class == SmallInteger) ifTrue:[ |
4162 | 698 |
abs := aNumber. |
699 |
abs := abs abs. |
|
700 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
|
701 |
quo := (self absFastDivMod:abs) at:1. |
|
702 |
(sign == otherSign) ifTrue:[^ quo]. |
|
703 |
^ quo sign:-1 |
|
704 |
] |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
705 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
706 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
707 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
708 |
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
|
709 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
710 |
(aNumber class == self class) ifFalse:[ |
4162 | 711 |
^ self retry:#quo: coercing:aNumber |
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 |
sign < 0 ifTrue:[ |
4162 | 715 |
(sign == otherSign) ifTrue:[^ (self absDivMod:aNumber negated) at:1]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
716 |
] ifFalse:[ |
4162 | 717 |
(sign == otherSign) ifTrue:[^ (self absDivMod:aNumber) at:1]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
718 |
]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
719 |
^ ((self absDivMod:aNumber) at:1) sign:-1 |
927
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 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
722 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
723 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
724 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
725 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
726 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
727 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
728 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
729 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
730 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
731 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
732 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
733 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
734 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
735 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
736 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
737 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
738 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
739 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
740 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
741 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
742 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
743 |
"Modified: / 5.11.1996 / 14:14:17 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
744 |
"Modified: / 27.4.1999 / 20:01:22 / stefan" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
745 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
746 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
747 |
rem:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
748 |
"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
|
749 |
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
|
750 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
751 |
|rem abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
752 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
753 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
754 |
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
|
755 |
Use special code for this case ... |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
756 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
757 |
(aNumber class == SmallInteger) ifTrue:[ |
4162 | 758 |
abs := aNumber. |
759 |
abs := abs abs. |
|
760 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
|
761 |
rem := (self absFastDivMod:abs) at:2. |
|
762 |
] ifFalse:[ |
|
763 |
rem := self absMod:abs asLargeInteger |
|
764 |
]. |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
765 |
] ifFalse:[ |
4162 | 766 |
" |
767 |
if the argument is not a largeInteger, coerce |
|
768 |
" |
|
769 |
(aNumber class == self class) ifFalse:[ |
|
770 |
^ self retry:#rem coercing:aNumber |
|
771 |
]. |
|
772 |
||
773 |
rem := self absMod:aNumber |
|
1 | 774 |
]. |
92 | 775 |
|
776 |
sign < 0 ifTrue:[ |
|
4162 | 777 |
^ rem sign:-1 |
1 | 778 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
779 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
780 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
781 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
782 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
783 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
784 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
785 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
786 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
787 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
788 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
789 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
790 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
791 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
792 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
793 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
794 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
795 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
796 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
797 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
798 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
799 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
800 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
801 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
802 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
803 |
"Modified: / 5.11.1996 / 14:02:59 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
804 |
"Modified: / 29.4.1999 / 11:26:51 / stefan" |
1 | 805 |
! ! |
806 |
||
2855 | 807 |
!LargeInteger methodsFor:'bit operators'! |
808 |
||
809 |
lowBit |
|
810 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
811 |
starts at 1 for the least significant bit. Returns -1 if no bit is set. |
|
812 |
For negative numbers, the low bit of my absolute value is returned. |
|
813 |
Redefined here for more performance of the gcd: algorithm, which |
|
814 |
is used when big fractions are reduced." |
|
815 |
||
816 |
|sz "{ Class: SmallInteger }" |
|
817 |
byte| |
|
818 |
||
819 |
sz := digitByteArray size. |
|
820 |
1 to:sz do:[:digitIndex | |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
821 |
(byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
822 |
^ (digitIndex-1)*8 + byte lowBit |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
823 |
] |
2855 | 824 |
]. |
825 |
^ -1 |
|
826 |
||
827 |
" |
|
828 |
(1 bitShift:30) lowBit |
|
829 |
(1 bitShift:30) highBit |
|
830 |
(1 bitShift:31) lowBit |
|
831 |
(1 bitShift:31) highBit |
|
832 |
(1 bitShift:32) lowBit |
|
833 |
(1 bitShift:32) highBit |
|
834 |
(1 bitShift:33) lowBit |
|
835 |
(1 bitShift:33) highBit |
|
836 |
(1 bitShift:64) lowBit |
|
837 |
(1 bitShift:64) highBit |
|
838 |
(1 bitShift:1000) lowBit |
|
839 |
(1 bitShift:1000) highBit |
|
840 |
((1 bitShift:64)-1) lowBit |
|
841 |
((1 bitShift:64)-1) highBit |
|
842 |
" |
|
843 |
||
844 |
"Modified: 14.8.1997 / 11:55:34 / cg" |
|
845 |
! ! |
|
846 |
||
701 | 847 |
!LargeInteger methodsFor:'byte access'! |
848 |
||
849 |
digitAt:index |
|
850 |
"return 8 bits of value, starting at byte index" |
|
851 |
||
852 |
index > digitByteArray size ifTrue:[^ 0]. |
|
853 |
^ digitByteArray at:index |
|
854 |
! |
|
855 |
||
856 |
digitAt:index put:aByte |
|
857 |
"set the 8 bits, index is a byte index" |
|
858 |
||
859 |
digitByteArray at:index put:aByte |
|
860 |
! |
|
861 |
||
3891 | 862 |
digitByteAt:index |
863 |
"return 8 bits of my signed value, starting at byte index. |
|
864 |
For positive receivers, this is the same as #digitAt:; |
|
865 |
for negative ones, the actual bit representation is returned." |
|
866 |
||
867 |
|t digits| |
|
868 |
sign < 0 ifFalse:[ |
|
4162 | 869 |
index > digitByteArray size ifTrue:[ |
870 |
^ 0 |
|
871 |
]. |
|
872 |
^ digitByteArray at:index. |
|
3891 | 873 |
]. |
874 |
"/ negative int - do 2's complement here |
|
875 |
||
876 |
t := self bitInvert + 1. |
|
877 |
t sign:1. |
|
878 |
digits := t digits. |
|
879 |
index > digits size ifTrue:[ |
|
4162 | 880 |
^ 16rFF |
3891 | 881 |
]. |
882 |
^ digits at:index. |
|
883 |
||
884 |
" |
|
885 |
16r11111111111111111111 negated digitByteAt:1 |
|
886 |
" |
|
887 |
||
888 |
"Created: / 25.10.1998 / 14:12:21 / cg" |
|
889 |
! |
|
890 |
||
701 | 891 |
digitLength |
2816 | 892 |
"return the number bytes used by this Integer. |
893 |
For negative receivers, the digitLength of its absolute value |
|
894 |
is returned." |
|
701 | 895 |
|
896 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
897 |
check if there is a 0-byte ... |
701 | 898 |
this allows to ask unnormalized LargeIntegers |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
899 |
for their digitLength |
701 | 900 |
" |
901 |
|l "{ Class: SmallInteger }" | |
|
92 | 902 |
|
701 | 903 |
l := digitByteArray size. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
904 |
[l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
905 |
l := l - 1. |
701 | 906 |
]. |
907 |
^ l |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
908 |
|
2816 | 909 |
"Modified: 31.7.1997 / 13:18:28 / cg" |
701 | 910 |
! |
911 |
||
912 |
digits |
|
4151 | 913 |
"return a byteArray filled with the receivers bits |
701 | 914 |
(8 bits of the absolute value per element)" |
915 |
||
916 |
^ digitByteArray |
|
4151 | 917 |
|
918 |
"Modified: / 5.5.1999 / 14:57:03 / stefan" |
|
701 | 919 |
! ! |
920 |
||
921 |
!LargeInteger methodsFor:'coercing & converting'! |
|
68 | 922 |
|
701 | 923 |
asFloat |
924 |
"return a Float with same value as myself. |
|
925 |
Since floats have a limited precision, you usually loose bits when |
|
926 |
doing this." |
|
927 |
||
928 |
|newFloat| |
|
929 |
||
930 |
newFloat := 0.0. |
|
931 |
(sign == 0) ifFalse:[ |
|
932 |
digitByteArray reverseDo:[:aDigit | |
|
933 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
934 |
]. |
|
935 |
(sign < 0) ifTrue:[ |
|
936 |
newFloat := newFloat negated |
|
345 | 937 |
] |
701 | 938 |
]. |
939 |
^ newFloat |
|
940 |
||
941 |
" |
|
942 |
1234567890 asFloat |
|
943 |
12345678901234567890 asFloat |
|
944 |
12345678901234567890 asFloat asInteger |
|
945 |
" |
|
946 |
! |
|
947 |
||
948 |
asLargeInteger |
|
949 |
"return a LargeInteger with same value as myself - thats me" |
|
950 |
||
951 |
^ self |
|
952 |
! |
|
953 |
||
954 |
asSmallInteger |
|
955 |
"return a SmallInteger with same value as myself - |
|
956 |
the result is invalid if the receivers value cannot |
|
957 |
be represented as a SmallInteger. |
|
958 |
Q: should we raise an exception if this happens ?" |
|
959 |
||
960 |
|value| |
|
961 |
||
962 |
value := 0. |
|
963 |
(sign == 0) ifFalse:[ |
|
964 |
digitByteArray reverseDo:[:aDigit | |
|
965 |
value := (value times:256) + aDigit |
|
345 | 966 |
]. |
701 | 967 |
(sign < 0) ifTrue:[ |
968 |
value := value negated |
|
345 | 969 |
] |
68 | 970 |
]. |
701 | 971 |
^ value |
972 |
! |
|
973 |
||
974 |
coerce:aNumber |
|
975 |
"return the argument as a LargeInteger" |
|
976 |
||
977 |
^ aNumber asLargeInteger |
|
978 |
! |
|
979 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
980 |
compressed |
701 | 981 |
"if the receiver can be represented as a SmallInteger, return |
2267 | 982 |
a SmallInteger with my value; |
983 |
otherwise remove leading zero bytes in the digitByte array |
|
984 |
and return the receiver" |
|
701 | 985 |
|
986 |
|index "{ Class: SmallInteger }" |
|
2267 | 987 |
val "{ Class: SmallInteger }" |
988 |
index0 |
|
989 |
| |
|
701 | 990 |
|
991 |
%{ /* NOCONTEXT */ |
|
992 |
OBJ t; |
|
993 |
||
1133 | 994 |
if (__INST(sign) == __MKSMALLINT(0)) { |
4230 | 995 |
RETURN (__MKSMALLINT(0)); |
701 | 996 |
} |
997 |
||
1133 | 998 |
t = __INST(digitByteArray); |
701 | 999 |
if (__isByteArray(t)) { |
4230 | 1000 |
unsigned char *_digitBytes = __ByteArrayInstPtr(t)->ba_element; |
1001 |
int _idx, _idx0; |
|
1002 |
INT _val; |
|
1003 |
||
1004 |
_idx = _idx0 = __byteArraySize(t); |
|
1005 |
while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) { |
|
1006 |
_idx--; |
|
1007 |
} |
|
2788 | 1008 |
#ifdef alpha64 |
4230 | 1009 |
switch (_idx) { |
1010 |
case 8: |
|
1011 |
_val = _digitBytes[7]; |
|
1012 |
if (_val <= 0x40) { |
|
1013 |
_val = (_val<<8); |
|
1014 |
_val = (_val + _digitBytes[6]) << 8; |
|
1015 |
_val = (_val + _digitBytes[5]) << 8; |
|
1016 |
_val = (_val + _digitBytes[4]) << 8; |
|
1017 |
_val = (_val + _digitBytes[3]) << 8; |
|
1018 |
_val = (_val + _digitBytes[2]) << 8; |
|
1019 |
_val = (_val + _digitBytes[1]) << 8; |
|
1020 |
_val += _digitBytes[0]; |
|
1021 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1022 |
_val = -_val; |
|
1023 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
|
1024 |
RETURN (__MKSMALLINT(_val)); |
|
1025 |
} |
|
1026 |
} |
|
1027 |
break; |
|
1028 |
case 7: |
|
1029 |
_val = (_digitBytes[6]<<8); |
|
1030 |
_val = (_val + _digitBytes[5]) << 8; |
|
1031 |
_val = (_val + _digitBytes[4]) << 8; |
|
1032 |
_val = (_val + _digitBytes[3]) << 8; |
|
1033 |
_val = (_val + _digitBytes[2]) << 8; |
|
1034 |
_val = (_val + _digitBytes[1]) << 8; |
|
1035 |
_val += _digitBytes[0]; |
|
1036 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1037 |
_val = -_val; |
|
1038 |
RETURN (__MKSMALLINT(_val)); |
|
1039 |
case 6: |
|
1040 |
_val = (_digitBytes[5]<<8); |
|
1041 |
_val = (_val + _digitBytes[4]) << 8; |
|
1042 |
_val = (_val + _digitBytes[3]) << 8; |
|
1043 |
_val = (_val + _digitBytes[2]) << 8; |
|
1044 |
_val = (_val + _digitBytes[1]) << 8; |
|
1045 |
_val += _digitBytes[0]; |
|
1046 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1047 |
_val = -_val; |
|
1048 |
RETURN (__MKSMALLINT(_val)); |
|
1049 |
case 5: |
|
1050 |
_val = (_digitBytes[4]<<8); |
|
1051 |
_val = (_val + _digitBytes[3]) << 8; |
|
1052 |
_val = (_val + _digitBytes[2]) << 8; |
|
1053 |
_val = (_val + _digitBytes[1]) << 8; |
|
1054 |
_val += _digitBytes[0]; |
|
1055 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1056 |
_val = -_val; |
|
1057 |
RETURN (__MKSMALLINT(_val)); |
|
1058 |
case 4: |
|
1059 |
_val = (_digitBytes[3]<<8); |
|
1060 |
_val = (_val + _digitBytes[2]) << 8; |
|
1061 |
_val = (_val + _digitBytes[1]) << 8; |
|
1062 |
_val += _digitBytes[0]; |
|
1063 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1064 |
_val = -_val; |
|
1065 |
RETURN (__MKSMALLINT(_val)); |
|
1066 |
case 3: |
|
1067 |
_val = (_digitBytes[2]<<8); |
|
1068 |
_val = (_val + _digitBytes[1]) << 8; |
|
1069 |
_val += _digitBytes[0]; |
|
1070 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1071 |
_val = -_val; |
|
1072 |
RETURN (__MKSMALLINT(_val)); |
|
1073 |
case 2: |
|
1074 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
1075 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1076 |
_val = -_val; |
|
1077 |
RETURN (__MKSMALLINT(_val)); |
|
1078 |
case 1: |
|
1079 |
_val = _digitBytes[0]; |
|
1080 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1081 |
_val = -_val; |
|
1082 |
RETURN (__MKSMALLINT(_val)); |
|
1083 |
case 0: |
|
1084 |
RETURN (__MKSMALLINT(0)); |
|
1085 |
||
1086 |
} |
|
2786 | 1087 |
#else |
4230 | 1088 |
if (_idx <= 4) { |
1089 |
if (_idx <= 2) { |
|
1090 |
if (_idx == 0) { |
|
1091 |
RETURN (__MKSMALLINT(0)); |
|
1092 |
} |
|
1093 |
if (_idx == 1) { |
|
1094 |
_val = _digitBytes[0]; |
|
1095 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1096 |
_val = -_val; |
|
1097 |
RETURN (__MKSMALLINT(_val)); |
|
1098 |
} |
|
1099 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
1100 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1101 |
_val = -_val; |
|
1102 |
RETURN (__MKSMALLINT(_val)); |
|
1103 |
} |
|
1104 |
if (_idx == 3) { |
|
1105 |
_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
1106 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1107 |
_val = -_val; |
|
1108 |
RETURN (__MKSMALLINT(_val)); |
|
1109 |
} |
|
1110 |
_val = _digitBytes[3]; |
|
1111 |
if (_val <= 0x40) { |
|
1112 |
_val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
1113 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1114 |
_val = -_val; |
|
1115 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
|
1116 |
RETURN (__MKSMALLINT(_val)); |
|
1117 |
} |
|
1118 |
} |
|
1119 |
} |
|
2786 | 1120 |
#endif |
701 | 1121 |
|
4230 | 1122 |
if (_idx == _idx0) { |
1123 |
RETURN (self); |
|
1124 |
} |
|
1125 |
||
1126 |
/* |
|
1127 |
* must copy & cut off some bytes |
|
1128 |
*/ |
|
1129 |
{ |
|
1130 |
OBJ newDigits; |
|
1131 |
OBJ oldDigits; |
|
1132 |
||
1133 |
/* |
|
1134 |
* careful - there is no context here to protect |
|
1135 |
* the receiver ... |
|
1136 |
*/ |
|
1137 |
__PROTECT__(self); |
|
1138 |
__PROTECT__(__INST(digitByteArray)); |
|
1139 |
newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx); |
|
1140 |
__UNPROTECT__(oldDigits); |
|
1141 |
__UNPROTECT__(self); |
|
1142 |
if (newDigits) { |
|
1143 |
bcopy(__ByteArrayInstPtr(oldDigits)->ba_element, |
|
1144 |
__ByteArrayInstPtr(newDigits)->ba_element, |
|
1145 |
_idx); |
|
1146 |
__INST(digitByteArray) = newDigits; __STORE(self, newDigits); |
|
1147 |
RETURN (self); |
|
1148 |
} |
|
1149 |
/* |
|
1150 |
* allocation failed ... |
|
1151 |
* ... fall through to trigger the error |
|
1152 |
*/ |
|
1153 |
} |
|
701 | 1154 |
} |
1155 |
%}. |
|
2267 | 1156 |
index0 := index := digitByteArray size. |
701 | 1157 |
[(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ |
4230 | 1158 |
index := index - 1 |
701 | 1159 |
]. |
1160 |
||
2267 | 1161 |
(index ~~ index0) ifTrue:[ |
4230 | 1162 |
digitByteArray := digitByteArray copyFrom:1 to:index |
701 | 1163 |
]. |
1164 |
^ self |
|
1165 |
! |
|
1166 |
||
1167 |
value:aSmallInteger |
|
3431 | 1168 |
"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
|
1169 |
This method will fail, if the argument is not a smallInteger. |
3431 | 1170 |
This should only be used internally, |
1171 |
since it will create an unnormalized LargeInt (by purpose) if asked for." |
|
701 | 1172 |
|
1173 |
|absValue |
|
1174 |
b1 "{ Class: SmallInteger }" |
|
1175 |
b2 "{ Class: SmallInteger }" |
|
2786 | 1176 |
b3 "{ Class: SmallInteger }" |
1177 |
b4 "{ Class: SmallInteger }" |
|
1178 |
b5 "{ Class: SmallInteger }" |
|
1179 |
b6 "{ Class: SmallInteger }" |
|
1180 |
b7 "{ Class: SmallInteger }"| |
|
92 | 1181 |
|
1182 |
" |
|
3431 | 1183 |
could have simply created a 4-byte largeinteger and normalize it. |
1184 |
The code below does the normalize right away, avoiding the |
|
701 | 1185 |
overhead of producing any intermediate byte-arrays (and the scanning) |
92 | 1186 |
" |
701 | 1187 |
(aSmallInteger == 0) ifTrue: [ |
4230 | 1188 |
digitByteArray := ByteArray with:0. |
1189 |
sign := 0. |
|
1190 |
^ self |
|
701 | 1191 |
]. |
3431 | 1192 |
|
701 | 1193 |
(aSmallInteger < 0) ifTrue: [ |
4230 | 1194 |
sign := -1. |
1195 |
absValue := aSmallInteger negated |
|
701 | 1196 |
] ifFalse: [ |
4230 | 1197 |
sign := 1. |
1198 |
absValue := aSmallInteger |
|
701 | 1199 |
]. |
3431 | 1200 |
|
701 | 1201 |
b1 := absValue bitAnd:16rFF. |
1202 |
absValue := absValue bitShift:-8. |
|
1203 |
absValue == 0 ifTrue:[ |
|
4230 | 1204 |
digitByteArray := ByteArray with:b1 |
701 | 1205 |
] ifFalse:[ |
4230 | 1206 |
b2 := absValue bitAnd:16rFF. |
1207 |
absValue := absValue bitShift:-8. |
|
1208 |
absValue == 0 ifTrue:[ |
|
1209 |
digitByteArray := ByteArray with:b1 with:b2 |
|
1210 |
] ifFalse:[ |
|
1211 |
b3 := absValue bitAnd:16rFF. |
|
1212 |
absValue := absValue bitShift:-8. |
|
1213 |
absValue == 0 ifTrue:[ |
|
1214 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
1215 |
] ifFalse:[ |
|
1216 |
b4 := absValue bitAnd:16rFF. |
|
1217 |
absValue := absValue bitShift:-8. |
|
1218 |
absValue == 0 ifTrue:[ |
|
1219 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 |
|
1220 |
] ifFalse:[ |
|
1221 |
b5 := absValue bitAnd:16rFF. |
|
1222 |
absValue := absValue bitShift:-8. |
|
1223 |
absValue == 0 ifTrue:[ |
|
1224 |
digitByteArray := ByteArray new:5. |
|
1225 |
digitByteArray at:1 put:b1. |
|
1226 |
digitByteArray at:2 put:b2. |
|
1227 |
digitByteArray at:3 put:b3. |
|
1228 |
digitByteArray at:4 put:b4. |
|
1229 |
digitByteArray at:5 put:b5. |
|
1230 |
] ifFalse:[ |
|
1231 |
b6 := absValue bitAnd:16rFF. |
|
1232 |
absValue := absValue bitShift:-8. |
|
1233 |
absValue == 0 ifTrue:[ |
|
1234 |
digitByteArray := ByteArray new:6. |
|
1235 |
digitByteArray at:1 put:b1. |
|
1236 |
digitByteArray at:2 put:b2. |
|
1237 |
digitByteArray at:3 put:b3. |
|
1238 |
digitByteArray at:4 put:b4. |
|
1239 |
digitByteArray at:5 put:b5. |
|
1240 |
digitByteArray at:6 put:b6. |
|
1241 |
] ifFalse:[ |
|
1242 |
b7 := absValue bitAnd:16rFF. |
|
1243 |
absValue := absValue bitShift:-8. |
|
1244 |
absValue == 0 ifTrue:[ |
|
1245 |
digitByteArray := ByteArray new:7. |
|
1246 |
digitByteArray at:1 put:b1. |
|
1247 |
digitByteArray at:2 put:b2. |
|
1248 |
digitByteArray at:3 put:b3. |
|
1249 |
digitByteArray at:4 put:b4. |
|
1250 |
digitByteArray at:5 put:b5. |
|
1251 |
digitByteArray at:6 put:b6. |
|
1252 |
digitByteArray at:7 put:b7. |
|
1253 |
] ifFalse:[ |
|
1254 |
digitByteArray := ByteArray new:8. |
|
1255 |
digitByteArray at:1 put:b1. |
|
1256 |
digitByteArray at:2 put:b2. |
|
1257 |
digitByteArray at:3 put:b3. |
|
1258 |
digitByteArray at:4 put:b4. |
|
1259 |
digitByteArray at:5 put:b5. |
|
1260 |
digitByteArray at:6 put:b6. |
|
1261 |
digitByteArray at:7 put:b7. |
|
1262 |
digitByteArray at:8 put:absValue. |
|
1263 |
] |
|
1264 |
] |
|
1265 |
] |
|
1266 |
] |
|
1267 |
] |
|
1268 |
] |
|
701 | 1269 |
] |
1890 | 1270 |
|
4230 | 1271 |
"Modified: / 26.5.1999 / 22:18:14 / cg" |
701 | 1272 |
! ! |
1273 |
||
1274 |
!LargeInteger methodsFor:'comparing'! |
|
1275 |
||
1276 |
< aNumber |
|
1277 |
"return true, if the argument, aNumber is greater than the receiver" |
|
1278 |
||
1279 |
|otherSign| |
|
1280 |
||
1281 |
(aNumber class == self class) ifFalse:[ |
|
1282 |
^ self retry:#< coercing:aNumber |
|
1283 |
]. |
|
1284 |
otherSign := aNumber sign. |
|
1285 |
||
1286 |
(sign > 0) ifTrue:[ |
|
1287 |
"I am positive" |
|
1288 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
1289 |
^ false "aNumber is <= 0" |
|
1290 |
]. |
|
1291 |
(sign == 0) ifTrue:[ |
|
1292 |
(otherSign > 0) ifTrue:[^ true]. |
|
1293 |
^ false |
|
1294 |
]. |
|
1295 |
"I am negative" |
|
1296 |
(otherSign > 0) ifTrue:[^ true]. |
|
1297 |
(otherSign == 0) ifTrue:[^ true]. |
|
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1298 |
^ (aNumber absLess:self) |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1299 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1300 |
"Modified: / 14.1.1998 / 12:51:42 / cg" |
92 | 1301 |
! |
1302 |
||
701 | 1303 |
= aNumber |
1304 |
"return true, if the argument, aNumber has the same value as |
|
1305 |
the receiver" |
|
1306 |
||
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1307 |
"/ speed up compare to 0 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1308 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1309 |
(aNumber == 0 and:[sign == 0]) ifTrue:[ |
4162 | 1310 |
^ true |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1311 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1312 |
|
701 | 1313 |
(aNumber class == self class) ifFalse:[ |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1314 |
"/ |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1315 |
"/ 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
|
1316 |
"/ 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
|
1317 |
"/ 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
|
1318 |
"/ |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1319 |
aNumber class == SmallInteger ifTrue:[^ false ]. |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1320 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1321 |
^ self retry:#= coercing:aNumber |
701 | 1322 |
]. |
1323 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
1324 |
^ self absEq:aNumber |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1325 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1326 |
"Modified: / 13.2.1998 / 11:43:15 / stefan" |
701 | 1327 |
! |
1328 |
||
1329 |
> aNumber |
|
1330 |
"return true, if the argument, aNumber is less than the receiver" |
|
1331 |
||
1332 |
|otherSign| |
|
1333 |
||
1334 |
(aNumber class == self class) ifFalse:[ |
|
1335 |
^ self retry:#> coercing:aNumber |
|
1336 |
]. |
|
1337 |
otherSign := aNumber sign. |
|
1338 |
||
1339 |
(sign > 0) ifTrue:[ |
|
1340 |
"I am positive" |
|
1341 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
1342 |
^ true "aNumber is <= 0" |
|
1343 |
]. |
|
1344 |
(sign == 0) ifTrue:[ |
|
1345 |
"I am zero" |
|
1346 |
(otherSign > 0) ifTrue:[^ false]. |
|
1347 |
^ true |
|
1348 |
]. |
|
1349 |
"I am negative" |
|
1350 |
(otherSign > 0) ifTrue:[^ false]. |
|
1351 |
(otherSign == 0) ifTrue:[^ false]. |
|
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1352 |
^ (self absLess:aNumber) |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1353 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1354 |
"Modified: / 14.1.1998 / 12:56:56 / cg" |
701 | 1355 |
! ! |
1356 |
||
1357 |
!LargeInteger methodsFor:'double dispatching'! |
|
1358 |
||
92 | 1359 |
differenceFromInteger:anInteger |
1360 |
"sent, when anInteger does not know how to subtract the receiver. |
|
1361 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
1362 |
||
1363 |
anInteger > 0 ifTrue:[ |
|
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1364 |
sign > 0 ifTrue:[ |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1365 |
^ self absFastMinus:anInteger sign:-1 |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1366 |
]. |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1367 |
^ self absFastPlus:anInteger sign:1 |
92 | 1368 |
]. |
1794 | 1369 |
anInteger == 0 ifTrue:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1370 |
^ self negated |
1794 | 1371 |
]. |
1372 |
sign > 0 ifTrue:[ |
|
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1373 |
^ self absFastPlus:anInteger negated sign:-1 |
1794 | 1374 |
]. |
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1375 |
^ self absFastMinus:anInteger sign:-1 |
92 | 1376 |
|
1377 |
" |
|
1378 |
12345678901234567890 |
|
1379 |
-12345678901234567890 |
|
1380 |
12345678901234567890 differenceFromInteger:0 |
|
1381 |
12345678901234567890 differenceFromInteger:1 |
|
1382 |
12345678901234567890 differenceFromInteger:-1 |
|
1383 |
-12345678901234567890 differenceFromInteger:1 |
|
1384 |
-12345678901234567890 differenceFromInteger:-1 |
|
1385 |
" |
|
1794 | 1386 |
|
1387 |
"Modified: 20.10.1996 / 18:41:54 / cg" |
|
68 | 1388 |
! |
1389 |
||
1390 |
productFromInteger:anInteger |
|
1391 |
"sent, when anInteger does not know how to multiply the receiver. |
|
1392 |
Return the product of the receiver and the argument, aSmallInteger" |
|
1393 |
||
1394 |
|num result |
|
1395 |
resultDigitByteArray |
|
1396 |
val "{ Class: SmallInteger }" |
|
1397 |
len "{ Class: SmallInteger }" |
|
1398 |
carry "{ Class: SmallInteger }" |
|
85 | 1399 |
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
|
1400 |
ok lResult| |
68 | 1401 |
|
1402 |
"multiplying by a small integer is done here" |
|
1403 |
||
1404 |
"trivial cases" |
|
1405 |
anInteger == 0 ifTrue:[^ 0]. |
|
1406 |
anInteger == 1 ifTrue:[^ self]. |
|
1407 |
||
1408 |
num := anInteger abs. |
|
1409 |
(num > 16r3FFFFF) ifTrue:[ |
|
4211 | 1410 |
"if num is too big (so that multiplying by a byte could create a Large)" |
1411 |
||
1412 |
^ anInteger retry:#* coercing:self |
|
68 | 1413 |
]. |
1414 |
||
1415 |
len := digitByteArray size. |
|
1416 |
||
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1417 |
val := num. |
2923 | 1418 |
val <= 16rFF ifTrue:[ |
4211 | 1419 |
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
|
1420 |
] ifFalse:[ |
4211 | 1421 |
val <= 16rFFFF ifTrue:[ |
1422 |
lResult := len + 2 |
|
1423 |
] ifFalse:[ |
|
1424 |
val <= 16rFFFFFF ifTrue:[ |
|
1425 |
lResult := len + 4. |
|
1426 |
] ifFalse:[ |
|
1427 |
lResult := len + 6. |
|
1428 |
] |
|
1429 |
] |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1430 |
]. |
2933 | 1431 |
resultDigitByteArray := ByteArray uninitializedNew:lResult. |
2897 | 1432 |
result := self class basicNew setDigits:resultDigitByteArray. |
1 | 1433 |
|
68 | 1434 |
anInteger < 0 ifTrue:[ |
4211 | 1435 |
sign > 0 ifTrue:[ |
1436 |
result sign:-1 |
|
1437 |
]. |
|
68 | 1438 |
] ifFalse:[ |
4211 | 1439 |
sign < 0 ifTrue:[ |
1440 |
result sign:sign |
|
1441 |
] |
|
68 | 1442 |
]. |
1443 |
||
85 | 1444 |
ok := false. |
1445 |
%{ |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1446 |
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
|
1447 |
|
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1448 |
if (__isSmallInteger(len) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1449 |
&& __isByteArray(__digitByteArray) |
85 | 1450 |
&& __isByteArray(resultDigitByteArray)) { |
4211 | 1451 |
INT _l = __intVal(len); |
1452 |
INT _v = __intVal(val); |
|
1453 |
unsigned INT _carry = 0; |
|
1454 |
unsigned INT _prod; |
|
1455 |
unsigned char *digitP = __ByteArrayInstPtr(__digitByteArray)->ba_element; |
|
1456 |
unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
1457 |
||
1458 |
/* |
|
1459 |
* skipping zeros does not help much (a few percent) on |
|
1460 |
* a P5 or other CPUS with a fast multiplier. |
|
1461 |
* It may make more of a difference on CPUs with slower 0-multiply. |
|
1462 |
*/ |
|
1463 |
while ((_l >= sizeof(INT)) && (((unsigned INT *)digitP)[0] == 0)) { |
|
1464 |
((unsigned long *)resultP)[0] = 0; |
|
1465 |
digitP += sizeof(INT); |
|
1466 |
resultP += sizeof(INT); |
|
1467 |
_l -= sizeof(INT); |
|
1468 |
} |
|
2923 | 1469 |
|
1470 |
#if defined(i386) || defined(alpha) /* XXX actually: LSB_FIRST */ |
|
1471 |
# if defined (__GNUC__) && defined(i386) |
|
4211 | 1472 |
/* |
1473 |
* can do it long-word-wise; |
|
1474 |
* 32*32 -> 64 multiplication |
|
1475 |
*/ |
|
1476 |
while (_l > 3) { |
|
1477 |
unsigned __pHi, __pLow; |
|
1478 |
||
1479 |
/* |
|
1480 |
* max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001 |
|
1481 |
* + maxCarry (0xFFFF.FFFF) -> 0xFFFF.FFFF.0000.0000 |
|
1482 |
*/ |
|
1483 |
asm ("mull %3 \n |
|
1484 |
addl %4,%%eax \n |
|
1485 |
adcl $0,%%edx" |
|
1486 |
: "=a" ((unsigned long)(__pLow)), |
|
1487 |
"=d" ((unsigned long)(__pHi)) |
|
1488 |
: "0" ((unsigned long)(((unsigned long *)digitP)[0])), |
|
1489 |
"1" ((unsigned long)(_v)), |
|
1490 |
"rm" ((unsigned long)(_carry)) ); |
|
1491 |
||
1492 |
((unsigned long *)resultP)[0] = __pLow; |
|
1493 |
_carry = __pHi; |
|
1494 |
digitP += 4; |
|
1495 |
resultP += 4; |
|
1496 |
_l -= 4; |
|
1497 |
} |
|
4170
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1498 |
# else /* not GNU-i386 */ |
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1499 |
# if defined (WIN32) && defined(i386) |
4211 | 1500 |
/* |
1501 |
* can do it long-word-wise; |
|
1502 |
* 32*32 -> 64 multiplication |
|
1503 |
*/ |
|
1504 |
while (_l > 3) { |
|
1505 |
unsigned __pLow; |
|
1506 |
unsigned digit; |
|
1507 |
||
1508 |
/* |
|
1509 |
* max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001 |
|
1510 |
* + maxCarry (0xFFFF.FFFF) -> 0xFFFF.FFFF.0000.0000 |
|
1511 |
*/ |
|
4180
cdfbc649b10d
slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents:
4179
diff
changeset
|
1512 |
/* |
4211 | 1513 |
digit = ((unsigned long *)digitP)[0]; |
1514 |
edx::eax = (digit * _v); |
|
1515 |
edx::eax += _carry; |
|
1516 |
((unsigned long *)resultP)[0] = eax; -- pLow |
|
1517 |
_carry = edx; -- pHigh |
|
1518 |
digitP += 4; |
|
1519 |
resultP += 4; |
|
4180
cdfbc649b10d
slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents:
4179
diff
changeset
|
1520 |
*/ |
4211 | 1521 |
digit = ((unsigned long *)digitP)[0]; |
1522 |
asm { |
|
1523 |
mov eax, digit |
|
1524 |
mov edx, _v |
|
1525 |
mul edx |
|
1526 |
add eax, _carry |
|
1527 |
adc edx, 0 |
|
1528 |
mov __pLow, eax |
|
1529 |
mov _carry, edx |
|
1530 |
} |
|
1531 |
||
1532 |
((unsigned long *)resultP)[0] = __pLow; |
|
1533 |
digitP += 4; |
|
1534 |
resultP += 4; |
|
1535 |
_l -= 4; |
|
1536 |
} |
|
4170
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1537 |
# else /* not WIN32-i386 */ |
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1538 |
# if defined(INT64) |
4211 | 1539 |
if (_v <= 0xFFFFFFFFL) { |
1540 |
/* |
|
1541 |
* have a 64bit int type ... good |
|
1542 |
*/ |
|
1543 |
UINT64 _prod64; |
|
1544 |
||
1545 |
/* have 64bit ints; can do it int-wise |
|
1546 |
* |
|
1547 |
* max: 0xFFFFFFFF * 0xFFFFFFFF -> 0xFFFFFFFE.0001 |
|
1548 |
* + maxCarry (0xFFFFFFFF) -> 0xFFFFFFFF.0000 |
|
1549 |
*/ |
|
1550 |
while (_l > 3) { |
|
1551 |
unsigned __t; |
|
1552 |
||
1553 |
__t = ((unsigned *)digitP)[0]; |
|
1554 |
digitP += 4; |
|
1555 |
_prod64 = (INT64)_v; |
|
1556 |
_prod64 *= __t; |
|
1557 |
_prod64 += _carry; |
|
1558 |
((unsigned *)resultP)[0] = _prod64 /* & 0xFFFFFFFFL */; |
|
1559 |
_carry = _prod64 >> 32; |
|
1560 |
resultP += 4; |
|
1561 |
_l -= 4; |
|
1562 |
} |
|
1563 |
if (_l > 1) { |
|
1564 |
unsigned short __t; |
|
1565 |
||
1566 |
__t = ((unsigned short *)digitP)[0]; |
|
1567 |
digitP += 2; |
|
1568 |
_prod64 = (INT64)_v; |
|
1569 |
_prod64 *= __t; |
|
1570 |
_prod64 += _carry; |
|
1571 |
((unsigned short *)resultP)[0] = _prod64 /* & 0xFFFF */; |
|
1572 |
_carry = _prod64 >> 16; |
|
1573 |
resultP += 2; |
|
1574 |
_l -= 2; |
|
1575 |
} |
|
1576 |
if (_l > 0) { |
|
1577 |
_prod64 = *digitP++ * _v + _carry; |
|
1578 |
*resultP++ = _prod64 /* & 0xFF */; |
|
1579 |
_carry = _prod64 >> 8; |
|
1580 |
_l--; |
|
1581 |
} |
|
1582 |
} |
|
4170
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1583 |
# else /* no INT64 type */ |
4211 | 1584 |
if (_v <= 0xFFFF) { |
1585 |
/* can do it short-wise |
|
1586 |
* |
|
1587 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
|
1588 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
|
1589 |
*/ |
|
1590 |
while (_l > 1) { |
|
1591 |
_prod = ((unsigned short *)digitP)[0] * _v + _carry; |
|
1592 |
((unsigned short *)resultP)[0] = _prod /* & 0xFFFF */; |
|
1593 |
_carry = _prod >> 16; |
|
1594 |
digitP += 2; |
|
1595 |
resultP += 2; |
|
1596 |
_l -= 2; |
|
1597 |
} |
|
1598 |
} |
|
4170
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1599 |
# endif /* no INT64 */ |
4b7dee48537e
inline asm for mul with WIN32
Claus Gittinger <cg@exept.de>
parents:
4164
diff
changeset
|
1600 |
# endif /* not WIN32-i386 */ |
3040 | 1601 |
# endif /* not GNU-i386 */ |
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1602 |
#else /* not LSB_FIRST */ |
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1603 |
|
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1604 |
# ifdef mips |
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1605 |
# define LOAD_WORD_WISE |
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1606 |
/* no, STORE_WORD_WISE makes it slower */ |
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1607 |
# endif |
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1608 |
|
4211 | 1609 |
if (_v <= 0xFFFF) { |
1610 |
/* can do it short-wise |
|
1611 |
* |
|
1612 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
|
1613 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
|
1614 |
*/ |
|
1615 |
while (_l > 1) { |
|
1616 |
unsigned int t; |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1617 |
|
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1618 |
#if defined(LOAD_WORD_WISE) |
4211 | 1619 |
/* better fetch short-wise */ |
1620 |
t = ((unsigned short *)digitP)[0]; |
|
1621 |
digitP += 2; |
|
1622 |
t = ((t >> 8) | (t << 8)) & 0xFFFF; |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1623 |
#else |
4211 | 1624 |
t = (digitP[1]<<8) + digitP[0]; |
1625 |
digitP += 2; |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1626 |
#endif |
4211 | 1627 |
_prod = t * _v + _carry; |
1628 |
_carry = _prod >> 16; |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1629 |
#if defined(STORE_WORD_WISE) |
4211 | 1630 |
/* better store short-wise */ |
1631 |
_prod = ((_prod >> 8) | (_prod << 8)) & 0xFFFF; |
|
1632 |
((unsigned short *)resultP)[0] = _prod; |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1633 |
#else |
4211 | 1634 |
resultP[0] = _prod /* & 0xFF */; |
1635 |
resultP[1] = (_prod>>8) /* & 0xFF */; |
|
4192
8ee270c413a0
slightly faster largeInt * smallInt for mips (msb machines)
Claus Gittinger <cg@exept.de>
parents:
4191
diff
changeset
|
1636 |
#endif |
4211 | 1637 |
resultP += 2; |
1638 |
_l -= 2; |
|
1639 |
} |
|
1640 |
} |
|
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1641 |
|
3040 | 1642 |
#endif /* LSB_FIRST */ |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1643 |
|
4211 | 1644 |
/* |
1645 |
* rest is done byte-wise |
|
1646 |
*/ |
|
1647 |
while (_l > 0) { |
|
1648 |
_prod = *digitP++ * _v + _carry; |
|
1649 |
*resultP++ = _prod /* & 0xFF */; |
|
1650 |
_carry = _prod >> 8; |
|
1651 |
_l--; |
|
1652 |
} |
|
1653 |
||
1654 |
_l = __intVal(lResult) - __intVal(len); |
|
1655 |
||
1656 |
/* |
|
1657 |
* remaining carry |
|
1658 |
*/ |
|
1659 |
while (_carry) { |
|
1660 |
*resultP++ = _carry /* & 0xFF */; |
|
1661 |
_carry >>= 8; |
|
1662 |
_l--; |
|
1663 |
} |
|
1664 |
||
1665 |
/* |
|
1666 |
* remaining zeros |
|
1667 |
*/ |
|
1668 |
while (_l--) { |
|
1669 |
*resultP++ = 0; |
|
1670 |
} |
|
1671 |
||
1672 |
/* |
|
1673 |
* need compress ? |
|
1674 |
*/ |
|
1675 |
if (resultP[-1]) { |
|
1676 |
/* |
|
1677 |
* no |
|
1678 |
*/ |
|
1679 |
RETURN(result); |
|
1680 |
} |
|
1681 |
||
1682 |
ok = true; |
|
85 | 1683 |
} |
1684 |
%}. |
|
92 | 1685 |
" |
1686 |
fall back - normally not reached |
|
1687 |
(could make it a primitive-failure as well) |
|
1688 |
" |
|
85 | 1689 |
ok ifFalse:[ |
4211 | 1690 |
carry := 0. |
1691 |
1 to:len do:[:i | |
|
1692 |
prod := (digitByteArray basicAt:i) * val + carry. |
|
1693 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
|
1694 |
carry := prod bitShift:-8. |
|
1695 |
]. |
|
1696 |
[carry ~~ 0] whileTrue:[ |
|
1697 |
len := len + 1. |
|
1698 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
|
1699 |
carry := carry bitShift:-8 |
|
1700 |
]. |
|
1701 |
[len < lResult] whileTrue:[ |
|
1702 |
len := len + 1. |
|
1703 |
resultDigitByteArray basicAt:len put:0 |
|
1704 |
] |
|
68 | 1705 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1706 |
^ result compressed |
92 | 1707 |
! |
1708 |
||
701 | 1709 |
sumFromInteger:anInteger |
1710 |
"sent, when anInteger does not know how to add the receiver. |
|
1711 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
|
92 | 1712 |
|
701 | 1713 |
anInteger > 0 ifTrue:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1714 |
sign > 0 ifTrue:[ |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1715 |
^ self absFastPlus:anInteger sign:1 |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1716 |
]. |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1717 |
^ self absFastMinus:anInteger sign:-1 |
701 | 1718 |
]. |
1794 | 1719 |
anInteger == 0 ifTrue:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1720 |
^ self |
1794 | 1721 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1722 |
sign > 0 ifTrue:[ |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
1723 |
^ self absFastMinus:anInteger sign:1 |
1794 | 1724 |
]. |
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1725 |
^ self absFastPlus:anInteger sign:-1 |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1726 |
|
92 | 1727 |
|
701 | 1728 |
" |
1729 |
12345678901234567890 |
|
1730 |
-12345678901234567890 |
|
1731 |
12345678901234567890 sumFromInteger:0 |
|
1732 |
-12345678901234567890 sumFromInteger:0 |
|
1733 |
12345678901234567890 sumFromInteger:1 |
|
1734 |
12345678901234567890 sumFromInteger:-1 |
|
1735 |
-12345678901234567890 sumFromInteger:1 |
|
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1736 |
-12345678901234567890 sumFromInteger:-1 |
701 | 1737 |
" |
1794 | 1738 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1739 |
"Modified: / 9.1.1998 / 13:27:37 / cg" |
68 | 1740 |
! ! |
1741 |
||
701 | 1742 |
!LargeInteger methodsFor:'printing & storing'! |
345 | 1743 |
|
701 | 1744 |
storeOn:aStream |
1745 |
"append a representation of the receiver to aStream, which can |
|
1746 |
be used to reconstruct the receiver." |
|
68 | 1747 |
|
701 | 1748 |
self printOn:aStream. |
1749 |
aStream nextPutAll:' asLargeInteger' |
|
68 | 1750 |
! ! |
1751 |
||
1752 |
!LargeInteger methodsFor:'private'! |
|
1753 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1754 |
absDivMod:anInteger |
701 | 1755 |
"return an array with two LargeIntegers representing |
1756 |
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
|
1757 |
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
|
1758 |
This method needs a rewrite." |
68 | 1759 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1760 |
|dividend divisor |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1761 |
quo digits |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1762 |
shift "{ Class: SmallInteger }" | |
92 | 1763 |
|
701 | 1764 |
anInteger == 0 ifTrue:[ |
4162 | 1765 |
^ DivisionByZeroSignal raise |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1766 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1767 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1768 |
self = anInteger ifTrue:[ |
4162 | 1769 |
^ Array with:1 with:0 |
701 | 1770 |
]. |
92 | 1771 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1772 |
shift := self highBit - anInteger highBit. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1773 |
dividend := self simpleDeepCopy sign:1. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1774 |
shift < 0 ifTrue:[ |
4162 | 1775 |
^ Array with:0 with:dividend. |
68 | 1776 |
]. |
4139 | 1777 |
shift == 0 ifTrue:[ |
4162 | 1778 |
divisor := anInteger simpleDeepCopy. |
4139 | 1779 |
] ifFalse:[ |
4162 | 1780 |
divisor := anInteger bitShift:shift. |
701 | 1781 |
]. |
1782 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1783 |
quo := self class basicNew numberOfDigits:((shift // 8) + 1). |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1784 |
digits := quo digits. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1785 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1786 |
shift := shift + 1. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1787 |
[shift > 0] whileTrue:[ |
4162 | 1788 |
(dividend absLess:divisor) ifFalse:[ |
1789 |
digits bitSetAt:shift. |
|
1790 |
(dividend absSubtract: divisor) ifFalse:[ "result == 0" |
|
1791 |
^ Array with:quo compressed with:dividend compressed |
|
1792 |
]. |
|
1793 |
]. |
|
1794 |
shift := shift - 1. |
|
1795 |
divisor div2. |
|
701 | 1796 |
]. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1797 |
^ Array with:quo compressed with:dividend compressed |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1798 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1799 |
" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1800 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000000 absDivMod:4000000000] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1801 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000000 absDivMod:3000000000] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1802 |
16000000000 absDivMod:4000000000 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1803 |
16000000000 absDivMod:3000000000 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1804 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1805 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1806 |
"Modified: / 5.11.1996 / 18:40:24 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1807 |
"Created: / 27.4.1999 / 19:45:44 / stefan" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1808 |
"Modified: / 3.5.1999 / 08:38:01 / stefan" |
92 | 1809 |
! |
1810 |
||
701 | 1811 |
absEq:aLargeInteger |
1812 |
"return true, if abs(self) = abs(theArgument)" |
|
92 | 1813 |
|
701 | 1814 |
|len1 "{ Class: SmallInteger }" |
1815 |
len2 "{ Class: SmallInteger }" |
|
1816 |
d1 "{ Class: SmallInteger }" |
|
1817 |
d2 "{ Class: SmallInteger }" |
|
1818 |
otherDigitByteArray | |
|
92 | 1819 |
|
701 | 1820 |
len1 := digitByteArray size. |
1821 |
otherDigitByteArray := aLargeInteger digits. |
|
1822 |
len2 := otherDigitByteArray size. |
|
92 | 1823 |
|
4157 | 1824 |
"/ the highest digit(s) should not be zero |
1825 |
"/ when properly normalized; |
|
1826 |
"/ but we are tolerant here, to allow for unnormalized |
|
1827 |
"/ numbers to be compared ... |
|
1828 |
||
701 | 1829 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
4162 | 1830 |
len1 := len1 - 1 |
701 | 1831 |
]. |
1832 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
4162 | 1833 |
len2 := len2 - 1 |
68 | 1834 |
]. |
701 | 1835 |
(len1 ~~ len2) ifTrue:[^ false]. |
1836 |
[len1 > 0] whileTrue:[ |
|
4162 | 1837 |
d1 := digitByteArray basicAt:len1. |
1838 |
d2 := otherDigitByteArray basicAt:len1. |
|
1839 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1840 |
len1 := len1 - 1 |
|
68 | 1841 |
]. |
701 | 1842 |
^ true |
4157 | 1843 |
|
1844 |
"Modified: / 8.5.1999 / 18:37:02 / cg" |
|
68 | 1845 |
! |
1846 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1847 |
absFastDivMod:aPositiveSmallInteger |
68 | 1848 |
"return an array with two LargeIntegers representing |
4180
cdfbc649b10d
slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents:
4179
diff
changeset
|
1849 |
abs(self) // aPositiveSmallInteger and |
cdfbc649b10d
slight speedup in productFromInteger for WIN32 (eliminated a move)
Claus Gittinger <cg@exept.de>
parents:
4179
diff
changeset
|
1850 |
abs(self) \\ aPositiveSmallInteger." |
68 | 1851 |
|
77 | 1852 |
|tmp1 "{ Class: SmallInteger }" |
1853 |
prevRest "{ Class: SmallInteger }" |
|
1854 |
count "{ Class: SmallInteger }" |
|
85 | 1855 |
newDigitByteArray result |
1856 |
ok| |
|
68 | 1857 |
|
1890 | 1858 |
aPositiveSmallInteger == 0 ifTrue:[ |
4230 | 1859 |
^ DivisionByZeroSignal raise |
68 | 1860 |
]. |
1861 |
||
1862 |
"This cannot happen (if always normalized) |
|
1890 | 1863 |
self < aPositiveSmallInteger ifTrue:[ |
4230 | 1864 |
^ Array with:0 with:self |
68 | 1865 |
]. |
1866 |
" |
|
1867 |
count := digitByteArray size. |
|
1868 |
result := self class basicNew numberOfDigits:count. |
|
1869 |
newDigitByteArray := result digits. |
|
85 | 1870 |
ok := false. |
1871 |
%{ |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1872 |
OBJ __digits; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1873 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1874 |
__digits = __INST(digitByteArray); |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1875 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1876 |
if (__isByteArray(__digits) |
85 | 1877 |
&& __isByteArray(newDigitByteArray) |
1890 | 1878 |
&& __bothSmallInteger(count, aPositiveSmallInteger)) { |
4230 | 1879 |
unsigned INT rest = 0; |
1880 |
int index = __intVal(count); |
|
1881 |
int index0; |
|
1882 |
unsigned INT divisor = __intVal(aPositiveSmallInteger); |
|
1883 |
unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element; |
|
1884 |
unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element; |
|
1885 |
||
1886 |
index0 = index - 1; |
|
1887 |
||
1888 |
/* |
|
1889 |
* divide short-wise |
|
1890 |
*/ |
|
1891 |
if (divisor <= 0xFFFF) { |
|
1892 |
if ((index & 1) == 0) { /* even number of bytes */ |
|
1893 |
while (index > 1) { |
|
1894 |
unsigned INT t; |
|
1895 |
unsigned INT div; |
|
1896 |
||
1897 |
#if defined(__LSBFIRST) || defined(i386) |
|
1898 |
index -= 2; |
|
1899 |
t = *((unsigned short *)(&digitBytes[index])); |
|
2952 | 1900 |
# else |
4230 | 1901 |
index--; |
1902 |
t = digitBytes[index]; |
|
1903 |
index--; |
|
1904 |
t = (t << 8) | digitBytes[index]; |
|
2952 | 1905 |
# endif |
4230 | 1906 |
t = t | (rest << 16); |
1907 |
div = t / divisor; |
|
1908 |
rest = t % divisor; |
|
1909 |
#if defined(__LSBFIRST) || defined(i386) |
|
1910 |
*((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF); |
|
2952 | 1911 |
# else |
4230 | 1912 |
resultBytes[index+1] = div >> 8; |
1913 |
resultBytes[index] = div /* & 0xFF */; |
|
2952 | 1914 |
# endif |
4230 | 1915 |
} |
1916 |
} |
|
1917 |
} |
|
1918 |
while (index > 0) { |
|
1919 |
unsigned INT t; |
|
1920 |
||
1921 |
index--; |
|
1922 |
t = digitBytes[index]; |
|
1923 |
t = t | (rest << 8); |
|
1924 |
resultBytes[index] = t / divisor; |
|
1925 |
rest = t % divisor; |
|
1926 |
} |
|
1927 |
prevRest = __MKSMALLINT(rest); |
|
1928 |
||
1929 |
/* |
|
1930 |
* no need to normalize ? |
|
1931 |
*/ |
|
1932 |
while ((index0 >= sizeof(INT)) && (resultBytes[index0]==0)) { |
|
1933 |
index0--; |
|
1934 |
} |
|
1935 |
||
1936 |
if (index0 > sizeof(INT)) { |
|
1937 |
RETURN ( __ARRAY_WITH2(result, prevRest)); |
|
1938 |
} |
|
1939 |
if ((index0 == sizeof(INT)) |
|
1940 |
&& resultBytes[index0] >= 0x40) { |
|
1941 |
RETURN ( __ARRAY_WITH2(result, prevRest)); |
|
1942 |
} |
|
1943 |
/* |
|
1944 |
* must compress |
|
1945 |
*/ |
|
1946 |
ok = true; |
|
85 | 1947 |
} |
1948 |
%}. |
|
1949 |
" |
|
1950 |
slow code - not normally reached |
|
1951 |
(could also do a primitiveFailure here) |
|
1952 |
" |
|
92 | 1953 |
ok ifFalse:[ |
4230 | 1954 |
self primitiveFailed |
68 | 1955 |
]. |
1956 |
||
2933 | 1957 |
^ Array with:(result compressed) with:prevRest |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1958 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1959 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1960 |
((16r1234 asLargeInteger absFastDiv:16rffff) at:2) printStringRadix:16 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1961 |
((16r00123456 asLargeInteger absFastDiv:16rffffff) at:2) printStringRadix:16 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1962 |
" |
68 | 1963 |
! |
1964 |
||
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1965 |
absFastMinus:aSmallInteger sign:newSign |
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1966 |
"return a LargeInteger representing abs(self) - abs(theArgument) |
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1967 |
with sign: newSign. |
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1968 |
The result is normalized. |
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1969 |
This is a helper for addition and subtraction - not for public use." |
701 | 1970 |
|
4181 | 1971 |
|result resultDigitByteArray lastDigit ok |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1972 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1973 |
rsltLen "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1974 |
index "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1975 |
borrow "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1976 |
diff "{ Class: SmallInteger }" | |
701 | 1977 |
|
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1978 |
"/ the following code only works with |
4181 | 1979 |
"/ smallIntegers in the range _MIN_INT+255 .. _MAX_INT-255 |
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1980 |
|
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1981 |
((aSmallInteger < (SmallInteger minVal + 255)) |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1982 |
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
1983 |
^ self absMinus:(LargeInteger value:aSmallInteger) sign:newSign. |
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1984 |
]. |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1985 |
|
701 | 1986 |
len := digitByteArray size. |
1987 |
||
4191 | 1988 |
rsltLen := len "+ 1". |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1989 |
result := self class basicNew numberOfDigits:rsltLen. |
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
1990 |
result sign:newSign. |
701 | 1991 |
resultDigitByteArray := result digits. |
1992 |
||
1993 |
borrow := aSmallInteger abs. |
|
1994 |
||
4181 | 1995 |
%{ |
1996 |
if (__isByteArray(__INST(digitByteArray)) |
|
1997 |
&& __isByteArray(resultDigitByteArray)) { |
|
4230 | 1998 |
unsigned INT __borrow = __intVal(borrow); |
1999 |
INT __diff; |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2000 |
int __index = 1; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2001 |
int __len = __intVal(len); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2002 |
unsigned char *__digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2003 |
unsigned char *__resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
4230 | 2004 |
int __len3; |
2005 |
||
2006 |
#if defined(alpha64) |
|
2007 |
/* |
|
2008 |
* subtract word-wise |
|
2009 |
*/ |
|
2010 |
__len3 = __len - 3; |
|
2011 |
while (__index < __len3) { |
|
2012 |
__diff = ((unsigned int *)(__digitP+__index-1))[0] - (__borrow & 0xFFFFFFFFL); |
|
2013 |
__borrow >>= 32; |
|
2014 |
if (__diff < 0) { |
|
4254 | 2015 |
/* __diff += 0x100000000; */ |
4230 | 2016 |
__borrow++; |
2017 |
} |
|
2018 |
((unsigned int *)(__resultP+__index-1))[0] = __diff; |
|
2019 |
__index += 4; |
|
2020 |
} |
|
2021 |
#endif |
|
4182 | 2022 |
#if defined(__LSBFIRST) || defined(i386) |
4215 | 2023 |
/* |
2024 |
* subtract short-wise |
|
2025 |
*/ |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2026 |
while (__index < __len) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2027 |
__diff = ((unsigned short *)(__digitP+__index-1))[0] - (__borrow & 0xFFFF); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2028 |
__borrow >>= 16; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2029 |
if (__diff < 0) { |
4254 | 2030 |
/* __diff += 0x10000; */ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2031 |
__borrow++; |
4254 | 2032 |
} else { |
2033 |
if (__borrow == 0) { |
|
2034 |
((unsigned short *)(__resultP+__index-1))[0] = __diff; |
|
2035 |
__index += 2; |
|
2036 |
||
2037 |
/* nothing more to subtract .. */ |
|
2038 |
while (__index < __len) { |
|
2039 |
((unsigned short *)(__resultP+__index-1))[0] = ((unsigned short *)(__digitP+__index-1))[0]; |
|
2040 |
__index += 2; |
|
2041 |
} |
|
2042 |
if (__index <= __len) { |
|
2043 |
__resultP[__index-1] = __digitP[__index-1]; |
|
2044 |
} |
|
2045 |
break; |
|
2046 |
} |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2047 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2048 |
((unsigned short *)(__resultP+__index-1))[0] = __diff; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2049 |
__index += 2; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2050 |
} |
4191 | 2051 |
#endif |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2052 |
while (__index <= __len) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2053 |
__diff = __digitP[__index-1] - (__borrow & 0xFF); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2054 |
__borrow >>= 8; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2055 |
if (__diff < 0) { |
4254 | 2056 |
/* __diff += 0x100; */ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2057 |
__borrow++; |
4254 | 2058 |
} else { |
2059 |
if (__borrow == 0) { |
|
2060 |
__resultP[__index-1] = __diff; |
|
2061 |
__index++; |
|
2062 |
||
2063 |
/* nothing more to subtract .. */ |
|
2064 |
while (__index <= __len) { |
|
2065 |
__resultP[__index-1] = __digitP[__index-1]; |
|
2066 |
__index++; |
|
2067 |
} |
|
2068 |
break; |
|
2069 |
} |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2070 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2071 |
__resultP[__index-1] = __diff; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2072 |
__index++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2073 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2074 |
lastDigit = __MKSMALLINT( __resultP[__index-1-1] ); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2075 |
ok = true; |
4181 | 2076 |
} |
2077 |
%}. |
|
2078 |
||
4191 | 2079 |
ok == true ifFalse:[ "/ cannot happen |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2080 |
index := 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2081 |
[borrow ~~ 0] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2082 |
(index <= len) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2083 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2084 |
borrow := borrow bitShift:-8. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2085 |
diff < 0 ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2086 |
diff := diff + 256. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2087 |
borrow := borrow + 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2088 |
] |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2089 |
] ifFalse:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2090 |
diff := borrow bitAnd:255. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2091 |
borrow := borrow bitShift:-8. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2092 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2093 |
resultDigitByteArray basicAt:index put:(lastDigit := diff). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2094 |
index := index + 1 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2095 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2096 |
[index <= len] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2097 |
resultDigitByteArray basicAt:index put:(lastDigit := digitByteArray basicAt:index). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2098 |
index := index + 1 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2099 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2100 |
(index <= rsltLen) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2101 |
lastDigit := 0. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2102 |
] |
701 | 2103 |
]. |
4191 | 2104 |
|
2105 |
(lastDigit == 0 or:[rsltLen == SmallInteger maxBytes]) ifTrue:[ |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2106 |
^ result compressed. |
4182 | 2107 |
]. |
701 | 2108 |
^ result |
2109 |
||
2110 |
" |
|
2111 |
12345678900000000000 absFastMinus:1 |
|
2112 |
12345678900000000000 absFastMinus:1000000 |
|
2113 |
12345678900000000000 absFastMinus:255 |
|
4191 | 2114 |
(SmallInteger maxVal + 1) absFastMinus:1 sign:1 |
701 | 2115 |
(SmallInteger minVal - 1) absFastMinus:1 |
2116 |
" |
|
1794 | 2117 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2118 |
"Modified: 24.3.1997 / 21:33:25 / cg" |
701 | 2119 |
! |
2120 |
||
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
2121 |
absFastPlus:aSmallInteger sign:newSign |
701 | 2122 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
2123 |
with sign: newSign. |
4171 | 2124 |
The result is normalized. The argument must be a smallInteger |
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
2125 |
with byteSize one less than the integer byteSize. |
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
2126 |
This is a helper for addition and subtraction - not for public use." |
701 | 2127 |
|
1794 | 2128 |
|result resultDigitByteArray lastDigit |
2952 | 2129 |
ok |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2130 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2131 |
rsltLen "{ Class: SmallInteger }" |
701 | 2132 |
index "{ Class: SmallInteger }" |
2133 |
carry "{ Class: SmallInteger }" | |
|
2134 |
||
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
2135 |
"/ the following code only works with |
4157 | 2136 |
"/ smallIntegers in the range _MIN_INT+255 .. _MAX_INT-255 |
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
2137 |
|
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
2138 |
((aSmallInteger < (SmallInteger minVal + 255)) |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
2139 |
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2140 |
^ self absPlus:(LargeInteger value:aSmallInteger) sign:newSign. |
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
2141 |
]. |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
2142 |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2143 |
len := rsltLen := digitByteArray size. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2144 |
"/ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2145 |
"/ there can only be an overflow from the high byte, |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2146 |
"/ if it is 255 (since the other number is definitely smaller) |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2147 |
"/ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2148 |
(digitByteArray at:len) == 16rFF ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2149 |
rsltLen := len + 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2150 |
]. |
701 | 2151 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2152 |
result := self class basicNew numberOfDigits:rsltLen. |
4173
0e74ef3ec49d
pass the new sign to absFastPlus/absFastMinus,
Claus Gittinger <cg@exept.de>
parents:
4172
diff
changeset
|
2153 |
result sign:newSign. |
701 | 2154 |
resultDigitByteArray := result digits. |
2155 |
||
2952 | 2156 |
%{ |
2157 |
if (__isByteArray(__INST(digitByteArray)) |
|
2158 |
&& __isByteArray(resultDigitByteArray) |
|
2159 |
&& __isSmallInteger(aSmallInteger)) { |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2160 |
/* carry is NOT unsigned (see negation below) */ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2161 |
INT __carry = __intVal(aSmallInteger); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2162 |
int __index = 1; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2163 |
int __len = __intVal(len); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2164 |
unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2165 |
unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2166 |
INT __ptrDelta = __dst - __src; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2167 |
unsigned char *__srcLast = __src + __len - 1; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2168 |
unsigned char *__dstLast = __dst + __intVal(rsltLen) - 1; |
4215 | 2169 |
unsigned char *__srcLast4; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2170 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2171 |
if (__carry < 0) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2172 |
__carry = -__carry; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2173 |
} |
4171 | 2174 |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2175 |
#if defined(__LSBFIRST) || defined(i386) || defined(alpha) |
4171 | 2176 |
# if defined(i386) && defined(__GNUC__) |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2177 |
/* |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2178 |
* add long-wise |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2179 |
*/ |
4215 | 2180 |
__srcLast4 = __srcLast - 3; |
2181 |
while (__src <= __srcLast4) { |
|
2182 |
unsigned int __sum; |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2183 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2184 |
asm ("addl %%edx,%%eax \n |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2185 |
movl $0,%%edx \n |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2186 |
adcl $0,%%edx" |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2187 |
: "=d" ((unsigned long)(__carry)), |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2188 |
"=a" ((unsigned long)(__sum)) |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2189 |
: "0" ((unsigned long)(__carry)), |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2190 |
"1" ((unsigned long)(((unsigned *)__src)[0])) ); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2191 |
|
4215 | 2192 |
((unsigned int *)(__src + __ptrDelta))[0] = __sum; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2193 |
__src += 4; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2194 |
if (__carry == 0) { |
4215 | 2195 |
while (__src <= __srcLast4) { |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2196 |
/* copy over */ |
4215 | 2197 |
((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0]; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2198 |
__src += 4; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2199 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2200 |
break; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2201 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2202 |
} |
4171 | 2203 |
# else |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2204 |
# if defined(i386) && defined(WIN32) |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2205 |
/* |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2206 |
* add long-wise |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2207 |
*/ |
4215 | 2208 |
__srcLast4 = __srcLast - 3; |
2209 |
while (__src <= __srcLast4) { |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2210 |
unsigned int __sum; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2211 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2212 |
__sum = ((unsigned int *)__src)[0]; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2213 |
asm { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2214 |
mov eax, __sum |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2215 |
add eax, __carry |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2216 |
mov edx, 0 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2217 |
adc edx, 0 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2218 |
mov __sum, eax |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2219 |
mov __carry, edx |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2220 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2221 |
|
4215 | 2222 |
((unsigned int *)(__src + __ptrDelta))[0] = __sum; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2223 |
__src += 4; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2224 |
if (__carry == 0) { |
4215 | 2225 |
while (__src <= __srcLast4) { |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2226 |
/* copy over */ |
4215 | 2227 |
((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0]; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2228 |
__src += 4; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2229 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2230 |
break; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2231 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2232 |
} |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2233 |
# else |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2234 |
# ifdef alpha64 |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2235 |
/* |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2236 |
* add long-wise |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2237 |
*/ |
4215 | 2238 |
__srcLast4 = __srcLast - 3; |
4222 | 2239 |
while (__src <= __srcLast4) { |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2240 |
unsigned INT __sum; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2241 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2242 |
__sum = ((unsigned int *)__src)[0] + __carry; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2243 |
((unsigned int *)(__src + __ptrDelta))[0] = __sum /* & 0xFFFF */; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2244 |
__src += 4; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2245 |
__carry = __sum >> 32; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2246 |
if (__carry == 0) { |
4222 | 2247 |
while (__src <= __srcLast4) { |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2248 |
/* copy over */ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2249 |
((unsigned int *)(__src + __ptrDelta))[0] = ((unsigned int *)__src)[0]; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2250 |
__src += 4; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2251 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2252 |
break; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2253 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2254 |
} |
4178
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2255 |
# endif /* alpha64 */ |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2256 |
# endif /* i386 & WIN32 */ |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2257 |
# endif /* i386 & GNUC */ |
e7121285caa8
inline asm for absPlus: for WIN32
Claus Gittinger <cg@exept.de>
parents:
4175
diff
changeset
|
2258 |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2259 |
/* |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2260 |
* add short-wise |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2261 |
*/ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2262 |
while (__src < __srcLast) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2263 |
__carry += ((unsigned short *)__src)[0]; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2264 |
((unsigned short *)(__src + __ptrDelta))[0] = __carry /* & 0xFFFF */; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2265 |
__carry >>= 16; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2266 |
__src += 2; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2267 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2268 |
if (__src <= __srcLast) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2269 |
__carry += __src[0]; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2270 |
__src[__ptrDelta] = __carry /* & 0xFF */; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2271 |
__carry >>= 8; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2272 |
__src++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2273 |
} |
4171 | 2274 |
#else /* not __LSBFIRST */ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2275 |
while (__src <= __srcLast) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2276 |
__carry += __src[0]; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2277 |
__src[__ptrDelta] = __carry /* & 0xFF */; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2278 |
__carry >>= 8; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2279 |
__src++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2280 |
} |
4171 | 2281 |
#endif /* __LSBFIRST */ |
4185 | 2282 |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2283 |
__dst = __src + __ptrDelta; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2284 |
while (__dst <= __dstLast) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2285 |
__dst[0] = __carry /* & 0xFF */; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2286 |
__carry >>= 8; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2287 |
__dst++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2288 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2289 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2290 |
if (__dst[-1]) { /* lastDigit */ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2291 |
RETURN (result); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2292 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2293 |
ok = true; |
2952 | 2294 |
} |
2295 |
%}. |
|
2296 |
||
2297 |
ok ~~ true ifTrue:[ |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2298 |
index := 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2299 |
carry := aSmallInteger abs. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2300 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2301 |
[carry ~~ 0] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2302 |
(index <= len) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2303 |
carry := (digitByteArray basicAt:index) + carry. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2304 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2305 |
resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2306 |
carry := carry bitShift:-8. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2307 |
index := index + 1 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2308 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2309 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2310 |
(index <= rsltLen) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2311 |
[index <= len] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2312 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2313 |
index := index + 1 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2314 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2315 |
lastDigit := 0. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2316 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2317 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2318 |
lastDigit ~~ 0 ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2319 |
^ result |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2320 |
]. |
701 | 2321 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2322 |
|
2952 | 2323 |
^ result compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2324 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2325 |
"Modified: 24.3.1997 / 21:32:41 / cg" |
701 | 2326 |
! |
2327 |
||
2328 |
absLess:aLargeInteger |
|
1890 | 2329 |
"return true, if abs(self) < abs(theArgument). |
2330 |
This handles unnormalized largeIntegers." |
|
701 | 2331 |
|
1890 | 2332 |
|myLen "{ Class: SmallInteger }" |
2333 |
otherLen "{ Class: SmallInteger }" |
|
701 | 2334 |
d1 "{ Class: SmallInteger }" |
2335 |
d2 "{ Class: SmallInteger }" |
|
2336 |
otherDigitByteArray | |
|
2337 |
||
1890 | 2338 |
myLen := digitByteArray size. |
701 | 2339 |
otherDigitByteArray := aLargeInteger digits. |
1890 | 2340 |
otherLen := otherDigitByteArray size. |
701 | 2341 |
|
4157 | 2342 |
"/ the highest digit(s) should not be zero |
2343 |
"/ when properly normalized; |
|
2344 |
"/ but we are tolerant here, to allow for unnormalized |
|
2345 |
"/ numbers to be compared ... |
|
2346 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2347 |
[myLen > 0 and:[(digitByteArray basicAt:myLen) == 0]] whileTrue:[ |
4162 | 2348 |
myLen := myLen - 1 |
701 | 2349 |
]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2350 |
[otherLen > 0 and:[(otherDigitByteArray basicAt:otherLen) == 0]] whileTrue:[ |
4162 | 2351 |
otherLen := otherLen - 1 |
701 | 2352 |
]. |
1890 | 2353 |
(myLen < otherLen) ifTrue:[^ true]. |
2354 |
(myLen > otherLen) ifTrue:[^ false]. |
|
701 | 2355 |
|
1890 | 2356 |
[myLen > 0] whileTrue:[ |
4162 | 2357 |
d1 := digitByteArray basicAt:myLen. |
2358 |
d2 := otherDigitByteArray basicAt:myLen. |
|
2359 |
d1 == d2 ifFalse:[ |
|
2360 |
(d1 < d2) ifTrue:[^ true]. |
|
2361 |
^ false |
|
2362 |
]. |
|
2363 |
myLen := myLen - 1 |
|
701 | 2364 |
]. |
2365 |
^ false |
|
1890 | 2366 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2367 |
"Modified: / 3.5.1999 / 08:06:28 / stefan" |
4157 | 2368 |
"Modified: / 8.5.1999 / 18:37:11 / cg" |
701 | 2369 |
! |
2370 |
||
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2371 |
absLessEq:aLargeInteger |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2372 |
"return true, if abs(self) <= abs(theArgument). |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2373 |
This handles unnormalized largeIntegers." |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2374 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2375 |
|myLen "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2376 |
otherLen "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2377 |
d1 "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2378 |
d2 "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2379 |
otherDigitByteArray | |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2380 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2381 |
myLen := digitByteArray size. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2382 |
otherDigitByteArray := aLargeInteger digits. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2383 |
otherLen := otherDigitByteArray size. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2384 |
|
4157 | 2385 |
"/ the highest digit(s) should not be zero |
2386 |
"/ when properly normalized; |
|
2387 |
"/ but we are tolerant here, to allow for unnormalized |
|
2388 |
"/ numbers to be compared ... |
|
2389 |
||
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2390 |
[(digitByteArray basicAt:myLen) == 0] whileTrue:[ |
4162 | 2391 |
myLen := myLen - 1 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2392 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2393 |
[(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[ |
4162 | 2394 |
otherLen := otherLen - 1 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2395 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2396 |
(myLen < otherLen) ifTrue:[^ true]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2397 |
(myLen > otherLen) ifTrue:[^ false]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2398 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2399 |
[myLen > 0] whileTrue:[ |
4162 | 2400 |
d1 := digitByteArray basicAt:myLen. |
2401 |
d2 := otherDigitByteArray basicAt:myLen. |
|
2402 |
d1 == d2 ifFalse:[ |
|
2403 |
(d1 < d2) ifTrue:[^ true]. |
|
2404 |
^ false. |
|
2405 |
]. |
|
2406 |
myLen := myLen - 1 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2407 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2408 |
^ true |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2409 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2410 |
"Created: / 13.2.1998 / 12:19:45 / stefan" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2411 |
"Modified: / 30.4.1999 / 12:46:31 / stefan" |
4157 | 2412 |
"Modified: / 8.5.1999 / 18:37:15 / cg" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2413 |
! |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2414 |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2415 |
absMinus:aLargeInteger sign:newSign |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2416 |
"return a LargeInteger representing abs(self) - abs(theArgument) |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2417 |
with sign: newSign. |
4215 | 2418 |
The result is normalized. The argument must be a largeInteger |
2419 |
with a byteSize smaller than the receivers byteSize. |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2420 |
This is a helper for addition and subtraction - not for public use." |
701 | 2421 |
|
2422 |
|result done |
|
2423 |
otherDigitByteArray resultDigitByteArray |
|
2424 |
len1 "{ Class: SmallInteger }" |
|
2425 |
len2 "{ Class: SmallInteger }" |
|
2426 |
index "{ Class: SmallInteger }" |
|
2427 |
borrow "{ Class: SmallInteger }" |
|
2428 |
diff "{ Class: SmallInteger }" |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2429 |
carry "{ Class: SmallInteger }" |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2430 |
lastDigit |
4190 | 2431 |
lResult ok| |
701 | 2432 |
|
2433 |
len1 := digitByteArray size. |
|
2434 |
otherDigitByteArray := aLargeInteger digits. |
|
2435 |
len2 := otherDigitByteArray size. |
|
2436 |
||
4251 | 2437 |
len1 > len2 ifTrue:[ |
2438 |
lResult := len1 |
|
2439 |
] ifFalse:[ |
|
2440 |
lResult := (len1 max: len2) + 1. |
|
2441 |
]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2442 |
result := self class basicNew numberOfDigits:lResult. |
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2443 |
result sign:newSign. |
701 | 2444 |
resultDigitByteArray := result digits. |
2445 |
||
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2446 |
lastDigit := 0. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2447 |
|
4190 | 2448 |
%{ |
2449 |
OBJ _digitByteArray = __INST(digitByteArray); |
|
2450 |
||
2451 |
if (__isByteArray(_digitByteArray) |
|
2452 |
&& __isByteArray(otherDigitByteArray) |
|
2453 |
&& __isByteArray(resultDigitByteArray)) { |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2454 |
int __len1 = __intVal(len1); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2455 |
int __len2 = __intVal(len2); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2456 |
int __minLen = __len1 < __len2 ? __len1 : __len2; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2457 |
int __index, __borrow = 0, __diff; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2458 |
unsigned char *__myDigits, *__otherDigits, *__resultDigits; |
4190 | 2459 |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2460 |
ok = true; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2461 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2462 |
__resultDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2463 |
__otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2464 |
__myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2465 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2466 |
__index = 1; |
4215 | 2467 |
#if defined(i386) || defined(__LSBFIRST) |
2468 |
/* |
|
2469 |
* subtract short-wise |
|
2470 |
*/ |
|
4214
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2471 |
while (__index < __minLen) { |
4251 | 2472 |
__diff = ((unsigned short *)(__myDigits+__index-1))[0] |
2473 |
- ((unsigned short *)(__otherDigits+__index-1))[0] |
|
2474 |
- __borrow; |
|
4214
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2475 |
if (__diff >= 0) { |
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2476 |
__borrow = 0; |
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2477 |
} else { |
4251 | 2478 |
__borrow = 1; |
4254 | 2479 |
/* __diff += 0x10000; */ |
4214
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2480 |
} |
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2481 |
((unsigned short *)(__resultDigits+__index-1))[0] = __diff; |
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2482 |
__index += 2; |
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2483 |
} |
4254 | 2484 |
|
2485 |
if (__index == __minLen) { |
|
4215 | 2486 |
/* one of the operands has odd length - cannot continue short-wise */ |
2487 |
} else { |
|
2488 |
if (__len1 > __len2) { |
|
2489 |
while (__index < __len1) { |
|
4251 | 2490 |
__diff = ((unsigned short *)(__myDigits+__index-1))[0] - __borrow; |
4215 | 2491 |
if (__diff >= 0) { |
2492 |
__borrow = 0; |
|
4254 | 2493 |
((unsigned short *)(__resultDigits+__index-1))[0] = __diff; |
2494 |
__index += 2; |
|
2495 |
||
4215 | 2496 |
/* copy over rest */ |
2497 |
while (__index < __len1) { |
|
2498 |
((unsigned short *)(__resultDigits+__index-1))[0] = ((unsigned short *)(__myDigits+__index-1))[0]; |
|
2499 |
__index+=2; |
|
2500 |
} |
|
4254 | 2501 |
if (__index <= __len1) { |
2502 |
__resultDigits[__index-1] = __myDigits[__index-1]; |
|
2503 |
__index++; |
|
2504 |
} |
|
4215 | 2505 |
break; |
2506 |
} |
|
4251 | 2507 |
__borrow = 1; |
4254 | 2508 |
/* __diff += 0x10000; */ |
4215 | 2509 |
((unsigned short *)(__resultDigits+__index-1))[0] = __diff; |
2510 |
__index += 2; |
|
2511 |
} |
|
2512 |
} else { |
|
2513 |
if (__len2 > __len1) { |
|
2514 |
while (__index < __len2) { |
|
4251 | 2515 |
__diff = 0 - ((unsigned short *)(__otherDigits+__index-1))[0] - __borrow; |
4215 | 2516 |
if (__diff >= 0) { |
2517 |
__borrow = 0; |
|
2518 |
} else { |
|
4251 | 2519 |
__borrow = 1; |
4254 | 2520 |
/* __diff += 0x10000; */ |
4215 | 2521 |
} |
2522 |
((unsigned short *)(__resultDigits+__index-1))[0] = __diff; |
|
2523 |
__index += 2; |
|
2524 |
} |
|
2525 |
} |
|
2526 |
} |
|
2527 |
} |
|
4214
ba20de3793e2
slightly tuned largeInt - largeInt for intel
Claus Gittinger <cg@exept.de>
parents:
4213
diff
changeset
|
2528 |
#endif |
4215 | 2529 |
/* |
2530 |
* subtract byte-wise |
|
2531 |
*/ |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2532 |
while (__index <= __minLen) { |
4251 | 2533 |
__diff = __myDigits[__index-1] - __otherDigits[__index-1] - __borrow; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2534 |
if (__diff >= 0) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2535 |
__borrow = 0; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2536 |
} else { |
4251 | 2537 |
__borrow = 1; |
4254 | 2538 |
/* __diff += 0x100; */ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2539 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2540 |
__resultDigits[__index-1] = __diff; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2541 |
__index++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2542 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2543 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2544 |
if (__len1 > __len2) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2545 |
while (__index <= __len1) { |
4251 | 2546 |
__diff = __myDigits[__index-1] - __borrow; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2547 |
if (__diff >= 0) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2548 |
__borrow = 0; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2549 |
/* copy over rest */ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2550 |
__resultDigits[__index-1] = __diff; |
4251 | 2551 |
__index++; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2552 |
while (__index <= __len1) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2553 |
__resultDigits[__index-1] = __myDigits[__index-1]; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2554 |
__index++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2555 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2556 |
break; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2557 |
} |
4251 | 2558 |
__borrow = 1; |
4254 | 2559 |
/* __diff += 0x100; */ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2560 |
__resultDigits[__index-1] = __diff; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2561 |
__index++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2562 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2563 |
} else { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2564 |
if (__len2 > __len1) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2565 |
while (__index <= __len2) { |
4251 | 2566 |
__diff = 0 - __otherDigits[__index-1] - __borrow; |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2567 |
if (__diff >= 0) { |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2568 |
__borrow = 0; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2569 |
} else { |
4251 | 2570 |
__borrow = 1; |
4254 | 2571 |
/* __diff += 0x100; */ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2572 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2573 |
__resultDigits[__index-1] = __diff; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2574 |
__index++; |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2575 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2576 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2577 |
} |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2578 |
borrow = __MKSMALLINT(__borrow); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2579 |
index = __MKSMALLINT(__index); |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2580 |
lastDigit = __MKSMALLINT(__resultDigits[__index-1-1]); |
4190 | 2581 |
} |
2582 |
%}. |
|
2583 |
ok == true ifFalse:[ |
|
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2584 |
index := 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2585 |
borrow := 0. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2586 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2587 |
done := false. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2588 |
[done] whileFalse:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2589 |
diff := borrow. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2590 |
(index <= len1) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2591 |
diff := diff + (digitByteArray basicAt:index). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2592 |
(index <= len2) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2593 |
diff := diff - (otherDigitByteArray basicAt:index) |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2594 |
] |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2595 |
] ifFalse:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2596 |
(index <= len2) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2597 |
diff := diff - (otherDigitByteArray basicAt:index) |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2598 |
] ifFalse:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2599 |
"end reached" |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2600 |
done := true |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2601 |
] |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2602 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2603 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2604 |
"/ workaround for |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2605 |
"/ gcc code generator bug |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2606 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2607 |
(diff >= 0) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2608 |
borrow := 0 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2609 |
] ifFalse:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2610 |
borrow := -1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2611 |
diff := diff + 16r100 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2612 |
]. |
4190 | 2613 |
|
2614 |
"/ (diff < 0) ifTrue:[ |
|
2615 |
"/ borrow := -1. |
|
2616 |
"/ diff := diff + 16r100 |
|
2617 |
"/ ] ifFalse:[ |
|
2618 |
"/ borrow := 0 |
|
2619 |
"/ ]. |
|
2620 |
||
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2621 |
resultDigitByteArray basicAt:index put:diff. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2622 |
index := index + 1 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2623 |
]. |
701 | 2624 |
]. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2625 |
|
701 | 2626 |
(borrow ~~ 0) ifTrue:[ |
4213
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2627 |
"/ must generate 255's complement |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2628 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2629 |
result sign:-1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2630 |
[index <= lResult] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2631 |
resultDigitByteArray basicAt:index put:16rFF. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2632 |
index := index + 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2633 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2634 |
index := resultDigitByteArray size. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2635 |
[index > 0] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2636 |
resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2637 |
index := index - 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2638 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2639 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2640 |
index := 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2641 |
carry := 1. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2642 |
[carry ~~ 0] whileTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2643 |
(index <= lResult) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2644 |
carry := (resultDigitByteArray basicAt:index) + carry. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2645 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2646 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2647 |
carry := carry bitShift:-8. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2648 |
index := index + 1 |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2649 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2650 |
lastDigit := resultDigitByteArray basicAt:(resultDigitByteArray size). |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2651 |
]. |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2652 |
|
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2653 |
(lastDigit ~~ 0) ifTrue:[ |
a66f711d86df
slightly tuned +/- for non-lsb machines
Claus Gittinger <cg@exept.de>
parents:
4211
diff
changeset
|
2654 |
^ result |
701 | 2655 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2656 |
^ result compressed |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2657 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2658 |
"Modified: 5.11.1996 / 14:09:25 / cg" |
701 | 2659 |
! |
2660 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2661 |
absMod:anInteger |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2662 |
"return a LargeIntegers representing |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2663 |
abs(self) \\ abs(theArgument). |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2664 |
Used as a helper for \\ and rem:" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2665 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2666 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2667 |
|dividend divisor |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2668 |
shift "{ Class: SmallInteger }" | |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2669 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2670 |
anInteger == 0 ifTrue:[ |
4162 | 2671 |
^ DivisionByZeroSignal raise |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2672 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2673 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2674 |
self = anInteger ifTrue:[ |
4162 | 2675 |
^ 0 |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2676 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2677 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2678 |
shift := self highBit - anInteger highBit. |
4140
3e66cfaae7a7
Some fixes for absMod: on a negative number
Stefan Vogel <sv@exept.de>
parents:
4139
diff
changeset
|
2679 |
dividend := self simpleDeepCopy sign:1. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2680 |
shift < 0 ifTrue:[ |
4162 | 2681 |
^ dividend. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2682 |
]. |
4139 | 2683 |
shift == 0 ifTrue:[ |
4162 | 2684 |
divisor := anInteger simpleDeepCopy. |
4139 | 2685 |
] ifFalse:[ |
4162 | 2686 |
divisor := anInteger bitShift:shift. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2687 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2688 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2689 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2690 |
shift := shift + 1. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2691 |
[shift > 0] whileTrue:[ |
4162 | 2692 |
(dividend absLess:divisor) ifFalse:[ |
2693 |
(dividend absSubtract: divisor) ifFalse:[ "result == 0" |
|
2694 |
^ dividend compressed |
|
2695 |
]. |
|
2696 |
]. |
|
2697 |
shift := shift - 1. |
|
2698 |
divisor div2. |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2699 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2700 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2701 |
^ dividend compressed |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2702 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2703 |
" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2704 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000001 absMod:4000000001] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2705 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000001 absDivMod:4000000001] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2706 |
16000000000 absMod:4000000000 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2707 |
16000000000 absMod:3000000000 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2708 |
" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2709 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2710 |
"Modified: / 5.11.1996 / 18:40:24 / cg" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2711 |
"Created: / 27.4.1999 / 19:45:44 / stefan" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2712 |
"Modified: / 3.5.1999 / 08:41:39 / stefan" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2713 |
! |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2714 |
|
68 | 2715 |
absMul:aLargeInteger |
2716 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
2717 |
||
92 | 2718 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 2719 |
idx "{ Class: SmallInteger }" |
2720 |
carry "{ Class: SmallInteger }" |
|
2721 |
len1 "{ Class: SmallInteger }" |
|
2722 |
len2 "{ Class: SmallInteger }" |
|
2723 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 2724 |
prod "{ Class: SmallInteger }" |
2725 |
v "{ Class: SmallInteger }"| |
|
68 | 2726 |
|
2727 |
len1 := digitByteArray size. |
|
2728 |
otherDigitByteArray := aLargeInteger digits. |
|
2729 |
len2 := otherDigitByteArray size. |
|
2730 |
||
4151 | 2731 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2). |
68 | 2732 |
resultDigitByteArray := result digits. |
92 | 2733 |
ok := false. |
2734 |
%{ |
|
1133 | 2735 |
if (__isByteArray(__INST(digitByteArray)) |
92 | 2736 |
&& __isByteArray(otherDigitByteArray) |
2737 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 2738 |
&& __bothSmallInteger(len1, len2)) { |
4231 | 2739 |
unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
2740 |
unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
2741 |
unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
2742 |
unsigned char *_p1, *_p2, *_pResult, *_pResult0, *_pResult1, *_p1Last, *_p2Last; |
|
2743 |
unsigned INT _v; |
|
2744 |
int _len1 = __intVal(len1); |
|
2745 |
int _len2 = __intVal(len2); |
|
2746 |
||
2747 |
_p1Last = myBytes + _len1 - 1; /* the last byte */ |
|
2748 |
_p2Last = otherBytes + _len2 - 1; /* the last byte */ |
|
2749 |
_pResult0 = resultBytes; |
|
2750 |
||
2751 |
/* |
|
2752 |
* aaa...aaa f1[0] * f2 |
|
2753 |
* bbb...bbb f1[1] * f2 |
|
2754 |
* ccc...ccc f1[2] * f2 |
|
2755 |
* ... |
|
2756 |
* xxx...xxx f1[high] * f2 |
|
2757 |
* |
|
2758 |
* start short-wise |
|
2759 |
* bounds: (16rFFFF * 16rFFFF) + 16rFFFF -> FFFF0000 |
|
2760 |
*/ |
|
2761 |
_p1 = myBytes; |
|
2762 |
||
2763 |
#if defined(alpha64) |
|
2764 |
/* loop over ints of f1 */ |
|
2765 |
for (; _p1 < _p1Last-2; _p1 += 4, _pResult0 += 4) { |
|
2766 |
unsigned INT word1 = ((unsigned int *)_p1)[0]; |
|
2767 |
||
2768 |
_pResult = _pResult0; |
|
2769 |
_p2 = otherBytes; |
|
2770 |
||
2771 |
/* loop over ints of f2 */ |
|
2772 |
while (_p2 < (_p2Last-2)) { |
|
2773 |
_v = (word1 * ((unsigned int *)_p2)[0]) + ((unsigned int *)_pResult)[0]; |
|
2774 |
((unsigned int *)_pResult)[0] = _v /* & 0xFFFFFFFF */; |
|
2775 |
_v >>= 32; /* now _v contains the carry*/ |
|
2776 |
_pResult += 4; |
|
2777 |
if (_v) { |
|
2778 |
/* distribute carry */ |
|
2779 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2780 |
_v += _pResult1[0]; |
|
2781 |
_pResult1[0] = _v /* & 0xFF */; |
|
2782 |
_v >>= 8; |
|
2783 |
} |
|
2784 |
} |
|
2785 |
_p2 += 4; |
|
2786 |
} |
|
2787 |
||
2788 |
/* possible odd highByte of f2 */ |
|
2789 |
while (_p2 <= _p2Last) { |
|
2790 |
_v = (word1 * _p2[0]) + ((unsigned int *)_pResult)[0]; |
|
2791 |
((unsigned int *)_pResult)[0] = _v /* & 0xFFFFFFFF */; |
|
2792 |
_v >>= 32; /* now _v contains the carry*/ |
|
2793 |
_pResult += 4; |
|
2794 |
if (_v) { |
|
2795 |
/* distribute carry */ |
|
2796 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2797 |
_v += _pResult1[0]; |
|
2798 |
_pResult1[0] = _v /* & 0xFF */; |
|
2799 |
_v >>= 8; |
|
2800 |
} |
|
2801 |
} |
|
2802 |
_p2++; |
|
2803 |
} |
|
2804 |
} |
|
2805 |
#endif /* alpha64 */ |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2806 |
|
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2807 |
#if defined(__LSBFIRST) || defined(alpha) || defined(i386) |
4231 | 2808 |
/* loop over shorts of f1 */ |
2809 |
for (; _p1 < _p1Last; _p1 += 2, _pResult0 += 2) { |
|
2810 |
unsigned int short1 = ((unsigned short *)_p1)[0]; |
|
2811 |
||
2812 |
_pResult = _pResult0; |
|
2813 |
_p2 = otherBytes; |
|
2814 |
||
2815 |
/* loop over shorts of f2 */ |
|
2816 |
while (_p2 < _p2Last) { |
|
2817 |
_v = (short1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0]; |
|
2818 |
((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */; |
|
2819 |
_v >>= 16; /* now _v contains the carry*/ |
|
2820 |
_pResult += 2; |
|
2821 |
if (_v) { |
|
2822 |
/* distribute carry */ |
|
2823 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2824 |
_v += _pResult1[0]; |
|
2825 |
_pResult1[0] = _v /* & 0xFF */; |
|
2826 |
_v >>= 8; |
|
2827 |
} |
|
2828 |
} |
|
2829 |
_p2 += 2; |
|
2830 |
} |
|
2831 |
||
2832 |
/* possible odd highByte of f2 */ |
|
2833 |
if (_p2 <= _p2Last) { |
|
2834 |
_v = (short1 * _p2[0]) + ((unsigned short *)_pResult)[0]; |
|
2835 |
((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */; |
|
2836 |
_v >>= 16; /* now _v contains the carry*/ |
|
2837 |
_pResult += 2; |
|
2838 |
if (_v) { |
|
2839 |
/* distribute carry */ |
|
2840 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2841 |
_v += _pResult1[0]; |
|
2842 |
_pResult1[0] = _v /* & 0xFF */; |
|
2843 |
_v >>= 8; |
|
2844 |
} |
|
2845 |
} |
|
2846 |
_p2++; |
|
2847 |
} |
|
2848 |
} |
|
4164
59da946fb307
oops - the short-multiply optimization is only ok
Claus Gittinger <cg@exept.de>
parents:
4163
diff
changeset
|
2849 |
#endif /* LSBFIRST */ |
59da946fb307
oops - the short-multiply optimization is only ok
Claus Gittinger <cg@exept.de>
parents:
4163
diff
changeset
|
2850 |
|
4231 | 2851 |
/* possible odd highByte of f1 (or byteLoop, if not LSBFIRST) */ |
2852 |
for (; _p1 <= _p1Last; _p1++, _pResult0++) { |
|
2853 |
unsigned int byte1 = _p1[0]; |
|
2854 |
||
2855 |
_pResult = _pResult0; |
|
2856 |
_p2 = otherBytes; |
|
4175
ecbeb3109903
fixed absMul: (for non-LSBFIRST)
Claus Gittinger <cg@exept.de>
parents:
4174
diff
changeset
|
2857 |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2858 |
#if defined(__LSBFIRST) || defined(alpha) || defined(i386) |
4231 | 2859 |
/* loop over shorts of f2 */ |
2860 |
while (_p2 < _p2Last) { |
|
2861 |
_v = (byte1 * ((unsigned short *)_p2)[0]) + ((unsigned short *)_pResult)[0]; |
|
2862 |
((unsigned short *)_pResult)[0] = _v /* & 0xFFFF */; |
|
2863 |
_v >>= 16; /* now _v contains the carry*/ |
|
2864 |
_pResult += 2; |
|
2865 |
if (_v) { |
|
2866 |
/* distribute carry */ |
|
2867 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2868 |
_v += _pResult1[0]; |
|
2869 |
_pResult1[0] = _v /* & 0xFF */; |
|
2870 |
_v >>= 8; |
|
2871 |
} |
|
2872 |
} |
|
2873 |
_p2 += 2; |
|
2874 |
} |
|
4164
59da946fb307
oops - the short-multiply optimization is only ok
Claus Gittinger <cg@exept.de>
parents:
4163
diff
changeset
|
2875 |
#endif /* __LSBFIRST */ |
59da946fb307
oops - the short-multiply optimization is only ok
Claus Gittinger <cg@exept.de>
parents:
4163
diff
changeset
|
2876 |
|
4231 | 2877 |
/* possible odd highByte of f2 (or byteLoop, if not LSBFIRST) */ |
2878 |
while (_p2 <= _p2Last) { |
|
2879 |
_v = (byte1 * _p2[0]) + _pResult[0]; |
|
2880 |
_pResult[0] = _v /* & 0xFF */; |
|
2881 |
_v >>= 8; /* now _v contains the carry*/ |
|
2882 |
_pResult++; |
|
2883 |
if (_v) { |
|
2884 |
/* distribute carry */ |
|
2885 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2886 |
_v += _pResult1[0]; |
|
2887 |
_pResult1[0] = _v /* & 0xFF */; |
|
2888 |
_v >>= 8; |
|
2889 |
} |
|
2890 |
} |
|
2891 |
_p2++; |
|
2892 |
} |
|
2893 |
} |
|
2894 |
ok = true; |
|
92 | 2895 |
} |
2896 |
%}. |
|
2897 |
ok ifFalse:[ |
|
4231 | 2898 |
1 to:len1 do:[:index1 | |
2899 |
1 to:len2 do:[:index2 | |
|
2900 |
dstIndex := index1 + index2 - 1. |
|
2901 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
2902 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
2903 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
2904 |
carry := prod bitShift:-8. |
|
2905 |
carry ~~ 0 ifTrue:[ |
|
2906 |
idx := dstIndex + 1. |
|
2907 |
[carry ~~ 0] whileTrue:[ |
|
2908 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
2909 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
2910 |
carry := v bitShift:-8. |
|
2911 |
idx := idx + 1 |
|
2912 |
] |
|
2913 |
] |
|
2914 |
] |
|
2915 |
]. |
|
68 | 2916 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2917 |
^ result compressed |
68 | 2918 |
! |
2919 |
||
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2920 |
absPlus:aLargeInteger sign:newSign |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2921 |
"return a LargeInteger representing abs(self) + abs(theArgument) |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2922 |
with sign: newSign. |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2923 |
The result is normalized. The argument must be a smallInteger |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2924 |
with byteSize one less than the integer byteSize. |
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
2925 |
This is a helper for addition and subtraction - not for public use." |
68 | 2926 |
|
2927 |
|result done otherDigitByteArray resultDigitByteArray |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2928 |
len1 "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2929 |
len2 "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2930 |
newLen "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2931 |
index "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2932 |
carry "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2933 |
sum "{ Class: SmallInteger }" | |
68 | 2934 |
|
2935 |
otherDigitByteArray := aLargeInteger digits. |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2936 |
|
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2937 |
%{ |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2938 |
OBJ _digitByteArray = __INST(digitByteArray); |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2939 |
|
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2940 |
if (__isByteArray(_digitByteArray) |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2941 |
&& __isByteArray(otherDigitByteArray)) { |
4215 | 2942 |
int _len1, _len2, _newLen; |
2943 |
unsigned char *_myDigits, *_otherDigits, *_newDigits; |
|
2944 |
int _index, _carry; |
|
4216 | 2945 |
int _comLen, _comLen2; |
4215 | 2946 |
|
2947 |
_len1 = __byteArraySize(_digitByteArray); |
|
2948 |
_len2 = __byteArraySize(otherDigitByteArray); |
|
2949 |
||
2950 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
2951 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
|
2952 |
||
2953 |
if (_len1 < _len2) { |
|
2954 |
_comLen = _len1; |
|
2955 |
_newLen = _len2; |
|
2956 |
if (_otherDigits[_len2 - 1] == 0xFF) _newLen++; |
|
2957 |
} else if (_len2 < _len1) { |
|
2958 |
_comLen = _len2; |
|
2959 |
_newLen = _len1; |
|
2960 |
if (_myDigits[_len1 - 1] == 0xFF) _newLen++; |
|
2961 |
} else { |
|
2962 |
/* |
|
2963 |
* there can only be an overflow from the high bytes, |
|
2964 |
* if their sum is >= 255 |
|
4216 | 2965 |
* (with sum==255, a carry could still occur from the next lower bytes) |
4215 | 2966 |
*/ |
2967 |
_newLen = _len1; |
|
2968 |
if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) { |
|
2969 |
_newLen++; |
|
2970 |
} else { |
|
2971 |
if (_newLen == sizeof(INT)) { |
|
2972 |
/* |
|
2973 |
* two 32bit numbers, no carry - a very common case ... |
|
2974 |
*/ |
|
4216 | 2975 |
#if defined(__LSB_FIRST) || defined(i386) || defined(alpha) |
4215 | 2976 |
unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits; |
4216 | 2977 |
#else |
2978 |
unsigned INT _sum = __longIntVal(self) + __longIntVal(aLargeInteger); |
|
2979 |
#endif /* not LSB_FIRST */ |
|
4215 | 2980 |
RETURN (__MKULARGEINT(_sum)); |
2981 |
} |
|
2982 |
} |
|
2983 |
_comLen = _len1; |
|
2984 |
} |
|
2985 |
resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen); |
|
2986 |
||
2987 |
/* |
|
2988 |
* must refetch - GC could have been invoked |
|
2989 |
*/ |
|
2990 |
_digitByteArray = __INST(digitByteArray); |
|
2991 |
||
2992 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
|
2993 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
2994 |
_newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
2995 |
||
2996 |
/* |
|
2997 |
* add them ... |
|
2998 |
*/ |
|
2999 |
_index = 1; |
|
3000 |
_carry = 0; |
|
4172
3fb2b94e7535
tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents:
4171
diff
changeset
|
3001 |
|
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
3002 |
#if defined(__LSBFIRST) || defined(alpha) || defined(i386) |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
3003 |
# ifdef INT64 |
4215 | 3004 |
/* |
3005 |
* have a 64bit integer type; |
|
3006 |
* add long-wise |
|
3007 |
* accessing bytes at: [index-1][index][index+1][index+1] |
|
3008 |
*/ |
|
4216 | 3009 |
_comLen2 = _comLen - 2; |
3010 |
while (_index < _comLen2) { |
|
4215 | 3011 |
UINT64 _sum; |
3012 |
||
3013 |
_sum = _carry; |
|
3014 |
_sum += *(unsigned *)(&(_myDigits[_index - 1])); |
|
3015 |
_sum += *(unsigned *)(&(_otherDigits[_index - 1])); |
|
3016 |
_carry = _sum >> 32; |
|
3017 |
_sum = _sum & 0xFFFFFFFF; |
|
3018 |
*(unsigned *)(&(_newDigits[_index - 1])) = _sum; |
|
3019 |
_index += 4; |
|
3020 |
} |
|
4172
3fb2b94e7535
tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents:
4171
diff
changeset
|
3021 |
# else |
3fb2b94e7535
tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents:
4171
diff
changeset
|
3022 |
# if defined(i386) && defined(__GNUC__) |
4215 | 3023 |
/* |
4216 | 3024 |
* add long-wise |
4215 | 3025 |
* accessing bytes at: [index-1][index][index+1][index+1] |
3026 |
*/ |
|
4216 | 3027 |
_comLen2 = _comLen - 2; |
3028 |
while (_index < _comLen2) { |
|
4215 | 3029 |
int _sum; |
3030 |
||
3031 |
asm ("addl %%edx,%%eax \n |
|
3032 |
movl $0,%%edx \n |
|
3033 |
adcl $0,%%edx \n |
|
3034 |
addl %4,%%eax \n |
|
3035 |
adcl $0,%%edx" |
|
3036 |
: "=d" ((unsigned long)(_carry)), |
|
3037 |
"=a" ((unsigned long)(_sum)) |
|
3038 |
: "0" ((unsigned long)(_carry)), |
|
3039 |
"1" ((unsigned long)(*(unsigned *)(&(_myDigits[_index - 1])))), |
|
3040 |
"rm" ((unsigned long)(*(unsigned *)(&(_otherDigits[_index - 1])))) |
|
3041 |
); |
|
3042 |
||
3043 |
*(unsigned *)(&(_newDigits[_index - 1])) = _sum; |
|
3044 |
_index += 4; |
|
3045 |
} |
|
4172
3fb2b94e7535
tune largeInt + largeInt (for i386)
Claus Gittinger <cg@exept.de>
parents:
4171
diff
changeset
|
3046 |
# endif |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
3047 |
# endif |
4215 | 3048 |
/* |
3049 |
* add short-wise |
|
4216 | 3050 |
* accessing bytes at: [index-1][index] |
4215 | 3051 |
*/ |
3052 |
while (_index < _comLen) { |
|
3053 |
unsigned int _sum; |
|
3054 |
||
3055 |
_sum = _carry; |
|
3056 |
_sum += *(unsigned short *)(&(_myDigits[_index - 1])); |
|
3057 |
_sum += *(unsigned short *)(&(_otherDigits[_index - 1])); |
|
3058 |
_carry = _sum >> 16; |
|
3059 |
_sum = _sum & 0xFFFF; |
|
3060 |
*(unsigned short *)(&(_newDigits[_index - 1])) = _sum; |
|
3061 |
_index += 2; |
|
3062 |
} |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
3063 |
#endif |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
3064 |
|
4215 | 3065 |
/* |
4216 | 3066 |
* add byte-wise |
4215 | 3067 |
*/ |
3068 |
while (_index <= _comLen) { |
|
3069 |
unsigned int _sum; |
|
3070 |
||
3071 |
_sum = _carry; |
|
3072 |
_sum += _myDigits[_index - 1]; |
|
3073 |
_sum += _otherDigits[_index - 1]; |
|
3074 |
_carry = _sum >> 8; |
|
3075 |
_sum = _sum & 0xFF; |
|
3076 |
_newDigits[_index - 1] = _sum; |
|
3077 |
_index++; |
|
3078 |
} |
|
3079 |
||
3080 |
/* |
|
3081 |
* rest |
|
3082 |
*/ |
|
4229
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3083 |
if (_len1 > _len2) { |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3084 |
while (_index <= _len1) { |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3085 |
unsigned int _sum; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3086 |
|
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3087 |
_sum = _carry; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3088 |
_sum += _myDigits[_index - 1]; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3089 |
_carry = _sum >> 8; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3090 |
_sum = _sum & 0xFF; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3091 |
_newDigits[_index - 1] = _sum; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3092 |
_index++; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3093 |
} |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3094 |
} else { |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3095 |
if (_len2 > _len1) { |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3096 |
while (_index <= _len2) { |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3097 |
unsigned int _sum; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3098 |
|
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3099 |
_sum = _carry; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3100 |
_sum += _otherDigits[_index - 1]; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3101 |
_carry = _sum >> 8; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3102 |
_sum = _sum & 0xFF; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3103 |
_newDigits[_index - 1] = _sum; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3104 |
_index++; |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3105 |
} |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3106 |
} |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3107 |
} |
7956ab97154f
speedup adding a large-largeInt with a small-largeInt
Claus Gittinger <cg@exept.de>
parents:
4222
diff
changeset
|
3108 |
|
4215 | 3109 |
while (_index <= _newLen) { |
3110 |
unsigned int _sum; |
|
3111 |
||
3112 |
_sum = _carry; |
|
3113 |
_carry = _sum >> 8; |
|
3114 |
_sum = _sum & 0xFF; |
|
3115 |
_newDigits[_index - 1] = _sum; |
|
3116 |
_index++; |
|
3117 |
} |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
3118 |
} |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
3119 |
%}. |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
3120 |
resultDigitByteArray notNil ifTrue:[ |
4215 | 3121 |
result := self class basicNew. |
3122 |
result setDigits:resultDigitByteArray. |
|
3123 |
result sign:newSign. |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
3124 |
] ifFalse:[ |
4215 | 3125 |
len1 := digitByteArray size. |
3126 |
len2 := otherDigitByteArray size. |
|
3127 |
||
3128 |
"/ earlier versions estimated the newLength as: |
|
3129 |
"/ (len1 max:len2) + 1 |
|
3130 |
"/ and reduced the result. |
|
3131 |
"/ however, if one of the addends is smaller, |
|
3132 |
"/ the result will never require another digit, |
|
3133 |
"/ if the highest digit of the larger addent is |
|
3134 |
"/ not equal to 255. Therefore, in most cases, |
|
3135 |
"/ we can avoid the computation and resizing |
|
3136 |
"/ in #reduced. |
|
3137 |
||
3138 |
len1 < len2 ifTrue:[ |
|
3139 |
newLen := len2. |
|
3140 |
(otherDigitByteArray at:len2) == 16rFF ifTrue:[ |
|
3141 |
newLen := newLen + 1 |
|
3142 |
] |
|
3143 |
] ifFalse:[ |
|
3144 |
len2 < len1 ifTrue:[ |
|
3145 |
newLen := len1. |
|
3146 |
(digitByteArray at:len1) == 16rFF ifTrue:[ |
|
3147 |
newLen := newLen + 1 |
|
3148 |
] |
|
3149 |
] ifFalse:[ |
|
3150 |
newLen := len1 + 1. |
|
3151 |
] |
|
3152 |
]. |
|
3153 |
||
3154 |
result := self class basicNew numberOfDigits:newLen. |
|
3155 |
result sign:newSign. |
|
3156 |
resultDigitByteArray := result digits. |
|
3157 |
||
3158 |
index := 1. |
|
3159 |
carry := 0. |
|
3160 |
||
3161 |
done := false. |
|
3162 |
[done] whileFalse:[ |
|
3163 |
sum := carry. |
|
3164 |
(index <= len1) ifTrue:[ |
|
3165 |
sum := sum + (digitByteArray basicAt:index). |
|
3166 |
(index <= len2) ifTrue:[ |
|
3167 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
3168 |
] |
|
3169 |
] ifFalse:[ |
|
3170 |
(index <= len2) ifTrue:[ |
|
3171 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
3172 |
] ifFalse:[ |
|
3173 |
"end reached" |
|
3174 |
done := true |
|
3175 |
] |
|
3176 |
]. |
|
3177 |
(sum >= 16r100) ifTrue:[ |
|
3178 |
carry := 1. |
|
3179 |
sum := sum - 16r100 |
|
3180 |
] ifFalse:[ |
|
3181 |
carry := 0 |
|
3182 |
]. |
|
3183 |
resultDigitByteArray basicAt:index put:sum. |
|
3184 |
index := index + 1 |
|
3185 |
]. |
|
68 | 3186 |
]. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
3187 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
3188 |
^ result compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
3189 |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
3190 |
"Modified: 11.8.1997 / 03:23:37 / cg" |
68 | 3191 |
! |
3192 |
||
1890 | 3193 |
absSubtract:aLargeInteger |
4215 | 3194 |
"private helper for division: |
3195 |
destructively subtract aLargeInteger from myself |
|
4254 | 3196 |
AND return true, if the result is non-zero, false otherwise. |
4215 | 3197 |
(i.e. this method has both a return value and a side-effect |
3198 |
on the receiver) |
|
3199 |
Only allowed for positive receiver and argument |
|
3200 |
The receiver must be >= the argument. |
|
3201 |
The receiver must be a temporary scratch-number" |
|
1890 | 3202 |
|
4187
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3203 |
|otherDigitByteArray |
1890 | 3204 |
len1 "{ Class: SmallInteger }" |
3205 |
len2 "{ Class: SmallInteger }" |
|
3206 |
index "{ Class: SmallInteger }" |
|
3207 |
borrow "{ Class: SmallInteger }" |
|
3208 |
diff "{ Class: SmallInteger }" |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3209 |
notZero |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3210 |
| |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3211 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3212 |
notZero := false. |
1890 | 3213 |
len1 := digitByteArray size. |
3214 |
otherDigitByteArray := aLargeInteger digits. |
|
3215 |
len2 := otherDigitByteArray size. |
|
3216 |
len2 > len1 ifTrue:[ |
|
4215 | 3217 |
[(otherDigitByteArray at:len2) == 0] whileTrue:[ |
3218 |
len2 := len2 - 1 |
|
3219 |
]. |
|
3220 |
len2 > len1 ifTrue:[ |
|
3221 |
self halt "/ may not be called that way |
|
3222 |
]. |
|
1890 | 3223 |
]. |
4187
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3224 |
"/ knowing that len2 is <= len1 |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3225 |
%{ |
4189 | 3226 |
|
4187
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3227 |
OBJ _digitByteArray = __INST(digitByteArray); |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3228 |
|
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3229 |
if (__isByteArray(_digitByteArray) |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3230 |
&& __isByteArray(otherDigitByteArray)) { |
4215 | 3231 |
int _len1 = __intVal(len1), |
3232 |
_len2 = __intVal(len2); |
|
3233 |
unsigned char *_myDigits, *_otherDigits; |
|
3234 |
int _index = 1, _borrow = 0; |
|
3235 |
int _diff; |
|
3236 |
int anyBitNonZero = 0; |
|
3237 |
||
3238 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
3239 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
|
3240 |
||
3241 |
#if defined(i386) || defined(__LSBFIRST) |
|
3242 |
/* |
|
3243 |
* subtract short-wise |
|
3244 |
*/ |
|
3245 |
while (_index < _len2) { |
|
4251 | 3246 |
_diff = ((unsigned short *)(_myDigits+_index-1))[0] |
3247 |
- ((unsigned short *)(_otherDigits+_index-1))[0] |
|
3248 |
- _borrow; |
|
4215 | 3249 |
if (_diff >= 0) { |
3250 |
_borrow = 0; |
|
3251 |
} else { |
|
4251 | 3252 |
_borrow = 1; |
4254 | 3253 |
/* _diff += 0x10000; */ |
4215 | 3254 |
} |
3255 |
((unsigned short *)(_myDigits+_index-1))[0] = _diff; |
|
3256 |
anyBitNonZero |= (_diff & 0xFFFF); |
|
3257 |
_index += 2; |
|
3258 |
} |
|
4254 | 3259 |
|
4215 | 3260 |
if (_index <= _len2) { |
3261 |
/* |
|
3262 |
* cannot continue with shorts - there is an odd number of |
|
3263 |
* bytes in the minuent |
|
3264 |
*/ |
|
3265 |
} else { |
|
3266 |
while (_index < _len1) { |
|
4251 | 3267 |
_diff = ((unsigned short *)(_myDigits+_index-1))[0] - _borrow; |
4215 | 3268 |
if (_diff >= 0) { |
3269 |
/* _borrow = 0; */ |
|
3270 |
((unsigned short *)(_myDigits+_index-1))[0] = _diff; |
|
3271 |
anyBitNonZero |= (_diff & 0xFFFF); |
|
3272 |
_index += 2; |
|
3273 |
while (_index < _len1) { |
|
3274 |
anyBitNonZero |= ((unsigned short *)(_myDigits+_index-1))[0]; |
|
3275 |
_index += 2; |
|
3276 |
} |
|
3277 |
/* last odd index */ |
|
3278 |
if (_index <= _len1) { |
|
3279 |
anyBitNonZero |= _myDigits[_index - 1];; |
|
3280 |
_index++; |
|
3281 |
} |
|
3282 |
RETURN (anyBitNonZero ? true : false); |
|
3283 |
} |
|
4251 | 3284 |
_borrow = 1; |
4254 | 3285 |
/* _diff += 0x10000; */ |
4215 | 3286 |
|
3287 |
((unsigned short *)(_myDigits+_index-1))[0] = _diff; |
|
3288 |
anyBitNonZero |= (_diff & 0xFFFF); |
|
3289 |
_index += 2; |
|
3290 |
} |
|
3291 |
} |
|
3292 |
#endif |
|
3293 |
/* |
|
3294 |
* subtract byte-wise |
|
3295 |
*/ |
|
3296 |
while (_index <= _len2) { |
|
4251 | 3297 |
_diff = _myDigits[_index - 1]; |
3298 |
- _otherDigits[_index - 1] |
|
3299 |
- _borrow; |
|
4215 | 3300 |
if (_diff >= 0) { |
3301 |
_borrow = 0; |
|
3302 |
} else { |
|
4251 | 3303 |
_borrow = 1; |
4254 | 3304 |
/* _diff += 0x100; */ |
4215 | 3305 |
} |
3306 |
_myDigits[_index - 1] = _diff; |
|
3307 |
anyBitNonZero |= (_diff & 0xFF); |
|
3308 |
_index++; |
|
3309 |
} |
|
3310 |
||
3311 |
while (_index <= _len1) { |
|
4251 | 3312 |
_diff = _myDigits[_index - 1] - _borrow; |
4215 | 3313 |
if (_diff >= 0) { |
3314 |
/* _borrow = 0; */ |
|
3315 |
_myDigits[_index - 1] = _diff; |
|
3316 |
anyBitNonZero |= (_diff & 0xFF); |
|
3317 |
_index++; |
|
3318 |
while (_index <= _len1) { |
|
3319 |
anyBitNonZero |= _myDigits[_index - 1]; |
|
3320 |
_index++; |
|
3321 |
} |
|
3322 |
break; |
|
3323 |
} |
|
4251 | 3324 |
_borrow = 1; |
4254 | 3325 |
/* _diff += 0x100; */ |
4215 | 3326 |
|
3327 |
_myDigits[_index - 1] = _diff; |
|
3328 |
anyBitNonZero |= (_diff & 0xFF); |
|
3329 |
_index++; |
|
3330 |
} |
|
3331 |
RETURN (anyBitNonZero ? true : false); |
|
4187
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3332 |
} |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3333 |
%}. |
4215 | 3334 |
self halt:'should not happen'. |
4187
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3335 |
|
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3336 |
"/ index := 1. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3337 |
"/ borrow := 0. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3338 |
"/ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3339 |
"/ [index <= len1] whileTrue:[ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3340 |
"/ diff := borrow. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3341 |
"/ diff := diff + (digitByteArray basicAt:index). |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3342 |
"/ index <= len2 ifTrue:[ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3343 |
"/ diff := diff - (otherDigitByteArray basicAt:index). |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3344 |
"/ ]. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3345 |
"/ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3346 |
"/ "/ workaround for |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3347 |
"/ "/ gcc code generator bug |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3348 |
"/ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3349 |
"/ (diff >= 0) ifTrue:[ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3350 |
"/ borrow := 0 |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3351 |
"/ ] ifFalse:[ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3352 |
"/ borrow := -1. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3353 |
"/ diff := diff + 16r100 |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3354 |
"/ ]. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3355 |
"/ diff ~~ 0 ifTrue:[ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3356 |
"/ notZero := true |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3357 |
"/ ]. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3358 |
"/ digitByteArray basicAt:index put:diff. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3359 |
"/ index := index + 1 |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3360 |
"/ ]. |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3361 |
"/ |
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3362 |
"/ ^ notZero |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3363 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3364 |
"Created: / 5.11.1996 / 16:23:47 / cg" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3365 |
"Modified: / 5.11.1996 / 18:56:50 / cg" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
3366 |
"Modified: / 27.4.1999 / 18:08:23 / stefan" |
1890 | 3367 |
! |
3368 |
||
3369 |
div2 |
|
4215 | 3370 |
"private helper for division: |
3371 |
destructively divide the receiver by 2." |
|
1890 | 3372 |
|
3373 |
%{ /* NOCONTEXT */ |
|
3374 |
OBJ __digits = __INST(digitByteArray); |
|
3375 |
||
3376 |
if (__isByteArray(__digits)) { |
|
4215 | 3377 |
int __nBytes = __byteArraySize(__digits); |
3378 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
3379 |
unsigned INT __this, __next; |
|
3380 |
int __idx; |
|
3381 |
||
3382 |
if (__nBytes == 1) { |
|
3383 |
__bp[0] >>= 1; |
|
3384 |
RETURN (self); |
|
3385 |
} |
|
3386 |
||
3387 |
__idx = 1; |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3388 |
|
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3389 |
#if defined(alpha64) |
4215 | 3390 |
if (sizeof(unsigned INT) == 8) { |
3391 |
int __endIndex = __nBytes - 8; |
|
3392 |
||
3393 |
if (__idx < __endIndex) { |
|
3394 |
__this = ((unsigned INT *)__bp)[0]; |
|
3395 |
||
3396 |
while (__idx < __endIndex) { |
|
3397 |
__next = ((unsigned INT *)__bp)[1]; |
|
3398 |
__this = (__this >> 1) /* & 0x7FFFFFFFFFFFFFF */; |
|
3399 |
__this |= __next << 63; |
|
3400 |
((unsigned INT *)__bp)[0] = __this; |
|
3401 |
__this = __next; |
|
3402 |
__bp += 8; |
|
3403 |
__idx += 8; |
|
3404 |
} |
|
3405 |
} |
|
3406 |
} |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3407 |
#else |
4215 | 3408 |
# if defined(__LSBFIRST) || defined(i386) |
3409 |
if (sizeof(unsigned INT) == 4) { |
|
3410 |
int __endIndex = __nBytes - 4; |
|
3411 |
||
3412 |
if (__idx < __endIndex) { |
|
3413 |
__this = ((unsigned INT *)__bp)[0]; |
|
3414 |
||
3415 |
while (__idx < __endIndex) { |
|
3416 |
__next = ((unsigned INT *)__bp)[1]; |
|
3417 |
__this = (__this >> 1) /* & 0x7FFFFFF */; |
|
3418 |
__this |= __next << 31; |
|
3419 |
((unsigned INT *)__bp)[0] = __this; |
|
3420 |
__this = __next; |
|
3421 |
__bp += 4; |
|
3422 |
__idx += 4; |
|
3423 |
} |
|
3424 |
} |
|
3425 |
} |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3426 |
# endif |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
3427 |
#endif |
1890 | 3428 |
|
4215 | 3429 |
__this = __bp[0]; |
3430 |
while (__idx < __nBytes) { |
|
3431 |
__next = __bp[1]; |
|
3432 |
__this >>= 1; |
|
3433 |
__this |= __next << 7; |
|
3434 |
__bp[0] = __this; |
|
3435 |
__this = __next; |
|
3436 |
__bp++; |
|
3437 |
__idx++; |
|
3438 |
} |
|
3439 |
__bp[0] = __this >> 1; |
|
3440 |
RETURN (self); |
|
1890 | 3441 |
} |
3442 |
%}. |
|
3443 |
self primitiveFailed |
|
3444 |
||
3445 |
" |
|
4187
3dc575bd3915
tuned absSubtract: (which is a helper for large-division)
Claus Gittinger <cg@exept.de>
parents:
4185
diff
changeset
|
3446 |
100000 asLargeInteger div2 |
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3447 |
1000000000000000000000000000 div2 |
1890 | 3448 |
" |
3449 |
||
3450 |
"Modified: 5.11.1996 / 16:12:56 / cg" |
|
3451 |
! |
|
3452 |
||
3453 |
mul2 |
|
4215 | 3454 |
"private helper for division: |
3455 |
destructively multiply the receiver by 2." |
|
1890 | 3456 |
|
3457 |
|nBytes "{ Class: SmallInteger }" |
|
3458 |
b "{ Class: SmallInteger }" |
|
3459 |
t| |
|
3460 |
||
3461 |
nBytes := digitByteArray size. |
|
3462 |
||
3463 |
b := digitByteArray at:nBytes. |
|
3464 |
(b bitAnd:16r80) ~~ 0 ifTrue:[ |
|
4215 | 3465 |
"/ need another byte |
3466 |
nBytes := nBytes + 1. |
|
3467 |
t := ByteArray uninitializedNew:nBytes. |
|
3468 |
t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1. |
|
3469 |
t at:nBytes put:0. |
|
3470 |
digitByteArray := t. |
|
1890 | 3471 |
]. |
3472 |
||
3473 |
%{ |
|
3474 |
OBJ __digits = __INST(digitByteArray); |
|
3475 |
||
3476 |
if (__isByteArray(__digits)) { |
|
4215 | 3477 |
int __nBytes = __intVal(nBytes); |
3478 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
3479 |
unsigned INT __carry = 0, __newCarry, __this; |
|
3480 |
int __idx; |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3481 |
|
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3482 |
#if defined(alpha64) |
4215 | 3483 |
if (sizeof(unsigned INT) == 8) { |
3484 |
while (__nBytes >= 8) { |
|
3485 |
__this = ((unsigned INT *)__bp)[0]; |
|
3486 |
__newCarry = (__this >> 63) /* & 1 */; |
|
3487 |
((unsigned INT *)__bp)[0] = (__this << 1) | __carry; |
|
3488 |
__carry = __newCarry; |
|
3489 |
__bp += 8; |
|
3490 |
__nBytes -= 8; |
|
3491 |
} |
|
3492 |
} |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3493 |
#else |
4174
62498b719e87
pass new sign to absPlus/absMinus, to allow for compress optimizations
Claus Gittinger <cg@exept.de>
parents:
4173
diff
changeset
|
3494 |
# if defined(__LSBFIRST) || defined(i386) /* XXX actually: LSB_FIRST */ |
4215 | 3495 |
if (sizeof(unsigned INT) == 4) { |
3496 |
while (__nBytes >= 4) { |
|
3497 |
__this = ((unsigned INT *)__bp)[0]; |
|
3498 |
__newCarry = (__this >> 31) /* & 1 */; |
|
3499 |
((unsigned INT *)__bp)[0] = (__this << 1) | __carry; |
|
3500 |
__carry = __newCarry; |
|
3501 |
__bp += 4; |
|
3502 |
__nBytes -= 4; |
|
3503 |
} |
|
3504 |
} |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3505 |
# endif |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
3506 |
#endif |
4215 | 3507 |
while (__nBytes) { |
3508 |
__this = __bp[0]; |
|
3509 |
__newCarry = (__this >> 7) /* & 1 */; |
|
3510 |
__bp[0] = (__this << 1) | __carry; |
|
3511 |
__carry = __newCarry; |
|
3512 |
__bp++; |
|
3513 |
__nBytes--; |
|
3514 |
} |
|
3515 |
RETURN (self); |
|
1890 | 3516 |
} |
3517 |
%}. |
|
3518 |
self primitiveFailed |
|
3519 |
||
3520 |
" |
|
3521 |
100000 asLargeInteger mul2 |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3522 |
10000000000000000000000000000 mul2 |
1890 | 3523 |
" |
3524 |
||
3525 |
"Modified: 5.11.1996 / 16:37:32 / cg" |
|
68 | 3526 |
! |
3527 |
||
3528 |
mul256 |
|
4215 | 3529 |
"private helper for division: |
3530 |
destructively multiply the receiver by 256." |
|
68 | 3531 |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3532 |
|newDigits newSize| |
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3533 |
|
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3534 |
newSize := digitByteArray size + 1. |
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3535 |
newDigits := ByteArray uninitializedNew:newSize. |
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3536 |
"/ newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3537 |
newDigits replaceBytesFrom:2 to:newSize with:digitByteArray startingAt:1. |
68 | 3538 |
newDigits at:1 put:0. |
3539 |
digitByteArray := newDigits |
|
4160
5fb87a530e29
tuned div2, mul2 for alpha64;
Claus Gittinger <cg@exept.de>
parents:
4157
diff
changeset
|
3540 |
|
4215 | 3541 |
"Modified: / 20.5.1999 / 09:16:42 / cg" |
701 | 3542 |
! |
68 | 3543 |
|
701 | 3544 |
numberOfDigits:nDigits |
2001 | 3545 |
"/ digitByteArray := ByteArray uninitializedNew:nDigits. |
701 | 3546 |
digitByteArray := ByteArray new:nDigits. |
3547 |
sign := 1. |
|
68 | 3548 |
! |
3549 |
||
701 | 3550 |
setDigits:digits |
3551 |
digitByteArray := digits. |
|
3552 |
sign := 1. |
|
68 | 3553 |
! |
3554 |
||
701 | 3555 |
sign:aNumber |
3556 |
sign := aNumber |
|
1 | 3557 |
! ! |
3558 |
||
3559 |
!LargeInteger methodsFor:'testing'! |
|
3560 |
||
701 | 3561 |
even |
3562 |
"return true if the receiver is even" |
|
1 | 3563 |
|
701 | 3564 |
^ (digitByteArray at:1) even |
1 | 3565 |
! |
3566 |
||
68 | 3567 |
negative |
3568 |
"return true, if the receiver is < 0" |
|
3569 |
||
3570 |
^ (sign < 0) |
|
3571 |
! |
|
3572 |
||
1 | 3573 |
odd |
3574 |
"return true if the receiver is odd" |
|
3575 |
||
1983 | 3576 |
^ (digitByteArray at:1) odd |
3577 |
||
3578 |
"Modified: 18.11.1996 / 22:19:19 / cg" |
|
1 | 3579 |
! |
3580 |
||
3581 |
positive |
|
3582 |
"return true, if the receiver is >= 0" |
|
3583 |
||
3584 |
^ (sign >= 0) |
|
3585 |
! |
|
3586 |
||
701 | 3587 |
sign |
3588 |
"return the sign of the receiver" |
|
3589 |
||
3590 |
^ sign |
|
3591 |
! |
|
3592 |
||
1 | 3593 |
strictlyPositive |
3594 |
"return true, if the receiver is > 0" |
|
3595 |
||
3596 |
^ (sign > 0) |
|
3597 |
! ! |
|
3598 |
||
1794 | 3599 |
!LargeInteger class methodsFor:'documentation'! |
1 | 3600 |
|
701 | 3601 |
version |
4254 | 3602 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.113 1999-06-02 12:56:46 cg Exp $' |
1 | 3603 |
! ! |