abacus: add initial bignum library

This commit is contained in:
Bruno BELANYI 2021-08-20 16:35:34 +02:00
parent ff35faa705
commit 12c8b6b114
4 changed files with 74 additions and 0 deletions

37
src/bignum/bignum.cc Normal file
View file

@ -0,0 +1,37 @@
#include "bignum.hh"
#include <algorithm>
#include <cmath>
namespace abacus::bignum {
BigNum::BigNum(std::int64_t number) {
if (number == 0) {
return;
}
if (number < 0) {
sign_ = -1;
} else {
sign_ = 1;
}
auto abs = static_cast<std::uint64_t>(std::abs(number));
do {
digits_.push_back(abs % 10);
abs /= 10;
} while (abs);
}
void BigNum::canonicalize() {
auto const it = std::find_if(digits_.rbegin(), digits_.rend(),
[](auto v) { return v != 0; });
digits_.erase(it.base(), digits_.end());
if (digits_.size() == 0) {
sign_ = 0;
}
}
} // namespace abacus::bignum

20
src/bignum/bignum.hh Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <vector>
#include <cstdint>
namespace abacus::bignum {
class BigNum {
public:
explicit BigNum(std::int64_t number);
private:
void canonicalize();
std::vector<std::uint8_t> digits_{};
int sign_ = 0;
};
} // namespace abacus::bignum

16
src/bignum/meson.build Normal file
View file

@ -0,0 +1,16 @@
bignum_sources = files(
'bignum.cc',
'bignum.hh',
)
bignum_inc = include_directories('.')
bignum_lib = library(
'bignum',
sources: bignum_sources,
)
bignum = declare_dependency(
link_with: bignum_lib,
include_directories: bignum_inc,
)

View file

@ -2,6 +2,7 @@ abacus_sources = files(
'abacus.cc', 'abacus.cc',
) )
subdir('bignum')
subdir('parse') subdir('parse')
abacus = executable( abacus = executable(