Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen

In particular I wanted the change that makes `suspend;` illegal in the
parser.
This commit is contained in:
Andrew Kelley
2021-04-24 10:44:41 -07:00
73 changed files with 1160 additions and 468 deletions

View File

@@ -38,7 +38,13 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {
}
}
fn sqrt_int(comptime T: type, value: T) std.meta.Int(.unsigned, @typeInfo(T).Int.bits / 2) {
fn sqrt_int(comptime T: type, value: T) Sqrt(T) {
switch (T) {
u0 => return 0,
u1 => return value,
else => {},
}
var op = value;
var res: T = 0;
var one: T = 1 << (@typeInfo(T).Int.bits - 2);
@@ -57,11 +63,13 @@ fn sqrt_int(comptime T: type, value: T) std.meta.Int(.unsigned, @typeInfo(T).Int
one >>= 2;
}
const ResultType = std.meta.Int(.unsigned, @typeInfo(T).Int.bits / 2);
const ResultType = Sqrt(T);
return @intCast(ResultType, res);
}
test "math.sqrt_int" {
expect(sqrt_int(u0, 0) == 0);
expect(sqrt_int(u1, 1) == 1);
expect(sqrt_int(u32, 3) == 1);
expect(sqrt_int(u32, 4) == 2);
expect(sqrt_int(u32, 5) == 2);
@@ -73,7 +81,13 @@ test "math.sqrt_int" {
/// Returns the return type `sqrt` will return given an operand of type `T`.
pub fn Sqrt(comptime T: type) type {
return switch (@typeInfo(T)) {
.Int => |int| std.meta.Int(.unsigned, int.bits / 2),
.Int => |int| {
return switch (int.bits) {
0 => u0,
1 => u1,
else => std.meta.Int(.unsigned, int.bits / 2),
};
},
else => T,
};
}