OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Original code by Matt McCutchen, see the LICENSE file |
| 6 |
1 #ifndef BIGINTEGER_H | 7 #ifndef BIGINTEGER_H |
2 #define BIGINTEGER_H | 8 #define BIGINTEGER_H |
3 | 9 |
4 #include "BigUnsigned.hh" | 10 #include "BigUnsigned.hh" |
5 | 11 |
6 /* A BigInteger object represents a signed integer of size limited only by | 12 /* A BigInteger object represents a signed integer of size limited only by |
7 * available memory. BigUnsigneds support most mathematical operators and can | 13 * available memory. BigUnsigneds support most mathematical operators and can |
8 * be converted to and from most primitive integer types. | 14 * be converted to and from most primitive integer types. |
9 * | 15 * |
10 * A BigInteger is just an aggregate of a BigUnsigned and a sign. (It is no | 16 * A BigInteger is just an aggregate of a BigUnsigned and a sign. (It is no |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 BigInteger ans; | 156 BigInteger ans; |
151 ans.subtract(*this, x); | 157 ans.subtract(*this, x); |
152 return ans; | 158 return ans; |
153 } | 159 } |
154 inline BigInteger BigInteger::operator *(const BigInteger &x) const { | 160 inline BigInteger BigInteger::operator *(const BigInteger &x) const { |
155 BigInteger ans; | 161 BigInteger ans; |
156 ans.multiply(*this, x); | 162 ans.multiply(*this, x); |
157 return ans; | 163 return ans; |
158 } | 164 } |
159 inline BigInteger BigInteger::operator /(const BigInteger &x) const { | 165 inline BigInteger BigInteger::operator /(const BigInteger &x) const { |
160 » if (x.isZero()) throw "BigInteger::operator /: division by zero"; | 166 » if (x.isZero()) |
| 167 #ifdef FOXIT_CHROME_BUILD |
| 168 abort(); |
| 169 #else |
| 170 throw "BigInteger::operator /: division by zero"; |
| 171 #endif |
161 BigInteger q, r; | 172 BigInteger q, r; |
162 r = *this; | 173 r = *this; |
163 r.divideWithRemainder(x, q); | 174 r.divideWithRemainder(x, q); |
164 return q; | 175 return q; |
165 } | 176 } |
166 inline BigInteger BigInteger::operator %(const BigInteger &x) const { | 177 inline BigInteger BigInteger::operator %(const BigInteger &x) const { |
167 » if (x.isZero()) throw "BigInteger::operator %: division by zero"; | 178 » if (x.isZero()) |
| 179 #ifdef FOXIT_CHROME_BUILD |
| 180 abort(); |
| 181 #else |
| 182 throw "BigInteger::operator %: division by zero"; |
| 183 #endif |
168 BigInteger q, r; | 184 BigInteger q, r; |
169 r = *this; | 185 r = *this; |
170 r.divideWithRemainder(x, q); | 186 r.divideWithRemainder(x, q); |
171 return r; | 187 return r; |
172 } | 188 } |
173 inline BigInteger BigInteger::operator -() const { | 189 inline BigInteger BigInteger::operator -() const { |
174 BigInteger ans; | 190 BigInteger ans; |
175 ans.negate(*this); | 191 ans.negate(*this); |
176 return ans; | 192 return ans; |
177 } | 193 } |
178 | 194 |
179 /* | 195 /* |
180 * ASSIGNMENT OPERATORS | 196 * ASSIGNMENT OPERATORS |
181 * | 197 * |
182 * Now the responsibility for making a temporary copy if necessary | 198 * Now the responsibility for making a temporary copy if necessary |
183 * belongs to the put-here operations. See Assignment Operators in | 199 * belongs to the put-here operations. See Assignment Operators in |
184 * BigUnsigned.hh. | 200 * BigUnsigned.hh. |
185 */ | 201 */ |
186 inline void BigInteger::operator +=(const BigInteger &x) { | 202 inline void BigInteger::operator +=(const BigInteger &x) { |
187 add(*this, x); | 203 add(*this, x); |
188 } | 204 } |
189 inline void BigInteger::operator -=(const BigInteger &x) { | 205 inline void BigInteger::operator -=(const BigInteger &x) { |
190 subtract(*this, x); | 206 subtract(*this, x); |
191 } | 207 } |
192 inline void BigInteger::operator *=(const BigInteger &x) { | 208 inline void BigInteger::operator *=(const BigInteger &x) { |
193 multiply(*this, x); | 209 multiply(*this, x); |
194 } | 210 } |
195 inline void BigInteger::operator /=(const BigInteger &x) { | 211 inline void BigInteger::operator /=(const BigInteger &x) { |
196 » if (x.isZero()) throw "BigInteger::operator /=: division by zero"; | 212 » if (x.isZero()) |
| 213 #ifdef FOXIT_CHROME_BUILD |
| 214 abort(); |
| 215 #else |
| 216 throw "BigInteger::operator /=: division by zero"; |
| 217 #endif |
197 /* The following technique is slightly faster than copying *this first | 218 /* The following technique is slightly faster than copying *this first |
198 * when x is large. */ | 219 * when x is large. */ |
199 BigInteger q; | 220 BigInteger q; |
200 divideWithRemainder(x, q); | 221 divideWithRemainder(x, q); |
201 // *this contains the remainder, but we overwrite it with the quotient. | 222 // *this contains the remainder, but we overwrite it with the quotient. |
202 *this = q; | 223 *this = q; |
203 } | 224 } |
204 inline void BigInteger::operator %=(const BigInteger &x) { | 225 inline void BigInteger::operator %=(const BigInteger &x) { |
205 » if (x.isZero()) throw "BigInteger::operator %=: division by zero"; | 226 » if (x.isZero()) |
| 227 #ifdef FOXIT_CHROME_BUILD |
| 228 abort(); |
| 229 #else |
| 230 throw "BigInteger::operator %=: division by zero"; |
| 231 #endif |
206 BigInteger q; | 232 BigInteger q; |
207 // Mods *this by x. Don't care about quotient left in q. | 233 // Mods *this by x. Don't care about quotient left in q. |
208 divideWithRemainder(x, q); | 234 divideWithRemainder(x, q); |
209 } | 235 } |
210 // This one is trivial | 236 // This one is trivial |
211 inline void BigInteger::flipSign() { | 237 inline void BigInteger::flipSign() { |
212 sign = Sign(-sign); | 238 sign = Sign(-sign); |
213 } | 239 } |
214 | 240 |
215 #endif | 241 #endif |
OLD | NEW |