What is different about C++ math.h abs() compared to my abs()

后端 未结 8 670
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 07:44

I am currently writing some glsl like vector math classes in C++, and I just implemented an abs() function like this:

template
static         


        
8条回答
  •  伪装坚强ぢ
    2021-02-07 08:18

    There can be several things:

    • are you sure the first call uses std::abs? It could just as well use the integer abs from C (either call std::abs explicitely, or have using std::abs;)

    • the compiler might have intrinsic implementation of some float functions (eg. compile them directly into FPU instructions)

    However, I'm surprised the compiler doesn't eliminate the loop altogether - since you don't do anything with any effect inside the loop, and at least in case of abs, the compiler should know there are no side-effects.

提交回复
热议问题