Skip to content

Commit

Permalink
add std.math.big.Int.fitsInTwosComp
Browse files Browse the repository at this point in the history
so that we can pass runtime-known values
  • Loading branch information
andrewrk committed Jul 22, 2018
1 parent d53fae3 commit 99153ac
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions std/math/big/int.zig
Expand Up @@ -150,16 +150,20 @@ pub const Int = struct {
return bits;
}

pub fn fits(self: Int, comptime T: type) bool {
pub fn fitsInTwosComp(self: Int, is_signed: bool, bit_count: usize) bool {
if (self.eqZero()) {
return true;
}
if (!T.is_signed and !self.positive) {
if (!is_signed and !self.positive) {
return false;
}

const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and T.is_signed);
return T.bit_count >= req_bits;
const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed);
return bit_count >= req_bits;
}

pub fn fits(self: Int, comptime T: type) bool {
return self.fitsInTwosComp(T.is_signed, T.bit_count);
}

// Returns the approximate size of the integer in the given base. Negative values accomodate for
Expand Down

0 comments on commit 99153ac

Please sign in to comment.