diff --git a/crates/compiler/builtins/bitcode/src/dec.zig b/crates/compiler/builtins/bitcode/src/dec.zig index 75340094e7c..d60057843c9 100644 --- a/crates/compiler/builtins/bitcode/src/dec.zig +++ b/crates/compiler/builtins/bitcode/src/dec.zig @@ -463,6 +463,10 @@ pub const RocDec = extern struct { return RocDec{ .num = out }; } + pub fn log(self: RocDec) RocDec { + return fromF64(@log(self.toF64())).?; + } + // I belive the output of the trig functions is always in range of Dec. // If not, we probably should just make it saturate the Dec. // I don't think this should crash or return errors. @@ -1190,6 +1194,10 @@ test "div: 500 / 1000" { try expectEqual(RocDec.fromStr(roc_str), number1.div(number2)); } +test "log: 1" { + try expectEqual(RocDec.fromU64(0), RocDec.log(RocDec.fromU64(1))); +} + // exports pub fn fromStr(arg: RocStr) callconv(.C) num_.NumParseResult(i128) { @@ -1282,6 +1290,10 @@ pub fn divC(arg1: RocDec, arg2: RocDec) callconv(.C) i128 { return @call(.always_inline, RocDec.div, .{ arg1, arg2 }).num; } +pub fn logC(arg: RocDec) callconv(.C) i128 { + return @call(.always_inline, RocDec.log, .{arg}).num; +} + pub fn sinC(arg: RocDec) callconv(.C) i128 { return @call(.always_inline, RocDec.sin, .{arg}).num; } diff --git a/crates/compiler/builtins/bitcode/src/main.zig b/crates/compiler/builtins/bitcode/src/main.zig index 8979819f9bb..6b0a5cc9d60 100644 --- a/crates/compiler/builtins/bitcode/src/main.zig +++ b/crates/compiler/builtins/bitcode/src/main.zig @@ -32,6 +32,7 @@ comptime { exportDecFn(dec.fromF64C, "from_float.f64"); exportDecFn(dec.fromStr, "from_str"); exportDecFn(dec.fromU64C, "from_u64"); + exportDecFn(dec.logC, "log"); exportDecFn(dec.mulC, "mul_with_overflow"); exportDecFn(dec.mulOrPanicC, "mul_or_panic"); exportDecFn(dec.mulSaturatedC, "mul_saturated"); diff --git a/crates/compiler/builtins/src/bitcode.rs b/crates/compiler/builtins/src/bitcode.rs index 8482f79739e..f9f28b12bb8 100644 --- a/crates/compiler/builtins/src/bitcode.rs +++ b/crates/compiler/builtins/src/bitcode.rs @@ -408,6 +408,7 @@ pub const DEC_FROM_FLOAT: IntrinsicName = float_intrinsic!("roc_builtins.dec.fro pub const DEC_FROM_INT: IntrinsicName = int_intrinsic!("roc_builtins.dec.from_int"); pub const DEC_FROM_STR: &str = "roc_builtins.dec.from_str"; pub const DEC_FROM_U64: &str = "roc_builtins.dec.from_u64"; +pub const DEC_LOG: &str = "roc_builtins.dec.log"; pub const DEC_MUL_OR_PANIC: &str = "roc_builtins.dec.mul_or_panic"; pub const DEC_MUL_SATURATED: &str = "roc_builtins.dec.mul_saturated"; pub const DEC_MUL_WITH_OVERFLOW: &str = "roc_builtins.dec.mul_with_overflow";