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