Force compiler to not optimize side-effect-less statements

前端 未结 11 1676
执念已碎
执念已碎 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条回答
  •  猫巷女王i
    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(f); // has no side-effects
    }
    

提交回复
热议问题