DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
SimdOps.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
26// --- Platform SIMD detection ------------------------------------------------
27#if defined(_M_AMD64) || defined(_M_X64) || defined(__x86_64__) || defined(__amd64__) \
28 || (defined(_M_IX86_FP) && _M_IX86_FP >= 2) || (defined(__i386__) && defined(__SSE2__))
29 #define DSPARK_SIMD_SSE2 1
30 #include <emmintrin.h> // SSE2
31 #if defined(__AVX__)
32 #define DSPARK_SIMD_AVX 1
33 #include <immintrin.h> // AVX
34 #endif
35 // Detect FMA3 support. __FMA__ is the authoritative flag on GCC/Clang:
36 // AVX2 alone does not licence FMA codegen there, and calling _mm*_fmadd_*
37 // without the fma target feature is a hard compile error (e.g. building
38 // with -mavx2 but not -mfma). MSVC never defines __FMA__ but permits FMA
39 // intrinsics under /arch:AVX2; clang-cl defines _MSC_VER yet enforces
40 // target features like clang, so it must take the __FMA__ route only.
41 #if defined(__FMA__) || (defined(__AVX2__) && defined(_MSC_VER) && !defined(__clang__))
42 #define DSPARK_SIMD_FMA 1
43 #endif
44#elif defined(__aarch64__) || defined(_M_ARM64)
45 #define DSPARK_SIMD_NEON 1
46 #include <arm_neon.h>
47#elif defined(__EMSCRIPTEN__) && defined(__wasm_simd128__) && defined(__SSE2__)
48 // Emscripten lowers the SSE2 intrinsic set to Wasm SIMD128 when compiled
49 // with -msimd128 -msse2 (both flags required). Anything less stays scalar.
50 #define DSPARK_SIMD_SSE2 1
51 #include <emmintrin.h>
52#endif
53
54// --- Run-time AVX2 dispatch -------------------------------------------------
55// x86-64 builds below AVX compile a second copy of the wide kernels inside an
56// AVX2+FMA target region and pick it at run time after a CPU check. Define
57// DSPARK_NO_RUNTIME_DISPATCH to compile the baseline paths only. clang-cl is
58// left out: its target-attribute and CPU-query support differ from both
59// clang and MSVC.
60#if defined(DSPARK_SIMD_SSE2) && !defined(DSPARK_SIMD_AVX) && !defined(DSPARK_NO_RUNTIME_DISPATCH) \
61 && (defined(_M_AMD64) || defined(_M_X64) || defined(__x86_64__) || defined(__amd64__)) \
62 && !(defined(_MSC_VER) && defined(__clang__))
63 #define DSPARK_SIMD_DISPATCH_AVX2 1
64 #include <immintrin.h>
65 #if defined(_MSC_VER)
66 #include <intrin.h>
67 #else
68 #include <cpuid.h>
69 #endif
70 #if defined(__clang__)
71 #define DSPARK_SIMD_AVX2_BEGIN \
72 _Pragma("clang attribute push (__attribute__((target(\"avx2,fma\"))), apply_to = function)")
73 #define DSPARK_SIMD_AVX2_END _Pragma("clang attribute pop")
74 #elif defined(__GNUC__)
75 #define DSPARK_SIMD_AVX2_BEGIN _Pragma("GCC push_options") _Pragma("GCC target(\"avx2,fma\")")
76 #define DSPARK_SIMD_AVX2_END _Pragma("GCC pop_options")
77 #else
78 // MSVC accepts AVX2/FMA intrinsics in any function.
79 #define DSPARK_SIMD_AVX2_BEGIN
80 #define DSPARK_SIMD_AVX2_END
81 #endif
82#endif
83
84// --- AVX2 without FMA --------------------------------------------------------
85// For kernels that must round exactly as the baseline code does (the block
86// forms of a scalar reader, bit for bit): a region that enables AVX2 only, so
87// the compiler has no fused multiply-add to contract the arithmetic into.
88// DSPARK_SIMD_EXACT_AVX2 is defined when such kernels can be built: always in
89// AVX2 builds, and in x86-64 baseline builds that dispatch at run time
90// (DSPARK_SIMD_EXACT_AVX2_DISPATCHED), where callers check
91// simd::isRuntimeDispatchActive() before using them.
92#if defined(DSPARK_SIMD_SSE2) && defined(__AVX2__)
93 #define DSPARK_SIMD_EXACT_AVX2 1
94 #define DSPARK_SIMD_EXACT_AVX2_BEGIN
95 #define DSPARK_SIMD_EXACT_AVX2_END
96#elif defined(DSPARK_SIMD_DISPATCH_AVX2)
97 #define DSPARK_SIMD_EXACT_AVX2 1
98 #define DSPARK_SIMD_EXACT_AVX2_DISPATCHED 1
99 #if defined(__clang__)
100 #define DSPARK_SIMD_EXACT_AVX2_BEGIN \
101 _Pragma("clang attribute push (__attribute__((target(\"avx2\"))), apply_to = function)")
102 #define DSPARK_SIMD_EXACT_AVX2_END _Pragma("clang attribute pop")
103 #elif defined(__GNUC__)
104 #define DSPARK_SIMD_EXACT_AVX2_BEGIN _Pragma("GCC push_options") _Pragma("GCC target(\"avx2\")")
105 #define DSPARK_SIMD_EXACT_AVX2_END _Pragma("GCC pop_options")
106 #else
107 // MSVC accepts AVX2 intrinsics in any function and never contracts.
108 #define DSPARK_SIMD_EXACT_AVX2_BEGIN
109 #define DSPARK_SIMD_EXACT_AVX2_END
110 #endif
111#endif
112
113// --- Restrict Macro for Pointer Aliasing Optimization -----------------------
114// NOTE: restrict constrains WRITTEN ranges only. A range written through one
115// DSPARK_RESTRICT pointer must not overlap any other range accessed in the
116// same call (so dst != src, no partial overlaps). Read-only pointers may
117// alias each other freely: dotProduct(data, data, n) is well defined, and is
118// exactly what sumOfSquares() does. Defined idempotently (#ifndef) because a
119// few headers historically declared the same macro.
120#ifndef DSPARK_RESTRICT
121 #if defined(_MSC_VER)
122 #define DSPARK_RESTRICT __restrict
123 #elif defined(__GNUC__) || defined(__clang__)
124 #define DSPARK_RESTRICT __restrict__
125 #else
126 #define DSPARK_RESTRICT
127 #endif
128#endif
129
130#include <atomic>
131#include <cmath>
132#include <cstddef>
133#include <cstdint>
134#include <type_traits>
135#include <utility>
136
137// --- Vec<T, W> and the kernels written on it --------------------------------
138// detail/SimdVec.inl defines Vec<T, W> (W lanes of T; W = 1 is scalar code),
139// kVecWidth / kVecNarrowWidth and the wide kernels, for the feature set named
140// by the DSPARK_VEC_* macros. The baseline copy lives in dspark::simd and is
141// what FFT.h, Oversampling.h and the effects use directly.
142#if defined(DSPARK_SIMD_SSE2)
143 #define DSPARK_VEC_SSE2 1
144#endif
145#if defined(DSPARK_SIMD_NEON)
146 #define DSPARK_VEC_NEON 1
147#endif
148#if defined(DSPARK_SIMD_AVX)
149 #define DSPARK_VEC_AVX 1
150 #if defined(__AVX2__)
151 #define DSPARK_VEC_AVX2 1
152 #endif
153#endif
154#if defined(DSPARK_SIMD_FMA)
155 #define DSPARK_VEC_FMA 1
156#endif
157// DSPARK_VEC_INLINE inlines a kernel block into its caller in the same copy:
158// a call per block would cost a large part of the block's own work.
159#if defined(_MSC_VER)
160 #define DSPARK_VEC_INLINE __forceinline
161#elif defined(__GNUC__) || defined(__clang__)
162 #define DSPARK_VEC_INLINE inline __attribute__((always_inline))
163#else
164 #define DSPARK_VEC_INLINE inline
165#endif
166namespace dspark {
167namespace simd {
168#include "detail/SimdVec.inl"
169} // namespace simd
170} // namespace dspark
171#undef DSPARK_VEC_SSE2
172#undef DSPARK_VEC_NEON
173#undef DSPARK_VEC_AVX
174#undef DSPARK_VEC_AVX2
175#undef DSPARK_VEC_FMA
176
177#if defined(DSPARK_SIMD_DISPATCH_AVX2)
178// The AVX2+FMA copy, compiled for that target and called only after the check.
179DSPARK_SIMD_AVX2_BEGIN
180#define DSPARK_VEC_SSE2 1
181#define DSPARK_VEC_AVX 1
182#define DSPARK_VEC_AVX2 1
183#define DSPARK_VEC_FMA 1
184namespace dspark {
185namespace simd {
186namespace avx2 {
187#include "detail/SimdVec.inl"
188} // namespace avx2
189} // namespace simd
190} // namespace dspark
191#undef DSPARK_VEC_SSE2
192#undef DSPARK_VEC_AVX
193#undef DSPARK_VEC_AVX2
194#undef DSPARK_VEC_FMA
195DSPARK_SIMD_AVX2_END
196
197namespace dspark {
198namespace simd {
199namespace dispatch {
200
202inline bool cpuSupportsAvx2Fma() noexcept
203{
204#if defined(_MSC_VER)
205 int r[4] = {};
206 __cpuid(r, 0);
207 if (r[0] < 7) return false;
208 __cpuid(r, 1);
209 const unsigned ecx = static_cast<unsigned>(r[2]);
210 const bool fma = (ecx & (1u << 12)) != 0, osxsave = (ecx & (1u << 27)) != 0,
211 avx = (ecx & (1u << 28)) != 0;
212 if (!fma || !osxsave || !avx) return false;
213 if ((_xgetbv(0) & 6u) != 6u) return false; // XMM and YMM state enabled
214 __cpuidex(r, 7, 0);
215 return (static_cast<unsigned>(r[1]) & (1u << 5)) != 0; // AVX2
216#else
217 unsigned a = 0, b = 0, c = 0, d = 0;
218 if (__get_cpuid_max(0, nullptr) < 7) return false;
219 if (!__get_cpuid(1, &a, &b, &c, &d)) return false;
220 const bool fma = (c & (1u << 12)) != 0, osxsave = (c & (1u << 27)) != 0,
221 avx = (c & (1u << 28)) != 0;
222 if (!fma || !osxsave || !avx) return false;
223 unsigned xcr0 = 0, xcr0High = 0;
224 // xgetbv, as bytes: the mnemonic needs the xsave target feature in clang.
225 __asm__ volatile(".byte 0x0f, 0x01, 0xd0" : "=a"(xcr0), "=d"(xcr0High) : "c"(0));
226 (void)xcr0High;
227 if ((xcr0 & 6u) != 6u) return false;
228 __cpuid_count(7, 0, a, b, c, d);
229 return (b & (1u << 5)) != 0; // AVX2
230#endif
231}
232
235inline std::atomic<bool> avx2Active { cpuSupportsAvx2Fma() };
236
237} // namespace dispatch
238} // namespace simd
239} // namespace dspark
240#endif
241
242namespace dspark {
243namespace simd {
244
252[[nodiscard]] inline bool runtimeDispatchSupported() noexcept
253{
254#if defined(DSPARK_SIMD_DISPATCH_AVX2)
255 return dispatch::cpuSupportsAvx2Fma();
256#else
257 return false;
258#endif
259}
260
262[[nodiscard]] inline bool isRuntimeDispatchActive() noexcept
263{
264#if defined(DSPARK_SIMD_DISPATCH_AVX2)
265 return dispatch::avx2Active.load(std::memory_order_relaxed);
266#else
267 return false;
268#endif
269}
270
280inline void setRuntimeDispatch(bool enabled) noexcept
281{
282#if defined(DSPARK_SIMD_DISPATCH_AVX2)
283 dispatch::avx2Active.store(enabled && dispatch::cpuSupportsAvx2Fma(), std::memory_order_relaxed);
284#else
285 (void)enabled;
286#endif
287}
288
289} // namespace simd
290} // namespace dspark
291
292namespace dspark {
293namespace simd {
294
295// ============================================================================
296// addWithGain -- dst[i] += src[i] * gain
297// ============================================================================
298
302inline void addWithGain(float* DSPARK_RESTRICT dst, const float* DSPARK_RESTRICT src, float gain, int count) noexcept
303{
304#if defined(DSPARK_SIMD_AVX)
305 const __m256 vGain = _mm256_set1_ps(gain);
306 int i = 0;
307 for (; i + 7 < count; i += 8)
308 {
309 __m256 vDst = _mm256_loadu_ps(dst + i);
310 __m256 vSrc = _mm256_loadu_ps(src + i);
311
312 #if defined(DSPARK_SIMD_FMA)
313 _mm256_storeu_ps(dst + i, _mm256_fmadd_ps(vSrc, vGain, vDst));
314 #else
315 _mm256_storeu_ps(dst + i, _mm256_add_ps(vDst, _mm256_mul_ps(vSrc, vGain)));
316 #endif
317 }
318 for (; i < count; ++i) dst[i] += src[i] * gain;
319
320#elif defined(DSPARK_SIMD_SSE2)
321 const __m128 vGain = _mm_set1_ps(gain);
322 int i = 0;
323 for (; i + 3 < count; i += 4)
324 {
325 __m128 vDst = _mm_loadu_ps(dst + i);
326 __m128 vSrc = _mm_loadu_ps(src + i);
327
328 #if defined(DSPARK_SIMD_FMA)
329 _mm_storeu_ps(dst + i, _mm_fmadd_ps(vSrc, vGain, vDst));
330 #else
331 _mm_storeu_ps(dst + i, _mm_add_ps(vDst, _mm_mul_ps(vSrc, vGain)));
332 #endif
333 }
334 for (; i < count; ++i) dst[i] += src[i] * gain;
335
336#elif defined(DSPARK_SIMD_NEON)
337 const float32x4_t vGain = vdupq_n_f32(gain);
338 int i = 0;
339 for (; i + 3 < count; i += 4)
340 {
341 float32x4_t vDst = vld1q_f32(dst + i);
342 float32x4_t vSrc = vld1q_f32(src + i);
343 // vfmaq guarantees a fused FMLA; vmlaq has separate mul+add semantics
344 // in ACLE and GCC lowers it to fmul+fadd.
345 vst1q_f32(dst + i, vfmaq_f32(vDst, vSrc, vGain));
346 }
347 for (; i < count; ++i) dst[i] += src[i] * gain;
348
349#else
350 for (int i = 0; i < count; ++i) dst[i] += src[i] * gain;
351#endif
352}
353
355inline void addWithGain(double* DSPARK_RESTRICT dst, const double* DSPARK_RESTRICT src, double gain, int count) noexcept
356{
357#if defined(DSPARK_SIMD_AVX)
358 const __m256d vGain = _mm256_set1_pd(gain);
359 int i = 0;
360 for (; i + 3 < count; i += 4)
361 {
362 __m256d vDst = _mm256_loadu_pd(dst + i);
363 __m256d vSrc = _mm256_loadu_pd(src + i);
364
365 #if defined(DSPARK_SIMD_FMA)
366 _mm256_storeu_pd(dst + i, _mm256_fmadd_pd(vSrc, vGain, vDst));
367 #else
368 _mm256_storeu_pd(dst + i, _mm256_add_pd(vDst, _mm256_mul_pd(vSrc, vGain)));
369 #endif
370 }
371 for (; i < count; ++i) dst[i] += src[i] * gain;
372
373#elif defined(DSPARK_SIMD_SSE2)
374 const __m128d vGain = _mm_set1_pd(gain);
375 int i = 0;
376 for (; i + 1 < count; i += 2)
377 {
378 __m128d vDst = _mm_loadu_pd(dst + i);
379 __m128d vSrc = _mm_loadu_pd(src + i);
380
381 #if defined(DSPARK_SIMD_FMA)
382 _mm_storeu_pd(dst + i, _mm_fmadd_pd(vSrc, vGain, vDst));
383 #else
384 _mm_storeu_pd(dst + i, _mm_add_pd(vDst, _mm_mul_pd(vSrc, vGain)));
385 #endif
386 }
387 for (; i < count; ++i) dst[i] += src[i] * gain;
388
389#elif defined(DSPARK_SIMD_NEON)
390 const float64x2_t vGain = vdupq_n_f64(gain);
391 int i = 0;
392 for (; i + 1 < count; i += 2)
393 {
394 float64x2_t vDst = vld1q_f64(dst + i);
395 float64x2_t vSrc = vld1q_f64(src + i);
396 vst1q_f64(dst + i, vfmaq_f64(vDst, vSrc, vGain));
397 }
398 for (; i < count; ++i) dst[i] += src[i] * gain;
399#else
400 for (int i = 0; i < count; ++i) dst[i] += src[i] * gain;
401#endif
402}
403
404// ============================================================================
405// applyGain -- data[i] *= gain
406// ============================================================================
407
411inline void applyGain(float* DSPARK_RESTRICT data, float gain, int count) noexcept
412{
413#if defined(DSPARK_SIMD_AVX)
414 const __m256 vGain = _mm256_set1_ps(gain);
415 int i = 0;
416 for (; i + 7 < count; i += 8)
417 {
418 __m256 v = _mm256_loadu_ps(data + i);
419 _mm256_storeu_ps(data + i, _mm256_mul_ps(v, vGain));
420 }
421 for (; i < count; ++i) data[i] *= gain;
422
423#elif defined(DSPARK_SIMD_SSE2)
424 const __m128 vGain = _mm_set1_ps(gain);
425 int i = 0;
426 for (; i + 3 < count; i += 4)
427 {
428 __m128 v = _mm_loadu_ps(data + i);
429 _mm_storeu_ps(data + i, _mm_mul_ps(v, vGain));
430 }
431 for (; i < count; ++i) data[i] *= gain;
432
433#elif defined(DSPARK_SIMD_NEON)
434 const float32x4_t vGain = vdupq_n_f32(gain);
435 int i = 0;
436 for (; i + 3 < count; i += 4)
437 {
438 float32x4_t v = vld1q_f32(data + i);
439 vst1q_f32(data + i, vmulq_f32(v, vGain));
440 }
441 for (; i < count; ++i) data[i] *= gain;
442
443#else
444 for (int i = 0; i < count; ++i) data[i] *= gain;
445#endif
446}
447
449inline void applyGain(double* DSPARK_RESTRICT data, double gain, int count) noexcept
450{
451#if defined(DSPARK_SIMD_AVX)
452 const __m256d vGain = _mm256_set1_pd(gain);
453 int i = 0;
454 for (; i + 3 < count; i += 4)
455 _mm256_storeu_pd(data + i, _mm256_mul_pd(_mm256_loadu_pd(data + i), vGain));
456 for (; i < count; ++i) data[i] *= gain;
457
458#elif defined(DSPARK_SIMD_SSE2)
459 const __m128d vGain = _mm_set1_pd(gain);
460 int i = 0;
461 for (; i + 1 < count; i += 2)
462 {
463 __m128d v = _mm_loadu_pd(data + i);
464 _mm_storeu_pd(data + i, _mm_mul_pd(v, vGain));
465 }
466 for (; i < count; ++i) data[i] *= gain;
467
468#elif defined(DSPARK_SIMD_NEON)
469 const float64x2_t vGain = vdupq_n_f64(gain);
470 int i = 0;
471 for (; i + 1 < count; i += 2)
472 vst1q_f64(data + i, vmulq_f64(vld1q_f64(data + i), vGain));
473 for (; i < count; ++i) data[i] *= gain;
474#else
475 for (int i = 0; i < count; ++i) data[i] *= gain;
476#endif
477}
478
479// ============================================================================
480// peakLevel -- max(abs(data[i]))
481// ============================================================================
482
489inline float peakLevel(const float* DSPARK_RESTRICT data, int count) noexcept
490{
491#if defined(DSPARK_SIMD_AVX)
492 const __m256 absMask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7FFFFFFF));
493 __m256 vMax = _mm256_setzero_ps();
494 int i = 0;
495 for (; i + 7 < count; i += 8)
496 {
497 __m256 v = _mm256_loadu_ps(data + i);
498 v = _mm256_and_ps(v, absMask);
499 // Data operand first: MAXPS returns the SECOND operand on NaN, so a
500 // NaN sample is skipped and the accumulator keeps its history
501 // (matching the scalar tail, whose comparison also skips NaNs).
502 vMax = _mm256_max_ps(v, vMax);
503 }
504 __m128 hi = _mm256_extractf128_ps(vMax, 1);
505 __m128 lo = _mm256_castps256_ps128(vMax);
506 __m128 m4 = _mm_max_ps(lo, hi);
507 __m128 m2 = _mm_max_ps(m4, _mm_movehl_ps(m4, m4));
508 __m128 m1 = _mm_max_ss(m2, _mm_shuffle_ps(m2, m2, 1));
509 float peak = _mm_cvtss_f32(m1);
510
511 for (; i < count; ++i)
512 {
513 float a = data[i] < 0.0f ? -data[i] : data[i];
514 if (a > peak) peak = a;
515 }
516 return peak;
517
518#elif defined(DSPARK_SIMD_SSE2)
519 const __m128 absMask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
520 __m128 vMax = _mm_setzero_ps();
521 int i = 0;
522 for (; i + 3 < count; i += 4)
523 {
524 __m128 v = _mm_loadu_ps(data + i);
525 v = _mm_and_ps(v, absMask);
526 vMax = _mm_max_ps(v, vMax); // data first: NaN skipped (see AVX note)
527 }
528 __m128 shuf = _mm_movehl_ps(vMax, vMax);
529 __m128 maxPair = _mm_max_ps(vMax, shuf);
530 __m128 maxSingle = _mm_max_ss(maxPair, _mm_shuffle_ps(maxPair, maxPair, 1));
531 float peak = _mm_cvtss_f32(maxSingle);
532
533 for (; i < count; ++i)
534 {
535 float a = data[i] < 0.0f ? -data[i] : data[i];
536 if (a > peak) peak = a;
537 }
538 return peak;
539
540#elif defined(DSPARK_SIMD_NEON)
541 float32x4_t vMax = vdupq_n_f32(0.0f);
542 int i = 0;
543 for (; i + 3 < count; i += 4)
544 {
545 float32x4_t v = vld1q_f32(data + i);
546 v = vabsq_f32(v);
547 vMax = vmaxnmq_f32(vMax, v); // FMAXNM: NaN operands are skipped
548 }
549 float peak = vmaxvq_f32(vMax);
550
551 for (; i < count; ++i)
552 {
553 float a = data[i] < 0.0f ? -data[i] : data[i];
554 if (a > peak) peak = a;
555 }
556 return peak;
557
558#else
559 float peak = 0.0f;
560 for (int i = 0; i < count; ++i)
561 {
562 float a = data[i] < 0.0f ? -data[i] : data[i];
563 if (a > peak) peak = a;
564 }
565 return peak;
566#endif
567}
568
570inline double peakLevel(const double* DSPARK_RESTRICT data, int count) noexcept
571{
572#if defined(DSPARK_SIMD_AVX)
573 const __m256d absMask = _mm256_castsi256_pd(_mm256_set1_epi64x(0x7FFFFFFFFFFFFFFFll));
574 __m256d vMax = _mm256_setzero_pd();
575 int i = 0;
576 for (; i + 3 < count; i += 4)
577 vMax = _mm256_max_pd(_mm256_and_pd(_mm256_loadu_pd(data + i), absMask), vMax);
578 __m128d lo = _mm256_castpd256_pd128(vMax);
579 __m128d hi = _mm256_extractf128_pd(vMax, 1);
580 __m128d m2 = _mm_max_pd(lo, hi);
581 double peak = _mm_cvtsd_f64(_mm_max_sd(m2, _mm_unpackhi_pd(m2, m2)));
582
583 for (; i < count; ++i)
584 {
585 double a = data[i] < 0.0 ? -data[i] : data[i];
586 if (a > peak) peak = a;
587 }
588 return peak;
589
590#elif defined(DSPARK_SIMD_SSE2)
591 const __m128d absMask = _mm_castsi128_pd(_mm_set_epi64x(
592 static_cast<int64_t>(0x7FFFFFFFFFFFFFFF),
593 static_cast<int64_t>(0x7FFFFFFFFFFFFFFF)));
594 __m128d vMax = _mm_setzero_pd();
595 int i = 0;
596 for (; i + 1 < count; i += 2)
597 {
598 __m128d v = _mm_loadu_pd(data + i);
599 v = _mm_and_pd(v, absMask);
600 vMax = _mm_max_pd(v, vMax); // data first: NaN skipped
601 }
602 __m128d hi = _mm_unpackhi_pd(vMax, vMax);
603 __m128d maxVal = _mm_max_sd(vMax, hi);
604 double peak = _mm_cvtsd_f64(maxVal);
605
606 for (; i < count; ++i)
607 {
608 double a = data[i] < 0.0 ? -data[i] : data[i];
609 if (a > peak) peak = a;
610 }
611 return peak;
612
613#elif defined(DSPARK_SIMD_NEON)
614 float64x2_t vMax = vdupq_n_f64(0.0);
615 int i = 0;
616 for (; i + 1 < count; i += 2)
617 vMax = vmaxnmq_f64(vMax, vabsq_f64(vld1q_f64(data + i)));
618 double peak = vmaxvq_f64(vMax);
619
620 for (; i < count; ++i)
621 {
622 double a = data[i] < 0.0 ? -data[i] : data[i];
623 if (a > peak) peak = a;
624 }
625 return peak;
626#else
627 double peak = 0.0;
628 for (int i = 0; i < count; ++i)
629 {
630 double a = data[i] < 0.0 ? -data[i] : data[i];
631 if (a > peak) peak = a;
632 }
633 return peak;
634#endif
635}
636
637// ============================================================================
638// dotProduct -- sum(a[i] * b[i])
639// ============================================================================
640
644inline float dotProduct(const float* DSPARK_RESTRICT a, const float* DSPARK_RESTRICT b, int count) noexcept
645{
646#if defined(DSPARK_SIMD_AVX)
647 return dotProductWide(a, b, count);
648#elif defined(DSPARK_SIMD_SSE2)
649 #if defined(DSPARK_SIMD_DISPATCH_AVX2)
650 if (isRuntimeDispatchActive()) return avx2::dotProductWide(a, b, count);
651 #endif
652 // Two accumulators hide the add/FMA latency on SSE-class hardware.
653 __m128 vSum0 = _mm_setzero_ps();
654 __m128 vSum1 = _mm_setzero_ps();
655 int i = 0;
656 for (; i + 7 < count; i += 8)
657 {
658 #if defined(DSPARK_SIMD_FMA)
659 vSum0 = _mm_fmadd_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i), vSum0);
660 vSum1 = _mm_fmadd_ps(_mm_loadu_ps(a + i + 4), _mm_loadu_ps(b + i + 4), vSum1);
661 #else
662 vSum0 = _mm_add_ps(vSum0, _mm_mul_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i)));
663 vSum1 = _mm_add_ps(vSum1, _mm_mul_ps(_mm_loadu_ps(a + i + 4), _mm_loadu_ps(b + i + 4)));
664 #endif
665 }
666 for (; i + 3 < count; i += 4)
667 {
668 #if defined(DSPARK_SIMD_FMA)
669 vSum0 = _mm_fmadd_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i), vSum0);
670 #else
671 vSum0 = _mm_add_ps(vSum0, _mm_mul_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i)));
672 #endif
673 }
674 alignas(16) float tmp[4];
675 _mm_store_ps(tmp, _mm_add_ps(vSum0, vSum1));
676 float sum = tmp[0] + tmp[1] + tmp[2] + tmp[3];
677
678 for (; i < count; ++i) sum += a[i] * b[i];
679 return sum;
680
681#elif defined(DSPARK_SIMD_NEON)
682 float32x4_t vSum0 = vdupq_n_f32(0.0f);
683 float32x4_t vSum1 = vdupq_n_f32(0.0f);
684 int i = 0;
685 for (; i + 7 < count; i += 8)
686 {
687 vSum0 = vfmaq_f32(vSum0, vld1q_f32(a + i), vld1q_f32(b + i));
688 vSum1 = vfmaq_f32(vSum1, vld1q_f32(a + i + 4), vld1q_f32(b + i + 4));
689 }
690 for (; i + 3 < count; i += 4)
691 vSum0 = vfmaq_f32(vSum0, vld1q_f32(a + i), vld1q_f32(b + i));
692 float sum = vaddvq_f32(vaddq_f32(vSum0, vSum1));
693
694 for (; i < count; ++i) sum += a[i] * b[i];
695 return sum;
696
697#else
698 float sum = 0.0f;
699 for (int i = 0; i < count; ++i) sum += a[i] * b[i];
700 return sum;
701#endif
702}
703
705inline double dotProduct(const double* DSPARK_RESTRICT a, const double* DSPARK_RESTRICT b, int count) noexcept
706{
707#if defined(DSPARK_SIMD_AVX)
708 return dotProductWide(a, b, count);
709#elif defined(DSPARK_SIMD_SSE2)
710 #if defined(DSPARK_SIMD_DISPATCH_AVX2)
711 if (isRuntimeDispatchActive()) return avx2::dotProductWide(a, b, count);
712 #endif
713 __m128d vSum0 = _mm_setzero_pd();
714 __m128d vSum1 = _mm_setzero_pd();
715 int i = 0;
716 for (; i + 3 < count; i += 4)
717 {
718 #if defined(DSPARK_SIMD_FMA)
719 vSum0 = _mm_fmadd_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i), vSum0);
720 vSum1 = _mm_fmadd_pd(_mm_loadu_pd(a + i + 2), _mm_loadu_pd(b + i + 2), vSum1);
721 #else
722 vSum0 = _mm_add_pd(vSum0, _mm_mul_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i)));
723 vSum1 = _mm_add_pd(vSum1, _mm_mul_pd(_mm_loadu_pd(a + i + 2), _mm_loadu_pd(b + i + 2)));
724 #endif
725 }
726 for (; i + 1 < count; i += 2)
727 {
728 #if defined(DSPARK_SIMD_FMA)
729 vSum0 = _mm_fmadd_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i), vSum0);
730 #else
731 vSum0 = _mm_add_pd(vSum0, _mm_mul_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i)));
732 #endif
733 }
734 __m128d vSum = _mm_add_pd(vSum0, vSum1);
735 __m128d hi = _mm_unpackhi_pd(vSum, vSum);
736 double sum = _mm_cvtsd_f64(_mm_add_sd(vSum, hi));
737
738 for (; i < count; ++i) sum += a[i] * b[i];
739 return sum;
740
741#elif defined(DSPARK_SIMD_NEON)
742 float64x2_t vSum0 = vdupq_n_f64(0.0);
743 float64x2_t vSum1 = vdupq_n_f64(0.0);
744 int i = 0;
745 for (; i + 3 < count; i += 4)
746 {
747 vSum0 = vfmaq_f64(vSum0, vld1q_f64(a + i), vld1q_f64(b + i));
748 vSum1 = vfmaq_f64(vSum1, vld1q_f64(a + i + 2), vld1q_f64(b + i + 2));
749 }
750 for (; i + 1 < count; i += 2)
751 vSum0 = vfmaq_f64(vSum0, vld1q_f64(a + i), vld1q_f64(b + i));
752 double sum = vaddvq_f64(vaddq_f64(vSum0, vSum1));
753
754 for (; i < count; ++i) sum += a[i] * b[i];
755 return sum;
756#else
757 double sum = 0.0;
758 for (int i = 0; i < count; ++i) sum += a[i] * b[i];
759 return sum;
760#endif
761}
762
773inline void dotProduct4(const double* shared, const double* const* other, int count, double* out) noexcept
774{
775#if defined(DSPARK_SIMD_AVX)
776 dotProduct4Wide(shared, other, count, out);
777#else
778 #if defined(DSPARK_SIMD_DISPATCH_AVX2)
780 {
781 avx2::dotProduct4Wide(shared, other, count, out);
782 return;
783 }
784 #endif
785 for (int x = 0; x < 4; ++x) out[x] = dotProduct(shared, other[x], count);
786#endif
787}
788
809inline void dotProductStrided(const double* shared, const double* first, std::ptrdiff_t stride, int count,
810 int outputs, double* out) noexcept
811{
812#if defined(DSPARK_SIMD_DISPATCH_AVX2)
814 {
815 avx2::dotProductStridedKernel(shared, first, stride, count, outputs, out);
816 return;
817 }
818#endif
819 dotProductStridedKernel(shared, first, stride, count, outputs, out);
820}
821
822// ============================================================================
823// add -- dst[i] += src[i]
824// ============================================================================
825
829inline void add(float* DSPARK_RESTRICT dst, const float* DSPARK_RESTRICT src, int count) noexcept
830{
831#if defined(DSPARK_SIMD_AVX)
832 int i = 0;
833 for (; i + 7 < count; i += 8)
834 {
835 __m256 vDst = _mm256_loadu_ps(dst + i);
836 __m256 vSrc = _mm256_loadu_ps(src + i);
837 _mm256_storeu_ps(dst + i, _mm256_add_ps(vDst, vSrc));
838 }
839 for (; i < count; ++i) dst[i] += src[i];
840
841#elif defined(DSPARK_SIMD_SSE2)
842 int i = 0;
843 for (; i + 3 < count; i += 4)
844 {
845 __m128 vDst = _mm_loadu_ps(dst + i);
846 __m128 vSrc = _mm_loadu_ps(src + i);
847 _mm_storeu_ps(dst + i, _mm_add_ps(vDst, vSrc));
848 }
849 for (; i < count; ++i) dst[i] += src[i];
850
851#elif defined(DSPARK_SIMD_NEON)
852 int i = 0;
853 for (; i + 3 < count; i += 4)
854 {
855 float32x4_t vDst = vld1q_f32(dst + i);
856 float32x4_t vSrc = vld1q_f32(src + i);
857 vst1q_f32(dst + i, vaddq_f32(vDst, vSrc));
858 }
859 for (; i < count; ++i) dst[i] += src[i];
860
861#else
862 for (int i = 0; i < count; ++i) dst[i] += src[i];
863#endif
864}
865
867inline void add(double* DSPARK_RESTRICT dst, const double* DSPARK_RESTRICT src, int count) noexcept
868{
869#if defined(DSPARK_SIMD_AVX)
870 int i = 0;
871 for (; i + 3 < count; i += 4)
872 _mm256_storeu_pd(dst + i, _mm256_add_pd(_mm256_loadu_pd(dst + i), _mm256_loadu_pd(src + i)));
873 for (; i < count; ++i) dst[i] += src[i];
874
875#elif defined(DSPARK_SIMD_SSE2)
876 int i = 0;
877 for (; i + 1 < count; i += 2)
878 {
879 __m128d vDst = _mm_loadu_pd(dst + i);
880 __m128d vSrc = _mm_loadu_pd(src + i);
881 _mm_storeu_pd(dst + i, _mm_add_pd(vDst, vSrc));
882 }
883 for (; i < count; ++i) dst[i] += src[i];
884
885#elif defined(DSPARK_SIMD_NEON)
886 int i = 0;
887 for (; i + 1 < count; i += 2)
888 vst1q_f64(dst + i, vaddq_f64(vld1q_f64(dst + i), vld1q_f64(src + i)));
889 for (; i < count; ++i) dst[i] += src[i];
890#else
891 for (int i = 0; i < count; ++i) dst[i] += src[i];
892#endif
893}
894
895// ============================================================================
896// multiply -- dst[i] = a[i] * b[i]
897// ============================================================================
898
900inline void multiply(float* DSPARK_RESTRICT dst, const float* DSPARK_RESTRICT a, const float* DSPARK_RESTRICT b, int count) noexcept
901{
902#if defined(DSPARK_SIMD_AVX)
903 int i = 0;
904 for (; i + 7 < count; i += 8)
905 _mm256_storeu_ps(dst + i, _mm256_mul_ps(_mm256_loadu_ps(a + i), _mm256_loadu_ps(b + i)));
906 for (; i < count; ++i) dst[i] = a[i] * b[i];
907#elif defined(DSPARK_SIMD_SSE2)
908 int i = 0;
909 for (; i + 3 < count; i += 4)
910 _mm_storeu_ps(dst + i, _mm_mul_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i)));
911 for (; i < count; ++i) dst[i] = a[i] * b[i];
912#elif defined(DSPARK_SIMD_NEON)
913 int i = 0;
914 for (; i + 3 < count; i += 4)
915 vst1q_f32(dst + i, vmulq_f32(vld1q_f32(a + i), vld1q_f32(b + i)));
916 for (; i < count; ++i) dst[i] = a[i] * b[i];
917#else
918 for (int i = 0; i < count; ++i) dst[i] = a[i] * b[i];
919#endif
920}
921
923inline void multiply(double* DSPARK_RESTRICT dst, const double* DSPARK_RESTRICT a, const double* DSPARK_RESTRICT b, int count) noexcept
924{
925#if defined(DSPARK_SIMD_AVX)
926 int i = 0;
927 for (; i + 3 < count; i += 4)
928 _mm256_storeu_pd(dst + i, _mm256_mul_pd(_mm256_loadu_pd(a + i), _mm256_loadu_pd(b + i)));
929 for (; i < count; ++i) dst[i] = a[i] * b[i];
930#elif defined(DSPARK_SIMD_SSE2)
931 int i = 0;
932 for (; i + 1 < count; i += 2)
933 _mm_storeu_pd(dst + i, _mm_mul_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i)));
934 for (; i < count; ++i) dst[i] = a[i] * b[i];
935
936#elif defined(DSPARK_SIMD_NEON)
937 int i = 0;
938 for (; i + 1 < count; i += 2)
939 vst1q_f64(dst + i, vmulq_f64(vld1q_f64(a + i), vld1q_f64(b + i)));
940 for (; i < count; ++i) dst[i] = a[i] * b[i];
941#else
942 for (int i = 0; i < count; ++i) dst[i] = a[i] * b[i];
943#endif
944}
945
946// ============================================================================
947// copyWithGain -- dst[i] = src[i] * gain
948// ============================================================================
949
951inline void copyWithGain(float* DSPARK_RESTRICT dst, const float* DSPARK_RESTRICT src, float gain, int count) noexcept
952{
953#if defined(DSPARK_SIMD_AVX)
954 const __m256 vGain = _mm256_set1_ps(gain);
955 int i = 0;
956 for (; i + 7 < count; i += 8)
957 _mm256_storeu_ps(dst + i, _mm256_mul_ps(_mm256_loadu_ps(src + i), vGain));
958 for (; i < count; ++i) dst[i] = src[i] * gain;
959#elif defined(DSPARK_SIMD_SSE2)
960 const __m128 vGain = _mm_set1_ps(gain);
961 int i = 0;
962 for (; i + 3 < count; i += 4)
963 _mm_storeu_ps(dst + i, _mm_mul_ps(_mm_loadu_ps(src + i), vGain));
964 for (; i < count; ++i) dst[i] = src[i] * gain;
965#elif defined(DSPARK_SIMD_NEON)
966 const float32x4_t vGain = vdupq_n_f32(gain);
967 int i = 0;
968 for (; i + 3 < count; i += 4)
969 vst1q_f32(dst + i, vmulq_f32(vld1q_f32(src + i), vGain));
970 for (; i < count; ++i) dst[i] = src[i] * gain;
971#else
972 for (int i = 0; i < count; ++i) dst[i] = src[i] * gain;
973#endif
974}
975
977inline void copyWithGain(double* DSPARK_RESTRICT dst, const double* DSPARK_RESTRICT src, double gain, int count) noexcept
978{
979#if defined(DSPARK_SIMD_AVX)
980 const __m256d vGain = _mm256_set1_pd(gain);
981 int i = 0;
982 for (; i + 3 < count; i += 4)
983 _mm256_storeu_pd(dst + i, _mm256_mul_pd(_mm256_loadu_pd(src + i), vGain));
984 for (; i < count; ++i) dst[i] = src[i] * gain;
985#elif defined(DSPARK_SIMD_SSE2)
986 const __m128d vGain = _mm_set1_pd(gain);
987 int i = 0;
988 for (; i + 1 < count; i += 2)
989 _mm_storeu_pd(dst + i, _mm_mul_pd(_mm_loadu_pd(src + i), vGain));
990 for (; i < count; ++i) dst[i] = src[i] * gain;
991
992#elif defined(DSPARK_SIMD_NEON)
993 const float64x2_t vGain = vdupq_n_f64(gain);
994 int i = 0;
995 for (; i + 1 < count; i += 2)
996 vst1q_f64(dst + i, vmulq_f64(vld1q_f64(src + i), vGain));
997 for (; i < count; ++i) dst[i] = src[i] * gain;
998#else
999 for (int i = 0; i < count; ++i) dst[i] = src[i] * gain;
1000#endif
1001}
1002
1003// ============================================================================
1004// applyGainRamp -- data[i] *= gainStart + i * step (linear fade)
1005// ============================================================================
1006
1020inline void applyGainRamp(float* DSPARK_RESTRICT data, float gainStart, float gainEnd, int count) noexcept
1021{
1022 if (count <= 0) return;
1023 const float step = (gainEnd - gainStart) / static_cast<float>(count);
1024 int i = 0;
1025#if defined(DSPARK_SIMD_AVX)
1026 const __m256 vStart = _mm256_set1_ps(gainStart), vStep = _mm256_set1_ps(step), vInc = _mm256_set1_ps(8.0f);
1027 __m256 vIdx = _mm256_setr_ps(0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f);
1028 for (; i + 7 < count; i += 8)
1029 {
1030 const __m256 vGain = _mm256_add_ps(vStart, _mm256_mul_ps(vStep, vIdx));
1031 _mm256_storeu_ps(data + i, _mm256_mul_ps(_mm256_loadu_ps(data + i), vGain));
1032 vIdx = _mm256_add_ps(vIdx, vInc);
1033 }
1034#elif defined(DSPARK_SIMD_SSE2)
1035 const __m128 vStart = _mm_set1_ps(gainStart), vStep = _mm_set1_ps(step), vInc = _mm_set1_ps(4.0f);
1036 __m128 vIdx = _mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f);
1037 for (; i + 3 < count; i += 4)
1038 {
1039 const __m128 vGain = _mm_add_ps(vStart, _mm_mul_ps(vStep, vIdx));
1040 _mm_storeu_ps(data + i, _mm_mul_ps(_mm_loadu_ps(data + i), vGain));
1041 vIdx = _mm_add_ps(vIdx, vInc);
1042 }
1043#elif defined(DSPARK_SIMD_NEON)
1044 const float32x4_t vStart = vdupq_n_f32(gainStart), vStep = vdupq_n_f32(step), vInc = vdupq_n_f32(4.0f);
1045 const float init[4] = { 0.0f, 1.0f, 2.0f, 3.0f };
1046 float32x4_t vIdx = vld1q_f32(init);
1047 for (; i + 3 < count; i += 4)
1048 {
1049 const float32x4_t vGain = vaddq_f32(vStart, vmulq_f32(vStep, vIdx));
1050 vst1q_f32(data + i, vmulq_f32(vld1q_f32(data + i), vGain));
1051 vIdx = vaddq_f32(vIdx, vInc);
1052 }
1053#endif
1054 for (; i < count; ++i) data[i] *= gainStart + step * static_cast<float>(i);
1055}
1056
1058inline void applyGainRamp(double* DSPARK_RESTRICT data, double gainStart, double gainEnd, int count) noexcept
1059{
1060 if (count <= 0) return;
1061 const double step = (gainEnd - gainStart) / static_cast<double>(count);
1062 int i = 0;
1063#if defined(DSPARK_SIMD_AVX)
1064 const __m256d vStart = _mm256_set1_pd(gainStart), vStep = _mm256_set1_pd(step), vInc = _mm256_set1_pd(4.0);
1065 __m256d vIdx = _mm256_setr_pd(0.0, 1.0, 2.0, 3.0);
1066 for (; i + 3 < count; i += 4)
1067 {
1068 const __m256d vGain = _mm256_add_pd(vStart, _mm256_mul_pd(vStep, vIdx));
1069 _mm256_storeu_pd(data + i, _mm256_mul_pd(_mm256_loadu_pd(data + i), vGain));
1070 vIdx = _mm256_add_pd(vIdx, vInc);
1071 }
1072#elif defined(DSPARK_SIMD_SSE2)
1073 const __m128d vStart = _mm_set1_pd(gainStart), vStep = _mm_set1_pd(step), vInc = _mm_set1_pd(2.0);
1074 __m128d vIdx = _mm_setr_pd(0.0, 1.0);
1075 for (; i + 1 < count; i += 2)
1076 {
1077 const __m128d vGain = _mm_add_pd(vStart, _mm_mul_pd(vStep, vIdx));
1078 _mm_storeu_pd(data + i, _mm_mul_pd(_mm_loadu_pd(data + i), vGain));
1079 vIdx = _mm_add_pd(vIdx, vInc);
1080 }
1081#elif defined(DSPARK_SIMD_NEON)
1082 const float64x2_t vStart = vdupq_n_f64(gainStart), vStep = vdupq_n_f64(step), vInc = vdupq_n_f64(2.0);
1083 const double init[2] = { 0.0, 1.0 };
1084 float64x2_t vIdx = vld1q_f64(init);
1085 for (; i + 1 < count; i += 2)
1086 {
1087 const float64x2_t vGain = vaddq_f64(vStart, vmulq_f64(vStep, vIdx));
1088 vst1q_f64(data + i, vmulq_f64(vld1q_f64(data + i), vGain));
1089 vIdx = vaddq_f64(vIdx, vInc);
1090 }
1091#endif
1092 for (; i < count; ++i) data[i] *= gainStart + step * static_cast<double>(i);
1093}
1094
1095// ============================================================================
1096// addWithGainRamp -- dst[i] += src[i] * (gainStart + i * step)
1097// ============================================================================
1098
1107inline void addWithGainRamp(float* DSPARK_RESTRICT dst, const float* DSPARK_RESTRICT src,
1108 float gainStart, float gainEnd, int count) noexcept
1109{
1110 if (count <= 0) return;
1111 const float step = (gainEnd - gainStart) / static_cast<float>(count);
1112 int i = 0;
1113#if defined(DSPARK_SIMD_AVX)
1114 const __m256 vStart = _mm256_set1_ps(gainStart), vStep = _mm256_set1_ps(step), vInc = _mm256_set1_ps(8.0f);
1115 __m256 vIdx = _mm256_setr_ps(0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f);
1116 for (; i + 7 < count; i += 8)
1117 {
1118 const __m256 vGain = _mm256_add_ps(vStart, _mm256_mul_ps(vStep, vIdx));
1119 const __m256 vDst = _mm256_loadu_ps(dst + i);
1120 const __m256 vSrc = _mm256_loadu_ps(src + i);
1121 #if defined(DSPARK_SIMD_FMA)
1122 _mm256_storeu_ps(dst + i, _mm256_fmadd_ps(vSrc, vGain, vDst));
1123 #else
1124 _mm256_storeu_ps(dst + i, _mm256_add_ps(vDst, _mm256_mul_ps(vSrc, vGain)));
1125 #endif
1126 vIdx = _mm256_add_ps(vIdx, vInc);
1127 }
1128#elif defined(DSPARK_SIMD_SSE2)
1129 const __m128 vStart = _mm_set1_ps(gainStart), vStep = _mm_set1_ps(step), vInc = _mm_set1_ps(4.0f);
1130 __m128 vIdx = _mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f);
1131 for (; i + 3 < count; i += 4)
1132 {
1133 const __m128 vGain = _mm_add_ps(vStart, _mm_mul_ps(vStep, vIdx));
1134 const __m128 vDst = _mm_loadu_ps(dst + i);
1135 const __m128 vSrc = _mm_loadu_ps(src + i);
1136 #if defined(DSPARK_SIMD_FMA)
1137 _mm_storeu_ps(dst + i, _mm_fmadd_ps(vSrc, vGain, vDst));
1138 #else
1139 _mm_storeu_ps(dst + i, _mm_add_ps(vDst, _mm_mul_ps(vSrc, vGain)));
1140 #endif
1141 vIdx = _mm_add_ps(vIdx, vInc);
1142 }
1143#elif defined(DSPARK_SIMD_NEON)
1144 const float32x4_t vStart = vdupq_n_f32(gainStart), vStep = vdupq_n_f32(step), vInc = vdupq_n_f32(4.0f);
1145 const float init[4] = { 0.0f, 1.0f, 2.0f, 3.0f };
1146 float32x4_t vIdx = vld1q_f32(init);
1147 for (; i + 3 < count; i += 4)
1148 {
1149 const float32x4_t vGain = vaddq_f32(vStart, vmulq_f32(vStep, vIdx));
1150 vst1q_f32(dst + i, vfmaq_f32(vld1q_f32(dst + i), vld1q_f32(src + i), vGain));
1151 vIdx = vaddq_f32(vIdx, vInc);
1152 }
1153#endif
1154 for (; i < count; ++i) dst[i] += src[i] * (gainStart + step * static_cast<float>(i));
1155}
1156
1158inline void addWithGainRamp(double* DSPARK_RESTRICT dst, const double* DSPARK_RESTRICT src,
1159 double gainStart, double gainEnd, int count) noexcept
1160{
1161 if (count <= 0) return;
1162 const double step = (gainEnd - gainStart) / static_cast<double>(count);
1163 int i = 0;
1164#if defined(DSPARK_SIMD_AVX)
1165 const __m256d vStart = _mm256_set1_pd(gainStart), vStep = _mm256_set1_pd(step), vInc = _mm256_set1_pd(4.0);
1166 __m256d vIdx = _mm256_setr_pd(0.0, 1.0, 2.0, 3.0);
1167 for (; i + 3 < count; i += 4)
1168 {
1169 const __m256d vGain = _mm256_add_pd(vStart, _mm256_mul_pd(vStep, vIdx));
1170 const __m256d vDst = _mm256_loadu_pd(dst + i);
1171 const __m256d vSrc = _mm256_loadu_pd(src + i);
1172 #if defined(DSPARK_SIMD_FMA)
1173 _mm256_storeu_pd(dst + i, _mm256_fmadd_pd(vSrc, vGain, vDst));
1174 #else
1175 _mm256_storeu_pd(dst + i, _mm256_add_pd(vDst, _mm256_mul_pd(vSrc, vGain)));
1176 #endif
1177 vIdx = _mm256_add_pd(vIdx, vInc);
1178 }
1179#elif defined(DSPARK_SIMD_SSE2)
1180 const __m128d vStart = _mm_set1_pd(gainStart), vStep = _mm_set1_pd(step), vInc = _mm_set1_pd(2.0);
1181 __m128d vIdx = _mm_setr_pd(0.0, 1.0);
1182 for (; i + 1 < count; i += 2)
1183 {
1184 const __m128d vGain = _mm_add_pd(vStart, _mm_mul_pd(vStep, vIdx));
1185 const __m128d vDst = _mm_loadu_pd(dst + i);
1186 const __m128d vSrc = _mm_loadu_pd(src + i);
1187 #if defined(DSPARK_SIMD_FMA)
1188 _mm_storeu_pd(dst + i, _mm_fmadd_pd(vSrc, vGain, vDst));
1189 #else
1190 _mm_storeu_pd(dst + i, _mm_add_pd(vDst, _mm_mul_pd(vSrc, vGain)));
1191 #endif
1192 vIdx = _mm_add_pd(vIdx, vInc);
1193 }
1194#elif defined(DSPARK_SIMD_NEON)
1195 const float64x2_t vStart = vdupq_n_f64(gainStart), vStep = vdupq_n_f64(step), vInc = vdupq_n_f64(2.0);
1196 const double init[2] = { 0.0, 1.0 };
1197 float64x2_t vIdx = vld1q_f64(init);
1198 for (; i + 1 < count; i += 2)
1199 {
1200 const float64x2_t vGain = vaddq_f64(vStart, vmulq_f64(vStep, vIdx));
1201 vst1q_f64(dst + i, vfmaq_f64(vld1q_f64(dst + i), vld1q_f64(src + i), vGain));
1202 vIdx = vaddq_f64(vIdx, vInc);
1203 }
1204#endif
1205 for (; i < count; ++i) dst[i] += src[i] * (gainStart + step * static_cast<double>(i));
1206}
1207
1208// ============================================================================
1209// sumOfSquares -- sum(data[i]^2) (RMS / energy metering)
1210// ============================================================================
1211
1213inline float sumOfSquares(const float* DSPARK_RESTRICT data, int count) noexcept
1214{
1215 return dotProduct(data, data, count);
1216}
1217
1219inline double sumOfSquares(const double* DSPARK_RESTRICT data, int count) noexcept
1220{
1221 return dotProduct(data, data, count);
1222}
1223
1224// ============================================================================
1225// complexMulAccum -- accum[k] += a[k] * b[k] over interleaved complex bins
1226// ============================================================================
1227
1234inline void complexMulAccum(float* DSPARK_RESTRICT accum, const float* DSPARK_RESTRICT a,
1235 const float* DSPARK_RESTRICT b, int bins) noexcept
1236{
1237#if defined(DSPARK_SIMD_AVX)
1238 complexMulAccumWide(accum, a, b, bins);
1239#elif defined(DSPARK_SIMD_SSE2)
1240 #if defined(DSPARK_SIMD_DISPATCH_AVX2)
1241 if (isRuntimeDispatchActive()) { avx2::complexMulAccumWide(accum, a, b, bins); return; }
1242 #endif
1243 // Sign mask: negate real positions for (re1*re2 - im1*im2)
1244 const __m128 negMask = _mm_castsi128_ps(_mm_setr_epi32(
1245 static_cast<int>(0x80000000u), 0,
1246 static_cast<int>(0x80000000u), 0));
1247
1248 int k = 0;
1249 for (; k + 1 < bins; k += 2)
1250 {
1251 const __m128 va = _mm_loadu_ps(a + 2 * k);
1252 const __m128 vb = _mm_loadu_ps(b + 2 * k);
1253 __m128 vacc = _mm_loadu_ps(accum + 2 * k);
1254
1255 const __m128 aRe = _mm_shuffle_ps(va, va, _MM_SHUFFLE(2, 2, 0, 0));
1256 const __m128 aIm = _mm_shuffle_ps(va, va, _MM_SHUFFLE(3, 3, 1, 1));
1257 const __m128 bSwap = _mm_shuffle_ps(vb, vb, _MM_SHUFFLE(2, 3, 0, 1));
1258
1259 const __m128 p1 = _mm_mul_ps(aRe, vb);
1260 const __m128 p2 = _mm_xor_ps(_mm_mul_ps(aIm, bSwap), negMask);
1261
1262 vacc = _mm_add_ps(vacc, _mm_add_ps(p1, p2));
1263 _mm_storeu_ps(accum + 2 * k, vacc);
1264 }
1265 for (; k < bins; ++k)
1266 {
1267 const float re1 = a[2 * k], im1 = a[2 * k + 1];
1268 const float re2 = b[2 * k], im2 = b[2 * k + 1];
1269 accum[2 * k] += re1 * re2 - im1 * im2;
1270 accum[2 * k + 1] += re1 * im2 + im1 * re2;
1271 }
1272#elif defined(DSPARK_SIMD_NEON)
1273 alignas(16) static constexpr uint32_t kNegRe[4] = { 0x80000000u, 0u, 0x80000000u, 0u };
1274 const uint32x4_t negMask = vld1q_u32(kNegRe);
1275
1276 int k = 0;
1277 for (; k + 1 < bins; k += 2)
1278 {
1279 const float32x4_t va = vld1q_f32(a + 2 * k);
1280 const float32x4_t vb = vld1q_f32(b + 2 * k);
1281 float32x4_t vacc = vld1q_f32(accum + 2 * k);
1282
1283 const float32x4_t aRe = vtrn1q_f32(va, va);
1284 const float32x4_t aIm = vtrn2q_f32(va, va);
1285 const float32x4_t bSwap = vrev64q_f32(vb);
1286
1287 const float32x4_t p1 = vmulq_f32(aRe, vb);
1288 const float32x4_t p2 = vreinterpretq_f32_u32(
1289 veorq_u32(vreinterpretq_u32_f32(vmulq_f32(aIm, bSwap)), negMask));
1290
1291 vacc = vaddq_f32(vacc, vaddq_f32(p1, p2));
1292 vst1q_f32(accum + 2 * k, vacc);
1293 }
1294 for (; k < bins; ++k)
1295 {
1296 const float re1 = a[2 * k], im1 = a[2 * k + 1];
1297 const float re2 = b[2 * k], im2 = b[2 * k + 1];
1298 accum[2 * k] += re1 * re2 - im1 * im2;
1299 accum[2 * k + 1] += re1 * im2 + im1 * re2;
1300 }
1301#else
1302 for (int k = 0; k < bins; ++k)
1303 {
1304 const float re1 = a[2 * k], im1 = a[2 * k + 1];
1305 const float re2 = b[2 * k], im2 = b[2 * k + 1];
1306 accum[2 * k] += re1 * re2 - im1 * im2;
1307 accum[2 * k + 1] += re1 * im2 + im1 * re2;
1308 }
1309#endif
1310}
1311
1313inline void complexMulAccum(double* DSPARK_RESTRICT accum, const double* DSPARK_RESTRICT a,
1314 const double* DSPARK_RESTRICT b, int bins) noexcept
1315{
1316 int k = 0;
1317#if defined(DSPARK_SIMD_AVX)
1318 k = complexMulAccumWide(accum, a, b, bins);
1319#elif defined(DSPARK_SIMD_SSE2)
1320 #if defined(DSPARK_SIMD_DISPATCH_AVX2)
1321 if (isRuntimeDispatchActive()) k = avx2::complexMulAccumWide(accum, a, b, bins);
1322 #endif
1323 const __m128d negRe = _mm_set_pd(0.0, -0.0);
1324 for (; k < bins; ++k)
1325 {
1326 const __m128d va = _mm_loadu_pd(a + 2 * k), vb = _mm_loadu_pd(b + 2 * k);
1327 const __m128d p1 = _mm_mul_pd(_mm_unpacklo_pd(va, va), vb);
1328 const __m128d p2 = _mm_xor_pd(_mm_mul_pd(_mm_unpackhi_pd(va, va), _mm_shuffle_pd(vb, vb, 1)), negRe);
1329 _mm_storeu_pd(accum + 2 * k, _mm_add_pd(_mm_loadu_pd(accum + 2 * k), _mm_add_pd(p1, p2)));
1330 }
1331#elif defined(DSPARK_SIMD_NEON)
1332 alignas(16) static constexpr uint64_t kNegRe[2] = { 0x8000000000000000ull, 0ull };
1333 const uint64x2_t negRe = vld1q_u64(kNegRe);
1334 for (; k < bins; ++k)
1335 {
1336 const float64x2_t va = vld1q_f64(a + 2 * k), vb = vld1q_f64(b + 2 * k);
1337 const float64x2_t p1 = vmulq_f64(vtrn1q_f64(va, va), vb);
1338 const float64x2_t p2 = vreinterpretq_f64_u64(
1339 veorq_u64(vreinterpretq_u64_f64(vmulq_f64(vtrn2q_f64(va, va), vextq_f64(vb, vb, 1))), negRe));
1340 vst1q_f64(accum + 2 * k, vaddq_f64(vld1q_f64(accum + 2 * k), vaddq_f64(p1, p2)));
1341 }
1342#endif
1343 for (; k < bins; ++k)
1344 {
1345 const double re1 = a[2 * k], im1 = a[2 * k + 1];
1346 const double re2 = b[2 * k], im2 = b[2 * k + 1];
1347 accum[2 * k] += re1 * re2 - im1 * im2;
1348 accum[2 * k + 1] += re1 * im2 + im1 * re2;
1349 }
1350}
1351
1352// ============================================================================
1353// complexExp -- z[k] = e^z[k] over interleaved complex bins
1354// ============================================================================
1355
1371inline void complexExp(double* data, int bins) noexcept
1372{
1373 if (data == nullptr || bins <= 0) return;
1374 int k = 0;
1375#if defined(DSPARK_SIMD_AVX) && defined(__AVX2__) && defined(DSPARK_SIMD_FMA)
1376 k = complexExpWide(data, bins);
1377#elif defined(DSPARK_SIMD_DISPATCH_AVX2)
1378 if (isRuntimeDispatchActive()) k = avx2::complexExpWide(data, bins);
1379#endif
1380 // One function a loop over packed values, a chunk at a time, for
1381 // compilers with vector forms of these functions.
1382 constexpr int kChunk = 64;
1383 double magnitude[kChunk], cosine[kChunk], sine[kChunk];
1384 while (k < bins)
1385 {
1386 const int n = bins - k < kChunk ? bins - k : kChunk;
1387 double* z = data + 2 * k;
1388 for (int j = 0; j < n; ++j) magnitude[j] = std::exp(z[2 * j]);
1389 for (int j = 0; j < n; ++j) cosine[j] = std::cos(z[2 * j + 1]);
1390 for (int j = 0; j < n; ++j) sine[j] = std::sin(z[2 * j + 1]);
1391 for (int j = 0; j < n; ++j)
1392 {
1393 z[2 * j] = magnitude[j] * cosine[j];
1394 z[2 * j + 1] = magnitude[j] * sine[j];
1395 }
1396 k += n;
1397 }
1398}
1399
1400// ============================================================================
1401// Template dispatchers
1402// ============================================================================
1403
1404template <typename T>
1405void addWithGainT(T* DSPARK_RESTRICT dst, const T* DSPARK_RESTRICT src, T gain, int count) noexcept
1406{
1407 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1408 addWithGain(dst, src, gain, count);
1409}
1410
1411template <typename T>
1412void applyGainT(T* DSPARK_RESTRICT data, T gain, int count) noexcept
1413{
1414 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1415 applyGain(data, gain, count);
1416}
1417
1418template <typename T>
1419T peakLevelT(const T* DSPARK_RESTRICT data, int count) noexcept
1420{
1421 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1422 return peakLevel(data, count);
1423}
1424
1425template <typename T>
1426T dotProductT(const T* DSPARK_RESTRICT a, const T* DSPARK_RESTRICT b, int count) noexcept
1427{
1428 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1429 return dotProduct(a, b, count);
1430}
1431
1432template <typename T>
1433void addT(T* DSPARK_RESTRICT dst, const T* DSPARK_RESTRICT src, int count) noexcept
1434{
1435 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1436 add(dst, src, count);
1437}
1438
1439template <typename T>
1440void multiplyT(T* DSPARK_RESTRICT dst, const T* DSPARK_RESTRICT a, const T* DSPARK_RESTRICT b, int count) noexcept
1441{
1442 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1443 multiply(dst, a, b, count);
1444}
1445
1446template <typename T>
1447void copyWithGainT(T* DSPARK_RESTRICT dst, const T* DSPARK_RESTRICT src, T gain, int count) noexcept
1448{
1449 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1450 copyWithGain(dst, src, gain, count);
1451}
1452
1453template <typename T>
1454T sumOfSquaresT(const T* DSPARK_RESTRICT data, int count) noexcept
1455{
1456 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1457 return sumOfSquares(data, count);
1458}
1459
1460template <typename T>
1461void applyGainRampT(T* DSPARK_RESTRICT data, T gainStart, T gainEnd, int count) noexcept
1462{
1463 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1464 applyGainRamp(data, gainStart, gainEnd, count);
1465}
1466
1467template <typename T>
1468void addWithGainRampT(T* DSPARK_RESTRICT dst, const T* DSPARK_RESTRICT src, T gainStart, T gainEnd, int count) noexcept
1469{
1470 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1471 addWithGainRamp(dst, src, gainStart, gainEnd, count);
1472}
1473
1474// ============================================================================
1475// firCorrelate -- block FIR on the vector abstraction (detail/SimdVec.inl)
1476// ============================================================================
1477
1495template <typename T>
1496void firCorrelate(const T* x, const T* h, int taps, T* DSPARK_RESTRICT y, int n, T gain) noexcept
1497{
1498 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1499#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1501 {
1502 avx2::firCorrelateKernel(x, h, taps, y, n, gain);
1503 return;
1504 }
1505#endif
1506 firCorrelateKernel(x, h, taps, y, n, gain);
1507}
1508
1528template <typename T, int Banks>
1529void firCorrelateBank(const T* x, const T* h, int taps, T* const* y, int n) noexcept
1530{
1531 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1532 static_assert(Banks >= 1);
1533#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1535 {
1536 avx2::firCorrelateBankKernel<T, Banks>(x, h, taps, y, n);
1537 return;
1538 }
1539#endif
1540 firCorrelateBankKernel<T, Banks>(x, h, taps, y, n);
1541}
1542
1561template <typename T, int Banks>
1562void firCorrelateBankSum(const T* m, const T* h, int taps, T* DSPARK_RESTRICT y, int n) noexcept
1563{
1564 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>, "SimdOps: only float and double are supported");
1565 static_assert(Banks >= 1);
1566#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1568 {
1569 avx2::firCorrelateBankSumKernel<T, Banks>(m, h, taps, y, n);
1570 return;
1571 }
1572#endif
1573 firCorrelateBankSumKernel<T, Banks>(m, h, taps, y, n);
1574}
1575
1576} // namespace simd
1577} // namespace dspark
1578
1579#undef DSPARK_VEC_INLINE
T peakLevelT(const T *DSPARK_RESTRICT data, int count) noexcept
Definition SimdOps.h:1419
T dotProductT(const T *DSPARK_RESTRICT a, const T *DSPARK_RESTRICT b, int count) noexcept
Definition SimdOps.h:1426
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.
Definition SimdOps.h:900
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.
Definition SimdOps.h:1234
void applyGainRamp(float *DSPARK_RESTRICT data, float gainStart, float gainEnd, int count) noexcept
In-place linear gain ramp (the canonical click-free fade primitive).
Definition SimdOps.h:1020
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.
Definition SimdOps.h:302
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)...
Definition SimdOps.h:1562
bool runtimeDispatchSupported() noexcept
Whether this build carries AVX2+FMA kernels and the CPU can run them.
Definition SimdOps.h:252
void applyGainT(T *DSPARK_RESTRICT data, T gain, int count) noexcept
Definition SimdOps.h:1412
void copyWithGainT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gain, int count) noexcept
Definition SimdOps.h:1447
void addT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, int count) noexcept
Definition SimdOps.h:1433
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.
Definition SimdOps.h:1107
void setRuntimeDispatch(bool enabled) noexcept
Selects or deselects the AVX2+FMA kernels (on by default where runtimeDispatchSupported()).
Definition SimdOps.h:280
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...
Definition SimdOps.h:1529
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],...
Definition SimdOps.h:773
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 ...
Definition SimdOps.h:1371
void addWithGainRampT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gainStart, T gainEnd, int count) noexcept
Definition SimdOps.h:1468
T sumOfSquaresT(const T *DSPARK_RESTRICT data, int count) noexcept
Definition SimdOps.h:1454
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...
Definition SimdOps.h:809
bool isRuntimeDispatchActive() noexcept
Whether the AVX2+FMA kernels are selected now (see setRuntimeDispatch()).
Definition SimdOps.h:262
float dotProduct(const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int count) noexcept
Computes the dot product of two arrays.
Definition SimdOps.h:644
void applyGainRampT(T *DSPARK_RESTRICT data, T gainStart, T gainEnd, int count) noexcept
Definition SimdOps.h:1461
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,...
Definition SimdOps.h:1496
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).
Definition SimdOps.h:951
void add(float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, int count) noexcept
Adds source samples into a destination buffer (no scaling).
Definition SimdOps.h:829
void multiplyT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT a, const T *DSPARK_RESTRICT b, int count) noexcept
Definition SimdOps.h:1440
void addWithGainT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gain, int count) noexcept
Definition SimdOps.h:1405
float sumOfSquares(const float *DSPARK_RESTRICT data, int count) noexcept
Returns the sum of squared samples (energy).
Definition SimdOps.h:1213
float peakLevel(const float *DSPARK_RESTRICT data, int count) noexcept
Returns the peak absolute sample value in a buffer.
Definition SimdOps.h:489
void applyGain(float *DSPARK_RESTRICT data, float gain, int count) noexcept
Multiplies all samples in a buffer by a gain factor.
Definition SimdOps.h:411
Main namespace for the DSPark framework.