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