From 811766e1cf0ad2fad73fbb82690969ce430ba1a5 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 24 Oct 2021 17:11:43 +0200 Subject: stage1/stage2: Simplify divTrunc impl According to the documentation, `divTrunc` is "Truncated division. Rounds toward zero". Lower it as a straightforward fdiv + trunc sequence to make it behave as expected with mixed positive/negative operands. Closes #10001 --- src/codegen/llvm.zig | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/codegen/llvm.zig') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index aa0a99ea6c..80f2d8b653 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -2874,13 +2874,8 @@ pub const FuncGen = struct { const inst_ty = self.air.typeOfIndex(inst); if (inst_ty.isRuntimeFloat()) { - const result_llvm_ty = try self.dg.llvmType(inst_ty); - const zero = result_llvm_ty.constNull(); const result = self.builder.buildFDiv(lhs, rhs, ""); - const ceiled = try self.callCeil(result, inst_ty); - const floored = try self.callFloor(result, inst_ty); - const ltz = self.builder.buildFCmp(.OLT, lhs, zero, ""); - return self.builder.buildSelect(ltz, ceiled, floored, ""); + return self.callTrunc(result, inst_ty); } if (inst_ty.isSignedInt()) return self.builder.buildSDiv(lhs, rhs, ""); return self.builder.buildUDiv(lhs, rhs, ""); @@ -3641,6 +3636,10 @@ pub const FuncGen = struct { return self.callFloatUnary(arg, ty, "ceil"); } + fn callTrunc(self: *FuncGen, arg: *const llvm.Value, ty: Type) !*const llvm.Value { + return self.callFloatUnary(arg, ty, "trunc"); + } + fn callFloatUnary(self: *FuncGen, arg: *const llvm.Value, ty: Type, name: []const u8) !*const llvm.Value { const target = self.dg.module.getTarget(); -- cgit v1.2.3