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

Ganesh Ajjanagadde gajjanag at mit.edu
Thu Sep 3 02:33:04 CEST 2015


On Wed, Sep 2, 2015 at 5:17 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
> From: Michael Niedermayer <michael at niedermayer.cc>
>
> This function avoids the undefined corner cases which affects other absolute
> value functions and macros

LGTM, except for the comment/commit message. This does not avoid all
weird cases, see e.g
https://stackoverflow.com/questions/202537/c-macro-question-x-vs-x
but only the reasonable ones, roughly speaking where one does not do
arithmetic expression inside the macro.
Somewhere a note should ideally be made regarding this.
Furthermore, the commit message ideally should say that this a macro,
not a function to guard people against such abuse of it.

>
> 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
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


More information about the ffmpeg-devel mailing list