--- src/sys/i386/include/Attic/endian.h 2003/08/26 21:42:18 1.3 +++ src/sys/i386/include/Attic/endian.h 2004/08/23 16:03:44 1.4 @@ -1,4 +1,6 @@ /* + * Copyright (c) 2004 The DragonFly Project. All rights reserved. + * * Copyright (c) 1987, 1991 Regents of the University of California. * All rights reserved. * @@ -38,82 +40,141 @@ #ifndef _MACHINE_ENDIAN_H_ #define _MACHINE_ENDIAN_H_ +#include +#include + /* * Define the order of 32-bit words in 64-bit words. */ #define _QUAD_HIGHWORD 1 #define _QUAD_LOWWORD 0 -#ifndef _POSIX_SOURCE - /* * Definitions for byte order, according to byte significance from low * address to high. */ -#define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ -#define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ -#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ +#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */ +#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */ +#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */ -#define BYTE_ORDER LITTLE_ENDIAN +#define _BYTE_ORDER _LITTLE_ENDIAN -#ifndef _KERNEL -#include +/* + * Deprecated variants that don't have enough underscores to be useful in more + * strict namespaces. + */ +#if __BSD_VISIBLE +#define LITTLE_ENDIAN _LITTLE_ENDIAN +#define BIG_ENDIAN _BIG_ENDIAN +#define PDP_ENDIAN _PDP_ENDIAN +#define BYTE_ORDER _BYTE_ORDER #endif -__BEGIN_DECLS -unsigned long htonl (unsigned long); -unsigned short htons (unsigned short); -unsigned long ntohl (unsigned long); -unsigned short ntohs (unsigned short); -__END_DECLS - -#define __word_swap_long(x) \ -__extension__ ({ register u_long __X = (x); \ - __asm ("rorl $16, %1" \ - : "=r" (__X) \ - : "0" (__X)); \ - __X; }) - -#if defined(_KERNEL) && (defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)) && !defined(I386_CPU) - -#define __byte_swap_long(x) \ -__extension__ ({ register u_long __X = (x); \ - __asm ("bswap %0" \ - : "=r" (__X) \ - : "0" (__X)); \ - __X; }) -#else - -#define __byte_swap_long(x) \ -__extension__ ({ register u_long __X = (x); \ - __asm ("xchgb %h1, %b1\n\trorl $16, %1\n\txchgb %h1, %b1" \ - : "=q" (__X) \ - : "0" (__X)); \ - __X; }) +#define __htonl(x) __bswap32(x) +#define __htons(x) __bswap16(x) +#define __ntohl(x) __bswap32(x) +#define __ntohs(x) __bswap16(x) + +#define __byte_swap16_const(x) \ + ((((x) & 0xff00) >> 8) | \ + (((x) & 0x00ff) << 8)) + +#define __byte_swap32_const(x) \ + ((((x) & 0xff000000) >> 24) | \ + (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | \ + (((x) & 0x000000ff) << 24)) + +#define __byte_swap64_const(x) \ + (((x) >> 56) | (((x) >> 40) & 0xff00) | (((x) >> 24) & 0xff0000) | \ + (((x) >> 8) & 0xff000000) | (((x) << 8) & ((__uint64_t)0xff << 32)) | \ + (((x) << 24) & ((__uint64_t)0xff << 40)) | \ + (((x) << 40) & ((__uint64_t)0xff << 48)) | (((x) << 56))) + +#if defined(__INTEL_COMPILER) +# if !defined(__cplusplus) || (defined(__cplusplus) && __INTEL_COMPILER >= 800) +# define __INTEL_COMPILER_with_DragonFly_endian 1 +# endif #endif -#define __byte_swap_word(x) \ -__extension__ ({ register u_short __X = (x); \ - __asm ("xchgb %h1, %b1" \ - : "=q" (__X) \ - : "0" (__X)); \ - __X; }) +#if defined(__GNUC__) || defined(__INTEL_COMPILER_with_DragonFly_endian) + +#if (defined(_KERNEL) && !defined(I386_CPU) && \ + (defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU))) || \ + defined(__i486__) || defined(__i586__) || defined(__i686__) + +#define __byte_swap32_var(x) \ + __extension__ ({ register __uint32_t __X = (x); \ + __asm ("bswap %0" : "+r" (__X)); \ + +#else /* !I386_CPU */ + +#define __byte_swap32_var(x) \ + __extension__ ({ register __uint32_t __X = (x); \ + __asm ("xchgb %h0, %b0\n\trorl $16, %0\n\txchgb %h0, %b0" \ + : "+q" (__X)); \ + __X; }) +#endif /* !I386_CPU */ + +#define __byte_swap16_var(x) \ + __extension__ ({ register __uint16_t __X = (x); \ + __asm ("xchgb %h0, %b0" : "+q" (__X)); \ + __X; }) + +#ifdef __OPTIMIZE__ + +#define __byte_swap16(x) (__builtin_constant_p(x) ? \ + __byte_swap16_const(x) : __byte_swap16_var(x)) + +#define __byte_swap32(x) (__builtin_constant_p(x) ? \ + __byte_swap32_const(x) : __byte_swap32_var(x)) + +#else /* __OPTIMIZE__ */ + +#define __byte_swap16(x) __byte_swap16_var(x) +#define __byte_swap32(x) __byte_swap32_var(x) + +#endif /* __OPTIMIZE__ */ + +#endif /* __GNUC__ || __INTEL_COMPILER_with_DragonFly_endian */ /* - * Macros for network/external number representation conversion. + * If the compiler-specific part didn't provide this, fallback + * to the generic versions. */ -#ifdef __GNUC__ -#define ntohl __byte_swap_long -#define ntohs __byte_swap_word -#define htonl __byte_swap_long -#define htons __byte_swap_word + +#ifndef __byte_swap16 +#define __byte_swap16(x) __byte_swap16_const(x) #endif -#define NTOHL(x) ((x) = ntohl((u_long)(x))) -#define NTOHS(x) ((x) = ntohs((u_short)(x))) -#define HTONL(x) ((x) = htonl((u_long)(x))) -#define HTONS(x) ((x) = htons((u_short)(x))) +#ifndef __byte_swap32 +#define __byte_swap32(x) __byte_swap32_const(x) +#endif + +#ifndef __byte_swap64 +#define __byte_swap64(x) __byte_swap64_const(x) +#endif -#endif /* ! _POSIX_SOURCE */ +__BEGIN_DECLS + +static __inline __uint16_t +__bswap16(__uint16_t _x) +{ + return (__byte_swap16(_x)); +} + +static __inline __uint32_t +__bswap32(__uint32_t _x) +{ + return (__byte_swap32(_x)); +} + +static __inline __uint64_t +__bswap64(__uint64_t _x) +{ + return (__byte_swap64(_x)); +} + +__END_DECLS #endif /* !_MACHINE_ENDIAN_H_ */