From 5b38e922360712c86e88ce783dff13a9375924ce Mon Sep 17 00:00:00 2001 From: Shriram Shastry Date: Mon, 27 May 2024 16:38:32 +0530 Subject: [PATCH] Audio: Use generic saturation logic for improved efficiency Introduce `sat_generic` for optimizing saturation across integer sizes (8, 16, 24, 32 bits) in the processing block. Consolidating saturation logic into a single function aims to: - Reduce code duplication and enhance maintainability. - Use bitwise operations to remove conditional branches, boosting execution speed. - Possibly lower cycle costs on supported architectures by streamlining the saturation process. This modification is poised to enhance efficiency without compromising functionality. Signed-off-by: Shriram Shastry --- src/include/sof/audio/format_generic.h | 41 +++++++++++--------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/src/include/sof/audio/format_generic.h b/src/include/sof/audio/format_generic.h index b63284cb872e..3e5677b52839 100644 --- a/src/include/sof/audio/format_generic.h +++ b/src/include/sof/audio/format_generic.h @@ -10,44 +10,37 @@ /* Saturation inline functions */ +static inline int32_t sat_generic(int64_t x, int64_t max_val, int64_t min_val); + static inline int32_t sat_int32(int64_t x) { - if (x > INT32_MAX) - return INT32_MAX; - else if (x < INT32_MIN) - return INT32_MIN; - else - return (int32_t)x; + return sat_generic(x, INT32_MAX, INT32_MIN); } static inline int32_t sat_int24(int32_t x) { - if (x > INT24_MAXVALUE) - return INT24_MAXVALUE; - else if (x < INT24_MINVALUE) - return INT24_MINVALUE; - else - return x; + return sat_generic(x, INT24_MAXVALUE, INT24_MINVALUE); } static inline int16_t sat_int16(int32_t x) { - if (x > INT16_MAX) - return INT16_MAX; - else if (x < INT16_MIN) - return INT16_MIN; - else - return (int16_t)x; + return (int16_t)sat_generic(x, INT16_MAX, INT16_MIN); } static inline int8_t sat_int8(int32_t x) { - if (x > INT8_MAX) - return INT8_MAX; - else if (x < INT8_MIN) - return INT8_MIN; - else - return (int8_t)x; + return (int8_t)sat_generic(x, INT8_MAX, INT8_MIN); +} + +static inline int32_t sat_generic(int64_t x, int64_t max_val, int64_t min_val) +{ + int64_t mask_overflow = (max_val - x) >> 63; + int64_t mask_underflow = (x - min_val) >> 63; + + /* Apply masks to selectively replace x with min or max values, or keep x as is. */ + x = (x & ~mask_overflow) | (max_val & mask_overflow); + x = (x & ~mask_underflow) | (min_val & mask_underflow); + return (int32_t)x; } #endif /* __SOF_AUDIO_FORMAT_GENERIC_H__ */