Skip to content

Commit

Permalink
Audio: Use generic saturation logic for improved efficiency
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ShriramShastry committed May 27, 2024
1 parent fa8b4db commit 375e417
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions src/include/sof/audio/format_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */

0 comments on commit 375e417

Please sign in to comment.