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

后端 未结 8 616
伪装坚强ぢ
伪装坚强ぢ 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:09

    The library abs function operates on integers while you are obviously testing floats. This means that call to abs with float argument involves conversion from float to int (may be a no-op as you are using constant and compiler may do it at compile time), then INTEGER abs operation and conversion int->float. You templated function will involve operations on floats and this is probably making a difference.

    0 讨论(0)
  • 2021-02-07 08:12

    Your version of abs is inlined and can be computed once and the compiler can trivially know that the value returned isn't going to change, so it doesn't even need to call the function.

    You really need to look at the generated assembly code (set a breakpoint, and open the "large" debugger view, this disassembly will be on the bottom left if memory serves), and then you can see what's going on.

    You can find documentation on your processor online without too much trouble, it'll tell you what all of the instructions are so you can figure out what's happening. Alternatively, paste it here and we'll tell you. ;)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-07 08:20

    Since they are the implementation, they are free to make as many assumptions as they want. They know the format of the double and can play tricks with that instead.

    Likely (as in almost not even a question), your double is the binary64 format. This means the sign has it's own bit, and an absolute value is merely clearing that bit. For example, as a specialization, a compiler implementer may do the following:

    template <>
    double abs<double>(const double x)
    {
        // breaks strict aliasing, but compiler writer knows this behavior for the platform
        uint64_t i = reinterpret_cast<const std::uint64_t&>(x);
        i &= 0x7FFFFFFFFFFFFFFFULL; // clear sign bit
    
        return reinterpret_cast<const double&>(i);
    }
    

    This removes branching and may run faster.

    0 讨论(0)
  • 2021-02-07 08:23

    There are well-known tricks for computing the absolute value of a two's complement signed number. If the number is negative, flip all the bits and add 1, that is, xor with -1 and subtract -1. If it is positive, do nothing, that is, xor with 0 and subtract 0.

    int my_abs(int x)
    {
        int s = x >> 31;
        return (x ^ s) - s;
    }
    
    0 讨论(0)
  • 2021-02-07 08:30

    It probably just uses a bitmask to set the sign bit to 0.

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