Fixed bug 4557 - SDL_SIMDAlloc and *Free should be in the public interface
authorSam Lantinga
Sat, 08 Jun 2019 14:54:37 -0700
changeset 12794a2dc7ba484fd
parent 12793 26634e392a7f
child 12795 b1077303b6fe
Fixed bug 4557 - SDL_SIMDAlloc and *Free should be in the public interface

Martin Gerhardy

These functions are really useful and should get exposed imo.
include/SDL_cpuinfo.h
src/cpuinfo/SDL_cpuinfo.c
src/cpuinfo/SDL_simd.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/video/SDL_RLEaccel.c
src/video/SDL_surface.c
     1.1 --- a/include/SDL_cpuinfo.h	Sat Jun 08 14:40:27 2019 -0700
     1.2 +++ b/include/SDL_cpuinfo.h	Sat Jun 08 14:54:37 2019 -0700
     1.3 @@ -196,6 +196,69 @@
     1.4   */
     1.5  extern DECLSPEC int SDLCALL SDL_GetSystemRAM(void);
     1.6  
     1.7 +/**
     1.8 + * \brief Report the alignment this system needs for SIMD allocations.
     1.9 + *
    1.10 + * This will return the minimum number of bytes to which a pointer must be
    1.11 + *  aligned to be compatible with SIMD instructions on the current machine.
    1.12 + *  For example, if the machine supports SSE only, it will return 16, but if
    1.13 + *  it supports AVX-512F, it'll return 64 (etc). This only reports values for
    1.14 + *  instruction sets SDL knows about, so if your SDL build doesn't have
    1.15 + *  SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
    1.16 + *  not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
    1.17 + *  Plan accordingly.
    1.18 + */
    1.19 +extern DECLSPEC size_t SDLCALL SDL_SIMDGetAlignment(void);
    1.20 +
    1.21 +/**
    1.22 + * \brief Allocate memory in a SIMD-friendly way.
    1.23 + *
    1.24 + * This will allocate a block of memory that is suitable for use with SIMD
    1.25 + *  instructions. Specifically, it will be properly aligned and padded for
    1.26 + *  the system's supported vector instructions.
    1.27 + *
    1.28 + * The memory returned will be padded such that it is safe to read or write
    1.29 + *  an incomplete vector at the end of the memory block. This can be useful
    1.30 + *  so you don't have to drop back to a scalar fallback at the end of your
    1.31 + *  SIMD processing loop to deal with the final elements without overflowing
    1.32 + *  the allocated buffer.
    1.33 + *
    1.34 + * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free()
    1.35 + *  or delete[], etc.
    1.36 + *
    1.37 + * Note that SDL will only deal with SIMD instruction sets it is aware of;
    1.38 + *  for example, SDL 2.0.8 knows that SSE wants 16-byte vectors
    1.39 + *  (SDL_HasSSE()), and AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't
    1.40 + *  know that AVX-512 wants 64. To be clear: if you can't decide to use an
    1.41 + *  instruction set with an SDL_Has*() function, don't use that instruction
    1.42 + *  set with memory allocated through here.
    1.43 + *
    1.44 + * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
    1.45 + *  out of memory.
    1.46 + *
    1.47 + *  \param len The length, in bytes, of the block to allocated. The actual
    1.48 + *             allocated block might be larger due to padding, etc.
    1.49 + * \return Pointer to newly-allocated block, NULL if out of memory.
    1.50 + *
    1.51 + * \sa SDL_SIMDAlignment
    1.52 + * \sa SDL_SIMDFree
    1.53 + */
    1.54 +extern DECLSPEC void * SDLCALL SDL_SIMDAlloc(const size_t len);
    1.55 +
    1.56 +/**
    1.57 + * \brief Deallocate memory obtained from SDL_SIMDAlloc
    1.58 + *
    1.59 + * It is not valid to use this function on a pointer from anything but
    1.60 + *  SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
    1.61 + *  SDL_malloc, memalign, new[], etc.
    1.62 + *
    1.63 + * However, SDL_SIMDFree(NULL) is a legal no-op.
    1.64 + *
    1.65 + * \sa SDL_SIMDAlloc
    1.66 + */
    1.67 +extern DECLSPEC void SDLCALL SDL_SIMDFree(void *ptr);
    1.68 +
    1.69 +/* vi: set ts=4 sw=4 expandtab: */
    1.70  /* Ends C function definitions when using C++ */
    1.71  #ifdef __cplusplus
    1.72  }
     2.1 --- a/src/cpuinfo/SDL_cpuinfo.c	Sat Jun 08 14:40:27 2019 -0700
     2.2 +++ b/src/cpuinfo/SDL_cpuinfo.c	Sat Jun 08 14:54:37 2019 -0700
     2.3 @@ -22,7 +22,6 @@
     2.4  #include "SDL_config.h"
     2.5  #else
     2.6  #include "../SDL_internal.h"
     2.7 -#include "SDL_simd.h"
     2.8  #endif
     2.9  
    2.10  #if defined(__WIN32__) || defined(__WINRT__)
     3.1 --- a/src/cpuinfo/SDL_simd.h	Sat Jun 08 14:40:27 2019 -0700
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,88 +0,0 @@
     3.4 -/*
     3.5 -  Simple DirectMedia Layer
     3.6 -  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     3.7 -
     3.8 -  This software is provided 'as-is', without any express or implied
     3.9 -  warranty.  In no event will the authors be held liable for any damages
    3.10 -  arising from the use of this software.
    3.11 -
    3.12 -  Permission is granted to anyone to use this software for any purpose,
    3.13 -  including commercial applications, and to alter it and redistribute it
    3.14 -  freely, subject to the following restrictions:
    3.15 -
    3.16 -  1. The origin of this software must not be misrepresented; you must not
    3.17 -     claim that you wrote the original software. If you use this software
    3.18 -     in a product, an acknowledgment in the product documentation would be
    3.19 -     appreciated but is not required.
    3.20 -  2. Altered source versions must be plainly marked as such, and must not be
    3.21 -     misrepresented as being the original software.
    3.22 -  3. This notice may not be removed or altered from any source distribution.
    3.23 -*/
    3.24 -
    3.25 -#include "SDL.h"
    3.26 -#include "../SDL_internal.h"
    3.27 -
    3.28 -/**
    3.29 - * \brief Report the alignment this system needs for SIMD allocations.
    3.30 - *
    3.31 - * This will return the minimum number of bytes to which a pointer must be
    3.32 - *  aligned to be compatible with SIMD instructions on the current machine.
    3.33 - *  For example, if the machine supports SSE only, it will return 16, but if
    3.34 - *  it supports AVX-512F, it'll return 64 (etc). This only reports values for
    3.35 - *  instruction sets SDL knows about, so if your SDL build doesn't have
    3.36 - *  SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
    3.37 - *  not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
    3.38 - *  Plan accordingly.
    3.39 - */
    3.40 -extern size_t SDL_SIMDGetAlignment(void);
    3.41 -
    3.42 -/**
    3.43 - * \brief Allocate memory in a SIMD-friendly way.
    3.44 - *
    3.45 - * This will allocate a block of memory that is suitable for use with SIMD
    3.46 - *  instructions. Specifically, it will be properly aligned and padded for
    3.47 - *  the system's supported vector instructions.
    3.48 - *
    3.49 - * The memory returned will be padded such that it is safe to read or write
    3.50 - *  an incomplete vector at the end of the memory block. This can be useful
    3.51 - *  so you don't have to drop back to a scalar fallback at the end of your
    3.52 - *  SIMD processing loop to deal with the final elements without overflowing
    3.53 - *  the allocated buffer.
    3.54 - *
    3.55 - * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free()
    3.56 - *  or delete[], etc.
    3.57 - *
    3.58 - * Note that SDL will only deal with SIMD instruction sets it is aware of;
    3.59 - *  for example, SDL 2.0.8 knows that SSE wants 16-byte vectors
    3.60 - *  (SDL_HasSSE()), and AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't
    3.61 - *  know that AVX-512 wants 64. To be clear: if you can't decide to use an
    3.62 - *  instruction set with an SDL_Has*() function, don't use that instruction
    3.63 - *  set with memory allocated through here.
    3.64 - *
    3.65 - * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
    3.66 - *  out of memory.
    3.67 - *
    3.68 - *  \param len The length, in bytes, of the block to allocated. The actual
    3.69 - *             allocated block might be larger due to padding, etc.
    3.70 - * \return Pointer to newly-allocated block, NULL if out of memory.
    3.71 - *
    3.72 - * \sa SDL_SIMDAlignment
    3.73 - * \sa SDL_SIMDFree
    3.74 - */
    3.75 -extern void * SDL_SIMDAlloc(const size_t len);
    3.76 -
    3.77 -/**
    3.78 - * \brief Deallocate memory obtained from SDL_SIMDAlloc
    3.79 - *
    3.80 - * It is not valid to use this function on a pointer from anything but
    3.81 - *  SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
    3.82 - *  SDL_malloc, memalign, new[], etc.
    3.83 - *
    3.84 - * However, SDL_SIMDFree(NULL) is a legal no-op.
    3.85 - *
    3.86 - * \sa SDL_SIMDAlloc
    3.87 - */
    3.88 -extern void SDL_SIMDFree(void *ptr);
    3.89 -
    3.90 -/* vi: set ts=4 sw=4 expandtab: */
    3.91 -
     4.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Sat Jun 08 14:40:27 2019 -0700
     4.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Sat Jun 08 14:54:37 2019 -0700
     4.3 @@ -714,3 +714,6 @@
     4.4  #define SDL_RenderCopyExF SDL_RenderCopyExF_REAL
     4.5  #define SDL_GetTouchDeviceType SDL_GetTouchDeviceType_REAL
     4.6  #define SDL_UIKitRunApp SDL_UIKitRunApp_REAL
     4.7 +#define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_REAL
     4.8 +#define SDL_SIMDAlloc SDL_SIMDAlloc_REAL
     4.9 +#define SDL_SIMDFree SDL_SIMDFree_REAL
     5.1 --- a/src/dynapi/SDL_dynapi_procs.h	Sat Jun 08 14:40:27 2019 -0700
     5.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Sat Jun 08 14:54:37 2019 -0700
     5.3 @@ -770,3 +770,6 @@
     5.4  #ifdef __IPHONEOS__
     5.5  SDL_DYNAPI_PROC(int,SDL_UIKitRunApp,(int a, char *b, SDL_main_func c),(a,b,c),return)
     5.6  #endif
     5.7 +SDL_DYNAPI_PROC(size_t,SDL_SIMDGetAlignment,(void),(),return)
     5.8 +SDL_DYNAPI_PROC(void*,SDL_SIMDAlloc,(const size_t a),(a),return)
     5.9 +SDL_DYNAPI_PROC(void,SDL_SIMDFree,(void *a),(a),)
     6.1 --- a/src/video/SDL_RLEaccel.c	Sat Jun 08 14:40:27 2019 -0700
     6.2 +++ b/src/video/SDL_RLEaccel.c	Sat Jun 08 14:54:37 2019 -0700
     6.3 @@ -89,7 +89,6 @@
     6.4  #include "SDL_sysvideo.h"
     6.5  #include "SDL_blit.h"
     6.6  #include "SDL_RLEaccel_c.h"
     6.7 -#include "../cpuinfo/SDL_simd.h"
     6.8  
     6.9  #ifndef MIN
    6.10  #define MIN(a, b) ((a) < (b) ? (a) : (b))
     7.1 --- a/src/video/SDL_surface.c	Sat Jun 08 14:40:27 2019 -0700
     7.2 +++ b/src/video/SDL_surface.c	Sat Jun 08 14:54:37 2019 -0700
     7.3 @@ -26,7 +26,6 @@
     7.4  #include "SDL_RLEaccel_c.h"
     7.5  #include "SDL_pixels_c.h"
     7.6  #include "SDL_yuv_c.h"
     7.7 -#include "../cpuinfo/SDL_simd.h"
     7.8  
     7.9  
    7.10  /* Check to make sure we can safely check multiplication of surface w and pitch and it won't overflow size_t */