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