diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-01-17 10:24:27 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-17 10:24:27 -0500 |
| commit | a4e8e55908eb406f4713c22a6721d6d73f6951a5 (patch) | |
| tree | d89e36d6b195dcd3862f2bc3e1f16a86384e0f1e /src/bigint.cpp | |
| parent | 2e6125bc66bd717838fb107107564cdd66112613 (diff) | |
| parent | 1d6f54cc7d6a00f3ebcc96b5265c845223c64e2e (diff) | |
| download | zig-a4e8e55908eb406f4713c22a6721d6d73f6951a5.tar.gz zig-a4e8e55908eb406f4713c22a6721d6d73f6951a5.zip | |
Merge pull request #701 from Hejsil/fix-xor-with-zero
Fixed bigint_xor for none negative numbers
Diffstat (limited to 'src/bigint.cpp')
| -rw-r--r-- | src/bigint.cpp | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/bigint.cpp b/src/bigint.cpp index a68dd3a4b8..85e5dad4ad 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -1271,6 +1271,12 @@ void bigint_and(BigInt *dest, const BigInt *op1, const BigInt *op2) { } void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) { + if (op1->digit_count == 0) { + return bigint_init_bigint(dest, op2); + } + if (op2->digit_count == 0) { + return bigint_init_bigint(dest, op1); + } if (op1->is_negative || op2->is_negative) { // TODO this code path is untested size_t big_bit_count = max(bigint_bits_needed(op1), bigint_bits_needed(op2)); @@ -1289,14 +1295,16 @@ void bigint_xor(BigInt *dest, const BigInt *op1, const BigInt *op2) { dest->is_negative = false; const uint64_t *op1_digits = bigint_ptr(op1); const uint64_t *op2_digits = bigint_ptr(op2); + + assert(op1->digit_count > 0 && op2->digit_count > 0); + uint64_t first_digit = op1_digits[0] ^ op2_digits[0]; if (op1->digit_count == 1 && op2->digit_count == 1) { dest->digit_count = 1; - dest->data.digit = op1_digits[0] ^ op2_digits[0]; + dest->data.digit = first_digit; bigint_normalize(dest); return; } // TODO this code path is untested - uint64_t first_digit = dest->data.digit; dest->digit_count = max(op1->digit_count, op2->digit_count); dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count); dest->data.digits[0] = first_digit; |
