| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code by Matt McCutchen, see the LICENSE file. | 5 // Original code by Matt McCutchen, see the LICENSE file. |
| 6 | 6 |
| 7 #ifndef BIGUNSIGNED_H | 7 #ifndef BIGUNSIGNED_H |
| 8 #define BIGUNSIGNED_H | 8 #define BIGUNSIGNED_H |
| 9 | 9 |
| 10 #include "NumberlikeArray.hh" | 10 #include "NumberlikeArray.hh" |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 ans.subtract(*this, x); | 260 ans.subtract(*this, x); |
| 261 return ans; | 261 return ans; |
| 262 } | 262 } |
| 263 inline BigUnsigned BigUnsigned::operator *(const BigUnsigned &x) const { | 263 inline BigUnsigned BigUnsigned::operator *(const BigUnsigned &x) const { |
| 264 BigUnsigned ans; | 264 BigUnsigned ans; |
| 265 ans.multiply(*this, x); | 265 ans.multiply(*this, x); |
| 266 return ans; | 266 return ans; |
| 267 } | 267 } |
| 268 inline BigUnsigned BigUnsigned::operator /(const BigUnsigned &x) const { | 268 inline BigUnsigned BigUnsigned::operator /(const BigUnsigned &x) const { |
| 269 if (x.isZero()) | 269 if (x.isZero()) |
| 270 #ifdef FOXIT_CHROME_BUILD | |
| 271 abort(); | 270 abort(); |
| 272 #else | |
| 273 throw "BigUnsigned::operator /: division by zero"; | |
| 274 #endif | |
| 275 BigUnsigned q, r; | 271 BigUnsigned q, r; |
| 276 r = *this; | 272 r = *this; |
| 277 r.divideWithRemainder(x, q); | 273 r.divideWithRemainder(x, q); |
| 278 return q; | 274 return q; |
| 279 } | 275 } |
| 280 inline BigUnsigned BigUnsigned::operator %(const BigUnsigned &x) const { | 276 inline BigUnsigned BigUnsigned::operator %(const BigUnsigned &x) const { |
| 281 if (x.isZero()) | 277 if (x.isZero()) |
| 282 #ifdef FOXIT_CHROME_BUILD | |
| 283 abort(); | 278 abort(); |
| 284 #else | |
| 285 throw "BigUnsigned::operator %: division by zero"; | |
| 286 #endif | |
| 287 BigUnsigned q, r; | 279 BigUnsigned q, r; |
| 288 r = *this; | 280 r = *this; |
| 289 r.divideWithRemainder(x, q); | 281 r.divideWithRemainder(x, q); |
| 290 return r; | 282 return r; |
| 291 } | 283 } |
| 292 inline BigUnsigned BigUnsigned::operator &(const BigUnsigned &x) const { | 284 inline BigUnsigned BigUnsigned::operator &(const BigUnsigned &x) const { |
| 293 BigUnsigned ans; | 285 BigUnsigned ans; |
| 294 ans.bitAnd(*this, x); | 286 ans.bitAnd(*this, x); |
| 295 return ans; | 287 return ans; |
| 296 } | 288 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 319 add(*this, x); | 311 add(*this, x); |
| 320 } | 312 } |
| 321 inline void BigUnsigned::operator -=(const BigUnsigned &x) { | 313 inline void BigUnsigned::operator -=(const BigUnsigned &x) { |
| 322 subtract(*this, x); | 314 subtract(*this, x); |
| 323 } | 315 } |
| 324 inline void BigUnsigned::operator *=(const BigUnsigned &x) { | 316 inline void BigUnsigned::operator *=(const BigUnsigned &x) { |
| 325 multiply(*this, x); | 317 multiply(*this, x); |
| 326 } | 318 } |
| 327 inline void BigUnsigned::operator /=(const BigUnsigned &x) { | 319 inline void BigUnsigned::operator /=(const BigUnsigned &x) { |
| 328 if (x.isZero()) | 320 if (x.isZero()) |
| 329 #ifdef FOXIT_CHROME_BUILD | |
| 330 abort(); | 321 abort(); |
| 331 #else | |
| 332 throw "BigUnsigned::operator /=: division by zero"; | |
| 333 #endif | |
| 334 /* The following technique is slightly faster than copying *this first | 322 /* The following technique is slightly faster than copying *this first |
| 335 * when x is large. */ | 323 * when x is large. */ |
| 336 BigUnsigned q; | 324 BigUnsigned q; |
| 337 divideWithRemainder(x, q); | 325 divideWithRemainder(x, q); |
| 338 // *this contains the remainder, but we overwrite it with the quotient. | 326 // *this contains the remainder, but we overwrite it with the quotient. |
| 339 *this = q; | 327 *this = q; |
| 340 } | 328 } |
| 341 inline void BigUnsigned::operator %=(const BigUnsigned &x) { | 329 inline void BigUnsigned::operator %=(const BigUnsigned &x) { |
| 342 if (x.isZero()) | 330 if (x.isZero()) |
| 343 #ifdef FOXIT_CHROME_BUILD | |
| 344 abort(); | 331 abort(); |
| 345 #else | |
| 346 throw "BigUnsigned::operator %=: division by zero"; | |
| 347 #endif | |
| 348 BigUnsigned q; | 332 BigUnsigned q; |
| 349 // Mods *this by x. Don't care about quotient left in q. | 333 // Mods *this by x. Don't care about quotient left in q. |
| 350 divideWithRemainder(x, q); | 334 divideWithRemainder(x, q); |
| 351 } | 335 } |
| 352 inline void BigUnsigned::operator &=(const BigUnsigned &x) { | 336 inline void BigUnsigned::operator &=(const BigUnsigned &x) { |
| 353 bitAnd(*this, x); | 337 bitAnd(*this, x); |
| 354 } | 338 } |
| 355 inline void BigUnsigned::operator |=(const BigUnsigned &x) { | 339 inline void BigUnsigned::operator |=(const BigUnsigned &x) { |
| 356 bitOr(*this, x); | 340 bitOr(*this, x); |
| 357 } | 341 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 } | 375 } |
| 392 } | 376 } |
| 393 | 377 |
| 394 /* Ditto, but first check that x is nonnegative. I could have put the check in | 378 /* Ditto, but first check that x is nonnegative. I could have put the check in |
| 395 * initFromPrimitive and let the compiler optimize it out for unsigned-type | 379 * initFromPrimitive and let the compiler optimize it out for unsigned-type |
| 396 * instantiations, but I wanted to avoid the warning stupidly issued by g++ for | 380 * instantiations, but I wanted to avoid the warning stupidly issued by g++ for |
| 397 * a condition that is constant in *any* instantiation, even if not in all. */ | 381 * a condition that is constant in *any* instantiation, even if not in all. */ |
| 398 template <class X> | 382 template <class X> |
| 399 void BigUnsigned::initFromSignedPrimitive(X x) { | 383 void BigUnsigned::initFromSignedPrimitive(X x) { |
| 400 if (x < 0) | 384 if (x < 0) |
| 401 #ifdef FOXIT_CHROME_BUILD | |
| 402 abort(); | 385 abort(); |
| 403 #else | |
| 404 throw "BigUnsigned constructor: " | |
| 405 "Cannot construct a BigUnsigned from a negative number"; | |
| 406 #endif | |
| 407 else | 386 else |
| 408 initFromPrimitive(x); | 387 initFromPrimitive(x); |
| 409 } | 388 } |
| 410 | 389 |
| 411 // CONVERSION TO PRIMITIVE INTEGERS | 390 // CONVERSION TO PRIMITIVE INTEGERS |
| 412 | 391 |
| 413 /* Template with the same idea as initFromPrimitive. This might be slightly | 392 /* Template with the same idea as initFromPrimitive. This might be slightly |
| 414 * slower than the previous version with the masks, but it's much shorter and | 393 * slower than the previous version with the masks, but it's much shorter and |
| 415 * clearer, which is the library's stated goal. */ | 394 * clearer, which is the library's stated goal. */ |
| 416 template <class X> | 395 template <class X> |
| 417 X BigUnsigned::convertToPrimitive() const { | 396 X BigUnsigned::convertToPrimitive() const { |
| 418 if (len == 0) | 397 if (len == 0) |
| 419 // The number is zero; return zero. | 398 // The number is zero; return zero. |
| 420 return 0; | 399 return 0; |
| 421 else if (len == 1) { | 400 else if (len == 1) { |
| 422 // The single block might fit in an X. Try the conversion. | 401 // The single block might fit in an X. Try the conversion. |
| 423 X x = X(blk[0]); | 402 X x = X(blk[0]); |
| 424 // Make sure the result accurately represents the block. | 403 // Make sure the result accurately represents the block. |
| 425 if (Blk(x) == blk[0]) | 404 if (Blk(x) == blk[0]) |
| 426 // Successful conversion. | 405 // Successful conversion. |
| 427 return x; | 406 return x; |
| 428 // Otherwise fall through. | 407 // Otherwise fall through. |
| 429 } | 408 } |
| 430 #ifdef FOXIT_CHROME_BUILD | |
| 431 abort(); | 409 abort(); |
| 432 #else | |
| 433 throw "BigUnsigned::to<Primitive>: " | |
| 434 "Value is too big to fit in the requested type"; | |
| 435 #endif | |
| 436 } | 410 } |
| 437 | 411 |
| 438 /* Wrap the above in an x >= 0 test to make sure we got a nonnegative result, | 412 /* Wrap the above in an x >= 0 test to make sure we got a nonnegative result, |
| 439 * not a negative one that happened to convert back into the correct nonnegative | 413 * not a negative one that happened to convert back into the correct nonnegative |
| 440 * one. (E.g., catch incorrect conversion of 2^31 to the long -2^31.) Again, | 414 * one. (E.g., catch incorrect conversion of 2^31 to the long -2^31.) Again, |
| 441 * separated to avoid a g++ warning. */ | 415 * separated to avoid a g++ warning. */ |
| 442 template <class X> | 416 template <class X> |
| 443 X BigUnsigned::convertToSignedPrimitive() const { | 417 X BigUnsigned::convertToSignedPrimitive() const { |
| 444 X x = convertToPrimitive<X>(); | 418 X x = convertToPrimitive<X>(); |
| 445 if (x >= 0) | 419 if (x >= 0) |
| 446 return x; | 420 return x; |
| 447 else | 421 else |
| 448 #ifdef FOXIT_CHROME_BUILD | |
| 449 abort(); | 422 abort(); |
| 450 #else | |
| 451 throw "BigUnsigned::to(Primitive): " | |
| 452 "Value is too big to fit in the requested type"; | |
| 453 #endif | |
| 454 } | 423 } |
| 455 | 424 |
| 456 #endif | 425 #endif |
| OLD | NEW |