diff options
author | Christian Weisgerber <naddy@cvs.openbsd.org> | 2018-10-02 21:30:45 +0000 |
---|---|---|
committer | Christian Weisgerber <naddy@cvs.openbsd.org> | 2018-10-02 21:30:45 +0000 |
commit | 491aedcdbec0f0ca718296e56874a00b7f08bb40 (patch) | |
tree | c571df7eaaa6bcb1519b95a608dea192e638fdfe /sys/arch/i386/include | |
parent | e432202b1919bb33356ba6ef483f333607327326 (diff) |
Unify the MD byteswapping code as much as possible across architectures.
Use inline functions instead of GNU C statement expressions, and
make them available to userland. With clues from guenther@.
ok guenther@ kettenis@
Diffstat (limited to 'sys/arch/i386/include')
-rw-r--r-- | sys/arch/i386/include/endian.h | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/sys/arch/i386/include/endian.h b/sys/arch/i386/include/endian.h index 1f58500c862..b107da44df9 100644 --- a/sys/arch/i386/include/endian.h +++ b/sys/arch/i386/include/endian.h @@ -1,4 +1,4 @@ -/* $OpenBSD: endian.h,v 1.18 2014/07/12 16:25:08 guenther Exp $ */ +/* $OpenBSD: endian.h,v 1.19 2018/10/02 21:30:44 naddy Exp $ */ /*- * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. @@ -27,33 +27,34 @@ #ifndef _MACHINE_ENDIAN_H_ #define _MACHINE_ENDIAN_H_ -#ifdef __GNUC__ - -#define __swap32md(x) __statement({ \ - __uint32_t __swap32md_x = (x); \ - \ - __asm ("bswap %0" : "+r" (__swap32md_x)); \ - __swap32md_x; \ -}) - -#define __swap64md(x) __statement({ \ - __uint64_t __swap64md_x = (x); \ - \ - (__uint64_t)__swap32md(__swap64md_x >> 32) | \ - (__uint64_t)__swap32md(__swap64md_x & 0xffffffff) << 32; \ -}) -#define __swap16md(x) __statement({ \ - __uint16_t __swap16md_x = (x); \ - \ - __asm ("rorw $8, %w0" : "+r" (__swap16md_x)); \ - __swap16md_x; \ -}) +#ifndef __FROM_SYS__ENDIAN +#include <sys/_types.h> +#endif + +static __inline __uint16_t +__swap16md(__uint16_t _x) +{ + __asm ("rorw $8, %w0" : "+r" (_x)); + return (_x); +} + +static __inline __uint32_t +__swap32md(__uint32_t _x) +{ + __asm ("bswap %0" : "+r" (_x)); + return (_x); +} + +static __inline __uint64_t +__swap64md(__uint64_t _x) +{ + return ((__uint64_t)__swap32md(_x >> 32) | + (__uint64_t)__swap32md(_x & 0xffffffff) << 32); +} /* Tell sys/endian.h we have MD variants of the swap macros. */ #define __HAVE_MD_SWAP -#endif /* __GNUC__ */ - #define _BYTE_ORDER _LITTLE_ENDIAN #ifndef __FROM_SYS__ENDIAN |