diff options
| author | LemonBoy <thatlemon@gmail.com> | 2021-10-24 17:11:43 +0200 |
|---|---|---|
| committer | LemonBoy <thatlemon@gmail.com> | 2021-10-24 17:11:43 +0200 |
| commit | 811766e1cf0ad2fad73fbb82690969ce430ba1a5 (patch) | |
| tree | 5f550e0115e5dcc9f6178be03c0dc8af63e86d58 /src/codegen/llvm.zig | |
| parent | 94879506ea8fe51310f38b3db1bc1ea1e71a4389 (diff) | |
| download | zig-811766e1cf0ad2fad73fbb82690969ce430ba1a5.tar.gz zig-811766e1cf0ad2fad73fbb82690969ce430ba1a5.zip | |
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
Diffstat (limited to 'src/codegen/llvm.zig')
| -rw-r--r-- | src/codegen/llvm.zig | 11 |
1 files changed, 5 insertions, 6 deletions
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(); |
