Skip to content

Commit 09f83ff

Browse files
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 <malladi.sastry@intel.com>
1 parent b6f1bba commit 09f83ff

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/audio/tdfb/tdfb_direction.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,13 @@ static void theoretical_time_differences(struct tdfb_comp_data *cd, int16_t az)
406406

407407
for (i = 0; i < n_mic - 1; i++) {
408408
delta_d = d[i + 1] - d[0]; /* Meters Q4.12 */
409+
/* Multiply by the precomputed reciprocal in Q8 format, then adjust with
410+
* a right shift to correct scaling. The multiplication result is initially
411+
* in Q4.20 (12+8) format, and we want it back in Q4.12, so we shift right
412+
* by 8 positions.
413+
*/
409414
cd->direction.timediff_iter[i] =
410-
(int32_t)((((int64_t)delta_d) << 19) / SPEED_OF_SOUND);
415+
(int32_t)((((int64_t)delta_d) * RECIPROCAL_SPEED_OF_SOUND_Q8) >> 8);
411416
}
412417
}
413418

src/include/sof/math/numbers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,6 @@ uint32_t crc32(uint32_t base, const void *data, uint32_t bytes);
117117

118118
/* Speed of sound (m/s) in 20 C temperature at standard atmospheric pressure */
119119
#define SPEED_OF_SOUND 343
120+
#define RECIPROCAL_SPEED_OF_SOUND_Q8 24458 /* Precomputed inverse in Q8 */
120121

121122
#endif /* __SOF_MATH_NUMBERS_H__ */

0 commit comments

Comments
 (0)