Jump to content

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.


Link to comment
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,


Edited by Philipp A. Hartmann
Link to comment
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 :-)



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.

Link to comment
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) ));


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...