Skip to content

Commit

Permalink
Add rt_T to XlsInt, making it easier to manipulate the type of the re…
Browse files Browse the repository at this point in the history
…sulting value using the various ac_datatypes.

PiperOrigin-RevId: 724469000
  • Loading branch information
JoshVarga authored and copybara-github committed Feb 7, 2025
1 parent 132a24c commit 42d1cff
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
57 changes: 57 additions & 0 deletions xls/contrib/xlscc/synth_only/xls_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,24 @@ struct [[hls_no_tuple]] BitElemRef {
bool v;
};

// Defines the result types for each operation using ac_int.
// T is the second operand while rt_T<XlsInt<W, S>> is the first operand.
// Since the operand order is reversed, the result types for non-commutative
// operations are also reversed.
template <typename T>
struct rt_ac_int_T {
template <int W, bool S>
struct op1 {
typedef typename T::template rt_T<XlsInt<W, S> >::mult mult;
typedef typename T::template rt_T<XlsInt<W, S> >::plus plus;
typedef typename T::template rt_T<XlsInt<W, S> >::minus2 minus;
typedef typename T::template rt_T<XlsInt<W, S> >::minus minus2;
typedef typename T::template rt_T<XlsInt<W, S> >::logic logic;
typedef typename T::template rt_T<XlsInt<W, S> >::div2 div;
typedef typename T::template rt_T<XlsInt<W, S> >::div div2;
};
};

template <int Width, bool Signed = true>
class [[hls_synthetic_int]] XlsInt : public XlsIntBase<Width, Signed> {
public:
Expand Down Expand Up @@ -590,6 +608,11 @@ class [[hls_synthetic_int]] XlsInt : public XlsIntBase<Width, Signed> {
static const int i_width = Width;
static const bool sign = Signed;

template <typename T>
struct map {
typedef T t;
};

// Defines the result types for each operation based on ac_int
template <int ToW, bool ToSign>
struct rt
Expand All @@ -604,6 +627,22 @@ class [[hls_synthetic_int]] XlsInt : public XlsIntBase<Width, Signed> {
typedef XlsInt ident;
};

template <typename T>
struct rt_T {
typedef typename map<T>::t map_T;
typedef typename rt_ac_int_T<map_T>::template op1<Width, Signed>::mult mult;
typedef typename rt_ac_int_T<map_T>::template op1<Width, Signed>::plus plus;
typedef
typename rt_ac_int_T<map_T>::template op1<Width, Signed>::minus minus;
typedef
typename rt_ac_int_T<map_T>::template op1<Width, Signed>::minus2 minus2;
typedef
typename rt_ac_int_T<map_T>::template op1<Width, Signed>::logic logic;
typedef typename rt_ac_int_T<map_T>::template op1<Width, Signed>::div div;
typedef typename rt_ac_int_T<map_T>::template op1<Width, Signed>::div2 div2;
typedef XlsInt arg1;
};

struct rt_unary : public ac_datatypes::ac_int<Width, Signed>::rt_unary {
typedef XlsInt<rt_unary::neg_w, rt_unary::neg_s> neg;
typedef XlsInt<Width + !Signed, true> bnot;
Expand Down Expand Up @@ -941,6 +980,24 @@ class [[hls_synthetic_int]] XlsInt : public XlsIntBase<Width, Signed> {
friend class XlsInt;
};

template <int W2, bool S2>
struct rt_ac_int_T<XlsInt<W2, S2> > {
typedef XlsInt<W2, S2> i2_t;
template <int W, bool S>
struct op1 {
typedef XlsInt<W, S> i_t;
typedef typename i_t::template rt<W2, S2>::mult mult;
typedef typename i_t::template rt<W2, S2>::plus plus;
typedef typename i_t::template rt<W2, S2>::minus minus;
typedef typename i2_t::template rt<W, S>::minus minus2;
typedef typename i_t::template rt<W2, S2>::logic logic;
typedef typename i_t::template rt<W2, S2>::div div;
typedef typename i2_t::template rt<W, S>::div div2;
typedef typename i_t::template rt<W2, S2>::mod mod;
typedef typename i2_t::template rt<W, S>::mod mod2;
};
};

template <int Width, bool Signed>
inline std::ostream &operator<<(std::ostream &os,
const XlsInt<Width, Signed> &x) {
Expand Down
22 changes: 22 additions & 0 deletions xls/contrib/xlscc/unit_tests/xls_int_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,28 @@ TEST_F(XlsIntTest, XlsToLongBounds) {
})";
RunAcDatatypeTest({{"a", 3}}, 3, content, xabsl::SourceLocation::current());
}

TEST_F(XlsIntTest, XlsIntHasNbits) {
const std::string content = R"(
#include "xls_int.h"
long long my_package() {
return ac_datatypes::ac::nbits<6>::val;
})";
RunAcDatatypeTest({}, 3, content, xabsl::SourceLocation::current());
}

TEST_F(XlsIntTest, XlsIntHasRT_T) {
const std::string content = R"(
#include "xls_int.h"
long long my_package(long long a) {
typedef XlsInt<4, true> d_t;
typedef d_t::rt_T<XlsInt<8, false>>::mult d_t2;
d_t2 result = a;
return result.to_long();
})";
RunAcDatatypeTest({{"a", 3}}, 3, content, xabsl::SourceLocation::current());
}

} // namespace

} // namespace xlscc

0 comments on commit 42d1cff

Please sign in to comment.