add zig builtins for wrapped mul and shift right (for 128-bit ints)

This commit is contained in:
Folkert 2023-04-26 14:03:50 +02:00
parent d56d3311d4
commit 312fb23567
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
3 changed files with 33 additions and 0 deletions

View file

@ -73,6 +73,9 @@ comptime {
exportNumFn(num.bytesToU64C, "bytes_to_u64");
exportNumFn(num.bytesToU128C, "bytes_to_u128");
exportNumFn(num.shiftRightZeroFillI128, "shift_right_zero_fill.i128");
exportNumFn(num.shiftRightZeroFillU128, "shift_right_zero_fill.u128");
inline for (INTEGERS) |T, i| {
num.exportPow(T, ROC_BUILTINS ++ "." ++ NUM ++ ".pow_int.");
num.exportDivCeil(T, ROC_BUILTINS ++ "." ++ NUM ++ ".div_ceil.");
@ -91,6 +94,7 @@ comptime {
num.exportMulWithOverflow(T, WIDEINTS[i], ROC_BUILTINS ++ "." ++ NUM ++ ".mul_with_overflow.");
num.exportMulOrPanic(T, WIDEINTS[i], ROC_BUILTINS ++ "." ++ NUM ++ ".mul_or_panic.");
num.exportMulSaturatedInt(T, WIDEINTS[i], ROC_BUILTINS ++ "." ++ NUM ++ ".mul_saturated.");
num.exportMulWrappedInt(T, ROC_BUILTINS ++ "." ++ NUM ++ ".mul_wrapped.");
num.exportCountLeadingZeroBits(T, ROC_BUILTINS ++ "." ++ NUM ++ ".count_leading_zero_bits.");
num.exportCountTrailingZeroBits(T, ROC_BUILTINS ++ "." ++ NUM ++ ".count_trailing_zero_bits.");

View file

@ -464,6 +464,31 @@ pub fn exportMulSaturatedInt(comptime T: type, comptime W: type, comptime name:
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
pub fn exportMulWrappedInt(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(self: T, other: T) callconv(.C) T {
return self *% other;
}
}.func;
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
pub export fn shiftRightZeroFillI128(self: i128, other: u8) i128 {
if (other & 0b1000_0000 > 0) {
return 0;
} else {
return self >> @intCast(u7, other);
}
}
pub export fn shiftRightZeroFillU128(self: u128, other: u8) u128 {
if (other & 0b1000_0000 > 0) {
return 0;
} else {
return self >> @intCast(u7, other);
}
}
pub fn exportMulOrPanic(comptime T: type, comptime W: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(self: T, other: T) callconv(.C) T {

View file

@ -285,10 +285,14 @@ pub const NUM_SUB_CHECKED_FLOAT: IntrinsicName =
pub const NUM_MUL_OR_PANIC_INT: IntrinsicName = int_intrinsic!("roc_builtins.num.mul_or_panic");
pub const NUM_MUL_SATURATED_INT: IntrinsicName = int_intrinsic!("roc_builtins.num.mul_saturated");
pub const NUM_MUL_WRAP_INT: IntrinsicName = int_intrinsic!("roc_builtins.num.mul_wrapped");
pub const NUM_MUL_CHECKED_INT: IntrinsicName = int_intrinsic!("roc_builtins.num.mul_with_overflow");
pub const NUM_MUL_CHECKED_FLOAT: IntrinsicName =
float_intrinsic!("roc_builtins.num.mul_with_overflow");
pub const NUM_SHIFT_RIGHT_ZERO_FILL: IntrinsicName =
int_intrinsic!("roc_builtins.num.shift_right_zero_fill");
pub const NUM_COUNT_LEADING_ZERO_BITS: IntrinsicName =
int_intrinsic!("roc_builtins.num.count_leading_zero_bits");
pub const NUM_COUNT_TRAILING_ZERO_BITS: IntrinsicName =