2

For GCC and Clang, I can easily do this:

    // absolute value
    inline constexpr int abs(const int number)
    { return __builtin_abs(number); }
    inline constexpr long abs(const long number)
    { return __builtin_labs(number); }
    inline constexpr long long abs(const long long number)
    { return __builtin_llabs(number); }
    inline constexpr double abs(const double& number)
    { return __builtin_fabs(number); }
    inline constexpr float abs(const float& number)
    { return __builtin_fabsf(number); }
    inline constexpr long double abs(const long double& number)
    { return __builtin_fabsl(number); }

Which works like a charm. I'd like to do a similar thing for pretty much every math function, and have my code work on MSVC as well. How can I do the equivalent of the above for MSVC?

EDIT: for clarity: the question is about the __builtin_* functions, nothing else. I tried

#pragma intrinsic(abs)

but this needs a declaration of the abs function, which I would like not to have in my global namespace.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 1
    There is a response to this that links to the available intrinsics in MSVC: http://stackoverflow.com/questions/355967/how-to-use-msvc-intrinsics-to-get-the-equivalent-of-this-gcc-code – Joe Jan 15 '12 at 17:17
  • 1
    Are you asking if `constexpr` is supported in Microsoft's compiler? The answer is no, not for Visual Studio 2010 or the next version, VS 11. – Cody Gray - on strike Jan 15 '12 at 17:17
  • 3
    IIRC you shouldn't need to even do this - I believe there's a compiler switch that determines whether the built-ins are used instead of library functions ? – Paul R Jan 15 '12 at 17:17

1 Answers1

1

Intrinsic functions are not portable, so you'll have to manually look up the corresponding builtin function and add it to the list, and use #ifdef to switch modes.

You don't have to have to have abs in the global namespace, by the way: include <cstdlib> instead of <stdlib.h> and you will get std::abs instead.

Compilers know what their own intrinsics are, MSVC uses the /Oi switch to enable them.

spraff
  • 32,570
  • 22
  • 121
  • 229