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