DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
dspark::simd Namespace Reference

Functions

bool runtimeDispatchSupported () noexcept
 Whether this build carries AVX2+FMA kernels and the CPU can run them.
 
bool isRuntimeDispatchActive () noexcept
 Whether the AVX2+FMA kernels are selected now (see setRuntimeDispatch()).
 
void setRuntimeDispatch (bool enabled) noexcept
 Selects or deselects the AVX2+FMA kernels (on by default where runtimeDispatchSupported()).
 
void addWithGain (float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, float gain, int count) noexcept
 Adds source samples scaled by a gain factor into a destination buffer.
 
void addWithGain (double *DSPARK_RESTRICT dst, const double *DSPARK_RESTRICT src, double gain, int count) noexcept
 Double overload.
 
void applyGain (float *DSPARK_RESTRICT data, float gain, int count) noexcept
 Multiplies all samples in a buffer by a gain factor.
 
void applyGain (double *DSPARK_RESTRICT data, double gain, int count) noexcept
 Double overload.
 
float peakLevel (const float *DSPARK_RESTRICT data, int count) noexcept
 Returns the peak absolute sample value in a buffer.
 
double peakLevel (const double *DSPARK_RESTRICT data, int count) noexcept
 Double overload. NaN samples are ignored, as in the float version.
 
float dotProduct (const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int count) noexcept
 Computes the dot product of two arrays.
 
double dotProduct (const double *DSPARK_RESTRICT a, const double *DSPARK_RESTRICT b, int count) noexcept
 Double overload.
 
void dotProduct4 (const double *shared, const double *const *other, int count, double *out) noexcept
 dotProduct() of one array with four others: out[x] equals dotProduct(shared, other[x], count) bit for bit. The wide kernels load the shared operand once for all four products and share their reductions.
 
void dotProductStrided (const double *shared, const double *first, std::ptrdiff_t stride, int count, int outputs, double *out) noexcept
 Dot products of one array with outputs arrays stride elements apart: out[x] = sum_i shared[i] * first[x * stride + i] for x in [0, outputs).
 
void add (float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, int count) noexcept
 Adds source samples into a destination buffer (no scaling).
 
void add (double *DSPARK_RESTRICT dst, const double *DSPARK_RESTRICT src, int count) noexcept
 Double overload.
 
void multiply (float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int count) noexcept
 Element-wise product of two buffers into a destination.
 
void multiply (double *DSPARK_RESTRICT dst, const double *DSPARK_RESTRICT a, const double *DSPARK_RESTRICT b, int count) noexcept
 Double overload.
 
void copyWithGain (float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, float gain, int count) noexcept
 Copies a buffer applying a gain factor (out-of-place).
 
void copyWithGain (double *DSPARK_RESTRICT dst, const double *DSPARK_RESTRICT src, double gain, int count) noexcept
 Double overload.
 
void applyGainRamp (float *DSPARK_RESTRICT data, float gainStart, float gainEnd, int count) noexcept
 In-place linear gain ramp (the canonical click-free fade primitive).
 
void applyGainRamp (double *DSPARK_RESTRICT data, double gainStart, double gainEnd, int count) noexcept
 Double overload.
 
void addWithGainRamp (float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, float gainStart, float gainEnd, int count) noexcept
 Adds source samples scaled by a linear gain ramp into a destination.
 
void addWithGainRamp (double *DSPARK_RESTRICT dst, const double *DSPARK_RESTRICT src, double gainStart, double gainEnd, int count) noexcept
 Double overload.
 
float sumOfSquares (const float *DSPARK_RESTRICT data, int count) noexcept
 Returns the sum of squared samples (energy).
 
double sumOfSquares (const double *DSPARK_RESTRICT data, int count) noexcept
 Double overload.
 
void complexMulAccum (float *DSPARK_RESTRICT accum, const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int bins) noexcept
 Complex multiply-accumulate over interleaved [re, im, ...] spectra.
 
void complexMulAccum (double *DSPARK_RESTRICT accum, const double *DSPARK_RESTRICT a, const double *DSPARK_RESTRICT b, int bins) noexcept
 Double overload: the products and sums of the scalar form, two or four lanes at a time.
 
void complexExp (double *data, int bins) noexcept
 The exponential of each complex bin of an interleaved [re, im, ...] spectrum, in place: e^re (cos im + i sin im).
 
template<typename T >
void addWithGainT (T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gain, int count) noexcept
 
template<typename T >
void applyGainT (T *DSPARK_RESTRICT data, T gain, int count) noexcept
 
template<typename T >
T peakLevelT (const T *DSPARK_RESTRICT data, int count) noexcept
 
