SDL_ALIGNED(3)

πŸ“¦
version 3.4.18 Β· 5 versions
, in fedora 43, fedora 44

✨ SDL_ALIGNED ✨

okay bestie, this is a page from the SDL3 FUNCTIONS collection, and it's literally called SDL_ALIGNED(3). it's a tiny macro with big main character energy. let's get into it πŸ’…


πŸŽ€ NAME πŸŽ€

SDL_ALIGNED β€” a macro that lets you specify data alignment. that's it, that's the whole job, and it does it flawlessly. periodt.


🌸 SYNOPSIS 🌸

#include <SDL3/SDL_begin_code.h>

#define SDL_ALIGNED(x) __attribute__((aligned(x)))

like. look at her. one line of preprocessor and she's changing your memory layout. iconic.


πŸ’– DESCRIPTION πŸ’–

so basically, this macro tells the compiler "hey. this datatype or this variable? it must be aligned to a specific byte count." no negotiating, no vibes-based alignment, we're being specific here.

for example:

// make sure this is struct is aligned to 16 bytes for SIMD access.
typedef struct {
   float x, y, z, w;
} SDL_ALIGNED(16) MySIMDAlignedData;

// make sure this one field in a struct is aligned to 16 bytes for SIMD access.
typedef struct {
   SomeStuff stuff;
   float SDL_ALIGNED(16) position[4];
   SomeOtherStuff other_stuff;
} MyStruct;

// make sure this variable is aligned to 32 bytes.
int SDL_ALIGNED(32) myval = 0;

see how it can go on the whole typedef, or just on one single field inside a struct, or on a plain old variable? versatile queen. πŸ‘‘

🫢 real talk though: alignment is only guaranteed for things the compiler places β€” local variables on the stack, and global/static variables. that's the deal.

so if you wanna dynamically allocate something that respects this alignment, you gotta use SDL_aligned_alloc() or some other mechanism. don't just assume it'll work out, bestie, the compiler isn't gonna read your mind 😭

and here's the lowkey shady part: on compilers that don't support alignment, this macro is defined to an invalid symbol on purpose. not to be mean β€” it's so it becomes super obvious that your current compiler is likely to generate incorrect code when it sees this macro. it's giving "I'm telling you before you find out the hard way." πŸ’€


🌈 MACRO PARAMETERS 🌈

  • x β€” the byte count to align to, so the data's address will be a multiple of this value. that's the one and only thing you hand her. ✨

⭐ AVAILABILITY ⭐

this macro is available since SDL 3.4.0. so if you're on something older, sorry babe, no alignment for you πŸ™ˆ


and that's the whole page!! simple, clean, slays. this was brought to you by Simple Directmedia Layer, version SDL 3.4.18, dated in the SDL_ALIGNED(3) era β€” catch you in the next manpage, mwah πŸ’œπŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from