Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math.big: improve the performance of left_shift_digits_in_place and right_shift_digits_in_place #22450

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions vlib/math/big/special_array_ops.v
Original file line number Diff line number Diff line change
Expand Up @@ -256,31 +256,24 @@ fn pow2(k int) Integer {
}

// optimized left shift in place. amount must be positive
@[direct_array_access]
fn left_shift_digits_in_place(mut a []u32, amount int) {
a_len := a.len
// control or allocate capacity
for _ in a_len .. a_len + amount {
a << u32(0)
}
for index := a_len - 1; index >= 0; index-- {
a[index + amount] = a[index]
}
for index in 0 .. amount {
a[index] = u32(0)
// this is actual in builtin/array.v, prepend_many (private fn)
// x := []u32{ len : amount }
// a.prepend_many(&x[0], amount)
old_len := a.len
elem_size := a.element_size
unsafe {
a.grow_len(amount)
sptr := &u8(a.data)
dptr := &u8(a.data) + u64(amount) * u64(elem_size)
vmemmove(dptr, sptr, u64(old_len) * u64(elem_size))
vmemset(sptr, 0, u64(amount) * u64(elem_size))
}
}

// optimized right shift in place. amount must be positive
@[direct_array_access]
fn right_shift_digits_in_place(mut a []u32, amount int) {
for index := 0; index < a.len - amount; index++ {
a[index] = a[index + amount]
}
for index := a.len - amount; index < a.len; index++ {
a[index] = u32(0)
}
shrink_tail_zeros(mut a)
a.drop(amount)
}

// operand b can be greater than operand a
Expand Down
Loading