Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect MSVC using preprocessor without detecting clang-cl, icl,

MSVC 2019 has added a new _udiv128() intrinsic (documentation) that I would like to use in my C++ code. However that intrinsic is currently not available in both clang-cl 9.0.0 and the Intel C++ compiler 2019 even though these compilers set _MSC_VER=1920 just like MSVC 2019.

Because of this issue the code below does not compile using both clang-cl and icl:

#include <immintrin.h>

#if _MSC_VER >= 1920
    uint64_t res = _udiv128(a, b, c, &d);
#endif

Is there a way to detect MSVC using the preprocessor without detecting clang-cl, icl, ... I would like to avoid checking for _MSC_VER and then excluding all other C/C++ Windows compilers.

Ideally I would like to detect MSVC using a Microsoft specific macro that is only defined for MSVC but not for clang-cl, icl...

like image 656
Linoliumz Avatar asked Oct 16 '22 14:10

Linoliumz


1 Answers

I can't speak for the Intel compiler.

Clang-cl defines _MSC_VER because it's trying to be a drop-in replacement for the MSVC compiler and to use the MS libraries that come with the compiler (e.g., the C RTL and the C++ Standard Library). Those library implementations depend on _MSC_VER, so clang-cl doesn't really have an option.

Options:

  1. Seva's proposal from the comments will work for you. I understand is not your preferred solution, but it is an option. You could test it in one place so that you only have to update one place when the situation improves.

  2. Try using the -fms-compatibility-version clang-cl flag to choose an older version. (If you don't provide this flag, clang-cl tries to discover you MSVC installation and then matches the compatibility flag to that installations libraries, which is probably why you're seeing exactly 1920.) This will prevent your #if _MSC_VER >= 1920 from triggering, but it could have side-effects if there are parts of the system libraries that require 1920-or-better.

  3. Wait for clang-cl to implement _udiv128(). Better yet, propose a patch to get it done sooner.

like image 150
Adrian McCarthy Avatar answered Oct 18 '22 23:10

Adrian McCarthy