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>
32 #define DSPARK_SIMD_AVX 1
33 #include <immintrin.h>
41 #if defined(__FMA__) || (defined(__AVX2__) && defined(_MSC_VER) && !defined(__clang__))
42 #define DSPARK_SIMD_FMA 1
44#elif defined(__aarch64__) || defined(_M_ARM64)
45 #define DSPARK_SIMD_NEON 1
47#elif defined(__EMSCRIPTEN__) && defined(__wasm_simd128__) && defined(__SSE2__)
50 #define DSPARK_SIMD_SSE2 1
51 #include <emmintrin.h>
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>
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")
79 #define DSPARK_SIMD_AVX2_BEGIN
80 #define DSPARK_SIMD_AVX2_END
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")
108 #define DSPARK_SIMD_EXACT_AVX2_BEGIN
109 #define DSPARK_SIMD_EXACT_AVX2_END
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__
126 #define DSPARK_RESTRICT
134#include <type_traits>
142#if defined(DSPARK_SIMD_SSE2)
143 #define DSPARK_VEC_SSE2 1
145#if defined(DSPARK_SIMD_NEON)
146 #define DSPARK_VEC_NEON 1
148#if defined(DSPARK_SIMD_AVX)
149 #define DSPARK_VEC_AVX 1
150 #if defined(__AVX2__)
151 #define DSPARK_VEC_AVX2 1
154#if defined(DSPARK_SIMD_FMA)
155 #define DSPARK_VEC_FMA 1
160 #define DSPARK_VEC_INLINE __forceinline
161#elif defined(__GNUC__) || defined(__clang__)
162 #define DSPARK_VEC_INLINE inline __attribute__((always_inline))
164 #define DSPARK_VEC_INLINE inline
168#include "detail/SimdVec.inl"
171#undef DSPARK_VEC_SSE2
172#undef DSPARK_VEC_NEON
174#undef DSPARK_VEC_AVX2
177#if defined(DSPARK_SIMD_DISPATCH_AVX2)
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
187#include "detail/SimdVec.inl"
191#undef DSPARK_VEC_SSE2
193#undef DSPARK_VEC_AVX2
202inline bool cpuSupportsAvx2Fma() noexcept
207 if (r[0] < 7)
return false;
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;
215 return (
static_cast<unsigned>(r[1]) & (1u << 5)) != 0;
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;
225 __asm__
volatile(
".byte 0x0f, 0x01, 0xd0" :
"=a"(xcr0),
"=d"(xcr0High) :
"c"(0));
227 if ((xcr0 & 6u) != 6u)
return false;
228 __cpuid_count(7, 0, a, b, c, d);
229 return (b & (1u << 5)) != 0;
235inline std::atomic<bool> avx2Active { cpuSupportsAvx2Fma() };
254#if defined(DSPARK_SIMD_DISPATCH_AVX2)
255 return dispatch::cpuSupportsAvx2Fma();
264#if defined(DSPARK_SIMD_DISPATCH_AVX2)
265 return dispatch::avx2Active.load(std::memory_order_relaxed);
282#if defined(DSPARK_SIMD_DISPATCH_AVX2)
283 dispatch::avx2Active.store(enabled && dispatch::cpuSupportsAvx2Fma(), std::memory_order_relaxed);
302inline void addWithGain(
float* DSPARK_RESTRICT dst,
const float* DSPARK_RESTRICT src,
float gain,
int count)
noexcept
304#if defined(DSPARK_SIMD_AVX)
305 const __m256 vGain = _mm256_set1_ps(gain);
307 for (; i + 7 < count; i += 8)
309 __m256 vDst = _mm256_loadu_ps(dst + i);
310 __m256 vSrc = _mm256_loadu_ps(src + i);
312 #if defined(DSPARK_SIMD_FMA)
313 _mm256_storeu_ps(dst + i, _mm256_fmadd_ps(vSrc, vGain, vDst));
315 _mm256_storeu_ps(dst + i, _mm256_add_ps(vDst, _mm256_mul_ps(vSrc, vGain)));
318 for (; i < count; ++i) dst[i] += src[i] * gain;
320#elif defined(DSPARK_SIMD_SSE2)
321 const __m128 vGain = _mm_set1_ps(gain);
323 for (; i + 3 < count; i += 4)
325 __m128 vDst = _mm_loadu_ps(dst + i);
326 __m128 vSrc = _mm_loadu_ps(src + i);
328 #if defined(DSPARK_SIMD_FMA)
329 _mm_storeu_ps(dst + i, _mm_fmadd_ps(vSrc, vGain, vDst));
331 _mm_storeu_ps(dst + i, _mm_add_ps(vDst, _mm_mul_ps(vSrc, vGain)));
334 for (; i < count; ++i) dst[i] += src[i] * gain;
336#elif defined(DSPARK_SIMD_NEON)
337 const float32x4_t vGain = vdupq_n_f32(gain);
339 for (; i + 3 < count; i += 4)
341 float32x4_t vDst = vld1q_f32(dst + i);
342 float32x4_t vSrc = vld1q_f32(src + i);
345 vst1q_f32(dst + i, vfmaq_f32(vDst, vSrc, vGain));
347 for (; i < count; ++i) dst[i] += src[i] * gain;
350 for (
int i = 0; i < count; ++i) dst[i] += src[i] * gain;
355inline void addWithGain(
double* DSPARK_RESTRICT dst,
const double* DSPARK_RESTRICT src,
double gain,
int count)
noexcept
357#if defined(DSPARK_SIMD_AVX)
358 const __m256d vGain = _mm256_set1_pd(gain);
360 for (; i + 3 < count; i += 4)
362 __m256d vDst = _mm256_loadu_pd(dst + i);
363 __m256d vSrc = _mm256_loadu_pd(src + i);
365 #if defined(DSPARK_SIMD_FMA)
366 _mm256_storeu_pd(dst + i, _mm256_fmadd_pd(vSrc, vGain, vDst));
368 _mm256_storeu_pd(dst + i, _mm256_add_pd(vDst, _mm256_mul_pd(vSrc, vGain)));
371 for (; i < count; ++i) dst[i] += src[i] * gain;
373#elif defined(DSPARK_SIMD_SSE2)
374 const __m128d vGain = _mm_set1_pd(gain);
376 for (; i + 1 < count; i += 2)
378 __m128d vDst = _mm_loadu_pd(dst + i);
379 __m128d vSrc = _mm_loadu_pd(src + i);
381 #if defined(DSPARK_SIMD_FMA)
382 _mm_storeu_pd(dst + i, _mm_fmadd_pd(vSrc, vGain, vDst));
384 _mm_storeu_pd(dst + i, _mm_add_pd(vDst, _mm_mul_pd(vSrc, vGain)));
387 for (; i < count; ++i) dst[i] += src[i] * gain;
389#elif defined(DSPARK_SIMD_NEON)
390 const float64x2_t vGain = vdupq_n_f64(gain);
392 for (; i + 1 < count; i += 2)
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));
398 for (; i < count; ++i) dst[i] += src[i] * gain;
400 for (
int i = 0; i < count; ++i) dst[i] += src[i] * gain;
411inline void applyGain(
float* DSPARK_RESTRICT data,
float gain,
int count)
noexcept
413#if defined(DSPARK_SIMD_AVX)
414 const __m256 vGain = _mm256_set1_ps(gain);
416 for (; i + 7 < count; i += 8)
418 __m256 v = _mm256_loadu_ps(data + i);
419 _mm256_storeu_ps(data + i, _mm256_mul_ps(v, vGain));
421 for (; i < count; ++i) data[i] *= gain;
423#elif defined(DSPARK_SIMD_SSE2)
424 const __m128 vGain = _mm_set1_ps(gain);
426 for (; i + 3 < count; i += 4)
428 __m128 v = _mm_loadu_ps(data + i);
429 _mm_storeu_ps(data + i, _mm_mul_ps(v, vGain));
431 for (; i < count; ++i) data[i] *= gain;
433#elif defined(DSPARK_SIMD_NEON)
434 const float32x4_t vGain = vdupq_n_f32(gain);
436 for (; i + 3 < count; i += 4)
438 float32x4_t v = vld1q_f32(data + i);
439 vst1q_f32(data + i, vmulq_f32(v, vGain));
441 for (; i < count; ++i) data[i] *= gain;
444 for (
int i = 0; i < count; ++i) data[i] *= gain;
449inline void applyGain(
double* DSPARK_RESTRICT data,
double gain,
int count)
noexcept
451#if defined(DSPARK_SIMD_AVX)
452 const __m256d vGain = _mm256_set1_pd(gain);
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;
458#elif defined(DSPARK_SIMD_SSE2)
459 const __m128d vGain = _mm_set1_pd(gain);
461 for (; i + 1 < count; i += 2)
463 __m128d v = _mm_loadu_pd(data + i);
464 _mm_storeu_pd(data + i, _mm_mul_pd(v, vGain));
466 for (; i < count; ++i) data[i] *= gain;
468#elif defined(DSPARK_SIMD_NEON)
469 const float64x2_t vGain = vdupq_n_f64(gain);
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;
475 for (
int i = 0; i < count; ++i) data[i] *= gain;
489inline float peakLevel(
const float* DSPARK_RESTRICT data,
int count)
noexcept
491#if defined(DSPARK_SIMD_AVX)
492 const __m256 absMask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7FFFFFFF));
493 __m256 vMax = _mm256_setzero_ps();
495 for (; i + 7 < count; i += 8)
497 __m256 v = _mm256_loadu_ps(data + i);
498 v = _mm256_and_ps(v, absMask);
502 vMax = _mm256_max_ps(v, vMax);
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);
511 for (; i < count; ++i)
513 float a = data[i] < 0.0f ? -data[i] : data[i];
514 if (a > peak) peak = a;
518#elif defined(DSPARK_SIMD_SSE2)
519 const __m128 absMask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
520 __m128 vMax = _mm_setzero_ps();
522 for (; i + 3 < count; i += 4)
524 __m128 v = _mm_loadu_ps(data + i);
525 v = _mm_and_ps(v, absMask);
526 vMax = _mm_max_ps(v, vMax);
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);
533 for (; i < count; ++i)
535 float a = data[i] < 0.0f ? -data[i] : data[i];
536 if (a > peak) peak = a;
540#elif defined(DSPARK_SIMD_NEON)
541 float32x4_t vMax = vdupq_n_f32(0.0f);
543 for (; i + 3 < count; i += 4)
545 float32x4_t v = vld1q_f32(data + i);
547 vMax = vmaxnmq_f32(vMax, v);
549 float peak = vmaxvq_f32(vMax);
551 for (; i < count; ++i)
553 float a = data[i] < 0.0f ? -data[i] : data[i];
554 if (a > peak) peak = a;
560 for (
int i = 0; i < count; ++i)
562 float a = data[i] < 0.0f ? -data[i] : data[i];
563 if (a > peak) peak = a;
570inline double peakLevel(
const double* DSPARK_RESTRICT data,
int count)
noexcept
572#if defined(DSPARK_SIMD_AVX)
573 const __m256d absMask = _mm256_castsi256_pd(_mm256_set1_epi64x(0x7FFFFFFFFFFFFFFFll));
574 __m256d vMax = _mm256_setzero_pd();
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)));
583 for (; i < count; ++i)
585 double a = data[i] < 0.0 ? -data[i] : data[i];
586 if (a > peak) peak = a;
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();
596 for (; i + 1 < count; i += 2)
598 __m128d v = _mm_loadu_pd(data + i);
599 v = _mm_and_pd(v, absMask);
600 vMax = _mm_max_pd(v, vMax);
602 __m128d hi = _mm_unpackhi_pd(vMax, vMax);
603 __m128d maxVal = _mm_max_sd(vMax, hi);
604 double peak = _mm_cvtsd_f64(maxVal);
606 for (; i < count; ++i)
608 double a = data[i] < 0.0 ? -data[i] : data[i];
609 if (a > peak) peak = a;
613#elif defined(DSPARK_SIMD_NEON)
614 float64x2_t vMax = vdupq_n_f64(0.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);
620 for (; i < count; ++i)
622 double a = data[i] < 0.0 ? -data[i] : data[i];
623 if (a > peak) peak = a;
628 for (
int i = 0; i < count; ++i)
630 double a = data[i] < 0.0 ? -data[i] : data[i];
631 if (a > peak) peak = a;
644inline float dotProduct(
const float* DSPARK_RESTRICT a,
const float* DSPARK_RESTRICT b,
int count)
noexcept
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)
653 __m128 vSum0 = _mm_setzero_ps();
654 __m128 vSum1 = _mm_setzero_ps();
656 for (; i + 7 < count; i += 8)
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);
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)));
666 for (; i + 3 < count; i += 4)
668 #if defined(DSPARK_SIMD_FMA)
669 vSum0 = _mm_fmadd_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i), vSum0);
671 vSum0 = _mm_add_ps(vSum0, _mm_mul_ps(_mm_loadu_ps(a + i), _mm_loadu_ps(b + i)));
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];
678 for (; i < count; ++i) sum += a[i] * b[i];
681#elif defined(DSPARK_SIMD_NEON)
682 float32x4_t vSum0 = vdupq_n_f32(0.0f);
683 float32x4_t vSum1 = vdupq_n_f32(0.0f);
685 for (; i + 7 < count; i += 8)
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));
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));
694 for (; i < count; ++i) sum += a[i] * b[i];
699 for (
int i = 0; i < count; ++i) sum += a[i] * b[i];
705inline double dotProduct(
const double* DSPARK_RESTRICT a,
const double* DSPARK_RESTRICT b,
int count)
noexcept
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)
713 __m128d vSum0 = _mm_setzero_pd();
714 __m128d vSum1 = _mm_setzero_pd();
716 for (; i + 3 < count; i += 4)
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);
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)));
726 for (; i + 1 < count; i += 2)
728 #if defined(DSPARK_SIMD_FMA)
729 vSum0 = _mm_fmadd_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i), vSum0);
731 vSum0 = _mm_add_pd(vSum0, _mm_mul_pd(_mm_loadu_pd(a + i), _mm_loadu_pd(b + i)));
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));
738 for (; i < count; ++i) sum += a[i] * b[i];
741#elif defined(DSPARK_SIMD_NEON)
742 float64x2_t vSum0 = vdupq_n_f64(0.0);
743 float64x2_t vSum1 = vdupq_n_f64(0.0);
745 for (; i + 3 < count; i += 4)
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));
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));
754 for (; i < count; ++i) sum += a[i] * b[i];
758 for (
int i = 0; i < count; ++i) sum += a[i] * b[i];
773inline void dotProduct4(
const double* shared,
const double*
const* other,
int count,
double* out)
noexcept
775#if defined(DSPARK_SIMD_AVX)
776 dotProduct4Wide(shared, other, count, out);
778 #if defined(DSPARK_SIMD_DISPATCH_AVX2)
781 avx2::dotProduct4Wide(shared, other, count, out);
785 for (
int x = 0; x < 4; ++x) out[x] =
dotProduct(shared, other[x], count);
809inline void dotProductStrided(
const double* shared,
const double* first, std::ptrdiff_t stride,
int count,
810 int outputs,
double* out)
noexcept
812#if defined(DSPARK_SIMD_DISPATCH_AVX2)
815 avx2::dotProductStridedKernel(shared, first, stride, count, outputs, out);
819 dotProductStridedKernel(shared, first, stride, count, outputs, out);
829inline void add(
float* DSPARK_RESTRICT dst,
const float* DSPARK_RESTRICT src,
int count)
noexcept
831#if defined(DSPARK_SIMD_AVX)
833 for (; i + 7 < count; i += 8)
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));
839 for (; i < count; ++i) dst[i] += src[i];
841#elif defined(DSPARK_SIMD_SSE2)
843 for (; i + 3 < count; i += 4)
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));
849 for (; i < count; ++i) dst[i] += src[i];
851#elif defined(DSPARK_SIMD_NEON)
853 for (; i + 3 < count; i += 4)
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));
859 for (; i < count; ++i) dst[i] += src[i];
862 for (
int i = 0; i < count; ++i) dst[i] += src[i];
867inline void add(
double* DSPARK_RESTRICT dst,
const double* DSPARK_RESTRICT src,
int count)
noexcept
869#if defined(DSPARK_SIMD_AVX)
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];
875#elif defined(DSPARK_SIMD_SSE2)
877 for (; i + 1 < count; i += 2)
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));
883 for (; i < count; ++i) dst[i] += src[i];
885#elif defined(DSPARK_SIMD_NEON)
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];
891 for (
int i = 0; i < count; ++i) dst[i] += src[i];
900inline void multiply(
float* DSPARK_RESTRICT dst,
const float* DSPARK_RESTRICT a,
const float* DSPARK_RESTRICT b,
int count)
noexcept
902#if defined(DSPARK_SIMD_AVX)
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)
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)
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];
918 for (
int i = 0; i < count; ++i) dst[i] = a[i] * b[i];
923inline void multiply(
double* DSPARK_RESTRICT dst,
const double* DSPARK_RESTRICT a,
const double* DSPARK_RESTRICT b,
int count)
noexcept
925#if defined(DSPARK_SIMD_AVX)
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)
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];
936#elif defined(DSPARK_SIMD_NEON)
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];
942 for (
int i = 0; i < count; ++i) dst[i] = a[i] * b[i];
951inline void copyWithGain(
float* DSPARK_RESTRICT dst,
const float* DSPARK_RESTRICT src,
float gain,
int count)
noexcept
953#if defined(DSPARK_SIMD_AVX)
954 const __m256 vGain = _mm256_set1_ps(gain);
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);
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);
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;
972 for (
int i = 0; i < count; ++i) dst[i] = src[i] * gain;
977inline void copyWithGain(
double* DSPARK_RESTRICT dst,
const double* DSPARK_RESTRICT src,
double gain,
int count)
noexcept
979#if defined(DSPARK_SIMD_AVX)
980 const __m256d vGain = _mm256_set1_pd(gain);
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);
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;
992#elif defined(DSPARK_SIMD_NEON)
993 const float64x2_t vGain = vdupq_n_f64(gain);
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;
999 for (
int i = 0; i < count; ++i) dst[i] = src[i] * gain;
1020inline void applyGainRamp(
float* DSPARK_RESTRICT data,
float gainStart,
float gainEnd,
int count)
noexcept
1022 if (count <= 0)
return;
1023 const float step = (gainEnd - gainStart) /
static_cast<float>(count);
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)
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);
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)
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);
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)
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);
1054 for (; i < count; ++i) data[i] *= gainStart + step * static_cast<float>(i);
1058inline void applyGainRamp(
double* DSPARK_RESTRICT data,
double gainStart,
double gainEnd,
int count)
noexcept
1060 if (count <= 0)
return;
1061 const double step = (gainEnd - gainStart) /
static_cast<double>(count);
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)
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);
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)
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);
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)
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);
1092 for (; i < count; ++i) data[i] *= gainStart + step * static_cast<double>(i);
1108 float gainStart,
float gainEnd,
int count)
noexcept
1110 if (count <= 0)
return;
1111 const float step = (gainEnd - gainStart) /
static_cast<float>(count);
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)
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));
1124 _mm256_storeu_ps(dst + i, _mm256_add_ps(vDst, _mm256_mul_ps(vSrc, vGain)));
1126 vIdx = _mm256_add_ps(vIdx, vInc);
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)
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));
1139 _mm_storeu_ps(dst + i, _mm_add_ps(vDst, _mm_mul_ps(vSrc, vGain)));
1141 vIdx = _mm_add_ps(vIdx, vInc);
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)
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);
1154 for (; i < count; ++i) dst[i] += src[i] * (gainStart + step * static_cast<float>(i));
1158inline void addWithGainRamp(
double* DSPARK_RESTRICT dst,
const double* DSPARK_RESTRICT src,
1159 double gainStart,
double gainEnd,
int count)
noexcept
1161 if (count <= 0)
return;
1162 const double step = (gainEnd - gainStart) /
static_cast<double>(count);
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)
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));
1175 _mm256_storeu_pd(dst + i, _mm256_add_pd(vDst, _mm256_mul_pd(vSrc, vGain)));
1177 vIdx = _mm256_add_pd(vIdx, vInc);
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)
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));
1190 _mm_storeu_pd(dst + i, _mm_add_pd(vDst, _mm_mul_pd(vSrc, vGain)));
1192 vIdx = _mm_add_pd(vIdx, vInc);
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)
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);
1205 for (; i < count; ++i) dst[i] += src[i] * (gainStart + step * static_cast<double>(i));
1213inline float sumOfSquares(
const float* DSPARK_RESTRICT data,
int count)
noexcept
1219inline double sumOfSquares(
const double* DSPARK_RESTRICT data,
int count)
noexcept
1235 const float* DSPARK_RESTRICT b,
int bins)
noexcept
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)
1244 const __m128 negMask = _mm_castsi128_ps(_mm_setr_epi32(
1245 static_cast<int>(0x80000000u), 0,
1246 static_cast<int>(0x80000000u), 0));
1249 for (; k + 1 < bins; k += 2)
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);
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));
1259 const __m128 p1 = _mm_mul_ps(aRe, vb);
1260 const __m128 p2 = _mm_xor_ps(_mm_mul_ps(aIm, bSwap), negMask);
1262 vacc = _mm_add_ps(vacc, _mm_add_ps(p1, p2));
1263 _mm_storeu_ps(accum + 2 * k, vacc);
1265 for (; k < bins; ++k)
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;
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);
1277 for (; k + 1 < bins; k += 2)
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);
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);
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));
1291 vacc = vaddq_f32(vacc, vaddq_f32(p1, p2));
1292 vst1q_f32(accum + 2 * k, vacc);
1294 for (; k < bins; ++k)
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;
1302 for (
int k = 0; k < bins; ++k)
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;
1313inline void complexMulAccum(
double* DSPARK_RESTRICT accum,
const double* DSPARK_RESTRICT a,
1314 const double* DSPARK_RESTRICT b,
int bins)
noexcept
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)
1323 const __m128d negRe = _mm_set_pd(0.0, -0.0);
1324 for (; k < bins; ++k)
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)));
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)
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)));
1343 for (; k < bins; ++k)
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;
1373 if (data ==
nullptr || bins <= 0)
return;
1375#if defined(DSPARK_SIMD_AVX) && defined(__AVX2__) && defined(DSPARK_SIMD_FMA)
1376 k = complexExpWide(data, bins);
1377#elif defined(DSPARK_SIMD_DISPATCH_AVX2)
1382 constexpr int kChunk = 64;
1383 double magnitude[kChunk], cosine[kChunk], sine[kChunk];
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)
1393 z[2 * j] = magnitude[j] * cosine[j];
1394 z[2 * j + 1] = magnitude[j] * sine[j];
1404template <
typename T>
1405void addWithGainT(T* DSPARK_RESTRICT dst,
const T* DSPARK_RESTRICT src, T gain,
int count)
noexcept
1407 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1411template <
typename T>
1412void applyGainT(T* DSPARK_RESTRICT data, T gain,
int count)
noexcept
1414 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1418template <
typename T>
1421 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1425template <
typename T>
1426T
dotProductT(
const T* DSPARK_RESTRICT a,
const T* DSPARK_RESTRICT b,
int count)
noexcept
1428 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1432template <
typename T>
1433void addT(T* DSPARK_RESTRICT dst,
const T* DSPARK_RESTRICT src,
int count)
noexcept
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);
1439template <
typename T>
1440void multiplyT(T* DSPARK_RESTRICT dst,
const T* DSPARK_RESTRICT a,
const T* DSPARK_RESTRICT b,
int count)
noexcept
1442 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1446template <
typename T>
1447void copyWithGainT(T* DSPARK_RESTRICT dst,
const T* DSPARK_RESTRICT src, T gain,
int count)
noexcept
1449 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1453template <
typename T>
1456 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1460template <
typename T>
1461void applyGainRampT(T* DSPARK_RESTRICT data, T gainStart, T gainEnd,
int count)
noexcept
1463 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1467template <
typename T>
1468void addWithGainRampT(T* DSPARK_RESTRICT dst,
const T* DSPARK_RESTRICT src, T gainStart, T gainEnd,
int count)
noexcept
1470 static_assert(std::is_same_v<T, float> || std::is_same_v<T, double>,
"SimdOps: only float and double are supported");
1495template <
typename T>
1496void firCorrelate(
const T* x,
const T* h,
int taps, T* DSPARK_RESTRICT y,
int n, T gain)
noexcept
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)
1502 avx2::firCorrelateKernel(x, h, taps, y, n, gain);
1506 firCorrelateKernel(x, h, taps, y, n, gain);
1528template <
typename T,
int Banks>
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)
1536 avx2::firCorrelateBankKernel<T, Banks>(x, h, taps, y, n);
1540 firCorrelateBankKernel<T, Banks>(x, h, taps, y, n);
1561template <
typename T,
int Banks>
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)
1569 avx2::firCorrelateBankSumKernel<T, Banks>(m, h, taps, y, n);
1573 firCorrelateBankSumKernel<T, Banks>(m, h, taps, y, n);
1579#undef DSPARK_VEC_INLINE
T peakLevelT(const T *DSPARK_RESTRICT data, int count) noexcept
T dotProductT(const T *DSPARK_RESTRICT a, const T *DSPARK_RESTRICT b, int count) noexcept
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 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 applyGainRamp(float *DSPARK_RESTRICT data, float gainStart, float gainEnd, int count) noexcept
In-place linear gain ramp (the canonical click-free fade primitive).
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 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)...
bool runtimeDispatchSupported() noexcept
Whether this build carries AVX2+FMA kernels and the CPU can run them.
void applyGainT(T *DSPARK_RESTRICT data, T gain, int count) noexcept
void copyWithGainT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gain, int count) noexcept
void addT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, int count) noexcept
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 setRuntimeDispatch(bool enabled) noexcept
Selects or deselects the AVX2+FMA kernels (on by default where runtimeDispatchSupported()).
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...
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],...
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 ...
void addWithGainRampT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gainStart, T gainEnd, int count) noexcept
T sumOfSquaresT(const T *DSPARK_RESTRICT data, int count) noexcept
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...
bool isRuntimeDispatchActive() noexcept
Whether the AVX2+FMA kernels are selected now (see setRuntimeDispatch()).
float dotProduct(const float *DSPARK_RESTRICT a, const float *DSPARK_RESTRICT b, int count) noexcept
Computes the dot product of two arrays.
void applyGainRampT(T *DSPARK_RESTRICT data, T gainStart, T gainEnd, int count) noexcept
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,...
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 add(float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, int count) noexcept
Adds source samples into a destination buffer (no scaling).
void multiplyT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT a, const T *DSPARK_RESTRICT b, int count) noexcept
void addWithGainT(T *DSPARK_RESTRICT dst, const T *DSPARK_RESTRICT src, T gain, int count) noexcept
float sumOfSquares(const float *DSPARK_RESTRICT data, int count) noexcept
Returns the sum of squared samples (energy).
float peakLevel(const float *DSPARK_RESTRICT data, int count) noexcept
Returns the peak absolute sample value in a buffer.
void applyGain(float *DSPARK_RESTRICT data, float gain, int count) noexcept
Multiplies all samples in a buffer by a gain factor.
Main namespace for the DSPark framework.