Optimize ntohl() code.
[seabios.git] / src / util.h
index 42289045c0b7f1ed845c877aae8fb35e81257bca..eb625077e338c655c7f51e0da4f68e12dc8ef235 100644 (file)
@@ -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