sc_fox Posted April 22, 2013 Report Share Posted April 22, 2013 Hello, 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; } Quote Link to comment Share on other sites More sharing options...
dakupoto Posted April 23, 2013 Report Share Posted April 23, 2013 If you do not mind, may we know why you use the type 'sc_fix' or 'sc_fixed' and then want to change the size ? Does the 'fix' or 'fixed' not indicate that the variable is of fixed length ? Quote Link to comment Share on other sites More sharing options...
David Long Posted April 23, 2013 Report Share Posted April 23, 2013 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 Regards, Dave Quote Link to comment Share on other sites More sharing options...
sc_fox Posted April 23, 2013 Author Report Share Posted April 23, 2013 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? Quote Link to comment Share on other sites More sharing options...
David Long Posted April 23, 2013 Report Share Posted April 23, 2013 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. Regards, Dave maehne 1 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.