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