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