commit 1704971666bce25add6b4f6e409658c5ce8b59aa (tree)
parent 88b49ed00d6d2efb5b523ab15cbf8ffb37383c56
Author: Meghan <hello@nektro.net>
Date: Thu, 15 Dec 2022 13:08:51 -0800
std: make builtin.Type.{Int,Float}.bits a u16 instead of comptime_int
Diffstat:
3 files changed, 7 insertions(+), 6 deletions(-)
diff --git 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
@@ -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
@@ -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 .{