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