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