[FFmpeg-cvslog] avutil/common: add FFDIFFSIGN macro

Ganesh Ajjanagadde git at videolan.org
Tue Nov 3 22:31:53 CET 2015


ffmpeg | branch: master | Ganesh Ajjanagadde <gajjanagadde at gmail.com> | Fri Oct 30 14:21:15 2015 -0400| [265f83fd35977a80e93b3cc13ceb65f52f129a3c] | committer: Ganesh Ajjanagadde

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 at niedermayer.cc>
Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=265f83fd35977a80e93b3cc13ceb65f52f129a3c
---

 libavutil/common.h |   11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/libavutil/common.h b/libavutil/common.h
index 6594f7d..6f0f582 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))



More information about the ffmpeg-cvslog mailing list