From 0e8242b905514f838f36757f2f890241e7448554 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Tue, 26 Apr 2022 15:16:37 -0700 Subject: stage1: Manually lower softfloat ops when needed Updates stage1 to manually lower softfloat operations for all unary floating point operations, extension/truncation, and arithmetic. --- src/stage1/codegen.cpp | 704 +++++++++++++++++++++++++++---------------------- 1 file changed, 382 insertions(+), 322 deletions(-) (limited to 'src/stage1/codegen.cpp') diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index a2efed6bde..88e73baa3c 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -869,7 +869,7 @@ static LLVMValueRef get_float_fn(CodeGen *g, ZigType *type_entry, ZigLLVMFnId fn name = "fma"; num_args = 3; } else if (fn_id == ZigLLVMFnIdFloatOp) { - name = float_op_to_name(op); + name = float_un_op_to_name(op); num_args = 1; } else { zig_unreachable(); @@ -1604,8 +1604,49 @@ static LLVMValueRef gen_assert_zero(CodeGen *g, LLVMValueRef expr_val, ZigType * return nullptr; } +static const char *get_compiler_rt_type_abbrev(ZigType *type) { + uint16_t bits; + if (type->id == ZigTypeIdFloat) { + bits = type->data.floating.bit_count; + } else if (type->id == ZigTypeIdInt) { + bits = type->data.integral.bit_count; + } else { + zig_unreachable(); + } + switch (bits) { + case 16: + return "h"; + case 32: + return "s"; + case 64: + return "d"; + case 80: + return "x"; + case 128: + return "t"; + default: + zig_unreachable(); + } +} + +static const char *get_math_h_type_abbrev(CodeGen *g, ZigType *float_type) { + if (float_type == g->builtin_types.entry_f16) + return "h"; // Non-standard + else if (float_type == g->builtin_types.entry_f32) + return "s"; + else if (float_type == g->builtin_types.entry_f64) + return ""; + else if (float_type == g->builtin_types.entry_f80) + return "x"; // Non-standard + else if (float_type == g->builtin_types.entry_c_longdouble) + return "l"; + else if (float_type == g->builtin_types.entry_f128) + return "q"; // Non-standard + else + zig_unreachable(); +} -static LLVMValueRef gen_soft_f80_widen_or_shorten(CodeGen *g, ZigType *actual_type, +static LLVMValueRef gen_soft_float_widen_or_shorten(CodeGen *g, ZigType *actual_type, ZigType *wanted_type, LLVMValueRef expr_val) { ZigType *scalar_actual_type = (actual_type->id == ZigTypeIdVector) ? @@ -1615,87 +1656,47 @@ static LLVMValueRef gen_soft_f80_widen_or_shorten(CodeGen *g, ZigType *actual_ty uint64_t actual_bits = scalar_actual_type->data.floating.bit_count; uint64_t wanted_bits = scalar_wanted_type->data.floating.bit_count; - - LLVMTypeRef param_type; - LLVMTypeRef return_type; - const char *func_name; + if (actual_bits == wanted_bits) + return expr_val; LLVMValueRef result; bool castTruncatedToF16 = false; - if (actual_bits == wanted_bits) { - return expr_val; - } else if (actual_bits == 80) { - param_type = g->builtin_types.entry_f80->llvm_type; - switch (wanted_bits) { - case 16: - // Only Arm has a native f16 type, other platforms soft-implement it - // using u16 instead. - if (target_is_arm(g->zig_target)) { - return_type = g->builtin_types.entry_f16->llvm_type; - } else { - return_type = g->builtin_types.entry_u16->llvm_type; - castTruncatedToF16 = true; - } - func_name = "__truncxfhf2"; - break; - case 32: - return_type = g->builtin_types.entry_f32->llvm_type; - func_name = "__truncxfsf2"; - break; - case 64: - return_type = g->builtin_types.entry_f64->llvm_type; - func_name = "__truncxfdf2"; - break; - case 128: - return_type = g->builtin_types.entry_f128->llvm_type; - func_name = "__extendxftf2"; - break; - default: - zig_unreachable(); + char fn_name[64]; + if (wanted_bits < actual_bits) { + sprintf(fn_name, "__trunc%sf%sf2", + get_compiler_rt_type_abbrev(scalar_actual_type), + get_compiler_rt_type_abbrev(scalar_wanted_type)); + } else { + sprintf(fn_name, "__extend%sf%sf2", + get_compiler_rt_type_abbrev(scalar_actual_type), + get_compiler_rt_type_abbrev(scalar_wanted_type)); + } + + LLVMTypeRef return_type = scalar_wanted_type->llvm_type; + LLVMTypeRef param_type = scalar_actual_type->llvm_type; + + if (!target_is_arm(g->zig_target)) { + // Only Arm has a native f16 type, other platforms soft-implement it using u16 instead. + if (scalar_wanted_type == g->builtin_types.entry_f16) { + return_type = g->builtin_types.entry_u16->llvm_type; + castTruncatedToF16 = true; } - } else if (wanted_bits == 80) { - return_type = g->builtin_types.entry_f80->llvm_type; - switch (actual_bits) { - case 16: - // Only Arm has a native f16 type, other platforms soft-implement it - // using u16 instead. - if (target_is_arm(g->zig_target)) { - param_type = g->builtin_types.entry_f16->llvm_type; - } else { - param_type = g->builtin_types.entry_u16->llvm_type; - expr_val = LLVMBuildBitCast(g->builder, expr_val, param_type, ""); - } - func_name = "__extendhfxf2"; - break; - case 32: - param_type = g->builtin_types.entry_f32->llvm_type; - func_name = "__extendsfxf2"; - break; - case 64: - param_type = g->builtin_types.entry_f64->llvm_type; - func_name = "__extenddfxf2"; - break; - case 128: - param_type = g->builtin_types.entry_f128->llvm_type; - func_name = "__trunctfxf2"; - break; - default: - zig_unreachable(); + if (scalar_actual_type == g->builtin_types.entry_f16) { + param_type = g->builtin_types.entry_u16->llvm_type; + expr_val = LLVMBuildBitCast(g->builder, expr_val, param_type, ""); } - } else { - zig_unreachable(); } - LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, func_name); + LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, fn_name); if (func_ref == nullptr) { LLVMTypeRef fn_type = LLVMFunctionType(return_type, ¶m_type, 1, false); - func_ref = LLVMAddFunction(g->module, func_name, fn_type); + func_ref = LLVMAddFunction(g->module, fn_name, fn_type); } result = LLVMBuildCall(g->builder, func_ref, &expr_val, 1, ""); - // On non-Arm platforms we need to bitcast __truncxfhf2 result back to f16 + // On non-Arm platforms we need to bitcast __trunc<>fhf2 result back to f16 if (castTruncatedToF16) { result = LLVMBuildBitCast(g->builder, result, g->builtin_types.entry_f16->llvm_type, ""); } @@ -1721,7 +1722,7 @@ static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_runtime_safety, Z || scalar_wanted_type == g->builtin_types.entry_f80) && !target_has_f80(g->zig_target)) { - return gen_soft_f80_widen_or_shorten(g, actual_type, wanted_type, expr_val); + return gen_soft_float_widen_or_shorten(g, actual_type, wanted_type, expr_val); } actual_bits = scalar_actual_type->data.floating.bit_count; wanted_bits = scalar_wanted_type->data.floating.bit_count; @@ -2978,10 +2979,50 @@ static LLVMValueRef gen_overflow_shr_op(CodeGen *g, ZigType *operand_type, return result; } -static LLVMValueRef gen_float_op(CodeGen *g, LLVMValueRef val, ZigType *type_entry, BuiltinFnId op) { - assert(type_entry->id == ZigTypeIdFloat || type_entry->id == ZigTypeIdVector); - LLVMValueRef floor_fn = get_float_fn(g, type_entry, ZigLLVMFnIdFloatOp, op); - return LLVMBuildCall(g->builder, floor_fn, &val, 1, ""); +static LLVMValueRef get_soft_float_fn(CodeGen *g, const char *name, int param_count, LLVMTypeRef param_type, LLVMTypeRef return_type) { + LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, name); + if (existing_llvm_fn != nullptr) return existing_llvm_fn; + LLVMValueRef existing_llvm_alias = LLVMGetNamedGlobalAlias(g->module, name, strlen(name)); + if (existing_llvm_alias != nullptr) return LLVMAliasGetAliasee(existing_llvm_alias); + + LLVMTypeRef param_types[3] = { param_type, param_type, param_type }; + LLVMTypeRef fn_type = LLVMFunctionType(return_type, param_types, param_count, false); + return LLVMAddFunction(g->module, name, fn_type); +} + +static LLVMValueRef gen_soft_float_un_op(CodeGen *g, LLVMValueRef op, ZigType *operand_type, BuiltinFnId op_id) { + uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; + + char fn_name[64]; + sprintf(fn_name, "%s%s", float_un_op_to_name(op_id), get_math_h_type_abbrev(g, operand_type)); + LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, 1, operand_type->llvm_type, operand_type->llvm_type); + + LLVMValueRef result; + if (vector_len == 0) { + return LLVMBuildCall(g->builder, func_ref, &op, 1, ""); + } else { + result = build_alloca(g, operand_type, "", 0); + LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; + for (uint32_t i = 0; i < vector_len; i++) { + LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); + LLVMValueRef param = LLVMBuildExtractElement(g->builder, op, index_value, ""); + LLVMValueRef call_result = LLVMBuildCall(g->builder, func_ref, ¶m, 1, ""); + LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), + call_result, index_value, ""); + } + return LLVMBuildLoad(g->builder, result, ""); + } +} + +static LLVMValueRef gen_float_un_op(CodeGen *g, LLVMValueRef operand, ZigType *operand_type, BuiltinFnId op) { + assert(operand_type->id == ZigTypeIdFloat || operand_type->id == ZigTypeIdVector); + ZigType *elem_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type; + if ((elem_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (elem_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + return gen_soft_float_un_op(g, operand, operand_type, op); + } + LLVMValueRef float_op_fn = get_float_fn(g, operand_type, ZigLLVMFnIdFloatOp, op); + return LLVMBuildCall(g->builder, float_op_fn, &operand, 1, ""); } enum DivKind { @@ -3088,7 +3129,7 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast case DivKindExact: if (want_runtime_safety) { // Safety check: a / b == floor(a / b) - LLVMValueRef floored = gen_float_op(g, result, operand_type, BuiltinFnIdFloor); + LLVMValueRef floored = gen_float_un_op(g, result, operand_type, BuiltinFnIdFloor); LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactOk"); LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactFail"); @@ -3105,9 +3146,9 @@ static LLVMValueRef gen_div(CodeGen *g, bool want_runtime_safety, bool want_fast } return result; case DivKindTrunc: - return gen_float_op(g, result, operand_type, BuiltinFnIdTrunc); + return gen_float_un_op(g, result, operand_type, BuiltinFnIdTrunc); case DivKindFloor: - return gen_float_op(g, result, operand_type, BuiltinFnIdFloor); + return gen_float_un_op(g, result, operand_type, BuiltinFnIdFloor); } zig_unreachable(); } @@ -3269,17 +3310,7 @@ static void gen_shift_rhs_check(CodeGen *g, ZigType *lhs_type, ZigType *rhs_type } } -static LLVMValueRef get_soft_f80_bin_op_func(CodeGen *g, const char *name, int param_count, LLVMTypeRef return_type) { - LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, name); - if (existing_llvm_fn != nullptr) return existing_llvm_fn; - - LLVMTypeRef float_type_ref = g->builtin_types.entry_f80->llvm_type; - LLVMTypeRef param_types[2] = { float_type_ref, float_type_ref }; - LLVMTypeRef fn_type = LLVMFunctionType(return_type, param_types, param_count, false); - return LLVMAddFunction(g->module, name, fn_type); -} - -enum SoftF80Icmp { +enum Icmp { NONE, EQ_ZERO, NE_ZERO, @@ -3289,7 +3320,7 @@ enum SoftF80Icmp { EQ_ONE, }; -static LLVMValueRef add_f80_icmp(CodeGen *g, LLVMValueRef val, SoftF80Icmp kind) { +static LLVMValueRef add_icmp(CodeGen *g, LLVMValueRef val, Icmp kind) { switch (kind) { case NONE: return val; @@ -3322,22 +3353,123 @@ static LLVMValueRef add_f80_icmp(CodeGen *g, LLVMValueRef val, SoftF80Icmp kind) } } -static LLVMValueRef ir_render_soft_f80_bin_op(CodeGen *g, Stage1Air *executable, - Stage1AirInstBinOp *bin_op_instruction) -{ - IrBinOp op_id = bin_op_instruction->op_id; - Stage1AirInst *op1 = bin_op_instruction->op1; - Stage1AirInst *op2 = bin_op_instruction->op2; - uint32_t vector_len = op1->value->type->id == ZigTypeIdVector ? op1->value->type->data.vector.len : 0; +static LLVMValueRef gen_soft_int_to_float_op(CodeGen *g, LLVMValueRef value_ref, ZigType *operand_type, ZigType *result_type) { + uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; - LLVMValueRef op1_value = ir_llvm_value(g, op1); - LLVMValueRef op2_value = ir_llvm_value(g, op2); + // Handle integers of non-pot bitsize by widening them. + const size_t bitsize = operand_type->data.integral.bit_count; + const bool is_signed = operand_type->data.integral.is_signed; + if (bitsize < 32 || !is_power_of_2(bitsize)) { + const size_t wider_bitsize = bitsize < 32 ? 32 : round_to_next_power_of_2(bitsize); + ZigType *const wider_type = get_int_type(g, is_signed, wider_bitsize); + value_ref = gen_widen_or_shorten(g, false, operand_type, wider_type, value_ref); + operand_type = wider_type; + } + assert(bitsize <= 128); + + const char *int_compiler_rt_type_abbrev = get_compiler_rt_type_abbrev(operand_type); + const char *float_compiler_rt_type_abbrev = get_compiler_rt_type_abbrev(result_type); + + char fn_name[64]; + if (is_signed) { + sprintf(fn_name, "__float%si%sf", int_compiler_rt_type_abbrev, float_compiler_rt_type_abbrev); + } else { + sprintf(fn_name, "__floatun%si%sf", int_compiler_rt_type_abbrev, float_compiler_rt_type_abbrev); + } + + int param_count = 1; + LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, param_count, operand_type->llvm_type, result_type->llvm_type); + + LLVMValueRef result; + if (vector_len == 0) { + LLVMValueRef params[1] = {value_ref}; + result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); + } else { + ZigType *alloca_ty = operand_type; + result = build_alloca(g, alloca_ty, "", 0); + + LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; + for (uint32_t i = 0; i < vector_len; i++) { + LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); + LLVMValueRef params[1] = { + LLVMBuildExtractElement(g->builder, value_ref, index_value, ""), + }; + LLVMValueRef call_result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); + LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), + call_result, index_value, ""); + } + + result = LLVMBuildLoad(g->builder, result, ""); + } + return result; +} - bool div_exact_safety_check = false; - LLVMTypeRef return_type = g->builtin_types.entry_f80->llvm_type; +static LLVMValueRef gen_soft_float_to_int_op(CodeGen *g, LLVMValueRef value_ref, ZigType *operand_type, ZigType *result_type) { + uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; + + // Handle integers of non-pot bitsize by truncating a sufficiently wide pot integer + const size_t bitsize = result_type->data.integral.bit_count; + const bool is_signed = result_type->data.integral.is_signed; + ZigType * wider_type = result_type; + if (bitsize < 32 || !is_power_of_2(bitsize)) { + const size_t wider_bitsize = bitsize < 32 ? 32 : round_to_next_power_of_2(bitsize); + wider_type = get_int_type(g, is_signed, wider_bitsize); + } + assert(bitsize <= 128); + + const char *float_compiler_rt_type_abbrev = get_compiler_rt_type_abbrev(operand_type); + const char *int_compiler_rt_type_abbrev = get_compiler_rt_type_abbrev(wider_type); + + char fn_name[64]; + if (is_signed) { + sprintf(fn_name, "__fix%sf%si", float_compiler_rt_type_abbrev, int_compiler_rt_type_abbrev); + } else { + sprintf(fn_name, "__fixuns%sf%si", float_compiler_rt_type_abbrev, int_compiler_rt_type_abbrev); + } + + int param_count = 1; + LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, param_count, operand_type->llvm_type, wider_type->llvm_type); + + LLVMValueRef result; + if (vector_len == 0) { + LLVMValueRef params[1] = {value_ref}; + result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); + } else { + ZigType *alloca_ty = operand_type; + result = build_alloca(g, alloca_ty, "", 0); + + LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; + for (uint32_t i = 0; i < vector_len; i++) { + LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); + LLVMValueRef params[1] = { + LLVMBuildExtractElement(g->builder, value_ref, index_value, ""), + }; + LLVMValueRef call_result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); + LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), + call_result, index_value, ""); + } + + result = LLVMBuildLoad(g->builder, result, ""); + } + + // Handle integers of non-pot bitsize by shortening them on the output + if (result_type != wider_type) { + return gen_widen_or_shorten(g, false, wider_type, result_type, result); + } + return result; +} + +static LLVMValueRef gen_soft_float_bin_op(CodeGen *g, LLVMValueRef op1_value, LLVMValueRef op2_value, ZigType *operand_type, IrBinOp op_id) { + uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; + + LLVMTypeRef return_type = operand_type->llvm_type; int param_count = 2; - const char *func_name; - SoftF80Icmp res_icmp = NONE; + + const char *compiler_rt_type_abbrev = get_compiler_rt_type_abbrev(operand_type); + const char *math_h_type_abbrev = get_math_h_type_abbrev(g, operand_type); + + char fn_name[64]; + Icmp res_icmp = NONE; switch (op_id) { case IrBinOpInvalid: case IrBinOpArrayCat: @@ -3362,152 +3494,129 @@ static LLVMValueRef ir_render_soft_f80_bin_op(CodeGen *g, Stage1Air *executable, zig_unreachable(); case IrBinOpCmpEq: return_type = g->builtin_types.entry_i32->llvm_type; - func_name = "__eqxf2"; + sprintf(fn_name, "__eq%sf2", compiler_rt_type_abbrev); res_icmp = EQ_ZERO; break; case IrBinOpCmpNotEq: return_type = g->builtin_types.entry_i32->llvm_type; - func_name = "__nexf2"; + sprintf(fn_name, "__ne%sf2", compiler_rt_type_abbrev); res_icmp = NE_ZERO; break; case IrBinOpCmpLessOrEq: return_type = g->builtin_types.entry_i32->llvm_type; - func_name = "__lexf2"; + sprintf(fn_name, "__le%sf2", compiler_rt_type_abbrev); res_icmp = LE_ZERO; break; case IrBinOpCmpLessThan: return_type = g->builtin_types.entry_i32->llvm_type; - func_name = "__lexf2"; + sprintf(fn_name, "__le%sf2", compiler_rt_type_abbrev); res_icmp = EQ_NEG; break; case IrBinOpCmpGreaterOrEq: return_type = g->builtin_types.entry_i32->llvm_type; - func_name = "__gexf2"; + sprintf(fn_name, "__ge%sf2", compiler_rt_type_abbrev); res_icmp = GE_ZERO; break; case IrBinOpCmpGreaterThan: return_type = g->builtin_types.entry_i32->llvm_type; - func_name = "__gexf2"; + sprintf(fn_name, "__ge%sf2", compiler_rt_type_abbrev); res_icmp = EQ_ONE; break; case IrBinOpMaximum: - func_name = "__fmaxx"; + sprintf(fn_name, "fmax%s", math_h_type_abbrev); break; case IrBinOpMinimum: - func_name = "__fminx"; + sprintf(fn_name, "fmin%s", math_h_type_abbrev); break; case IrBinOpMult: - func_name = "__mulxf3"; + sprintf(fn_name, "__mul%sf3", compiler_rt_type_abbrev); break; case IrBinOpAdd: - func_name = "__addxf3"; + sprintf(fn_name, "__add%sf3", compiler_rt_type_abbrev); break; case IrBinOpSub: - func_name = "__subxf3"; + sprintf(fn_name, "__sub%sf3", compiler_rt_type_abbrev); break; case IrBinOpDivUnspecified: - func_name = "__divxf3"; - break; case IrBinOpDivExact: - func_name = "__divxf3"; - div_exact_safety_check = bin_op_instruction->safety_check_on && - ir_want_runtime_safety(g, &bin_op_instruction->base); - break; case IrBinOpDivTrunc: - param_count = 1; - func_name = "__truncx"; - break; case IrBinOpDivFloor: - param_count = 1; - func_name = "__floorx"; + sprintf(fn_name, "__div%sf3", compiler_rt_type_abbrev); break; case IrBinOpRemRem: - param_count = 1; - func_name = "__remx"; - break; case IrBinOpRemMod: - param_count = 1; - func_name = "__modx"; + sprintf(fn_name, "fmod%s", math_h_type_abbrev); break; default: zig_unreachable(); } - LLVMValueRef func_ref = get_soft_f80_bin_op_func(g, func_name, param_count, return_type); + LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, param_count, operand_type->llvm_type, return_type); LLVMValueRef result; if (vector_len == 0) { LLVMValueRef params[2] = {op1_value, op2_value}; result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); - result = add_f80_icmp(g, result, res_icmp); + result = add_icmp(g, result, res_icmp); } else { - ZigType *alloca_ty = op1->value->type; + ZigType *alloca_ty = operand_type; if (res_icmp != NONE) alloca_ty = get_vector_type(g, vector_len, g->builtin_types.entry_bool); result = build_alloca(g, alloca_ty, "", 0); - } - - LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; - for (uint32_t i = 0; i < vector_len; i++) { - LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); - LLVMValueRef params[2] = { - LLVMBuildExtractElement(g->builder, op1_value, index_value, ""), - LLVMBuildExtractElement(g->builder, op2_value, index_value, ""), - }; - LLVMValueRef call_result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); - call_result = add_f80_icmp(g, call_result, res_icmp); - LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), - call_result, index_value, ""); - } - - if (div_exact_safety_check) { - // Safety check: a / b == floor(a / b) - LLVMValueRef floor_func = get_soft_f80_bin_op_func(g, "__floorx", 1, return_type); - LLVMValueRef eq_func = get_soft_f80_bin_op_func(g, "__eqxf2", 2, g->builtin_types.entry_i32->llvm_type); - - LLVMValueRef ok_bit; - if (vector_len == 0) { - LLVMValueRef floored = LLVMBuildCall(g->builder, floor_func, &result, 1, ""); - - LLVMValueRef params[2] = {result, floored}; - ok_bit = LLVMBuildCall(g->builder, eq_func, params, 2, ""); - } else { - ZigType *bool_vec_ty = get_vector_type(g, vector_len, g->builtin_types.entry_bool); - ok_bit = build_alloca(g, bool_vec_ty, "", 0); - } + LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; for (uint32_t i = 0; i < vector_len; i++) { LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); - LLVMValueRef div_res = LLVMBuildExtractElement(g->builder, - LLVMBuildLoad(g->builder, result, ""), index_value, ""); - LLVMValueRef params[2] = { - div_res, - LLVMBuildCall(g->builder, floor_func, &div_res, 1, ""), + LLVMBuildExtractElement(g->builder, op1_value, index_value, ""), + LLVMBuildExtractElement(g->builder, op2_value, index_value, ""), }; - LLVMValueRef cmp_res = LLVMBuildCall(g->builder, eq_func, params, 2, ""); - cmp_res = LLVMBuildTrunc(g->builder, cmp_res, g->builtin_types.entry_bool->llvm_type, ""); - LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, ok_bit, ""), - cmp_res, index_value, ""); + LLVMValueRef call_result = LLVMBuildCall(g->builder, func_ref, params, param_count, ""); + call_result = add_icmp(g, call_result, res_icmp); + LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), + call_result, index_value, ""); } - if (vector_len != 0) { - ok_bit = ZigLLVMBuildAndReduce(g->builder, LLVMBuildLoad(g->builder, ok_bit, "")); - } - LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactOk"); - LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactFail"); + result = LLVMBuildLoad(g->builder, result, ""); + } - LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block); + // Some operations are implemented as compound ops and require us to perform some + // more operations before we obtain the final result + switch (op_id) { + case IrBinOpDivTrunc: + return gen_float_un_op(g, result, operand_type, BuiltinFnIdTrunc); + case IrBinOpDivFloor: + return gen_float_un_op(g, result, operand_type, BuiltinFnIdFloor); + case IrBinOpRemMod: + { + LLVMValueRef b = gen_soft_float_bin_op(g, result, op2_value, operand_type, IrBinOpAdd); + LLVMValueRef wrapped_result = gen_soft_float_bin_op(g, b, op2_value, operand_type, IrBinOpRemRem); + LLVMValueRef zero = LLVMConstNull(operand_type->llvm_type); + LLVMValueRef ltz = gen_soft_float_bin_op(g, op1_value, zero, operand_type, IrBinOpCmpLessThan); - LLVMPositionBuilderAtEnd(g->builder, fail_block); - gen_safety_crash(g, PanicMsgIdExactDivisionRemainder); + return LLVMBuildSelect(g->builder, ltz, wrapped_result, result, ""); + } + case IrBinOpDivExact: + { + LLVMValueRef floored = gen_float_un_op(g, result, operand_type, BuiltinFnIdFloor); + LLVMValueRef ok_bit = gen_soft_float_bin_op(g, result, floored, operand_type, IrBinOpCmpEq); + if (vector_len != 0) { + ok_bit = ZigLLVMBuildAndReduce(g->builder, ok_bit); + } - LLVMPositionBuilderAtEnd(g->builder, ok_block); - } + LLVMBasicBlockRef ok_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactOk"); + LLVMBasicBlockRef fail_block = LLVMAppendBasicBlock(g->cur_fn_val, "DivExactFail"); + LLVMBuildCondBr(g->builder, ok_bit, ok_block, fail_block); - if (vector_len != 0) { - result = LLVMBuildLoad(g->builder, result, ""); + LLVMPositionBuilderAtEnd(g->builder, fail_block); + gen_safety_crash(g, PanicMsgIdExactDivisionRemainder); + + LLVMPositionBuilderAtEnd(g->builder, ok_block); + } + return result; + default: + return result; } - return result; + zig_unreachable(); } static LLVMValueRef ir_render_bin_op(CodeGen *g, Stage1Air *executable, @@ -3519,8 +3628,13 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, Stage1Air *executable, ZigType *operand_type = op1->value->type; ZigType *scalar_type = (operand_type->id == ZigTypeIdVector) ? operand_type->data.vector.elem_type : operand_type; - if (scalar_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) { - return ir_render_soft_f80_bin_op(g, executable, bin_op_instruction); + if ((scalar_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (scalar_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + // LLVM incorrectly lowers the soft float calls for f128 as if they operated on `long double`. + // On some targets this will be incorrect, so we manually lower the call ourselves. + LLVMValueRef op1_value = ir_llvm_value(g, op1); + LLVMValueRef op2_value = ir_llvm_value(g, op2); + return gen_soft_float_bin_op(g, op1_value, op2_value, operand_type, op_id); } @@ -3828,10 +3942,17 @@ static LLVMValueRef ir_render_cast(CodeGen *g, Stage1Air *executable, } case CastOpIntToFloat: assert(actual_type->id == ZigTypeIdInt); - if (actual_type->data.integral.is_signed) { - return LLVMBuildSIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); - } else { - return LLVMBuildUIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + { + if ((wanted_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (wanted_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + return gen_soft_int_to_float_op(g, expr_val, actual_type, wanted_type); + } else { + if (actual_type->data.integral.is_signed) { + return LLVMBuildSIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + } else { + return LLVMBuildUIToFP(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + } + } } case CastOpFloatToInt: { assert(wanted_type->id == ZigTypeIdInt); @@ -3840,18 +3961,28 @@ static LLVMValueRef ir_render_cast(CodeGen *g, Stage1Air *executable, bool want_safety = ir_want_runtime_safety(g, &cast_instruction->base); LLVMValueRef result; - if (wanted_type->data.integral.is_signed) { - result = LLVMBuildFPToSI(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + if ((actual_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (actual_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + result = gen_soft_float_to_int_op(g, expr_val, actual_type, wanted_type); } else { - result = LLVMBuildFPToUI(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + if (wanted_type->data.integral.is_signed) { + result = LLVMBuildFPToSI(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + } else { + result = LLVMBuildFPToUI(g->builder, expr_val, get_llvm_type(g, wanted_type), ""); + } } if (want_safety) { LLVMValueRef back_to_float; - if (wanted_type->data.integral.is_signed) { - back_to_float = LLVMBuildSIToFP(g->builder, result, LLVMTypeOf(expr_val), ""); + if ((actual_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (actual_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + back_to_float = gen_soft_int_to_float_op(g, result, wanted_type, actual_type); } else { - back_to_float = LLVMBuildUIToFP(g->builder, result, LLVMTypeOf(expr_val), ""); + if (wanted_type->data.integral.is_signed) { + back_to_float = LLVMBuildSIToFP(g->builder, result, LLVMTypeOf(expr_val), ""); + } else { + back_to_float = LLVMBuildUIToFP(g->builder, result, LLVMTypeOf(expr_val), ""); + } } LLVMValueRef difference = LLVMBuildFSub(g->builder, expr_val, back_to_float, ""); LLVMValueRef one_pos = LLVMConstReal(LLVMTypeOf(expr_val), 1.0f); @@ -4151,42 +4282,46 @@ static LLVMValueRef ir_render_binary_not(CodeGen *g, Stage1Air *executable, return LLVMBuildNot(g->builder, operand, ""); } -static LLVMValueRef ir_gen_soft_f80_neg(CodeGen *g, ZigType *op_type, LLVMValueRef operand) { - uint32_t vector_len = op_type->id == ZigTypeIdVector ? op_type->data.vector.len : 0; +static LLVMValueRef gen_soft_float_neg(CodeGen *g, ZigType *operand_type, LLVMValueRef operand) { + uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; + uint16_t num_bits = operand_type->data.floating.bit_count; - LLVMTypeRef llvm_i80 = LLVMIntType(80); - LLVMValueRef sign_mask = LLVMConstInt(llvm_i80, 1, false); - sign_mask = LLVMConstShl(sign_mask, LLVMConstInt(llvm_i80, 79, false)); + ZigType *iX_type = get_int_type(g, true, num_bits); + LLVMValueRef sign_mask = LLVMConstInt(iX_type->llvm_type, 1, false); + sign_mask = LLVMConstShl(sign_mask, LLVMConstInt(iX_type->llvm_type, num_bits - 1, false)); - LLVMValueRef result; if (vector_len == 0) { - result = LLVMBuildXor(g->builder, operand, sign_mask, ""); + LLVMValueRef bitcasted_operand = LLVMBuildBitCast(g->builder, operand, iX_type->llvm_type, ""); + LLVMValueRef result = LLVMBuildXor(g->builder, bitcasted_operand, sign_mask, ""); + + return LLVMBuildBitCast(g->builder, result, operand_type->llvm_type, ""); } else { - result = build_alloca(g, op_type, "", 0); - } + LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; + ZigType *iX_vector_type = get_vector_type(g, vector_len, iX_type); - LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; - for (uint32_t i = 0; i < vector_len; i++) { - LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); - LLVMValueRef xor_operand = LLVMBuildExtractElement(g->builder, operand, index_value, ""); - LLVMValueRef xor_result = LLVMBuildXor(g->builder, xor_operand, sign_mask, ""); - LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), - xor_result, index_value, ""); - } - if (vector_len != 0) { - result = LLVMBuildLoad(g->builder, result, ""); + LLVMValueRef result = build_alloca(g, iX_vector_type, "", 0); + LLVMValueRef bitcasted_operand = LLVMBuildBitCast(g->builder, operand, iX_vector_type->llvm_type, ""); + for (uint32_t i = 0; i < vector_len; i++) { + LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); + LLVMValueRef elem = LLVMBuildExtractElement(g->builder, bitcasted_operand, index_value, ""); + LLVMValueRef result_elem = LLVMBuildXor(g->builder, elem, sign_mask, ""); + LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), + result_elem, index_value, ""); + } + return LLVMBuildBitCast(g->builder, LLVMBuildLoad(g->builder, result, ""), operand_type->llvm_type, ""); } - return result; } -static LLVMValueRef ir_gen_negation(CodeGen *g, Stage1AirInst *inst, Stage1AirInst *operand, bool wrapping) { +static LLVMValueRef gen_negation(CodeGen *g, Stage1AirInst *inst, Stage1AirInst *operand, bool wrapping) { LLVMValueRef llvm_operand = ir_llvm_value(g, operand); ZigType *operand_type = operand->value->type; ZigType *scalar_type = (operand_type->id == ZigTypeIdVector) ? operand_type->data.vector.elem_type : operand_type; - if (scalar_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) - return ir_gen_soft_f80_neg(g, operand_type, llvm_operand); + if ((scalar_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (scalar_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + return gen_soft_float_neg(g, operand_type, llvm_operand); + } if (scalar_type->id == ZigTypeIdFloat) { ZigLLVMSetFastMath(g->builder, ir_want_fast_math(g, inst)); @@ -4210,7 +4345,7 @@ static LLVMValueRef ir_gen_negation(CodeGen *g, Stage1AirInst *inst, Stage1AirIn static LLVMValueRef ir_render_negation(CodeGen *g, Stage1Air *executable, Stage1AirInstNegation *inst) { - return ir_gen_negation(g, &inst->base, inst->operand, inst->wrapping); + return gen_negation(g, &inst->base, inst->operand, inst->wrapping); } static LLVMValueRef ir_render_bool_not(CodeGen *g, Stage1Air *executable, Stage1AirInstBoolNot *instruction) { @@ -7024,110 +7159,34 @@ static LLVMValueRef ir_render_atomic_store(CodeGen *g, Stage1Air *executable, return nullptr; } -static LLVMValueRef ir_render_soft_f80_float_op(CodeGen *g, Stage1Air *executable, Stage1AirInstFloatOp *instruction) { - ZigType *op_type = instruction->operand->value->type; - uint32_t vector_len = op_type->id == ZigTypeIdVector ? op_type->data.vector.len : 0; - - const char *func_name; - switch (instruction->fn_id) { - case BuiltinFnIdSqrt: - func_name = "__sqrtx"; - break; - case BuiltinFnIdSin: - func_name = "__sinx"; - break; - case BuiltinFnIdCos: - func_name = "__cosx"; - break; - case BuiltinFnIdExp: - func_name = "__expx"; - break; - case BuiltinFnIdExp2: - func_name = "__exp2x"; - break; - case BuiltinFnIdLog: - func_name = "__logx"; - break; - case BuiltinFnIdLog2: - func_name = "__log2x"; - break; - case BuiltinFnIdLog10: - func_name = "__log10x"; - break; - case BuiltinFnIdFabs: - func_name = "__fabsx"; - break; - case BuiltinFnIdFloor: - func_name = "__floorx"; - break; - case BuiltinFnIdCeil: - func_name = "__ceilx"; - break; - case BuiltinFnIdTrunc: - func_name = "__truncx"; - break; - case BuiltinFnIdNearbyInt: - func_name = "__nearbyintx"; - break; - case BuiltinFnIdRound: - func_name = "__roundx"; - break; - default: - zig_unreachable(); - } - - - LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, func_name); - if (func_ref == nullptr) { - LLVMTypeRef f80_ref = g->builtin_types.entry_f80->llvm_type; - LLVMTypeRef fn_type = LLVMFunctionType(f80_ref, &f80_ref, 1, false); - func_ref = LLVMAddFunction(g->module, func_name, fn_type); - } - - LLVMValueRef operand = ir_llvm_value(g, instruction->operand); - LLVMValueRef result; - if (vector_len == 0) { - result = LLVMBuildCall(g->builder, func_ref, &operand, 1, ""); - } else { - result = build_alloca(g, instruction->operand->value->type, "", 0); - } - - LLVMTypeRef usize_ref = g->builtin_types.entry_usize->llvm_type; - for (uint32_t i = 0; i < vector_len; i++) { - LLVMValueRef index_value = LLVMConstInt(usize_ref, i, false); - LLVMValueRef param = LLVMBuildExtractElement(g->builder, operand, index_value, ""); - LLVMValueRef call_result = LLVMBuildCall(g->builder, func_ref, ¶m, 1, ""); - LLVMBuildInsertElement(g->builder, LLVMBuildLoad(g->builder, result, ""), - call_result, index_value, ""); - } - if (vector_len != 0) { - result = LLVMBuildLoad(g->builder, result, ""); - } - return result; -} - static LLVMValueRef ir_render_float_op(CodeGen *g, Stage1Air *executable, Stage1AirInstFloatOp *instruction) { - ZigType *op_type = instruction->operand->value->type; - op_type = op_type->id == ZigTypeIdVector ? op_type->data.vector.elem_type : op_type; - if (op_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) { - return ir_render_soft_f80_float_op(g, executable, instruction); - } LLVMValueRef operand = ir_llvm_value(g, instruction->operand); - LLVMValueRef fn_val = get_float_fn(g, instruction->base.value->type, ZigLLVMFnIdFloatOp, instruction->fn_id); - return LLVMBuildCall(g->builder, fn_val, &operand, 1, ""); + ZigType *operand_type = instruction->operand->value->type; + return gen_float_un_op(g, operand, operand_type, instruction->fn_id); } -static LLVMValueRef ir_render_soft_f80_mul_add(CodeGen *g, Stage1Air *executable, Stage1AirInstMulAdd *instruction) { - ZigType *op_type = instruction->op1->value->type; - uint32_t vector_len = op_type->id == ZigTypeIdVector ? op_type->data.vector.len : 0; +static LLVMValueRef ir_render_soft_mul_add(CodeGen *g, Stage1Air *executable, Stage1AirInstMulAdd *instruction, ZigType *float_type) { + ZigType *operand_type = instruction->op1->value->type; + uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; + + const char *fn_name; + if (float_type == g->builtin_types.entry_f32) + fn_name = "fmaf"; + else if (float_type == g->builtin_types.entry_f64) + fn_name = "fma"; + else if (float_type == g->builtin_types.entry_f80) + fn_name = "__fmax"; + else if (float_type == g->builtin_types.entry_f128) + fn_name = "fmaq"; + else + zig_unreachable(); - const char *func_name = "__fmax"; - LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, func_name); + LLVMValueRef func_ref = LLVMGetNamedFunction(g->module, fn_name); if (func_ref == nullptr) { - LLVMTypeRef f80_ref = g->builtin_types.entry_f80->llvm_type; - LLVMTypeRef params[3] = { f80_ref, f80_ref, f80_ref }; - LLVMTypeRef fn_type = LLVMFunctionType(f80_ref, params, 3, false); - func_ref = LLVMAddFunction(g->module, func_name, fn_type); + LLVMTypeRef float_type_ref = float_type->llvm_type; + LLVMTypeRef params[3] = { float_type_ref, float_type_ref, float_type_ref }; + LLVMTypeRef fn_type = LLVMFunctionType(float_type_ref, params, 3, false); + func_ref = LLVMAddFunction(g->module, fn_name, fn_type); } LLVMValueRef op1 = ir_llvm_value(g, instruction->op1); @@ -7161,10 +7220,11 @@ static LLVMValueRef ir_render_soft_f80_mul_add(CodeGen *g, Stage1Air *executable } static LLVMValueRef ir_render_mul_add(CodeGen *g, Stage1Air *executable, Stage1AirInstMulAdd *instruction) { - ZigType *op_type = instruction->op1->value->type; - op_type = op_type->id == ZigTypeIdVector ? op_type->data.vector.elem_type : op_type; - if (op_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) { - return ir_render_soft_f80_mul_add(g, executable, instruction); + ZigType *operand_type = instruction->op1->value->type; + operand_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type; + if ((operand_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || + (operand_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + return ir_render_soft_mul_add(g, executable, instruction, operand_type); } LLVMValueRef op1 = ir_llvm_value(g, instruction->op1); LLVMValueRef op2 = ir_llvm_value(g, instruction->op2); -- cgit v1.2.3 From 09f1d62bdfb5794534b21d1cd9dafc4822697d60 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 27 Apr 2022 16:45:23 -0700 Subject: add new builtin function `@tan` The reason for having `@tan` is that we already have `@sin` and `@cos` because some targets have machine code instructions for them, but in the case that the implementation needs to go into compiler-rt, sin, cos, and tan all share a common dependency which includes a table of data. To avoid duplicating this table of data, we promote tan to become a builtin alongside sin and cos. ZIR: The tag enum is at capacity so this commit moves `field_call_bind_named` to be `extended`. I measured this as one of the least used tags in the zig codebase. Fix libc math suffix for `f32` being wrong in both stage1 and stage2. stage1: add missing libc prefix for float functions. --- doc/langref.html.in | 16 +++++++++- lib/std/math/complex/tanh.zig | 4 +-- src/Air.zig | 8 +++-- src/AstGen.zig | 6 ++-- src/BuiltinFn.zig | 8 +++++ src/Liveness.zig | 1 + src/Sema.zig | 70 +++++++++++++++++++++---------------------- src/Zir.zig | 33 ++++++++++++-------- src/arch/aarch64/CodeGen.zig | 1 + src/arch/arm/CodeGen.zig | 1 + src/arch/riscv64/CodeGen.zig | 1 + src/arch/sparcv9/CodeGen.zig | 1 + src/arch/wasm/CodeGen.zig | 1 + src/arch/x86_64/CodeGen.zig | 1 + src/codegen/c.zig | 1 + src/codegen/llvm.zig | 7 +++-- src/print_air.zig | 1 + src/print_zir.zig | 12 +++++++- src/stage1/all_types.hpp | 1 + src/stage1/analyze.cpp | 2 ++ src/stage1/astgen.cpp | 1 + src/stage1/codegen.cpp | 41 +++++++++++++++++++------ src/stage1/ir.cpp | 11 +++++++ src/value.zig | 38 +++++++++++++++++++++++ test/behavior/bugs/920.zig | 5 ++-- 25 files changed, 203 insertions(+), 69 deletions(-) (limited to 'src/stage1/codegen.cpp') diff --git a/doc/langref.html.in b/doc/langref.html.in index 5cccced446..3c5de6c8d2 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -8026,7 +8026,7 @@ fn func(y: *i32) void { only rounds once, and is thus more accurate.

- Supports Floats and Vectors of floats. + Supports {#link|Floats#} and {#link|Vectors#} of floats.

{#header_close#} @@ -9440,6 +9440,7 @@ fn doTheTest() !void { some float operations are not yet implemented for all float types.

{#header_close#} + {#header_open|@cos#}
{#syntax#}@cos(value: anytype) @TypeOf(value){#endsyntax#}

@@ -9451,6 +9452,19 @@ fn doTheTest() !void { some float operations are not yet implemented for all float types.

{#header_close#} + + {#header_open|@tan#} +
{#syntax#}@tan(value: anytype) @TypeOf(value){#endsyntax#}
+

+ Tangent trigonometric function on a floating point number. + Uses a dedicated hardware instruction when available. +

+

+ Supports {#link|Floats#} and {#link|Vectors#} of floats, with the caveat that + some float operations are not yet implemented for all float types. +

+ {#header_close#} + {#header_open|@exp#}
{#syntax#}@exp(value: anytype) @TypeOf(value){#endsyntax#}

diff --git a/lib/std/math/complex/tanh.zig b/lib/std/math/complex/tanh.zig index e61ec1e95b..d5195d6c73 100644 --- a/lib/std/math/complex/tanh.zig +++ b/lib/std/math/complex/tanh.zig @@ -49,7 +49,7 @@ fn tanh32(z: Complex(f32)) Complex(f32) { } // Kahan's algorithm - const t = math.tan(y); + const t = @tan(y); const beta = 1.0 + t * t; const s = math.sinh(x); const rho = @sqrt(1 + s * s); @@ -92,7 +92,7 @@ fn tanh64(z: Complex(f64)) Complex(f64) { } // Kahan's algorithm - const t = math.tan(y); + const t = @tan(y); const beta = 1.0 + t * t; const s = math.sinh(x); const rho = @sqrt(1 + s * s); diff --git a/src/Air.zig b/src/Air.zig index d02491ff89..0968d95180 100644 --- a/src/Air.zig +++ b/src/Air.zig @@ -249,12 +249,15 @@ pub const Inst = struct { /// Square root of a floating point number. /// Uses the `un_op` field. sqrt, - /// Sine a floating point number. + /// Sine function on a floating point number. /// Uses the `un_op` field. sin, - /// Cosine a floating point number. + /// Cosine function on a floating point number. /// Uses the `un_op` field. cos, + /// Tangent function on a floating point number. + /// Uses the `un_op` field. + tan, /// Base e exponential of a floating point number. /// Uses the `un_op` field. exp, @@ -921,6 +924,7 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/AstGen.zig b/src/AstGen.zig index 34b29b28fb..230b46a489 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -2237,7 +2237,6 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner .field_call_bind, .field_ptr_named, .field_val_named, - .field_call_bind_named, .func, .func_inferred, .int, @@ -2329,6 +2328,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, @@ -7259,6 +7259,7 @@ fn builtinCall( .sqrt => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sqrt), .sin => return simpleUnOp(gz, scope, rl, node, .none, params[0], .sin), .cos => return simpleUnOp(gz, scope, rl, node, .none, params[0], .cos), + .tan => return simpleUnOp(gz, scope, rl, node, .none, params[0], .tan), .exp => return simpleUnOp(gz, scope, rl, node, .none, params[0], .exp), .exp2 => return simpleUnOp(gz, scope, rl, node, .none, params[0], .exp2), .log => return simpleUnOp(gz, scope, rl, node, .none, params[0], .log), @@ -7947,7 +7948,8 @@ fn calleeExpr( if (std.mem.eql(u8, builtin_name, "@field") and params.len == 2) { const lhs = try expr(gz, scope, .ref, params[0]); const field_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]); - return gz.addPlNode(.field_call_bind_named, node, Zir.Inst.FieldNamed{ + return gz.addExtendedPayload(.field_call_bind_named, Zir.Inst.FieldNamedNode{ + .node = gz.nodeIndexToRelative(node), .lhs = lhs, .field_name = field_name, }); diff --git a/src/BuiltinFn.zig b/src/BuiltinFn.zig index 3bf7224fab..04cad19354 100644 --- a/src/BuiltinFn.zig +++ b/src/BuiltinFn.zig @@ -89,6 +89,7 @@ pub const Tag = enum { sqrt, sin, cos, + tan, exp, exp2, log, @@ -771,6 +772,13 @@ pub const list = list: { .param_count = 1, }, }, + .{ + "@tan", + .{ + .tag = .tan, + .param_count = 1, + }, + }, .{ "@exp", .{ diff --git a/src/Liveness.zig b/src/Liveness.zig index be4344ab90..e606c15b4b 100644 --- a/src/Liveness.zig +++ b/src/Liveness.zig @@ -422,6 +422,7 @@ fn analyzeInst( .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/Sema.zig b/src/Sema.zig index 5d1d51b58f..3fa0353e9d 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -743,7 +743,6 @@ fn analyzeBodyInner( .field_val => try sema.zirFieldVal(block, inst), .field_val_named => try sema.zirFieldValNamed(block, inst), .field_call_bind => try sema.zirFieldCallBind(block, inst), - .field_call_bind_named => try sema.zirFieldCallBindNamed(block, inst), .func => try sema.zirFunc(block, inst, false), .func_inferred => try sema.zirFunc(block, inst, true), .import => try sema.zirImport(block, inst), @@ -855,6 +854,7 @@ fn analyzeBodyInner( .sqrt => try sema.zirUnaryMath(block, inst, .sqrt, Value.sqrt), .sin => try sema.zirUnaryMath(block, inst, .sin, Value.sin), .cos => try sema.zirUnaryMath(block, inst, .cos, Value.cos), + .tan => try sema.zirUnaryMath(block, inst, .tan, Value.tan), .exp => try sema.zirUnaryMath(block, inst, .exp, Value.exp), .exp2 => try sema.zirUnaryMath(block, inst, .exp2, Value.exp2), .log => try sema.zirUnaryMath(block, inst, .log, Value.log), @@ -910,35 +910,36 @@ fn analyzeBodyInner( const extended = datas[inst].extended; break :ext switch (extended.opcode) { // zig fmt: off - .func => try sema.zirFuncExtended( block, extended, inst), - .variable => try sema.zirVarExtended( block, extended), - .struct_decl => try sema.zirStructDecl( block, extended, inst), - .enum_decl => try sema.zirEnumDecl( block, extended), - .union_decl => try sema.zirUnionDecl( block, extended, inst), - .opaque_decl => try sema.zirOpaqueDecl( block, extended), - .ret_ptr => try sema.zirRetPtr( block, extended), - .ret_type => try sema.zirRetType( block, extended), - .this => try sema.zirThis( block, extended), - .ret_addr => try sema.zirRetAddr( block, extended), - .builtin_src => try sema.zirBuiltinSrc( block, extended), - .error_return_trace => try sema.zirErrorReturnTrace( block, extended), - .frame => try sema.zirFrame( block, extended), - .frame_address => try sema.zirFrameAddress( block, extended), - .alloc => try sema.zirAllocExtended( block, extended), - .builtin_extern => try sema.zirBuiltinExtern( block, extended), - .@"asm" => try sema.zirAsm( block, extended), - .typeof_peer => try sema.zirTypeofPeer( block, extended), - .compile_log => try sema.zirCompileLog( block, extended), - .add_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), - .sub_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), - .mul_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), - .shl_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), - .c_undef => try sema.zirCUndef( block, extended), - .c_include => try sema.zirCInclude( block, extended), - .c_define => try sema.zirCDefine( block, extended), - .wasm_memory_size => try sema.zirWasmMemorySize( block, extended), - .wasm_memory_grow => try sema.zirWasmMemoryGrow( block, extended), - .prefetch => try sema.zirPrefetch( block, extended), + .func => try sema.zirFuncExtended( block, extended, inst), + .variable => try sema.zirVarExtended( block, extended), + .struct_decl => try sema.zirStructDecl( block, extended, inst), + .enum_decl => try sema.zirEnumDecl( block, extended), + .union_decl => try sema.zirUnionDecl( block, extended, inst), + .opaque_decl => try sema.zirOpaqueDecl( block, extended), + .ret_ptr => try sema.zirRetPtr( block, extended), + .ret_type => try sema.zirRetType( block, extended), + .this => try sema.zirThis( block, extended), + .ret_addr => try sema.zirRetAddr( block, extended), + .builtin_src => try sema.zirBuiltinSrc( block, extended), + .error_return_trace => try sema.zirErrorReturnTrace( block, extended), + .frame => try sema.zirFrame( block, extended), + .frame_address => try sema.zirFrameAddress( block, extended), + .alloc => try sema.zirAllocExtended( block, extended), + .builtin_extern => try sema.zirBuiltinExtern( block, extended), + .@"asm" => try sema.zirAsm( block, extended), + .typeof_peer => try sema.zirTypeofPeer( block, extended), + .compile_log => try sema.zirCompileLog( block, extended), + .add_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), + .sub_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), + .mul_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), + .shl_with_overflow => try sema.zirOverflowArithmetic(block, extended, extended.opcode), + .c_undef => try sema.zirCUndef( block, extended), + .c_include => try sema.zirCInclude( block, extended), + .c_define => try sema.zirCDefine( block, extended), + .wasm_memory_size => try sema.zirWasmMemorySize( block, extended), + .wasm_memory_grow => try sema.zirWasmMemoryGrow( block, extended), + .prefetch => try sema.zirPrefetch( block, extended), + .field_call_bind_named => try sema.zirFieldCallBindNamed(block, extended), // zig fmt: on .dbg_block_begin => { dbg_block_begins += 1; @@ -6938,14 +6939,13 @@ fn zirFieldPtrNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr return sema.fieldPtr(block, src, object_ptr, field_name, field_name_src); } -fn zirFieldCallBindNamed(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { +fn zirFieldCallBindNamed(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { const tracy = trace(@src()); defer tracy.end(); - const inst_data = sema.code.instructions.items(.data)[inst].pl_node; - const src = inst_data.src(); - const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node }; - const extra = sema.code.extraData(Zir.Inst.FieldNamed, inst_data.payload_index).data; + const extra = sema.code.extraData(Zir.Inst.FieldNamedNode, extended.operand).data; + const src: LazySrcLoc = .{ .node_offset = extra.node }; + const field_name_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = extra.node }; const object_ptr = sema.resolveInst(extra.lhs); const field_name = try sema.resolveConstString(block, field_name_src, extra.field_name); return sema.fieldCallBind(block, src, object_ptr, field_name, field_name_src); diff --git a/src/Zir.zig b/src/Zir.zig index 8fe5276792..f4c62a6f24 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -407,15 +407,6 @@ pub const Inst = struct { /// The field name is a comptime instruction. Used by @field. /// Uses `pl_node` field. The AST node is the builtin call. Payload is FieldNamed. field_val_named, - /// Given a pointer to a struct or object that contains virtual fields, returns the - /// named field. If there is no named field, searches in the type for a decl that - /// matches the field name. The decl is resolved and we ensure that it's a function - /// which can accept the object as the first parameter, with one pointer fixup. If - /// all of that works, this instruction produces a special "bound function" value - /// which contains both the function and the saved first parameter value. - /// Bound functions may only be used as the function parameter to a `call` or - /// `builtin_call` instruction. Any other use is invalid zir and may crash the compiler. - field_call_bind_named, /// Returns a function type, or a function instance, depending on whether /// the body_len is 0. Calling convention is auto. /// Uses the `pl_node` union field. `payload_index` points to a `Func`. @@ -797,6 +788,8 @@ pub const Inst = struct { sin, /// Implement builtin `@cos`. Uses `un_node`. cos, + /// Implement builtin `@tan`. Uses `un_node`. + tan, /// Implement builtin `@exp`. Uses `un_node`. exp, /// Implement builtin `@exp2`. Uses `un_node`. @@ -1069,7 +1062,6 @@ pub const Inst = struct { .field_call_bind, .field_ptr_named, .field_val_named, - .field_call_bind_named, .func, .func_inferred, .has_decl, @@ -1179,6 +1171,7 @@ pub const Inst = struct { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, @@ -1358,7 +1351,6 @@ pub const Inst = struct { .field_call_bind, .field_ptr_named, .field_val_named, - .field_call_bind_named, .func, .func_inferred, .has_decl, @@ -1451,6 +1443,7 @@ pub const Inst = struct { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, @@ -1607,7 +1600,6 @@ pub const Inst = struct { .field_ptr_named = .pl_node, .field_val_named = .pl_node, .field_call_bind = .pl_node, - .field_call_bind_named = .pl_node, .func = .pl_node, .func_inferred = .pl_node, .import = .str_tok, @@ -1713,6 +1705,7 @@ pub const Inst = struct { .sqrt = .un_node, .sin = .un_node, .cos = .un_node, + .tan = .un_node, .exp = .un_node, .exp2 = .un_node, .log = .un_node, @@ -1928,6 +1921,16 @@ pub const Inst = struct { dbg_block_begin, /// Marks the end of a semantic scope for debug info variables. dbg_block_end, + /// Given a pointer to a struct or object that contains virtual fields, returns the + /// named field. If there is no named field, searches in the type for a decl that + /// matches the field name. The decl is resolved and we ensure that it's a function + /// which can accept the object as the first parameter, with one pointer fixup. If + /// all of that works, this instruction produces a special "bound function" value + /// which contains both the function and the saved first parameter value. + /// Bound functions may only be used as the function parameter to a `call` or + /// `builtin_call` instruction. Any other use is invalid zir and may crash the compiler. + /// Uses `pl_node` field. The AST node is the `@field` builtin. Payload is FieldNamedNode. + field_call_bind_named, pub const InstData = struct { opcode: Extended, @@ -2963,6 +2966,12 @@ pub const Inst = struct { field_name: Ref, }; + pub const FieldNamedNode = struct { + node: i32, + lhs: Ref, + field_name: Ref, + }; + pub const As = struct { dest_type: Ref, operand: Ref, diff --git a/src/arch/aarch64/CodeGen.zig b/src/arch/aarch64/CodeGen.zig index fc37ae00dd..5ed7b63db3 100644 --- a/src/arch/aarch64/CodeGen.zig +++ b/src/arch/aarch64/CodeGen.zig @@ -533,6 +533,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/arch/arm/CodeGen.zig b/src/arch/arm/CodeGen.zig index 54de053475..73f51f6481 100644 --- a/src/arch/arm/CodeGen.zig +++ b/src/arch/arm/CodeGen.zig @@ -571,6 +571,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig index 15377378cd..61fddee207 100644 --- a/src/arch/riscv64/CodeGen.zig +++ b/src/arch/riscv64/CodeGen.zig @@ -500,6 +500,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/arch/sparcv9/CodeGen.zig b/src/arch/sparcv9/CodeGen.zig index 7e1ecefbb7..bcd8cf8eeb 100644 --- a/src/arch/sparcv9/CodeGen.zig +++ b/src/arch/sparcv9/CodeGen.zig @@ -451,6 +451,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 8eadfe6cd8..5171dfb460 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1559,6 +1559,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 4097352975..0103f5382f 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -656,6 +656,7 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 464f144f5a..46fee271cc 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1749,6 +1749,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index de25a7821d..c9ea5bebac 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -3521,6 +3521,7 @@ pub const FuncGen = struct { .sqrt => try self.airUnaryOp(inst, .sqrt), .sin => try self.airUnaryOp(inst, .sin), .cos => try self.airUnaryOp(inst, .cos), + .tan => try self.airUnaryOp(inst, .tan), .exp => try self.airUnaryOp(inst, .exp), .exp2 => try self.airUnaryOp(inst, .exp2), .log => try self.airUnaryOp(inst, .log), @@ -5553,7 +5554,7 @@ pub const FuncGen = struct { fn libcFloatSuffix(float_bits: u16) []const u8 { return switch (float_bits) { 16 => "h", // Non-standard - 32 => "s", + 32 => "f", 64 => "", 80 => "x", // Non-standard 128 => "q", // Non-standard (mimics convention in GCC libquadmath) @@ -5661,6 +5662,7 @@ pub const FuncGen = struct { sin, sqrt, sub, + tan, trunc, }; @@ -5684,7 +5686,7 @@ pub const FuncGen = struct { const llvm_ty = try self.dg.llvmType(ty); const scalar_llvm_ty = try self.dg.llvmType(scalar_ty); - const intrinsics_allowed = intrinsicsAllowed(scalar_ty, target); + const intrinsics_allowed = op != .tan and intrinsicsAllowed(scalar_ty, target); var fn_name_buf: [64]u8 = undefined; const strat: FloatOpStrat = if (intrinsics_allowed) switch (op) { // Some operations are dedicated LLVM instructions, not available as intrinsics @@ -5720,6 +5722,7 @@ pub const FuncGen = struct { .round, .sin, .sqrt, + .tan, .trunc, => FloatOpStrat{ .libc = std.fmt.bufPrintZ(&fn_name_buf, "{s}{s}{s}", .{ diff --git a/src/print_air.zig b/src/print_air.zig index 27d222f262..6e336e138b 100644 --- a/src/print_air.zig +++ b/src/print_air.zig @@ -158,6 +158,7 @@ const Writer = struct { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, diff --git a/src/print_zir.zig b/src/print_zir.zig index e85e69fe7f..776aeffbdc 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -207,6 +207,7 @@ const Writer = struct { .sqrt, .sin, .cos, + .tan, .exp, .exp2, .log, @@ -400,7 +401,6 @@ const Writer = struct { .field_ptr_named, .field_val_named, - .field_call_bind_named, => try self.writePlNodeFieldNamed(stream, inst), .as_node => try self.writeAs(stream, inst), @@ -509,6 +509,16 @@ const Writer = struct { try stream.writeAll(")) "); try self.writeSrc(stream, src); }, + + .field_call_bind_named => { + const extra = self.code.extraData(Zir.Inst.FieldNamedNode, extended.operand).data; + const src: LazySrcLoc = .{ .node_offset = extra.node }; + try self.writeInstRef(stream, extra.lhs); + try stream.writeAll(", "); + try self.writeInstRef(stream, extra.field_name); + try stream.writeAll(") "); + try self.writeSrc(stream, src); + }, } } diff --git a/src/stage1/all_types.hpp b/src/stage1/all_types.hpp index cbefcd1078..398693e6d8 100644 --- a/src/stage1/all_types.hpp +++ b/src/stage1/all_types.hpp @@ -1768,6 +1768,7 @@ enum BuiltinFnId { BuiltinFnIdSqrt, BuiltinFnIdSin, BuiltinFnIdCos, + BuiltinFnIdTan, BuiltinFnIdExp, BuiltinFnIdExp2, BuiltinFnIdLog, diff --git a/src/stage1/analyze.cpp b/src/stage1/analyze.cpp index 73e3cd0da6..6e49c91fd8 100644 --- a/src/stage1/analyze.cpp +++ b/src/stage1/analyze.cpp @@ -10383,6 +10383,8 @@ const char *float_un_op_to_name(BuiltinFnId op) { return "sin"; case BuiltinFnIdCos: return "cos"; + case BuiltinFnIdTan: + return "tan"; case BuiltinFnIdExp: return "exp"; case BuiltinFnIdExp2: diff --git a/src/stage1/astgen.cpp b/src/stage1/astgen.cpp index 35566e2143..367bed69cf 100644 --- a/src/stage1/astgen.cpp +++ b/src/stage1/astgen.cpp @@ -4497,6 +4497,7 @@ static Stage1ZirInst *astgen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, Ast case BuiltinFnIdSqrt: case BuiltinFnIdSin: case BuiltinFnIdCos: + case BuiltinFnIdTan: case BuiltinFnIdExp: case BuiltinFnIdExp2: case BuiltinFnIdLog: diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 88e73baa3c..34ae82eb82 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -1629,11 +1629,28 @@ static const char *get_compiler_rt_type_abbrev(ZigType *type) { } } -static const char *get_math_h_type_abbrev(CodeGen *g, ZigType *float_type) { +static const char *libc_float_prefix(CodeGen *g, ZigType *float_type) { + if (float_type == g->builtin_types.entry_f16) + return "__"; + else if (float_type == g->builtin_types.entry_f32) + return ""; + else if (float_type == g->builtin_types.entry_f64) + return ""; + else if (float_type == g->builtin_types.entry_f80) + return "__"; + else if (float_type == g->builtin_types.entry_c_longdouble) + return "l"; + else if (float_type == g->builtin_types.entry_f128) + return ""; + else + zig_unreachable(); +} + +static const char *libc_float_suffix(CodeGen *g, ZigType *float_type) { if (float_type == g->builtin_types.entry_f16) return "h"; // Non-standard else if (float_type == g->builtin_types.entry_f32) - return "s"; + return "f"; else if (float_type == g->builtin_types.entry_f64) return ""; else if (float_type == g->builtin_types.entry_f80) @@ -2992,10 +3009,12 @@ static LLVMValueRef get_soft_float_fn(CodeGen *g, const char *name, int param_co static LLVMValueRef gen_soft_float_un_op(CodeGen *g, LLVMValueRef op, ZigType *operand_type, BuiltinFnId op_id) { uint32_t vector_len = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.len : 0; + ZigType *scalar_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type; char fn_name[64]; - sprintf(fn_name, "%s%s", float_un_op_to_name(op_id), get_math_h_type_abbrev(g, operand_type)); - LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, 1, operand_type->llvm_type, operand_type->llvm_type); + sprintf(fn_name, "%s%s%s", libc_float_prefix(g, scalar_type), + float_un_op_to_name(op_id), libc_float_suffix(g, scalar_type)); + LLVMValueRef func_ref = get_soft_float_fn(g, fn_name, 1, scalar_type->llvm_type, scalar_type->llvm_type); LLVMValueRef result; if (vector_len == 0) { @@ -3018,7 +3037,9 @@ static LLVMValueRef gen_float_un_op(CodeGen *g, LLVMValueRef operand, ZigType *o assert(operand_type->id == ZigTypeIdFloat || operand_type->id == ZigTypeIdVector); ZigType *elem_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type; if ((elem_type == g->builtin_types.entry_f80 && !target_has_f80(g->zig_target)) || - (elem_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target))) { + (elem_type == g->builtin_types.entry_f128 && !target_long_double_is_f128(g->zig_target)) || + op == BuiltinFnIdTan) + { return gen_soft_float_un_op(g, operand, operand_type, op); } LLVMValueRef float_op_fn = get_float_fn(g, operand_type, ZigLLVMFnIdFloatOp, op); @@ -3466,7 +3487,8 @@ static LLVMValueRef gen_soft_float_bin_op(CodeGen *g, LLVMValueRef op1_value, LL int param_count = 2; const char *compiler_rt_type_abbrev = get_compiler_rt_type_abbrev(operand_type); - const char *math_h_type_abbrev = get_math_h_type_abbrev(g, operand_type); + const char *math_float_prefix = libc_float_prefix(g, operand_type); + const char *math_float_suffix = libc_float_suffix(g, operand_type); char fn_name[64]; Icmp res_icmp = NONE; @@ -3523,10 +3545,10 @@ static LLVMValueRef gen_soft_float_bin_op(CodeGen *g, LLVMValueRef op1_value, LL res_icmp = EQ_ONE; break; case IrBinOpMaximum: - sprintf(fn_name, "fmax%s", math_h_type_abbrev); + sprintf(fn_name, "%sfmax%s", math_float_prefix, math_float_suffix); break; case IrBinOpMinimum: - sprintf(fn_name, "fmin%s", math_h_type_abbrev); + sprintf(fn_name, "%sfmin%s", math_float_prefix, math_float_suffix); break; case IrBinOpMult: sprintf(fn_name, "__mul%sf3", compiler_rt_type_abbrev); @@ -3545,7 +3567,7 @@ static LLVMValueRef gen_soft_float_bin_op(CodeGen *g, LLVMValueRef op1_value, LL break; case IrBinOpRemRem: case IrBinOpRemMod: - sprintf(fn_name, "fmod%s", math_h_type_abbrev); + sprintf(fn_name, "%sfmod%s", math_float_prefix, math_float_suffix); break; default: zig_unreachable(); @@ -9810,6 +9832,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdSqrt, "sqrt", 1); create_builtin_fn(g, BuiltinFnIdSin, "sin", 1); create_builtin_fn(g, BuiltinFnIdCos, "cos", 1); + create_builtin_fn(g, BuiltinFnIdTan, "tan", 1); create_builtin_fn(g, BuiltinFnIdExp, "exp", 1); create_builtin_fn(g, BuiltinFnIdExp2, "exp2", 1); create_builtin_fn(g, BuiltinFnIdLog, "log", 1); diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 874d068c03..1eef354864 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -24132,6 +24132,9 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_ case BuiltinFnIdCos: out_val->data.x_f16 = zig_double_to_f16(cos(zig_f16_to_double(op->data.x_f16))); break; + case BuiltinFnIdTan: + out_val->data.x_f16 = zig_double_to_f16(tan(zig_f16_to_double(op->data.x_f16))); + break; case BuiltinFnIdExp: out_val->data.x_f16 = zig_double_to_f16(exp(zig_f16_to_double(op->data.x_f16))); break; @@ -24181,6 +24184,9 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_ case BuiltinFnIdCos: out_val->data.x_f32 = cosf(op->data.x_f32); break; + case BuiltinFnIdTan: + out_val->data.x_f32 = tanf(op->data.x_f32); + break; case BuiltinFnIdExp: out_val->data.x_f32 = expf(op->data.x_f32); break; @@ -24230,6 +24236,9 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_ case BuiltinFnIdCos: out_val->data.x_f64 = cos(op->data.x_f64); break; + case BuiltinFnIdTan: + out_val->data.x_f64 = tan(op->data.x_f64); + break; case BuiltinFnIdExp: out_val->data.x_f64 = exp(op->data.x_f64); break; @@ -24293,6 +24302,7 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_ case BuiltinFnIdNearbyInt: case BuiltinFnIdSin: case BuiltinFnIdCos: + case BuiltinFnIdTan: case BuiltinFnIdExp: case BuiltinFnIdExp2: case BuiltinFnIdLog: @@ -24337,6 +24347,7 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_ case BuiltinFnIdNearbyInt: case BuiltinFnIdSin: case BuiltinFnIdCos: + case BuiltinFnIdTan: case BuiltinFnIdExp: case BuiltinFnIdExp2: case BuiltinFnIdLog: diff --git a/src/value.zig b/src/value.zig index e951b075c0..a39984d1d4 100644 --- a/src/value.zig +++ b/src/value.zig @@ -4473,6 +4473,44 @@ pub const Value = extern union { } } + pub fn tan(val: Value, float_type: Type, arena: Allocator, target: Target) Allocator.Error!Value { + if (float_type.zigTypeTag() == .Vector) { + const result_data = try arena.alloc(Value, float_type.vectorLen()); + for (result_data) |*scalar, i| { + scalar.* = try tanScalar(val.indexVectorlike(i), float_type.scalarType(), arena, target); + } + return Value.Tag.aggregate.create(arena, result_data); + } + return tanScalar(val, float_type, arena, target); + } + + pub fn tanScalar(val: Value, float_type: Type, arena: Allocator, target: Target) Allocator.Error!Value { + switch (float_type.floatBits(target)) { + 16 => { + const f = val.toFloat(f16); + return Value.Tag.float_16.create(arena, @tan(f)); + }, + 32 => { + const f = val.toFloat(f32); + return Value.Tag.float_32.create(arena, @tan(f)); + }, + 64 => { + const f = val.toFloat(f64); + return Value.Tag.float_64.create(arena, @tan(f)); + }, + 80 => { + const f = val.toFloat(f80); + return Value.Tag.float_80.create(arena, @tan(f)); + }, + 128 => { + const f = val.toFloat(f128); + return Value.Tag.float_128.create(arena, @tan(f)); + }, + else => unreachable, + } + } + + pub fn exp(val: Value, float_type: Type, arena: Allocator, target: Target) Allocator.Error!Value { if (float_type.zigTypeTag() == .Vector) { const result_data = try arena.alloc(Value, float_type.vectorLen()); diff --git a/test/behavior/bugs/920.zig b/test/behavior/bugs/920.zig index 380d42e5de..5a7cadc595 100644 --- a/test/behavior/bugs/920.zig +++ b/test/behavior/bugs/920.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const math = std.math; const Random = std.rand.Random; const ZigTable = struct { @@ -40,10 +39,10 @@ const norm_r = 3.6541528853610088; const norm_v = 0.00492867323399; fn norm_f(x: f64) f64 { - return math.exp(-x * x / 2.0); + return @exp(-x * x / 2.0); } fn norm_f_inv(y: f64) f64 { - return math.sqrt(-2.0 * math.ln(y)); + return @sqrt(-2.0 * @log(y)); } fn norm_zero_case(random: *Random, u: f64) f64 { _ = random; -- cgit v1.2.3 From 1ac21cdec5747e2c7788c566a2998b2bee54eb47 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 27 Apr 2022 18:14:44 -0700 Subject: compiler-rt: avoid symbol conflicts Weak aliases don't work on Windows, so we avoid exporting the `l` alias on this platform for functions we know will collide. --- lib/std/special/compiler_rt.zig | 54 ++++++++++++++++++++++------------------- src/stage1/codegen.cpp | 47 +++++++++++++++-------------------- 2 files changed, 48 insertions(+), 53 deletions(-) (limited to 'src/stage1/codegen.cpp') diff --git a/lib/std/special/compiler_rt.zig b/lib/std/special/compiler_rt.zig index dccb9264bd..f0b0c49152 100644 --- a/lib/std/special/compiler_rt.zig +++ b/lib/std/special/compiler_rt.zig @@ -723,25 +723,25 @@ comptime { @export(_aullrem, .{ .name = "\x01__aullrem", .linkage = strong_linkage }); } - mathExport("ceil", @import("./compiler_rt/ceil.zig")); - mathExport("cos", @import("./compiler_rt/cos.zig")); - mathExport("exp", @import("./compiler_rt/exp.zig")); - mathExport("exp2", @import("./compiler_rt/exp2.zig")); - mathExport("fabs", @import("./compiler_rt/fabs.zig")); - mathExport("floor", @import("./compiler_rt/floor.zig")); - mathExport("fma", @import("./compiler_rt/fma.zig")); - mathExport("fmax", @import("./compiler_rt/fmax.zig")); - mathExport("fmin", @import("./compiler_rt/fmin.zig")); - mathExport("fmod", @import("./compiler_rt/fmod.zig")); - mathExport("log", @import("./compiler_rt/log.zig")); - mathExport("log10", @import("./compiler_rt/log10.zig")); - mathExport("log2", @import("./compiler_rt/log2.zig")); - mathExport("round", @import("./compiler_rt/round.zig")); - mathExport("sin", @import("./compiler_rt/sin.zig")); - mathExport("sincos", @import("./compiler_rt/sincos.zig")); - mathExport("sqrt", @import("./compiler_rt/sqrt.zig")); - mathExport("tan", @import("./compiler_rt/tan.zig")); - mathExport("trunc", @import("./compiler_rt/trunc.zig")); + mathExport("ceil", @import("./compiler_rt/ceil.zig"), true); + mathExport("cos", @import("./compiler_rt/cos.zig"), true); + mathExport("exp", @import("./compiler_rt/exp.zig"), true); + mathExport("exp2", @import("./compiler_rt/exp2.zig"), true); + mathExport("fabs", @import("./compiler_rt/fabs.zig"), true); + mathExport("floor", @import("./compiler_rt/floor.zig"), true); + mathExport("fma", @import("./compiler_rt/fma.zig"), true); + mathExport("fmax", @import("./compiler_rt/fmax.zig"), true); + mathExport("fmin", @import("./compiler_rt/fmin.zig"), true); + mathExport("fmod", @import("./compiler_rt/fmod.zig"), true); + mathExport("log", @import("./compiler_rt/log.zig"), true); + mathExport("log10", @import("./compiler_rt/log10.zig"), true); + mathExport("log2", @import("./compiler_rt/log2.zig"), true); + mathExport("round", @import("./compiler_rt/round.zig"), true); + mathExport("sin", @import("./compiler_rt/sin.zig"), true); + mathExport("sincos", @import("./compiler_rt/sincos.zig"), true); + mathExport("sqrt", @import("./compiler_rt/sqrt.zig"), true); + mathExport("tan", @import("./compiler_rt/tan.zig"), false); + mathExport("trunc", @import("./compiler_rt/trunc.zig"), true); if (arch.isSPARC()) { // SPARC systems use a different naming scheme @@ -827,7 +827,7 @@ comptime { } } -inline fn mathExport(double_name: []const u8, comptime import: type) void { +inline fn mathExport(double_name: []const u8, comptime import: type, is_standard: bool) void { const half_name = "__" ++ double_name ++ "h"; const half_fn = @field(import, half_name); const float_name = double_name ++ "f"; @@ -853,11 +853,15 @@ inline fn mathExport(double_name: []const u8, comptime import: type) void { .{ f128, quad_fn }, }; - inline for (pairs) |pair| { - const F = pair[0]; - const func = pair[1]; - if (builtin.target.longDoubleIs(F)) { - @export(func, .{ .name = long_double_name, .linkage = linkage }); + // Weak aliases don't work on Windows, so we avoid exporting the `l` alias + // on this platform for functions we know will collide. + if (builtin.os.tag != .windows or !builtin.link_libc or !is_standard) { + inline for (pairs) |pair| { + const F = pair[0]; + const func = pair[1]; + if (builtin.target.longDoubleIs(F)) { + @export(func, .{ .name = long_double_name, .linkage = linkage }); + } } } } diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index 34ae82eb82..c575aff53d 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -1630,37 +1630,28 @@ static const char *get_compiler_rt_type_abbrev(ZigType *type) { } static const char *libc_float_prefix(CodeGen *g, ZigType *float_type) { - if (float_type == g->builtin_types.entry_f16) - return "__"; - else if (float_type == g->builtin_types.entry_f32) - return ""; - else if (float_type == g->builtin_types.entry_f64) - return ""; - else if (float_type == g->builtin_types.entry_f80) - return "__"; - else if (float_type == g->builtin_types.entry_c_longdouble) - return "l"; - else if (float_type == g->builtin_types.entry_f128) - return ""; - else - zig_unreachable(); + switch (float_type->data.floating.bit_count) { + case 16: + case 80: + return "__"; + case 32: + case 64: + case 128: + return ""; + default: + zig_unreachable(); + } } static const char *libc_float_suffix(CodeGen *g, ZigType *float_type) { - if (float_type == g->builtin_types.entry_f16) - return "h"; // Non-standard - else if (float_type == g->builtin_types.entry_f32) - return "f"; - else if (float_type == g->builtin_types.entry_f64) - return ""; - else if (float_type == g->builtin_types.entry_f80) - return "x"; // Non-standard - else if (float_type == g->builtin_types.entry_c_longdouble) - return "l"; - else if (float_type == g->builtin_types.entry_f128) - return "q"; // Non-standard - else - zig_unreachable(); + switch (float_type->size_in_bits) { + case 16: return "h"; // Non-standard + case 32: return "f"; + case 64: return ""; + case 80: return "x"; // Non-standard + case 128: return "q"; // Non-standard + default: zig_unreachable(); + } } static LLVMValueRef gen_soft_float_widen_or_shorten(CodeGen *g, ZigType *actual_type, -- cgit v1.2.3 From 9d098657a069b36e3eed9bc63c3421c031be7348 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 27 Apr 2022 22:57:12 -0700 Subject: stage1: fix i386-windows f80 sizeof/alignof --- src/stage1/codegen.cpp | 7 +++++-- test/behavior/math.zig | 6 ------ 2 files changed, 5 insertions(+), 8 deletions(-) (limited to 'src/stage1/codegen.cpp') diff --git a/src/stage1/codegen.cpp b/src/stage1/codegen.cpp index c575aff53d..9d46a660bc 100644 --- a/src/stage1/codegen.cpp +++ b/src/stage1/codegen.cpp @@ -9586,10 +9586,13 @@ static void define_builtin_types(CodeGen *g) { switch (g->zig_target->arch) { case ZigLLVM_x86: case ZigLLVM_x86_64: - if (g->zig_target->abi != ZigLLVM_MSVC) + if (g->zig_target->abi != ZigLLVM_MSVC) { add_fp_entry(g, "c_longdouble", 80, LLVMX86FP80Type(), &g->builtin_types.entry_c_longdouble); - else + g->builtin_types.entry_c_longdouble->abi_size = g->builtin_types.entry_f80->abi_size; + g->builtin_types.entry_c_longdouble->abi_align = g->builtin_types.entry_f80->abi_align; + } else { add_fp_entry(g, "c_longdouble", 64, LLVMDoubleType(), &g->builtin_types.entry_c_longdouble); + } break; case ZigLLVM_arm: case ZigLLVM_armeb: diff --git a/test/behavior/math.zig b/test/behavior/math.zig index 85cf7f5643..0479015eee 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -1416,12 +1416,6 @@ test "fabs" { if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage1 and builtin.os.tag == .windows and - builtin.cpu.arch == .i386) - { - return error.SkipZigTest; - } - inline for ([_]type{ f16, f32, f64, f80, f128, c_longdouble }) |T| { // normals try expect(@fabs(@as(T, 1.0)) == 1.0); -- cgit v1.2.3