mirror of
https://github.com/Z3Prover/z3
synced 2025-05-01 21:05:52 +00:00
cleanup for pull request
This commit is contained in:
parent
6e508d4221
commit
a8586746be
7 changed files with 212 additions and 170 deletions
|
@ -16,7 +16,8 @@ Author:
|
|||
Revision History:
|
||||
|
||||
--*/
|
||||
#include<sstream>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include "util/mpz.h"
|
||||
#include "util/buffer.h"
|
||||
#include "util/trace.h"
|
||||
|
@ -1725,6 +1726,102 @@ void mpz_manager<SYNCH>::display_smt2(std::ostream & out, mpz const & a, bool de
|
|||
}
|
||||
}
|
||||
|
||||
template<bool SYNCH>
|
||||
void mpz_manager<SYNCH>::display_hex(std::ostream & out, mpz const & a, unsigned num_bits) const {
|
||||
SASSERT(num_bits % 4 == 0);
|
||||
std::ios fmt(nullptr);
|
||||
fmt.copyfmt(out);
|
||||
out << std::hex;
|
||||
if (is_small(a)) {
|
||||
out << std::setw(num_bits/4) << std::setfill('0') << get_uint64(a);
|
||||
} else {
|
||||
#ifndef _MP_GMP
|
||||
digit_t *ds = digits(a);
|
||||
unsigned sz = size(a);
|
||||
unsigned bitSize = sz * sizeof(digit_t) * 8;
|
||||
unsigned firstDigitSize;
|
||||
if (num_bits >= bitSize) {
|
||||
firstDigitSize = sizeof(digit_t) * 2;
|
||||
|
||||
for (unsigned i = 0; i < (num_bits - bitSize)/4; ++i) {
|
||||
out << "0";
|
||||
}
|
||||
} else {
|
||||
firstDigitSize = num_bits % (sizeof(digit_t) * 8) / 4;
|
||||
}
|
||||
|
||||
out << std::setfill('0') << std::setw(firstDigitSize) << ds[sz-1] << std::setw(sizeof(digit_t)*2);
|
||||
for (unsigned i = 1; i < sz; ++i) {
|
||||
out << ds[sz-i-1];
|
||||
}
|
||||
#else
|
||||
// GMP version
|
||||
size_t sz = mpz_sizeinbase(*(a.m_ptr), 16);
|
||||
unsigned requiredLength = num_bits / 4;
|
||||
unsigned padding = requiredLength > sz ? requiredLength - sz : 0;
|
||||
sbuffer<char, 1024> buffer(sz, 0);
|
||||
mpz_get_str(buffer.c_ptr(), 16, *(a.m_ptr));
|
||||
for (unsigned i = 0; i < padding; ++i) {
|
||||
out << "0";
|
||||
}
|
||||
out << buffer.c_ptr() + (sz > requiredLength ? sz - requiredLength : 0);
|
||||
#endif
|
||||
}
|
||||
out.copyfmt(fmt);
|
||||
out << std::dec;
|
||||
}
|
||||
|
||||
void display_binary_data(std::ostream &out, unsigned val, unsigned numBits) {
|
||||
SASSERT(numBits <= sizeof(unsigned)*8);
|
||||
for (int shift = numBits-1; shift >= 0; --shift) {
|
||||
if (val & (1 << shift)) {
|
||||
out << "1";
|
||||
} else {
|
||||
out << "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<bool SYNCH>
|
||||
void mpz_manager<SYNCH>::display_bin(std::ostream & out, mpz const & a, unsigned num_bits) const {
|
||||
if (is_small(a)) {
|
||||
display_binary_data(out, get_uint64(a), num_bits);
|
||||
} else {
|
||||
#ifndef _MP_GMP
|
||||
digit_t *ds = digits(a);
|
||||
unsigned sz = size(a);
|
||||
const unsigned digitBitSize = sizeof(digit_t) * 8;
|
||||
unsigned bitSize = sz * digitBitSize;
|
||||
unsigned firstDigitLength;
|
||||
if (num_bits > bitSize) {
|
||||
firstDigitLength = 0;
|
||||
for (unsigned i = 0; i < (num_bits - bitSize); ++i) {
|
||||
out << "0";
|
||||
}
|
||||
} else {
|
||||
firstDigitLength = num_bits % digitBitSize;
|
||||
}
|
||||
for (unsigned i = 0; i < sz; ++i) {
|
||||
if (i == 0 && firstDigitLength != 0) {
|
||||
display_binary_data(out, ds[sz-1], firstDigitLength);
|
||||
} else {
|
||||
display_binary_data(out, ds[sz-i-1], digitBitSize);
|
||||
}
|
||||
}
|
||||
#else
|
||||
// GMP version
|
||||
size_t sz = mpz_sizeinbase(*(a.m_ptr), 2);
|
||||
unsigned padding = num_bits > sz ? num_bits - sz : 0;
|
||||
sbuffer<char, 1024> buffer(sz, 0);
|
||||
mpz_get_str(buffer.c_ptr(), 2, *(a.m_ptr));
|
||||
for (unsigned i = 0; i < padding; ++i) {
|
||||
out << "0";
|
||||
}
|
||||
out << buffer.c_ptr() + (sz > num_bits ? sz - num_bits : 0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
template<bool SYNCH>
|
||||
std::string mpz_manager<SYNCH>::to_string(mpz const & a) const {
|
||||
std::ostringstream buffer;
|
||||
|
|
|
@ -99,7 +99,6 @@ class mpz {
|
|||
friend class mpbq;
|
||||
friend class mpbq_manager;
|
||||
friend class mpz_stack;
|
||||
friend class bv_util;
|
||||
mpz & operator=(mpz const & other) { UNREACHABLE(); return *this; }
|
||||
public:
|
||||
mpz(int v):m_val(v), m_kind(mpz_small), m_owner(mpz_self), m_ptr(nullptr) {}
|
||||
|
@ -135,7 +134,6 @@ inline void swap(mpz & m1, mpz & m2) { m1.swap(m2); }
|
|||
|
||||
template<bool SYNCH = true>
|
||||
class mpz_manager {
|
||||
friend class bv_util;
|
||||
mutable small_object_allocator m_allocator;
|
||||
mutable omp_nest_lock_t m_lock;
|
||||
#define MPZ_BEGIN_CRITICAL() if (SYNCH) omp_set_nest_lock(&m_lock);
|
||||
|
@ -594,6 +592,17 @@ public:
|
|||
*/
|
||||
void display_smt2(std::ostream & out, mpz const & a, bool decimal) const;
|
||||
|
||||
/**
|
||||
\brief Displays the num_bits least significant bits of a mpz number in hexadecimal format.
|
||||
num_bits must be divisible by 4.
|
||||
*/
|
||||
void display_hex(std::ostream & out, mpz const & a, unsigned num_bits) const;
|
||||
|
||||
/**
|
||||
\brief Displays the num_bits least significant bits of a mpz number in binary format.
|
||||
*/
|
||||
void display_bin(std::ostream & out, mpz const & a, unsigned num_bits) const;
|
||||
|
||||
static unsigned hash(mpz const & a);
|
||||
|
||||
static bool is_one(mpz const & a) {
|
||||
|
|
|
@ -95,6 +95,10 @@ public:
|
|||
|
||||
void display_smt2(std::ostream & out) const { return m().display_smt2(out, m_val, false); }
|
||||
|
||||
void display_hex(std::ostream & out, unsigned num_bits) const { SASSERT(is_int()); return m().display_hex(out, m_val.numerator(), num_bits); }
|
||||
|
||||
void display_bin(std::ostream & out, unsigned num_bits) const { SASSERT(is_int()); return m().display_bin(out, m_val.numerator(), num_bits); }
|
||||
|
||||
bool is_uint64() const { return m().is_uint64(m_val); }
|
||||
|
||||
bool is_int64() const { return m().is_int64(m_val); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue