Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 27 Oct 2000 16:07:25 +0300
From:      Ruslan Ermilov <ru@FreeBSD.org>
To:        Bruce Evans <bde@FreeBSD.org>
Cc:        Konstantin Chuguev <Konstantin.Chuguev@dante.org.uk>, Brian Somers <brian@Awfulhak.org>, kargl@apl.washington.edu, freebsd-current@FreeBSD.org
Subject:   Re: platform byte order macros?
Message-ID:  <20001027160724.A29559@sunbay.com>
In-Reply-To: <Pine.BSF.4.21.0010272129330.47948-100000@besplex.bde.org>; from bde@zeta.org.au on Fri, Oct 27, 2000 at 09:49:57PM %2B1100
References:  <39F94740.66FCEB9E@dante.org.uk> <Pine.BSF.4.21.0010272129330.47948-100000@besplex.bde.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--opJtzjQTFsWo+cga
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Fri, Oct 27, 2000 at 09:49:57PM +1100, Bruce Evans wrote:
[...]
> 
> NetBSD supports the ntohl family on constants, but only on some arches
> (at least in last year's version).  It takes fancier macros to support
> constants.  This gives an excuse to change the inline functions back to
> macros :-).
> 
Cool!  My upcoming byte-swapping changes to IPv4 code would benefit from
having these macros.  Could you please review the attached patch (it was
obtained from NetBSD)?

<PS>
BTW, converting from macros to inline functions slightly broke the things.
It is currently impossible to simply include the <machine/endian.h>, since
it now depends on <sys/inttypes.h>.
</PS>

-- 
Ruslan Ermilov		Oracle Developer/DBA,
ru@sunbay.com		Sunbay Software AG,
ru@FreeBSD.org		FreeBSD committer,
+380.652.512.251	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age

--opJtzjQTFsWo+cga
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=p

Index: endian.h
===================================================================
RCS file: /home/ncvs/src/sys/i386/include/endian.h,v
retrieving revision 1.21
diff -u -p -r1.21 endian.h
--- endian.h	2000/10/16 17:06:48	1.21
+++ endian.h	2000/10/27 13:03:55
@@ -69,7 +69,7 @@ __END_DECLS
 #ifdef __GNUC__
 
 static __inline uint32_t
-__uint16_swap_uint32(uint32_t __x)
+__uint16_swap_uint32_variable(uint32_t __x)
 {
 	__asm ("rorl $16, %1" : "=r" (__x) : "0" (__x));
 
@@ -77,7 +77,7 @@ __uint16_swap_uint32(uint32_t __x)
 }
 
 static __inline uint32_t
-__uint8_swap_uint32(uint32_t __x)
+__uint8_swap_uint32_variable(uint32_t __x)
 {
 #if defined(_KERNEL) && (defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)) && !defined(I386_CPU)
 	__asm ("bswap %0" : "=r" (__x) : "0" (__x));
@@ -89,12 +89,36 @@ __uint8_swap_uint32(uint32_t __x)
 }
 
 static __inline uint16_t
-__uint8_swap_uint16(uint16_t __x)
+__uint8_swap_uint16_variable(uint16_t __x)
 {
 	__asm ("xchgb %h1, %b1" : "=q" (__x) : "0" (__x));
 
 	return __x;
 }
+
+#ifdef __OPTIMIZE__
+
+#define	__uint8_swap_uint32_constant(x) \
+	((((x) & 0xff000000) >> 24) | \
+	 (((x) & 0x00ff0000) >>  8) | \
+	 (((x) & 0x0000ff00) <<  8) | \
+	 (((x) & 0x000000ff) << 24))
+#define	__uint8_swap_uint16_constant(x) \
+	((((x) & 0xff00) >> 8) | \
+	 (((x) & 0x00ff) << 8))
+#define	__uint8_swap_uint32(x) \
+	(__builtin_constant_p((x)) ? \
+	 __uint8_swap_uint32_constant(x) : __uint8_swap_uint32_variable(x))
+#define	__uint8_swap_uint16(x) \
+	(__builtin_constant_p((x)) ? \
+	 __uint8_swap_uint16_constant(x) : __uint8_swap_uint16_variable(x))
+
+#else /* __OPTIMIZE__ */
+
+#define	__uint8_swap_uint32(x)	__uint8_swap_uint32_variable(x)
+#define	__uint8_swap_uint16(x)	__uint8_swap_uint16_variable(x)
+
+#endif /* __OPTIMIZE__ */
 
 /*
  * Macros for network/external number representation conversion.

--opJtzjQTFsWo+cga--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20001027160724.A29559>