Krotos Modules 3
Loading...
Searching...
No Matches
Utilities.cpp
Go to the documentation of this file.
1// ==================================================================================
2// Copyright (c) 2017 HiFi-LoFi
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is furnished
9// to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20// ==================================================================================
21
22#include "Utilities.h"
23
24namespace fftconvolver
25{
26
28 {
29#if defined(FFTCONVOLVER_USE_SSE)
30 return true;
31#else
32 return false;
33#endif
34 }
35
37 const Sample* FFTCONVOLVER_RESTRICT b, size_t len)
38 {
39 const size_t end4 = 4 * (len / 4);
40 for (size_t i = 0; i < end4; i += 4)
41 {
42 result[i + 0] = a[i + 0] + b[i + 0];
43 result[i + 1] = a[i + 1] + b[i + 1];
44 result[i + 2] = a[i + 2] + b[i + 2];
45 result[i + 3] = a[i + 3] + b[i + 3];
46 }
47 for (size_t i = end4; i < len; ++i)
48 {
49 result[i] = a[i] + b[i];
50 }
51 }
52
54 {
55 assert(result.size() == a.size());
56 assert(result.size() == b.size());
57 ComplexMultiplyAccumulate(result.re(), result.im(), a.re(), a.im(), b.re(), b.im(), result.size());
58 }
59
63 const size_t len)
64 {
65#if defined(FFTCONVOLVER_USE_SSE)
66 const size_t end4 = 4 * (len / 4);
67 for (size_t i = 0; i < end4; i += 4)
68 {
69 const __m128 ra = _mm_load_ps(&reA[i]);
70 const __m128 rb = _mm_load_ps(&reB[i]);
71 const __m128 ia = _mm_load_ps(&imA[i]);
72 const __m128 ib = _mm_load_ps(&imB[i]);
73 __m128 real = _mm_load_ps(&re[i]);
74 __m128 imag = _mm_load_ps(&im[i]);
75 real = _mm_add_ps(real, _mm_mul_ps(ra, rb));
76 real = _mm_sub_ps(real, _mm_mul_ps(ia, ib));
77 _mm_store_ps(&re[i], real);
78 imag = _mm_add_ps(imag, _mm_mul_ps(ra, ib));
79 imag = _mm_add_ps(imag, _mm_mul_ps(ia, rb));
80 _mm_store_ps(&im[i], imag);
81 }
82 for (size_t i = end4; i < len; ++i)
83 {
84 re[i] += reA[i] * reB[i] - imA[i] * imB[i];
85 im[i] += reA[i] * imB[i] + imA[i] * reB[i];
86 }
87#else
88 const size_t end4 = 4 * (len / 4);
89 for (size_t i = 0; i < end4; i += 4)
90 {
91 re[i + 0] += reA[i + 0] * reB[i + 0] - imA[i + 0] * imB[i + 0];
92 re[i + 1] += reA[i + 1] * reB[i + 1] - imA[i + 1] * imB[i + 1];
93 re[i + 2] += reA[i + 2] * reB[i + 2] - imA[i + 2] * imB[i + 2];
94 re[i + 3] += reA[i + 3] * reB[i + 3] - imA[i + 3] * imB[i + 3];
95 im[i + 0] += reA[i + 0] * imB[i + 0] + imA[i + 0] * reB[i + 0];
96 im[i + 1] += reA[i + 1] * imB[i + 1] + imA[i + 1] * reB[i + 1];
97 im[i + 2] += reA[i + 2] * imB[i + 2] + imA[i + 2] * reB[i + 2];
98 im[i + 3] += reA[i + 3] * imB[i + 3] + imA[i + 3] * reB[i + 3];
99 }
100 for (size_t i = end4; i < len; ++i)
101 {
102 re[i] += reA[i] * reB[i] - imA[i] * imB[i];
103 im[i] += reA[i] * imB[i] + imA[i] * reB[i];
104 }
105#endif
106 }
107
108} // End of namespace fftconvolver
Buffer for split-complex representation of FFT results.
Definition Utilities.h:173
size_t size() const
Definition Utilities.h:213
Sample * im()
Definition Utilities.h:209
Sample * re()
Definition Utilities.h:205
Definition FFTConvolver.cpp:29
bool SSEEnabled()
Returns whether SSE optimization for the convolver is enabled.
Definition Utilities.cpp:27
float Sample
Type of one sample.
Definition Utilities.h:157
void ComplexMultiplyAccumulate(SplitComplex &result, const SplitComplex &a, const SplitComplex &b)
Adds the complex product of two split-complex buffers to a result buffer.
Definition Utilities.cpp:53
void Sum(Sample *FFTCONVOLVER_RESTRICT result, const Sample *FFTCONVOLVER_RESTRICT a, const Sample *FFTCONVOLVER_RESTRICT b, size_t len)
Sums two given sample arrays.
Definition Utilities.cpp:36
#define FFTCONVOLVER_RESTRICT
Definition Utilities.h:47