Jump to content
richpodraza

Trouble with this addition operation...

Recommended Posts

I have two sc_bv<256> objects and am trying to add a range selection of each and store the result in a sc_bv<128>. Here is the code

sc_bv<256> wdata;
sc_bv<256> rdata;
// wdata and rdata get populated here
sc_bv<128> result;

result = rdata.range(127, 0) + wdata.range(63, 0); // compiler error!

I have tried things like to_uint() following the range() functions, but that doesn't give me the range I want (cuts it off at 32 bits) and I don't think even to_longuint() is enough.

I tried to convert these to sc_bigint<128>, but that gives a compiler error too.

sc_bigint<128> temp1 = rdata.range(127, 0); // compiler error!

Can anyone recommend a solution? It seems trivial to me but I've been looking at this for a while and can't find one.

Thanks!

Share this post


Link to post
Share on other sites

You need to jump through a temporary bit-vector (at least for the widerer range), I'm afraid.

You can try something like the following untested snippet

sc_biguint<128> temp1  = sc_bv<128>(rdata.range(127,0));
temp1 += wdata.range(63,0).to_uint64();
result = temp1;

Greetings from Oldenburg,

Philipp

Edited by Philipp A. Hartmann

Share this post


Link to post
Share on other sites

Hi, the first problem is that sc_bv doesn't have arithmetic operators defined, so you have to convert to an integer type (e.g. sc_bigint).

The second problem is that the range() method returns a hidden proxy class, and there are two ambiguous type conversions from that class. It's counter-intuitive, but you have to cast to a known type on the right-hand side (not the type on the left-hand side), then let operator= do its work.

result = sc_biguint<128>( sc_bv<128>(rdata.range(127, 0))) + sc_biguint<64>(sc_bv<64>(wdata.range(63, 0)));

The section sc_bv<128>(rdata.range(127,0)) converts the hidden proxy class to a bit vector, which can then be cast to an sc_biguint. The two sc_biguints produce an sc_biguint, which is converted to the result by the overloaded.operator=.

Obviously using temporary variables would make it a bit more readable :-)

regards

Alan

P.S. The advantage of the code I've shown is it would work with any widths as you never use conversion to native C++ types.

Share this post


Link to post
Share on other sites

A small addition to Alan's nice explanation:

P.S. The advantage of the code I've shown is it would work with any widths as you never use conversion to native C++ types.

If you don't target synthesis (which may not support this), you can avoid inconsistency errors due to the duplicated width information by using the arbitrary width base classes as well:

result = sc_unsigned(sc_bv_base( rdata.range(127, 0) ))
 + sc_unsigned(sc_bv_base( wdata.range(63, 0) ));

/Philipp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×