[FFmpeg-devel] [PATCH 01/15] avutil/common: Add macro for left-shifting

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Wed Sep 25 01:02:56 EEST 2019


Left shifting a negative integer is undefined, yet often needed.
Therefore add a macro that internally uses multiplication by powers of
two to make it clear that a shift is intended.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
I don't insist on this macro. I only added it so that one can easily see
that shifting was intended. I initially wanted to use "FFLS", but
eventually chose FFLSHIFT because it is self-explanatory.

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

diff --git a/libavutil/common.h b/libavutil/common.h
index af35397eb9..93c5dd0af7 100644
--- a/libavutil/common.h
+++ b/libavutil/common.h
@@ -60,6 +60,14 @@
 /* Backwards compat. */
 #define FF_CEIL_RSHIFT AV_CEIL_RSHIFT
 
+/**
+ * Left shift macro designed to tackle the undefinedness
+ * of left-shifting negative numbers.
+ *
+ * Note: Still undefined if the multiplication overflows.
+ */
+#define FFLSHIFT(a,b) ((a) * (1 << (b)))
+
 #define FFUDIV(a,b) (((a)>0 ?(a):(a)-(b)+1) / (b))
 #define FFUMOD(a,b) ((a)-(b)*FFUDIV(a,b))
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list