mirror of
https://git.ffmpeg.org/ffmpeg.git
synced 2024-12-26 01:02:33 +00:00
avutil/common: Add saturated add/sub operations for int64_t.
Many places are using their own custom code for handling overflow around timestamps or other int64_t values. There are enough of these now that having some common saturated math functions seems sound. Signed-off-by: Dale Curtis <dalecurtis@chromium.org> Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
parent
2d8d554f15
commit
a7e1af3cb1
@ -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 av_always_inline int64_t av_sat_add64_c(int64_t a, int64_t b) {
|
||||
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 av_always_inline int64_t av_sat_sub64_c(int64_t a, int64_t b) {
|
||||
if (b <= 0 && a >= INT64_MAX + b)
|
||||
return INT64_MAX;
|
||||
if (b >= 0 && a <= INT64_MIN + b)
|
||||
return INT64_MIN;
|
||||
return a - b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clip a float value into the amin-amax range.
|
||||
* @param a value to clip
|
||||
@ -545,6 +575,12 @@ static av_always_inline av_const int av_parity_c(uint32_t v)
|
||||
#ifndef av_sat_dsub32
|
||||
# define av_sat_dsub32 av_sat_dsub32_c
|
||||
#endif
|
||||
#ifndef av_sat_add64
|
||||
# define av_sat_add64 av_sat_add64_c
|
||||
#endif
|
||||
#ifndef av_sat_sub64
|
||||
# define av_sat_sub64 av_sat_sub64_c
|
||||
#endif
|
||||
#ifndef av_clipf
|
||||
# define av_clipf av_clipf_c
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user