From 1704971666bce25add6b4f6e409658c5ce8b59aa Mon Sep 17 00:00:00 2001 From: Meghan Date: Thu, 15 Dec 2022 13:08:51 -0800 Subject: [PATCH] std: make builtin.Type.{Int,Float}.bits a u16 instead of comptime_int --- lib/std/builtin.zig | 6 ++---- lib/std/math/sqrt.zig | 5 ++++- src/type.zig | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index bf3c3e1ee3..2d56b477f9 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -220,15 +220,13 @@ pub const Type = union(enum) { /// therefore must be kept in sync with the compiler implementation. pub const Int = struct { signedness: Signedness, - /// TODO make this u16 instead of comptime_int - bits: comptime_int, + bits: u16, }; /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. pub const Float = struct { - /// TODO make this u16 instead of comptime_int - bits: comptime_int, + bits: u16, }; /// This data structure is used by the Zig language code generation and diff --git a/lib/std/math/sqrt.zig b/lib/std/math/sqrt.zig index 871cc58e47..e642f8a309 100644 --- a/lib/std/math/sqrt.zig +++ b/lib/std/math/sqrt.zig @@ -37,9 +37,12 @@ fn sqrt_int(comptime T: type, value: T) Sqrt(T) { if (@typeInfo(T).Int.bits <= 2) { return if (value == 0) 0 else 1; // shortcut for small number of bits to simplify general case } else { + const bits = @typeInfo(T).Int.bits; + const max = math.maxInt(T); + const minustwo = (@as(T, 2) ^ max) + 1; // unsigned int cannot represent -2 var op = value; var res: T = 0; - var one: T = 1 << ((@typeInfo(T).Int.bits - 1) & -2); // highest power of four that fits into T + var one: T = 1 << ((bits - 1) & minustwo); // highest power of four that fits into T // "one" starts at the highest power of four <= than the argument. while (one > op) { diff --git a/src/type.zig b/src/type.zig index 5e6f032798..53aee5051a 100644 --- a/src/type.zig +++ b/src/type.zig @@ -4586,7 +4586,7 @@ pub const Type = extern union { } /// Asserts the type is an integer, enum, error set, or vector of one of them. - pub fn intInfo(self: Type, target: Target) struct { signedness: std.builtin.Signedness, bits: u16 } { + pub fn intInfo(self: Type, target: Target) std.builtin.Type.Int { var ty = self; while (true) switch (ty.tag()) { .int_unsigned => return .{