author | Claus Gittinger <cg@exept.de> |
Sat, 08 May 1999 18:38:51 +0200 | |
changeset 4157 | f9a673441ba5 |
parent 4151 | 6d6971c27567 |
child 4160 | 5fb87a530e29 |
permissions | -rw-r--r-- |
1 | 1 |
" |
68 | 2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
345 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Integer subclass:#LargeInteger |
|
1210 | 14 |
instanceVariableNames:'sign digitByteArray' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
1794 | 20 |
!LargeInteger class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
345 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
This class provides arbitrary precision integers. These are represented as: |
|
39 |
sign (-1/0/+1) and, if sign ~~ 0 |
|
40 |
a ByteArray of digits with 8 bits per element; |
|
88 | 41 |
least significant 8 bits at index 1 ... |
1 | 42 |
|
88 | 43 |
The implementation is definitely not tuned for high performance |
44 |
(some key-methods should be rewritten as primitives), |
|
77 | 45 |
but ok for now, since LargeIntegers are not used very often. |
46 |
It will be reimplemented when everything else runs fine and a need arises |
|
47 |
(or someone rewrites it and sends me the code :-). |
|
48 |
||
49 |
For real speed, its implementation should be mapped onto a tuned arbitrary |
|
88 | 50 |
precision math package (maybe even special-cased for 32 and 64 bit |
51 |
large-ints, since those are the most common). |
|
52 |
||
53 |
LargeIntegers are usually not created explicitely, but result from |
|
54 |
SmallInteger arithmetic overflowing the SmallInteger range. |
|
357 | 55 |
Also, results of LargeInteger operations are converted to back to |
56 |
SmallIntegers, when possible (see normalize). |
|
92 | 57 |
|
58 |
In contrast to ST-80, there is only one class for LargeIntegers, keeping |
|
59 |
the sign as an instance variable (ST-80 has LargePositiveInteger and |
|
60 |
LargeNegativeInteger). This may change. |
|
1295 | 61 |
|
62 |
[author:] |
|
2001 | 63 |
Claus Gittinger |
1556 | 64 |
|
65 |
[see also:] |
|
2001 | 66 |
Number |
67 |
Float Fraction FixedPoint |
|
68 |
SmallInteger |
|
77 | 69 |
" |
2923 | 70 |
! |
71 |
||
72 |
testing |
|
73 |
" |
|
74 |
test largeInt multiplication & division |
|
75 |
(at least, test if they have not both errors which anull each other ;-) |
|
76 |
||
77 |
|last v| |
|
78 |
||
2952 | 79 |
v := last := 1. |
2923 | 80 |
2 to:3000 do:[:i | |
81 |
i printCR. |
|
2952 | 82 |
v := v * i. |
2923 | 83 |
(v / i) ~= last ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
84 |
self halt |
2923 | 85 |
]. |
86 |
last := v. |
|
87 |
] |
|
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
88 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
89 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
90 |
test addition, subtraction: |
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 maxVal -> 1073741823 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
93 |
SmallInteger maxVal + 1 -> 1073741824 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
94 |
SmallInteger maxVal + 2 -> 1073741825 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
95 |
(SmallInteger maxVal + 2) - 2 -> 1073741823 |
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 |
SmallInteger minVal -> -1073741824 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
98 |
SmallInteger minVal - 1 -> -1073741825 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
99 |
SmallInteger minVal - 2 -> -1073741826 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
100 |
(SmallInteger minVal - 2) + 2 -> -1073741824 |
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 |
1234567890 + 10 -> 1234567900 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
103 |
1111111111 + 1111111111 -> 2222222222 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
104 |
1111111111 - 1111111111 -> 0 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
105 |
1111111111 - 2222222222 -> -1111111111 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
106 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
107 |
1111111111 * 2 -> 2222222222 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
108 |
1111111111 * -2 -> -2222222222 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
109 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
110 |
1111111111 * 1111111111 -> 1234567900987654321 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
111 |
1234567900987654321 // 1111111111 -> 1111111111 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
112 |
1234567900987654321 \\ 1111111111 -> 0 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
113 |
1234567900987654322 \\ 1111111111 -> 1 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
114 |
1234567900987654421 \\ 1111111111 -> 100 |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
115 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
116 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
117 |
test comparison: |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
118 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
119 |
-1234567890 > -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
120 |
-1234567890 >= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
121 |
-1234567890 < -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
122 |
-1234567890 <= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
123 |
-1234567890 = -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
124 |
-1234567890 ~= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
125 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
126 |
-1234567890 > -1234567891 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
127 |
-1234567890 >= -1234567891 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
128 |
-1234567890 < -1234567891 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
129 |
-1234567890 <= -1234567891 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
130 |
-1234567890 = -1234567891 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
131 |
-1234567890 ~= -1234567891 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
132 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
133 |
-1234567891 > -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
134 |
-1234567891 >= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
135 |
-1234567891 < -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
136 |
-1234567891 <= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
137 |
-1234567891 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
138 |
-1234567891 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
139 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
140 |
1234567890 > -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
141 |
1234567890 >= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
142 |
1234567890 < -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
143 |
1234567890 <= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
144 |
1234567890 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
145 |
1234567890 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
146 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
147 |
-1234567890 > 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
148 |
-1234567890 >= 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
149 |
-1234567890 < 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
150 |
-1234567890 <= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
151 |
-1234567890 = 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
152 |
-1234567890 ~= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
153 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
154 |
1234567890 > 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
155 |
1234567890 >= 1234567890 true |
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 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
161 |
-1234567890 > -10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
162 |
-1234567890 >= -10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
163 |
-1234567890 < -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
164 |
-1234567890 <= -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
165 |
-1234567890 = -10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
166 |
-1234567890 ~= -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
167 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
168 |
-1234567890 > 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
169 |
-1234567890 >= 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
170 |
-1234567890 < 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
171 |
-1234567890 <= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
172 |
-1234567890 = 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
173 |
-1234567890 ~= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
174 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
175 |
-10 > -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
176 |
-10 >= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
177 |
-10 < -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
178 |
-10 <= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
179 |
-10 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
180 |
-10 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
181 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
182 |
10 > -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
183 |
10 >= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
184 |
10 < -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
185 |
10 <= -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
186 |
10 = -1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
187 |
10 ~= -1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
188 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
189 |
1234567890 > -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
190 |
1234567890 >= -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
191 |
1234567890 < -10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
192 |
1234567890 <= -10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
193 |
1234567890 = -10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
194 |
1234567890 ~= -10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
195 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
196 |
1234567890 > 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
197 |
1234567890 >= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
198 |
1234567890 < 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
199 |
1234567890 <= 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
200 |
1234567890 = 10 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
201 |
1234567890 ~= 10 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
202 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
203 |
-10 > 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
204 |
-10 >= 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
205 |
-10 < 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
206 |
-10 <= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
207 |
-10 = 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
208 |
-10 ~= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
209 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
210 |
10 > 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
211 |
10 >= 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
212 |
10 < 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
213 |
10 <= 1234567890 true |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
214 |
10 = 1234567890 false |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
215 |
10 ~= 1234567890 true |
2923 | 216 |
" |
77 | 217 |
! ! |
1 | 218 |
|
1794 | 219 |
!LargeInteger class methodsFor:'instance creation'! |
1 | 220 |
|
221 |
new |
|
222 |
"catch creation message" |
|
223 |
||
224 |
self error:'LargeIntegers cannot be created with new' |
|
225 |
! |
|
226 |
||
701 | 227 |
new:numberOfDigits |
228 |
"catch creation message" |
|
2 | 229 |
|
701 | 230 |
self error:'LargeIntegers cannot be created with new' |
1 | 231 |
! |
232 |
||
701 | 233 |
value:aSmallInteger |
234 |
"create and return a new LargeInteger with value taken from |
|
235 |
the argument, aSmallInteger. |
|
3431 | 236 |
Notice: |
3438
2e64ef848871
oops - last change was not good
Claus Gittinger <cg@exept.de>
parents:
3431
diff
changeset
|
237 |
this should be only used internally, since such small |
3431 | 238 |
largeIntegers do not normally occur in the system. |
3438
2e64ef848871
oops - last change was not good
Claus Gittinger <cg@exept.de>
parents:
3431
diff
changeset
|
239 |
(They are used by myself) |
3431 | 240 |
May change/be removed without notice." |
241 |
||
701 | 242 |
^ self basicNew value:aSmallInteger |
243 |
||
244 |
"LargeInteger value:3689" |
|
3438
2e64ef848871
oops - last change was not good
Claus Gittinger <cg@exept.de>
parents:
3431
diff
changeset
|
245 |
|
2e64ef848871
oops - last change was not good
Claus Gittinger <cg@exept.de>
parents:
3431
diff
changeset
|
246 |
"Modified: / 8.5.1998 / 21:40:41 / cg" |
701 | 247 |
! ! |
248 |
||
1794 | 249 |
!LargeInteger class methodsFor:'queries'! |
701 | 250 |
|
251 |
isBuiltInClass |
|
1264 | 252 |
"return true if this class is known by the run-time-system. |
253 |
Here, true is returned." |
|
701 | 254 |
|
255 |
^ true |
|
1264 | 256 |
|
257 |
"Modified: 23.4.1996 / 15:59:21 / cg" |
|
1 | 258 |
! ! |
259 |
||
68 | 260 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 261 |
|
701 | 262 |
* aNumber |
263 |
"return the product of the receiver and the argument, aNumber" |
|
264 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
265 |
|otherSign numberClass| |
701 | 266 |
|
267 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
268 |
||
269 |
" |
|
270 |
this is the common case, multiplying with SmallInteger. |
|
271 |
Use a special method for this case ... |
|
272 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
273 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
701 | 274 |
^ self productFromInteger:aNumber |
275 |
]. |
|
276 |
||
277 |
" |
|
278 |
if the argument is not a largeInteger, coerce |
|
279 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
280 |
(numberClass == self class) ifFalse:[ |
701 | 281 |
^ self retry:#* coercing:aNumber |
282 |
]. |
|
283 |
||
284 |
otherSign := aNumber sign. |
|
285 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
286 |
(otherSign == 0) ifTrue:[^ 0]. |
|
287 |
^ (self absMul:aNumber) sign:-1 |
|
288 |
! |
|
289 |
||
92 | 290 |
+ aNumber |
291 |
"return the sum of the receiver and the argument, aNumber" |
|
292 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
293 |
|otherSign numberClass| |
92 | 294 |
|
295 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
296 |
||
297 |
" |
|
298 |
this is the common case, adding a SmallInteger. |
|
299 |
Use a special method for this case ... |
|
300 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
301 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
345 | 302 |
^ self sumFromInteger:aNumber |
92 | 303 |
]. |
304 |
||
305 |
" |
|
306 |
if the argument is not a largeInteger, coerce |
|
307 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
308 |
(numberClass == self class) ifFalse:[ |
345 | 309 |
^ self retry:#+ coercing:aNumber |
92 | 310 |
]. |
311 |
||
312 |
otherSign := aNumber sign. |
|
313 |
(sign > 0) ifTrue:[ |
|
345 | 314 |
"I am positive" |
315 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber]. |
|
316 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber]. |
|
317 |
^ self |
|
92 | 318 |
]. |
319 |
"I am negative" |
|
320 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self]. |
|
321 |
(otherSign < 0) ifTrue:[^ (self absPlus:aNumber) sign:-1]. |
|
322 |
^ self |
|
357 | 323 |
|
324 |
" |
|
325 |
SmallInteger maxVal |
|
326 |
SmallInteger maxVal + 1 |
|
327 |
SmallInteger maxVal + 2 |
|
328 |
SmallInteger minVal |
|
329 |
SmallInteger minVal - 1 |
|
330 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
331 |
|
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
332 |
"Modified: / 9.1.1998 / 13:26:28 / cg" |
92 | 333 |
! |
334 |
||
335 |
- aNumber |
|
336 |
"return the difference of the receiver and the argument, aNumber" |
|
337 |
||
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
338 |
|otherSign numberClass| |
92 | 339 |
|
340 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
341 |
||
342 |
" |
|
343 |
this is the common case, subtracting a SmallInteger. |
|
344 |
Use a special method for this case ... |
|
345 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
346 |
((numberClass := aNumber class) == SmallInteger) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
347 |
sign > 0 ifTrue:[ |
2001 | 348 |
aNumber > 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
349 |
^ self absFastMinus:aNumber |
2001 | 350 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
351 |
^ self absFastPlus:aNumber |
2001 | 352 |
]. |
353 |
aNumber > 0 ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
354 |
^ (self absFastPlus:aNumber) sign:-1 |
2001 | 355 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
356 |
^ (self absFastMinus:aNumber) sign:-1 |
92 | 357 |
]. |
358 |
||
359 |
" |
|
360 |
if the argument is not a largeInteger, coerce |
|
361 |
" |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
362 |
(numberClass == self class) ifFalse:[ |
2001 | 363 |
^ self retry:#- coercing:aNumber |
92 | 364 |
]. |
365 |
||
366 |
otherSign := aNumber sign. |
|
367 |
(sign > 0) ifTrue:[ |
|
2001 | 368 |
"I am positive" |
369 |
(otherSign > 0) ifTrue:[^ self absMinus:aNumber]. |
|
370 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber]. |
|
371 |
^ self |
|
92 | 372 |
]. |
373 |
"I am negative" |
|
374 |
(otherSign > 0) ifTrue:[^ (self absPlus:aNumber) negated]. |
|
375 |
(otherSign < 0) ifTrue:[^ (self absMinus:aNumber) negated]. |
|
376 |
^ self |
|
377 |
||
378 |
" |
|
379 |
12345678901234567890 - 0 |
|
357 | 380 |
12345678901234567890 - 1 |
92 | 381 |
12345678901234567890 - -1 |
382 |
-12345678901234567890 - 1 |
|
383 |
-12345678901234567890 - -1 |
|
384 |
||
385 |
12345678901234567890 - 12345678901234567880 |
|
386 |
12345678901234567890 - 12345000000000000000 |
|
387 |
12345678901234567890 - -87654321098765432110 |
|
388 |
-12345678901234567890 - 87654321098765432110 |
|
389 |
-12345678901234567890 - -12345678901234567880 |
|
390 |
-12345678901234567890 - -12345678901234567980 |
|
391 |
" |
|
1794 | 392 |
|
393 |
"Modified: 20.10.1996 / 18:42:16 / cg" |
|
92 | 394 |
! |
395 |
||
701 | 396 |
/ aNumber |
1065 | 397 |
"return the quotient of the receiver and the argument, aNumber" |
92 | 398 |
|
701 | 399 |
aNumber isInteger ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
400 |
^ Fraction numerator:self |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
401 |
denominator:aNumber |
92 | 402 |
]. |
403 |
||
701 | 404 |
"this is a q&d hack - we loose lots of precision here ..." |
405 |
^ (self asFloat / aNumber asFloat) |
|
2793 | 406 |
|
407 |
"Modified: 28.7.1997 / 19:07:55 / cg" |
|
92 | 408 |
! |
409 |
||
68 | 410 |
// aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
411 |
"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
|
412 |
The result is truncated toward negative infinity and negative, |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
413 |
if the operands signs differ." |
1 | 414 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
415 |
|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
|
416 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
417 |
cls := aNumber class. |
1 | 418 |
|
92 | 419 |
" |
420 |
this is the common case, dividing by a SmallInteger. |
|
421 |
Use a special method for this case ... |
|
422 |
" |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
423 |
(cls == SmallInteger) ifTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
424 |
abs := aNumber. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
425 |
abs := abs abs. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
426 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
427 |
divMod := self absFastDivMod:abs. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
428 |
] ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
429 |
n := abs asLargeInteger. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
430 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
431 |
] ifFalse:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
432 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
433 |
if the argument is not a largeInteger, coerce |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
434 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
435 |
(cls == self class) ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
436 |
^ self retry:#// coercing:aNumber |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
437 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
438 |
n := aNumber |
1 | 439 |
]. |
68 | 440 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
441 |
divMod isNil ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
442 |
divMod := self absDivMod:n. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
443 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
444 |
quo := divMod at:1. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
445 |
(sign == aNumber sign) ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
446 |
"/ adjust for truncation if negative and there is a remainder ... |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
447 |
quo := quo sign:-1. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
448 |
(divMod at:2) == 0 ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
449 |
^ quo - 1 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
450 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
451 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
452 |
^ quo |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
453 |
|
92 | 454 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
455 |
(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
|
456 |
(-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
|
457 |
(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
|
458 |
(-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
|
459 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
460 |
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
|
461 |
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
|
462 |
16rfffffffff // 16r001fffff = 32768 ifFalse:[self halt]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
463 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
464 |
900 quo: 400 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
465 |
-900 quo: 400 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
466 |
900 quo: -400 |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
467 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
468 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
469 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
470 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
471 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
472 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
473 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
474 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
475 |
"Modified: / 5.11.1996 / 16:39:36 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
476 |
"Modified: / 27.4.1999 / 19:50:26 / stefan" |
68 | 477 |
! |
478 |
||
479 |
\\ aNumber |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
480 |
"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
|
481 |
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
|
482 |
m < |aNumber| AND there is an integer k with (k * aNumber + m) = self |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
483 |
Compare with #rem:" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
484 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
485 |
|abs rem negativeDivisor| |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
486 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
487 |
aNumber negative ifTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
488 |
negativeDivisor := true. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
489 |
abs := aNumber negated. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
490 |
] ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
491 |
negativeDivisor := false. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
492 |
abs := aNumber. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
493 |
]. |
92 | 494 |
|
495 |
" |
|
496 |
this is the common case, dividing by a SmallInteger. |
|
497 |
Use a special method for this case ... |
|
498 |
" |
|
68 | 499 |
(aNumber class == SmallInteger) ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
500 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
501 |
rem := (self absFastDivMod:abs) at:2. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
502 |
] ifFalse:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
503 |
rem := self absMod:abs asLargeInteger |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
504 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
505 |
] ifFalse:[ |
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 |
if the argument is not a largeInteger, coerce |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
508 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
509 |
(aNumber class == self class) ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
510 |
^ self retry:#\\ coercing:aNumber |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
511 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
512 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
513 |
rem := self absMod:abs. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
514 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
515 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
516 |
rem = 0 ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
517 |
negativeDivisor ifTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
518 |
rem := rem sign:-1 |
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 |
(self negative ~~ negativeDivisor) ifTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
521 |
"different sign, so remainder would have been negative. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
522 |
rem has been rounded toward zero, this code will simulate |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
523 |
rounding to negative infinity." |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
524 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
525 |
rem := aNumber - rem. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
526 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
527 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
528 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
529 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
530 |
" |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
531 |
(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
|
532 |
(-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
|
533 |
(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
|
534 |
(-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
|
535 |
(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
|
536 |
(-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
|
537 |
(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
|
538 |
(-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
|
539 |
|
1890 | 540 |
9000000000 \\ 7 |
541 |
-9000000000 \\ 7 |
|
542 |
9000000000 \\ -7 |
|
543 |
-9000000000 \\ -7 |
|
544 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
545 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
546 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
547 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
548 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
549 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
550 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
551 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
552 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
553 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
554 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
555 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
556 |
"Modified: / 5.11.1996 / 17:10:10 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
557 |
"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
|
558 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
559 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
560 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
561 |
"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
|
562 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
563 |
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
|
564 |
argument." |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
565 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
566 |
|cls n| |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
567 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
568 |
cls := aNumber class. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
569 |
(cls == SmallInteger) ifTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
570 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
571 |
this is the common case, dividing by a SmallInteger. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
572 |
Use a special method for this case ... |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
573 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
574 |
(aNumber between:1 and:16r00ffffff) ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
575 |
^ self absFastDivMod:aNumber abs. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
576 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
577 |
n := aNumber asLargeInteger. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
578 |
] ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
579 |
(cls == self class) ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
580 |
^ super divMod:aNumber |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
581 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
582 |
n := aNumber. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
583 |
]. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
584 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
585 |
^ self absDivMod:n abs |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
586 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
587 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
588 |
9000000000 // 4000000000 => 2 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
589 |
9000000000 \\ 4000000000 => 1000000000 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
590 |
9000000000 divMod: 4000000000 => #(2 1000000000) |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
591 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
592 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
593 |
"Created: / 29.10.1996 / 21:22:05 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
594 |
"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
|
595 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
596 |
|
1210 | 597 |
negated |
598 |
"return an integer with value negated from the receivers value." |
|
599 |
||
2952 | 600 |
|newNumber sz| |
1210 | 601 |
|
602 |
(sign == 0) ifTrue:[^ 0]. |
|
603 |
||
604 |
" |
|
605 |
special case for SmallInteger minVal |
|
606 |
" |
|
607 |
sign == 1 ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
608 |
sz := digitByteArray size. |
2952 | 609 |
%{ |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
610 |
int idx; |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
611 |
unsigned char *bp; |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
612 |
|
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
613 |
bp = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element); |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
614 |
idx = __intVal(sz); |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
615 |
|
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
616 |
while ((idx > 1) && (bp[idx-1] == 0)) idx--; |
3431 | 617 |
|
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
618 |
if (idx == sizeof(INT)) { |
3431 | 619 |
#if defined(alpha64) |
620 |
if ( ((unsigned INT *)bp)[0] == 0x4000000000000000L) |
|
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
621 |
#else |
3431 | 622 |
# if defined(i386) /* actually: LSBFIRST */ |
623 |
if ( ((unsigned INT *)bp)[0] == 0x40000000) |
|
624 |
# else |
|
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
625 |
/* |
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
626 |
* generic code |
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
627 |
*/ |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
628 |
if ((bp[idx-1] == 0x40) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
629 |
&& (bp[idx-2] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
630 |
&& (bp[idx-3] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
631 |
&& (bp[idx-4] == 0) |
3431 | 632 |
# ifdef alpha64 |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
633 |
&& (bp[idx-5] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
634 |
&& (bp[idx-6] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
635 |
&& (bp[idx-7] == 0) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
636 |
&& (bp[idx-8] == 0) |
3431 | 637 |
# endif |
638 |
) |
|
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
639 |
# endif |
2952 | 640 |
#endif |
3428
fb8dbb5dce40
fixed negated for the minVal case;
Claus Gittinger <cg@exept.de>
parents:
3424
diff
changeset
|
641 |
{ |
2952 | 642 |
RETURN (__MKSMALLINT(_MIN_INT)); |
643 |
} |
|
644 |
} |
|
645 |
%}. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
646 |
"/ sz == 4 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
647 |
"/ (digitByteArray at:1) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
648 |
"/ (digitByteArray at:2) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
649 |
"/ (digitByteArray at:3) == 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
650 |
"/ (digitByteArray at:4) == 16r40 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
651 |
"/ ^ SmallInteger minVal |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
652 |
"/ ]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
653 |
"/ ] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
654 |
"/ ] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
655 |
"/ ] |
2952 | 656 |
"/ ] |
1210 | 657 |
]. |
658 |
newNumber := self shallowCopy. |
|
659 |
newNumber sign:(sign negated). |
|
660 |
^ newNumber |
|
661 |
! |
|
662 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
663 |
quo:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
664 |
"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
|
665 |
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
|
666 |
truncates toward negative infinity). |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
667 |
The results sign is negative if the receiver has a sign |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
668 |
different from the args sign" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
669 |
|
2521 | 670 |
|otherSign quo abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
671 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
672 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
673 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
674 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
675 |
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
|
676 |
Use a special method for this case ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
677 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
678 |
(aNumber class == SmallInteger) ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
679 |
abs := aNumber. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
680 |
abs := abs abs. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
681 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
682 |
quo := (self absFastDivMod:abs) at:1. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
683 |
(sign == otherSign) ifTrue:[^ quo]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
684 |
^ quo sign:-1 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
685 |
] |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
686 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
687 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
688 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
689 |
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
|
690 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
691 |
(aNumber class == self class) ifFalse:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
692 |
^ self retry:#quo: coercing:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
693 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
694 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
695 |
sign < 0 ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
696 |
(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
|
697 |
] ifFalse:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
698 |
(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
|
699 |
]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
700 |
^ ((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
|
701 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
702 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
703 |
900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
704 |
-900 // 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
705 |
900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
706 |
-900 // -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
707 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
708 |
9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
709 |
-9000000000 // 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
710 |
9000000000 // -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
711 |
-9000000000 // -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
712 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
713 |
900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
714 |
-900 quo: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
715 |
900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
716 |
-900 quo: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
717 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
718 |
9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
719 |
-9000000000 quo: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
720 |
9000000000 quo: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
721 |
-9000000000 quo: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
722 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
723 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
724 |
"Modified: / 5.11.1996 / 14:14:17 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
725 |
"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
|
726 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
727 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
728 |
rem:aNumber |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
729 |
"return the remainder of division of the receiver by the argument, aNumber. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
730 |
The returned remainder has the same sign as the receiver." |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
731 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
732 |
|rem abs "{ Class: SmallInteger }" | |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
733 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
734 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
735 |
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
|
736 |
Use special code for this case ... |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
737 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
738 |
(aNumber class == SmallInteger) ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
739 |
abs := aNumber. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
740 |
abs := abs abs. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
741 |
(abs between:1 and:16r00ffffff) ifTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
742 |
rem := (self absFastDivMod:abs) at:2. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
743 |
] ifFalse:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
744 |
rem := self absMod:abs asLargeInteger |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
745 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
746 |
] ifFalse:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
747 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
748 |
if the argument is not a largeInteger, coerce |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
749 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
750 |
(aNumber class == self class) ifFalse:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
751 |
^ self retry:#rem coercing:aNumber |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
752 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
753 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
754 |
rem := self absMod:aNumber |
1 | 755 |
]. |
92 | 756 |
|
757 |
sign < 0 ifTrue:[ |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
758 |
^ rem sign:-1 |
1 | 759 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
760 |
^ rem |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
761 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
762 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
763 |
900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
764 |
-900 \\ 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
765 |
900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
766 |
-900 \\ -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
767 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
768 |
9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
769 |
-9000000000 \\ 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
770 |
9000000000 \\ -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
771 |
-9000000000 \\ -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
772 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
773 |
900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
774 |
-900 rem: 400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
775 |
900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
776 |
-900 rem: -400 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
777 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
778 |
9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
779 |
-9000000000 rem: 4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
780 |
9000000000 rem: -4000000000 |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
781 |
-9000000000 rem: -4000000000 |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
782 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
783 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
784 |
"Modified: / 5.11.1996 / 14:02:59 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
785 |
"Modified: / 29.4.1999 / 11:26:51 / stefan" |
1 | 786 |
! ! |
787 |
||
2855 | 788 |
!LargeInteger methodsFor:'bit operators'! |
789 |
||
790 |
lowBit |
|
791 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
792 |
starts at 1 for the least significant bit. Returns -1 if no bit is set. |
|
793 |
For negative numbers, the low bit of my absolute value is returned. |
|
794 |
Redefined here for more performance of the gcd: algorithm, which |
|
795 |
is used when big fractions are reduced." |
|
796 |
||
797 |
|sz "{ Class: SmallInteger }" |
|
798 |
byte| |
|
799 |
||
800 |
sz := digitByteArray size. |
|
801 |
1 to:sz do:[:digitIndex | |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
802 |
(byte := digitByteArray at:digitIndex) ~~ 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
803 |
^ (digitIndex-1)*8 + byte lowBit |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
804 |
] |
2855 | 805 |
]. |
806 |
^ -1 |
|
807 |
||
808 |
" |
|
809 |
(1 bitShift:30) lowBit |
|
810 |
(1 bitShift:30) highBit |
|
811 |
(1 bitShift:31) lowBit |
|
812 |
(1 bitShift:31) highBit |
|
813 |
(1 bitShift:32) lowBit |
|
814 |
(1 bitShift:32) highBit |
|
815 |
(1 bitShift:33) lowBit |
|
816 |
(1 bitShift:33) highBit |
|
817 |
(1 bitShift:64) lowBit |
|
818 |
(1 bitShift:64) highBit |
|
819 |
(1 bitShift:1000) lowBit |
|
820 |
(1 bitShift:1000) highBit |
|
821 |
((1 bitShift:64)-1) lowBit |
|
822 |
((1 bitShift:64)-1) highBit |
|
823 |
" |
|
824 |
||
825 |
"Modified: 14.8.1997 / 11:55:34 / cg" |
|
826 |
! ! |
|
827 |
||
701 | 828 |
!LargeInteger methodsFor:'byte access'! |
829 |
||
830 |
digitAt:index |
|
831 |
"return 8 bits of value, starting at byte index" |
|
832 |
||
833 |
index > digitByteArray size ifTrue:[^ 0]. |
|
834 |
^ digitByteArray at:index |
|
835 |
! |
|
836 |
||
837 |
digitAt:index put:aByte |
|
838 |
"set the 8 bits, index is a byte index" |
|
839 |
||
840 |
digitByteArray at:index put:aByte |
|
841 |
! |
|
842 |
||
3891 | 843 |
digitByteAt:index |
844 |
"return 8 bits of my signed value, starting at byte index. |
|
845 |
For positive receivers, this is the same as #digitAt:; |
|
846 |
for negative ones, the actual bit representation is returned." |
|
847 |
||
848 |
|t digits| |
|
849 |
sign < 0 ifFalse:[ |
|
850 |
index > digitByteArray size ifTrue:[ |
|
851 |
^ 0 |
|
852 |
]. |
|
853 |
^ digitByteArray at:index. |
|
854 |
]. |
|
855 |
"/ negative int - do 2's complement here |
|
856 |
||
857 |
t := self bitInvert + 1. |
|
858 |
t sign:1. |
|
859 |
digits := t digits. |
|
860 |
index > digits size ifTrue:[ |
|
861 |
^ 16rFF |
|
862 |
]. |
|
863 |
^ digits at:index. |
|
864 |
||
865 |
" |
|
866 |
16r11111111111111111111 negated digitByteAt:1 |
|
867 |
" |
|
868 |
||
869 |
"Created: / 25.10.1998 / 14:12:21 / cg" |
|
870 |
! |
|
871 |
||
701 | 872 |
digitLength |
2816 | 873 |
"return the number bytes used by this Integer. |
874 |
For negative receivers, the digitLength of its absolute value |
|
875 |
is returned." |
|
701 | 876 |
|
877 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
878 |
check if there is a 0-byte ... |
701 | 879 |
this allows to ask unnormalized LargeIntegers |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
880 |
for their digitLength |
701 | 881 |
" |
882 |
|l "{ Class: SmallInteger }" | |
|
92 | 883 |
|
701 | 884 |
l := digitByteArray size. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
885 |
[l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
886 |
l := l - 1. |
701 | 887 |
]. |
888 |
^ l |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
889 |
|
2816 | 890 |
"Modified: 31.7.1997 / 13:18:28 / cg" |
701 | 891 |
! |
892 |
||
893 |
digits |
|
4151 | 894 |
"return a byteArray filled with the receivers bits |
701 | 895 |
(8 bits of the absolute value per element)" |
896 |
||
897 |
^ digitByteArray |
|
4151 | 898 |
|
899 |
"Modified: / 5.5.1999 / 14:57:03 / stefan" |
|
701 | 900 |
! ! |
901 |
||
902 |
!LargeInteger methodsFor:'coercing & converting'! |
|
68 | 903 |
|
701 | 904 |
asFloat |
905 |
"return a Float with same value as myself. |
|
906 |
Since floats have a limited precision, you usually loose bits when |
|
907 |
doing this." |
|
908 |
||
909 |
|newFloat| |
|
910 |
||
911 |
newFloat := 0.0. |
|
912 |
(sign == 0) ifFalse:[ |
|
913 |
digitByteArray reverseDo:[:aDigit | |
|
914 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
915 |
]. |
|
916 |
(sign < 0) ifTrue:[ |
|
917 |
newFloat := newFloat negated |
|
345 | 918 |
] |
701 | 919 |
]. |
920 |
^ newFloat |
|
921 |
||
922 |
" |
|
923 |
1234567890 asFloat |
|
924 |
12345678901234567890 asFloat |
|
925 |
12345678901234567890 asFloat asInteger |
|
926 |
" |
|
927 |
! |
|
928 |
||
929 |
asLargeInteger |
|
930 |
"return a LargeInteger with same value as myself - thats me" |
|
931 |
||
932 |
^ self |
|
933 |
! |
|
934 |
||
935 |
asSmallInteger |
|
936 |
"return a SmallInteger with same value as myself - |
|
937 |
the result is invalid if the receivers value cannot |
|
938 |
be represented as a SmallInteger. |
|
939 |
Q: should we raise an exception if this happens ?" |
|
940 |
||
941 |
|value| |
|
942 |
||
943 |
value := 0. |
|
944 |
(sign == 0) ifFalse:[ |
|
945 |
digitByteArray reverseDo:[:aDigit | |
|
946 |
value := (value times:256) + aDigit |
|
345 | 947 |
]. |
701 | 948 |
(sign < 0) ifTrue:[ |
949 |
value := value negated |
|
345 | 950 |
] |
68 | 951 |
]. |
701 | 952 |
^ value |
953 |
! |
|
954 |
||
955 |
coerce:aNumber |
|
956 |
"return the argument as a LargeInteger" |
|
957 |
||
958 |
^ aNumber asLargeInteger |
|
959 |
! |
|
960 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
961 |
compressed |
701 | 962 |
"if the receiver can be represented as a SmallInteger, return |
2267 | 963 |
a SmallInteger with my value; |
964 |
otherwise remove leading zero bytes in the digitByte array |
|
965 |
and return the receiver" |
|
701 | 966 |
|
967 |
|index "{ Class: SmallInteger }" |
|
2267 | 968 |
val "{ Class: SmallInteger }" |
969 |
index0 |
|
970 |
| |
|
701 | 971 |
|
972 |
%{ /* NOCONTEXT */ |
|
973 |
OBJ t; |
|
974 |
||
1133 | 975 |
if (__INST(sign) == __MKSMALLINT(0)) { |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
976 |
RETURN (__MKSMALLINT(0)); |
701 | 977 |
} |
978 |
||
1133 | 979 |
t = __INST(digitByteArray); |
701 | 980 |
if (__isByteArray(t)) { |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
981 |
unsigned char *_digitBytes = __ByteArrayInstPtr(t)->ba_element; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
982 |
int _idx, _idx0; |
2786 | 983 |
INT _val; |
701 | 984 |
|
3221
21085ae8e1be
eliminated some single-underscore macro refs (stringSize, arraySize ...)
Claus Gittinger <cg@exept.de>
parents:
3178
diff
changeset
|
985 |
_idx = _idx0 = __byteArraySize(t); |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
986 |
while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
987 |
_idx--; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
988 |
} |
2788 | 989 |
#ifdef alpha64 |
2786 | 990 |
switch (_idx) { |
991 |
case 7: |
|
992 |
_val = (_digitBytes[6]<<8); |
|
993 |
_val = (_val + _digitBytes[5]) << 8; |
|
994 |
_val = (_val + _digitBytes[4]) << 8; |
|
995 |
_val = (_val + _digitBytes[3]) << 8; |
|
996 |
_val = (_val + _digitBytes[2]) << 8; |
|
997 |
_val = (_val + _digitBytes[1]) << 8; |
|
998 |
_val += _digitBytes[0]; |
|
999 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1000 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1001 |
RETURN (__MKSMALLINT(_val)); |
2786 | 1002 |
case 6: |
1003 |
_val = (_digitBytes[5]<<8); |
|
1004 |
_val = (_val + _digitBytes[4]) << 8; |
|
1005 |
_val = (_val + _digitBytes[3]) << 8; |
|
1006 |
_val = (_val + _digitBytes[2]) << 8; |
|
1007 |
_val = (_val + _digitBytes[1]) << 8; |
|
1008 |
_val += _digitBytes[0]; |
|
1009 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1010 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1011 |
RETURN (__MKSMALLINT(_val)); |
2786 | 1012 |
case 5: |
1013 |
_val = (_digitBytes[4]<<8); |
|
1014 |
_val = (_val + _digitBytes[3]) << 8; |
|
1015 |
_val = (_val + _digitBytes[2]) << 8; |
|
1016 |
_val = (_val + _digitBytes[1]) << 8; |
|
1017 |
_val += _digitBytes[0]; |
|
1018 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1019 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1020 |
RETURN (__MKSMALLINT(_val)); |
2786 | 1021 |
case 4: |
1022 |
_val = (_digitBytes[3]<<8); |
|
1023 |
_val = (_val + _digitBytes[2]) << 8; |
|
1024 |
_val = (_val + _digitBytes[1]) << 8; |
|
1025 |
_val += _digitBytes[0]; |
|
1026 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1027 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1028 |
RETURN (__MKSMALLINT(_val)); |
2786 | 1029 |
case 3: |
1030 |
_val = (_digitBytes[2]<<8); |
|
1031 |
_val = (_val + _digitBytes[1]) << 8; |
|
1032 |
_val += _digitBytes[0]; |
|
1033 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1034 |
_val = -_val; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1035 |
RETURN (__MKSMALLINT(_val)); |
2786 | 1036 |
case 2: |
1037 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
1038 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1039 |
_val = -_val; |
|
1040 |
RETURN (__MKSMALLINT(_val)); |
|
1041 |
case 1: |
|
1042 |
_val = _digitBytes[0]; |
|
1043 |
if (__INST(sign) == __MKSMALLINT(-1)) |
|
1044 |
_val = -_val; |
|
1045 |
RETURN (__MKSMALLINT(_val)); |
|
1046 |
case 0: |
|
1047 |
RETURN (__MKSMALLINT(0)); |
|
1048 |
||
1049 |
} |
|
1050 |
#else |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1051 |
if (_idx <= 4) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1052 |
if (_idx <= 2) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1053 |
if (_idx == 0) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1054 |
RETURN (__MKSMALLINT(0)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1055 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1056 |
if (_idx == 1) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1057 |
_val = _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1058 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1059 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1060 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1061 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1062 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1063 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1064 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1065 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1066 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1067 |
if (_idx == 3) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1068 |
_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1069 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1070 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1071 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1072 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1073 |
_val = _digitBytes[3]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1074 |
if (_val <= 0x40) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1075 |
_val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1076 |
if (__INST(sign) == __MKSMALLINT(-1)) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1077 |
_val = -_val; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1078 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1079 |
RETURN (__MKSMALLINT(_val)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1080 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1081 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1082 |
} |
2786 | 1083 |
#endif |
701 | 1084 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1085 |
if (_idx == _idx0) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1086 |
RETURN (self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1087 |
} |
2267 | 1088 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1089 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1090 |
* must copy & cut off some bytes |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1091 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1092 |
{ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1093 |
OBJ newDigits; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1094 |
OBJ oldDigits; |
701 | 1095 |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1096 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1097 |
* careful - there is no context here to protect |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1098 |
* the receiver ... |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1099 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1100 |
__PROTECT__(self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1101 |
__PROTECT__(__INST(digitByteArray)); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1102 |
newDigits = __BYTEARRAY_UNINITIALIZED_NEW_INT(_idx); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1103 |
__UNPROTECT__(oldDigits); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1104 |
__UNPROTECT__(self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1105 |
if (newDigits) { |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1106 |
bcopy(__ByteArrayInstPtr(oldDigits)->ba_element, |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1107 |
__ByteArrayInstPtr(newDigits)->ba_element, |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1108 |
_idx); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1109 |
__INST(digitByteArray) = newDigits; |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1110 |
__STORE(self, newDigits); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1111 |
RETURN (self); |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1112 |
} |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1113 |
/* |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1114 |
* allocation failed ... |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1115 |
* ... fall through to trigger the error |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1116 |
*/ |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1117 |
} |
701 | 1118 |
} |
1119 |
%}. |
|
2267 | 1120 |
index0 := index := digitByteArray size. |
701 | 1121 |
[(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1122 |
index := index - 1 |
701 | 1123 |
]. |
1124 |
||
2267 | 1125 |
(index ~~ index0) ifTrue:[ |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1126 |
digitByteArray := digitByteArray copyFrom:1 to:index |
701 | 1127 |
]. |
1128 |
^ self |
|
1129 |
! |
|
1130 |
||
1131 |
value:aSmallInteger |
|
3431 | 1132 |
"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
|
1133 |
This method will fail, if the argument is not a smallInteger. |
3431 | 1134 |
This should only be used internally, |
1135 |
since it will create an unnormalized LargeInt (by purpose) if asked for." |
|
701 | 1136 |
|
1137 |
|absValue |
|
1138 |
b1 "{ Class: SmallInteger }" |
|
1139 |
b2 "{ Class: SmallInteger }" |
|
2786 | 1140 |
b3 "{ Class: SmallInteger }" |
1141 |
b4 "{ Class: SmallInteger }" |
|
1142 |
b5 "{ Class: SmallInteger }" |
|
1143 |
b6 "{ Class: SmallInteger }" |
|
1144 |
b7 "{ Class: SmallInteger }"| |
|
92 | 1145 |
|
1146 |
" |
|
3431 | 1147 |
could have simply created a 4-byte largeinteger and normalize it. |
1148 |
The code below does the normalize right away, avoiding the |
|
701 | 1149 |
overhead of producing any intermediate byte-arrays (and the scanning) |
92 | 1150 |
" |
701 | 1151 |
(aSmallInteger == 0) ifTrue: [ |
2001 | 1152 |
digitByteArray := ByteArray with:0. |
1153 |
sign := 0. |
|
1154 |
^ self |
|
701 | 1155 |
]. |
3431 | 1156 |
|
701 | 1157 |
(aSmallInteger < 0) ifTrue: [ |
2001 | 1158 |
sign := -1. |
1159 |
absValue := aSmallInteger negated |
|
701 | 1160 |
] ifFalse: [ |
2001 | 1161 |
sign := 1. |
1162 |
absValue := aSmallInteger |
|
701 | 1163 |
]. |
3431 | 1164 |
|
701 | 1165 |
b1 := absValue bitAnd:16rFF. |
1166 |
absValue := absValue bitShift:-8. |
|
1167 |
absValue == 0 ifTrue:[ |
|
2001 | 1168 |
digitByteArray := ByteArray with:b1 |
701 | 1169 |
] ifFalse:[ |
2001 | 1170 |
b2 := absValue bitAnd:16rFF. |
1171 |
absValue := absValue bitShift:-8. |
|
1172 |
absValue == 0 ifTrue:[ |
|
1173 |
digitByteArray := ByteArray with:b1 with:b2 |
|
1174 |
] ifFalse:[ |
|
1175 |
b3 := absValue bitAnd:16rFF. |
|
1176 |
absValue := absValue bitShift:-8. |
|
1177 |
absValue == 0 ifTrue:[ |
|
1178 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
1179 |
] ifFalse:[ |
|
2786 | 1180 |
b4 := absValue bitAnd:16rFF. |
1181 |
absValue := absValue bitShift:-8. |
|
1182 |
absValue == 0 ifTrue:[ |
|
1183 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:b4 |
|
1184 |
] ifFalse:[ |
|
1185 |
b5 := absValue bitAnd:16rFF. |
|
1186 |
absValue := absValue bitShift:-8. |
|
1187 |
absValue == 0 ifTrue:[ |
|
1188 |
digitByteArray := ByteArray new:5. |
|
1189 |
digitByteArray at:1 put:b1. |
|
1190 |
digitByteArray at:2 put:b2. |
|
1191 |
digitByteArray at:3 put:b3. |
|
1192 |
digitByteArray at:4 put:b4. |
|
1193 |
digitByteArray at:5 put:b5. |
|
1194 |
] ifFalse:[ |
|
1195 |
b6 := absValue bitAnd:16rFF. |
|
1196 |
absValue := absValue bitShift:-8. |
|
1197 |
absValue == 0 ifTrue:[ |
|
1198 |
digitByteArray := ByteArray new:6. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1199 |
digitByteArray at:1 put:b1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1200 |
digitByteArray at:2 put:b2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1201 |
digitByteArray at:3 put:b3. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1202 |
digitByteArray at:4 put:b4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1203 |
digitByteArray at:5 put:b5. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1204 |
digitByteArray at:6 put:b6. |
2786 | 1205 |
] ifFalse:[ |
1206 |
b7 := absValue bitAnd:16rFF. |
|
1207 |
absValue := absValue bitShift:-8. |
|
1208 |
absValue == 0 ifTrue:[ |
|
1209 |
digitByteArray := ByteArray new:7. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1210 |
digitByteArray at:1 put:b1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1211 |
digitByteArray at:2 put:b2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1212 |
digitByteArray at:3 put:b3. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1213 |
digitByteArray at:4 put:b4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1214 |
digitByteArray at:5 put:b5. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1215 |
digitByteArray at:6 put:b6. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1216 |
digitByteArray at:7 put:b7. |
2786 | 1217 |
] ifFalse:[ |
1218 |
digitByteArray := ByteArray new:8. |
|
1219 |
digitByteArray at:1 put:b1. |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1220 |
digitByteArray at:2 put:b2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1221 |
digitByteArray at:3 put:b3. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1222 |
digitByteArray at:4 put:b4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1223 |
digitByteArray at:5 put:b5. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1224 |
digitByteArray at:6 put:b6. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1225 |
digitByteArray at:7 put:b7. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1226 |
digitByteArray at:7 put:absValue. |
2786 | 1227 |
] |
1228 |
] |
|
1229 |
] |
|
1230 |
] |
|
2001 | 1231 |
] |
1232 |
] |
|
701 | 1233 |
] |
1890 | 1234 |
|
1235 |
"Modified: 5.11.1996 / 16:15:39 / cg" |
|
701 | 1236 |
! ! |
1237 |
||
1238 |
!LargeInteger methodsFor:'comparing'! |
|
1239 |
||
1240 |
< aNumber |
|
1241 |
"return true, if the argument, aNumber is greater than the receiver" |
|
1242 |
||
1243 |
|otherSign| |
|
1244 |
||
1245 |
(aNumber class == self class) ifFalse:[ |
|
1246 |
^ self retry:#< coercing:aNumber |
|
1247 |
]. |
|
1248 |
otherSign := aNumber sign. |
|
1249 |
||
1250 |
(sign > 0) ifTrue:[ |
|
1251 |
"I am positive" |
|
1252 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
1253 |
^ false "aNumber is <= 0" |
|
1254 |
]. |
|
1255 |
(sign == 0) ifTrue:[ |
|
1256 |
(otherSign > 0) ifTrue:[^ true]. |
|
1257 |
^ false |
|
1258 |
]. |
|
1259 |
"I am negative" |
|
1260 |
(otherSign > 0) ifTrue:[^ true]. |
|
1261 |
(otherSign == 0) ifTrue:[^ true]. |
|
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1262 |
^ (aNumber absLess:self) |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1263 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1264 |
"Modified: / 14.1.1998 / 12:51:42 / cg" |
92 | 1265 |
! |
1266 |
||
701 | 1267 |
= aNumber |
1268 |
"return true, if the argument, aNumber has the same value as |
|
1269 |
the receiver" |
|
1270 |
||
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1271 |
"/ speed up compare to 0 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1272 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1273 |
(aNumber == 0 and:[sign == 0]) ifTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1274 |
^ true |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1275 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1276 |
|
701 | 1277 |
(aNumber class == self class) ifFalse:[ |
3424
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1278 |
"/ |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1279 |
"/ here, we depend on the fact, that largeinteger |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1280 |
"/ results are always converted to smallInts, if possible. |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1281 |
"/ therefore, a largeInt in the smallInt range is not allowed (possible) |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1282 |
"/ |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1283 |
aNumber class == SmallInteger ifTrue:[^ false ]. |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1284 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
39de1fface52
oops - negation of (maxInt+1) to minInt failed
Claus Gittinger <cg@exept.de>
parents:
3300
diff
changeset
|
1285 |
^ self retry:#= coercing:aNumber |
701 | 1286 |
]. |
1287 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
1288 |
^ self absEq:aNumber |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1289 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1290 |
"Modified: / 13.2.1998 / 11:43:15 / stefan" |
701 | 1291 |
! |
1292 |
||
1293 |
> aNumber |
|
1294 |
"return true, if the argument, aNumber is less than the receiver" |
|
1295 |
||
1296 |
|otherSign| |
|
1297 |
||
1298 |
(aNumber class == self class) ifFalse:[ |
|
1299 |
^ self retry:#> coercing:aNumber |
|
1300 |
]. |
|
1301 |
otherSign := aNumber sign. |
|
1302 |
||
1303 |
(sign > 0) ifTrue:[ |
|
1304 |
"I am positive" |
|
1305 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
1306 |
^ true "aNumber is <= 0" |
|
1307 |
]. |
|
1308 |
(sign == 0) ifTrue:[ |
|
1309 |
"I am zero" |
|
1310 |
(otherSign > 0) ifTrue:[^ false]. |
|
1311 |
^ true |
|
1312 |
]. |
|
1313 |
"I am negative" |
|
1314 |
(otherSign > 0) ifTrue:[^ false]. |
|
1315 |
(otherSign == 0) ifTrue:[^ false]. |
|
3178
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1316 |
^ (self absLess:aNumber) |
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1317 |
|
649320240430
fixed #> (-1234567890 > -10)
Claus Gittinger <cg@exept.de>
parents:
3161
diff
changeset
|
1318 |
"Modified: / 14.1.1998 / 12:56:56 / cg" |
701 | 1319 |
! ! |
1320 |
||
1321 |
!LargeInteger methodsFor:'double dispatching'! |
|
1322 |
||
92 | 1323 |
differenceFromInteger:anInteger |
1324 |
"sent, when anInteger does not know how to subtract the receiver. |
|
1325 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
1326 |
||
1327 |
anInteger > 0 ifTrue:[ |
|
2001 | 1328 |
sign > 0 ifTrue:[ |
1329 |
^ (self absFastMinus:anInteger) sign:-1 |
|
1330 |
]. |
|
1331 |
^ self absFastPlus:anInteger |
|
92 | 1332 |
]. |
1794 | 1333 |
anInteger == 0 ifTrue:[ |
2001 | 1334 |
^ self negated |
1794 | 1335 |
]. |
1336 |
sign > 0 ifTrue:[ |
|
2001 | 1337 |
^ (self absFastPlus:anInteger negated) sign:-1 |
1794 | 1338 |
]. |
1339 |
^ (self absFastMinus:anInteger) sign:-1 |
|
92 | 1340 |
|
1341 |
" |
|
1342 |
12345678901234567890 |
|
1343 |
-12345678901234567890 |
|
1344 |
12345678901234567890 differenceFromInteger:0 |
|
1345 |
12345678901234567890 differenceFromInteger:1 |
|
1346 |
12345678901234567890 differenceFromInteger:-1 |
|
1347 |
-12345678901234567890 differenceFromInteger:1 |
|
1348 |
-12345678901234567890 differenceFromInteger:-1 |
|
1349 |
" |
|
1794 | 1350 |
|
1351 |
"Modified: 20.10.1996 / 18:41:54 / cg" |
|
68 | 1352 |
! |
1353 |
||
1354 |
productFromInteger:anInteger |
|
1355 |
"sent, when anInteger does not know how to multiply the receiver. |
|
1356 |
Return the product of the receiver and the argument, aSmallInteger" |
|
1357 |
||
1358 |
|num result |
|
1359 |
resultDigitByteArray |
|
1360 |
val "{ Class: SmallInteger }" |
|
1361 |
len "{ Class: SmallInteger }" |
|
1362 |
carry "{ Class: SmallInteger }" |
|
85 | 1363 |
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
|
1364 |
ok lResult| |
68 | 1365 |
|
1366 |
"multiplying by a small integer is done here" |
|
1367 |
||
1368 |
"trivial cases" |
|
1369 |
anInteger == 0 ifTrue:[^ 0]. |
|
1370 |
anInteger == 1 ifTrue:[^ self]. |
|
1371 |
||
1372 |
num := anInteger abs. |
|
1373 |
(num > 16r3FFFFF) ifTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1374 |
"if num is too big (so that multiplying by a byte could create a Large)" |
68 | 1375 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1376 |
^ anInteger retry:#* coercing:self |
68 | 1377 |
]. |
1378 |
||
1379 |
len := digitByteArray size. |
|
1380 |
||
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1381 |
val := num. |
2923 | 1382 |
val <= 16rFF ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1383 |
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
|
1384 |
] ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1385 |
val <= 16rFFFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1386 |
lResult := len + 2 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1387 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1388 |
val <= 16rFFFFFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1389 |
lResult := len + 4. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1390 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1391 |
lResult := len + 6. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1392 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1393 |
] |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1394 |
]. |
2933 | 1395 |
resultDigitByteArray := ByteArray uninitializedNew:lResult. |
2897 | 1396 |
result := self class basicNew setDigits:resultDigitByteArray. |
1 | 1397 |
|
68 | 1398 |
anInteger < 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1399 |
sign > 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1400 |
result sign:-1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1401 |
]. |
68 | 1402 |
] ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1403 |
sign < 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1404 |
result sign:sign |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1405 |
] |
68 | 1406 |
]. |
1407 |
||
85 | 1408 |
ok := false. |
1409 |
%{ |
|
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1410 |
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
|
1411 |
|
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1412 |
if (__isSmallInteger(len) |
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1413 |
&& __isByteArray(__digitByteArray) |
85 | 1414 |
&& __isByteArray(resultDigitByteArray)) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1415 |
INT _l = __intVal(len); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1416 |
INT _v = __intVal(val); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1417 |
unsigned INT _carry = 0; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1418 |
unsigned INT _prod; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1419 |
unsigned char *digitP = __ByteArrayInstPtr(__digitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1420 |
unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1421 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1422 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1423 |
* skipping zeros does not help much (a few percent) on |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1424 |
* a P5 or other CPUS with a fast multiplier. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1425 |
* It may make more of a difference on CPUs with slower 0-multiply. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1426 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1427 |
while ((_l >= sizeof(INT)) && (((unsigned INT *)digitP)[0] == 0)) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1428 |
((unsigned long *)resultP)[0] = 0; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1429 |
digitP += sizeof(INT); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1430 |
resultP += sizeof(INT); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1431 |
_l -= sizeof(INT); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1432 |
} |
2923 | 1433 |
|
1434 |
#if defined(i386) || defined(alpha) /* XXX actually: LSB_FIRST */ |
|
1435 |
# if defined (__GNUC__) && defined(i386) |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1436 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1437 |
* can do it long-word-wise; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1438 |
* 32*32 -> 64 multiplication |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1439 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1440 |
while (_l > 3) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1441 |
unsigned __pHi, __pLow; |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1442 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1443 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1444 |
* max: 0xFFFF.FFFF * 0xFFFF.FFFF -> 0xFFFF.FFFE.0000.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1445 |
* + maxCarry (0xFFFF.FFFF) -> 0xFFFF.FFFF.0000.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1446 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1447 |
asm ("mull %3 \n |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1448 |
addl %4,%%eax \n |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1449 |
adcl $0,%%edx" |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1450 |
: "=a" ((unsigned long)(__pLow)), |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1451 |
"=d" ((unsigned long)(__pHi)) |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1452 |
: "0" ((unsigned long)(((unsigned long *)digitP)[0])), |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1453 |
"1" ((unsigned long)(_v)), |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1454 |
"rm" ((unsigned long)(_carry)) ); |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1455 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1456 |
((unsigned long *)resultP)[0] = __pLow; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1457 |
_carry = __pHi; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1458 |
digitP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1459 |
resultP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1460 |
_l -= 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1461 |
} |
2923 | 1462 |
# else |
85 | 1463 |
|
2923 | 1464 |
# if defined(INT64) |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1465 |
if (_v <= 0xFFFFFFFFL) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1466 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1467 |
* have a 64bit int type ... good |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1468 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1469 |
UINT64 _prod64; |
2923 | 1470 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1471 |
/* have 64bit ints; can do it int-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1472 |
* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1473 |
* max: 0xFFFFFFFF * 0xFFFFFFFF -> 0xFFFFFFFE.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1474 |
* + maxCarry (0xFFFFFFFF) -> 0xFFFFFFFF.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1475 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1476 |
while (_l > 3) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1477 |
unsigned __t; |
2964 | 1478 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1479 |
__t = ((unsigned *)digitP)[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1480 |
digitP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1481 |
_prod64 = (INT64)_v; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1482 |
_prod64 *= __t; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1483 |
_prod64 += _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1484 |
((unsigned *)resultP)[0] = _prod64 /* & 0xFFFFFFFFL */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1485 |
_carry = _prod64 >> 32; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1486 |
resultP += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1487 |
_l -= 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1488 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1489 |
if (_l > 1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1490 |
unsigned short __t; |
2964 | 1491 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1492 |
__t = ((unsigned short *)digitP)[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1493 |
digitP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1494 |
_prod64 = (INT64)_v; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1495 |
_prod64 *= __t; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1496 |
_prod64 += _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1497 |
((unsigned short *)resultP)[0] = _prod64 /* & 0xFFFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1498 |
_carry = _prod64 >> 16; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1499 |
resultP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1500 |
_l -= 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1501 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1502 |
if (_l > 0) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1503 |
_prod64 = *digitP++ * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1504 |
*resultP++ = _prod64 /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1505 |
_carry = _prod64 >> 8; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1506 |
_l--; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1507 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1508 |
} |
2923 | 1509 |
# else |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1510 |
if (_v <= 0xFFFF) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1511 |
/* can do it short-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1512 |
* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1513 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1514 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1515 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1516 |
while (_l > 1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1517 |
_prod = ((unsigned short *)digitP)[0] * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1518 |
((unsigned short *)resultP)[0] = _prod /* & 0xFFFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1519 |
_carry = _prod >> 16; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1520 |
digitP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1521 |
resultP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1522 |
_l -= 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1523 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1524 |
} |
3040 | 1525 |
# endif /* no INT64 */ |
1526 |
# endif /* not GNU-i386 */ |
|
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1527 |
#else /* not LSB_FIRST */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1528 |
if (_v <= 0xFFFF) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1529 |
/* can do it short-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1530 |
* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1531 |
* max: 0xFFFF * 0xFFFF -> 0xFFFE.0001 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1532 |
* + maxCarry (0xFFFF) -> 0xFFFF.0000 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1533 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1534 |
while (_l > 1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1535 |
unsigned int t; |
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1536 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1537 |
t = (digitP[1]<<8) + digitP[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1538 |
_prod = t * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1539 |
resultP[0] = _prod /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1540 |
resultP[1] = (_prod>>8) /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1541 |
_carry = _prod >> 16; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1542 |
digitP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1543 |
resultP += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1544 |
_l -= 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1545 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1546 |
} |
3099
9b76bb13bc5e
16bit-wise multiplication (non LSB machines)
Claus Gittinger <cg@exept.de>
parents:
3040
diff
changeset
|
1547 |
|
3040 | 1548 |
#endif /* LSB_FIRST */ |
1797
8dafe47b33fd
faster multiplication by an integer.
Claus Gittinger <cg@exept.de>
parents:
1794
diff
changeset
|
1549 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1550 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1551 |
* rest is done byte-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1552 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1553 |
while (_l > 0) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1554 |
_prod = *digitP++ * _v + _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1555 |
*resultP++ = _prod /* & 0xFF */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1556 |
_carry = _prod >> 8; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1557 |
_l--; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1558 |
} |
2923 | 1559 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1560 |
_l = __intVal(lResult) - __intVal(len); |
2933 | 1561 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1562 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1563 |
* remaining carry |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1564 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1565 |
while (_carry) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1566 |
*resultP++ = _carry & 0xFF; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1567 |
_carry >>= 8; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1568 |
_l--; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1569 |
} |
2933 | 1570 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1571 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1572 |
* remaining zeros |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1573 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1574 |
while (_l--) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1575 |
*resultP++ = 0; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1576 |
} |
2933 | 1577 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1578 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1579 |
* need compress ? |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1580 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1581 |
if (resultP[-1]) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1582 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1583 |
* no |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1584 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1585 |
RETURN(result); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1586 |
} |
2471
035dbf8db9f3
faster large*small on i386 - use 32*32->64 bit multiplication.
Claus Gittinger <cg@exept.de>
parents:
2267
diff
changeset
|
1587 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1588 |
ok = true; |
85 | 1589 |
} |
1590 |
%}. |
|
92 | 1591 |
" |
1592 |
fall back - normally not reached |
|
1593 |
(could make it a primitive-failure as well) |
|
1594 |
" |
|
85 | 1595 |
ok ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1596 |
carry := 0. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1597 |
1 to:len do:[:i | |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1598 |
prod := (digitByteArray basicAt:i) * val + carry. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1599 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1600 |
carry := prod bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1601 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1602 |
[carry ~~ 0] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1603 |
len := len + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1604 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1605 |
carry := carry bitShift:-8 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1606 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1607 |
[len < lResult] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1608 |
len := len + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1609 |
resultDigitByteArray basicAt:len put:0 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1610 |
] |
68 | 1611 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1612 |
^ result compressed |
92 | 1613 |
! |
1614 |
||
701 | 1615 |
sumFromInteger:anInteger |
1616 |
"sent, when anInteger does not know how to add the receiver. |
|
1617 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
|
92 | 1618 |
|
701 | 1619 |
anInteger > 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1620 |
sign > 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1621 |
^ self absFastPlus:anInteger |
2001 | 1622 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1623 |
^ (self absFastMinus:anInteger) sign:-1 |
701 | 1624 |
]. |
1794 | 1625 |
anInteger == 0 ifTrue:[ |
2001 | 1626 |
^ self |
1794 | 1627 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1628 |
sign > 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1629 |
^ self absFastMinus:anInteger |
1794 | 1630 |
]. |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1631 |
^ (self absFastPlus:anInteger abs) sign:-1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1632 |
|
92 | 1633 |
|
701 | 1634 |
" |
1635 |
12345678901234567890 |
|
1636 |
-12345678901234567890 |
|
1637 |
12345678901234567890 sumFromInteger:0 |
|
1638 |
-12345678901234567890 sumFromInteger:0 |
|
1639 |
12345678901234567890 sumFromInteger:1 |
|
1640 |
12345678901234567890 sumFromInteger:-1 |
|
1641 |
-12345678901234567890 sumFromInteger:1 |
|
1642 |
-12345678901234567890 sumFromInteger:-1 |
|
1643 |
" |
|
1794 | 1644 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1645 |
"Modified: / 9.1.1998 / 13:27:37 / cg" |
68 | 1646 |
! ! |
1647 |
||
701 | 1648 |
!LargeInteger methodsFor:'printing & storing'! |
345 | 1649 |
|
701 | 1650 |
storeOn:aStream |
1651 |
"append a representation of the receiver to aStream, which can |
|
1652 |
be used to reconstruct the receiver." |
|
68 | 1653 |
|
701 | 1654 |
self printOn:aStream. |
1655 |
aStream nextPutAll:' asLargeInteger' |
|
68 | 1656 |
! ! |
1657 |
||
1658 |
!LargeInteger methodsFor:'private'! |
|
1659 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1660 |
absDivMod:anInteger |
701 | 1661 |
"return an array with two LargeIntegers representing |
1662 |
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
|
1663 |
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
|
1664 |
This method needs a rewrite." |
68 | 1665 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1666 |
|dividend divisor |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1667 |
quo digits |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1668 |
shift "{ Class: SmallInteger }" | |
92 | 1669 |
|
701 | 1670 |
anInteger == 0 ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1671 |
^ DivisionByZeroSignal raise |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1672 |
]. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1673 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
1674 |
self = anInteger ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1675 |
^ Array with:1 with:0 |
701 | 1676 |
]. |
92 | 1677 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1678 |
shift := self highBit - anInteger highBit. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1679 |
dividend := self simpleDeepCopy sign:1. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1680 |
shift < 0 ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1681 |
^ Array with:0 with:dividend. |
68 | 1682 |
]. |
4139 | 1683 |
shift == 0 ifTrue:[ |
1684 |
divisor := anInteger simpleDeepCopy. |
|
1685 |
] ifFalse:[ |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1686 |
divisor := anInteger bitShift:shift. |
701 | 1687 |
]. |
1688 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1689 |
quo := self class basicNew numberOfDigits:((shift // 8) + 1). |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1690 |
digits := quo digits. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1691 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1692 |
shift := shift + 1. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1693 |
[shift > 0] whileTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1694 |
(dividend absLess:divisor) ifFalse:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1695 |
digits bitSetAt:shift. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1696 |
(dividend absSubtract: divisor) ifFalse:[ "result == 0" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1697 |
^ Array with:quo compressed with:dividend compressed |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1698 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1699 |
]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1700 |
shift := shift - 1. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1701 |
divisor div2. |
701 | 1702 |
]. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1703 |
^ 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
|
1704 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1705 |
" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1706 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000000 absDivMod:4000000000] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1707 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000000 absDivMod:3000000000] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1708 |
16000000000 absDivMod:4000000000 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1709 |
16000000000 absDivMod:3000000000 |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1710 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1711 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1712 |
"Modified: / 5.11.1996 / 18:40:24 / cg" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1713 |
"Created: / 27.4.1999 / 19:45:44 / stefan" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1714 |
"Modified: / 3.5.1999 / 08:38:01 / stefan" |
92 | 1715 |
! |
1716 |
||
701 | 1717 |
absEq:aLargeInteger |
1718 |
"return true, if abs(self) = abs(theArgument)" |
|
92 | 1719 |
|
701 | 1720 |
|len1 "{ Class: SmallInteger }" |
1721 |
len2 "{ Class: SmallInteger }" |
|
1722 |
d1 "{ Class: SmallInteger }" |
|
1723 |
d2 "{ Class: SmallInteger }" |
|
1724 |
otherDigitByteArray | |
|
92 | 1725 |
|
701 | 1726 |
len1 := digitByteArray size. |
1727 |
otherDigitByteArray := aLargeInteger digits. |
|
1728 |
len2 := otherDigitByteArray size. |
|
92 | 1729 |
|
4157 | 1730 |
"/ the highest digit(s) should not be zero |
1731 |
"/ when properly normalized; |
|
1732 |
"/ but we are tolerant here, to allow for unnormalized |
|
1733 |
"/ numbers to be compared ... |
|
1734 |
||
701 | 1735 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
4157 | 1736 |
len1 := len1 - 1 |
701 | 1737 |
]. |
1738 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
4157 | 1739 |
len2 := len2 - 1 |
68 | 1740 |
]. |
701 | 1741 |
(len1 ~~ len2) ifTrue:[^ false]. |
1742 |
[len1 > 0] whileTrue:[ |
|
4157 | 1743 |
d1 := digitByteArray basicAt:len1. |
1744 |
d2 := otherDigitByteArray basicAt:len1. |
|
1745 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1746 |
len1 := len1 - 1 |
|
68 | 1747 |
]. |
701 | 1748 |
^ true |
4157 | 1749 |
|
1750 |
"Modified: / 8.5.1999 / 18:37:02 / cg" |
|
68 | 1751 |
! |
1752 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
1753 |
absFastDivMod:aPositiveSmallInteger |
68 | 1754 |
"return an array with two LargeIntegers representing |
1890 | 1755 |
abs(self) // aPositiveSmallInteger and abs(self) \\ aPositiveSmallInteger" |
68 | 1756 |
|
77 | 1757 |
|tmp1 "{ Class: SmallInteger }" |
1758 |
prevRest "{ Class: SmallInteger }" |
|
1759 |
count "{ Class: SmallInteger }" |
|
85 | 1760 |
newDigitByteArray result |
1761 |
ok| |
|
68 | 1762 |
|
1890 | 1763 |
aPositiveSmallInteger == 0 ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1764 |
^ DivisionByZeroSignal raise |
68 | 1765 |
]. |
1766 |
||
1767 |
"This cannot happen (if always normalized) |
|
1890 | 1768 |
self < aPositiveSmallInteger ifTrue:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1769 |
^ Array with:0 with:self |
68 | 1770 |
]. |
1771 |
" |
|
1772 |
count := digitByteArray size. |
|
1773 |
result := self class basicNew numberOfDigits:count. |
|
1774 |
newDigitByteArray := result digits. |
|
85 | 1775 |
ok := false. |
1776 |
%{ |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1777 |
OBJ __digits; |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1778 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1779 |
__digits = __INST(digitByteArray); |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1780 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1797
diff
changeset
|
1781 |
if (__isByteArray(__digits) |
85 | 1782 |
&& __isByteArray(newDigitByteArray) |
1890 | 1783 |
&& __bothSmallInteger(count, aPositiveSmallInteger)) { |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1784 |
unsigned INT rest = 0; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1785 |
int index = __intVal(count); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1786 |
int index0; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1787 |
unsigned INT divisor = __intVal(aPositiveSmallInteger); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1788 |
unsigned char *digitBytes = __ByteArrayInstPtr(__digits)->ba_element; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1789 |
unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1790 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1791 |
index0 = index - 1; |
2952 | 1792 |
|
1793 |
#ifdef i386 |
|
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1794 |
if (divisor <= 0xFFFF) { |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1795 |
if ((index & 1) == 0) { /* even number of bytes */ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1796 |
while (index > 1) { |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1797 |
unsigned INT t; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1798 |
unsigned INT div; |
85 | 1799 |
|
2952 | 1800 |
# ifdef i386 /* LSB_FIRST */ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1801 |
index -= 2; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1802 |
t = *((unsigned short *)(&digitBytes[index])); |
2952 | 1803 |
# else |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1804 |
index--; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1805 |
t = digitBytes[index]; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1806 |
index--; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1807 |
t = (t << 8) | digitBytes[index]; |
2952 | 1808 |
# endif |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1809 |
t = t | (rest << 16); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1810 |
div = t / divisor; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1811 |
rest = t % divisor; |
2952 | 1812 |
# ifdef i386 /* LSB_FIRST */ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1813 |
*((unsigned short *)(&resultBytes[index])) = (div & 0xFFFF); |
2952 | 1814 |
# else |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1815 |
resultBytes[index+1] = (div >> 8); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1816 |
resultBytes[index] = (div & 0xFF); |
2952 | 1817 |
# endif |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1818 |
} |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1819 |
} |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1820 |
} |
2952 | 1821 |
#endif |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1822 |
while (index > 0) { |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1823 |
unsigned INT t; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1824 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1825 |
index--; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1826 |
t = digitBytes[index]; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1827 |
t = t | (rest << 8); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1828 |
resultBytes[index] = t / divisor; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1829 |
rest = t % divisor; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1830 |
} |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1831 |
prevRest = __MKSMALLINT(rest); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1832 |
ok = true; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1833 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1834 |
/* |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1835 |
* no need to normalize ? |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1836 |
*/ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1837 |
while ((index0 > sizeof(INT)) && (resultBytes[index0]==0)) { |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1838 |
index0--; |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1839 |
} |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1840 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1841 |
if ((index0 > sizeof(INT)) |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1842 |
&& (resultBytes[index0-1])) { |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1843 |
RETURN ( __ARRAY_WITH2(result, prevRest)); |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1844 |
} |
85 | 1845 |
} |
1846 |
%}. |
|
1847 |
" |
|
1848 |
slow code - not normally reached |
|
1849 |
(could also do a primitiveFailure here) |
|
1850 |
" |
|
92 | 1851 |
ok ifFalse:[ |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1852 |
self primitiveFailed |
68 | 1853 |
]. |
1854 |
||
2933 | 1855 |
^ 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
|
1856 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1857 |
" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1858 |
((16r1234 asLargeInteger absFastDiv:16rffff) at:2) printStringRadix:16 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1859 |
((16r00123456 asLargeInteger absFastDiv:16rffffff) at:2) printStringRadix:16 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
1860 |
" |
68 | 1861 |
! |
1862 |
||
701 | 1863 |
absFastMinus:aSmallInteger |
1864 |
"return a LargeInteger representing abs(self) - abs(theArgument). |
|
1865 |
The result is not normalized." |
|
1866 |
||
1794 | 1867 |
|result resultDigitByteArray lastDigit |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1868 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1869 |
rsltLen "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1870 |
index "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1871 |
borrow "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1872 |
diff "{ Class: SmallInteger }" | |
701 | 1873 |
|
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1874 |
"/ the following code only works with |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1875 |
"/ smallIntegers in te range _MIN_INT+255 .. _MAX_INT-255 |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1876 |
|
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1877 |
((aSmallInteger < (SmallInteger minVal + 255)) |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1878 |
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[ |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1879 |
^ self absMinus:(LargeInteger value:aSmallInteger). |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1880 |
]. |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1881 |
|
701 | 1882 |
len := digitByteArray size. |
1883 |
||
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1884 |
rsltLen := len + 1. |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1885 |
result := self class basicNew numberOfDigits:rsltLen. |
701 | 1886 |
resultDigitByteArray := result digits. |
1887 |
||
1888 |
index := 1. |
|
1889 |
borrow := aSmallInteger abs. |
|
1890 |
||
1891 |
[borrow ~~ 0] whileTrue:[ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1892 |
(index <= len) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1893 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1894 |
borrow := borrow bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1895 |
diff < 0 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1896 |
diff := diff + 256. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1897 |
borrow := borrow + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1898 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1899 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1900 |
diff := borrow bitAnd:255. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1901 |
borrow := borrow bitShift:-8. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1902 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1903 |
resultDigitByteArray basicAt:index put:(lastDigit := diff). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1904 |
index := index + 1 |
701 | 1905 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1906 |
(index <= rsltLen) ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1907 |
[index <= len] whileTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1908 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1909 |
index := index + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1910 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1911 |
lastDigit := 0. |
701 | 1912 |
]. |
1913 |
||
1794 | 1914 |
lastDigit == 0 ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
1915 |
^ result compressed |
1794 | 1916 |
]. |
701 | 1917 |
^ result |
1918 |
||
1919 |
" |
|
1920 |
12345678900000000000 absFastMinus:1 |
|
1921 |
12345678900000000000 absFastMinus:1000000 |
|
1922 |
12345678900000000000 absFastMinus:255 |
|
1923 |
(SmallInteger maxVal + 1) absFastMinus:1 |
|
1924 |
(SmallInteger minVal - 1) absFastMinus:1 |
|
1925 |
" |
|
1794 | 1926 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1927 |
"Modified: 24.3.1997 / 21:33:25 / cg" |
701 | 1928 |
! |
1929 |
||
1930 |
absFastPlus:aSmallInteger |
|
1931 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
|
2952 | 1932 |
The result is normalized." |
701 | 1933 |
|
1794 | 1934 |
|result resultDigitByteArray lastDigit |
2952 | 1935 |
ok |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1936 |
len "{ Class: SmallInteger }" |
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1937 |
rsltLen "{ Class: SmallInteger }" |
701 | 1938 |
index "{ Class: SmallInteger }" |
1939 |
carry "{ Class: SmallInteger }" | |
|
1940 |
||
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1941 |
"/ the following code only works with |
4157 | 1942 |
"/ 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
|
1943 |
|
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1944 |
((aSmallInteger < (SmallInteger minVal + 255)) |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1945 |
or:[aSmallInteger > (SmallInteger maxVal - 255)]) ifTrue:[ |
4157 | 1946 |
^ self absPlus:(LargeInteger value:aSmallInteger). |
3440
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1947 |
]. |
ac93772ac2ca
oops absFastPlus/Minus must care for the arguments range.
Claus Gittinger <cg@exept.de>
parents:
3438
diff
changeset
|
1948 |
|
701 | 1949 |
len := digitByteArray size. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1950 |
rsltLen := len + 1. |
701 | 1951 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
1952 |
result := self class basicNew numberOfDigits:rsltLen. |
701 | 1953 |
resultDigitByteArray := result digits. |
1954 |
||
2952 | 1955 |
%{ |
1956 |
if (__isByteArray(__INST(digitByteArray)) |
|
1957 |
&& __isByteArray(resultDigitByteArray) |
|
1958 |
&& __isSmallInteger(aSmallInteger)) { |
|
4157 | 1959 |
int __carry = __intVal(aSmallInteger); |
1960 |
int __index = 1; |
|
1961 |
int __len = __intVal(len); |
|
1962 |
unsigned char *__src = (unsigned char *)(__ByteArrayInstPtr(__INST(digitByteArray))->ba_element); |
|
1963 |
unsigned char *__dst = (unsigned char *)(__ByteArrayInstPtr(resultDigitByteArray)->ba_element); |
|
1964 |
int __lastDigit = 0; |
|
1965 |
||
1966 |
if (__carry < 0) { |
|
1967 |
__carry = -__carry; |
|
1968 |
} |
|
1969 |
||
1970 |
while (__carry) { |
|
1971 |
if (__index <= __len) { |
|
1972 |
__carry += __src[__index-1]; |
|
1973 |
} |
|
1974 |
__dst[__index-1] = __lastDigit = __carry & 0xFF; |
|
1975 |
__carry >>= 8; |
|
1976 |
__index++; |
|
1977 |
} |
|
1978 |
if (__index <= __intVal(rsltLen)) { |
|
1979 |
while (__index <= __len) { |
|
1980 |
__dst[__index-1] = __src[__index-1]; |
|
1981 |
__index++; |
|
1982 |
} |
|
1983 |
__lastDigit = 0; |
|
1984 |
} |
|
1985 |
if (lastDigit) { |
|
1986 |
RETURN (result); |
|
1987 |
} |
|
1988 |
ok = true; |
|
2952 | 1989 |
} |
1990 |
%}. |
|
1991 |
||
1992 |
ok ~~ true ifTrue:[ |
|
4157 | 1993 |
index := 1. |
1994 |
carry := aSmallInteger abs. |
|
1995 |
||
1996 |
[carry ~~ 0] whileTrue:[ |
|
1997 |
(index <= len) ifTrue:[ |
|
1998 |
carry := (digitByteArray basicAt:index) + carry. |
|
1999 |
]. |
|
2000 |
resultDigitByteArray basicAt:index put:(lastDigit := carry bitAnd:16rFF). |
|
2001 |
carry := carry bitShift:-8. |
|
2002 |
index := index + 1 |
|
2003 |
]. |
|
2004 |
||
2005 |
(index <= rsltLen) ifTrue:[ |
|
2006 |
[index <= len] whileTrue:[ |
|
2007 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
|
2008 |
index := index + 1 |
|
2009 |
]. |
|
2010 |
lastDigit := 0. |
|
2011 |
]. |
|
2012 |
||
2013 |
lastDigit ~~ 0 ifTrue:[ |
|
2014 |
^ result |
|
2015 |
]. |
|
701 | 2016 |
]. |
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2017 |
|
2952 | 2018 |
^ result compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2019 |
|
2495
0e6c781fd370
care to not create uncompressed largeInts
Claus Gittinger <cg@exept.de>
parents:
2471
diff
changeset
|
2020 |
"Modified: 24.3.1997 / 21:32:41 / cg" |
701 | 2021 |
! |
2022 |
||
2023 |
absLess:aLargeInteger |
|
1890 | 2024 |
"return true, if abs(self) < abs(theArgument). |
2025 |
This handles unnormalized largeIntegers." |
|
701 | 2026 |
|
1890 | 2027 |
|myLen "{ Class: SmallInteger }" |
2028 |
otherLen "{ Class: SmallInteger }" |
|
701 | 2029 |
d1 "{ Class: SmallInteger }" |
2030 |
d2 "{ Class: SmallInteger }" |
|
2031 |
otherDigitByteArray | |
|
2032 |
||
1890 | 2033 |
myLen := digitByteArray size. |
701 | 2034 |
otherDigitByteArray := aLargeInteger digits. |
1890 | 2035 |
otherLen := otherDigitByteArray size. |
701 | 2036 |
|
4157 | 2037 |
"/ the highest digit(s) should not be zero |
2038 |
"/ when properly normalized; |
|
2039 |
"/ but we are tolerant here, to allow for unnormalized |
|
2040 |
"/ numbers to be compared ... |
|
2041 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2042 |
[myLen > 0 and:[(digitByteArray basicAt:myLen) == 0]] whileTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2043 |
myLen := myLen - 1 |
701 | 2044 |
]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2045 |
[otherLen > 0 and:[(otherDigitByteArray basicAt:otherLen) == 0]] whileTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2046 |
otherLen := otherLen - 1 |
701 | 2047 |
]. |
1890 | 2048 |
(myLen < otherLen) ifTrue:[^ true]. |
2049 |
(myLen > otherLen) ifTrue:[^ false]. |
|
701 | 2050 |
|
1890 | 2051 |
[myLen > 0] whileTrue:[ |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2052 |
d1 := digitByteArray basicAt:myLen. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2053 |
d2 := otherDigitByteArray basicAt:myLen. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2054 |
d1 == d2 ifFalse:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2055 |
(d1 < d2) ifTrue:[^ true]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2056 |
^ false |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2057 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2058 |
myLen := myLen - 1 |
701 | 2059 |
]. |
2060 |
^ false |
|
1890 | 2061 |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2062 |
"Modified: / 3.5.1999 / 08:06:28 / stefan" |
4157 | 2063 |
"Modified: / 8.5.1999 / 18:37:11 / cg" |
701 | 2064 |
! |
2065 |
||
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2066 |
absLessEq:aLargeInteger |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2067 |
"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
|
2068 |
This handles unnormalized largeIntegers." |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2069 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2070 |
|myLen "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2071 |
otherLen "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2072 |
d1 "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2073 |
d2 "{ Class: SmallInteger }" |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2074 |
otherDigitByteArray | |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2075 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2076 |
myLen := digitByteArray size. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2077 |
otherDigitByteArray := aLargeInteger digits. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2078 |
otherLen := otherDigitByteArray size. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2079 |
|
4157 | 2080 |
"/ the highest digit(s) should not be zero |
2081 |
"/ when properly normalized; |
|
2082 |
"/ but we are tolerant here, to allow for unnormalized |
|
2083 |
"/ numbers to be compared ... |
|
2084 |
||
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2085 |
[(digitByteArray basicAt:myLen) == 0] whileTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2086 |
myLen := myLen - 1 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2087 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2088 |
[(otherDigitByteArray basicAt:otherLen) == 0] whileTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2089 |
otherLen := otherLen - 1 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2090 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2091 |
(myLen < otherLen) ifTrue:[^ true]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2092 |
(myLen > otherLen) ifTrue:[^ false]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2093 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2094 |
[myLen > 0] whileTrue:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2095 |
d1 := digitByteArray basicAt:myLen. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2096 |
d2 := otherDigitByteArray basicAt:myLen. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2097 |
d1 == d2 ifFalse:[ |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2098 |
(d1 < d2) ifTrue:[^ true]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2099 |
^ false. |
3984
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2100 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2101 |
myLen := myLen - 1 |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2102 |
]. |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2103 |
^ true |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2104 |
|
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2105 |
"Created: / 13.2.1998 / 12:19:45 / stefan" |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2106 |
"Modified: / 30.4.1999 / 12:46:31 / stefan" |
4157 | 2107 |
"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
|
2108 |
! |
45a76e2f4236
Fix so that // and \\ truncate towards negative infinity and
Stefan Vogel <sv@exept.de>
parents:
3910
diff
changeset
|
2109 |
|
701 | 2110 |
absMinus:aLargeInteger |
2111 |
"return a LargeInteger representing abs(self) - abs(theArgument)" |
|
2112 |
||
2113 |
|result done |
|
2114 |
otherDigitByteArray resultDigitByteArray |
|
2115 |
len1 "{ Class: SmallInteger }" |
|
2116 |
len2 "{ Class: SmallInteger }" |
|
2117 |
index "{ Class: SmallInteger }" |
|
2118 |
borrow "{ Class: SmallInteger }" |
|
2119 |
diff "{ Class: SmallInteger }" |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2120 |
carry "{ Class: SmallInteger }" |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2121 |
lResult| |
701 | 2122 |
|
2123 |
len1 := digitByteArray size. |
|
2124 |
otherDigitByteArray := aLargeInteger digits. |
|
2125 |
len2 := otherDigitByteArray size. |
|
2126 |
||
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2127 |
lResult := (len1 max: len2) + 1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2128 |
result := self class basicNew numberOfDigits:lResult. |
701 | 2129 |
resultDigitByteArray := result digits. |
2130 |
||
2131 |
index := 1. |
|
2132 |
borrow := 0. |
|
2133 |
||
2134 |
done := false. |
|
2135 |
[done] whileFalse:[ |
|
2001 | 2136 |
diff := borrow. |
2137 |
(index <= len1) ifTrue:[ |
|
2138 |
diff := diff + (digitByteArray basicAt:index). |
|
2139 |
(index <= len2) ifTrue:[ |
|
2140 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
2141 |
] |
|
2142 |
] ifFalse:[ |
|
2143 |
(index <= len2) ifTrue:[ |
|
2144 |
diff := diff - (otherDigitByteArray basicAt:index) |
|
2145 |
] ifFalse:[ |
|
2146 |
"end reached" |
|
2147 |
done := true |
|
2148 |
] |
|
2149 |
]. |
|
701 | 2150 |
|
2001 | 2151 |
"/ workaround for |
2152 |
"/ gcc code generator bug |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2153 |
|
2001 | 2154 |
(diff >= 0) ifTrue:[ |
2155 |
borrow := 0 |
|
2156 |
] ifFalse:[ |
|
2157 |
borrow := -1. |
|
2158 |
diff := diff + 16r100 |
|
2159 |
]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2160 |
|
701 | 2161 |
"/ (diff < 0) ifTrue:[ |
2162 |
"/ borrow := -1. |
|
2163 |
"/ diff := diff + 16r100 |
|
2164 |
"/ ] ifFalse:[ |
|
2165 |
"/ borrow := 0 |
|
2166 |
"/ ]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2167 |
|
2001 | 2168 |
resultDigitByteArray basicAt:index put:diff. |
2169 |
index := index + 1 |
|
701 | 2170 |
]. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2171 |
|
701 | 2172 |
(borrow ~~ 0) ifTrue:[ |
2001 | 2173 |
"/ must generate 255's complement |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
2174 |
|
2001 | 2175 |
result sign:-1. |
2176 |
[index <= lResult] whileTrue:[ |
|
2177 |
resultDigitByteArray basicAt:index put:16rFF. |
|
2178 |
index := index + 1. |
|
2179 |
]. |
|
2180 |
index := resultDigitByteArray size. |
|
2181 |
[index > 0] whileTrue:[ |
|
2182 |
resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)). |
|
2183 |
index := index -1. |
|
2184 |
]. |
|
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
2185 |
|
2001 | 2186 |
index := 1. |
2187 |
carry := 1. |
|
2188 |
[carry ~~ 0] whileTrue:[ |
|
2189 |
(index <= lResult) ifTrue:[ |
|
2190 |
carry := (resultDigitByteArray basicAt:index) + carry. |
|
2191 |
]. |
|
2192 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
|
2193 |
carry := carry bitShift:-8. |
|
2194 |
index := index + 1 |
|
2195 |
]. |
|
701 | 2196 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2197 |
^ result compressed |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
2198 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2199 |
"Modified: 5.11.1996 / 14:09:25 / cg" |
701 | 2200 |
! |
2201 |
||
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2202 |
absMod:anInteger |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2203 |
"return a LargeIntegers representing |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2204 |
abs(self) \\ abs(theArgument). |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2205 |
Used as a helper for \\ and rem:" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2206 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2207 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2208 |
|dividend divisor |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2209 |
shift "{ Class: SmallInteger }" | |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2210 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2211 |
anInteger == 0 ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2212 |
^ DivisionByZeroSignal raise |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2213 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2214 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2215 |
self = anInteger ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2216 |
^ 0 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2217 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2218 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2219 |
shift := self highBit - anInteger highBit. |
4140
3e66cfaae7a7
Some fixes for absMod: on a negative number
Stefan Vogel <sv@exept.de>
parents:
4139
diff
changeset
|
2220 |
dividend := self simpleDeepCopy sign:1. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2221 |
shift < 0 ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2222 |
^ dividend. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2223 |
]. |
4139 | 2224 |
shift == 0 ifTrue:[ |
2225 |
divisor := anInteger simpleDeepCopy. |
|
2226 |
] ifFalse:[ |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2227 |
divisor := anInteger bitShift:shift. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2228 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2229 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2230 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2231 |
shift := shift + 1. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2232 |
[shift > 0] whileTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2233 |
(dividend absLess:divisor) ifFalse:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2234 |
(dividend absSubtract: divisor) ifFalse:[ "result == 0" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2235 |
^ dividend compressed |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2236 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2237 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2238 |
shift := shift - 1. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2239 |
divisor div2. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2240 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2241 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2242 |
^ dividend compressed |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2243 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2244 |
" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2245 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000001 absMod:4000000001] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2246 |
Time millisecondsToRun:[ 10000 timesRepeat:[ 16000000001 absDivMod:4000000001] ] |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2247 |
16000000000 absMod:4000000000 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2248 |
16000000000 absMod:3000000000 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2249 |
" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2250 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2251 |
"Modified: / 5.11.1996 / 18:40:24 / cg" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2252 |
"Created: / 27.4.1999 / 19:45:44 / stefan" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2253 |
"Modified: / 3.5.1999 / 08:41:39 / stefan" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2254 |
! |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2255 |
|
68 | 2256 |
absMul:aLargeInteger |
2257 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
2258 |
||
92 | 2259 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 2260 |
idx "{ Class: SmallInteger }" |
2261 |
carry "{ Class: SmallInteger }" |
|
2262 |
len1 "{ Class: SmallInteger }" |
|
2263 |
len2 "{ Class: SmallInteger }" |
|
2264 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 2265 |
prod "{ Class: SmallInteger }" |
2266 |
v "{ Class: SmallInteger }"| |
|
68 | 2267 |
|
2268 |
len1 := digitByteArray size. |
|
2269 |
otherDigitByteArray := aLargeInteger digits. |
|
2270 |
len2 := otherDigitByteArray size. |
|
2271 |
||
4151 | 2272 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2). |
68 | 2273 |
resultDigitByteArray := result digits. |
92 | 2274 |
ok := false. |
2275 |
%{ |
|
1133 | 2276 |
if (__isByteArray(__INST(digitByteArray)) |
92 | 2277 |
&& __isByteArray(otherDigitByteArray) |
2278 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 2279 |
&& __bothSmallInteger(len1, len2)) { |
3910 | 2280 |
unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
2281 |
unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
2282 |
unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
4151 | 2283 |
unsigned char *_p1, *_p2, *_pResult, *_pResult1; |
2284 |
unsigned INT _v; |
|
3910 | 2285 |
int _len1 = __intVal(len1); |
2286 |
int _len2 = __intVal(len2); |
|
2287 |
||
4151 | 2288 |
for (_p1 = myBytes; _p1 < myBytes + _len1; _p1++) { |
2289 |
_pResult = resultBytes + (_p1 - myBytes); |
|
2290 |
for (_p2 = otherBytes; _p2 < otherBytes + _len2; _p2++) { |
|
2291 |
_v = ((*_p1) * (*_p2)) + *_pResult; |
|
2292 |
*_pResult = _v & 0xFF; |
|
2293 |
_v >>= 8; /* now _v contains the carry*/ |
|
2294 |
_pResult++; |
|
2295 |
if (_v) { |
|
2296 |
for (_pResult1 = _pResult; _v; _pResult1++) { |
|
2297 |
_v += *_pResult1; |
|
2298 |
*_pResult1 = _v & 0xFF; |
|
2299 |
_v >>= 8; |
|
3910 | 2300 |
} |
2301 |
} |
|
2302 |
} |
|
2303 |
} |
|
2304 |
ok = true; |
|
92 | 2305 |
} |
2306 |
%}. |
|
2307 |
ok ifFalse:[ |
|
3910 | 2308 |
1 to:len1 do:[:index1 | |
2309 |
1 to:len2 do:[:index2 | |
|
2310 |
dstIndex := index1 + index2 - 1. |
|
2311 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
2312 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
2313 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
2314 |
carry := prod bitShift:-8. |
|
2315 |
carry ~~ 0 ifTrue:[ |
|
2316 |
idx := dstIndex + 1. |
|
2317 |
[carry ~~ 0] whileTrue:[ |
|
2318 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
2319 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
2320 |
carry := v bitShift:-8. |
|
2321 |
idx := idx + 1 |
|
2322 |
] |
|
2323 |
] |
|
2324 |
] |
|
2325 |
]. |
|
68 | 2326 |
]. |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2327 |
^ result compressed |
68 | 2328 |
! |
2329 |
||
2330 |
absPlus:aLargeInteger |
|
2331 |
"return a LargeInteger representing abs(self) + abs(theArgument)" |
|
2332 |
||
2333 |
|result done otherDigitByteArray resultDigitByteArray |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2334 |
len1 "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2335 |
len2 "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2336 |
newLen "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2337 |
index "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2338 |
carry "{ Class: SmallInteger }" |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2339 |
sum "{ Class: SmallInteger }" | |
68 | 2340 |
|
2341 |
otherDigitByteArray := aLargeInteger digits. |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2342 |
|
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2343 |
%{ |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2344 |
OBJ _digitByteArray = __INST(digitByteArray); |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2345 |
|
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2346 |
if (__isByteArray(_digitByteArray) |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2347 |
&& __isByteArray(otherDigitByteArray)) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2348 |
int _len1, _len2, _newLen; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2349 |
unsigned char *_myDigits, *_otherDigits, *_newDigits; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2350 |
int _index, _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2351 |
int _comLen; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2352 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2353 |
_len1 = __byteArraySize(_digitByteArray); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2354 |
_len2 = __byteArraySize(otherDigitByteArray); |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2355 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2356 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2357 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
68 | 2358 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2359 |
if (_len1 < _len2) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2360 |
_comLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2361 |
_newLen = _len2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2362 |
if (_otherDigits[_len2 - 1] == 0xFF) _newLen++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2363 |
} else if (_len2 < _len1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2364 |
_comLen = _len2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2365 |
_newLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2366 |
if (_myDigits[_len1 - 1] == 0xFF) _newLen++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2367 |
} else { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2368 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2369 |
* there can only be an overflow from the high bytes, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2370 |
* if their sum is >= 255 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2371 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2372 |
_newLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2373 |
if ((_otherDigits[_len2 - 1] + _myDigits[_len1 - 1]) >= 0xFF) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2374 |
_newLen++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2375 |
} else { |
2850 | 2376 |
#if defined(i386) || defined(alpha) /* actually: LSB_FIRST */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2377 |
if (_newLen == sizeof(INT)) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2378 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2379 |
* two 32bit numbers, no carry - a very common case ... |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2380 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2381 |
unsigned INT _sum = *(unsigned INT *)_otherDigits + *(unsigned INT *)_myDigits; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2382 |
RETURN (__MKULARGEINT(_sum)); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2383 |
} |
2849 | 2384 |
#endif /* not LSB_FIRST */ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2385 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2386 |
_comLen = _len1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2387 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2388 |
resultDigitByteArray = __BYTEARRAY_UNINITIALIZED_NEW_INT(_newLen); |
68 | 2389 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2390 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2391 |
* must refetch - GC could have been invoked |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2392 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2393 |
_digitByteArray = __INST(digitByteArray); |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2394 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2395 |
_myDigits = __ByteArrayInstPtr(_digitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2396 |
_otherDigits = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2397 |
_newDigits = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2398 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2399 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2400 |
* add them ... |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2401 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2402 |
_index = 1; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2403 |
_carry = 0; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2404 |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2405 |
#if defined(i386) || defined(alpha) /* actually: LSB first */ |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2406 |
# ifdef INT64 |
2923 | 2407 |
/* |
2408 |
* have a 64bit integer type; |
|
2409 |
* add long-wise |
|
2410 |
*/ |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2411 |
while (_index < _comLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2412 |
UINT64 _sum; |
2923 | 2413 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2414 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2415 |
_sum += *(unsigned *)(&(_myDigits[_index - 1])); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2416 |
_sum += *(unsigned *)(&(_otherDigits[_index - 1])); |
2952 | 2417 |
_carry = _sum >> 32; |
2923 | 2418 |
_sum = _sum & 0xFFFFFFFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2419 |
*(unsigned *)(&(_newDigits[_index - 1])) = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2420 |
_index += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2421 |
} |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2422 |
# endif |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2423 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2424 |
* add short-wise |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2425 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2426 |
while (_index < _comLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2427 |
unsigned int _sum; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2428 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2429 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2430 |
_sum += *(unsigned short *)(&(_myDigits[_index - 1])); |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2431 |
_sum += *(unsigned short *)(&(_otherDigits[_index - 1])); |
2952 | 2432 |
_carry = _sum >> 16; |
2923 | 2433 |
_sum = _sum & 0xFFFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2434 |
*(unsigned short *)(&(_newDigits[_index - 1])) = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2435 |
_index += 2; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2436 |
} |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2437 |
#endif |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2438 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2439 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2440 |
* add byte |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2441 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2442 |
while (_index <= _comLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2443 |
unsigned int _sum; |
68 | 2444 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2445 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2446 |
_sum += _myDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2447 |
_sum += _otherDigits[_index - 1]; |
2952 | 2448 |
_carry = _sum >> 8; |
2923 | 2449 |
_sum = _sum & 0xFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2450 |
_newDigits[_index - 1] = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2451 |
_index++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2452 |
} |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2453 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2454 |
/* |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2455 |
* rest |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2456 |
*/ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2457 |
while (_index <= _newLen) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2458 |
unsigned int _sum; |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2459 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2460 |
_sum = _carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2461 |
if (_index <= _len1) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2462 |
_sum += _myDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2463 |
if (_index <= _len2) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2464 |
_sum += _otherDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2465 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2466 |
} else { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2467 |
if (_index <= _len2) { |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2468 |
_sum += _otherDigits[_index - 1]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2469 |
} |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2470 |
} |
2952 | 2471 |
_carry = _sum >> 8; |
2923 | 2472 |
_sum = _sum & 0xFF; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2473 |
_newDigits[_index - 1] = _sum; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2474 |
_index++; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2475 |
} |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2476 |
} |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2477 |
%}. |
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2478 |
resultDigitByteArray notNil ifTrue:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2479 |
result := self class basicNew. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2480 |
result setDigits:resultDigitByteArray. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2481 |
] ifFalse:[ |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2482 |
len1 := digitByteArray size. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2483 |
len2 := otherDigitByteArray size. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2484 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2485 |
"/ earlier versions estimated the newLength as: |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2486 |
"/ (len1 max:len2) + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2487 |
"/ and reduced the result. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2488 |
"/ however, if one of the addends is smaller, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2489 |
"/ the result will never require another digit, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2490 |
"/ if the highest digit of the larger addent is |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2491 |
"/ not equal to 255. Therefore, in most cases, |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2492 |
"/ we can avoid the computation and resizing |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2493 |
"/ in #reduced. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2494 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2495 |
len1 < len2 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2496 |
newLen := len2. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2497 |
(otherDigitByteArray at:len2) == 16rFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2498 |
newLen := newLen + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2499 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2500 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2501 |
len2 < len1 ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2502 |
newLen := len1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2503 |
(digitByteArray at:len1) == 16rFF ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2504 |
newLen := newLen + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2505 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2506 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2507 |
newLen := len1 + 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2508 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2509 |
]. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2510 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2511 |
result := self class basicNew numberOfDigits:newLen. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2512 |
resultDigitByteArray := result digits. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2513 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2514 |
index := 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2515 |
carry := 0. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2516 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2517 |
done := false. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2518 |
[done] whileFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2519 |
sum := carry. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2520 |
(index <= len1) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2521 |
sum := sum + (digitByteArray basicAt:index). |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2522 |
(index <= len2) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2523 |
sum := sum + (otherDigitByteArray basicAt:index) |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2524 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2525 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2526 |
(index <= len2) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2527 |
sum := sum + (otherDigitByteArray basicAt:index) |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2528 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2529 |
"end reached" |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2530 |
done := true |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2531 |
] |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2532 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2533 |
(sum >= 16r100) ifTrue:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2534 |
carry := 1. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2535 |
sum := sum - 16r100 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2536 |
] ifFalse:[ |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2537 |
carry := 0 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2538 |
]. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2539 |
resultDigitByteArray basicAt:index put:sum. |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2540 |
index := index + 1 |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2541 |
]. |
68 | 2542 |
]. |
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2543 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2544 |
^ result compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1857
diff
changeset
|
2545 |
|
2848
8be5134b3679
tuned addition - especially for 4-byte largeInts
Claus Gittinger <cg@exept.de>
parents:
2816
diff
changeset
|
2546 |
"Modified: 11.8.1997 / 03:23:37 / cg" |
68 | 2547 |
! |
2548 |
||
1890 | 2549 |
absSubtract:aLargeInteger |
2550 |
"destructively subtract aLargeInteger from myself. |
|
2551 |
A helper for division; only allowed for positive receiver |
|
2552 |
and argument. The receiver must be >= the argument. |
|
2553 |
The receiver must be a temporary scratch-number" |
|
2554 |
||
2521 | 2555 |
|done |
2556 |
otherDigitByteArray |
|
1890 | 2557 |
len1 "{ Class: SmallInteger }" |
2558 |
len2 "{ Class: SmallInteger }" |
|
2559 |
index "{ Class: SmallInteger }" |
|
2560 |
borrow "{ Class: SmallInteger }" |
|
2561 |
diff "{ Class: SmallInteger }" |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2562 |
notZero |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2563 |
| |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2564 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2565 |
notZero := false. |
1890 | 2566 |
len1 := digitByteArray size. |
2567 |
otherDigitByteArray := aLargeInteger digits. |
|
2568 |
len2 := otherDigitByteArray size. |
|
2569 |
len2 > len1 ifTrue:[ |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2570 |
[(otherDigitByteArray at:len2) == 0] whileTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2571 |
len2 := len2 - 1 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2572 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2573 |
len2 > len1 ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2574 |
self halt "/ may not be called that way |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2575 |
]. |
1890 | 2576 |
]. |
2577 |
||
2578 |
index := 1. |
|
2579 |
borrow := 0. |
|
2580 |
||
2581 |
done := false. |
|
2582 |
[index <= len1] whileTrue:[ |
|
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2583 |
diff := borrow. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2584 |
diff := diff + (digitByteArray basicAt:index). |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2585 |
index <= len2 ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2586 |
diff := diff - (otherDigitByteArray basicAt:index). |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2587 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2588 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2589 |
"/ workaround for |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2590 |
"/ gcc code generator bug |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2591 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2592 |
(diff >= 0) ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2593 |
borrow := 0 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2594 |
] ifFalse:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2595 |
borrow := -1. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2596 |
diff := diff + 16r100 |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2597 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2598 |
diff ~~ 0 ifTrue:[ |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2599 |
notZero := true |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2600 |
]. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2601 |
digitByteArray basicAt:index put:diff. |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2602 |
index := index + 1 |
1890 | 2603 |
]. |
4137
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2604 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2605 |
^ notZero |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2606 |
|
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2607 |
"Created: / 5.11.1996 / 16:23:47 / cg" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2608 |
"Modified: / 5.11.1996 / 18:56:50 / cg" |
f85d341a13e4
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
3984
diff
changeset
|
2609 |
"Modified: / 27.4.1999 / 18:08:23 / stefan" |
1890 | 2610 |
! |
2611 |
||
2612 |
div2 |
|
2613 |
"destructively divide the receiver by 2. |
|
2614 |
private - used for division only. |
|
2615 |
This is worth a primitive." |
|
2616 |
||
2617 |
%{ /* NOCONTEXT */ |
|
2618 |
OBJ __digits = __INST(digitByteArray); |
|
2619 |
||
2620 |
if (__isByteArray(__digits)) { |
|
2001 | 2621 |
int __nBytes = __byteArraySize(__digits); |
2622 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2623 |
unsigned int __this, __next; |
2001 | 2624 |
int __idx; |
1890 | 2625 |
|
2001 | 2626 |
if (__nBytes == 1) { |
2627 |
__bp[0] >>= 1; |
|
2628 |
RETURN (self); |
|
2629 |
} |
|
1890 | 2630 |
|
2001 | 2631 |
__idx = 1; |
1890 | 2632 |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2633 |
#if defined(i386) /* XXX actually: LSB_FIRST */ |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2634 |
if (sizeof(unsigned int) == 4) { |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2635 |
if ((__idx + 4) < __nBytes) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2636 |
__this = ((unsigned int *)__bp)[0]; |
68 | 2637 |
|
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2638 |
while ((__idx + 4) < __nBytes) { |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2639 |
__next = ((unsigned int *)__bp)[1]; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2640 |
__this = (__this >> 1) /* & 0x7FFFFFF */; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2641 |
__this |= __next << 31; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2642 |
((unsigned int *)__bp)[0] = __this; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2643 |
__this = __next; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2644 |
__bp += 4; |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2645 |
__idx += 4; |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2646 |
} |
2001 | 2647 |
} |
2648 |
} |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2649 |
#endif |
1890 | 2650 |
|
2001 | 2651 |
__this = __bp[0]; |
2652 |
while (__idx < __nBytes) { |
|
2653 |
__next = __bp[1]; |
|
2654 |
__this >>= 1; |
|
2655 |
__this |= __next << 7; |
|
2656 |
__bp[0] = __this; |
|
2657 |
__this = __next; |
|
2658 |
__bp++; |
|
2659 |
__idx++; |
|
2660 |
} |
|
2661 |
__bp[0] = __this >> 1; |
|
2662 |
RETURN (self); |
|
1890 | 2663 |
} |
2664 |
%}. |
|
2665 |
self primitiveFailed |
|
2666 |
||
2667 |
" |
|
2668 |
100000 asLargeInteger div2 |
|
2669 |
" |
|
2670 |
||
2671 |
"Modified: 5.11.1996 / 16:12:56 / cg" |
|
2672 |
! |
|
2673 |
||
2674 |
mul2 |
|
2675 |
"destructively multiply the receiver by 2. |
|
2676 |
private - used for division only. |
|
2677 |
This is worth a primitive." |
|
2678 |
||
2679 |
|nBytes "{ Class: SmallInteger }" |
|
2680 |
b "{ Class: SmallInteger }" |
|
2681 |
t| |
|
2682 |
||
2683 |
nBytes := digitByteArray size. |
|
2684 |
||
2685 |
b := digitByteArray at:nBytes. |
|
2686 |
(b bitAnd:16r80) ~~ 0 ifTrue:[ |
|
2001 | 2687 |
"/ need another byte |
2688 |
nBytes := nBytes + 1. |
|
2689 |
t := ByteArray uninitializedNew:nBytes. |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2690 |
t replaceFrom:1 to:nBytes-1 with:digitByteArray startingAt:1. |
2001 | 2691 |
t at:nBytes put:0. |
2692 |
digitByteArray := t. |
|
1890 | 2693 |
]. |
2694 |
||
2695 |
%{ |
|
2696 |
OBJ __digits = __INST(digitByteArray); |
|
2697 |
||
2698 |
if (__isByteArray(__digits)) { |
|
2001 | 2699 |
int __nBytes = __intVal(nBytes); |
2700 |
unsigned char *__bp = __ByteArrayInstPtr(__digits)->ba_element; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2701 |
unsigned int __carry = 0, __newCarry, __this; |
2001 | 2702 |
int __idx; |
1890 | 2703 |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2704 |
#if defined(i386) /* XXX actually: LSB_FIRST */ |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2705 |
if (sizeof(unsigned int) == 4) { |
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2706 |
while (__nBytes >= 4) { |
3161
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2707 |
__this = ((unsigned int *)__bp)[0]; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2708 |
__newCarry = (__this >> 31) /* & 1 */; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2709 |
((unsigned int *)__bp)[0] = (__this << 1) | __carry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2710 |
__carry = __newCarry; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2711 |
__bp += 4; |
923a6931dded
avoid sending #class twice in +/-
Claus Gittinger <cg@exept.de>
parents:
3119
diff
changeset
|
2712 |
__nBytes -= 4; |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2713 |
} |
2001 | 2714 |
} |
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2715 |
#endif |
2001 | 2716 |
while (__nBytes) { |
2717 |
__this = __bp[0]; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2718 |
__newCarry = (__this >> 7) /* & 1 */; |
2001 | 2719 |
__bp[0] = (__this << 1) | __carry; |
2720 |
__carry = __newCarry; |
|
3119
65247b9d1201
oops - mul2 was wrong for MSB-First machines
Claus Gittinger <cg@exept.de>
parents:
3099
diff
changeset
|
2721 |
__bp++; |
2001 | 2722 |
__nBytes--; |
2723 |
} |
|
2724 |
RETURN (self); |
|
1890 | 2725 |
} |
2726 |
%}. |
|
2727 |
self primitiveFailed |
|
2728 |
||
2729 |
" |
|
2730 |
100000 asLargeInteger mul2 |
|
2731 |
" |
|
2732 |
||
2733 |
"Modified: 5.11.1996 / 16:37:32 / cg" |
|
68 | 2734 |
! |
2735 |
||
2736 |
mul256 |
|
2737 |
"destructively multiply the receiver by 256. |
|
2738 |
private - used for division only" |
|
2739 |
||
2740 |
|newDigits| |
|
2741 |
||
359 | 2742 |
newDigits := ByteArray uninitializedNew:(digitByteArray size + 1). |
68 | 2743 |
newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
2744 |
newDigits at:1 put:0. |
|
2745 |
digitByteArray := newDigits |
|
701 | 2746 |
! |
68 | 2747 |
|
701 | 2748 |
numberOfDigits:nDigits |
2001 | 2749 |
"/ digitByteArray := ByteArray uninitializedNew:nDigits. |
701 | 2750 |
digitByteArray := ByteArray new:nDigits. |
2751 |
sign := 1. |
|
68 | 2752 |
! |
2753 |
||
701 | 2754 |
setDigits:digits |
2755 |
digitByteArray := digits. |
|
2756 |
sign := 1. |
|
68 | 2757 |
! |
2758 |
||
701 | 2759 |
sign:aNumber |
2760 |
sign := aNumber |
|
1 | 2761 |
! ! |
2762 |
||
2763 |
!LargeInteger methodsFor:'testing'! |
|
2764 |
||
701 | 2765 |
even |
2766 |
"return true if the receiver is even" |
|
1 | 2767 |
|
701 | 2768 |
^ (digitByteArray at:1) even |
1 | 2769 |
! |
2770 |
||
68 | 2771 |
negative |
2772 |
"return true, if the receiver is < 0" |
|
2773 |
||
2774 |
^ (sign < 0) |
|
2775 |
! |
|
2776 |
||
1 | 2777 |
odd |
2778 |
"return true if the receiver is odd" |
|
2779 |
||
1983 | 2780 |
^ (digitByteArray at:1) odd |
2781 |
||
2782 |
"Modified: 18.11.1996 / 22:19:19 / cg" |
|
1 | 2783 |
! |
2784 |
||
2785 |
positive |
|
2786 |
"return true, if the receiver is >= 0" |
|
2787 |
||
2788 |
^ (sign >= 0) |
|
2789 |
! |
|
2790 |
||
701 | 2791 |
sign |
2792 |
"return the sign of the receiver" |
|
2793 |
||
2794 |
^ sign |
|
2795 |
! |
|
2796 |
||
1 | 2797 |
strictlyPositive |
2798 |
"return true, if the receiver is > 0" |
|
2799 |
||
2800 |
^ (sign > 0) |
|
2801 |
! ! |
|
2802 |
||
1794 | 2803 |
!LargeInteger class methodsFor:'documentation'! |
1 | 2804 |
|
701 | 2805 |
version |
4157 | 2806 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.78 1999-05-08 16:38:51 cg Exp $' |
1 | 2807 |
! ! |