Speedup a short to float cast?

后端 未结 7 2011
孤独总比滥情好
孤独总比滥情好 2020-12-17 01:39

I have a short to float cast in C++ that is bottlenecking my code.

The code translates from a hardware device buffer which is natively shorts, this represents the in

相关标签:
7条回答
  • 2020-12-17 02:04

    You could try to approximate the expression

    float factor = 1.0f/value;
    

    by an fraction numerator/denomitator where both numerator and denominator are ints. This can be done to the precision you need in your application like

    int denominator = 10000;
    int numerator = factor * denominator;
    

    Then you can do your computation in integer arithmetics like

    int value = source[i];
    destination[i] = (value * numerator) / numerator;
    

    You have to take care of overflows, perhaps you need to switch to long (or even long long on 64bit systems) for the calculation.

    0 讨论(0)
提交回复
热议问题