topic Re: Q (number format) Multiply in NiosĀ® II Embedded Design Suite (EDS)
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173943#M43387
<P>Thanks for your help. </P><P></P> <P></P>I'll consider using the package, but I'll need to convert all the code over. <P></P> <P></P>If I continue down the same path using std_logic_vector, I understand that the product will be a Q62.2 value. How would I scale that value to be 32 bits? Would I use the upper 32-bits? <P></P> <P></P>temp = CONV_STD_LOGIC_VECTOR(CONV_INTEGER(CONV_INTEGER(in tra_clk_offset) * CONV_INTEGER(step_size)), 64); <P></P> <P></P>amplitude = temp(64 downto 32); <P></P> <P></P>I know "CONV_STD_LOGIC_VECTOR" will not work with a 64 bit value, just using this as an example.Thu, 04 Oct 2012 04:49:25 GMTAltera_Forum2012-10-04T04:49:25ZQ (number format) Multiply
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173940#M43384
<P>I'm working on a project that uses a 32-bit accumulator, which it's input is a 32-bit step size (unsigned Q30.2) value. The accumulator can rollover when preforming a large number of iterations or working with a large step size. I need to be able to calculate the final value of the 32-bit accumulator overtime. The step size will be added to the accumulator every 8 ns. The number of iterations (8 ns increment) will be giving as a 32-bit clk_offset value. </P><P></P> <P></P>Will the following code work? <P></P> <P></P>amplitude = CONV_STD_LOGIC_VECTOR(CONV_INTEGER(CONV_INTEGER(intra_clk_offset) * CONV_INTEGER(step_size)), 32); <P></P> <P></P>Since the step_size is a Q30.2 number, shouldn't I shift the value left? <P></P> <P></P>Thanks,Wed, 03 Oct 2012 20:15:25 GMThttps://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173940#M43384Altera_Forum2012-10-03T20:15:25ZRe: Q (number format) Multiply
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173941#M43385
<P>Urgh. The problem of fixed point using non-fixed point libraries, and the use of non-standard legacy VHDL libraries. (and why o why does everything have to be a std_logic_vector - you can use integers and unsigned and any other type in ports you know) </P><P></P> <P></P>Anyway, back to code. You are going to overflow big time. If box tr_clk_offset and step_size are 32 bits, the output needs to be 64 bits, not 32., with a Q62.2 output. <P></P> <P></P>PS. Have a look into the new fixed point VHDL libraries (Quartus may one day support the real VHDL 2008 files, but until then you can easily use the VHDL93 versions avaiable from <A href="http://www.vhdl.org/fphdl">www.vhdl.org/fphdl</A>) <P></P> <P></P>it allows you to do stuff like this: <P></P> <P></P><CODE>
signal tr_clk_offset : ufixed(31 downto 0);
signal step_size : ufixed(29 downto -2); --Q30.2
signal amplitude : ufixed(61 downto -2);
amplitude <= tr_clk_offset * step_size;
</CODE> <P></P> <P></P>Look - no hideous type converting (which is what you get when you insist everything has to be a std_logic_vector. Btw, if you decide to use this package, you'll have to forget about using std_logic_unsigned, std_logic_signed and std_logic_arith and use numeric_std instead (which is only a good thing).Wed, 03 Oct 2012 21:40:44 GMThttps://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173941#M43385Altera_Forum2012-10-03T21:40:44ZRe: Q (number format) Multiply
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173942#M43386
<P>PS. No, you dont need to shift step_size, because its really just an integer with an implied 2^-2 offset. This means the output is Q62.2</P>Wed, 03 Oct 2012 21:42:06 GMThttps://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173942#M43386Altera_Forum2012-10-03T21:42:06ZRe: Q (number format) Multiply
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173943#M43387
<P>Thanks for your help. </P><P></P> <P></P>I'll consider using the package, but I'll need to convert all the code over. <P></P> <P></P>If I continue down the same path using std_logic_vector, I understand that the product will be a Q62.2 value. How would I scale that value to be 32 bits? Would I use the upper 32-bits? <P></P> <P></P>temp = CONV_STD_LOGIC_VECTOR(CONV_INTEGER(CONV_INTEGER(in tra_clk_offset) * CONV_INTEGER(step_size)), 64); <P></P> <P></P>amplitude = temp(64 downto 32); <P></P> <P></P>I know "CONV_STD_LOGIC_VECTOR" will not work with a 64 bit value, just using this as an example.Thu, 04 Oct 2012 04:49:25 GMThttps://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173943#M43387Altera_Forum2012-10-04T04:49:25ZRe: Q (number format) Multiply
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173944#M43388
<P> </P><P></P> --- Quote Start --- <P></P> <P></P>If I continue down the same path using std_logic_vector, I understand that the product will be a Q62.2 value. How would I scale that value to be 32 bits? Would I use the upper 32-bits? <P></P> <P></P> --- Quote End --- <P></P>It 'depends' :) <P></P> <P></P>I'll explain why with a simplified example ... <P></P> <P></P>If you have 3-bits, you can represent the signed values -4, -3, -2, -1, 0, 1, 2, 3. If you scale them by 4, you get the Q0.2 numbers -1.0, -0.75, -0.5, -0.25, 0, 0.25, 0.5. 0.75. <P></P> <P></P>If you multiply two of these numbers together, the largest value you can get is -1.0 x -1.0 = +1.0, and the smallest is 0.25 x 0.25 = 0.0625, so your product needs to have a Q1.4 representation. <P></P> <P></P>In general, Qm1.n1 x Qm2.n2 = Q(m1+m2+1).(n1+n2). <P></P> <P></P>However, if you don't like asymmetry, or like wasting bits, you can use a slight variation on the numbering scheme where you eliminate the most negative value, i.e., your signed-symmetric Q0.2 numbers are -0.75, -0.5, -0.25, 0, 0.25, 0.5. 0.75 (any -1.0 values in an input data stream are replaced with -0.75 values - a quantization error no worse than the one that happens to positive values). With this numeric representation at the input to your multiplier, you never get the product 1.0, so you can eliminate that bit when you bit-slice to convert your Q1.4 product back into Q0.2 format (or whatever narrower bit-width you plan on using). <P></P> <P></P>In that case, you'd keep the MSB (the sign bit), drop the next bit (the bit needed to represent +1.0), and then keep the remaining bits you want to keep, and eliminate the LSBs. <P></P> <P></P>Depending on your application, you might actually want to round the result before discarding the LSBs ... but thats another discussion. <P></P> <P></P>Cheers, <P></P>DaveThu, 04 Oct 2012 05:28:17 GMThttps://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173944#M43388Altera_Forum2012-10-04T05:28:17ZRe: Q (number format) Multiply
https://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173945#M43389
<P> </P><P></P> --- Quote Start --- <P></P> <P></P>I know "CONV_STD_LOGIC_VECTOR" will not work with a 64 bit value, just using this as an example. <P></P> --- Quote End --- <P></P> <P></P> <P></P>This is only because of the limitations of the integer type. If you kept it all in the unsiged (or even better for this application, the ufixed type) there are no limitiations. <P></P> <P></P>Basically, to do what you want, you cannot use integers at all, because they are so large. You will get an overflow error when you try and similate because the result of multiplying the two integers is too large.Thu, 04 Oct 2012 14:53:25 GMThttps://community.intel.com/t5/Nios-II-Embedded-Design-Suite/Q-number-format-Multiply/m-p/173945#M43389Altera_Forum2012-10-04T14:53:25Z