summaryrefslogtreecommitdiffstats
path: root/sys/i386/include/endian.h
diff options
context:
space:
mode:
authorpeter <peter@FreeBSD.org>2003-09-22 21:46:47 +0000
committerpeter <peter@FreeBSD.org>2003-09-22 21:46:47 +0000
commit9cd1883cc6b92fce290a70a6ce3787cac3ef1286 (patch)
tree8a30e71a727161af57a328e08b3a0284f533986b /sys/i386/include/endian.h
parentb4cfe6d4c8e939571c285264ed4d1caa4fef0f1b (diff)
downloadFreeBSD-src-9cd1883cc6b92fce290a70a6ce3787cac3ef1286.zip
FreeBSD-src-9cd1883cc6b92fce290a70a6ce3787cac3ef1286.tar.gz
Microoptimization to allow the compiler to evaluate ntohl() etc on
known constants at compile time rather than at run time. We have a number of nasty hacks around the place to cache ntohl() of constants (eg: nfs). This change allows the compiler to compile-time evaluate ntohl(1) as 0x01000000 rather than having to emit assembler code to do it. This has other smaller flow-on effects because the compiler can see that ntohl(constant) itself has a constant value now and can propagate the compile time evaluation. Obtained from: Ideas from NetBSD and Linux, and some code from NetBSD
Diffstat (limited to 'sys/i386/include/endian.h')
-rw-r--r--sys/i386/include/endian.h53
1 files changed, 49 insertions, 4 deletions
diff --git a/sys/i386/include/endian.h b/sys/i386/include/endian.h
index 3ee77f5..dc924b1 100644
--- a/sys/i386/include/endian.h
+++ b/sys/i386/include/endian.h
@@ -69,31 +69,76 @@
#ifdef __GNUC__
-#define __word_swap_int(x) \
+#define __word_swap_int_var(x) \
__extension__ ({ register __uint32_t __X = (x); \
__asm ("rorl $16, %0" : "+r" (__X)); \
__X; })
+#ifdef __OPTIMIZE__
+
+#define __word_swap_int_const(x) \
+ ((((x) & 0xffff0000) >> 16) | \
+ (((x) & 0x0000ffff) << 16))
+#define __word_swap_int(x) (__builtin_constant_p(x) ? \
+ __word_swap_int_const(x) : __word_swap_int_var(x))
+
+#else /* __OPTIMIZE__ */
+
+#define __word_swap_int(x) __word_swap_int_var(x)
+
+#endif /* __OPTIMIZE__ */
+
#if defined(_KERNEL) && (defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)) && !defined(I386_CPU)
-#define __byte_swap_int(x) \
+#define __byte_swap_int_var(x) \
__extension__ ({ register __uint32_t __X = (x); \
__asm ("bswap %0" : "+r" (__X)); \
__X; })
#else
-#define __byte_swap_int(x) \
+#define __byte_swap_int_var(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) \
+#ifdef __OPTIMIZE__
+
+#define __byte_swap_int_const(x) \
+ ((((x) & 0xff000000) >> 24) | \
+ (((x) & 0x00ff0000) >> 8) | \
+ (((x) & 0x0000ff00) << 8) | \
+ (((x) & 0x000000ff) << 24))
+#define __byte_swap_int(x) (__builtin_constant_p(x) ? \
+ __byte_swap_int_const(x) : __byte_swap_int_var(x))
+
+#else /* __OPTIMIZE__ */
+
+#define __byte_swap_int(x) __byte_swap_int_var(x)
+
+#endif /* __OPTIMIZE__ */
+
+#define __byte_swap_word_var(x) \
__extension__ ({ register __uint16_t __X = (x); \
__asm ("xchgb %h0, %b0" : "+q" (__X)); \
__X; })
+#ifdef __OPTIMIZE__
+
+#define __byte_swap_word_const(x) \
+ ((((x) & 0xff00) >> 8) | \
+ (((x) & 0x00ff) << 8))
+
+#define __byte_swap_word(x) (__builtin_constant_p(x) ? \
+ __byte_swap_word_const(x) : __byte_swap_word_var(x))
+
+#else /* __OPTIMIZE__ */
+
+#define __byte_swap_word(x) __byte_swap_word_var(x)
+
+#endif /* __OPTIMIZE__ */
+
static __inline __uint64_t
__bswap64(__uint64_t _x)
{
OpenPOWER on IntegriCloud