authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-24 14:39:27-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-10-24 14:39:27-04:00
logf80fd7e1a6bd3d4f9094301a3815909ce64a3696
tree54e423d7053945cadb7a76a3af46baf64e9f817f
parent94879506ea8fe51310f38b3db1bc1ea1e71a4389
parent2a733051bb5da245ac377f8771a482a85fb88519
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10022 from LemonBoy/fix-10001

stage1/stage2: Simplify divTrunc impl

5 files changed, 25 insertions(+), 33 deletions(-)

lib/std/math/trunc.zig+4
......@@ -21,6 +21,10 @@ pub fn trunc(x: anytype) @TypeOf(x) {
2121 f32 => trunc32(x),
2222 f64 => trunc64(x),
2323 f128 => trunc128(x),
24
25 // TODO this is not correct for some targets
26 c_longdouble => @floatCast(c_longdouble, trunc128(x)),
27
2428 else => @compileError("trunc not implemented for " ++ @typeName(T)),
2529 };
2630}
lib/std/special/c_stage1.zig+7
......@@ -763,6 +763,13 @@ export fn truncf(a: f32) f32 {
763763 return math.trunc(a);
764764}
765765
766export fn truncl(a: c_longdouble) c_longdouble {
767 if (!long_double_is_f128) {
768 @panic("TODO implement this");
769 }
770 return math.trunc(a);
771}
772
766773export fn round(a: f64) f64 {
767774 return math.round(a);
768775}
src/codegen/llvm.zig+5-6
......@@ -2874,13 +2874,8 @@ pub const FuncGen = struct {
28742874 const inst_ty = self.air.typeOfIndex(inst);
28752875
28762876 if (inst_ty.isRuntimeFloat()) {
2877 const result_llvm_ty = try self.dg.llvmType(inst_ty);
2878 const zero = result_llvm_ty.constNull();
28792877 const result = self.builder.buildFDiv(lhs, rhs, "");
2880 const ceiled = try self.callCeil(result, inst_ty);
2881 const floored = try self.callFloor(result, inst_ty);
2882 const ltz = self.builder.buildFCmp(.OLT, lhs, zero, "");
2883 return self.builder.buildSelect(ltz, ceiled, floored, "");
2878 return self.callTrunc(result, inst_ty);
28842879 }
28852880 if (inst_ty.isSignedInt()) return self.builder.buildSDiv(lhs, rhs, "");
28862881 return self.builder.buildUDiv(lhs, rhs, "");
......@@ -3641,6 +3636,10 @@ pub const FuncGen = struct {
36413636 return self.callFloatUnary(arg, ty, "ceil");
36423637 }
36433638
3639 fn callTrunc(self: *FuncGen, arg: *const llvm.Value, ty: Type) !*const llvm.Value {
3640 return self.callFloatUnary(arg, ty, "trunc");
3641 }
3642
36443643 fn callFloatUnary(self: *FuncGen, arg: *const llvm.Value, ty: Type, name: []const u8) !*const llvm.Value {
36453644 const target = self.dg.module.getTarget();
36463645
src/stage1/codegen.cpp+1-27
......@@ -2964,33 +2964,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast
29642964 }
29652965 return result;
29662966 case DivKindTrunc:
2967 {
2968 LLVMBasicBlockRef ltz_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivTruncLTZero");
2969 LLVMBasicBlockRef gez_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivTruncGEZero");
2970 LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivTruncEnd");
2971 LLVMValueRef ltz = LLVMBuildFCmp(g->builder, LLVMRealOLT, val1, zero, "");
2972 if (operand_type->id == ZigTypeIdVector) {
2973 ltz = ZigLLVMBuildOrReduce(g->builder, ltz);
2974 }
2975 LLVMBuildCondBr(g->builder, ltz, ltz_block, gez_block);
2976
2977 LLVMPositionBuilderAtEnd(g->builder, ltz_block);
2978 LLVMValueRef ceiled = gen_float_op(g, result, operand_type, BuiltinFnIdCeil);
2979 LLVMBasicBlockRef ceiled_end_block = LLVMGetInsertBlock(g->builder);
2980 LLVMBuildBr(g->builder, end_block);
2981
2982 LLVMPositionBuilderAtEnd(g->builder, gez_block);
2983 LLVMValueRef floored = gen_float_op(g, result, operand_type, BuiltinFnIdFloor);
2984 LLVMBasicBlockRef floored_end_block = LLVMGetInsertBlock(g->builder);
2985 LLVMBuildBr(g->builder, end_block);
2986
2987 LLVMPositionBuilderAtEnd(g->builder, end_block);
2988 LLVMValueRef phi = LLVMBuildPhi(g->builder, get_llvm_type(g, operand_type), "");
2989 LLVMValueRef incoming_values[] = { ceiled, floored };
2990 LLVMBasicBlockRef incoming_blocks[] = { ceiled_end_block, floored_end_block };
2991 LLVMAddIncoming(phi, incoming_values, incoming_blocks, 2);
2992 return phi;
2993 }
2967 return gen_float_op(g, result, operand_type, BuiltinFnIdTrunc);
29942968 case DivKindFloor:
29952969 return gen_float_op(g, result, operand_type, BuiltinFnIdFloor);
29962970 }
test/behavior/math.zig+8
......@@ -282,12 +282,20 @@ fn testDivision() !void {
282282
283283 try expect(divTrunc(i32, 5, 3) == 1);
284284 try expect(divTrunc(i32, -5, 3) == -1);
285 try expect(divTrunc(i32, 9, -10) == 0);
286 try expect(divTrunc(i32, -9, 10) == 0);
285287 try expect(divTrunc(f16, 5.0, 3.0) == 1.0);
286288 try expect(divTrunc(f16, -5.0, 3.0) == -1.0);
289 try expect(divTrunc(f16, 9.0, -10.0) == 0.0);
290 try expect(divTrunc(f16, -9.0, 10.0) == 0.0);
287291 try expect(divTrunc(f32, 5.0, 3.0) == 1.0);
288292 try expect(divTrunc(f32, -5.0, 3.0) == -1.0);
293 try expect(divTrunc(f32, 9.0, -10.0) == 0.0);
294 try expect(divTrunc(f32, -9.0, 10.0) == 0.0);
289295 try expect(divTrunc(f64, 5.0, 3.0) == 1.0);
290296 try expect(divTrunc(f64, -5.0, 3.0) == -1.0);
297 try expect(divTrunc(f64, 9.0, -10.0) == 0.0);
298 try expect(divTrunc(f64, -9.0, 10.0) == 0.0);
291299 try expect(divTrunc(i32, 10, 12) == 0);
292300 try expect(divTrunc(i32, -14, 12) == -1);
293301 try expect(divTrunc(i32, -2, 12) == 0);