forked from timblechmann/nova-simd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simd_peakmeter.hpp
157 lines (126 loc) · 4.08 KB
/
simd_peakmeter.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// simd functions for peak metering
// Copyright (C) 2010 Tim Blechmann
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#ifndef SIMD_PEAKMETER_HPP
#define SIMD_PEAKMETER_HPP
#include "vec.hpp"
#include <cmath> /* for abs */
#include <algorithm> /* for max */
#if defined(__GNUC__) && defined(NDEBUG)
#define always_inline inline __attribute__((always_inline))
#else
#define always_inline inline
#endif
namespace nova
{
/* updates peak, returns last abs(in[n-1]) */
template <typename F>
inline F peak_vec(const F * in, F * peak, unsigned int n)
{
F last;
F local_peak = *peak;
using namespace std;
do {
last = std::fabs(*in++);
local_peak = max(local_peak, last);
} while(--n);
*peak = local_peak;
return last;
}
template <typename F>
inline F peak_vec_simd(const F * in, F * peak, unsigned int n)
{
vec<F> maximum, abs3;
maximum.load_first(peak);
/* loop */
const size_t vec_size = vec<F>::size;
const size_t unroll = 4 * vec_size;
n /= unroll;
do {
vec<F> in0, in1, in2, in3;
in0.load_aligned(in);
in1.load_aligned(in+vec_size);
in2.load_aligned(in+2*vec_size);
in3.load_aligned(in+3*vec_size);
vec<F> abs0 = abs(in0);
vec<F> abs1 = abs(in1);
vec<F> abs2 = abs(in2);
abs3 = abs(in3);
vec<F> local_max = max_(max_(abs0, abs1),
max_(abs2, abs3));
maximum = max_(maximum, local_max);
in += unroll;
} while(--n);
/* horizonal accumulation */
*peak = maximum.horizontal_max();
/* return absolute of last sample */
return abs3.get(vec<F>::size - 1);
}
/* updates peak and squared sum */
template <typename F>
inline void peak_rms_vec(const F * in, F * peak, F * squared_sum, unsigned int n)
{
F local_peak = *peak;
F local_squared_sum = *squared_sum;
using namespace std;
do {
F in_sample = *in++;
F last = std::fabs(in_sample);
local_peak = max(local_peak, last);
local_squared_sum += in_sample * in_sample;
} while(--n);
*peak = local_peak;
*squared_sum = local_squared_sum;
}
template <typename F>
inline void peak_rms_vec_simd(const F * in, F * peak, F * squared_sum, unsigned int n)
{
vec<F> maximum;
maximum.load_first(peak);
vec<F> local_squared_sum;
local_squared_sum.load_first(squared_sum);
/* loop */
const size_t vec_size = vec<F>::size;
const size_t unroll = 4 * vec_size;
n /= unroll;
do {
vec<F> in0, in1, in2, in3;
in0.load_aligned(in);
in1.load_aligned(in+vec_size);
in2.load_aligned(in+2*vec_size);
in3.load_aligned(in+3*vec_size);
vec<F> abs0 = abs(in0);
vec<F> sqr0 = square(in0);
vec<F> abs1 = abs(in1);
vec<F> sqr1 = square(in1);
vec<F> abs2 = abs(in2);
vec<F> sqr2 = square(in2);
vec<F> abs3 = abs(in3);
vec<F> sqr3 = square(in3);
vec<F> local_max = max_(max_(abs0, abs1),
max_(abs2, abs3));
maximum = max_(maximum, local_max);
local_squared_sum += sqr0 + sqr1 + sqr2 + sqr3;
in += unroll;
} while(--n);
/* horizonal accumulation */
*peak = maximum.horizontal_max();
*squared_sum = local_squared_sum.horizontal_sum();
}
} /* namespace nova */
#undef always_inline
#endif /* SIMD_PEAKMETER_HPP */