7 #ifndef JLM_RVSDG_BITSTRING_VALUE_REPRESENTATION_HPP
8 #define JLM_RVSDG_BITSTRING_VALUE_REPRESENTATION_HPP
39 throw util::Error(
"Value cannot be represented with the given number of bits.");
41 for (
size_t n = 0; n <
nbits; ++n)
43 data_.push_back(
'0' + (value & 1));
53 for (
size_t n = 0; n < strlen(s); n++)
55 if (s[n] !=
'0' && s[n] !=
'1' && s[n] !=
'X' && s[n] !=
'D')
57 data_.push_back(s[n]);
77 lor(
char a,
char b)
const noexcept
101 lxor(
char a,
char b)
const noexcept
131 land(
char a,
char b)
const noexcept
155 carry(
char a,
char b,
char c)
const noexcept
161 add(
char a,
char b,
char c)
const noexcept
182 if (divisor.
nbits() == 0)
185 for (
size_t n = 0; n <
nbits(); n++)
187 remainder = remainder.
shl(1);
189 if (remainder.
uge(divisor) ==
'1')
191 remainder = remainder.
sub(divisor);
192 quotient[
nbits() - n - 1] =
'1';
204 for (
size_t i = 0; i < factor1.
nbits(); i++)
207 for (
size_t j = 0; j < factor2.
nbits(); j++)
209 char s =
land(factor1[i], factor2[j]);
210 char nc =
carry(s, product[i + j], c);
211 product[i + j] =
add(s, product[i + j], c);
234 data_ = std::move(other.data_);
255 return data_ == other.data_;
261 return !(*
this == other);
279 if (
nbits() != other.size())
282 for (
size_t n = 0; n < other.size(); n++)
284 if (
data_[n] != other[n])
294 return !(*
this == other);
306 for (
auto bit :
data_)
318 for (
auto bit :
data_)
320 if (bit ==
'X' || bit ==
'D')
330 return sign() ==
'1';
342 slice(
size_t low,
size_t high)
const
344 if (high <= low || high >
nbits())
379 return std::string(
data_.begin(),
data_.end());
396 for (
size_t n = 1; n <
nbits(); n++)
406 t1[t1.nbits() - 1] =
lnot(t1.sign());
419 for (
size_t n = 0; n <
nbits(); n++)
429 t1[t1.nbits() - 1] =
lnot(t1.sign());
442 for (
size_t n = 0; n <
nbits(); n++)
486 for (
size_t n = 0; n <
nbits(); n++)
488 sum[n] =
add(
data_[n], other[n], c);
503 for (
size_t n = 0; n <
nbits(); n++)
517 for (
size_t n = 0; n <
nbits(); n++)
518 result[n] =
lor(
data_[n], other[n]);
531 for (
size_t n = 0; n <
nbits(); n++)
548 for (
size_t n = 0; n <
nbits(); n++)
551 result[n] =
add(tmp,
'0', c);
552 c =
carry(tmp,
'0', c);
567 if (shift >=
nbits())
571 return result.
zext(shift);
577 if (shift >=
nbits())
581 return result.
sext(shift);
587 if (shift >=
nbits())
598 udiv(other, quotient, remainder);
607 udiv(other, quotient, remainder);
616 if (dividend.is_negative())
617 dividend = dividend.neg();
620 divisor = divisor.
neg();
623 dividend.udiv(divisor, quotient, remainder);
626 remainder = remainder.
neg();
629 quotient = quotient.
neg();
639 if (dividend.is_negative())
640 dividend = dividend.neg();
643 divisor = divisor.
neg();
646 dividend.udiv(divisor, quotient, remainder);
649 remainder = remainder.
neg();
652 quotient = quotient.
neg();
665 mul(*
this, other, product);
679 mul(factor1, factor2, product);
693 mul(factor1, factor2, product);
BitValueRepresentation shl(size_t shift) const
void udiv(const BitValueRepresentation &divisor, BitValueRepresentation "ient, BitValueRepresentation &remainder) const
BitValueRepresentation land(const BitValueRepresentation &other) const
BitValueRepresentation smod(const BitValueRepresentation &other) const
BitValueRepresentation(const char *s)
BitValueRepresentation sext(size_t nbits) const
BitValueRepresentation(const BitValueRepresentation &other)
void mul(const BitValueRepresentation &factor1, const BitValueRepresentation &factor2, BitValueRepresentation &product) const
size_t nbits() const noexcept
char sgt(const BitValueRepresentation &other) const
BitValueRepresentation & operator=(const BitValueRepresentation &other)
char sge(const BitValueRepresentation &other) const
BitValueRepresentation mul(const BitValueRepresentation &other) const
BitValueRepresentation sdiv(const BitValueRepresentation &other) const
bool operator==(int64_t value) const
char ne(const BitValueRepresentation &other) const
BitValueRepresentation & operator=(BitValueRepresentation &&other)
bool is_defined() const noexcept
char ule(const BitValueRepresentation &other) const
bool operator==(const std::string &other) const noexcept
char ult(const BitValueRepresentation &other) const
BitValueRepresentation neg() const
BitValueRepresentation umulh(const BitValueRepresentation &other) const
char carry(char a, char b, char c) const noexcept
char lor(char a, char b) const noexcept
char sign() const noexcept
bool operator!=(const std::string &other) const noexcept
bool operator==(const BitValueRepresentation &other) const noexcept
char ugt(const BitValueRepresentation &other) const
BitValueRepresentation concat(const BitValueRepresentation &other) const
const char & operator[](size_t n) const
std::vector< char > data_
char eq(const BitValueRepresentation &other) const
BitValueRepresentation(size_t nbits, int64_t value)
bool operator!=(const BitValueRepresentation &other) const noexcept
char lxor(char a, char b) const noexcept
BitValueRepresentation lxor(const BitValueRepresentation &other) const
BitValueRepresentation sub(const BitValueRepresentation &other) const
void Append(const BitValueRepresentation &other)
BitValueRepresentation lor(const BitValueRepresentation &other) const
char slt(const BitValueRepresentation &other) const
BitValueRepresentation ashr(size_t shift) const
BitValueRepresentation(BitValueRepresentation &&other)
BitValueRepresentation zext(size_t nbits) const
BitValueRepresentation udiv(const BitValueRepresentation &other) const
static BitValueRepresentation repeat(size_t nbits, char bit)
BitValueRepresentation shr(size_t shift) const
bool operator!=(int64_t value) const
char & operator[](size_t n)
BitValueRepresentation umod(const BitValueRepresentation &other) const
BitValueRepresentation slice(size_t low, size_t high) const
BitValueRepresentation smulh(const BitValueRepresentation &other) const
bool is_negative() const noexcept
BitValueRepresentation lnot() const
char sle(const BitValueRepresentation &other) const
char land(char a, char b) const noexcept
BitValueRepresentation add(const BitValueRepresentation &other) const
char uge(const BitValueRepresentation &other) const
char lnot(char a) const noexcept
char add(char a, char b, char c) const noexcept
bool is_known() const noexcept
static std::string strfmt(Args... args)