authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-12-15 13:08:51-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-12-15 23:08:51+02:00
log1704971666bce25add6b4f6e409658c5ce8b59aa
tree2a31b6d2dd10c83e79129c38ba33863b54d77d53
parent88b49ed00d6d2efb5b523ab15cbf8ffb37383c56
signature Signed by PGP key 4AEE18F83AFDEB23

std: make builtin.Type.{Int,Float}.bits a u16 instead of comptime_int


3 files changed, 7 insertions(+), 6 deletions(-)

lib/std/builtin.zig+2-4
...@@ -220,15 +220,13 @@ pub const Type = union(enum) {...@@ -220,15 +220,13 @@ pub const Type = union(enum) {
220 /// therefore must be kept in sync with the compiler implementation.220 /// therefore must be kept in sync with the compiler implementation.
221 pub const Int = struct {221 pub const Int = struct {
222 signedness: Signedness,222 signedness: Signedness,
223 /// TODO make this u16 instead of comptime_int223 bits: u16,
224 bits: comptime_int,
225 };224 };
226225
227 /// This data structure is used by the Zig language code generation and226 /// This data structure is used by the Zig language code generation and
228 /// therefore must be kept in sync with the compiler implementation.227 /// therefore must be kept in sync with the compiler implementation.
229 pub const Float = struct {228 pub const Float = struct {
230 /// TODO make this u16 instead of comptime_int229 bits: u16,
231 bits: comptime_int,
232 };230 };
233231
234 /// This data structure is used by the Zig language code generation and232 /// This data structure is used by the Zig language code generation and
lib/std/math/sqrt.zig+4-1
...@@ -37,9 +37,12 @@ fn sqrt_int(comptime T: type, value: T) Sqrt(T) {...@@ -37,9 +37,12 @@ fn sqrt_int(comptime T: type, value: T) Sqrt(T) {
37 if (@typeInfo(T).Int.bits <= 2) {37 if (@typeInfo(T).Int.bits <= 2) {
38 return if (value == 0) 0 else 1; // shortcut for small number of bits to simplify general case38 return if (value == 0) 0 else 1; // shortcut for small number of bits to simplify general case
39 } else {39 } 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
40 var op = value;43 var op = value;
41 var res: T = 0;44 var res: T = 0;
42 var one: T = 1 << ((@typeInfo(T).Int.bits - 1) & -2); // highest power of four that fits into T45 var one: T = 1 << ((bits - 1) & minustwo); // highest power of four that fits into T
4346
44 // "one" starts at the highest power of four <= than the argument.47 // "one" starts at the highest power of four <= than the argument.
45 while (one > op) {48 while (one > op) {
src/type.zig+1-1
...@@ -4586,7 +4586,7 @@ pub const Type = extern union {...@@ -4586,7 +4586,7 @@ pub const Type = extern union {
4586 }4586 }
45874587
4588 /// Asserts the type is an integer, enum, error set, or vector of one of them.4588 /// 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 {
4590 var ty = self;4590 var ty = self;
4591 while (true) switch (ty.tag()) {4591 while (true) switch (ty.tag()) {
4592 .int_unsigned => return .{4592 .int_unsigned => return .{