Windows: NEON detection and intrinsic includes on Visual Studio
authorSylvain Becker <sylvain.becker@gmail.com>
Tue, 04 Dec 2018 16:50:31 +0100
changeset 124501055156e99f3
parent 12449 17cb4ff70507
child 12451 572bb959fdc5
Windows: NEON detection and intrinsic includes on Visual Studio

Visual Studio doesn't define __ARM_ARCH nor _ARM_NEON, but _M_ARM and _M_ARM64,
so SDL_HasNEON() was bypassed.

PF_ARM_NEON_INSTRUCTIONS_AVAILABLE doesn't see to be defined (but still works
when defined as 19).
include/SDL_cpuinfo.h
src/cpuinfo/SDL_cpuinfo.c
     1.1 --- a/include/SDL_cpuinfo.h	Tue Dec 04 12:34:45 2018 +0100
     1.2 +++ b/include/SDL_cpuinfo.h	Tue Dec 04 16:50:31 2018 +0100
     1.3 @@ -54,8 +54,22 @@
     1.4  #if defined(HAVE_ALTIVEC_H) && defined(__ALTIVEC__) && !defined(__APPLE_ALTIVEC__) && defined(SDL_ENABLE_ALTIVEC_H)
     1.5  #include <altivec.h>
     1.6  #endif
     1.7 -#if defined(__ARM_NEON) && !defined(SDL_DISABLE_ARM_NEON_H)
     1.8 -#include <arm_neon.h>
     1.9 +#if !defined(SDL_DISABLE_ARM_NEON_H)
    1.10 +#  if defined(__ARM_NEON)
    1.11 +#    include <arm_neon.h>
    1.12 +#  elif defined(__WINDOWS__) || defined(__WINRT__)
    1.13 +/* Visual Studio doesn't define __ARM_ARCH, but _M_ARM (if set, always 7), and _M_ARM64 (if set, always 1).
    1.14 +#    if defined(_M_ARM)
    1.15 +#      include <armintr.h>
    1.16 +#      include <arm_neon.h>
    1.17 +#    endif
    1.18 +#    if defined (_M_ARM64)
    1.19 +#      include <armintr.h>
    1.20 +#      include <arm_neon.h>
    1.21 +#    endif
    1.22 +/* Set __ARM_NEON so that it can be used elsewhere, at compile time */
    1.23 +#    define __ARM_NEON 1
    1.24 +#  endif
    1.25  #endif
    1.26  #if defined(__3dNOW__) && !defined(SDL_DISABLE_MM3DNOW_H)
    1.27  #include <mm3dnow.h>
     2.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Tue Dec 04 12:34:45 2018 +0100
     2.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Tue Dec 04 16:50:31 2018 +0100
     2.3 @@ -352,8 +352,18 @@
     2.4  {
     2.5  /* The way you detect NEON is a privileged instruction on ARM, so you have
     2.6     query the OS kernel in a platform-specific way. :/ */
     2.7 -#if defined(SDL_CPUINFO_DISABLED) || !defined(__ARM_ARCH)
     2.8 -    return 0;  /* disabled or not an ARM CPU at all. */
     2.9 +#if defined(SDL_CPUINFO_DISABLED)
    2.10 +   return 0; /* disabled */
    2.11 +#elif (defined(__WINDOWS__) || defined(__WINRT__)) && (defined(_M_ARM) || defined(_M_ARM64))
    2.12 +/* Visual Studio, for ARM, doesn't define __ARM_ARCH. Handle this first. */
    2.13 +/* Seems to have been removed */
    2.14 +#  if !defined(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE)
    2.15 +#    define PF_ARM_NEON_INSTRUCTIONS_AVAILABLE 19
    2.16 +#  endif
    2.17 +/* All WinRT ARM devices are required to support NEON, but just in case. */
    2.18 +    return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
    2.19 +#elif !defined(__ARM_ARCH)
    2.20 +    return 0;  /* not an ARM CPU at all. */
    2.21  #elif __ARM_ARCH >= 8
    2.22      return 1;  /* ARMv8 always has non-optional NEON support. */
    2.23  #elif defined(__APPLE__) && (__ARM_ARCH >= 7)
    2.24 @@ -379,9 +389,6 @@
    2.25          }
    2.26          return 0;
    2.27      }
    2.28 -#elif (defined(__WINDOWS__) || defined(__WINRT__)) && defined(_M_ARM)
    2.29 -    /* All WinRT ARM devices are required to support NEON, but just in case. */
    2.30 -    return IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE) != 0;
    2.31  #else
    2.32  #warning SDL_HasNEON is not implemented for this ARM platform. Write me.
    2.33      return 0;