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

Submitted by Andreas Rheinhardt on Sept. 24, 2019, 10:02 p.m.

Details

Message ID 20190924220310.31157-2-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Sept. 24, 2019, 10:02 p.m.
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@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(+)

Comments

Michael Niedermayer Sept. 26, 2019, 7:38 a.m.
On Wed, Sep 25, 2019 at 12:02:56AM +0200, Andreas Rheinhardt wrote:
> 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@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)))

with 
a * (1 << b)
everyone knows what it does even if one doesnt know why its done that way

with 
FFLSHIFT(a, b)
only people who looked up the macro know what it does and why.

so iam a bit sceptic that this is a good idea.

thx

[...]

Patch hide | download patch | download mbox

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))