mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
math.big: optimize add_digit_array() (#25139)
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
Some checks are pending
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
This commit is contained in:
parent
4c4028a84b
commit
b502d52280
1 changed files with 7 additions and 10 deletions
|
@ -45,28 +45,25 @@ fn add_digit_array(operand_a []u64, operand_b []u64, mut sum []u64) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// First pass intersects with both operands
|
|
||||||
smaller_limit := imin(operand_a.len, operand_b.len)
|
|
||||||
larger_limit := imax(operand_a.len, operand_b.len)
|
|
||||||
mut a, mut b := if operand_a.len >= operand_b.len {
|
mut a, mut b := if operand_a.len >= operand_b.len {
|
||||||
operand_a, operand_b
|
operand_a, operand_b
|
||||||
} else {
|
} else {
|
||||||
operand_b, operand_a
|
operand_b, operand_a
|
||||||
}
|
}
|
||||||
mut carry := u64(0)
|
mut carry := u64(0)
|
||||||
for index in 0 .. smaller_limit {
|
for index in 0 .. b.len {
|
||||||
partial := carry + a[index] + b[index]
|
partial := carry + a[index] + b[index]
|
||||||
sum[index] = u64(partial) & max_digit
|
sum[index] = partial & max_digit
|
||||||
carry = u64(partial >> digit_bits)
|
carry = partial >> digit_bits
|
||||||
}
|
}
|
||||||
|
|
||||||
for index in smaller_limit .. larger_limit {
|
for index in b.len .. a.len {
|
||||||
partial := carry + a[index]
|
partial := carry + a[index]
|
||||||
sum[index] = u64(partial) & max_digit
|
sum[index] = partial & max_digit
|
||||||
carry = u64(partial >> digit_bits)
|
carry = partial >> digit_bits
|
||||||
}
|
}
|
||||||
|
|
||||||
sum[larger_limit] = carry
|
sum[a.len] = carry
|
||||||
shrink_tail_zeros(mut sum)
|
shrink_tail_zeros(mut sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue