[PATCH xserver 1/3] Rewrite the byte swapping macros.

Keith Packard keithp at keithp.com
Fri Apr 21 20:21:06 UTC 2017


Eric Anholt <eric at anholt.net> writes:

>  #define swapll(x) do { \
> +		uint64_t temp; \
>  		if (sizeof(*(x)) != 8) \
>  			wrong_size(); \
> -                swap_uint64((uint64_t *)(x));   \
> +		memcpy(&temp, x, 8); \
> +		temp = bswap_64(temp); \
> +		memcpy(x, &temp, 8); \
>  	} while (0)

>  #define swapl(x) do { \
> +		uint32_t temp; \
>  		if (sizeof(*(x)) != 4) \
>  			wrong_size(); \
> -		if (__builtin_constant_p((uintptr_t)(x) & 3) && ((uintptr_t)(x) & 3) == 0) \
> -			*(x) = lswapl(*(x)); \
> -		else \
> -			swap_uint32((uint32_t *)(x)); \
> +		memcpy(&temp, x, 4); \
> +		temp = bswap_32(temp); \
> +		memcpy(x, &temp, 4); \
>  	} while (0)

>  #define swaps(x) do { \
> +		uint16_t temp; \
>  		if (sizeof(*(x)) != 2) \
>  			wrong_size(); \
> -		if (__builtin_constant_p((uintptr_t)(x) & 1) && ((uintptr_t)(x) & 1) == 0) \
> -			*(x) = lswaps(*(x)); \
> -		else \
> -			swap_uint16((uint16_t *)(x)); \
> +		memcpy(&temp, x, 2); \
> +		temp = bswap_16(temp); \
> +		memcpy(x, &temp, 2); \
>  	} while (0)

Any particular reason to think that x might be unaligned (and hence need
the memcpys? Yeah, I know they're probably 'free' when things are
aligned.

The rest of this is

Reviewed-by: Keith Packard <keithp at keithp.com>

-- 
-keith
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <https://lists.x.org/archives/xorg-devel/attachments/20170421/44519e50/attachment.sig>


More information about the xorg-devel mailing list