Skip to content

Commit

Permalink
std::math::big: minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 9, 2024
1 parent 766c099 commit 0bb6b60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
42 changes: 21 additions & 21 deletions std/math/big/bits.jule
Original file line number Diff line number Diff line change
Expand Up @@ -337,50 +337,50 @@ fn recursive_div(mut &x: bits, mut &y: bits): bits {
| 0:
ret [1]
}
let mut yq = make(bits, y.len, x.len)
_ = copy(yq, y)
let mut q = make(bits, y.len, x.len)
_ = copy(q, y)
for {
yq = append(yq[:1], yq...)
yq[0] = 0b0
if cmp(yq, x) != -1 {
q = append(q[:1], q...)
q[0] = 0b0
if cmp(q, x) != -1 {
break
}
}
sub_res(x, yq[1:])
sub_res(x, q[1:])
fit(x)
let mut k = recursive_div(x, y)
yq = yq[y.len:]
for i in yq {
yq[i] = 0b0
q = q[y.len:]
for i in q {
q[i] = 0b0
}
yq[yq.len - 1] = 0b1
q[q.len - 1] = 0b1
if k.len == 0 {
ret yq
ret q
}
add_res(yq, k)
ret yq
add_res(q, k)
ret q
}

// Recursion modulo algorithm. It will update left operand if necessary.
// Uses bit shifting strategy.
// Returns remainder.
fn recursive_mod(mut &x: bits, &y: bits): bits {
fn recursive_mod(mut &x: bits, &y: bits, mut &q: bits): bits {
match cmp(x, y) {
| 0:
ret nil
| -1:
ret x
}
let mut yq = make(bits, y.len, x.len)
_ = copy(yq, y)
q = q[:y.len]
_ = copy(q, y)
for {
yq = append(yq[:1], yq...)
yq[0] = 0b0
if cmp(yq, x) != -1 {
q = append(q[:1], q...)
q[0] = 0b0
if cmp(q, x) != -1 {
break
}
}
sub_res(x, yq[1:])
sub_res(x, q[1:])
fit(x)
ret recursive_mod(x, y)
ret recursive_mod(x, y, q)
}
4 changes: 3 additions & 1 deletion std/math/big/nat.jule
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ impl Nat {
}
// Use clone because of recursive division can change left operand.
let mut xb = clone(self.bits)
self.bits = recursive_mod(xb, y.bits)
// Use single allocation for quotient computation.
let mut q = make(bits, y.len(), xb.len)
self.bits = recursive_mod(xb, y.bits, q)
self.fit()
}

Expand Down

0 comments on commit 0bb6b60

Please sign in to comment.