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