abacus: bignum: move do_halve & is_odd up

This commit is contained in:
Bruno BELANYI 2021-08-23 18:59:43 +02:00
parent 31dcb91a26
commit a99f60dcd4

View file

@ -30,6 +30,33 @@ void trim_leading_zeros(digits_type& num) {
num.erase(it.base(), num.end()); num.erase(it.base(), num.end());
} }
// More optimised than full-on div_mod
digits_type do_halve(digits_type num) {
assert(num.size() != 0);
int carry = 0;
for (auto i = num.rbegin(); i != num.rend(); ++i) {
auto const was_odd = (*i % 2) == 1;
*i /= 2;
*i += carry;
if (was_odd) {
carry = BASE / 2;
}
}
trim_leading_zeros(num);
return num;
}
bool is_odd(digits_type const& num) {
if (num.size() == 0) {
return false;
}
return (num.front() % 2) == 1;
}
digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { digits_type do_addition(digits_type const& lhs, digits_type const& rhs) {
int carry = 0; int carry = 0;
digits_type res; digits_type res;
@ -136,33 +163,6 @@ std::pair<digits_type, digits_type> do_div_mod(digits_type const& lhs,
return std::make_pair(quotient, remainder); return std::make_pair(quotient, remainder);
} }
// More optimised than full-on div_mod
digits_type do_halve(digits_type num) {
assert(num.size() != 0);
int carry = 0;
for (auto i = num.rbegin(); i != num.rend(); ++i) {
auto const was_odd = (*i % 2) == 1;
*i /= 2;
*i += carry;
if (was_odd) {
carry = BASE / 2;
}
}
trim_leading_zeros(num);
return num;
}
bool is_odd(digits_type const& num) {
if (num.size() == 0) {
return false;
}
return (num.front() % 2) == 1;
}
digits_type do_pow(digits_type lhs, digits_type rhs) { digits_type do_pow(digits_type lhs, digits_type rhs) {
assert(rhs.size() != 0); assert(rhs.size() != 0);