Add support for Num.abs to Dec type

This commit is contained in:
Sekerez 2023-09-24 23:10:25 -04:00
parent 8f210241ab
commit fa65d89a3c
4 changed files with 56 additions and 47 deletions

View file

@ -235,6 +235,11 @@ pub const RocDec = extern struct {
return if (negated) |n| .{ .num = n } else null;
}
pub fn abs(self: RocDec) !RocDec {
const absolute = try math.absInt(self.num);
return RocDec{ .num = absolute };
}
pub fn addWithOverflow(self: RocDec, other: RocDec) WithOverflow(RocDec) {
var answer: i128 = undefined;
const overflowed = @addWithOverflow(i128, self.num, other.num, &answer);
@ -1244,6 +1249,11 @@ pub fn negateC(arg: RocDec) callconv(.C) i128 {
return if (@call(.{ .modifier = always_inline }, RocDec.negate, .{arg})) |dec| dec.num else @panic("TODO overflow for negating RocDec");
}
pub fn absC(arg: RocDec) callconv(.C) i128 {
var result = @call(.{ .modifier = always_inline }, RocDec.abs, .{arg}) catch @panic("TODO overflow for calling absolute value on RocDec");
return result.num;
}
pub fn addC(arg1: RocDec, arg2: RocDec) callconv(.C) WithOverflow(RocDec) {
return @call(.{ .modifier = always_inline }, RocDec.addWithOverflow, .{ arg1, arg2 });
}

View file

@ -18,40 +18,37 @@ const STR = "str";
const dec = @import("dec.zig");
comptime {
exportDecFn(dec.fromStr, "from_str");
exportDecFn(dec.toStr, "to_str");
exportDecFn(dec.fromU64C, "from_u64");
exportDecFn(dec.toI128, "to_i128");
exportDecFn(dec.toF64, "to_f64");
exportDecFn(dec.eqC, "eq");
exportDecFn(dec.neqC, "neq");
exportDecFn(dec.negateC, "negate");
exportDecFn(dec.divC, "div");
exportDecFn(dec.sinC, "sin");
exportDecFn(dec.cosC, "cos");
exportDecFn(dec.tanC, "tan");
exportDecFn(dec.asinC, "asin");
exportDecFn(dec.absC, "abs");
exportDecFn(dec.acosC, "acos");
exportDecFn(dec.atanC, "atan");
exportDecFn(dec.addC, "add_with_overflow");
exportDecFn(dec.addOrPanicC, "add_or_panic");
exportDecFn(dec.addSaturatedC, "add_saturated");
exportDecFn(dec.subC, "sub_with_overflow");
exportDecFn(dec.subOrPanicC, "sub_or_panic");
exportDecFn(dec.subSaturatedC, "sub_saturated");
exportDecFn(dec.asinC, "asin");
exportDecFn(dec.atanC, "atan");
exportDecFn(dec.cosC, "cos");
exportDecFn(dec.divC, "div");
exportDecFn(dec.eqC, "eq");
exportDecFn(dec.fromF32C, "from_float.f32");
exportDecFn(dec.fromF64C, "from_float.f64");
exportDecFn(dec.fromStr, "from_str");
exportDecFn(dec.fromU64C, "from_u64");
exportDecFn(dec.mulC, "mul_with_overflow");
exportDecFn(dec.mulOrPanicC, "mul_or_panic");
exportDecFn(dec.mulSaturatedC, "mul_saturated");
exportDecFn(dec.negateC, "negate");
exportDecFn(dec.neqC, "neq");
exportDecFn(dec.sinC, "sin");
exportDecFn(dec.subC, "sub_with_overflow");
exportDecFn(dec.subOrPanicC, "sub_or_panic");
exportDecFn(dec.subSaturatedC, "sub_saturated");
exportDecFn(dec.tanC, "tan");
exportDecFn(dec.toF64, "to_f64");
exportDecFn(dec.toI128, "to_i128");
exportDecFn(dec.toStr, "to_str");
inline for (INTEGERS) |T| {
dec.exportFromInt(T, ROC_BUILTINS ++ ".dec.from_int.");
}
exportDecFn(dec.fromF32C, "from_float.f32");
exportDecFn(dec.fromF64C, "from_float.f64");
}
// List Module

View file

@ -393,32 +393,33 @@ pub const LIST_CAPACITY: &str = "roc_builtins.list.capacity";
pub const LIST_REFCOUNT_PTR: &str = "roc_builtins.list.refcount_ptr";
pub const LIST_RELEASE_EXCESS_CAPACITY: &str = "roc_builtins.list.release_excess_capacity";
pub const DEC_FROM_STR: &str = "roc_builtins.dec.from_str";
pub const DEC_TO_STR: &str = "roc_builtins.dec.to_str";
pub const DEC_FROM_F64: &str = "roc_builtins.dec.from_f64";
pub const DEC_FROM_U64: &str = "roc_builtins.dec.from_u64";
pub const DEC_FROM_INT: IntrinsicName = int_intrinsic!("roc_builtins.dec.from_int");
pub const DEC_FROM_FLOAT: IntrinsicName = float_intrinsic!("roc_builtins.dec.from_float");
pub const DEC_TO_I128: &str = "roc_builtins.dec.to_i128";
pub const DEC_EQ: &str = "roc_builtins.dec.eq";
pub const DEC_NEQ: &str = "roc_builtins.dec.neq";
pub const DEC_NEGATE: &str = "roc_builtins.dec.negate";
pub const DEC_MUL_WITH_OVERFLOW: &str = "roc_builtins.dec.mul_with_overflow";
pub const DEC_DIV: &str = "roc_builtins.dec.div";
pub const DEC_ADD_WITH_OVERFLOW: &str = "roc_builtins.dec.add_with_overflow";
pub const DEC_ABS: &str = "roc_builtins.dec.abs";
pub const DEC_ACOS: &str = "roc_builtins.dec.acos";
pub const DEC_ADD_OR_PANIC: &str = "roc_builtins.dec.add_or_panic";
pub const DEC_ADD_SATURATED: &str = "roc_builtins.dec.add_saturated";
pub const DEC_SUB_WITH_OVERFLOW: &str = "roc_builtins.dec.sub_with_overflow";
pub const DEC_SUB_OR_PANIC: &str = "roc_builtins.dec.sub_or_panic";
pub const DEC_SUB_SATURATED: &str = "roc_builtins.dec.sub_saturated";
pub const DEC_ADD_WITH_OVERFLOW: &str = "roc_builtins.dec.add_with_overflow";
pub const DEC_ASIN: &str = "roc_builtins.dec.asin";
pub const DEC_ATAN: &str = "roc_builtins.dec.atan";
pub const DEC_COS: &str = "roc_builtins.dec.cos";
pub const DEC_DIV: &str = "roc_builtins.dec.div";
pub const DEC_EQ: &str = "roc_builtins.dec.eq";
pub const DEC_FROM_F64: &str = "roc_builtins.dec.from_f64";
pub const DEC_FROM_FLOAT: IntrinsicName = float_intrinsic!("roc_builtins.dec.from_float");
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_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";
pub const DEC_NEGATE: &str = "roc_builtins.dec.negate";
pub const DEC_NEQ: &str = "roc_builtins.dec.neq";
pub const DEC_SIN: &str = "roc_builtins.dec.sin";
pub const DEC_COS: &str = "roc_builtins.dec.cos";
pub const DEC_SUB_OR_PANIC: &str = "roc_builtins.dec.sub_or_panic";
pub const DEC_SUB_SATURATED: &str = "roc_builtins.dec.sub_saturated";
pub const DEC_SUB_WITH_OVERFLOW: &str = "roc_builtins.dec.sub_with_overflow";
pub const DEC_TAN: &str = "roc_builtins.dec.tan";
pub const DEC_ASIN: &str = "roc_builtins.dec.asin";
pub const DEC_ACOS: &str = "roc_builtins.dec.acos";
pub const DEC_ATAN: &str = "roc_builtins.dec.atan";
pub const DEC_TO_I128: &str = "roc_builtins.dec.to_i128";
pub const DEC_TO_STR: &str = "roc_builtins.dec.to_str";
pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic";
pub const UTILS_ALLOCATE_WITH_REFCOUNT: &str = "roc_builtins.utils.allocate_with_refcount";