Optimizing a floating point division and conversion operation

后端 未结 4 1366
迷失自我
迷失自我 2020-12-19 21:37

I have the following formula

float mean = (r+b+g)/3/255.0f;

I want to speed it up. There are the following preconditions

0         


        
4条回答
  •  情歌与酒
    2020-12-19 21:54

    As shown by Andrew, the original function is not optimized at all. The compiler couldn't because you were dividing the sum first by an integer and then by a float. That's not the same as multiplying by the aforementioned average scale factor. If you would change (r+g+b)/3/255.0f into (r+g+b)/3.0f/255.0f, the compiler might optimize it to use fimull automatically.

提交回复
热议问题