Index: trunk/src/emx/include/386/endian.h
===================================================================
--- trunk/src/emx/include/386/endian.h	(revision 168)
+++ trunk/src/emx/include/386/endian.h	(revision 169)
@@ -32,9 +32,12 @@
  *
  *	from: @(#)endian.h	7.8 (Berkeley) 4/3/91
- *	$Id: endian.h,v 1.9 1994/02/12 07:23:49 cgd Exp $
+ * $FreeBSD: src/sys/i386/include/endian.h,v 1.35 2002/09/23 04:32:16 mike Exp $
+ *
+ * bird: this is a bit mixed up. with 1.18 too.
  */
 
 #ifndef _MACHINE_ENDIAN_H_
 #define _MACHINE_ENDIAN_H_
+
 
 /*
@@ -44,93 +47,130 @@
 #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
 
-unsigned long   htonl(unsigned long);
-unsigned short  htons(unsigned short);
-unsigned long   ntohl(unsigned long);
-unsigned short  ntohs(unsigned short);
+/*
+ * 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
 
-#ifndef __EMX__
+#include <sys/cdefs.h>
+
 
 #ifdef __GNUC__
 
-#if __GNUC__ >= 2
+#define __word_swap_int(x) \
+__extension__ ({ register __uint32_t __X = (x); \
+   __asm ("rorl $16, %0" : "+r" (__X)); \
+   __X; })
 
-#if defined(KERNEL) && ((defined(I486_CPU) || defined(I586_CPU)) && !defined(I386_CPU))
-#define __byte_swap_long(x) \
-({ register unsigned long __x = (x); \
-   asm ("bswap %1" \
-	: "=q" (__x) \
-	: "0" (__x)); \
-   __x; })
+#if defined(_KERNEL) && (defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)) && !defined(I386_CPU)
+
+#define __byte_swap_int(x) \
+__extension__ ({ register __uint32_t __X = (x); \
+   __asm ("bswap %0" : "+r" (__X)); \
+   __X; })
 #else
-#define __byte_swap_long(x) \
-({ register unsigned long __x = (x); \
-   asm ("xchgb %h1, %b1\n\trorl $16, %1\n\txchgb %h1, %b1" \
-	: "=q" (__x) \
-	: "0" (__x)); \
-   __x; })
-#endif	/* KERNEL && ... */
+
+#define __byte_swap_int(x) \
+__extension__ ({ register __uint32_t __X = (x); \
+   __asm ("xchgb %h0, %b0\n\trorl $16, %0\n\txchgb %h0, %b0" \
+       : "+q" (__X)); \
+   __X; })
+#endif
+
 #define __byte_swap_word(x) \
-({ register unsigned short __x = (x); \
-   asm ("xchgb %h1, %b1" \
-	: "=q" (__x) \
-	: "0" (__x)); \
-   __x; })
+__extension__ ({ register __uint16_t __X = (x); \
+   __asm ("xchgb %h0, %b0" : "+q" (__X)); \
+   __X; })
 
-#else	/* __GNUC__ >= 2 */
+static __inline __uint64_t
+__bswap64(__uint64_t _x)
+{
+	return ((_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(KERNEL) && ((defined(I486_CPU) || defined(I586_CPU)) && !defined(I386_CPU))
-#define __byte_swap_long(x) \
-({ register unsigned long __x = (x); \
-   asm ("bswap %1" \
-	: "=r" (__x) \
-	: "0" (__x)); \
-   __x; })
-#else
-#define __byte_swap_long(x) \
-({ register u_long __x = (x); \
-   asm ("rorw $8, %w1\n\trorl $16, %1\n\trorw $8, %w1" \
-	: "=r" (__x) \
-	: "0" (__x)); \
-   __x; })
-#endif	/* KERNEL && ... */
-#define __byte_swap_word(x) \
-({ register u_short __x = (x); \
-   asm ("rorw $8, %w1" \
-	: "=r" (__x) \
-	: "0" (__x)); \
-   __x; })
+static __inline __uint32_t
+__bswap32(__uint32_t _x)
+{
 
-#endif	/* __GNUC__ >= 2 */
+	return (__byte_swap_int(_x));
+}
 
-#define	ntohl(x)	__byte_swap_long(x)
-#define	ntohs(x)	__byte_swap_word(x)
-#define	htonl(x)	__byte_swap_long(x)
-#define	htons(x)	__byte_swap_word(x)
+static __inline __uint16_t
+__bswap16(__uint16_t _x)
+{
 
-#endif	/* __GNUC__ */
+	return (__byte_swap_word(_x));
+}
 
-#endif /* not __EMX__ */
+#define	__htonl(x)	__bswap32(x)
+#define	__htons(x)	__bswap16(x)
+#define	__ntohl(x)	__bswap32(x)
+#define	__ntohs(x)	__bswap16(x)
+
+#else /* !__GNUC__ */
+
+/*
+ * No optimizations are available for this compiler.  Fall back to
+ * non-optimized functions by defining the constant usually used to prevent
+ * redefinition.
+ */
+#define	_BYTEORDER_FUNC_DEFINED
+
+#endif /* __GNUC__ */
+
+
+/* For toolkit compatibility we must do this stuff here.
+ * Not sure how correct this is now. And mixing 4 versions
+ * of (Free)BSD doesn't help....
+ */
+#ifndef _POSIX_SOURCE
+
+#ifndef _KERNEL
+#include <sys/cdefs.h>
+#endif
+
+__BEGIN_DECLS
+unsigned long  _System htonl __P((unsigned long));
+unsigned short _System htons __P((unsigned short));
+unsigned long  _System ntohl __P((unsigned long));
+unsigned short _System ntohs __P((unsigned short));
+__END_DECLS
+
+#ifndef _BYTEORDER_FUNC_DEFINED
+#define _BYTEORDER_FUNC_DEFINED
+#define htonl(x)   __htonl(x)
+#define htons(x)   __htons(x)
+#define ntohl(x)   __ntohl(x)
+#define ntohs(x)   __ntohs(x)
+#endif
 
 /*
  * Macros for network/external number representation conversion.
  */
-#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)
+#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)))
 
-#endif /* _POSIX_SOURCE */
+#endif /*!_POSIX_SOURCE*/
+
 
 #endif /* _MACHINE_ENDIAN_H_ */
Index: trunk/src/emx/include/machine/endian.h
===================================================================
--- trunk/src/emx/include/machine/endian.h	(revision 168)
+++ trunk/src/emx/include/machine/endian.h	(revision 169)
@@ -1,3 +1,3 @@
-/* machine/endian.h (emx+gcc) */
+/* machine/endian.h (emx+gcc) (freebsd 5.0 inspired) */
 
 #ifndef _MACHINE_ENDIAN_H
@@ -6,3 +6,64 @@
 #include <386/endian.h>
 
+
+/** NOTE this is actually sys/endian.h! */
+
+#ifndef _UINT16_T_DECLARED
+typedef	__uint16_t	uint16_t;
+#define	_UINT16_T_DECLARED
+#endif
+
+#ifndef _UINT32_T_DECLARED
+typedef	__uint32_t	uint32_t;
+#define	_UINT32_T_DECLARED
+#endif
+
+#ifndef _UINT64_T_DECLARED
+typedef	__uint64_t	uint64_t;
+#define	_UINT64_T_DECLARED
+#endif
+
+/*
+ * General byte order swapping functions.
+ */
+#define	bswap16(x)	__bswap16(x)
+#define	bswap32(x)	__bswap32(x)
+#define	bswap64(x)	__bswap64(x)
+
+/*
+ * Host to big endian, host to little endian, big endian to host, and little
+ * endian to host byte order functions as detailed in byteorder(9).
+ */
+#if _BYTE_ORDER == _LITTLE_ENDIAN
+#define	htobe16(x)	bswap16((x))
+#define	htobe32(x)	bswap32((x))
+#define	htobe64(x)	bswap64((x))
+#define	htole16(x)	((uint16_t)(x))
+#define	htole32(x)	((uint32_t)(x))
+#define	htole64(x)	((uint64_t)(x))
+
+#define	be16toh(x)	bswap16((x))
+#define	be32toh(x)	bswap32((x))
+#define	be64toh(x)	bswap64((x))
+#define	le16toh(x)	((uint16_t)(x))
+#define	le32toh(x)	((uint32_t)(x))
+#define	le64toh(x)	((uint64_t)(x))
+#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
+#define	htobe16(x)	((uint16_t)(x))
+#define	htobe32(x)	((uint32_t)(x))
+#define	htobe64(x)	((uint64_t)(x))
+#define	htole16(x)	bswap16((x))
+#define	htole32(x)	bswap32((x))
+#define	htole64(x)	bswap64((x))
+
+#define	be16toh(x)	((uint16_t)(x))
+#define	be32toh(x)	((uint32_t)(x))
+#define	be64toh(x)	((uint64_t)(x))
+#define	le16toh(x)	bswap16((x))
+#define	le32toh(x)	bswap32((x))
+#define	le64toh(x)	bswap64((x))
+#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
+
 #endif /* not _MACHINE_ENDIAN_H */
+
+
