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