Type Function IntFittingRange [src]
Returns the smallest integer type that can hold both from and to.
Prototype
pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) type
Parameters
from: comptime_int
to: comptime_int
Example
test IntFittingRange {
try testing.expect(IntFittingRange(0, 0) == u0);
try testing.expect(IntFittingRange(0, 1) == u1);
try testing.expect(IntFittingRange(0, 2) == u2);
try testing.expect(IntFittingRange(0, 3) == u2);
try testing.expect(IntFittingRange(0, 4) == u3);
try testing.expect(IntFittingRange(0, 7) == u3);
try testing.expect(IntFittingRange(0, 8) == u4);
try testing.expect(IntFittingRange(0, 9) == u4);
try testing.expect(IntFittingRange(0, 15) == u4);
try testing.expect(IntFittingRange(0, 16) == u5);
try testing.expect(IntFittingRange(0, 17) == u5);
try testing.expect(IntFittingRange(0, 4095) == u12);
try testing.expect(IntFittingRange(2000, 4095) == u12);
try testing.expect(IntFittingRange(0, 4096) == u13);
try testing.expect(IntFittingRange(2000, 4096) == u13);
try testing.expect(IntFittingRange(0, 4097) == u13);
try testing.expect(IntFittingRange(2000, 4097) == u13);
try testing.expect(IntFittingRange(0, 123456789123456798123456789) == u87);
try testing.expect(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177);
try testing.expect(IntFittingRange(-1, -1) == i1);
try testing.expect(IntFittingRange(-1, 0) == i1);
try testing.expect(IntFittingRange(-1, 1) == i2);
try testing.expect(IntFittingRange(-2, -2) == i2);
try testing.expect(IntFittingRange(-2, -1) == i2);
try testing.expect(IntFittingRange(-2, 0) == i2);
try testing.expect(IntFittingRange(-2, 1) == i2);
try testing.expect(IntFittingRange(-2, 2) == i3);
try testing.expect(IntFittingRange(-1, 2) == i3);
try testing.expect(IntFittingRange(-1, 3) == i3);
try testing.expect(IntFittingRange(-1, 4) == i4);
try testing.expect(IntFittingRange(-1, 7) == i4);
try testing.expect(IntFittingRange(-1, 8) == i5);
try testing.expect(IntFittingRange(-1, 9) == i5);
try testing.expect(IntFittingRange(-1, 15) == i5);
try testing.expect(IntFittingRange(-1, 16) == i6);
try testing.expect(IntFittingRange(-1, 17) == i6);
try testing.expect(IntFittingRange(-1, 4095) == i13);
try testing.expect(IntFittingRange(-4096, 4095) == i13);
try testing.expect(IntFittingRange(-1, 4096) == i14);
try testing.expect(IntFittingRange(-4097, 4095) == i14);
try testing.expect(IntFittingRange(-1, 4097) == i14);
try testing.expect(IntFittingRange(-1, 123456789123456798123456789) == i88);
try testing.expect(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178);
}
Source
pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) type {
assert(from <= to);
if (from == 0 and to == 0) {
return u0;
}
const signedness: std.builtin.Signedness = if (from < 0) .signed else .unsigned;
const largest_positive_integer = @max(if (from < 0) (-from) - 1 else from, to); // two's complement
const base = log2(largest_positive_integer);
const upper = (1 << base) - 1;
var magnitude_bits = if (upper >= largest_positive_integer) base else base + 1;
if (signedness == .signed) {
magnitude_bits += 1;
}
return std.meta.Int(signedness, magnitude_bits);
}