0

I am working on a piece of code that needs SIMD vectorization, and I need to make a set of macros to decide which vector types and intrinsics to use (i.e. MYLIB_HAS_SSE, MYLIB_HAS_AVX, etc.).

On GCC and CLang this is fairly straightforward, I just need to check for the corresponding pre-defined macros such as __SSE__ or __SSE4_1__. MSVC on the other hand does not define any such macros other than __AVX__ and __AVX2__ to my knowledge, and support for SSE and SSE2 must be checked via the _M_IX86_FP macro. _M_IX86_FP however, does not seem to give any information beyond SSE2, so if I want to check for SSE3 and beyond I am out of luck. As such, I need to "emulate" the missing macros in some other way.

Is it safe to assume that if MSVC has the __AVX__ macro defined (i.e. the target supports AVX), then the target CPU also supports all levels of SSE up to SSE4.2? Is there any other way to check for SSE support on x86 targets (other than runtime cpuid)?

Also if anyone knows, is there some convenient table of x86 architectures and their corresponding feature levels?

JustClaire
  • 451
  • 3
  • 11
  • Yes, `__AVX__` implies `__SSE_4_2__` and all earlier Intel extensions. A CPU supporting AVX 100% guarantees the VEX encoding of those instructions, which is what compilers will use get when compiling with `/arch:AVX`, e.g. `vpshufb xmm` instead of legacy-SSE `pshufb xmm`. I think it's safe for software to also assume that legacy-SSE encodings are available, too. I think this is a duplicate of [Do all CPUs which support AVX2 also support SSE4.2 and AVX?](https://stackoverflow.com/q/53443249) - let me know if your question has aspects that aren't covered by that. – Peter Cordes Jan 02 '23 at 09:28
  • @PeterCordes yes, that question does answer mine, thank you :) neither google nor stackoverflow itself has shown me that for some reason – JustClaire Jan 02 '23 at 09:31
  • e.g. https://www.felixcloutier.com/x86/pcmpgtq shows the VEX encoding of `vpcmpgtq xmm` requires AVX, and that's what MSVC will use for `_mm_cmpgt_epi64` in code compiled with `-arch:AVX`. – Peter Cordes Jan 02 '23 at 09:31
  • I found it with `site:stackoverflow.com does avx imply sse`, but google is probably biased towards showing me pages like that in my results. Also semi-related: [How do I enable SSE4.1 and SSE3 (but NOT AVX) in MSVC](https://stackoverflow.com/q/64053597) if you did want to use the legacy encodings to make binaries that can run on Skylake-pentium / celeron, and Silvermont-family before Alder Lake E-cores. – Peter Cordes Jan 02 '23 at 09:33

0 Answers0