Jump to content

How to change the word length of "sc_fix" dynamically? Any examples?


Recommended Posts


please let me ask a trivial question.


How to change the integer bit length of "sc_fix" variable dynamically after its initialization?

As far as I know "sc_fix" variable can be configured dynamically, though "sc_fixed" is not.


Many thanks in advance.



int sc_main( int argv, char* argc[]) 



  sc_fix   var_1( 16, 4, SC_RND, SC_SAT);

  sc_fix   var_2( 16, 4, SC_RND, SC_SAT);

  sc_fix   var_3( 16, 4, SC_RND, SC_SAT);


  var_1 = 0.5;

  var_2 = 1.125;

  var_3 = 0.7;


  var_1 = var_2 + var_3;


   //  *** at this moment, I'd like to change var_1's integer bit length to 8. ***

  var_1 = sc_fix( 16, 8, SC_RND, SC_SAT);  // This does not work.


  // further processing,......


  return 0;

Link to comment
Share on other sites

sc_fix is the base class for sc_fixed. The size of an object of type sc_fix is set when the object is created (i.e. by the constructor) as opposed to sc_fixed where the size is set by the compiler (using the template parameter).


If you want to change the size, you will have to create a new object and copy the original value to it. ,e,g. using the copy constructor



sc_fix var_11 = sc_fix(var_1,16, 4, SC_RND, SC_SAT); //decalre and create a new variable





Link to comment
Share on other sites

I should have mentioned as "integer bit length" instead of just "length".

So the question was, how to change the integer bit length of sc_fix dynamically.

I have updated my original posting accordingly.


To dakupto's question:

The reason why I'm trying to change the integer bit length is, that I need to find the optimum integer bit length in my model so I'd like to make it dynamically programmable and find the optimum value through running simulation runs over many different values (for the integer bit length).


David Long, thank you for letting me know one technique that I can take advantage of.

Is there any technique to change the "integer bit length" dynamically?

Link to comment
Share on other sites

The integer bit length is set when the sc_fix object is created and cannot subsequently be varied. If you want to find the optimum representation, I would suggest an iterative approach where you copy the original number to a new object with a different word length on iteration until you find the best fit. If you want to affect the representation of multiple object from one place, have a look at the sc_context_switch (see sections 7.2.3 and 7.11 in the LRM).


You might also want to look at the sc_fxcast_switch. This can be used to turn fixed-point represention on and off (sc_fix reverts to floating point when off). This is good to see how your fixed-point representation compares against ideal floating point behaviour.




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...