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 Sep 5, 2024
1 parent 0782bb2 commit 65ff895
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
18 changes: 9 additions & 9 deletions std/math/big/bits.jule
Original file line number Diff line number Diff line change
Expand Up @@ -316,23 +316,23 @@ fn karatsuba(mut x: bits, mut y: bits): bits {
ret basicMul(x, y)
}
m := max(len(x), len(y)) >> 1
mut x1 := cloneBits(rsh(x, m))
mut x1 := rsh(x, m)
mut x0 := cloneBits(x)
subRes(x0, lsh(x1, m))
mut y1 := cloneBits(rsh(y, m))
mut y1 := rsh(y, m)
mut y0 := cloneBits(y)
subRes(y0, lsh(y1, m))
mut z2 := karatsuba(x1, y1)
mut z0 := karatsuba(x0, y0)
addRes(x1, x0)
addRes(y1, y0)
mut z1 := karatsuba(x1, y1)
addRes(x0, x1)
addRes(y0, y1)
mut z1 := karatsuba(x0, y0)
subRes(z1, z2)
subRes(z1, z0)
mut r := lsh(z2, m << 1)
addRes(r, lsh(z1, m))
addRes(r, z0)
ret r
z1 = lsh(z1, m)
addRes(z1, z0)
addRes(z1, lsh(z2, m << 1))
ret z1
}

// Recursion division algorithm. It will update left operand if necessary.
Expand Down
5 changes: 3 additions & 2 deletions std/math/big/conv.jule
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ fn formatDecimalPart(mut &buf: []byte, m: byte) {
fn formatDecimal(&b: bits): str {
mut buf := make([]byte, 1, len(b) >> 1 + 1)
buf[0] = '0'
for _, bit in formatBinary(b) {
mut i := len(b) - 1
for i >= 0; i-- {
formatDecimalPart(buf, 1)
if bit == '1' {
if b[i] == 0b1 {
formatDecimalPart(buf, 0)
}
}
Expand Down

0 comments on commit 65ff895

Please sign in to comment.