[SeaBIOS] [PATCH] Optimize ntohl() code.

Kevin O'Connor kevin at koconnor.net
Tue Feb 16 01:27:21 CET 2010


    Optimize ntohl() code.
    
    Use the assembler bswapl instruction if the value is not constant.

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



More information about the SeaBIOS mailing list