template<typename T >
T dotProductT (const T *DSPARK_RESTRICT a, const T *DSPARK_RESTRICT b, int count) noexcept
 
template<typename T >
void addT (T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, int count) noexcept
 
template<typename T >
void multiplyT (T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT a, const T *DSPARK_RESTRICT b, int count) noexcept
 
template<typename T >
void copyWithGainT (T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gain, int count) noexcept
 
template<typename T >
T sumOfSquaresT (const T *DSPARK_RESTRICT data, int count) noexcept
 
template<typename T >
void applyGainRampT (T *DSPARK_RESTRICT data, T gainStart, T gainEnd, int count) noexcept
 
template<typename T >
void addWithGainRampT (T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gainStart, T gainEnd, int count) noexcept
 
template<typename T >
void firCorrelate (const T *x, const T *h, int taps, T *DSPARK_RESTRICT y, int n, T gain) noexcept
 Block FIR as a valid correlation: y[i] = gain * sum_j h[j] * x[i + j] for i in [0, n), reading x[0 .. n + taps - 1).
 
template<typename T , int Banks>
void firCorrelateBank (const T *x, const T *h, int taps, T *const *y, int n) noexcept
 A bank of block FIRs sharing one input, as valid correlations: y[k][i] = sum_j h[j * Banks + k] * x[i + j] for i in [0, n) and each bank k, reading x[0 .. n + taps - 1).
 
template<typename T , int Banks>
void firCorrelateBankSum (const T *m, const T *h, int taps, T *DSPARK_RESTRICT y, int n) noexcept
 Sum of a bank of block FIRs over interleaved inputs: y[i] = sum_k (sum_j h[j * Banks + k] * m[(i + j) * Banks + k]), the outer sum taken in bank order, reading m[0 .. (n + taps - 1) * Banks).
 

Function Documentation

◆ add() [1/2]

