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