From 6c4975eaafd7f8f91e81ad8d6be744a434241fd3 Mon Sep 17 00:00:00 2001 From: Mans Rullgard Date: Sat, 11 Aug 2012 01:15:19 +0100 Subject: libavutil: add saturating addition functions Fixed-point audio codecs often use saturating arithmetic, and special instructions for these operations are common. Signed-off-by: Mans Rullgard --- libavutil/common.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libavutil/common.h') diff --git a/libavutil/common.h b/libavutil/common.h index c99d858472..433e8e490a 100644 --- a/libavutil/common.h +++ b/libavutil/common.h @@ -181,6 +181,30 @@ static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p) else return a; } +/** + * Add two signed 32-bit values with saturation. + * + * @param a one value + * @param b another value + * @return sum with signed saturation + */ +static av_always_inline int av_sat_add32_c(int a, int b) +{ + return av_clipl_int32((int64_t)a + b); +} + +/** + * Add a doubled value to another value with saturation at both stages. + * + * @param a first value + * @param b value doubled and added to a + * @return sum with signed saturation + */ +static av_always_inline int av_sat_dadd32_c(int a, int b) +{ + return av_sat_add32(a, av_sat_add32(b, b)); +} + /** * Clip a float value into the amin-amax range. * @param a value to clip @@ -387,6 +411,12 @@ static av_always_inline av_const int av_popcount64_c(uint64_t x) #ifndef av_clip_uintp2 # define av_clip_uintp2 av_clip_uintp2_c #endif +#ifndef av_sat_add32 +# define av_sat_add32 av_sat_add32_c +#endif +#ifndef av_sat_dadd32 +# define av_sat_dadd32 av_sat_dadd32_c +#endif #ifndef av_clipf # define av_clipf av_clipf_c #endif -- cgit v1.2.3