diff --git a/lib/std/zig/AstGen.zig b/lib/std/zig/AstGen.zig index d8d214e1ac104b707fa78518102e0156ad23970a..77a9d222106c9e5d111f16902fc8feca008acd63 100644 --- a/lib/std/zig/AstGen.zig +++ b/lib/std/zig/AstGen.zig @@ -9795,14 +9795,14 @@ fn floatRoundOp( const operand = try expr(gz, scope, .{ .rl = .{ .coerced_ty = operand_ty_inst } }, operand_node); try emitDbgStmt(gz, cursor); - const cast_tag: Zir.Inst.Extended = switch (float_tag) { - .round => .round_cast, - .floor => .floor_cast, - .ceil => .ceil_cast, - .trunc => .trunc_cast, + const round_op: Zir.Inst.RoundOp = switch (float_tag) { + .round => .round, + .floor => .floor, + .ceil => .ceil, + .trunc => .trunc, else => unreachable, }; - const result = try gz.addExtendedPayload(cast_tag, Zir.Inst.BinNode{ + const result = try gz.addExtendedPayloadSmall(.round_op, @intFromEnum(round_op), Zir.Inst.BinNode{ .node = gz.nodeIndexToRelative(node), .lhs = dest_type, .rhs = operand, diff --git a/lib/std/zig/Zir.zig b/lib/std/zig/Zir.zig index e3cccdd6026481be88e111ac8069ff6c85d6dd5c..bcded94045019e06b82c86778829d2d8687296d3 100644 --- a/lib/std/zig/Zir.zig +++ b/lib/std/zig/Zir.zig @@ -2009,22 +2009,10 @@ pub const Inst = struct { /// `operand` is payload index to `BinNode`. /// `small` is unused. shl_with_overflow, - /// Explicit rounding cast. - /// `operand` is payload index to `Bin`. - /// `small` is unused. - round_cast, - /// Explicit floor cast. - /// `operand` is payload index to `Bin`. - /// `small` is unused. - floor_cast, - /// Explicit ceil cast. - /// `operand` is payload index to `Bin`. - /// `small` is unused. - ceil_cast, - /// Explicit trunc cast. - /// `operand` is payload index to `Bin`. - /// `small` is unused. - trunc_cast, + /// `@round`, `@floor`, `@ceil`, or `@trunc`, with a result type. + /// `operand` is payload index to `BinNode`. + /// `small` is a `RoundOp` representing the specific operation being performed. + round_op, /// Returns the type for the operand of a rounding op. /// `operand` is `UnNode`. /// `small` is unused. @@ -3253,6 +3241,13 @@ pub const Inst = struct { string_to_union_field_attrs, }; + pub const RoundOp = enum(u16) { + round, + floor, + ceil, + trunc, + }; + pub const UnNode = struct { node: Ast.Node.Offset, operand: Ref, @@ -4364,10 +4359,7 @@ fn findTrackableInner( .sub_with_overflow, .mul_with_overflow, .shl_with_overflow, - .round_cast, - .floor_cast, - .ceil_cast, - .trunc_cast, + .round_op, .c_undef, .c_include, .c_define, diff --git a/src/Sema.zig b/src/Sema.zig index 200af6eac9a28b4c31ee3aa98e0f7ed30da52537..81eb4539d5e3e53055071562dba412bb4e39312e 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1405,10 +1405,7 @@ fn analyzeBodyInner( .@"asm" => try sema.zirAsm( block, extended, false), .asm_expr => try sema.zirAsm( block, extended, true), .typeof_peer => try sema.zirTypeofPeer( block, extended, inst), - .round_cast => try sema.zirRoundCast( block, extended, .round), - .floor_cast => try sema.zirRoundCast( block, extended, .floor), - .ceil_cast => try sema.zirRoundCast( block, extended, .ceil), - .trunc_cast => try sema.zirRoundCast( block, extended, .truncate), + .round_op => try sema.zirRoundCast( block, extended), .round_op_ty => try sema.zirRoundOpType( block, extended), .compile_log => try sema.zirCompileLog( block, extended), .min_multi => try sema.zirMinMaxMulti( block, extended, .min), @@ -20854,7 +20851,6 @@ fn zirRoundCast( sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData, - mode: IntFromFloatMode, ) CompileError!Air.Inst.Ref { const pt = sema.pt; const zcu = pt.zcu; @@ -20864,6 +20860,14 @@ fn zirRoundCast( const operand = sema.resolveInst(extra.rhs); + const round_op: Zir.Inst.RoundOp = @enumFromInt(extended.small); + const mode: IntFromFloatMode = switch (round_op) { + .round => .round, + .floor => .floor, + .ceil => .ceil, + .trunc => .truncate, + }; + const dest_ty = (try sema.resolveTypeOrPoison(block, src, extra.lhs) orelse switch (mode) { // zig fmt: off .round => return sema.unaryMath(block, operand_src, operand, .round, Value.round), diff --git a/src/print_zir.zig b/src/print_zir.zig index d4ace18fb255ba37e20c7fa336da96aee7db4af1..c6b8f60173feee98c393e8d9ea6b678640bbbda3 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -585,10 +585,6 @@ const Writer = struct { .prefetch, .c_va_arg, .reify_enum_value_slice_ty, - .round_cast, - .floor_cast, - .ceil_cast, - .trunc_cast, => { const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data; try self.writeInstRef(stream, inst_data.lhs); @@ -598,6 +594,17 @@ const Writer = struct { try self.writeSrcNode(stream, inst_data.node); }, + .round_op => { + const round_op: Zir.Inst.RoundOp = @enumFromInt(extended.small); + const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data; + try stream.print("{s}, ", .{@tagName(round_op)}); + try self.writeInstRef(stream, inst_data.lhs); + try stream.writeAll(", "); + try self.writeInstRef(stream, inst_data.rhs); + try stream.writeAll(")) "); + try self.writeSrcNode(stream, inst_data.node); + }, + .reify_slice_arg_ty => { const reify_slice_arg_info: Zir.Inst.ReifySliceArgInfo = @enumFromInt(extended.small); const extra = self.code.extraData(Zir.Inst.UnNode, extended.operand).data; diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 04c5ce384850d18fa406eacdefb03b5f3d1ad381..79170037d6d93d878bbdd369aaa3ee2a7b5fb776 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -1361,7 +1361,6 @@ test "comptime float casts" { try expectIntFromFloat(comptime_int, 1234, i16, 1234); try expectIntFromFloat(comptime_float, 12.3, comptime_int, 12); - try expectRoundCast(comptime_int, 1234, i16, 1234); try expectRoundCast(comptime_float, 12.3, comptime_int, 12); try expectFloorCast(comptime_float, 12.3, comptime_int, 12);