Skip to contentSkip to conversation

util: Fix MIN_NON_ZERO

PATCH In review Fam Zheng 5 participants 5 messages Jul 12, 2016 – Jul 18, 2016

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 util: Fix MIN_NON_ZERO

MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

---
 include/qemu/osdep.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/qemu/osdep.h +2 -1
index e63da28..e4c6ae6 100644
@@ -151,7 +151,8 @@ extern int daemon(int, int);
 /* Minimum function that returns zero only iff both values are zero.
  * Intended for use with unsigned values only. */
 #ifndef MIN_NON_ZERO
-#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
+#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
+                                ((b) == 0 ? (a) : (MIN(a, b))))
 #endif
 
 /* Round number down to multiple */
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Fam Zheng

MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

Huh?

Old expansion, in various stages:

(((0) != 0 && (0) < (1)) ? (0) : (1)) ((0 && 1) ? 0 : 1)
(0 ? 0 : 1)
1

Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to:

(((1) != 0 && (1) < (0)) ? (1) : (0)) ((1 && 0) ? 1 : 0)
(0 ? 1 : 0)
0

in which case, you are correct that there is a bug.

Quoted 12 lines
---
 include/qemu/osdep.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/qemu/osdep.h +0 -0
index e63da28..e4c6ae6 100644

include/qemu/osdep.h extern int daemon(int, int); · line 155

Quoted 6 lines
include/qemu/osdep.h +2 -1
@@ -151,7 +151,8 @@ extern int daemon(int, int);
 /* Minimum function that returns zero only iff both values are zero.
  * Intended for use with unsigned values only. */
 #ifndef MIN_NON_ZERO
-#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
+#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
+                                ((b) == 0 ? (a) : (MIN(a, b))))

Another way might be:

(!(a) == !(b) ? MIN(a, b) : (a) + (b))

but you have to put more mental thought into that. I'm just fine with your version.

With the commit message fixed,

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 12 lines · Eric Blake
Quoted 1 line · Fam Zheng

MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

Huh?

Old expansion, in various stages:

(((0) != 0 && (0) < (1)) ? (0) : (1)) ((0 && 1) ? 0 : 1)
(0 ? 0 : 1)
1

Quoted 8 lines

Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to:

(((1) != 0 && (1) < (0)) ? (1) : (0)) ((1 && 0) ? 1 : 0)
(0 ? 1 : 0)
0

in which case, you are correct that there is a bug.

Commit message fixed, patch queued.

Paolo

Quoted 29 lines
Quoted 12 lines
---
 include/qemu/osdep.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/qemu/osdep.h +0 -0
index e63da28..e4c6ae6 100644

include/qemu/osdep.h extern int daemon(int, int); · line 155

Quoted 6 lines
include/qemu/osdep.h +2 -1
@@ -151,7 +151,8 @@ extern int daemon(int, int);
 /* Minimum function that returns zero only iff both values are zero.
  * Intended for use with unsigned values only. */
 #ifndef MIN_NON_ZERO
-#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
+#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
+                                ((b) == 0 ? (a) : (MIN(a, b))))

Another way might be:

(!(a) == !(b) ? MIN(a, b) : (a) + (b))

but you have to put more mental thought into that. I'm just fine with your version.

With the commit message fixed,

#3 lore raw

Am 12.07.2016 um 18:24 schrieb Paolo Bonzini:

Quoted 20 lines · Eric Blake
Quoted 18 lines · Fam Zheng
Quoted 1 line

MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

Huh?

Old expansion, in various stages:

(((0) != 0 && (0) < (1)) ? (0) : (1))
((0 && 1) ? 0 : 1)
(0 ? 0 : 1)
1

Maybe you meant MIN_NON_ZERO(1, 0), which evaluates to:

(((1) != 0 && (1) < (0)) ? (1) : (0))
((1 && 0) ? 1 : 0)
(0 ? 1 : 0)
0

Quoted 4 lines
Quoted 1 line

in which case, you are correct that there is a bug.

Commit message fixed, patch queued.

Paolo

Shouldn't we Cc qemu-stable?

Peter

#4 lore raw
In reply to Fam Zheng
Quoted 1 line · Fam Zheng

MIN_NON_ZERO(0, 1) is evaluated to 0. Rewrite the macro to fix it.

This is incorrect. The actual results are:

MIN_NON_ZERO(0, 1) = 1
MIN_NON_ZERO(1, 0) = 0

The second case is the buggy one.

Quoted 24 lines
---
 include/qemu/osdep.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/qemu/osdep.h +2 -1
index e63da28..e4c6ae6 100644
@@ -151,7 +151,8 @@ extern int daemon(int, int);
 /* Minimum function that returns zero only iff both values are zero.
  * Intended for use with unsigned values only. */
 #ifndef MIN_NON_ZERO
-#define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
+#define MIN_NON_ZERO(a, b) ((a) == 0 ? (b) : \
+                                ((b) == 0 ? (a) : (MIN(a, b))))
 #endif
 
 /* Round number down to multiple */
Signature · 1 line
2.7.4