Altera_Forum
Honored Contributor
17 years agoC2H fixed point mpy
I had an array of fixed point numbers, log values actually. 10 bit integer 22 bit fraction. Needed the sum of squares to calculate standard deviation.
In regular C this works okay ...
long long VkSDV(int mean, alt_u16 NumPoints)
{
int * QXP = QXvector;
long long SumSquare = 0;
int val;
do {
val = *QXP++ - mean ;
SumSquare += ((long long)val * (long long)val)>>22;
} while (--NumPoints > 0);
return SumSquare;
}
But that is not supported in C2H. --- Quote Start --- Actually it is supported ... false correlation when some other problem tricked the compiler into apparent complaint about the long long cast. Nevermind. --- Quote End --- A C2H workaround that seems to work is ...
do {
val = *QXP++ - mean ;
SumSquare += val * val;
} while (--NumPoints > 0);
Which produces overflow results ... Then I opened accelerator_Kit2C70_VkSDV.v Located the point where the multiplier is input . --- Quote Start --- assign accelerator_Kit2C70_VkSDV_multiplier_resource0_res0 = lpm_multiply_result0[31 : 0]; --- Quote End --- As you can see it limits the 64 bit result to the lowest 32 bits … Modified it like this … --- Quote Start --- assign accelerator_Kit2C70_VkSDV_multiplier_resource0_res0 = lpm_multiply_result0[53 : 22]; --- Quote End --- To implement a >> 22 shift … Compiled the Quartus project, tried it and it produced the same result as the regular C function.