src/cpuinfo/SDL_simd.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Jan 2019 22:01:14 -0800
changeset 12503 806492103856
parent 11984 987c5dc71309
permissions -rw-r--r--
Updated copyright for 2019
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include "SDL.h"
    23 #include "../SDL_internal.h"
    24 
    25 /**
    26  * \brief Report the alignment this system needs for SIMD allocations.
    27  *
    28  * This will return the minimum number of bytes to which a pointer must be
    29  *  aligned to be compatible with SIMD instructions on the current machine.
    30  *  For example, if the machine supports SSE only, it will return 16, but if
    31  *  it supports AVX-512F, it'll return 64 (etc). This only reports values for
    32  *  instruction sets SDL knows about, so if your SDL build doesn't have
    33  *  SDL_HasAVX512F(), then it might return 16 for the SSE support it sees and
    34  *  not 64 for the AVX-512 instructions that exist but SDL doesn't know about.
    35  *  Plan accordingly.
    36  */
    37 extern size_t SDL_SIMDGetAlignment(void);
    38 
    39 /**
    40  * \brief Allocate memory in a SIMD-friendly way.
    41  *
    42  * This will allocate a block of memory that is suitable for use with SIMD
    43  *  instructions. Specifically, it will be properly aligned and padded for
    44  *  the system's supported vector instructions.
    45  *
    46  * The memory returned will be padded such that it is safe to read or write
    47  *  an incomplete vector at the end of the memory block. This can be useful
    48  *  so you don't have to drop back to a scalar fallback at the end of your
    49  *  SIMD processing loop to deal with the final elements without overflowing
    50  *  the allocated buffer.
    51  *
    52  * You must free this memory with SDL_FreeSIMD(), not free() or SDL_free()
    53  *  or delete[], etc.
    54  *
    55  * Note that SDL will only deal with SIMD instruction sets it is aware of;
    56  *  for example, SDL 2.0.8 knows that SSE wants 16-byte vectors
    57  *  (SDL_HasSSE()), and AVX2 wants 32 bytes (SDL_HasAVX2()), but doesn't
    58  *  know that AVX-512 wants 64. To be clear: if you can't decide to use an
    59  *  instruction set with an SDL_Has*() function, don't use that instruction
    60  *  set with memory allocated through here.
    61  *
    62  * SDL_AllocSIMD(0) will return a non-NULL pointer, assuming the system isn't
    63  *  out of memory.
    64  *
    65  *  \param len The length, in bytes, of the block to allocated. The actual
    66  *             allocated block might be larger due to padding, etc.
    67  * \return Pointer to newly-allocated block, NULL if out of memory.
    68  *
    69  * \sa SDL_SIMDAlignment
    70  * \sa SDL_SIMDFree
    71  */
    72 extern void * SDL_SIMDAlloc(const size_t len);
    73 
    74 /**
    75  * \brief Deallocate memory obtained from SDL_SIMDAlloc
    76  *
    77  * It is not valid to use this function on a pointer from anything but
    78  *  SDL_SIMDAlloc(). It can't be used on pointers from malloc, realloc,
    79  *  SDL_malloc, memalign, new[], etc.
    80  *
    81  * However, SDL_SIMDFree(NULL) is a legal no-op.
    82  *
    83  * \sa SDL_SIMDAlloc
    84  */
    85 extern void SDL_SIMDFree(void *ptr);
    86 
    87 /* vi: set ts=4 sw=4 expandtab: */
    88