From 68caaa745ab1df22a4b9c52235f40d19d2084f35 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 15 Feb 2010 10:46:37 -0500 Subject: [PATCH] Optimize ntohl() code. Use the assembler bswapl instruction if the value is not constant. --- src/util.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/util.h b/src/util.h index 4228904..eb62507 100644 --- a/src/util.h +++ b/src/util.h @@ -101,6 +101,21 @@ static inline u32 __fls(u32 word) return word; } +static inline u16 __htons_constant(u16 val) { + return (val<<8) | (val>>8); +} +static inline u32 __htonl_constant(u32 val) { + return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24); +} +static inline u32 __htonl(u32 val) { + asm("bswapl %0" : "+r"(val)); + return val; +} +#define htonl(x) (__builtin_constant_p((u32)(x)) ? __htonl_constant(x) : __htonl(x)) +#define ntohl(x) htonl(x) +#define htons(x) __htons_constant(x) +#define ntohs(x) htons(x) + static inline u32 getesp(void) { u32 esp; asm("movl %%esp, %0" : "=rm"(esp)); @@ -395,11 +410,4 @@ extern u8 BiosChecksum; // version (auto generated file out/version.c) extern const char VERSION[]; -// XXX - optimize -#define ntohl(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | \ - (((x)&0xff0000) >> 8) | (((x)&0xff000000) >> 24)) -#define htonl(x) ntohl(x) -#define ntohs(x) ((((x)&0xff)<<8) | (((x)&0xff00)>>8)) -#define htons(x) ntohs(x) - #endif // util.h -- 2.39.5