Hi all.
I'm playing around with Jack and I'm doing some audio mixing in my code. This is simplified version of my mixing funcion:
fn audio_mix_2_channels(length: u32, b1: &[f32], b2: &[f32], f1: f32, f2: f32, out: &mut [f32]) {
assert!(b1.len() == length as usize);
assert!(b2.len() == length as usize);
assert!(out.len() == length as usize);
for i in 0 .. length as usize {
out[i] = b1[i] * f1 + b2[i] * f2;
}
}
The gist of my question is obviously the for
loop. What i wrote above is the most naive way to make a weighted sum of the elements of two slices, but it works perfectly. I'm not a machine code expert, but I looked at the resulting assembly code with https://godbolt.org/ and there's a lot of it for a simple calculation like this. I'm not surprised
How can I help the compiler to do a better job, given the assert!
s?
And what if I sum 3 or 4 buffers, like
out[i] = b1[i] * f1 + b2[i] * f2 + b3[i] * f3;
out[i] = b1[i] * f1 + b2[i] * f2 + b3[i] * f3 + b4[i] * f4;
Thanks in advance for any hint.