Skip to content

Commit

Permalink
Update List.dropAt to no longer use Nat
Browse files Browse the repository at this point in the history
  • Loading branch information
rtfeldman committed Jan 23, 2024
1 parent ccbb22d commit 2f3d56a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions crates/compiler/builtins/bitcode/src/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -698,28 +698,33 @@ pub fn listDropAt(
list: RocList,
alignment: u32,
element_width: usize,
drop_index: usize,
drop_index_u64: u64,
dec: Dec,
) callconv(.C) RocList {
const size = list.len();
const size_u64 = @as(u64, @intCast(size));
// If droping the first or last element, return a seamless slice.
// For simplicity, do this by calling listSublist.
// In the future, we can test if it is faster to manually inline the important parts here.
if (drop_index == 0) {
if (drop_index_u64 == 0) {
return listSublist(list, alignment, element_width, 1, size -| 1, dec);
} else if (drop_index == size -| 1) {
} else if (drop_index_u64 == size_u64 - 1) { // It's fine if (size - 1) wraps on size == 0 here,
// because if size is 0 then it's always fine for this branch to be taken; no
// matter what drop_index was, we're size == 0, so empty list will always be returned.
return listSublist(list, alignment, element_width, 0, size -| 1, dec);
}

if (list.bytes) |source_ptr| {
if (drop_index >= size) {
if (drop_index_u64 >= size_u64) {
return list;
}

if (drop_index < size) {
const element = source_ptr + drop_index * element_width;
dec(element);
}
// This cast must be lossless, because we would have just early-returned if drop_index
// were >= than `size`, and we know `size` fits in usize.
const drop_index = @as(usize, @intCast(drop_index_u64));

const element = source_ptr + drop_index * element_width;
dec(element);

// NOTE
// we need to return an empty list explicitly,
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/gen_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ trait Backend<'a> {
// list: RocList,
// alignment: u32,
// element_width: usize,
// drop_index: usize,
// drop_index: u64,
// dec: Dec,

self.build_fn_call(
Expand All @@ -2008,7 +2008,7 @@ trait Backend<'a> {
list_layout,
Layout::U32,
layout_usize,
layout_usize,
Layout::U64,
layout_usize,
],
ret_layout,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/gen_llvm/src/llvm/build_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub(crate) fn list_sublist<'a, 'ctx>(
)
}

/// List.dropAt : List elem, Nat -> List elem
/// List.dropAt : List elem, U64 -> List elem
pub(crate) fn list_drop_at<'a, 'ctx>(
env: &Env<'a, 'ctx, '_>,
layout_interner: &STLayoutInterner<'a>,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/gen_wasm/src/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ impl<'a> LowLevelCall<'a> {
// list: RocList, i32
// element_width: usize, i32
// alignment: u32, i32
// drop_index: usize, i32
// drop_index: u64, i64
// dec: Dec, i32

// Load the return pointer and the list
Expand Down

0 comments on commit 2f3d56a

Please sign in to comment.