From f8d6d13a249d7a64cef40f428b5aa6359c5bda06 Mon Sep 17 00:00:00 2001 From: Nicolas Abril Date: Sat, 15 Jun 2024 19:32:21 +0200 Subject: [PATCH] Write log and atan2 as native hvm defs --- docs/builtins.md | 16 ++++++++++++++++ src/fun/builtins.bend | 17 +++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/builtins.md b/docs/builtins.md index 109c164d..d1ffda39 100644 --- a/docs/builtins.md +++ b/docs/builtins.md @@ -226,3 +226,19 @@ A Natural Number can be written with literals with a `#` before the literal numb ## IO IO Functions are in the **next milestone**! + +## Numeric operations + +### log +```py +def log(x: f24, base: f24) -> f24 +``` +Computes the logarithm of `x` with the specified `base`. + +### atan2 +```py +def atan2(x: f24, y: f24) -> f24 +``` +Computes the arctangent of `y / x`. + +Has the same behaviour as `atan2f` in the C math lib. diff --git a/src/fun/builtins.bend b/src/fun/builtins.bend index 822eef05..21d95372 100644 --- a/src/fun/builtins.bend +++ b/src/fun/builtins.bend @@ -95,7 +95,10 @@ print text = (IO/Call IO/MAGIC "PUT_TEXT" text @x (IO/Done IO/MAGIC x)) #read_file path = (IO/Call IO/MAGIC "GET_FILE" path @x (IO/Done IO/MAGIC x)) #write_file path text = (IO/Call IO/MAGIC "PUT_FILE" (path, text) @x (IO/Done IO/MAGIC x)) +# Returns a monotonically increasing nanosecond timestamp as an u48 encoded as a pair of u24s. get_time = (IO/Call IO/MAGIC "GET_TIME" * @x (IO/Done IO/MAGIC x)) + +# Sleeps for the given number of nanoseconds, given by an u48 encoded as a pair of u24s. sleep hi_lo = (IO/Call IO/MAGIC "PUT_TIME" hi_lo @x (IO/Done IO/MAGIC x)) @@ -110,10 +113,16 @@ defer val = @x (x val) defer_arg defered arg = @x (defered x arg) undefer defered = (defered @x x) -# log :: f24 -> f24 -> f24 + +# Native numeric operations + +# log(x: f24, base: f24) -> f24 # Computes the logarithm of `x` with the specified `base`. -log x base = (| base x) -# atan2 :: f24 -> f24 -> f24 +hvm log: + (x ($([|] $(x ret)) ret)) + +# atan2(x: f24, y: f24) -> f24 # Has the same behaviour as `atan2f` in the C math lib. # Computes the arctangent of the quotient of its two arguments. -atan2 x y = (& x y) +hvm atan2: + ($([&] $(y ret)) (y ret))