Commit 1704971666

Meghan <hello@nektro.net>
2022-12-15 22:08:51
std: make builtin.Type.{Int,Float}.bits a u16 instead of comptime_int
1 parent 88b49ed
Changed files (3)
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) {
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
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 .{