[FFmpeg-devel] [PATCH] avutil/common: Add FFNABS()

Michael Niedermayer michaelni at gmx.at
Thu Sep 3 02:17:24 CEST 2015


From: Michael Niedermayer <michael at niedermayer.cc>

This function avoids the undefined corner cases which affects other absolute
value functions and macros

Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
---
 libavutil/common.h |    4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libavutil/common.h b/libavutil/common.h
index 14343d9..89906be 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -63,10 +63,14 @@
  * Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they
  * are not representable as absolute values of their type. This is the same
  * as with *abs()
+ * @see FFNABS()
  */
 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
 #define FFSIGN(a) ((a) > 0 ? 1 : -1)
 
+// Negative Absolute value, this works for all integers of all types
+#define FFNABS(a) ((a) <= 0 ? (a) : (-(a)))
+
 #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))
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list