aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-10-04 12:21:31 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-10-04 12:23:49 -0700
commitac2333ee63b3c0af8a075d325e2313c9b255a46e (patch)
tree8e5d07a4aaa946c1c681428adafde533d58f5c59 /src/type.zig
parenta28f2e0dd2d7b78464115bca4968f7c0befa6b28 (diff)
downloadzig-ac2333ee63b3c0af8a075d325e2313c9b255a46e.tar.gz
zig-ac2333ee63b3c0af8a075d325e2313c9b255a46e.zip
stage2: fix Type max/min int calculation
This was an attempt to move saturating_arithmetic.zig to the "passing for stage2" section, which did not pan out due to the discovery of 2 prerequisite items that need to be done, but I did make a bug fix along the way of the calculation of max/min integers. This commit also simplifies the saturating arithmetic behavior tests to depend on less of the zig language that is not related to saturating arithmetic.
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/type.zig b/src/type.zig
index fdaa445041..317d2dacbe 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -3096,7 +3096,7 @@ pub const Type = extern union {
return Value.initTag(.zero);
}
- if ((info.bits - 1) <= std.math.maxInt(u6)) {
+ if (info.bits <= 6) {
const n: i64 = -(@as(i64, 1) << @truncate(u6, info.bits - 1));
return Value.Tag.int_i64.create(arena, n);
}
@@ -3117,13 +3117,16 @@ pub const Type = extern union {
assert(self.zigTypeTag() == .Int);
const info = self.intInfo(target);
- if (info.signedness == .signed and (info.bits - 1) <= std.math.maxInt(u6)) {
- const n: i64 = (@as(i64, 1) << @truncate(u6, info.bits - 1)) - 1;
- return Value.Tag.int_i64.create(arena, n);
- } else if (info.signedness == .signed and info.bits <= std.math.maxInt(u6)) {
- const n: u64 = (@as(u64, 1) << @truncate(u6, info.bits)) - 1;
- return Value.Tag.int_u64.create(arena, n);
- }
+ if (info.bits <= 6) switch (info.signedness) {
+ .signed => {
+ const n: i64 = (@as(i64, 1) << @truncate(u6, info.bits - 1)) - 1;
+ return Value.Tag.int_i64.create(arena, n);
+ },
+ .unsigned => {
+ const n: u64 = (@as(u64, 1) << @truncate(u6, info.bits)) - 1;
+ return Value.Tag.int_u64.create(arena, n);
+ },
+ };
var res = try std.math.big.int.Managed.init(arena);
try res.setTwosCompIntLimit(.max, info.signedness, info.bits);