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