- case positive:
- if (len == 1 && (blk[0] & ~lMask) == 0)
- return long(blk[0]);
- else
- throw "BigInteger operator long() const: Value is too big for a long";
- case negative:
- if (len == 1 && (blk[0] & ~lMask) == 0)
- return -long(blk[0]);
- else
- throw "BigInteger operator long() const: Value is too big for a long";
- default:
- throw "BigInteger: Internal error";
- }
-}
-
-BigInteger::operator unsigned int() const {
- switch (sign) {
- case zero:
- return 0;
- case positive:
- if (len == 1 && (blk[0] & ~uiMask) == 0)
- return (unsigned int)(blk[0]);
- else
- throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int";
- case negative:
- throw "BigInteger operator unsigned int() const: Cannot convert a negative integer to an unsigned type";
- default:
- throw "BigInteger: Internal error";
- }
-}
-
-BigInteger::operator int() const {
- switch (sign) {
- case zero:
- return 0;
- case positive:
- if (len == 1 && (blk[0] & ~iMask) == 0)
- return int(blk[0]);
- else
- throw "BigInteger operator int() const: Value is too big for an int";
- case negative:
- if (len == 1 && (blk[0] & ~iMask) == 0)
- return -int(blk[0]);
- else
- throw "BigInteger operator int() const: Value is too big for an int";
- default:
- throw "BigInteger: Internal error";
- }
-}
-
-BigInteger::operator unsigned short() const {
- switch (sign) {
- case zero:
- return 0;
- case positive:
- if (len == 1 && (blk[0] & ~usMask) == 0)
- return (unsigned short)(blk[0]);
- else
- throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short";
- case negative:
- throw "BigInteger operator unsigned short() const: Cannot convert a negative integer to an unsigned type";
- default:
- throw "BigInteger: Internal error";
+ else if (mag.getLength() == 1) {
+ // The single block might fit in an X. Try the conversion.
+ Blk b = mag.getBlock(0);
+ if (sign == positive) {
+ X x = X(b);
+ if (x >= 0 && Blk(x) == b)
+ return x;
+ } else {
+ X x = -X(b);
+ /* UX(...) needed to avoid rejecting conversion of
+ * -2^15 to a short. */
+ if (x < 0 && Blk(UX(-x)) == b)
+ return x;
+ }
+ // Otherwise fall through.