#[derive(Clone, Copy)]
#[repr(usize)]
pub enum Stage {
EncMdct,
EncPsy,
EncTns,
EncXpow,
EncEstimate,
EncQuant,
EncCodebook,
DecIcs,
DecDequant,
DecTools,
DecImdct,
DecSbrAnalysis,
DecSbrHf,
DecSbrSynthesis,
DecPs,
}
pub const STAGES: [&str; 15] = [
"enc mdct (analyze_long/short)",
"enc psy (perceptual offsets)",
"enc tns analysis",
"enc xpow (|x|^0.75)",
"enc rate-loop estimate",
"enc quantize (code_core)",
"enc codebook select",
"dec ics (huffman + side info)",
"dec dequant (|q|^4/3 x gain, pns)",
"dec tools (ms/is/pred/ltp/tns)",
"dec imdct + window",
"dec sbr qmf analysis",
"dec sbr hf gen + adjust",
"dec sbr qmf synthesis",
"dec ps",
];
#[derive(Clone, Copy)]
#[repr(usize)]
pub enum Kernel {
Quantize,
Xpow,
Fft,
FmulWindow,
QmfWindow,
}
pub const KERNELS: [&str; 5] = [
"quantize_band",
"xpow",
"radix2 fft",
"fmul_window",
"qmf_window",
];
#[cfg(feature = "profile")]
mod imp {
use super::{Kernel, Stage, KERNELS, STAGES};
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::time::Instant;
static NS: [AtomicU64; STAGES.len()] = [const { AtomicU64::new(0) }; STAGES.len()];
static CALLS: [AtomicU64; STAGES.len()] = [const { AtomicU64::new(0) }; STAGES.len()];
static SIMD: [AtomicU64; KERNELS.len()] = [const { AtomicU64::new(0) }; KERNELS.len()];
static SCALAR: [AtomicU64; KERNELS.len()] = [const { AtomicU64::new(0) }; KERNELS.len()];
pub struct Scope(usize, Instant);
impl Drop for Scope {
fn drop(&mut self) {
NS[self.0].fetch_add(self.1.elapsed().as_nanos() as u64, Relaxed);
CALLS[self.0].fetch_add(1, Relaxed);
}
}
#[inline]
pub fn scope(s: Stage) -> Scope {
Scope(s as usize, Instant::now())
}
#[inline]
pub fn count(k: Kernel, simd: bool, elems: usize) {
let t = if simd { &SIMD } else { &SCALAR };
t[k as usize].fetch_add(elems as u64, Relaxed);
}
pub fn take() -> (Vec<(&'static str, u64, u64)>, Vec<(&'static str, u64, u64)>) {
let stages = (0..STAGES.len())
.map(|i| (STAGES[i], NS[i].swap(0, Relaxed), CALLS[i].swap(0, Relaxed)))
.collect();
let kernels = (0..KERNELS.len())
.map(|i| {
(
KERNELS[i],
SIMD[i].swap(0, Relaxed),
SCALAR[i].swap(0, Relaxed),
)
})
.collect();
(stages, kernels)
}
}
#[cfg(not(feature = "profile"))]
mod imp {
use super::{Kernel, Stage};
pub struct Scope;
#[inline(always)]
pub fn scope(_: Stage) -> Scope {
Scope
}
#[inline(always)]
pub fn count(_: Kernel, _: bool, _: usize) {}
}
pub use imp::*;