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,
[EMAIL PROTECTED]           Sunbay Software AG,
[EMAIL PROTECTED]          FreeBSD committer,
+380.652.512.251        Simferopol, Ukraine

http://www.FreeBSD.org  The Power To Serve
http://www.oracle.com   Enabling The Information Age
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.

Reply via email to