Skip to content

Commit 375e417

Browse files
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 <malladi.sastry@intel.com>
1 parent fa8b4db commit 375e417

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/include/sof/audio/format_generic.h

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,37 @@
1010

1111
/* Saturation inline functions */
1212

13+
static inline int32_t sat_generic(int64_t x, int64_t max_val, int64_t min_val);
14+
1315
static inline int32_t sat_int32(int64_t x)
1416
{
15-
if (x > INT32_MAX)
16-
return INT32_MAX;
17-
else if (x < INT32_MIN)
18-
return INT32_MIN;
19-
else
20-
return (int32_t)x;
17+
return sat_generic(x, INT32_MAX, INT32_MIN);
2118
}
2219

2320
static inline int32_t sat_int24(int32_t x)
2421
{
25-
if (x > INT24_MAXVALUE)
26-
return INT24_MAXVALUE;
27-
else if (x < INT24_MINVALUE)
28-
return INT24_MINVALUE;
29-
else
30-
return x;
22+
return sat_generic(x, INT24_MAXVALUE, INT24_MINVALUE);
3123
}
3224

3325
static inline int16_t sat_int16(int32_t x)
3426
{
35-
if (x > INT16_MAX)
36-
return INT16_MAX;
37-
else if (x < INT16_MIN)
38-
return INT16_MIN;
39-
else
40-
return (int16_t)x;
27+
return (int16_t)sat_generic(x, INT16_MAX, INT16_MIN);
4128
}
4229

4330
static inline int8_t sat_int8(int32_t x)
4431
{
45-
if (x > INT8_MAX)
46-
return INT8_MAX;
47-
else if (x < INT8_MIN)
48-
return INT8_MIN;
49-
else
50-
return (int8_t)x;
32+
return (int8_t)sat_generic(x, INT8_MAX, INT8_MIN);
33+
}
34+
35+
static inline int32_t sat_generic(int64_t x, int64_t max_val, int64_t min_val)
36+
{
37+
int64_t mask_overflow = (max_val - x) >> 63;
38+
int64_t mask_underflow = (x - min_val) >> 63;
39+
40+
/* Apply masks to selectively replace x with min or max values, or keep x as is. */
41+
x = (x & ~mask_overflow) | (max_val & mask_overflow);
42+
x = (x & ~mask_underflow) | (min_val & mask_underflow);
43+
return (int32_t)x;
5144
}
5245

5346
#endif /* __SOF_AUDIO_FORMAT_GENERIC_H__ */

0 commit comments

Comments
 (0)