make @boolToInt always return a u1

Signed-off-by: tison <wander4096@gmail.com>
This commit is contained in:
tison 2023-05-24 08:01:48 +08:00 committed by GitHub
parent dcc1b4fd15
commit bfe02ff61a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 13 deletions

View File

@ -7850,10 +7850,6 @@ comptime {
Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to Converts {#syntax#}true{#endsyntax#} to {#syntax#}@as(u1, 1){#endsyntax#} and {#syntax#}false{#endsyntax#} to
{#syntax#}@as(u1, 0){#endsyntax#}. {#syntax#}@as(u1, 0){#endsyntax#}.
</p> </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_close#}
{#header_open|@bitSizeOf#} {#header_open|@bitSizeOf#}

View File

@ -1684,7 +1684,7 @@ pub fn break_f80(x: f80) F80 {
pub inline fn sign(i: anytype) @TypeOf(i) { pub inline fn sign(i: anytype) @TypeOf(i) {
const T = @TypeOf(i); const T = @TypeOf(i);
return switch (@typeInfo(T)) { 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)), .Float, .ComptimeFloat => @intToFloat(T, @boolToInt(i > 0)) - @intToFloat(T, @boolToInt(i < 0)),
.Vector => |vinfo| blk: { .Vector => |vinfo| blk: {
switch (@typeInfo(vinfo.child)) { switch (@typeInfo(vinfo.child)) {

View File

@ -48,7 +48,7 @@ fn ilogbX(comptime T: type, x: T) i32 {
} }
// offset sign bit, exponent bits, and integer bit (if present) + bias // 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)); return offset - @intCast(i32, @clz(u));
} }

View File

@ -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 inst_data = sema.code.instructions.items(.data)[inst].un_node;
const operand = try sema.resolveInst(inst_data.operand); const operand = try sema.resolveInst(inst_data.operand);
if (try sema.resolveMaybeUndefVal(operand)) |val| { if (try sema.resolveMaybeUndefVal(operand)) |val| {
if (val.isUndef()) return sema.addConstUndef(Type.initTag(.u1)); if (val.isUndef()) return sema.addConstUndef(Type.u1);
const bool_ints = [2]Air.Inst.Ref{ .zero, .one }; if (val.toBool()) return sema.addConstant(Type.u1, Value.one);
return bool_ints[@boolToInt(val.toBool())]; return sema.addConstant(Type.u1, Value.zero);
} }
return block.addUnOp(.bool_to_int, operand); return block.addUnOp(.bool_to_int, operand);
} }

View File

@ -2494,6 +2494,7 @@ fn transCCast(
if (isBoolRes(src_int_expr)) { if (isBoolRes(src_int_expr)) {
src_int_expr = try Tag.bool_to_int.create(c.arena, 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)) { switch (cIntTypeCmp(dst_type, src_type)) {

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const expect = std.testing.expect; const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
test "bool literals" { test "bool literals" {
try expect(true); try expect(true);
@ -12,14 +13,22 @@ test "cast bool to int" {
const t = true; const t = true;
const f = false; const f = false;
try expect(@boolToInt(t) == @as(u32, 1)); try expectEqual(@as(u32, 1), @boolToInt(t));
try expect(@boolToInt(f) == @as(u32, 0)); 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); try nonConstCastBoolToInt(t, f);
} }
fn nonConstCastBoolToInt(t: bool, f: bool) !void { fn nonConstCastBoolToInt(t: bool, f: bool) !void {
try expect(@boolToInt(t) == @as(u32, 1)); try expectEqual(@as(u32, 1), @boolToInt(t));
try expect(@boolToInt(f) == @as(u32, 0)); 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" { test "bool cmp" {