void dspark::simd::add ( double *DSPARK_RESTRICT  dst,
const double *DSPARK_RESTRICT  src,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 867 of file SimdOps.h.

◆ add() [2/2]

void dspark::simd::add ( float *DSPARK_RESTRICT  dst,
const float *DSPARK_RESTRICT  src,
int  count 
)
inlinenoexcept

Adds source samples into a destination buffer (no scaling).

Definition at line 829 of file SimdOps.h.

◆ addT()

template<typename T >
void dspark::simd::addT ( T *DSPARK_RESTRICT  dst,
const T *DSPARK_RESTRICT  src,
int  count 
)
noexcept

Definition at line 1433 of file SimdOps.h.

◆ addWithGain() [1/2]

void dspark::simd::addWithGain ( double *DSPARK_RESTRICT  dst,
const double *DSPARK_RESTRICT  src,
double  gain,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 355 of file SimdOps.h.

◆ addWithGain() [2/2]

void dspark::simd::addWithGain ( float *DSPARK_RESTRICT  dst,
const float *DSPARK_RESTRICT  src,
float  gain,
int  count 
)
inlinenoexcept

Adds source samples scaled by a gain factor into a destination buffer.

Definition at line 302 of file SimdOps.h.

◆ addWithGainRamp() [1/2]

void dspark::simd::addWithGainRamp ( double *DSPARK_RESTRICT  dst,
const double *DSPARK_RESTRICT  src,
double  gainStart,
double  gainEnd,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 1158 of file SimdOps.h.

◆ addWithGainRamp() [2/2]

void dspark::simd::addWithGainRamp ( float *DSPARK_RESTRICT  dst,
const float *DSPARK_RESTRICT  src,
float  gainStart,
float  gainEnd,
int  count 
)
inlinenoexcept

Adds source samples scaled by a linear gain ramp into a destination.

Same ramp convention as applyGainRamp: sample i uses gainStart + i * (gainEnd - gainStart) / count, spanning [gainStart, gainEnd) across the block, each gain computed from its index (no drift). count <= 0 is a no-op.

Definition at line 1107 of file SimdOps.h.

◆ addWithGainRampT()

template<typename T >
void dspark::simd::addWithGainRampT ( T *DSPARK_RESTRICT  dst,
const T *DSPARK_RESTRICT  src,
T  gainStart,
T  gainEnd,
int  count 
)
noexcept

Definition at line 1468 of file SimdOps.h.

◆ addWithGainT()

template<typename T >
void dspark::simd::addWithGainT ( T *DSPARK_RESTRICT  dst,
const T *DSPARK_RESTRICT  src,
T  gain,
int  count 
)
noexcept

Definition at line 1405 of file SimdOps.h.

◆ applyGain() [1/2]

void dspark::simd::applyGain ( double *DSPARK_RESTRICT  data,
double  gain,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 449 of file SimdOps.h.

◆ applyGain() [2/2]

void dspark::simd::applyGain ( float *DSPARK_RESTRICT  data,
float  gain,
int  count 
)
inlinenoexcept

Multiplies all samples in a buffer by a gain factor.

Definition at line 411 of file SimdOps.h.

◆ applyGainRamp() [1/2]

void dspark::simd::applyGainRamp ( double *DSPARK_RESTRICT  data,
double  gainStart,
double  gainEnd,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 1058 of file SimdOps.h.

◆ applyGainRamp() [2/2]

void dspark::simd::applyGainRamp ( float *DSPARK_RESTRICT  data,
float  gainStart,
float  gainEnd,
int  count 
)
inlinenoexcept

In-place linear gain ramp (the canonical click-free fade primitive).

Sample i is scaled by gainStart + i * (gainEnd - gainStart) / count, so the ramp covers [gainStart, gainEnd) across the block: the last sample sits one step below gainEnd. For a continuous fade across consecutive blocks, start the next block at gainEnd. count <= 0 is a no-op.

Every gain is computed from its own index, never accumulated: an accumulated ramp drifted by 1.3e-3 over 4.8M samples and stepped at the seam between the vector body and the scalar tail. Here the vector lanes and the tail evaluate the same expression and agree exactly.

Definition at line 1020 of file SimdOps.h.

◆ applyGainRampT()

template<typename T >
void dspark::simd::applyGainRampT ( T *DSPARK_RESTRICT  data,
T  gainStart,
T  gainEnd,
int  count 
)
noexcept

Definition at line 1461 of file SimdOps.h.

◆ applyGainT()

template<typename T >
void dspark::simd::applyGainT ( T *DSPARK_RESTRICT  data,
T  gain,
int  count 
)
noexcept

Definition at line 1412 of file SimdOps.h.

◆ complexExp()

void dspark::simd::complexExp ( double *  data,
int  bins 
)
inlinenoexcept

The exponential of each complex bin of an interleaved [re, im, ...] spectrum, in place: e^re (cos im + i sin im).

What a minimum-phase design does with the spectrum of its cepstrum. The baseline takes each bin from the standard library. Builds and CPUs with AVX2 and FMA (at compile time, or after the run-time check of an x86-64 baseline build: see setRuntimeDispatch()) take four bins at a time by polynomials, within 4e-16 of the library's result relative to the bin's magnitude (measured), and leave to the library any four with a real part beyond +-700, an imaginary part beyond +-1e5 or a value that is not finite.

Parameters
dataInterleaved complex bins, overwritten.
binsHow many complex bins (none for zero or less).

Definition at line 1371 of file SimdOps.h.

◆ complexMulAccum() [1/2]

void dspark::simd::complexMulAccum ( double *DSPARK_RESTRICT  accum,
const double *DSPARK_RESTRICT  a,
const double *DSPARK_RESTRICT  b,
int  bins 
)
inlinenoexcept

Double overload: the products and sums of the scalar form, two or four lanes at a time.

Definition at line 1313 of file SimdOps.h.

◆ complexMulAccum() [2/2]

void dspark::simd::complexMulAccum ( float *DSPARK_RESTRICT  accum,
const float *DSPARK_RESTRICT  a,
const float *DSPARK_RESTRICT  b,
int  bins 
)
inlinenoexcept

Complex multiply-accumulate over interleaved [re, im, ...] spectra.

The workhorse of frequency-domain (partitioned) convolution: accum += a * b evaluated as complex numbers, for bins complex bins.

Definition at line 1234 of file SimdOps.h.

◆ copyWithGain() [1/2]

void dspark::simd::copyWithGain ( double *DSPARK_RESTRICT  dst,
const double *DSPARK_RESTRICT  src,
double  gain,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 977 of file SimdOps.h.

◆ copyWithGain() [2/2]

void dspark::simd::copyWithGain ( float *DSPARK_RESTRICT  dst,
const float *DSPARK_RESTRICT  src,
float  gain,
int  count 
)
inlinenoexcept

Copies a buffer applying a gain factor (out-of-place).

Definition at line 951 of file SimdOps.h.

◆ copyWithGainT()

template<typename T >
void dspark::simd::copyWithGainT ( T *DSPARK_RESTRICT  dst,
const T *DSPARK_RESTRICT  src,
T  gain,
int  count 
)
noexcept

Definition at line 1447 of file SimdOps.h.

◆ dotProduct() [1/2]

double dspark::simd::dotProduct ( const double *DSPARK_RESTRICT  a,
const double *DSPARK_RESTRICT  b,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 705 of file SimdOps.h.

◆ dotProduct() [2/2]

float dspark::simd::dotProduct ( const float *DSPARK_RESTRICT  a,
const float *DSPARK_RESTRICT  b,
int  count 
)
inlinenoexcept

Computes the dot product of two arrays.

Definition at line 644 of file SimdOps.h.

◆ dotProduct4()

void dspark::simd::dotProduct4 ( const double *  shared,
const double *const *  other,
int  count,
double *  out 
)
inlinenoexcept

dotProduct() of one array with four others: out[x] equals dotProduct(shared, other[x], count) bit for bit. The wide kernels load the shared operand once for all four products and share their reductions.

Parameters
sharedThe operand common to the four products.
otherFour operands (may repeat, may alias the shared one).
countLength of every product.
outReceives the four results.

Definition at line 773 of file SimdOps.h.

◆ dotProductStrided()

void dspark::simd::dotProductStrided ( const double *  shared,
const double *  first,
std::ptrdiff_t  stride,
int  count,
int  outputs,
double *  out 
)
inlinenoexcept

Dot products of one array with outputs arrays stride elements apart: out[x] = sum_i shared[i] * first[x * stride + i] for x in [0, outputs).

Each product keeps one accumulator of the widest vector (a multiply-add chain over its vectors), whose lanes are then summed pairwise; the last count % width terms follow one at a time. A result therefore depends only on its own operands: splitting a set of products into calls of any sizes gives the same bits. The shared operand is loaded once for up to eight products, whose chains run side by side, so a set costs well below one dotProduct() per product. Runs on the AVX2+FMA copy when isRuntimeDispatchActive().

Parameters
sharedThe operand common to every product.
firstThe first product's other operand.
strideDistance in elements from one product's operand to the next.
countLength of every product.
outputsNumber of products (none when below 1).
outReceives the outputs results.

Definition at line 809 of file SimdOps.h.

◆ dotProductT()

template<typename T >
T dspark::simd::dotProductT ( const T *DSPARK_RESTRICT  a,
const T *DSPARK_RESTRICT  b,
int  count 
)
noexcept

Definition at line 1426 of file SimdOps.h.

◆ firCorrelate()

template<typename T >
void dspark::simd::firCorrelate ( const T *  x,
const T *  h,
int  taps,
T *DSPARK_RESTRICT  y,
int  n,
T  gain 
)
noexcept

Block FIR as a valid correlation: y[i] = gain * sum_j h[j] * x[i + j] for i in [0, n), reading x[0 .. n + taps - 1).

Register-blocked over outputs (four vectors at a time, the taps in the inner loop), so no output pays a horizontal sum: for the 16..256-tap kernels of oversamplers and resamplers it runs several times faster than one dotProduct() per output. y must not overlap x or h. Runs on the AVX2+FMA copy when isRuntimeDispatchActive().

Parameters
xInput, n + taps - 1 samples.
hKernel, taps coefficients (h[0] meets x[i]).
tapsKernel length.
yOutput, n samples.
nNumber of outputs.
gainScale applied to every output.

Definition at line 1496 of file SimdOps.h.

◆ firCorrelateBank()

template<typename T , int Banks>
void dspark::simd::firCorrelateBank ( const T *  x,
const T *  h,
int  taps,
T *const *  y,
int  n 
)
noexcept

A bank of block FIRs sharing one input, as valid correlations: y[k][i] = sum_j h[j * Banks + k] * x[i + j] for i in [0, n) and each bank k, reading x[0 .. n + taps - 1).

The coefficients are interleaved by tap, so each input sample is loaded once for all banks. Every output is the multiply-add chain that firCorrelate() with unit gain computes for that bank alone, in the same order, so the results are identical to Banks separate firCorrelate() calls on any target. Outputs must not overlap x or h. Runs on the AVX2+FMA copy when isRuntimeDispatchActive(), like firCorrelate().

Template Parameters
BanksNumber of banks (a multiple of the vector width, or any count).
Parameters
xInput, n + taps - 1 samples.
hInterleaved kernels, taps * Banks coefficients.
tapsKernel length.
yBanks output pointers, n samples each.
nNumber of outputs per bank.

Definition at line 1529 of file SimdOps.h.

◆ firCorrelateBankSum()

template<typename T , int Banks>
void dspark::simd::firCorrelateBankSum ( const T *  m,
const T *  h,
int  taps,
T *DSPARK_RESTRICT  y,
int  n 
)
noexcept

Sum of a bank of block FIRs over interleaved inputs: y[i] = sum_k (sum_j h[j * Banks + k] * m[(i + j) * Banks + k]), the outer sum taken in bank order, reading m[0 .. (n + taps - 1) * Banks).

Each bank's inner sum is one multiply-add chain over the taps, as in firCorrelate(). Inputs and coefficients are both interleaved by bank, so every load is a contiguous vector. y must not overlap m or h. Runs on the AVX2+FMA copy when isRuntimeDispatchActive().

Template Parameters
BanksNumber of banks.
Parameters
mInterleaved inputs, (n + taps - 1) * Banks samples.
hInterleaved kernels, taps * Banks coefficients.
tapsKernel length.
yOutput, n samples.
nNumber of outputs.

Definition at line 1562 of file SimdOps.h.

◆ isRuntimeDispatchActive()

bool dspark::simd::isRuntimeDispatchActive ( )
inlinenoexcept

Whether the AVX2+FMA kernels are selected now (see setRuntimeDispatch()).

Definition at line 262 of file SimdOps.h.

◆ multiply() [1/2]

void dspark::simd::multiply ( double *DSPARK_RESTRICT  dst,
const double *DSPARK_RESTRICT  a,
const double *DSPARK_RESTRICT  b,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 923 of file SimdOps.h.

◆ multiply() [2/2]

void dspark::simd::multiply ( float *DSPARK_RESTRICT  dst,
const float *DSPARK_RESTRICT  a,
const float *DSPARK_RESTRICT  b,
int  count 
)
inlinenoexcept

Element-wise product of two buffers into a destination.

Definition at line 900 of file SimdOps.h.

◆ multiplyT()

template<typename T >
void dspark::simd::multiplyT ( T *DSPARK_RESTRICT  dst,
const T *DSPARK_RESTRICT  a,
const T *DSPARK_RESTRICT  b,
int  count 
)
noexcept

Definition at line 1440 of file SimdOps.h.

◆ peakLevel() [1/2]

double dspark::simd::peakLevel ( const double *DSPARK_RESTRICT  data,
int  count 
)
inlinenoexcept

Double overload. NaN samples are ignored, as in the float version.

Definition at line 570 of file SimdOps.h.

◆ peakLevel() [2/2]

float dspark::simd::peakLevel ( const float *DSPARK_RESTRICT  data,
int  count 
)
inlinenoexcept

Returns the peak absolute sample value in a buffer.

NaN samples are ignored on every code path (SIMD and scalar): the result is the peak of the finite samples, 0 if there are none.

Definition at line 489 of file SimdOps.h.

◆ peakLevelT()

template<typename T >
T dspark::simd::peakLevelT ( const T *DSPARK_RESTRICT  data,
int  count 
)
noexcept

Definition at line 1419 of file SimdOps.h.

◆ runtimeDispatchSupported()

bool dspark::simd::runtimeDispatchSupported ( )
inlinenoexcept

Whether this build carries AVX2+FMA kernels and the CPU can run them.

True only in x86-64 builds below AVX (see DSPARK_NO_RUNTIME_DISPATCH) on a CPU with AVX2, FMA and OS support for the YMM state. Builds that target AVX2 at compile time use those kernels everywhere and return false here.

Definition at line 252 of file SimdOps.h.

◆ setRuntimeDispatch()

void dspark::simd::setRuntimeDispatch ( bool  enabled)
inlinenoexcept

Selects or deselects the AVX2+FMA kernels (on by default where runtimeDispatchSupported()).

The two kernel sets round differently (FMA, wider sums), so output can differ between machines in the last bits. Disable it before processing when renders must be bit-identical on every CPU. Enabling it on a CPU without AVX2 and FMA has no effect. Thread-safe; calls in progress finish on the set they began.

Definition at line 280 of file SimdOps.h.

◆ sumOfSquares() [1/2]

double dspark::simd::sumOfSquares ( const double *DSPARK_RESTRICT  data,
int  count 
)
inlinenoexcept

Double overload.

Definition at line 1219 of file SimdOps.h.

◆ sumOfSquares() [2/2]

float dspark::simd::sumOfSquares ( const float *DSPARK_RESTRICT  data,
int  count 
)
inlinenoexcept

Returns the sum of squared samples (energy).

Definition at line 1213 of file SimdOps.h.

◆ sumOfSquaresT()

template<typename T >
T dspark::simd::sumOfSquaresT ( const T *DSPARK_RESTRICT  data,
int  count 
)
noexcept

Definition at line 1454 of file SimdOps.h.