[FFmpeg-devel] [PATCHv3] avutil/common: add av_rint64_clip
Hendrik Leppkes
h.leppkes at gmail.com
Fri Nov 13 15:33:05 CET 2015
On Fri, Nov 13, 2015 at 3:23 PM, Ganesh Ajjanagadde
<gajjanagadde at gmail.com> wrote:
> The rationale for this function is reflected in the documentation for
> it, and is copied here:
>
> Clip a double value into the long long amin-amax range.
> This function is needed because conversion of floating point to integers when
> it does not fit in the integer's representation does not necessarily saturate
> correctly (usually converted to a cvttsd2si on x86) which saturates numbers
>> INT64_MAX to INT64_MIN. The standard marks such conversions as undefined
> behavior, allowing this sort of mathematically bogus conversions. This provides
> a safe alternative that is slower obviously but assures safety and better
> mathematical behavior.
> API:
> @param a value to clip
> @param amin minimum value of the clip range
> @param amax maximum value of the clip range
> @return clipped value
>
> Note that a priori if one can guarantee from the calling side that the
> double is in range, it is safe to simply do an explicit/implicit cast,
> and that will be far faster. However, otherwise this function should be
> used.
>
> avutil minor version is bumped.
>
> Reviewed-by: Ronald S. Bultje <rsbultje at gmail.com>
> Signed-off-by: Ganesh Ajjanagadde <gajjanagadde at gmail.com>
> ---
> libavutil/common.h | 34 ++++++++++++++++++++++++++++++++++
> libavutil/version.h | 4 ++--
> 2 files changed, 36 insertions(+), 2 deletions(-)
>
> diff --git a/libavutil/common.h b/libavutil/common.h
> index 6f0f582..54e5109 100644
> --- a/libavutil/common.h
> +++ b/libavutil/common.h
> @@ -298,6 +298,37 @@ static av_always_inline av_const double av_clipd_c(double a, double amin, double
> else return a;
> }
>
> +/**
> + * Clip and convert a double value into the long long amin-amax range.
> + * This function is needed because conversion of floating point to integers when
> + * it does not fit in the integer's representation does not necessarily saturate
> + * correctly (usually converted to a cvttsd2si on x86) which saturates numbers
> + * > INT64_MAX to INT64_MIN. The standard marks such conversions as undefined
> + * behavior, allowing this sort of mathematically bogus conversions. This provides
> + * a safe alternative that is slower obviously but assures safety and better
> + * mathematical behavior.
> + * @param a value to clip
> + * @param amin minimum value of the clip range
> + * @param amax maximum value of the clip range
> + * @return clipped value
> + */
> +static av_always_inline av_const int64_t av_rint64_clip_c(double a, int64_t amin, int64_t amax)
> +{
> +#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2
> + if (amin > amax) abort();
> +#endif
> + // INT64_MAX+1,INT64_MIN are exactly representable as IEEE doubles
> + if (a >= 9223372036854775807.0)
> + return FFMIN( 9223372036854775807, amax);
How could amax ever be > INT64_MAX? The variable could not represent that.
> + if (a <= -9223372036854775808.0)
> + return FFMAX(-9223372036854775807-1, amin);
Similar here.
If I'm missing some reason why this could be possible, these should
still be using the INT64_MIN/MAX constants however, and the build
problem fixed at the source.
libutvideo are C++ files, they probably neglected to set a define to
unlock these constants.
> + if (a < amin)
> + return amin;
> + if (a > amax)
> + return amax;
> + return llrint(a);
> +}
> +
> /** Compute ceil(log2(x)).
> * @param x value used to compute ceil(log2(x))
> * @return computed ceiling of log2(x)
> @@ -511,6 +542,9 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x)
> #ifndef av_clipd
> # define av_clipd av_clipd_c
> #endif
> +#ifndef av_rint64_clip
> +# define av_rint64_clip av_rint64_clip_c
> +#endif
> #ifndef av_popcount
> # define av_popcount av_popcount_c
> #endif
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 909f9a6..ea10ff0 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -56,8 +56,8 @@
> */
>
> #define LIBAVUTIL_VERSION_MAJOR 55
> -#define LIBAVUTIL_VERSION_MINOR 5
> -#define LIBAVUTIL_VERSION_MICRO 100
> +#define LIBAVUTIL_VERSION_MINOR 6
> +#define LIBAVUTIL_VERSION_MICRO 0
>
> #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
> LIBAVUTIL_VERSION_MINOR, \
> --
> 2.6.2
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
More information about the ffmpeg-devel
mailing list