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