| ... | ... | @@ -150,16 +150,20 @@ pub const Int = struct { |
| 150 | 150 | return bits; |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | | pub fn fits(self: Int, comptime T: type) bool { |
| 153 | pub fn fitsInTwosComp(self: Int, is_signed: bool, bit_count: usize) bool { |
| 154 | 154 | if (self.eqZero()) { |
| 155 | 155 | return true; |
| 156 | 156 | } |
| 157 | | if (!T.is_signed and !self.positive) { |
| 157 | if (!is_signed and !self.positive) { |
| 158 | 158 | return false; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | | const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and T.is_signed); |
| 162 | | return T.bit_count >= req_bits; |
| 161 | const req_bits = self.bitCountTwosComp() + @boolToInt(self.positive and is_signed); |
| 162 | return bit_count >= req_bits; |
| 163 | } |
| 164 | |
| 165 | pub fn fits(self: Int, comptime T: type) bool { |
| 166 | return self.fitsInTwosComp(T.is_signed, T.bit_count); |
| 163 | 167 | } |
| 164 | 168 | |
| 165 | 169 | // Returns the approximate size of the integer in the given base. Negative values accomodate for |