Remove SIMD functions
Benchmarking showed that they were usually at least 2.5x slower on common platforms (x86_64), and the maintenance overhead tracking nightly wasn't worth it.
This commit is contained in:
parent
af8e638c59
commit
249b7321c9
2 changed files with 0 additions and 102 deletions
|
|
@ -1,5 +1,4 @@
|
||||||
#![allow(clippy::uninlined_format_args)]
|
#![allow(clippy::uninlined_format_args)]
|
||||||
#![cfg_attr(feature = "simd", feature(portable_simd))]
|
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod standalone;
|
mod standalone;
|
||||||
|
|
|
||||||
101
src/utilities.rs
101
src/utilities.rs
|
|
@ -1,6 +1,3 @@
|
||||||
#[cfg(feature = "simd")]
|
|
||||||
use std::simd::{f32x16, i16x16, SimdInt};
|
|
||||||
|
|
||||||
/// Convert an array of 16 bit mono audio samples to a vector of 32 bit floats.
|
/// Convert an array of 16 bit mono audio samples to a vector of 32 bit floats.
|
||||||
///
|
///
|
||||||
/// This variant does not use SIMD instructions.
|
/// This variant does not use SIMD instructions.
|
||||||
|
|
@ -18,40 +15,6 @@ pub fn convert_integer_to_float_audio(samples: &[i16]) -> Vec<f32> {
|
||||||
floats
|
floats
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert an array of 16 bit mono audio samples to a vector of 32 bit floats.
|
|
||||||
///
|
|
||||||
/// This variant uses SIMD instructions, and as such is only available on
|
|
||||||
/// nightly Rust.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
/// * `samples` - The array of 16 bit mono audio samples.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A vector of 32 bit floats.
|
|
||||||
#[cfg(feature = "simd")]
|
|
||||||
pub fn convert_integer_to_float_audio_simd(samples: &[i16]) -> Vec<f32> {
|
|
||||||
let mut floats = Vec::with_capacity(samples.len());
|
|
||||||
|
|
||||||
let div_arr = f32x16::splat(32768.0);
|
|
||||||
|
|
||||||
let chunks = samples.chunks_exact(16);
|
|
||||||
let remainder = chunks.remainder();
|
|
||||||
for chunk in chunks {
|
|
||||||
let simd = i16x16::from_slice(chunk).cast::<f32>();
|
|
||||||
let simd = simd / div_arr;
|
|
||||||
floats.extend(&simd.to_array()[..]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the remainder.
|
|
||||||
// do this normally because it's only a few samples and the overhead of
|
|
||||||
// converting to SIMD is not worth it.
|
|
||||||
for sample in remainder {
|
|
||||||
floats.push(*sample as f32 / 32768.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
floats
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert 32 bit floating point stereo PCM audio to 32 bit floating point mono PCM audio.
|
/// Convert 32 bit floating point stereo PCM audio to 32 bit floating point mono PCM audio.
|
||||||
///
|
///
|
||||||
/// This variant does not use SIMD instructions.
|
/// This variant does not use SIMD instructions.
|
||||||
|
|
@ -73,46 +36,6 @@ pub fn convert_stereo_to_mono_audio(samples: &[f32]) -> Result<Vec<f32>, &'stati
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert 32 bit floating point stereo PCM audio to 32 bit floating point mono PCM audio.
|
|
||||||
///
|
|
||||||
/// This variant uses SIMD instructions, and as such is only available on
|
|
||||||
/// nightly Rust.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
/// * `samples` - The array of 32 bit floating point stereo PCM audio samples.
|
|
||||||
///
|
|
||||||
/// # Returns
|
|
||||||
/// A vector of 32 bit floating point mono PCM audio samples.
|
|
||||||
#[cfg(feature = "simd")]
|
|
||||||
pub fn convert_stereo_to_mono_audio_simd(samples: &[f32]) -> Result<Vec<f32>, &'static str> {
|
|
||||||
let mut mono = Vec::with_capacity(samples.len() / 2);
|
|
||||||
|
|
||||||
let div_array = f32x16::splat(2.0);
|
|
||||||
|
|
||||||
let chunks = samples.chunks_exact(32);
|
|
||||||
let remainder = chunks.remainder();
|
|
||||||
for chunk in chunks {
|
|
||||||
let [c1, c2] = [0, 1].map(|offset| {
|
|
||||||
let mut arr = [0.0; 16];
|
|
||||||
std::iter::zip(&mut arr, chunk.iter().skip(offset).step_by(2).copied())
|
|
||||||
.for_each(|(a, c)| *a = c);
|
|
||||||
arr
|
|
||||||
});
|
|
||||||
|
|
||||||
let c1 = f32x16::from(c1);
|
|
||||||
let c2 = f32x16::from(c2);
|
|
||||||
let mono_simd = (c1 + c2) / div_array;
|
|
||||||
mono.extend(&mono_simd.to_array()[..]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the remainder.
|
|
||||||
// do this normally because it's only a few samples and the overhead of
|
|
||||||
// converting to SIMD is not worth it.
|
|
||||||
mono.extend(convert_stereo_to_mono_audio(remainder)?);
|
|
||||||
|
|
||||||
Ok(mono)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "simd")]
|
#[cfg(feature = "simd")]
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
|
@ -126,27 +49,3 @@ mod test {
|
||||||
assert!(mono.is_err());
|
assert!(mono.is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "simd")]
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test_simd {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn assert_stereo_to_mono_simd() {
|
|
||||||
// fake some sample data
|
|
||||||
let samples = (0u16..1028).map(f32::from).collect::<Vec<f32>>();
|
|
||||||
let mono_simd = convert_stereo_to_mono_audio_simd(&samples);
|
|
||||||
let mono = convert_stereo_to_mono_audio(&samples);
|
|
||||||
assert_eq!(mono_simd, mono);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
pub fn assert_stereo_to_mono_simd_err() {
|
|
||||||
// fake some sample data
|
|
||||||
let samples = (0u16..1029).map(f32::from).collect::<Vec<f32>>();
|
|
||||||
let mono_simd = convert_stereo_to_mono_audio_simd(&samples);
|
|
||||||
let mono = convert_stereo_to_mono_audio(&samples);
|
|
||||||
assert_eq!(mono_simd, mono);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue