diff options
author | H. Peter Anvin <hpa@zytor.com> | 2014-02-12 09:56:15 -0800 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2014-02-12 09:56:15 -0800 |
commit | 83730db928b303bc3e9f7bd35c2e64cd85ab0267 (patch) | |
tree | b383a3263acc13360ab4e6f9c3ac5d5f85888d2b /com32/include | |
parent | 9376eaf8582bcec4aebd697d33836f0d180aee13 (diff) | |
parent | 5de463f724da515fd6c5ea49ded6dde178362181 (diff) | |
download | syslinux-83730db928b303bc3e9f7bd35c2e64cd85ab0267.tar.gz syslinux-83730db928b303bc3e9f7bd35c2e64cd85ab0267.tar.xz syslinux-83730db928b303bc3e9f7bd35c2e64cd85ab0267.zip |
Merge remote-tracking branch 'origin/rockridge'
Resolved Conflicts:
com32/include/byteswap.h
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'com32/include')
-rw-r--r-- | com32/include/byteswap.h | 142 | ||||
-rw-r--r-- | com32/include/netinet/in.h | 12 |
2 files changed, 138 insertions, 16 deletions
diff --git a/com32/include/byteswap.h b/com32/include/byteswap.h index b7eeeb40..5ff2cd69 100644 --- a/com32/include/byteswap.h +++ b/com32/include/byteswap.h @@ -1,11 +1,12 @@ #ifndef _BYTESWAP_H #define _BYTESWAP_H -/* COM32 will be running on an i386 platform */ - #include <stdint.h> +#include <klibc/endian.h> #include <klibc/compiler.h> +/* This assumes an i386 platform */ + #define __bswap_16_macro(v) ((uint16_t) \ (((uint16_t)(v) << 8) | \ ((uint16_t)(v) >> 8))) @@ -26,15 +27,13 @@ static inline __constfunc uint16_t __bswap_16(uint16_t v) static inline __constfunc uint32_t __bswap_32(uint32_t v) { -#if __SIZEOF_POINTER__ == 4 +#if defined(__x86_64__) + asm("bswap %0" : "+r" (v)); +#elif defined(__i386__) asm("xchgb %h0,%b0 ; roll $16,%0 ; xchgb %h0,%b0" : "+q" (v)); -#elif __SIZEOF_POINTER__ == 8 - asm("bswap %0" - : "=r" (v) - : "0" (v)); #else -#error "unable to build for architecture" + v = __bswap_32_macro(v); #endif return v; } @@ -49,10 +48,133 @@ static inline __constfunc uint32_t __bswap_32(uint32_t v) static inline __constfunc uint64_t __bswap_64(uint64_t v) { - return ((uint64_t)__bswap_32(v) << 32) | __bswap_32(v >> 32); +#if defined(__x86_64__) + asm("bswap %0" : "+r" (v)); +#else + v = ((uint64_t)__bswap_32(v) << 32) | __bswap_32(v >> 32); +#endif + return v; } #define bswap_64(x) (__builtin_constant_p(x) ? \ __bswap_64_macro(x) : __bswap_64(x)) -#endif /* byteswap.h */ +/* This is generic */ + +#if __BYTE_ORDER == __LITTLE_ENDIAN + +#define be16_to_cpu(x) bswap_16(x) +#define cpu_to_be16(x) bswap_16(x) +#define be32_to_cpu(x) bswap_32(x) +#define cpu_to_be32(x) bswap_32(x) +#define be64_to_cpu(x) bswap_64(x) +#define cpu_to_be64(x) bswap_64(x) + +#define le16_to_cpu(x) (x) +#define cpu_to_le16(x) (x) +#define le32_to_cpu(x) (x) +#define cpu_to_le32(x) (x) +#define le64_to_cpu(x) (x) +#define cpu_to_le64(x) (x) + +#elif __BYTE_ORDER == __BIG_ENDIAN + +#define le16_to_cpu(x) bswap_16(x) +#define cpu_to_le16(x) bswap_16(x) +#define le32_to_cpu(x) bswap_32(x) +#define cpu_to_le32(x) bswap_32(x) +#define le64_to_cpu(x) bswap_64(x) +#define cpu_to_le64(x) bswap_64(x) + +#define be16_to_cpu(x) (x) +#define cpu_to_be16(x) (x) +#define be32_to_cpu(x) (x) +#define cpu_to_be32(x) (x) +#define be64_to_cpu(x) (x) +#define cpu_to_be64(x) (x) + +#else + +#error "Unknown byte order!" + +#endif + +typedef struct { uint16_t x; } __attribute__((packed)) __ua_uint16_t; +typedef struct { uint32_t x; } __attribute__((packed)) __ua_uint32_t; +typedef struct { uint64_t x; } __attribute__((packed)) __ua_uint64_t; + +/* These are guaranteed to support unaligned accesses */ +static inline uint16_t get_le16(const uint16_t *p) +{ + const __ua_uint16_t *up = (const __ua_uint16_t *)p; + return le16_to_cpu(up->x); +} + +static inline uint32_t get_le32(const uint32_t *p) +{ + const __ua_uint32_t *up = (const __ua_uint32_t *)p; + return le32_to_cpu(up->x); +} + +static inline uint64_t get_le64(const uint64_t *p) +{ + const __ua_uint64_t *up = (const __ua_uint64_t *)p; + return le64_to_cpu(up->x); +} + +static inline uint16_t get_be16(const uint16_t *p) +{ + const __ua_uint16_t *up = (const __ua_uint16_t *)p; + return be16_to_cpu(up->x); +} + +static inline uint32_t get_be32(const uint32_t *p) +{ + const __ua_uint32_t *up = (const __ua_uint32_t *)p; + return be32_to_cpu(up->x); +} + +static inline uint64_t get_be64(const uint64_t *p) +{ + const __ua_uint64_t *up = (const __ua_uint64_t *)p; + return be64_to_cpu(up->x); +} + +static inline void put_le16(uint16_t v, uint16_t *p) +{ + __ua_uint16_t *up = (__ua_uint16_t *)p; + up->x = cpu_to_le16(v); +} + +static inline void put_le32(uint32_t v, uint32_t *p) +{ + __ua_uint32_t *up = (__ua_uint32_t *)p; + up->x = cpu_to_le32(v); +} + +static inline void put_le64(uint64_t v, uint64_t *p) +{ + __ua_uint64_t *up = (__ua_uint64_t *)p; + up->x = cpu_to_le64(v); +} + +static inline void put_be16(uint16_t v, uint16_t *p) +{ + __ua_uint16_t *up = (__ua_uint16_t *)p; + up->x = cpu_to_be16(v); +} + +static inline void put_be32(uint32_t v, uint32_t *p) +{ + __ua_uint32_t *up = (__ua_uint32_t *)p; + up->x = cpu_to_be32(v); +} + +static inline void put_be64(uint64_t v, uint64_t *p) +{ + __ua_uint64_t *up = (__ua_uint64_t *)p; + up->x = cpu_to_be64(v); +} + +#endif /* _BYTESWAP_H */ + diff --git a/com32/include/netinet/in.h b/com32/include/netinet/in.h index b24f8046..0a0049fd 100644 --- a/com32/include/netinet/in.h +++ b/com32/include/netinet/in.h @@ -8,12 +8,12 @@ #include <stdint.h> #include <byteswap.h> -#define htons(x) bswap_16(x) -#define ntohs(x) bswap_16(x) -#define htonl(x) bswap_32(x) -#define ntohl(x) bswap_32(x) -#define htonq(x) bswap_64(x) -#define ntohq(x) bswap_64(x) +#define htons(x) cpu_to_be16(x) +#define ntohs(x) be16_to_cpu(x) +#define htonl(x) cpu_to_be32(x) +#define ntohl(x) be32_to_cpu(x) +#define htonq(x) cpu_to_be64(x) +#define ntohq(x) be64_to_cpu(x) typedef uint32_t in_addr_t; typedef uint16_t in_port_t; |