Switch Num.tan to a zig builtin

It should be able to share some work between sine and cosine to run faster.
This commit is contained in:
Brendan Hansknecht 2023-09-16 21:27:51 -07:00
parent 7986d6cdba
commit 108d9a54e3
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
11 changed files with 25 additions and 4 deletions

View file

@ -163,6 +163,7 @@ comptime {
num.exportSin(T, ROC_BUILTINS ++ "." ++ NUM ++ ".sin.");
num.exportCos(T, ROC_BUILTINS ++ "." ++ NUM ++ ".cos.");
num.exportTan(T, ROC_BUILTINS ++ "." ++ NUM ++ ".tan.");
num.exportPow(T, ROC_BUILTINS ++ "." ++ NUM ++ ".pow.");
num.exportLog(T, ROC_BUILTINS ++ "." ++ NUM ++ ".log.");

View file

@ -168,6 +168,15 @@ pub fn exportCos(comptime T: type, comptime name: []const u8) void {
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
pub fn exportTan(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(input: T) callconv(.C) T {
return math.tan(input);
}
}.func;
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
pub fn exportLog(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(input: T) callconv(.C) T {

View file

@ -825,11 +825,7 @@ max = \a, b ->
sin : Frac a -> Frac a
cos : Frac a -> Frac a
tan : Frac a -> Frac a
tan = \x ->
# `tan` is not available as an intrinsic in LLVM
Num.div (Num.sin x) (Num.cos x)
asin : Frac a -> Frac a
acos : Frac a -> Frac a

View file

@ -272,6 +272,7 @@ macro_rules! int_intrinsic {
pub const NUM_SIN: IntrinsicName = float_intrinsic!("roc_builtins.num.sin");
pub const NUM_COS: IntrinsicName = float_intrinsic!("roc_builtins.num.cos");
pub const NUM_TAN: IntrinsicName = float_intrinsic!("roc_builtins.num.tan");
pub const NUM_ASIN: IntrinsicName = float_intrinsic!("roc_builtins.num.asin");
pub const NUM_ACOS: IntrinsicName = float_intrinsic!("roc_builtins.num.acos");
pub const NUM_ATAN: IntrinsicName = float_intrinsic!("roc_builtins.num.atan");

View file

@ -186,6 +186,7 @@ map_symbol_to_lowlevel_and_arity! {
NumNeg; NUM_NEG; 1,
NumSin; NUM_SIN; 1,
NumCos; NUM_COS; 1,
NumTan; NUM_TAN; 1,
NumSqrtUnchecked; NUM_SQRT; 1,
NumLogUnchecked; NUM_LOG; 1,
NumRound; NUM_ROUND; 1,

View file

@ -935,6 +935,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
| NumLogUnchecked
| NumSin
| NumCos
| NumTan
| NumCeiling
| NumFloor
| NumToFrac
@ -2655,6 +2656,7 @@ fn build_float_unary_op<'a, 'ctx>(
// trigonometry
NumSin => call_bitcode_fn(env, &[arg.into()], &bitcode::NUM_SIN[float_width]),
NumCos => call_bitcode_fn(env, &[arg.into()], &bitcode::NUM_COS[float_width]),
NumTan => call_bitcode_fn(env, &[arg.into()], &bitcode::NUM_TAN[float_width]),
NumAtan => call_bitcode_fn(env, &[arg.into()], &bitcode::NUM_ATAN[float_width]),
NumAcos => call_bitcode_fn(env, &[arg.into()], &bitcode::NUM_ACOS[float_width]),

View file

@ -1539,6 +1539,12 @@ impl<'a> LowLevelCall<'a> {
}
_ => panic_ret_type(),
},
NumTan => match self.ret_layout_raw {
LayoutRepr::Builtin(Builtin::Float(width)) => {
self.load_args_and_call_zig(backend, &bitcode::NUM_TAN[width]);
}
_ => panic_ret_type(),
},
NumSqrtUnchecked => {
self.load_args(backend);
match self.ret_layout_raw {

View file

@ -78,6 +78,7 @@ pub enum LowLevel {
NumNeg,
NumSin,
NumCos,
NumTan,
NumSqrtUnchecked,
NumLogUnchecked,
NumRound,
@ -324,6 +325,7 @@ map_symbol_to_lowlevel! {
NumNeg <= NUM_NEG,
NumSin <= NUM_SIN,
NumCos <= NUM_COS,
NumTan <= NUM_TAN,
NumSqrtUnchecked <= NUM_SQRT,
NumLogUnchecked <= NUM_LOG,
NumRound <= NUM_ROUND,

View file

@ -1571,6 +1571,7 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC {
| NumNeg
| NumSin
| NumCos
| NumTan
| NumSqrtUnchecked
| NumLogUnchecked
| NumRound

View file

@ -1327,6 +1327,7 @@ fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[Ownership] {
| NumNeg
| NumSin
| NumCos
| NumTan
| NumSqrtUnchecked
| NumLogUnchecked
| NumRound

View file

@ -101,6 +101,7 @@ enum FirstOrder {
NumNeg,
NumSin,
NumCos,
NumTan,
NumSqrtUnchecked,
NumLogUnchecked,
NumRound,