src/cpuinfo/SDL_cpuinfo.c
changeset 11984 987c5dc71309
parent 11811 5d94cb6b24d3
child 11985 36aa0bf7312b
     1.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Fri May 18 13:09:30 2018 -0700
     1.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Mon May 21 11:34:57 2018 -0400
     1.3 @@ -38,6 +38,7 @@
     1.4  /* CPU feature detection for SDL */
     1.5  
     1.6  #include "SDL_cpuinfo.h"
     1.7 +#include "SDL_assert.h"
     1.8  
     1.9  #ifdef HAVE_SYSCONF
    1.10  #include <unistd.h>
    1.11 @@ -571,6 +572,7 @@
    1.12  }
    1.13  
    1.14  static Uint32 SDL_CPUFeatures = 0xFFFFFFFF;
    1.15 +static Uint32 SDL_SIMDAlignment = 0xFFFFFFFF;
    1.16  
    1.17  static Uint32
    1.18  SDL_GetCPUFeatures(void)
    1.19 @@ -578,41 +580,53 @@
    1.20      if (SDL_CPUFeatures == 0xFFFFFFFF) {
    1.21          CPU_calcCPUIDFeatures();
    1.22          SDL_CPUFeatures = 0;
    1.23 +        SDL_SIMDAlignment = 4;  /* a good safe base value */
    1.24          if (CPU_haveRDTSC()) {
    1.25              SDL_CPUFeatures |= CPU_HAS_RDTSC;
    1.26          }
    1.27          if (CPU_haveAltiVec()) {
    1.28              SDL_CPUFeatures |= CPU_HAS_ALTIVEC;
    1.29 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.30          }
    1.31          if (CPU_haveMMX()) {
    1.32              SDL_CPUFeatures |= CPU_HAS_MMX;
    1.33 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
    1.34          }
    1.35          if (CPU_have3DNow()) {
    1.36              SDL_CPUFeatures |= CPU_HAS_3DNOW;
    1.37 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 8);
    1.38          }
    1.39          if (CPU_haveSSE()) {
    1.40              SDL_CPUFeatures |= CPU_HAS_SSE;
    1.41 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.42          }
    1.43          if (CPU_haveSSE2()) {
    1.44              SDL_CPUFeatures |= CPU_HAS_SSE2;
    1.45 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.46          }
    1.47          if (CPU_haveSSE3()) {
    1.48              SDL_CPUFeatures |= CPU_HAS_SSE3;
    1.49 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.50          }
    1.51          if (CPU_haveSSE41()) {
    1.52              SDL_CPUFeatures |= CPU_HAS_SSE41;
    1.53 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.54          }
    1.55          if (CPU_haveSSE42()) {
    1.56              SDL_CPUFeatures |= CPU_HAS_SSE42;
    1.57 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.58          }
    1.59          if (CPU_haveAVX()) {
    1.60              SDL_CPUFeatures |= CPU_HAS_AVX;
    1.61 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
    1.62          }
    1.63          if (CPU_haveAVX2()) {
    1.64              SDL_CPUFeatures |= CPU_HAS_AVX2;
    1.65 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 32);
    1.66          }
    1.67          if (CPU_haveNEON()) {
    1.68              SDL_CPUFeatures |= CPU_HAS_NEON;
    1.69 +            SDL_SIMDAlignment = SDL_max(SDL_SIMDAlignment, 16);
    1.70          }
    1.71      }
    1.72      return SDL_CPUFeatures;
    1.73 @@ -745,6 +759,44 @@
    1.74  }
    1.75  
    1.76  
    1.77 +size_t
    1.78 +SDL_SIMDGetAlignment(void)
    1.79 +{
    1.80 +    if (SDL_SIMDAlignment == 0xFFFFFFFF) {
    1.81 +        SDL_GetCPUFeatures();  /* make sure this has been calculated */
    1.82 +    }
    1.83 +    SDL_assert(SDL_SIMDAlignment != 0);
    1.84 +    return SDL_SIMDAlignment;
    1.85 +}
    1.86 +
    1.87 +void *
    1.88 +SDL_SIMDAlloc(const size_t len)
    1.89 +{
    1.90 +    const size_t alignment = SDL_SIMDGetAlignment();
    1.91 +    const size_t padding = alignment - (len % alignment);
    1.92 +    const size_t padded = (padding != alignment) ? (len + padding) : len;
    1.93 +    Uint8 *retval = NULL;
    1.94 +    Uint8 *ptr = (Uint8 *) SDL_malloc(padded + alignment + sizeof (void *));
    1.95 +    if (ptr) {
    1.96 +        /* store the actual malloc pointer right before our aligned pointer. */
    1.97 +        retval = ptr + sizeof (void *);
    1.98 +        retval += alignment - (((size_t) retval) % alignment);
    1.99 +        *(((void **) retval) - 1) = ptr;
   1.100 +    }
   1.101 +    return retval;
   1.102 +}
   1.103 +
   1.104 +void
   1.105 +SDL_SIMDFree(void *ptr)
   1.106 +{
   1.107 +    if (ptr) {
   1.108 +        void **realptr = (void **) ptr;
   1.109 +        realptr--;
   1.110 +        SDL_free(*(((void **) ptr) - 1));
   1.111 +    }
   1.112 +}
   1.113 +
   1.114 +
   1.115  #ifdef TEST_MAIN
   1.116  
   1.117  #include <stdio.h>