Could type punning signed to unsigned integers make bounds checking faster by eliminating the need for >= comparison?

后端 未结 2 697
半阙折子戏
半阙折子戏 2021-02-08 00:46

Say I had a really performance-critical loop in my program where I need to check if a point was inside a rectangle, but I know at compile time that the lower bounds are always g

2条回答
  •  执笔经年
    2021-02-08 01:12

    Maybe...

    Whilst on "paper" this would seem to allow you to perform only two comparisons rather than four (which is nice) - you cannot guarantee how this will perform. Most CPUs these days can perform multiple parallel operations simultaneously - and the four comparisons you have are easily computed in parallel.

    Your question depends on compiler, CPU and also the code before and after the check - so my answer is "maybe".

    Avoid casting x,y to a type that is of a different size than what they currently are - i.e. cast from int8_t to uint8_t is fine, int8_t to uint32_t might incur a penalty.

    Rewriting as you desire:

    if ( ( static_cast(x) < width ) &&
         ( static_cast(y) < length ) )
    

    Testing the performance delta is quite difficult, you will need to wrap your code with some assembly using the RDTSC instruction to catch the time before and after. You will likely also need to use the CPUID instruction to flush the pipeline as well.

    In short, to me your optimization seems reasonable, but probably won't yield much if anything. It will work though.

提交回复
热议问题