aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGanesh Ajjanagadde <gajjanagadde@gmail.com>2015-10-30 14:21:15 -0400
committerMichael Niedermayer <michael@niedermayer.cc>2015-11-12 00:00:57 +0100
commit476ddffccbede4fdd3272b174b8f63a2e8125c8b (patch)
tree7dd9354073a0b2c110f412c42298716f7241777a
parentb533998d0a1da259b08c21a48de9cedb3e262348 (diff)
downloadffmpeg-476ddffccbede4fdd3272b174b8f63a2e8125c8b.tar.gz
avutil/common: add FFDIFFSIGN macro
This is of use for defining comparator callbacks. Common approaches like return x-y are not safe due to the risks of overflow. Furthermore, the (x > y) - (x < y) trick is optimized to branchless code. This also documents this macro accordingly. Reviewed-by: Michael Niedermayer <michael@niedermayer.cc> Signed-off-by: Ganesh Ajjanagadde <gajjanagadde@gmail.com> (cherry picked from commit 265f83fd35977a80e93b3cc13ceb65f52f129a3c) Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
-rw-r--r--libavutil/common.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/libavutil/common.h b/libavutil/common.h
index 38eae28d2a..7fe3ccc25a 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -76,6 +76,17 @@
*/
#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))
+/**
+ * Comparator.
+ * For two numerical expressions x and y, gives 1 if x > y, -1 if x < y, and 0
+ * if x == y. This is useful for instance in a qsort comparator callback.
+ * Furthermore, compilers are able to optimize this to branchless code, and
+ * there is no risk of overflow with signed types.
+ * As with many macros, this evaluates its argument multiple times, it thus
+ * must not have a side-effect.
+ */
+#define FFDIFFSIGN(x,y) (((x)>(y)) - ((x)<(y)))
+
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
#define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))