Optimisation of division in gcc

前端 未结 4 1032
無奈伤痛
無奈伤痛 2021-01-17 19:43

Here\'s some code (full program follows later in the question):

template 
T fizzbuzz(T n) {
    T count(0);
    #if CONST
        const T d         


        
4条回答
  •  花落未央
    2021-01-17 20:00

    Is there a known way of wrapping an int such that the compiler can discard the wrapping when optimising?

    Try passing WrappedInt by value. Then WrappedInts can be passed in registers. Pass-by-const-reference sometimes forces gcc to fall back to the stack for argument passing.

    About the int vs const int slowdown, I can only speculate that GCC is trying to play it safe in the face of aliasing. Basically, if it cannot prove that div is not an alias for another, more accessible variable, it cannot turn it into a constant. If you declare it const, GCC assumes it's not aliased and performs the conversion into a constant. Apart from the idivl, you should also see a memory fetch, once, when entering the function, as opposed to immediate values being used for the const case.

提交回复
热议问题