richpodraza Posted March 7, 2013 Report Share Posted March 7, 2013 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! Quote Link to comment Share on other sites More sharing options...
Philipp A Hartmann Posted March 7, 2013 Report Share Posted March 7, 2013 (edited) 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 March 7, 2013 by Philipp A. Hartmann Quote Link to comment Share on other sites More sharing options...
apfitch Posted March 8, 2013 Report Share Posted March 8, 2013 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. Quote Link to comment Share on other sites More sharing options...
Philipp A Hartmann Posted March 8, 2013 Report Share Posted March 8, 2013 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 apfitch 1 Quote Link to comment Share on other sites More sharing options...
richpodraza Posted March 8, 2013 Author Report Share Posted March 8, 2013 Thanks guys, this general idea is what I needed! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.