From 99153ac0aa390f01091308073b39947c45851ae6 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 22 Jul 2018 10:58:45 -0400 Subject: [PATCH] add std.math.big.Int.fitsInTwosComp so that we can pass runtime-known values --- std/math/big/int.zig | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/std/math/big/int.zig b/std/math/big/int.zig index acd2a6874..09bbfe586 100644 --- a/std/math/big/int.zig +++ b/std/math/big/int.zig @@ -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