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