Skip to content

Commit 69f95df

Browse files
committed
begin_code: Added SDL_ALIGNED macro.
1 parent b4f7948 commit 69f95df

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

include/SDL3/SDL_begin_code.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,48 @@
281281
*/
282282
#define SDL_HAS_BUILTIN(x) __has_builtin(x)
283283

284+
/**
285+
* A macro to specify data alignment.
286+
*
287+
* This informs the compiler that a given datatype or variable must be aligned
288+
* to a specific byte count.
289+
*
290+
* For example:
291+
*
292+
* ```c
293+
* // make sure this is struct is aligned to 16 bytes for SIMD access.
294+
* typedef struct {
295+
* float x, y, z, w;
296+
* } SDL_ALIGNED(16) MySIMDAlignedData;
297+
*
298+
299+
* // make sure this one field in a struct is aligned to 16 bytes for SIMD access.
300+
* typedef struct {
301+
* SomeStuff stuff;
302+
* float position[4] SDL_ALIGNED(16);
303+
* SomeOtherStuff other_stuff;
304+
* } MyStruct;
305+
*
306+
* // make sure this variable is aligned to 32 bytes.
307+
* int SDL_ALIGNED(32) myval = 0;
308+
* ```
309+
*
310+
* Alignment is only guaranteed for things the compiler places: local
311+
* variables on the stack and global/static variables. To dynamically allocate
312+
* something that respects this alignment, use SDL_aligned_alloc() or some
313+
* other mechanism.
314+
*
315+
* On compilers without alignment support, this macro is defined to an
316+
* invalid symbol, to make it clear that the current compiler is likely to
317+
* generate incorrect code when it sees this macro.
318+
*
319+
* \param x the byte count to align to, so the data's address will be a
320+
* multiple of this value.
321+
*
322+
* \since This macro is available since SDL 3.4.0.
323+
*/
324+
#define SDL_ALIGNED(x) __attribute__((aligned(x)))
325+
284326
/* end of wiki documentation section. */
285327
#endif
286328

@@ -484,3 +526,18 @@
484526
#define SDL_ALLOC_SIZE2(p1, p2)
485527
#endif
486528
#endif /* SDL_ALLOC_SIZE2 not defined */
529+
530+
#ifndef SDL_ALIGNED
531+
#if defined(__clang__) || defined(__GNUC__)
532+
#define SDL_ALIGNED(x) __attribute__((aligned(x)))
533+
#elif defined(_MSC_VER)
534+
#define SDL_ALIGNED(x) __declspec(align(x))
535+
#elif defined(__cplusplus) && (__cplusplus >= 201103L)
536+
#define SDL_ALIGNED(x) alignas(x)
537+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
538+
#define SDL_ALIGNED(x) _Alignas(x)
539+
#else
540+
#define SDL_ALIGNED(x) PLEASE_DEFINE_SDL_ALIGNED
541+
#endif
542+
#endif /* SDL_ALIGNED not defined */
543+

0 commit comments

Comments
 (0)