authorgravatar for adria.arrufat@gmail.comAdrià Arrufat <adria.arrufat@gmail.com> 2026-03-20 21:27:38+09:00
committergravatar for adria.arrufat@gmail.comAdrià Arrufat <adria.arrufat@gmail.com> 2026-03-20 21:27:38+09:00
logc10cb4470172f9c5db2a503af7608b2f784780dd
treeac04e14a28fadda3a52d66a85a1d17a1c78e8c7a
parentfeba59acf35fcb7dd6005b67b518872aa1039e83

Zir: unify float rounding instructions into round_op


5 files changed, 38 insertions(+), 36 deletions(-)

lib/std/zig/AstGen.zig+6-6
......@@ -9795,14 +9795,14 @@ fn floatRoundOp(
97959795 const operand = try expr(gz, scope, .{ .rl = .{ .coerced_ty = operand_ty_inst } }, operand_node);
97969796
97979797 try emitDbgStmt(gz, cursor);
9798 const cast_tag: Zir.Inst.Extended = switch (float_tag) {
9799 .round => .round_cast,
9800 .floor => .floor_cast,
9801 .ceil => .ceil_cast,
9802 .trunc => .trunc_cast,
9798 const round_op: Zir.Inst.RoundOp = switch (float_tag) {
9799 .round => .round,
9800 .floor => .floor,
9801 .ceil => .ceil,
9802 .trunc => .trunc,
98039803 else => unreachable,
98049804 };
9805 const result = try gz.addExtendedPayload(cast_tag, Zir.Inst.BinNode{
9805 const result = try gz.addExtendedPayloadSmall(.round_op, @intFromEnum(round_op), Zir.Inst.BinNode{
98069806 .node = gz.nodeIndexToRelative(node),
98079807 .lhs = dest_type,
98089808 .rhs = operand,
lib/std/zig/Zir.zig+12-20
......@@ -2009,22 +2009,10 @@ pub const Inst = struct {
20092009 /// `operand` is payload index to `BinNode`.
20102010 /// `small` is unused.
20112011 shl_with_overflow,
2012 /// Explicit rounding cast.
2013 /// `operand` is payload index to `Bin`.
2014 /// `small` is unused.
2015 round_cast,
2016 /// Explicit floor cast.
2017 /// `operand` is payload index to `Bin`.
2018 /// `small` is unused.
2019 floor_cast,
2020 /// Explicit ceil cast.
2021 /// `operand` is payload index to `Bin`.
2022 /// `small` is unused.
2023 ceil_cast,
2024 /// Explicit trunc cast.
2025 /// `operand` is payload index to `Bin`.
2026 /// `small` is unused.
2027 trunc_cast,
2012 /// `@round`, `@floor`, `@ceil`, or `@trunc`, with a result type.
2013 /// `operand` is payload index to `BinNode`.
2014 /// `small` is a `RoundOp` representing the specific operation being performed.
2015 round_op,
20282016 /// Returns the type for the operand of a rounding op.
20292017 /// `operand` is `UnNode`.
20302018 /// `small` is unused.
......@@ -3253,6 +3241,13 @@ pub const Inst = struct {
32533241 string_to_union_field_attrs,
32543242 };
32553243
3244 pub const RoundOp = enum(u16) {
3245 round,
3246 floor,
3247 ceil,
3248 trunc,
3249 };
3250
32563251 pub const UnNode = struct {
32573252 node: Ast.Node.Offset,
32583253 operand: Ref,
......@@ -4364,10 +4359,7 @@ fn findTrackableInner(
43644359 .sub_with_overflow,
43654360 .mul_with_overflow,
43664361 .shl_with_overflow,
4367 .round_cast,
4368 .floor_cast,
4369 .ceil_cast,
4370 .trunc_cast,
4362 .round_op,
43714363 .c_undef,
43724364 .c_include,
43734365 .c_define,
src/Sema.zig+9-5
......@@ -1405,10 +1405,7 @@ fn analyzeBodyInner(
14051405 .@"asm" => try sema.zirAsm( block, extended, false),
14061406 .asm_expr => try sema.zirAsm( block, extended, true),
14071407 .typeof_peer => try sema.zirTypeofPeer( block, extended, inst),
1408 .round_cast => try sema.zirRoundCast( block, extended, .round),
1409 .floor_cast => try sema.zirRoundCast( block, extended, .floor),
1410 .ceil_cast => try sema.zirRoundCast( block, extended, .ceil),
1411 .trunc_cast => try sema.zirRoundCast( block, extended, .truncate),
1408 .round_op => try sema.zirRoundCast( block, extended),
14121409 .round_op_ty => try sema.zirRoundOpType( block, extended),
14131410 .compile_log => try sema.zirCompileLog( block, extended),
14141411 .min_multi => try sema.zirMinMaxMulti( block, extended, .min),
......@@ -20854,7 +20851,6 @@ fn zirRoundCast(
2085420851 sema: *Sema,
2085520852 block: *Block,
2085620853 extended: Zir.Inst.Extended.InstData,
20857 mode: IntFromFloatMode,
2085820854) CompileError!Air.Inst.Ref {
2085920855 const pt = sema.pt;
2086020856 const zcu = pt.zcu;
......@@ -20864,6 +20860,14 @@ fn zirRoundCast(
2086420860
2086520861 const operand = sema.resolveInst(extra.rhs);
2086620862
20863 const round_op: Zir.Inst.RoundOp = @enumFromInt(extended.small);
20864 const mode: IntFromFloatMode = switch (round_op) {
20865 .round => .round,
20866 .floor => .floor,
20867 .ceil => .ceil,
20868 .trunc => .truncate,
20869 };
20870
2086720871 const dest_ty = (try sema.resolveTypeOrPoison(block, src, extra.lhs) orelse switch (mode) {
2086820872 // zig fmt: off
2086920873 .round => return sema.unaryMath(block, operand_src, operand, .round, Value.round),
src/print_zir.zig+11-4
......@@ -585,10 +585,6 @@ const Writer = struct {
585585 .prefetch,
586586 .c_va_arg,
587587 .reify_enum_value_slice_ty,
588 .round_cast,
589 .floor_cast,
590 .ceil_cast,
591 .trunc_cast,
592588 => {
593589 const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
594590 try self.writeInstRef(stream, inst_data.lhs);
......@@ -598,6 +594,17 @@ const Writer = struct {
598594 try self.writeSrcNode(stream, inst_data.node);
599595 },
600596
597 .round_op => {
598 const round_op: Zir.Inst.RoundOp = @enumFromInt(extended.small);
599 const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
600 try stream.print("{s}, ", .{@tagName(round_op)});
601 try self.writeInstRef(stream, inst_data.lhs);
602 try stream.writeAll(", ");
603 try self.writeInstRef(stream, inst_data.rhs);
604 try stream.writeAll(")) ");
605 try self.writeSrcNode(stream, inst_data.node);
606 },
607
601608 .reify_slice_arg_ty => {
602609 const reify_slice_arg_info: Zir.Inst.ReifySliceArgInfo = @enumFromInt(extended.small);
603610 const extra = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
test/behavior/cast.zig-1
......@@ -1361,7 +1361,6 @@ test "comptime float casts" {
13611361 try expectIntFromFloat(comptime_int, 1234, i16, 1234);
13621362 try expectIntFromFloat(comptime_float, 12.3, comptime_int, 12);
13631363
1364 try expectRoundCast(comptime_int, 1234, i16, 1234);
13651364 try expectRoundCast(comptime_float, 12.3, comptime_int, 12);
13661365
13671366 try expectFloorCast(comptime_float, 12.3, comptime_int, 12);