feat(builtins): Num.acos : Float -> Float

This commit is contained in:
lrosa007 2020-11-01 09:27:42 -05:00
parent 00445b3bc6
commit 4da2d7f101
9 changed files with 46 additions and 2 deletions

View file

@ -21,6 +21,11 @@ fn powInt(base: i64, exp: i64) callconv(.C) i64 {
return math.pow(i64, base, exp); return math.pow(i64, base, exp);
} }
comptime { @export(acos, .{ .name = math_namespace ++ ".acos", .linkage = .Strong }); }
fn acos(num: f64) callconv(.C) f64 {
return math.acos(num);
}
// Str.split // Str.split

View file

@ -17,6 +17,7 @@ pub fn get_bytes() -> Vec<u8> {
buffer buffer
} }
pub const MATH_ACOS: &str = "roc_builtins.math.acos";
pub const MATH_ATAN: &str = "roc_builtins.math.atan"; pub const MATH_ATAN: &str = "roc_builtins.math.atan";
pub const MATH_IS_FINITE: &str = "roc_builtins.math.is_finite"; pub const MATH_IS_FINITE: &str = "roc_builtins.math.is_finite";
pub const MATH_POW_INT: &str = "roc_builtins.math.pow_int"; pub const MATH_POW_INT: &str = "roc_builtins.math.pow_int";

View file

@ -350,6 +350,12 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
top_level_function(vec![float_type()], Box::new(float_type())), top_level_function(vec![float_type()], Box::new(float_type())),
); );
// acos : Float -> Float
add_type(
Symbol::NUM_ACOS,
top_level_function(vec![float_type()], Box::new(float_type())),
);
// Bool module // Bool module
// and : Bool, Bool -> Bool // and : Bool, Bool -> Bool

View file

@ -375,6 +375,12 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
unique_function(vec![float_type(star1)], float_type(star2)) unique_function(vec![float_type(star1)], float_type(star2))
}); });
// acos : Float -> Float
add_type(Symbol::NUM_ACOS, {
let_tvars! { star1, star2 };
unique_function(vec![float_type(star1)], float_type(star2))
});
// Bool module // Bool module
// isEq or (==) : Attr * a, Attr * a -> Attr * Bool // isEq or (==) : Attr * a, Attr * a -> Attr * Bool

View file

@ -98,6 +98,7 @@ pub fn builtin_defs(var_store: &mut VarStore) -> MutMap<Symbol, Def> {
Symbol::NUM_POW_INT => num_pow_int, Symbol::NUM_POW_INT => num_pow_int,
Symbol::NUM_FLOOR => num_floor, Symbol::NUM_FLOOR => num_floor,
Symbol::NUM_ATAN => num_atan, Symbol::NUM_ATAN => num_atan,
Symbol::NUM_ACOS => num_acos,
} }
} }
@ -781,6 +782,26 @@ fn num_atan(symbol: Symbol, var_store: &mut VarStore) -> Def {
) )
} }
// Num.acos : Float -> Float
fn num_acos(symbol: Symbol, var_store: &mut VarStore) -> Def {
let arg_float_var = var_store.fresh();
let ret_float_var = var_store.fresh();
let body = RunLowLevel {
op: LowLevel::NumAcos,
args: vec![(arg_float_var, Var(Symbol::ARG_1))],
ret_var: ret_float_var,
};
defn(
symbol,
vec![(arg_float_var, Symbol::ARG_1)],
var_store,
body,
ret_float_var,
)
}
/// List.isEmpty : List * -> Bool /// List.isEmpty : List * -> Bool
fn list_is_empty(symbol: Symbol, var_store: &mut VarStore) -> Def { fn list_is_empty(symbol: Symbol, var_store: &mut VarStore) -> Def {
let list_var = var_store.fresh(); let list_var = var_store.fresh();

View file

@ -2172,7 +2172,7 @@ fn run_low_level<'a, 'ctx, 'env>(
list_join(env, inplace, parent, list, outer_list_layout) list_join(env, inplace, parent, list, outer_list_layout)
} }
NumAbs | NumNeg | NumRound | NumSqrtUnchecked | NumSin | NumCos | NumCeiling | NumFloor NumAbs | NumNeg | NumRound | NumSqrtUnchecked | NumSin | NumCos | NumCeiling | NumFloor
| NumToFloat | NumIsFinite | NumAtan => { | NumToFloat | NumIsFinite | NumAtan | NumAcos => {
debug_assert_eq!(args.len(), 1); debug_assert_eq!(args.len(), 1);
let (arg, arg_layout) = load_symbol_and_layout(env, scope, &args[0]); let (arg, arg_layout) = load_symbol_and_layout(env, scope, &args[0]);
@ -2737,6 +2737,7 @@ fn build_float_unary_op<'a, 'ctx, 'env>(
), ),
NumIsFinite => call_bitcode_fn(NumIsFinite, env, &[arg.into()], &bitcode::MATH_IS_FINITE), NumIsFinite => call_bitcode_fn(NumIsFinite, env, &[arg.into()], &bitcode::MATH_IS_FINITE),
NumAtan => call_bitcode_fn(NumAtan, env, &[arg.into()], &bitcode::MATH_ATAN), NumAtan => call_bitcode_fn(NumAtan, env, &[arg.into()], &bitcode::MATH_ATAN),
NumAcos => call_bitcode_fn(NumAcos, env, &[arg.into()], &bitcode::MATH_ACOS),
_ => { _ => {
unreachable!("Unrecognized int unary operation: {:?}", op); unreachable!("Unrecognized int unary operation: {:?}", op);
} }

View file

@ -44,6 +44,7 @@ pub enum LowLevel {
NumFloor, NumFloor,
NumIsFinite, NumIsFinite,
NumAtan, NumAtan,
NumAcos,
Eq, Eq,
NotEq, NotEq,
And, And,

View file

@ -652,6 +652,7 @@ define_builtins! {
42 NUM_ADD_WRAP: "addWrap" 42 NUM_ADD_WRAP: "addWrap"
43 NUM_ADD_CHECKED: "addChecked" 43 NUM_ADD_CHECKED: "addChecked"
44 NUM_ATAN: "atan" 44 NUM_ATAN: "atan"
45 NUM_ACOS: "acos"
} }
2 BOOL: "Bool" => { 2 BOOL: "Bool" => {
0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias 0 BOOL_BOOL: "Bool" imported // the Bool.Bool type alias

View file

@ -527,6 +527,8 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
| NumPowInt => arena.alloc_slice_copy(&[irrelevant, irrelevant]), | NumPowInt => arena.alloc_slice_copy(&[irrelevant, irrelevant]),
NumAbs | NumNeg | NumSin | NumCos | NumSqrtUnchecked | NumRound | NumCeiling | NumFloor NumAbs | NumNeg | NumSin | NumCos | NumSqrtUnchecked | NumRound | NumCeiling | NumFloor
| NumToFloat | Not | NumIsFinite | NumAtan => arena.alloc_slice_copy(&[irrelevant]), | NumToFloat | Not | NumIsFinite | NumAtan | NumAcos => {
arena.alloc_slice_copy(&[irrelevant])
}
} }
} }