Skip to contentSkip to conversation

minios: Fix incorrect {, un}likely() definitions

PATCH In review Andrew Cooper 2 participants 2 messages Jun 30, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 minios: Fix incorrect {, un}likely() definitions

As identified in e5545fb6, likely() and unlikely() must convert their expressions to booleans before comparing to 1 or 0, to avoid truncation issues.

While editing this file, add inclusion guards.

CC Stefano Stabellini <[email protected]>
CC Samuel Thibault <[email protected]>
---
 extras/mini-os/include/compiler.h |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
extras/mini-os/include/compiler.h +7 -2
index e35c9d5..4188277 100644
@@ -1,5 +1,10 @@
+#ifndef __MINIOS_COMPILER_H_
+#define __MINIOS_COMPILER_H_
+
 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
 #define __builtin_expect(x, expected_value) (x)
 #endif
-#define unlikely(x)  __builtin_expect((x),0)
-#define likely(x)  __builtin_expect((x),1)
+#define unlikely(x)  __builtin_expect(!!(x),0)
+#define likely(x)    __builtin_expect(!!(x),1)
+
+#endif /* __MINIOS_COMPILER_H_ */
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Andrew Cooper, le Mon 30 Jun 2014 16:57:15 +0100, a écrit :

As identified in e5545fb6, likely() and unlikely() must convert their expressions to booleans before comparing to 1 or 0, to avoid truncation issues.

While editing this file, add inclusion guards.

CC Stefano Stabellini <[email protected]>
CC Samuel Thibault <[email protected]>
Quoted 24 lines
---
 extras/mini-os/include/compiler.h |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
extras/mini-os/include/compiler.h +7 -2
index e35c9d5..4188277 100644
@@ -1,5 +1,10 @@
+#ifndef __MINIOS_COMPILER_H_
+#define __MINIOS_COMPILER_H_
+
 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96
 #define __builtin_expect(x, expected_value) (x)
 #endif
-#define unlikely(x)  __builtin_expect((x),0)
-#define likely(x)  __builtin_expect((x),1)
+#define unlikely(x)  __builtin_expect(!!(x),0)
+#define likely(x)    __builtin_expect(!!(x),1)
+
+#endif /* __MINIOS_COMPILER_H_ */
Signature · 1 line
1.7.10.4
Signature · 1 line
Samuel
Quoted 1 line

No manual is ever necessary.

May I politely interject here: BULLSHIT. That's the biggest Apple lie of all! (Discussion in comp.os.linux.misc on the intuitiveness of interfaces.)