Skip to content

Commit

Permalink
numeral helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobR authored and NikolajBjorner committed Aug 1, 2022
1 parent e31926d commit 6eae27f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/util/mpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class mpf_manager {

void neg(mpf & o);
void neg(mpf const & x, mpf & o);

void swap(mpf& a, mpf& b) { a.swap(b); }

bool is_zero(mpf const & x);
bool is_neg(mpf const & x);
Expand Down
7 changes: 7 additions & 0 deletions src/util/mpz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ static uint64_t _trailing_zeros64(uint64_t x) {
}
#endif

unsigned trailing_zeros(uint32_t x) {
return static_cast<unsigned>(_trailing_zeros32(x));
}

unsigned trailing_zeros(uint64_t x) {
return static_cast<unsigned>(_trailing_zeros64(x));
}

#define _bit_min(x, y) (y + ((x - y) & ((int)(x - y) >> 31)))
#define _bit_max(x, y) (x - ((x - y) & ((int)(x - y) >> 31)))
Expand Down
3 changes: 3 additions & 0 deletions src/util/mpz.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Revision History:

unsigned u_gcd(unsigned u, unsigned v);
uint64_t u64_gcd(uint64_t u, uint64_t v);
unsigned trailing_zeros(uint64_t);
unsigned trailing_zeros(uint32_t);


#ifdef _MP_GMP
typedef unsigned digit_t;
Expand Down
2 changes: 1 addition & 1 deletion src/util/rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool rational::limit_denominator(rational &num, rational const& limit) {
return false;
}

bool rational::mult_inverse(unsigned num_bits, rational & result) {
bool rational::mult_inverse(unsigned num_bits, rational & result) const {
rational const& n = *this;
if (n.is_one()) {
result = n;
Expand Down
13 changes: 12 additions & 1 deletion src/util/rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ class rational {
friend inline rational numerator(rational const & r) { rational result; m().get_numerator(r.m_val, result.m_val); return result; }

friend inline rational denominator(rational const & r) { rational result; m().get_denominator(r.m_val, result.m_val); return result; }

friend inline rational inv(rational const & r) {
rational result;
m().inv(r.m_val, result.m_val);
return result;
}

rational & operator+=(rational const & r) {
m().add(m_val, r.m_val, m_val);
Expand Down Expand Up @@ -346,8 +352,13 @@ class rational {
bool is_power_of_two(unsigned & shift) const {
return m().is_power_of_two(m_val, shift);
}

bool is_power_of_two() const {
unsigned shift = 0;
return m().is_power_of_two(m_val, shift);
}

bool mult_inverse(unsigned num_bits, rational & result);
bool mult_inverse(unsigned num_bits, rational & result) const;

static rational const & zero() {
return m_zero;
Expand Down
4 changes: 2 additions & 2 deletions src/util/scoped_numeral.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class _scoped_numeral {
}

void swap(_scoped_numeral & n) {
m_num.swap(n.m_num);
m().swap(m_num, n.m_num);
}

void swap(numeral & n) {
m_num.swap(n);
m().swap(m_num, n);
}

_scoped_numeral & operator+=(numeral const & a) {
Expand Down

0 comments on commit 6eae27f

Please sign in to comment.