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(
1962319623 return null;
1962419624}
1962519625
19626fn zirUnaryMath(
19626fn unaryMath(
1962719627 sema: *Sema,
1962819628 block: *Block,
19629 inst: Zir.Inst.Index,
19629 operand_src: LazySrcLoc,
19630 operand: Air.Inst.Ref,
1963019631 air_tag: Air.Inst.Tag,
1963119632 comptime eval: fn (Value, Type, Allocator, Zcu.PerThread) Allocator.Error!Value,
1963219633) CompileError!Air.Inst.Ref {
19633 const tracy = trace(@src());
19634 defer tracy.end();
19635
1963619634 const pt = sema.pt;
1963719635 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);
1964119636 const operand_ty = sema.typeOf(operand);
1964219637 const scalar_ty = operand_ty.scalarType(zcu);
1964319638
......@@ -19657,6 +19652,23 @@ fn zirUnaryMath(
1965719652 };
1965819653}
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
1966019672fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1966119673 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
1966219674 const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
......@@ -20850,36 +20862,33 @@ fn zirRoundCast(
2085020862 const src = block.nodeOffset(extra.node);
2085120863 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
2086420865 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
2086520877 const operand_ty = sema.typeOf(operand);
2086620878
20867 const is_poison = dest_ty.isGenericPoison();
20868 if (!is_poison) {
20869 try sema.checkVectorizableBinaryOperands(block, operand_src, dest_ty, operand_ty, src, operand_src);
20870 }
20871 const dest_scalar_ty = if (is_poison) dest_ty else dest_ty.scalarType(zcu);
20879 try sema.checkVectorizableBinaryOperands(block, operand_src, dest_ty, operand_ty, src, operand_src);
20880
20881 const dest_scalar_ty = dest_ty.scalarType(zcu);
2087220882 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) {
2087520885 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
2087820887 const result_ref = switch (mode) {
20879 .round => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.round),
20880 .floor => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.floor),
20881 .ceil => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.ceil),
20882 .truncate => try sema.maybeConstantUnaryMath(coerced_operand, math_ty, Value.trunc),
20888 .round => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.round),
20889 .floor => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.floor),
20890 .ceil => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.ceil),
20891 .truncate => try sema.maybeConstantUnaryMath(coerced_operand, dest_ty, Value.trunc),
2088320892 else => unreachable,
2088420893 };
2088520894