Skip to content

Commit 1704971

Browse files
authored
std: make builtin.Type.{Int,Float}.bits a u16 instead of comptime_int
1 parent 88b49ed commit 1704971

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

lib/std/builtin.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,13 @@ pub const Type = union(enum) {
220220
/// therefore must be kept in sync with the compiler implementation.
221221
pub const Int = struct {
222222
signedness: Signedness,
223-
/// TODO make this u16 instead of comptime_int
224-
bits: comptime_int,
223+
bits: u16,
225224
};
226225

227226
/// This data structure is used by the Zig language code generation and
228227
/// therefore must be kept in sync with the compiler implementation.
229228
pub const Float = struct {
230-
/// TODO make this u16 instead of comptime_int
231-
bits: comptime_int,
229+
bits: u16,
232230
};
233231

234232
/// This data structure is used by the Zig language code generation and

lib/std/math/sqrt.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ fn sqrt_int(comptime T: type, value: T) Sqrt(T) {
3737
if (@typeInfo(T).Int.bits <= 2) {
3838
return if (value == 0) 0 else 1; // shortcut for small number of bits to simplify general case
3939
} else {
40+
const bits = @typeInfo(T).Int.bits;
41+
const max = math.maxInt(T);
42+
const minustwo = (@as(T, 2) ^ max) + 1; // unsigned int cannot represent -2
4043
var op = value;
4144
var res: T = 0;
42-
var one: T = 1 << ((@typeInfo(T).Int.bits - 1) & -2); // highest power of four that fits into T
45+
var one: T = 1 << ((bits - 1) & minustwo); // highest power of four that fits into T
4346

4447
// "one" starts at the highest power of four <= than the argument.
4548
while (one > op) {

src/type.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4586,7 +4586,7 @@ pub const Type = extern union {
45864586
}
45874587

45884588
/// Asserts the type is an integer, enum, error set, or vector of one of them.
4589-
pub fn intInfo(self: Type, target: Target) struct { signedness: std.builtin.Signedness, bits: u16 } {
4589+
pub fn intInfo(self: Type, target: Target) std.builtin.Type.Int {
45904590
var ty = self;
45914591
while (true) switch (ty.tag()) {
45924592
.int_unsigned => return .{

0 commit comments

Comments
 (0)