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