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