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