authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-28 15:59:04+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-29 14:55:43+03:00
log1ea1228036b6d3424c46a3ce66f4b7cbf461fc21
tree9d1982e59158cca3ce32797c8c72923b95e295d6
parent61f5ea4c9adc96dbdabca533f77d475233089b1c

Sema: fix floatToInt to zero bit ints

Closes #9415

2 files changed, 15 insertions(+), 0 deletions(-)

src/Sema.zig+7
...@@ -18668,6 +18668,13 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!...@@ -18668,6 +18668,13 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
18668 }18668 }
1866918669
18670 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);18670 try sema.requireRuntimeBlock(block, inst_data.src(), operand_src);
18671 if (dest_ty.intInfo(sema.mod.getTarget()).bits == 0) {
18672 if (block.wantSafety()) {
18673 const ok = try block.addBinOp(if (block.float_mode == .Optimized) .cmp_eq_optimized else .cmp_eq, operand, try sema.addConstant(operand_ty, Value.zero));
18674 try sema.addSafetyCheck(block, ok, .integer_part_out_of_bounds);
18675 }
18676 return sema.addConstant(dest_ty, Value.zero);
18677 }
18671 const result = try block.addTyOp(if (block.float_mode == .Optimized) .float_to_int_optimized else .float_to_int, dest_ty, operand);18678 const result = try block.addTyOp(if (block.float_mode == .Optimized) .float_to_int_optimized else .float_to_int, dest_ty, operand);
18672 if (block.wantSafety()) {18679 if (block.wantSafety()) {
18673 const back = try block.addTyOp(.int_to_float, operand_ty, result);18680 const back = try block.addTyOp(.int_to_float, operand_ty, result);
test/behavior/cast.zig+8
...@@ -1451,3 +1451,11 @@ test "peer type resolution of const and non-const pointer to array" {...@@ -1451,3 +1451,11 @@ test "peer type resolution of const and non-const pointer to array" {
1451 try std.testing.expect(@TypeOf(a, b) == *const [1024]u8);1451 try std.testing.expect(@TypeOf(a, b) == *const [1024]u8);
1452 try std.testing.expect(a == b);1452 try std.testing.expect(a == b);
1453}1453}
1454
1455test "floatToInt to zero-bit int" {
1456 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1457 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1458
1459 var a: f32 = 0.0;
1460 comptime try std.testing.expect(@floatToInt(u0, a) == 0);
1461}