[FFmpeg-devel] [PATCH] [libavutil] Add saturated add/sub operations for int64_t.

James Almer jamrial at gmail.com
Fri May 1 20:32:30 EEST 2020


On 5/1/2020 2:23 PM, Dale Curtis wrote:
> On Fri, May 1, 2020 at 6:12 AM James Almer <jamrial at gmail.com> wrote:
> 
>> On 5/1/2020 6:36 AM, Carl Eugen Hoyos wrote:
>>>
>>> The macro exists to avoid separate patches?
>>
>> No, it exists to not require configure checks just to enable a path for
>> gcc/clang and another for other compilers.
>>
> 
> Since consensus seems to have landed on splitting the patches, I've done
> so. This thread now contains just the default implementation.

That wasn't the consensus. Neither Nicholas or I thought it was
required, but i don't have strong feelings about it.

> diff --git a/libavutil/common.h b/libavutil/common.h
> index 142ff9abe7..e926e7cb02 100644
> --- a/libavutil/common.h
> +++ b/libavutil/common.h
> @@ -291,6 +291,36 @@ static av_always_inline int av_sat_dsub32_c(int a, int b)
>      return av_sat_sub32(a, av_sat_add32(b, b));
>  }
>  
> +/**
> + * Add two signed 64-bit values with saturation.
> + *
> + * @param  a one value
> + * @param  b another value
> + * @return sum with signed saturation
> + */
> +static int64_t av_sat_add64_c(int64_t a, int64_t b) {

Missing av_always_inline attribute

> +  if (b >= 0 && a >= INT64_MAX - b)
> +    return INT64_MAX;
> +  if (b <= 0 && a <= INT64_MIN - b)
> +    return INT64_MIN;
> +  return a + b;
> +}
> +
> +/**
> + * Subtract two signed 64-bit values with saturation.
> + *
> + * @param  a one value
> + * @param  b another value
> + * @return difference with signed saturation
> + */
> +static int64_t av_sat_sub64_c(int64_t a, int64_t b) {

Ditto

> +  if (b <= 0 && a >= INT64_MAX + b) {
> +    return INT64_MAX;
> +  if (b >= 0 && a <= INT64_MIN + b) {
> +    return INT64_MIN;
> +  return a - b;
> +}



More information about the ffmpeg-devel mailing list