2026-06-26 21:10:10 -07:00
|
|
|
// SPDX-FileCopyrightText: 2026 Erin Catto
|
2026-05-10 13:36:52 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2026-06-26 21:10:10 -07:00
|
|
|
#include <string.h>
|
2026-05-10 13:36:52 -07:00
|
|
|
|
2026-06-26 21:10:10 -07:00
|
|
|
// Swap two same-size lvalues through a byte buffer. Avoids __typeof__ so it builds on any C compiler.
|
2026-05-10 13:36:52 -07:00
|
|
|
#define B3_SWAP( x, y ) \
|
|
|
|
|
do \
|
|
|
|
|
{ \
|
2026-06-26 21:10:10 -07:00
|
|
|
_Static_assert( sizeof( x ) == sizeof( y ), "size mismatch" ); \
|
|
|
|
|
char B3_SWAP_TEMP[sizeof( x )]; \
|
|
|
|
|
memcpy( B3_SWAP_TEMP, &( x ), sizeof( x ) ); \
|
|
|
|
|
memcpy( &( x ), &( y ), sizeof( x ) ); \
|
|
|
|
|
memcpy( &( y ), B3_SWAP_TEMP, sizeof( x ) ); \
|
2026-05-10 13:36:52 -07:00
|
|
|
} \
|
|
|
|
|
while ( 0 )
|