The quantiser runs the level curve eight lanes wide
Browse files at this commit59ae2400e11d489f11d2e0b26ac768a76b3f737bParent: dcaf6bb- perf
- the quantiser runs the level curve eight lanes wide
3 changed files +44 -10
- Cargo.lock +1 -0
- crates/ec-mp3/Cargo.toml +1 -0
- crates/ec-mp3/src/encode.rs +42 -10
Cargo.lockmodified+1 -0
| @@ -396,6 +396,7 @@ version = "0.1.0" | |||
| 396 | dependencies = [ | 396 | dependencies = [ |
| 397 | "ec-core", | 397 | "ec-core", |
| 398 | "ec-dsp", | 398 | "ec-dsp", |
| 399 | + "wide", | ||
| 399 | ] | 400 | ] |
| 400 | 401 | ||
| 401 | [[package]] | 402 | [[package]] |
crates/ec-mp3/Cargo.tomlmodified+1 -0
| @@ -9,3 +9,4 @@ authors.workspace = true | |||
| 9 | [dependencies] | 9 | [dependencies] |
| 10 | ec-core = { workspace = true } | 10 | ec-core = { workspace = true } |
| 11 | ec-dsp = { workspace = true } | 11 | ec-dsp = { workspace = true } |
| 12 | +wide = { workspace = true } | ||
crates/ec-mp3/src/encode.rsmodified+42 -10
| @@ -27,6 +27,7 @@ use crate::tables::{ | |||
| 27 | use ec_core::bitio::BitWriter; | 27 | use ec_core::bitio::BitWriter; |
| 28 | use ec_core::error::{Error, Result}; | 28 | use ec_core::error::{Error, Result}; |
| 29 | use ec_dsp::{RealFft, Window}; | 29 | use ec_dsp::{RealFft, Window}; |
| 30 | +use wide::f32x8; | ||
| 30 | 31 | ||
| 31 | /// Encoder settings. | 32 | /// Encoder settings. |
| 32 | /// | 33 | /// |
| @@ -1523,12 +1524,8 @@ fn quantise( | |||
| 1523 | block_type: u8, | 1524 | block_type: u8, |
| 1524 | ix: &mut [i32], | 1525 | ix: &mut [i32], |
| 1525 | ) { | 1526 | ) { |
| 1527 | + // The scalar form, kept for spans shorter than one SIMD width. | ||
| 1526 | let quantise_line = |xr: f32, scale: f32| -> i32 { | 1528 | let quantise_line = |xr: f32, scale: f32| -> i32 { |
| 1527 | - // |xr·scale|^(3/4) as a cube and two square roots: the libm `powf` | ||
| 1528 | - // this replaces was a tenth of the encoder's runtime all by itself. | ||
| 1529 | - // The two forms differ by an ulp or two, which next to the level | ||
| 1530 | - // boundary offset below can flip the rare line by one quantisation | ||
| 1531 | - // step and nothing more. | ||
| 1532 | let x = f64::from(xr.abs()) * f64::from(scale); | 1529 | let x = f64::from(xr.abs()) * f64::from(scale); |
| 1533 | let value = (x * x * x).sqrt().sqrt(); | 1530 | let value = (x * x * x).sqrt().sqrt(); |
| 1534 | let magnitude = (value + 0.4054).min(MAX_QUANT as f64) as i32; | 1531 | let magnitude = (value + 0.4054).min(MAX_QUANT as f64) as i32; |
| @@ -1552,9 +1549,7 @@ fn quantise( | |||
| 1552 | usize::from(short[band + 1]) * 3, | 1549 | usize::from(short[band + 1]) * 3, |
| 1553 | ); | 1550 | ); |
| 1554 | let scale = scales[band]; | 1551 | let scale = scales[band]; |
| 1555 | - for i in from..to.min(576) { | 1552 | + quantise_span(xr, ix, from, to.min(576), scale, &quantise_line); |
| 1556 | - ix[i] = quantise_line(xr[i], scale); | ||
| 1557 | - } | ||
| 1558 | } | 1553 | } |
| 1559 | return; | 1554 | return; |
| 1560 | } | 1555 | } |
| @@ -1568,10 +1563,47 @@ fn quantise( | |||
| 1568 | ); | 1563 | ); |
| 1569 | let scalefac = if band < SFB_LONG { scalefac[band] } else { 0 }; | 1564 | let scalefac = if band < SFB_LONG { scalefac[band] } else { 0 }; |
| 1570 | let scale = 1.0 / band_gain(gain, scalefac, block_type); | 1565 | let scale = 1.0 / band_gain(gain, scalefac, block_type); |
| 1571 | - for i in from..to { | 1566 | + quantise_span(xr, ix, from, to, scale, &quantise_line); |
| 1572 | - ix[i] = quantise_line(xr[i], scale); | 1567 | + } |
| 1568 | +} | ||
| 1569 | + | ||
| 1570 | +/// Quantises `xr[from..to]` at `scale` into `ix[from..to]`, eight lanes at a | ||
| 1571 | +/// time through `wide` — the per-line curve the scalar closure carries out | ||
| 1572 | +/// one line per call. Overflow lanes to `inf`, which the `MAX_QUANT` clamp | ||
| 1573 | +/// folds back the same way the scalar path clamps a saturated line; the | ||
| 1574 | +/// signed value truncates toward zero, exactly the sign-and-floor the scalar | ||
| 1575 | +/// form does in two steps. Levels can differ from the scalar path by one | ||
| 1576 | +/// step where the true level sits within f32 rounding of a boundary; the | ||
| 1577 | +/// decoded-quality gate exists for exactly that. | ||
| 1578 | +fn quantise_span( | ||
| 1579 | + xr: &[f32], | ||
| 1580 | + ix: &mut [i32], | ||
| 1581 | + from: usize, | ||
| 1582 | + to: usize, | ||
| 1583 | + scale: f32, | ||
| 1584 | + scalar: &impl Fn(f32, f32) -> i32, | ||
| 1585 | +) { | ||
| 1586 | + let mut i = from; | ||
| 1587 | + if to - i >= 8 { | ||
| 1588 | + let vscale = f32x8::splat(scale); | ||
| 1589 | + let voff = f32x8::splat(0.4054); | ||
| 1590 | + let vmax = f32x8::splat(MAX_QUANT as f32); | ||
| 1591 | + while i + 8 <= to { | ||
| 1592 | + let mut block = [0.0f32; 8]; | ||
| 1593 | + block.copy_from_slice(&xr[i..i + 8]); | ||
| 1594 | + let v = f32x8::new(block); | ||
| 1595 | + let x = v.abs() * vscale; | ||
| 1596 | + let value = (x * x * x).sqrt().sqrt(); | ||
| 1597 | + let magnitude = (value + voff).min(vmax); | ||
| 1598 | + let ints = magnitude.copysign(v).trunc_int(); | ||
| 1599 | + ix[i..i + 8].copy_from_slice(&ints.to_array()); | ||
| 1600 | + i += 8; | ||
| 1573 | } | 1601 | } |
| 1574 | } | 1602 | } |
| 1603 | + while i < to { | ||
| 1604 | + ix[i] = scalar(xr[i], scale); | ||
| 1605 | + i += 1; | ||
| 1606 | + } | ||
| 1575 | } | 1607 | } |
| 1576 | 1608 | ||
| 1577 | /// The short scalefactor band a spectral line belongs to. Short-block spectra | 1609 | /// The short scalefactor band a spectral line belongs to. Short-block spectra |