Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make @boolToInt always return a u1 #15701

Merged
merged 14 commits into from
May 24, 2023
Merged
4 changes: 0 additions & 4 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -7850,10 +7850,6 @@ comptime {
Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
{#syntax#}@as(u1, 0){#endsyntax#}.
</p>
<p>
If the value is known at compile-time, the return type is {#syntax#}comptime_int{#endsyntax#}
instead of {#syntax#}u1{#endsyntax#}.
</p>
{#header_close#}

{#header_open|@bitSizeOf#}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ pub fn break_f80(x: f80) F80 {
pub inline fn sign(i: anytype) @TypeOf(i) {
const T = @TypeOf(i);
return switch (@typeInfo(T)) {
.Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @boolToInt(i < 0),
.Int, .ComptimeInt => @as(T, @boolToInt(i > 0)) - @as(T, @boolToInt(i < 0)),
.Float, .ComptimeFloat => @intToFloat(T, @boolToInt(i > 0)) - @intToFloat(T, @boolToInt(i < 0)),
.Vector => |vinfo| blk: {
switch (@typeInfo(vinfo.child)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/math/ilogb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn ilogbX(comptime T: type, x: T) i32 {
}

// offset sign bit, exponent bits, and integer bit (if present) + bias
const offset = 1 + exponentBits + @boolToInt(T == f80) - exponentBias;
const offset = 1 + exponentBits + @as(comptime_int, @boolToInt(T == f80)) - exponentBias;
return offset - @intCast(i32, @clz(u));
}

Expand Down
6 changes: 3 additions & 3 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18445,9 +18445,9 @@ fn zirBoolToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
const inst_data = sema.code.instructions.items(.data)[inst].un_node;
const operand = try sema.resolveInst(inst_data.operand);
if (try sema.resolveMaybeUndefVal(operand)) |val| {
if (val.isUndef()) return sema.addConstUndef(Type.initTag(.u1));
const bool_ints = [2]Air.Inst.Ref{ .zero, .one };
return bool_ints[@boolToInt(val.toBool())];
if (val.isUndef()) return sema.addConstUndef(Type.u1);
if (val.toBool()) return sema.addConstant(Type.u1, Value.one);
return sema.addConstant(Type.u1, Value.zero);
}
return block.addUnOp(.bool_to_int, operand);
}
Expand Down
1 change: 1 addition & 0 deletions src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,7 @@ fn transCCast(

if (isBoolRes(src_int_expr)) {
src_int_expr = try Tag.bool_to_int.create(c.arena, src_int_expr);
return Tag.as.create(c.arena, .{ .lhs = dst_node, .rhs = src_int_expr });
}

switch (cIntTypeCmp(dst_type, src_type)) {
Expand Down
17 changes: 13 additions & 4 deletions test/behavior/bool.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;

test "bool literals" {
try expect(true);
Expand All @@ -12,14 +13,22 @@ test "cast bool to int" {

const t = true;
const f = false;
try expect(@boolToInt(t) == @as(u32, 1));
try expect(@boolToInt(f) == @as(u32, 0));
try expectEqual(@as(u32, 1), @boolToInt(t));
try expectEqual(@as(u32, 0), @boolToInt(f));
try expectEqual(-1, @bitCast(i1, @boolToInt(t)));
try expectEqual(0, @bitCast(i1, @boolToInt(f)));
try expectEqual(u1, @TypeOf(@boolToInt(t)));
try expectEqual(u1, @TypeOf(@boolToInt(f)));
try nonConstCastBoolToInt(t, f);
}

fn nonConstCastBoolToInt(t: bool, f: bool) !void {
try expect(@boolToInt(t) == @as(u32, 1));
try expect(@boolToInt(f) == @as(u32, 0));
try expectEqual(@as(u32, 1), @boolToInt(t));
try expectEqual(@as(u32, 0), @boolToInt(f));
try expectEqual(@as(i1, -1), @bitCast(i1, @boolToInt(t)));
try expectEqual(@as(i1, 0), @bitCast(i1, @boolToInt(f)));
try expectEqual(u1, @TypeOf(@boolToInt(t)));
try expectEqual(u1, @TypeOf(@boolToInt(f)));
}

test "bool cmp" {
Expand Down