aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-04-28 13:34:38 -0400
committerGitHub <noreply@github.com>2022-04-28 13:34:38 -0400
commit360ecc1a2f72967f3a3882b3327e130bdc4e18c0 (patch)
treec02dfab372e5b79bc2130d666c5e0a0e5cb3af2e /src
parentd5fcb509881e1b022d2bcef303b53b4f67db1c9a (diff)
parent11911f55a73a49e2fda85bddd38d1993b93547c9 (diff)
downloadzig-360ecc1a2f72967f3a3882b3327e130bdc4e18c0.tar.gz
zig-360ecc1a2f72967f3a3882b3327e130bdc4e18c0.zip
Merge pull request #11532 from ziglang/compiler-rt-math
compiler-rt math functions reorg
Diffstat (limited to 'src')
-rw-r--r--src/Air.zig8
-rw-r--r--src/AstGen.zig6
-rw-r--r--src/BuiltinFn.zig8
-rw-r--r--src/Liveness.zig1
-rw-r--r--src/Sema.zig74
-rw-r--r--src/Zir.zig33
-rw-r--r--src/arch/aarch64/CodeGen.zig1
-rw-r--r--src/arch/arm/CodeGen.zig1
-rw-r--r--src/arch/riscv64/CodeGen.zig1
-rw-r--r--src/arch/sparcv9/CodeGen.zig1
-rw-r--r--src/arch/wasm/CodeGen.zig1
-rw-r--r--src/arch/x86_64/CodeGen.zig1
-rw-r--r--src/codegen/c.zig1
-rw-r--r--src/codegen/llvm.zig443
-rw-r--r--src/codegen/llvm/bindings.zig8
-rw-r--r--src/print_air.zig1
-rw-r--r--src/print_zir.zig12
-rw-r--r--src/stage1/all_types.hpp1
-rw-r--r--src/stage1/analyze.cpp8
-rw-r--r--src/stage1/analyze.hpp2
-rw-r--r--src/stage1/astgen.cpp1
-rw-r--r--src/stage1/codegen.cpp725
-rw-r--r--src/stage1/ir.cpp106
-rw-r--r--src/stage1/ir_print.cpp4
-rw-r--r--src/translate_c.zig2
-rw-r--r--src/value.zig149
-rw-r--r--src/zig_llvm.cpp4
-rw-r--r--src/zig_llvm.h1
28 files changed, 963 insertions, 641 deletions
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,
@@ -772,6 +773,13 @@ pub const list = list: {
},
},
.{
+ "@tan",
+ .{
+ .tag = .tan,
+ .param_count = 1,
+ },
+ },
+ .{
"@exp",
.{
.tag = .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 8abcbd47ed..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);
@@ -14051,7 +14051,7 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
const result_val = val.floatToInt(sema.arena, operand_ty, dest_ty, target) catch |err| switch (err) {
error.FloatCannotFit => {
return sema.fail(block, operand_src, "integer value {d} cannot be stored in type '{}'", .{
- std.math.floor(val.toFloat(f64)),
+ @floor(val.toFloat(f64)),
dest_ty.fmt(sema.mod),
});
},
@@ -18371,7 +18371,7 @@ fn coerce(
}
const result_val = val.floatToInt(sema.arena, inst_ty, dest_ty, target) catch |err| switch (err) {
error.FloatCannotFit => {
- return sema.fail(block, inst_src, "integer value {d} cannot be stored in type '{}'", .{ std.math.floor(val.toFloat(f64)), dest_ty.fmt(sema.mod) });
+ return sema.fail(block, inst_src, "integer value {d} cannot be stored in type '{}'", .{ @floor(val.toFloat(f64)), dest_ty.fmt(sema.mod) });
},
else => |e| return e,
};
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 b2abc536ef..c9ea5bebac 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -3518,19 +3518,20 @@ pub const FuncGen = struct {
.shr => try self.airShr(inst, false),
.shr_exact => try self.airShr(inst, true),
- .sqrt => try self.airUnaryOp(inst, "sqrt"),
- .sin => try self.airUnaryOp(inst, "sin"),
- .cos => try self.airUnaryOp(inst, "cos"),
- .exp => try self.airUnaryOp(inst, "exp"),
- .exp2 => try self.airUnaryOp(inst, "exp2"),
- .log => try self.airUnaryOp(inst, "log"),
- .log2 => try self.airUnaryOp(inst, "log2"),
- .log10 => try self.airUnaryOp(inst, "log10"),
- .fabs => try self.airUnaryOp(inst, "fabs"),
- .floor => try self.airUnaryOp(inst, "floor"),
- .ceil => try self.airUnaryOp(inst, "ceil"),
- .round => try self.airUnaryOp(inst, "round"),
- .trunc_float => try self.airUnaryOp(inst, "trunc"),
+ .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),
+ .log2 => try self.airUnaryOp(inst, .log2),
+ .log10 => try self.airUnaryOp(inst, .log10),
+ .fabs => try self.airUnaryOp(inst, .fabs),
+ .floor => try self.airUnaryOp(inst, .floor),
+ .ceil => try self.airUnaryOp(inst, .ceil),
+ .round => try self.airUnaryOp(inst, .round),
+ .trunc_float => try self.airUnaryOp(inst, .trunc),
.cmp_eq => try self.airCmp(inst, .eq),
.cmp_gt => try self.airCmp(inst, .gt),
@@ -3905,7 +3906,7 @@ pub const FuncGen = struct {
rhs: *const llvm.Value,
operand_ty: Type,
op: math.CompareOperator,
- ) *const llvm.Value {
+ ) Allocator.Error!*const llvm.Value {
var int_buffer: Type.Payload.Bits = undefined;
var opt_buffer: Type.Payload.ElemType = undefined;
@@ -3947,7 +3948,7 @@ pub const FuncGen = struct {
self.builder.positionBuilderAtEnd(both_pl_block);
const lhs_payload = self.optPayloadHandle(lhs, is_by_ref);
const rhs_payload = self.optPayloadHandle(rhs, is_by_ref);
- const payload_cmp = self.cmp(lhs_payload, rhs_payload, payload_ty, op);
+ const payload_cmp = try self.cmp(lhs_payload, rhs_payload, payload_ty, op);
_ = self.builder.buildBr(end_block);
const both_pl_block_end = self.builder.getInsertBlock();
@@ -3983,17 +3984,7 @@ pub const FuncGen = struct {
);
return phi_node;
},
- .Float => {
- const operation: llvm.RealPredicate = switch (op) {
- .eq => .OEQ,
- .neq => .UNE,
- .lt => .OLT,
- .lte => .OLE,
- .gt => .OGT,
- .gte => .OGE,
- };
- return self.builder.buildFCmp(operation, lhs, rhs, "");
- },
+ .Float => return self.buildFloatCmp(op, operand_ty, .{ lhs, rhs }),
else => unreachable,
};
const is_signed = int_ty.isSignedInt();
@@ -5221,7 +5212,7 @@ pub const FuncGen = struct {
const inst_ty = self.air.typeOfIndex(inst);
const scalar_ty = inst_ty.scalarType();
- if (scalar_ty.isAnyFloat()) return self.builder.buildFAdd(lhs, rhs, "");
+ if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.add, inst_ty, 2, .{ lhs, rhs });
if (scalar_ty.isSignedInt()) return self.builder.buildNSWAdd(lhs, rhs, "");
return self.builder.buildNUWAdd(lhs, rhs, "");
}
@@ -5260,7 +5251,7 @@ pub const FuncGen = struct {
const inst_ty = self.air.typeOfIndex(inst);
const scalar_ty = inst_ty.scalarType();
- if (scalar_ty.isAnyFloat()) return self.builder.buildFSub(lhs, rhs, "");
+ if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.sub, inst_ty, 2, .{ lhs, rhs });
if (scalar_ty.isSignedInt()) return self.builder.buildNSWSub(lhs, rhs, "");
return self.builder.buildNUWSub(lhs, rhs, "");
}
@@ -5298,7 +5289,7 @@ pub const FuncGen = struct {
const inst_ty = self.air.typeOfIndex(inst);
const scalar_ty = inst_ty.scalarType();
- if (scalar_ty.isAnyFloat()) return self.builder.buildFMul(lhs, rhs, "");
+ if (scalar_ty.isAnyFloat()) return self.buildFloatOp(.mul, inst_ty, 2, .{ lhs, rhs });
if (scalar_ty.isSignedInt()) return self.builder.buildNSWMul(lhs, rhs, "");
return self.builder.buildNUWMul(lhs, rhs, "");
}
@@ -5333,8 +5324,9 @@ pub const FuncGen = struct {
const bin_op = self.air.instructions.items(.data)[inst].bin_op;
const lhs = try self.resolveInst(bin_op.lhs);
const rhs = try self.resolveInst(bin_op.rhs);
+ const inst_ty = self.air.typeOfIndex(inst);
- return self.builder.buildFDiv(lhs, rhs, "");
+ return self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs });
}
fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
@@ -5347,8 +5339,8 @@ pub const FuncGen = struct {
const scalar_ty = inst_ty.scalarType();
if (scalar_ty.isRuntimeFloat()) {
- const result = self.builder.buildFDiv(lhs, rhs, "");
- return self.callTrunc(result, inst_ty);
+ const result = try self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs });
+ return self.buildFloatOp(.trunc, inst_ty, 1, .{result});
}
if (scalar_ty.isSignedInt()) return self.builder.buildSDiv(lhs, rhs, "");
return self.builder.buildUDiv(lhs, rhs, "");
@@ -5364,8 +5356,8 @@ pub const FuncGen = struct {
const scalar_ty = inst_ty.scalarType();
if (scalar_ty.isRuntimeFloat()) {
- const result = self.builder.buildFDiv(lhs, rhs, "");
- return try self.callFloor(result, inst_ty);
+ const result = try self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs });
+ return self.buildFloatOp(.floor, inst_ty, 1, .{result});
}
if (scalar_ty.isSignedInt()) {
// const d = @divTrunc(a, b);
@@ -5395,7 +5387,7 @@ pub const FuncGen = struct {
const inst_ty = self.air.typeOfIndex(inst);
const scalar_ty = inst_ty.scalarType();
- if (scalar_ty.isRuntimeFloat()) return self.builder.buildFDiv(lhs, rhs, "");
+ if (scalar_ty.isRuntimeFloat()) return self.buildFloatOp(.div, inst_ty, 2, .{ lhs, rhs });
if (scalar_ty.isSignedInt()) return self.builder.buildExactSDiv(lhs, rhs, "");
return self.builder.buildExactUDiv(lhs, rhs, "");
}
@@ -5409,7 +5401,7 @@ pub const FuncGen = struct {
const inst_ty = self.air.typeOfIndex(inst);
const scalar_ty = inst_ty.scalarType();
- if (scalar_ty.isRuntimeFloat()) return self.builder.buildFRem(lhs, rhs, "");
+ if (scalar_ty.isRuntimeFloat()) return self.buildFloatOp(.fmod, inst_ty, 2, .{ lhs, rhs });
if (scalar_ty.isSignedInt()) return self.builder.buildSRem(lhs, rhs, "");
return self.builder.buildURem(lhs, rhs, "");
}
@@ -5425,11 +5417,11 @@ pub const FuncGen = struct {
const scalar_ty = inst_ty.scalarType();
if (scalar_ty.isRuntimeFloat()) {
- const a = self.builder.buildFRem(lhs, rhs, "");
- const b = self.builder.buildFAdd(a, rhs, "");
- const c = self.builder.buildFRem(b, rhs, "");
+ const a = try self.buildFloatOp(.fmod, inst_ty, 2, .{ lhs, rhs });
+ const b = try self.buildFloatOp(.add, inst_ty, 2, .{ a, rhs });
+ const c = try self.buildFloatOp(.fmod, inst_ty, 2, .{ b, rhs });
const zero = inst_llvm_ty.constNull();
- const ltz = self.builder.buildFCmp(.OLT, lhs, zero, "");
+ const ltz = try self.buildFloatCmp(.lt, inst_ty, .{ lhs, zero });
return self.builder.buildSelect(ltz, c, a, "");
}
if (scalar_ty.isSignedInt()) {
@@ -5508,75 +5500,266 @@ pub const FuncGen = struct {
return result_struct;
}
- fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
- if (self.liveness.isUnused(inst)) return null;
+ fn buildElementwiseCall(
+ self: *FuncGen,
+ llvm_fn: *const llvm.Value,
+ args_vectors: []const *const llvm.Value,
+ result_vector: *const llvm.Value,
+ vector_len: usize,
+ ) !*const llvm.Value {
+ const args_len = @intCast(c_uint, args_vectors.len);
+ const llvm_i32 = self.context.intType(32);
+ assert(args_len <= 3);
- const pl_op = self.air.instructions.items(.data)[inst].pl_op;
- const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
+ var i: usize = 0;
+ var result = result_vector;
+ while (i < vector_len) : (i += 1) {
+ const index_i32 = llvm_i32.constInt(i, .False);
- const mulend1 = try self.resolveInst(extra.lhs);
- const mulend2 = try self.resolveInst(extra.rhs);
- const addend = try self.resolveInst(pl_op.operand);
+ var args: [3]*const llvm.Value = undefined;
+ for (args_vectors) |arg_vector, k| {
+ args[k] = self.builder.buildExtractElement(arg_vector, index_i32, "");
+ }
+ const result_elem = self.builder.buildCall(llvm_fn, &args, args_len, .C, .Auto, "");
+ result = self.builder.buildInsertElement(result, result_elem, index_i32, "");
+ }
+ return result;
+ }
- const ty = self.air.typeOfIndex(inst);
- const llvm_ty = try self.dg.llvmType(ty);
- const scalar_ty = ty.scalarType();
- const target = self.dg.module.getTarget();
+ fn getLibcFunction(
+ self: *FuncGen,
+ fn_name: [:0]const u8,
+ param_types: []const *const llvm.Type,
+ return_type: *const llvm.Type,
+ ) *const llvm.Value {
+ return self.dg.object.llvm_module.getNamedFunction(fn_name.ptr) orelse b: {
+ const alias = self.dg.object.llvm_module.getNamedGlobalAlias(fn_name.ptr, fn_name.len);
+ break :b if (alias) |a| a.getAliasee() else null;
+ } orelse b: {
+ const params_len = @intCast(c_uint, param_types.len);
+ const fn_type = llvm.functionType(return_type, param_types.ptr, params_len, .False);
+ const f = self.dg.object.llvm_module.addFunction(fn_name, fn_type);
+ break :b f;
+ };
+ }
+
+ fn libcFloatPrefix(float_bits: u16) []const u8 {
+ return switch (float_bits) {
+ 16, 80 => "__",
+ 32, 64, 128 => "",
+ else => unreachable,
+ };
+ }
- const Strat = union(enum) {
- intrinsic,
- libc: [*:0]const u8,
+ fn libcFloatSuffix(float_bits: u16) []const u8 {
+ return switch (float_bits) {
+ 16 => "h", // Non-standard
+ 32 => "f",
+ 64 => "",
+ 80 => "x", // Non-standard
+ 128 => "q", // Non-standard (mimics convention in GCC libquadmath)
+ else => unreachable,
};
+ }
- const strat: Strat = switch (scalar_ty.floatBits(target)) {
- 16, 32, 64 => Strat.intrinsic,
- 80 => if (CType.longdouble.sizeInBits(target) == 80) Strat{ .intrinsic = {} } else Strat{ .libc = "__fmax" },
- // LLVM always lowers the fma builtin for f128 to fmal, which is for `long double`.
- // On some targets this will be correct; on others it will be incorrect.
- 128 => if (CType.longdouble.sizeInBits(target) == 128) Strat{ .intrinsic = {} } else Strat{ .libc = "fmaq" },
+ fn compilerRtFloatAbbrev(float_bits: u16) []const u8 {
+ return switch (float_bits) {
+ 16 => "h",
+ 32 => "s",
+ 64 => "d",
+ 80 => "x",
+ 128 => "t",
else => unreachable,
};
+ }
- switch (strat) {
- .intrinsic => {
- const llvm_fn = self.getIntrinsic("llvm.fma", &.{llvm_ty});
- const params = [_]*const llvm.Value{ mulend1, mulend2, addend };
- return self.builder.buildCall(llvm_fn, &params, params.len, .C, .Auto, "");
- },
- .libc => |fn_name| {
- const scalar_llvm_ty = try self.dg.llvmType(scalar_ty);
- const llvm_fn = self.dg.object.llvm_module.getNamedFunction(fn_name) orelse b: {
- const param_types = [_]*const llvm.Type{ scalar_llvm_ty, scalar_llvm_ty, scalar_llvm_ty };
- const fn_type = llvm.functionType(scalar_llvm_ty, &param_types, param_types.len, .False);
- break :b self.dg.object.llvm_module.addFunction(fn_name, fn_type);
- };
+ /// Creates a floating point comparison by lowering to the appropriate
+ /// hardware instruction or softfloat routine for the target
+ fn buildFloatCmp(
+ self: *FuncGen,
+ pred: math.CompareOperator,
+ ty: Type,
+ params: [2]*const llvm.Value,
+ ) !*const llvm.Value {
+ const target = self.dg.module.getTarget();
+ const scalar_ty = ty.scalarType();
+ const scalar_llvm_ty = try self.dg.llvmType(scalar_ty);
- if (ty.zigTypeTag() == .Vector) {
- const llvm_i32 = self.context.intType(32);
- const vector_llvm_ty = try self.dg.llvmType(ty);
+ if (intrinsicsAllowed(scalar_ty, target)) {
+ const llvm_predicate: llvm.RealPredicate = switch (pred) {
+ .eq => .OEQ,
+ .neq => .UNE,
+ .lt => .OLT,
+ .lte => .OLE,
+ .gt => .OGT,
+ .gte => .OGE,
+ };
+ return self.builder.buildFCmp(llvm_predicate, params[0], params[1], "");
+ }
+
+ const float_bits = scalar_ty.floatBits(target);
+ const compiler_rt_float_abbrev = compilerRtFloatAbbrev(float_bits);
+ var fn_name_buf: [64]u8 = undefined;
+ const fn_base_name = switch (pred) {
+ .neq => "ne",
+ .eq => "eq",
+ .lt => "lt",
+ .lte => "le",
+ .gt => "gt",
+ .gte => "ge",
+ };
+ const fn_name = std.fmt.bufPrintZ(&fn_name_buf, "__{s}{s}f2", .{
+ fn_base_name, compiler_rt_float_abbrev,
+ }) catch unreachable;
- var i: usize = 0;
- var vector = vector_llvm_ty.getUndef();
- while (i < ty.vectorLen()) : (i += 1) {
- const index_i32 = llvm_i32.constInt(i, .False);
+ const param_types = [2]*const llvm.Type{ scalar_llvm_ty, scalar_llvm_ty };
+ const llvm_i32 = self.context.intType(32);
+ const libc_fn = self.getLibcFunction(fn_name, param_types[0..], llvm_i32);
- const mulend1_elem = self.builder.buildExtractElement(mulend1, index_i32, "");
- const mulend2_elem = self.builder.buildExtractElement(mulend2, index_i32, "");
- const addend_elem = self.builder.buildExtractElement(addend, index_i32, "");
+ const zero = llvm_i32.constInt(0, .False);
+ const int_pred: llvm.IntPredicate = switch (pred) {
+ .eq => .EQ,
+ .neq => .NE,
+ .lt => .SLT,
+ .lte => .SLE,
+ .gt => .SGT,
+ .gte => .SGE,
+ };
- const params = [_]*const llvm.Value{ mulend1_elem, mulend2_elem, addend_elem };
- const mul_add = self.builder.buildCall(llvm_fn, &params, params.len, .C, .Auto, "");
+ if (ty.zigTypeTag() == .Vector) {
+ const vec_len = ty.vectorLen();
+ const vector_result_ty = llvm_i32.vectorType(vec_len);
+
+ var result = vector_result_ty.getUndef();
+ result = try self.buildElementwiseCall(libc_fn, &params, result, vec_len);
+
+ const zero_vector = self.builder.buildVectorSplat(vec_len, zero, "");
+ return self.builder.buildICmp(int_pred, result, zero_vector, "");
+ }
+
+ const result = self.builder.buildCall(libc_fn, &params, params.len, .C, .Auto, "");
+ return self.builder.buildICmp(int_pred, result, zero, "");
+ }
+
+ const FloatOp = enum {
+ add,
+ ceil,
+ cos,
+ div,
+ exp,
+ exp2,
+ fabs,
+ floor,
+ fma,
+ log,
+ log10,
+ log2,
+ fmax,
+ fmin,
+ mul,
+ fmod,
+ round,
+ sin,
+ sqrt,
+ sub,
+ tan,
+ trunc,
+ };
- vector = self.builder.buildInsertElement(vector, mul_add, index_i32, "");
- }
+ const FloatOpStrat = union(enum) {
+ intrinsic: []const u8,
+ libc: [:0]const u8,
+ };
- return vector;
- } else {
- const params = [_]*const llvm.Value{ mulend1, mulend2, addend };
- return self.builder.buildCall(llvm_fn, &params, params.len, .C, .Auto, "");
+ /// Creates a floating point operation (add, sub, fma, sqrt, exp, etc.)
+ /// by lowering to the appropriate hardware instruction or softfloat
+ /// routine for the target
+ fn buildFloatOp(
+ self: *FuncGen,
+ comptime op: FloatOp,
+ ty: Type,
+ comptime params_len: usize,
+ params: [params_len]*const llvm.Value,
+ ) !*const llvm.Value {
+ const target = self.dg.module.getTarget();
+ const scalar_ty = ty.scalarType();
+ const llvm_ty = try self.dg.llvmType(ty);
+ const scalar_llvm_ty = try self.dg.llvmType(scalar_ty);
+
+ 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
+ .add => return self.builder.buildFAdd(params[0], params[1], ""),
+ .sub => return self.builder.buildFSub(params[0], params[1], ""),
+ .mul => return self.builder.buildFMul(params[0], params[1], ""),
+ .div => return self.builder.buildFDiv(params[0], params[1], ""),
+ .fmod => return self.builder.buildFRem(params[0], params[1], ""),
+ .fmax => return self.builder.buildMaxNum(params[0], params[1], ""),
+ .fmin => return self.builder.buildMinNum(params[0], params[1], ""),
+ else => .{ .intrinsic = "llvm." ++ @tagName(op) },
+ } else b: {
+ const float_bits = scalar_ty.floatBits(target);
+ break :b switch (op) {
+ .add, .sub, .div, .mul => FloatOpStrat{
+ .libc = std.fmt.bufPrintZ(&fn_name_buf, "__{s}{s}f3", .{
+ @tagName(op), compilerRtFloatAbbrev(float_bits),
+ }) catch unreachable,
+ },
+ .ceil,
+ .cos,
+ .exp,
+ .exp2,
+ .fabs,
+ .floor,
+ .fma,
+ .fmax,
+ .fmin,
+ .fmod,
+ .log,
+ .log10,
+ .log2,
+ .round,
+ .sin,
+ .sqrt,
+ .tan,
+ .trunc,
+ => FloatOpStrat{
+ .libc = std.fmt.bufPrintZ(&fn_name_buf, "{s}{s}{s}", .{
+ libcFloatPrefix(float_bits), @tagName(op), libcFloatSuffix(float_bits),
+ }) catch unreachable,
+ },
+ };
+ };
+
+ const llvm_fn: *const llvm.Value = switch (strat) {
+ .intrinsic => |fn_name| self.getIntrinsic(fn_name, &.{llvm_ty}),
+ .libc => |fn_name| b: {
+ const param_types = [3]*const llvm.Type{ scalar_llvm_ty, scalar_llvm_ty, scalar_llvm_ty };
+ const libc_fn = self.getLibcFunction(fn_name, param_types[0..params.len], scalar_llvm_ty);
+ if (ty.zigTypeTag() == .Vector) {
+ const result = llvm_ty.getUndef();
+ return self.buildElementwiseCall(libc_fn, &params, result, ty.vectorLen());
}
+
+ break :b libc_fn;
},
- }
+ };
+ return self.builder.buildCall(llvm_fn, &params, params_len, .C, .Auto, "");
+ }
+
+ fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
+ if (self.liveness.isUnused(inst)) return null;
+
+ const pl_op = self.air.instructions.items(.data)[inst].pl_op;
+ const extra = self.air.extraData(Air.Bin, pl_op.payload).data;
+
+ const mulend1 = try self.resolveInst(extra.lhs);
+ const mulend2 = try self.resolveInst(extra.rhs);
+ const addend = try self.resolveInst(pl_op.operand);
+
+ const ty = self.air.typeOfIndex(inst);
+ return self.buildFloatOp(.fma, ty, 3, .{ mulend1, mulend2, addend });
}
fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
@@ -6381,14 +6564,14 @@ pub const FuncGen = struct {
}
}
- fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !?*const llvm.Value {
+ fn airUnaryOp(self: *FuncGen, inst: Air.Inst.Index, comptime op: FloatOp) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;
const un_op = self.air.instructions.items(.data)[inst].un_op;
const operand = try self.resolveInst(un_op);
const operand_ty = self.air.typeOf(un_op);
- return self.callFloatUnary(operand, operand_ty, llvm_fn_name);
+ return self.buildFloatOp(op, operand_ty, 1, .{operand});
}
fn airClzCtz(self: *FuncGen, inst: Air.Inst.Index, llvm_fn_name: []const u8) !?*const llvm.Value {
@@ -6652,17 +6835,9 @@ pub const FuncGen = struct {
const ty_op = self.air.instructions.items(.data)[inst].ty_op;
const scalar = try self.resolveInst(ty_op.operand);
- const scalar_ty = self.air.typeOf(ty_op.operand);
const vector_ty = self.air.typeOfIndex(inst);
const len = vector_ty.vectorLen();
- const scalar_llvm_ty = try self.dg.llvmType(scalar_ty);
- const op_llvm_ty = scalar_llvm_ty.vectorType(1);
- const u32_llvm_ty = self.context.intType(32);
- const mask_llvm_ty = u32_llvm_ty.vectorType(len);
- const undef_vector = op_llvm_ty.getUndef();
- const u32_zero = u32_llvm_ty.constNull();
- const op_vector = self.builder.buildInsertElement(undef_vector, scalar, u32_zero, "");
- return self.builder.buildShuffleVector(op_vector, undef_vector, mask_llvm_ty.constNull(), "");
+ return self.builder.buildVectorSplat(len, scalar, "");
}
fn airSelect(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
@@ -7191,48 +7366,6 @@ pub const FuncGen = struct {
return self.builder.buildExtractValue(opt_handle, 0, "");
}
- fn callFloor(self: *FuncGen, arg: *const llvm.Value, ty: Type) !*const llvm.Value {
- return self.callFloatUnary(arg, ty, "floor");
- }
-
- fn callCeil(self: *FuncGen, arg: *const llvm.Value, ty: Type) !*const llvm.Value {
- 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();
-
- var fn_name_buf: [100]u8 = undefined;
- const llvm_fn_name = switch (ty.zigTypeTag()) {
- .Vector => std.fmt.bufPrintZ(&fn_name_buf, "llvm.{s}.v{d}f{d}", .{
- name, ty.vectorLen(), ty.childType().floatBits(target),
- }) catch unreachable,
- .Float => std.fmt.bufPrintZ(&fn_name_buf, "llvm.{s}.f{d}", .{
- name, ty.floatBits(target),
- }) catch unreachable,
- else => unreachable,
- };
-
- const llvm_fn = self.dg.object.llvm_module.getNamedFunction(llvm_fn_name) orelse blk: {
- const operand_llvm_ty = try self.dg.llvmType(ty);
- const param_types = [_]*const llvm.Type{operand_llvm_ty};
- const fn_type = llvm.functionType(operand_llvm_ty, &param_types, param_types.len, .False);
- break :blk self.dg.object.llvm_module.addFunction(llvm_fn_name, fn_type);
- };
-
- const args: [1]*const llvm.Value = .{arg};
- return self.builder.buildCall(llvm_fn, &args, args.len, .C, .Auto, "");
- }
-
fn fieldPtr(
self: *FuncGen,
inst: Air.Inst.Index,
@@ -8055,6 +8188,26 @@ fn backendSupportsF80(target: std.Target) bool {
};
}
+/// This function returns true if we expect LLVM to lower f16 correctly
+/// and false if we expect LLVM to crash if it counters an f16 type or
+/// if it produces miscompilations.
+fn backendSupportsF16(target: std.Target) bool {
+ return switch (target.cpu.arch) {
+ else => true,
+ };
+}
+
+/// LLVM does not support all relevant intrinsics for all targets, so we
+/// may need to manually generate a libc call
+fn intrinsicsAllowed(scalar_ty: Type, target: std.Target) bool {
+ return switch (scalar_ty.tag()) {
+ .f16 => backendSupportsF16(target),
+ .f80 => target.longDoubleIs(f80) and backendSupportsF80(target),
+ .f128 => target.longDoubleIs(f128),
+ else => true,
+ };
+}
+
/// We need to insert extra padding if LLVM's isn't enough.
/// However we don't want to ever call LLVMABIAlignmentOfType or
/// LLVMABISizeOfType because these functions will trip assertions
diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig
index 3863385a06..b8dc3e1830 100644
--- a/src/codegen/llvm/bindings.zig
+++ b/src/codegen/llvm/bindings.zig
@@ -675,6 +675,14 @@ pub const Builder = opaque {
Name: [*:0]const u8,
) *const Value;
+ pub const buildVectorSplat = LLVMBuildVectorSplat;
+ extern fn LLVMBuildVectorSplat(
+ *const Builder,
+ ElementCount: c_uint,
+ EltVal: *const Value,
+ Name: [*:0]const u8,
+ ) *const Value;
+
pub const buildPtrToInt = LLVMBuildPtrToInt;
extern fn LLVMBuildPtrToInt(
*const Builder,
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 15a8fdf81e..aef4966ee7 100644
--- a/src/stage1/analyze.cpp
+++ b/src/stage1/analyze.cpp
@@ -8928,7 +8928,7 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
assert(next_offset >= llvm_next_offset);
if (next_offset > llvm_next_offset) {
- size_t pad_bytes = next_offset - (field->offset + LLVMStoreSizeOfType(g->target_data_ref, llvm_type));
+ size_t pad_bytes = next_offset - (field->offset + LLVMABISizeOfType(g->target_data_ref, llvm_type));
if (pad_bytes != 0) {
LLVMTypeRef pad_llvm_type = LLVMArrayType(LLVMInt8Type(), pad_bytes);
element_types[gen_field_index] = pad_llvm_type;
@@ -10375,7 +10375,7 @@ void ZigValue::dump() {
// float ops that take a single argument
//TODO Powi, Pow, minnum, maxnum, maximum, minimum, copysign, lround, llround, lrint, llrint
-const char *float_op_to_name(BuiltinFnId op) {
+const char *float_un_op_to_name(BuiltinFnId op) {
switch (op) {
case BuiltinFnIdSqrt:
return "sqrt";
@@ -10383,6 +10383,8 @@ const char *float_op_to_name(BuiltinFnId op) {
return "sin";
case BuiltinFnIdCos:
return "cos";
+ case BuiltinFnIdTan:
+ return "tan";
case BuiltinFnIdExp:
return "exp";
case BuiltinFnIdExp2:
@@ -10405,6 +10407,8 @@ const char *float_op_to_name(BuiltinFnId op) {
return "nearbyint";
case BuiltinFnIdRound:
return "round";
+ case BuiltinFnIdMulAdd:
+ return "fma";
default:
zig_unreachable();
}
diff --git a/src/stage1/analyze.hpp b/src/stage1/analyze.hpp
index 6d584ff361..64e0e199f8 100644
--- a/src/stage1/analyze.hpp
+++ b/src/stage1/analyze.hpp
@@ -307,7 +307,7 @@ void copy_const_val(CodeGen *g, ZigValue *dest, ZigValue *src);
bool type_has_optional_repr(ZigType *ty);
bool is_opt_err_set(ZigType *ty);
bool type_is_numeric(ZigType *ty);
-const char *float_op_to_name(BuiltinFnId op);
+const char *float_un_op_to_name(BuiltinFnId op);
#define src_assert(OK, SOURCE_NODE) src_assert_impl((OK), (SOURCE_NODE), __FILE__, __LINE__)
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 a2efed6bde..9d46a660bc 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,57 @@ 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 LLVMValueRef gen_soft_f80_widen_or_shorten(CodeGen *g, ZigType *actual_type,
+static const char *libc_float_prefix(CodeGen *g, ZigType *float_type) {
+ 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) {
+ 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,
ZigType *wanted_type, LLVMValueRef expr_val)
{
ZigType *scalar_actual_type = (actual_type->id == ZigTypeIdVector) ?
@@ -1615,87 +1664,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, &param_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 +1730,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 +2987,54 @@ 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;
+ ZigType *scalar_type = operand_type->id == ZigTypeIdVector ? operand_type->data.vector.elem_type : operand_type;
+
+ char fn_name[64];
+ 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) {
+ 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, &param, 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)) ||
+ 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);
+ return LLVMBuildCall(g->builder, float_op_fn, &operand, 1, "");
}
enum DivKind {
@@ -3088,7 +3141,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 +3158,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 +3322,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 +3332,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 +3365,124 @@ 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);
- bool div_exact_safety_check = false;
- LLVMTypeRef return_type = g->builtin_types.entry_f80->llvm_type;
+ 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;
+}
+
+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_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;
switch (op_id) {
case IrBinOpInvalid:
case IrBinOpArrayCat:
@@ -3362,152 +3507,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, "%sfmax%s", math_float_prefix, math_float_suffix);
break;
case IrBinOpMinimum:
- func_name = "__fminx";
+ sprintf(fn_name, "%sfmin%s", math_float_prefix, math_float_suffix);
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, "%sfmod%s", math_float_prefix, math_float_suffix);
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 +3641,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 +3955,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 +3974,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 +4295,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 +4358,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 +7172,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, &param, 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 +7233,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);
@@ -9513,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:
@@ -9750,6 +9826,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 b8ae1ea93e..f7ab5e12fa 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:
@@ -24300,7 +24310,7 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_
case BuiltinFnIdLog2:
return ir_add_error_node(ira, source_node,
buf_sprintf("compiler bug: TODO: implement '%s' for type '%s'. See https://github.com/ziglang/zig/issues/4026",
- float_op_to_name(fop), buf_ptr(&float_type->name)));
+ float_un_op_to_name(fop), buf_ptr(&float_type->name)));
default:
zig_unreachable();
}
@@ -24327,24 +24337,94 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, Scope *scope, AstNode *source_
break;
case BuiltinFnIdCeil:
f128M_roundToInt(in, softfloat_round_max, false, out);
- break;
+ break;
case BuiltinFnIdTrunc:
f128M_trunc(in, out);
break;
case BuiltinFnIdRound:
f128M_roundToInt(in, softfloat_round_near_maxMag, false, out);
break;
- case BuiltinFnIdNearbyInt:
- case BuiltinFnIdSin:
- case BuiltinFnIdCos:
- case BuiltinFnIdExp:
- case BuiltinFnIdExp2:
- case BuiltinFnIdLog:
- case BuiltinFnIdLog10:
- case BuiltinFnIdLog2:
- return ir_add_error_node(ira, source_node,
- buf_sprintf("compiler bug: TODO: implement '%s' for type '%s'. See https://github.com/ziglang/zig/issues/4026",
- float_op_to_name(fop), buf_ptr(&float_type->name)));
+ case BuiltinFnIdNearbyInt: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = nearbyint(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdSin: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = sin(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdCos: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = cos(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdTan: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = tan(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdExp: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = exp(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdExp2: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = exp2(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdLog: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = log(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdLog10: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = log10(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
+ case BuiltinFnIdLog2: {
+ float64_t f64_value = f128M_to_f64(in);
+ double double_value;
+ memcpy(&double_value, &f64_value, sizeof(double));
+ double_value = log2(double_value);
+ memcpy(&f64_value, &double_value, sizeof(double));
+ f64_to_f128M(f64_value, out);
+ break;
+ }
default:
zig_unreachable();
}
diff --git a/src/stage1/ir_print.cpp b/src/stage1/ir_print.cpp
index 5c7727da0c..9296242a3e 100644
--- a/src/stage1/ir_print.cpp
+++ b/src/stage1/ir_print.cpp
@@ -2558,13 +2558,13 @@ static void ir_print_add_implicit_return_type(IrPrintSrc *irp, Stage1ZirInstAddI
}
static void ir_print_float_op(IrPrintSrc *irp, Stage1ZirInstFloatOp *instruction) {
- fprintf(irp->f, "@%s(", float_op_to_name(instruction->fn_id));
+ fprintf(irp->f, "@%s(", float_un_op_to_name(instruction->fn_id));
ir_print_other_inst_src(irp, instruction->operand);
fprintf(irp->f, ")");
}
static void ir_print_float_op(IrPrintGen *irp, Stage1AirInstFloatOp *instruction) {
- fprintf(irp->f, "@%s(", float_op_to_name(instruction->fn_id));
+ fprintf(irp->f, "@%s(", float_un_op_to_name(instruction->fn_id));
ir_print_other_inst_gen(irp, instruction->operand);
fprintf(irp->f, ")");
}
diff --git a/src/translate_c.zig b/src/translate_c.zig
index e09ebea4d7..0139ec8ec3 100644
--- a/src/translate_c.zig
+++ b/src/translate_c.zig
@@ -3998,7 +3998,7 @@ fn transFloatingLiteral(c: *Context, scope: *Scope, expr: *const clang.FloatingL
var dbl = expr.getValueAsApproximateDouble();
const is_negative = dbl < 0;
if (is_negative) dbl = -dbl;
- const str = if (dbl == std.math.floor(dbl))
+ const str = if (dbl == @floor(dbl))
try std.fmt.allocPrint(c.arena, "{d}.0", .{dbl})
else
try std.fmt.allocPrint(c.arena, "{d}", .{dbl});
diff --git a/src/value.zig b/src/value.zig
index bb7b742290..d2de389de9 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -1155,6 +1155,7 @@ pub const Value = extern union {
16 => return floatWriteToMemory(f16, val.toFloat(f16), target, buffer),
32 => return floatWriteToMemory(f32, val.toFloat(f32), target, buffer),
64 => return floatWriteToMemory(f64, val.toFloat(f64), target, buffer),
+ 80 => return floatWriteToMemory(f80, val.toFloat(f80), target, buffer),
128 => return floatWriteToMemory(f128, val.toFloat(f128), target, buffer),
else => unreachable,
},
@@ -1379,25 +1380,21 @@ pub const Value = extern union {
}
fn floatWriteToMemory(comptime F: type, f: F, target: Target, buffer: []u8) void {
+ const endian = target.cpu.arch.endian();
if (F == f80) {
- switch (target.cpu.arch) {
- .i386, .x86_64 => {
- const repr = std.math.break_f80(f);
- std.mem.writeIntLittle(u64, buffer[0..8], repr.fraction);
- std.mem.writeIntLittle(u16, buffer[8..10], repr.exp);
- // TODO set the rest of the bytes to undefined. should we use 0xaa
- // or is there a different way?
- return;
- },
- else => {},
- }
+ const repr = std.math.break_f80(f);
+ std.mem.writeInt(u64, buffer[0..8], repr.fraction, endian);
+ std.mem.writeInt(u16, buffer[8..10], repr.exp, endian);
+ // TODO set the rest of the bytes to undefined. should we use 0xaa
+ // or is there a different way?
+ return;
}
const Int = @Type(.{ .Int = .{
.signedness = .unsigned,
.bits = @typeInfo(F).Float.bits,
} });
const int = @bitCast(Int, f);
- std.mem.writeInt(Int, buffer[0..@sizeOf(Int)], int, target.cpu.arch.endian());
+ std.mem.writeInt(Int, buffer[0..@sizeOf(Int)], int, endian);
}
fn floatReadFromMemory(comptime F: type, target: Target, buffer: []const u8) F {
@@ -2869,9 +2866,7 @@ pub const Value = extern union {
16 => return Value.Tag.float_16.create(arena, @intToFloat(f16, x)),
32 => return Value.Tag.float_32.create(arena, @intToFloat(f32, x)),
64 => return Value.Tag.float_64.create(arena, @intToFloat(f64, x)),
- // We can't lower this properly on non-x86 llvm backends yet
- //80 => return Value.Tag.float_80.create(arena, @intToFloat(f80, x)),
- 80 => @panic("TODO f80 intToFloat"),
+ 80 => return Value.Tag.float_80.create(arena, @intToFloat(f80, x)),
128 => return Value.Tag.float_128.create(arena, @intToFloat(f128, x)),
else => unreachable,
}
@@ -2908,9 +2903,9 @@ pub const Value = extern union {
}
const isNegative = std.math.signbit(value);
- value = std.math.fabs(value);
+ value = @fabs(value);
- const floored = std.math.floor(value);
+ const floored = @floor(value);
var rational = try std.math.big.Rational.init(arena);
defer rational.deinit();
@@ -2941,7 +2936,7 @@ pub const Value = extern union {
return 1;
}
- const w_value = std.math.fabs(scalar);
+ const w_value = @fabs(scalar);
return @divFloor(@floatToInt(std.math.big.Limb, std.math.log2(w_value)), @typeInfo(std.math.big.Limb).Int.bits) + 1;
}
@@ -3737,9 +3732,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @rem(lhs_val, rhs_val));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __remx");
- }
const lhs_val = lhs.toFloat(f80);
const rhs_val = rhs.toFloat(f80);
return Value.Tag.float_80.create(arena, @rem(lhs_val, rhs_val));
@@ -3782,9 +3774,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @mod(lhs_val, rhs_val));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __modx");
- }
const lhs_val = lhs.toFloat(f80);
const rhs_val = rhs.toFloat(f80);
return Value.Tag.float_80.create(arena, @mod(lhs_val, rhs_val));
@@ -4198,9 +4187,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, lhs_val / rhs_val);
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __divxf3");
- }
const lhs_val = lhs.toFloat(f80);
const rhs_val = rhs.toFloat(f80);
return Value.Tag.float_80.create(arena, lhs_val / rhs_val);
@@ -4255,9 +4241,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @divFloor(lhs_val, rhs_val));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __floorx");
- }
const lhs_val = lhs.toFloat(f80);
const rhs_val = rhs.toFloat(f80);
return Value.Tag.float_80.create(arena, @divFloor(lhs_val, rhs_val));
@@ -4312,9 +4295,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @divTrunc(lhs_val, rhs_val));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __truncx");
- }
const lhs_val = lhs.toFloat(f80);
const rhs_val = rhs.toFloat(f80);
return Value.Tag.float_80.create(arena, @divTrunc(lhs_val, rhs_val));
@@ -4369,9 +4349,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, lhs_val * rhs_val);
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __mulxf3");
- }
const lhs_val = lhs.toFloat(f80);
const rhs_val = rhs.toFloat(f80);
return Value.Tag.float_80.create(arena, lhs_val * rhs_val);
@@ -4411,16 +4388,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @sqrt(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt __sqrtx");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @sqrt(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt sqrtq");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @sqrt(f));
},
@@ -4454,16 +4425,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @sin(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt sin for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @sin(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt sin for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @sin(f));
},
@@ -4497,16 +4462,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @cos(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt cos for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @cos(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt cos for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @cos(f));
},
@@ -4514,6 +4473,43 @@ 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());
@@ -4540,16 +4536,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @exp(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt exp for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @exp(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt exp for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @exp(f));
},
@@ -4583,16 +4573,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @exp2(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt exp2 for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @exp2(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt exp2 for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @exp2(f));
},
@@ -4626,16 +4610,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @log(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt log for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @log(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt log for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @log(f));
},
@@ -4669,16 +4647,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @log2(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt log2 for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @log2(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt log2 for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @log2(f));
},
@@ -4712,16 +4684,10 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @log10(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt log10 for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @log10(f));
},
128 => {
- if (true) {
- @panic("TODO implement compiler_rt log10 for f128");
- }
const f = val.toFloat(f128);
return Value.Tag.float_128.create(arena, @log10(f));
},
@@ -4755,9 +4721,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @fabs(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt fabs for f80 (__fabsx)");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @fabs(f));
},
@@ -4795,9 +4758,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @floor(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt floor for f80 (__floorx)");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @floor(f));
},
@@ -4835,9 +4795,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @ceil(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt ceil for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @ceil(f));
},
@@ -4875,9 +4832,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @round(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt round for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @round(f));
},
@@ -4915,9 +4869,6 @@ pub const Value = extern union {
return Value.Tag.float_64.create(arena, @trunc(f));
},
80 => {
- if (true) {
- @panic("TODO implement compiler_rt trunc for f80");
- }
const f = val.toFloat(f80);
return Value.Tag.float_80.create(arena, @trunc(f));
},
diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp
index 0b0b33b8d9..78082abf88 100644
--- a/src/zig_llvm.cpp
+++ b/src/zig_llvm.cpp
@@ -541,6 +541,10 @@ LLVMValueRef ZigLLVMBuildUShlSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRe
return wrap(call_inst);
}
+LLVMValueRef LLVMBuildVectorSplat(LLVMBuilderRef B, unsigned elem_count, LLVMValueRef V, const char *Name) {
+ return wrap(unwrap(B)->CreateVectorSplat(elem_count, unwrap(V), Name));
+}
+
void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
assert( isa<Function>(unwrap(fn)) );
Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));
diff --git a/src/zig_llvm.h b/src/zig_llvm.h
index 63d184c417..90dcd1de39 100644
--- a/src/zig_llvm.h
+++ b/src/zig_llvm.h
@@ -149,6 +149,7 @@ ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildSMulFixSat(LLVMBuilderRef B, LLVMValueRef
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildUMulFixSat(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, const char *name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildUShlSat(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS, const char* name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildSShlSat(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS, const char* name);
+ZIG_EXTERN_C LLVMValueRef LLVMBuildVectorSplat(LLVMBuilderRef B, unsigned elem_count, LLVMValueRef V, const char *Name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildNSWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,