From 3559f58742359a3e2060dda7da17de4e1bb464b3 Mon Sep 17 00:00:00 2001 From: Shriram Shastry Date: Thu, 23 May 2024 12:24:59 +0530 Subject: [PATCH] Audio: Optimize division by speed of sound using fixed-point reciprocal This commit optimizes the division by the speed of sound in the `theoretical_time_differences` function. By precomputing the fixed-point reciprocal of the speed of sound, we eliminate the need for a costly division operation in each iteration. Signed-off-by: Shriram Shastry --- src/audio/tdfb/tdfb_direction.c | 7 ++++++- src/include/sof/math/numbers.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/audio/tdfb/tdfb_direction.c b/src/audio/tdfb/tdfb_direction.c index bbf9c9be1de1..cb246d805278 100644 --- a/src/audio/tdfb/tdfb_direction.c +++ b/src/audio/tdfb/tdfb_direction.c @@ -406,8 +406,13 @@ static void theoretical_time_differences(struct tdfb_comp_data *cd, int16_t az) for (i = 0; i < n_mic - 1; i++) { delta_d = d[i + 1] - d[0]; /* Meters Q4.12 */ + /* Multiply by the precomputed reciprocal in Q8 format, then adjust with + * a right shift to correct scaling. The multiplication result is initially + * in Q4.20 (12+8) format, and we want it back in Q4.12, so we shift right + * by 8 positions. + */ cd->direction.timediff_iter[i] = - (int32_t)((((int64_t)delta_d) << 19) / SPEED_OF_SOUND); + (int32_t)((((int64_t)delta_d) * RECIPROCAL_SPEED_OF_SOUND_Q8) >> 8); } } diff --git a/src/include/sof/math/numbers.h b/src/include/sof/math/numbers.h index 1fd61f6324eb..21243c6f6088 100644 --- a/src/include/sof/math/numbers.h +++ b/src/include/sof/math/numbers.h @@ -117,5 +117,6 @@ uint32_t crc32(uint32_t base, const void *data, uint32_t bytes); /* Speed of sound (m/s) in 20 C temperature at standard atmospheric pressure */ #define SPEED_OF_SOUND 343 +#define RECIPROCAL_SPEED_OF_SOUND_Q8 24280 /* Precomputed inverse in Q8 */ #endif /* __SOF_MATH_NUMBERS_H__ */