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