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