dev backend: Num.isMultipleOf

This commit is contained in:
Folkert 2023-04-27 13:50:27 +02:00
parent 10a497fdde
commit 0bf3eefbf2
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
5 changed files with 34 additions and 1 deletions

View file

@ -96,6 +96,8 @@ comptime {
num.exportMulSaturatedInt(T, WIDEINTS[i], ROC_BUILTINS ++ "." ++ NUM ++ ".mul_saturated.");
num.exportMulWrappedInt(T, ROC_BUILTINS ++ "." ++ NUM ++ ".mul_wrapped.");
num.exportIsMultipleOf(T, ROC_BUILTINS ++ "." ++ NUM ++ ".is_multiple_of.");
num.exportCountLeadingZeroBits(T, ROC_BUILTINS ++ "." ++ NUM ++ ".count_leading_zero_bits.");
num.exportCountTrailingZeroBits(T, ROC_BUILTINS ++ "." ++ NUM ++ ".count_trailing_zero_bits.");
num.exportCountOneBits(T, ROC_BUILTINS ++ "." ++ NUM ++ ".count_one_bits.");

View file

@ -254,6 +254,30 @@ fn bytesToU128(arg: RocList, position: usize) u128 {
return @bitCast(u128, [_]u8{ bytes[position], bytes[position + 1], bytes[position + 2], bytes[position + 3], bytes[position + 4], bytes[position + 5], bytes[position + 6], bytes[position + 7], bytes[position + 8], bytes[position + 9], bytes[position + 10], bytes[position + 11], bytes[position + 12], bytes[position + 13], bytes[position + 14], bytes[position + 15] });
}
fn isMultipleOf(comptime T: type, lhs: T, rhs: T) bool {
if (rhs == 0 or rhs == -1) {
// lhs is a multiple of rhs iff
//
// - rhs == -1
// - both rhs and lhs are 0
//
// the -1 case is important for overflow reasons `isize::MIN % -1` crashes in rust
return (rhs == -1) or (lhs == 0);
} else {
const rem = @mod(lhs, rhs);
return rem == 0;
}
}
pub fn exportIsMultipleOf(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {
fn func(self: T, other: T) callconv(.C) bool {
return @call(.{ .modifier = always_inline }, isMultipleOf, .{ T, self, other });
}
}.func;
@export(f, .{ .name = name ++ @typeName(T), .linkage = .Strong });
}
fn addWithOverflow(comptime T: type, self: T, other: T) WithOverflow(T) {
switch (@typeInfo(T)) {
.Int => {

View file

@ -290,6 +290,8 @@ pub const NUM_MUL_CHECKED_INT: IntrinsicName = int_intrinsic!("roc_builtins.num.
pub const NUM_MUL_CHECKED_FLOAT: IntrinsicName =
float_intrinsic!("roc_builtins.num.mul_with_overflow");
pub const NUM_IS_MULTIPLE_OF: IntrinsicName = int_intrinsic!("roc_builtins.num.is_multiple_of");
pub const NUM_SHIFT_RIGHT_ZERO_FILL: IntrinsicName =
int_intrinsic!("roc_builtins.num.shift_right_zero_fill");