authorgravatar for adria.arrufat@gmail.comAdrià Arrufat <adria.arrufat@gmail.com> 2026-03-20 15:10:23+09:00
committergravatar for adria.arrufat@gmail.comAdrià Arrufat <adria.arrufat@gmail.com> 2026-03-20 15:10:23+09:00
log7b27dc60f3d251d418ff80131d43730c6afe5964
tree1bc9a644da5670fd9f8d84bd81e52c75b3725759
parenta25f94ddb26d218af2ecbf7cdf943ac755a355a6

Sema: extract unaryMath and simplify rounding cast


1 files changed, 39 insertions(+), 30 deletions(-)

src/Sema.zig+39-30
...@@ -19623,21 +19623,16 @@ fn maybeConstantUnaryMath(...@@ -19623,21 +19623,16 @@ fn maybeConstantUnaryMath(
19623 return null;19623 return null;
19624}19624}
1962519625
19626fn zirUnaryMath(19626fn unaryMath(
19627 sema: *Sema,19627 sema: *Sema,
19628 block: *Block,19628 block: *Block,
19629 inst: Zir.Inst.Index,19629 operand_src: LazySrcLoc,
19630 operand: Air.Inst.Ref,
19630 air_tag: Air.Inst.Tag,19631 air_tag: Air.Inst.Tag,
19631 comptime eval: fn (Value, Type, Allocator, Zcu.PerThread) Allocator.Error!Value,19632 comptime eval: fn (Value, Type, Allocator, Zcu.PerThread) Allocator.Error!Value,
19632) CompileError!Air.Inst.Ref {19633) CompileError!Air.Inst.Ref {
19633 const tracy = trace(@src());
19634 defer tracy.end();
19635
19636 const pt = sema.pt;19634 const pt = sema.pt;
19637 const zcu = pt.zcu;19635 const zcu = pt.zcu;
19638 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
19639 const operand = sema.resolveInst(inst_data.operand);
19640 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
19641 const operand_ty = sema.typeOf(operand);19636 const operand_ty = sema.typeOf(operand);
19642 const scalar_ty = operand_ty.scalarType(zcu);19637 const scalar_ty = operand_ty.scalarType(zcu);
1964319638
...@@ -19657,6 +19652,23 @@ fn zirUnaryMath(...@@ -19657,6 +19652,23 @@ fn zirUnaryMath(
19657 };19652 };
19658}19653}
1965919654
19655fn zirUnaryMath(
19656 sema: *Sema,
19657 block: *Block,
19658 inst: Zir.Inst.Index,
19659 air_tag: Air.Inst.Tag,
19660 comptime eval: fn (Value, Type, Allocator, Zcu.PerThread) Allocator.Error!Value,
19661) CompileError!Air.Inst.Ref {
19662 const tracy = trace(@src());
19663 defer tracy.end();
19664
19665 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
19666 const operand = sema.resolveInst(inst_data.operand);
19667 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
19668
19669 return sema.unaryMath(block, operand_src, operand, air_tag, eval);
19670}
19671
19660fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {19672fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
19661 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;19673 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
19662 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);19674 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
...@@ -20850,36 +20862,33 @@ fn zirRoundCast(...@@ -20850,36 +20862,33 @@ fn zirRoundCast(
20850 const src = block.nodeOffset(extra.node);20862 const src = block.nodeOffset(extra.node);
20851 const operand_src = block.builtinCallArgSrc(extra.node, 0);20863 const operand_src = block.builtinCallArgSrc(extra.node, 0);
2085220864
20853 const dest_ty_or_poison = try sema.resolveTypeOrPoison(block, src, extra.lhs) orelse Type.generic_poison;
20854 var dest_ty = dest_ty_or_poison;
20855 if (!dest_ty.isGenericPoison()) {
20856 if (dest_ty.zigTypeTag(zcu) == .error_union) {
20857 dest_ty = dest_ty.errorUnionPayload(zcu);
20858 }
20859 if (dest_ty.zigTypeTag(zcu) == .optional) {
20860 dest_ty = dest_ty.childType(zcu);
20861 }
20862 }
20863
20864 const operand = sema.resolveInst(extra.rhs);20865 const operand = sema.resolveInst(extra.rhs);
20866
20867 const dest_ty = (try sema.resolveTypeOrPoison(block, src, extra.lhs) orelse switch (mode) {
20868 // zig fmt: off
20869 .round => return sema.unaryMath(block, operand_src, operand, .round, Value.round),
20870 .floor => return sema.unaryMath(block, operand_src, operand, .floor, Value.floor),
20871 .ceil => return sema.unaryMath(block, operand_src, operand, .ceil, Value.ceil),
20872 .truncate => return sema.unaryMath(block, operand_src, operand, .trunc_float, Value.trunc),
20873 // zig fmt: on
20874 .exact => unreachable,
20875 }).optEuBaseType(zcu);
20876
20865 const operand_ty = sema.typeOf(operand);20877 const operand_ty = sema.typeOf(operand);
2086620878
20867 const is_poison = dest_ty.isGenericPoison();20879 try sema.checkVectorizableBinaryOperands(block, operand_src, dest_ty, operand_ty, src, operand_src);
20868 if (!is_poison) {20880
20869 try sema.checkVectorizableBinaryOperands(block, operand_src, dest_ty, operand_ty, src, operand_src);20881 const dest_scalar_ty = dest_ty.scalarType(zcu);
20870 }
20871 const dest_scalar_ty = if (is_poison) dest_ty else dest_ty.scalarType(zcu);
20872 const operand_scalar_ty = operand_ty.scalarType(zcu);20882 const operand_scalar_ty = operand_ty.scalarType(zcu);
2087320883
20874 if (is_poison or dest_scalar_ty.zigTypeTag(zcu) == .float or dest_scalar_ty.zigTypeTag(zcu) == .comptime_float) {20884 if (dest_scalar_ty.zigTypeTag(zcu) == .float or dest_scalar_ty.zigTypeTag(zcu) == .comptime_float) {
20875 const coerced_operand = try sema.coerce(block, dest_ty, operand, operand_src);20885 const coerced_operand = try sema.coerce(block, dest_ty, operand, operand_src);
20876 const math_ty = if (is_poison) operand_ty else dest_ty;
2087720886
20878 const result_ref = switch (mode) {20887 const result_ref = switch (mode) {
20879 .round => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.round),20888 .round => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.round),
20880 .floor => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.floor),20889 .floor => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.floor),
20881 .ceil => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.ceil),20890 .ceil => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.ceil),
20882 .truncate => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.trunc),20891 .truncate => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.trunc),
20883 else => unreachable,20892 else => unreachable,
20884 };20893 };
2088520894