Force compiler to not optimize side-effect-less statements

前端 未结 11 1579
执念已碎
执念已碎 2021-01-11 11:25

I was reading some old game programming books and as some of you might know, back in that day it was usually faster to do bit hacks than do things the standard way. (Convert

相关标签:
11条回答
  • 2021-01-11 12:07

    In this case I suggest you make the function return the integer value:

    int float_to_int(float f)
    {
       return static_cast<int>(f);
    }
    

    Your calling code can then exercise it with a printf to guarantee it won't optimize it out. Also make sure float_to_int is in a separate compilation unit so the compiler can't play any tricks.

    extern int float_to_int(float f)
    int sum = 0;
    // start timing here
    for (int i = 0; i < 1000000; i++)
    {
       sum += float_to_int(1.0f);
    }
    // end timing here
    printf("sum=%d\n", sum);
    

    Now compare this to an empty function like:

    int take_float_return_int(float /* f */)
    {
       return 1;
    }
    

    Which should also be external.

    The difference in times should give you an idea of the expense of what you're trying to measure.

    0 讨论(0)
  • 2021-01-11 12:07

    You just need to skip to the part where you learn something and read the published Intel CPU optimisation manual.

    These quite clearly state that casting between float and int is a really bad idea because it requires a store from the int register to memory followed by a load into a float register. These operations cause a bubble in the pipeline and waste many precious cycles.

    0 讨论(0)
  • 2021-01-11 12:08

    Return the value?

    int float_to_int(float f)
    {
        return static_cast<int>(f); // has no side-effects
    }
    

    and then at the call site, you can sum all the return values up, and print out the result when the benchmark is done. The usual way to do this is to somehow make sure you depend on the result.

    You could use a global variable instead, but it seems like that'd generate more cache misses. Usually, simply returning the value to the caller (and making sure the caller actually does something with it) does the trick.

    0 讨论(0)
  • 2021-01-11 12:08

    GCC 4 does a lot of micro-optimizations now, that GCC 3.4 has never done. GCC4 includes a tree vectorizer that turns out to do a very good job of taking advantage of SSE and MMX. It also uses the GMP and MPFR libraries to assist in optimizing calls to things like sin(), fabs(), etc., as well as optimizing such calls to their FPU, SSE or 3D Now! equivalents.

    I know the Intel compiler is also extremely good at these kinds of optimizations.

    My suggestion is to not worry about micro-optimizations like this - on relatively new hardware (anything built in the last 5 or 6 years), they're almost completely moot.

    Edit: On recent CPUs, the FPU's fabs instruction is far faster than a cast to int and bit mask, and the fsin instruction is generally going to be faster than precalculating a table or extrapolating a Taylor series. A lot of the optimizations you would find in, for example, "Tricks of the Game Programming Gurus," are completely moot, and as pointed out in another answer, could potentially be slower than instructions on the FPU and in SSE.

    All of this is due to the fact that newer CPUs are pipelined - instructions are decoded and dispatched to fast computation units. Instructions no longer run in terms of clock cycles, and are more sensitive to cache misses and inter-instruction dependencies.

    Check the AMD and Intel processor programming manuals for all the gritty details.

    0 讨论(0)
  • 2021-01-11 12:11

    Assignment to a volatile variable shold never be optimized away, so this might give you the result you want:

    static volatile int i = 0;
    
    void float_to_int(float f)
    {
        i = static_cast<int>(f); // has no side-effects
    }
    
    0 讨论(0)
  • 2021-01-11 12:11

    What always worked on all compilers I used so far:

    extern volatile int writeMe = 0;
    
    void float_to_int(float f)
    {    
      writeMe = static_cast<int>(f); 
    }
    

    note that this skews results, boith methods should write to writeMe.

    volatile tells the compiler "the value may be accessed without your notice", thus the compiler cannot omit the calculation and drop the result. To block propagiation of input constants, you might need to run them through an extern volatile, too:

    extern volatile float readMe = 0;
    extern volatile int writeMe = 0;
    
    void float_to_int(float f)
    {    
      writeMe = static_cast<int>(f); 
    }
    
    int main()
    {
      readMe = 17;
      float_to_int(readMe);
    }
    

    Still, all optimizations inbetween the read and the write can be applied "with full force". The read and write to the global variable are often good "fenceposts" when inspecting the generated assembly.

    Without the extern the compiler may notice that a reference to the variable is never taken, and thus determine it can't be volatile. Technically, with Link Time Code Generation, it might not be enough, but I haven't found a compiler that agressive. (For a compiler that indeed removes the access, the reference would need to be passed to a function in a DLL loaded at runtime)

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