| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (C) 2003-2013 Fons Adriaensen <[email protected]> |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License as published by |
| 7 |
// the Free Software Foundation; either version 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// ---------------------------------------------------------------------------- |
| 19 |
|
| 20 |
|
| 21 |
#define _XOPEN_SOURCE 600 // for posix_memalign() |
| 22 |
|
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include "convolve.h" |
| 28 |
|
| 29 |
|
| 30 |
#ifdef USE_SSE_AND_3DN |
| 31 |
extern "C" void mac_sse (fftwf_complex *A, fftwf_complex *B, fftwf_complex *S, int N); |
| 32 |
extern "C" void mac_3dn (fftwf_complex *A, fftwf_complex *B, fftwf_complex *S, int N); |
| 33 |
#else |
| 34 |
extern "C" void mac_sse (fftwf_complex *A, fftwf_complex *B, fftwf_complex *S, int N) {} |
| 35 |
extern "C" void mac_3dn (fftwf_complex *A, fftwf_complex *B, fftwf_complex *S, int N) {} |
| 36 |
#endif |
| 37 |
|
| 38 |
|
| 39 |
Convdata::Convdata (size_t part, size_t size, unsigned int opts) : |
| 40 |
_refc (0), _part (part), _opts (opts), _nact (0), _buff (0), _plan (0) |
| 41 |
{ |
| 42 |
unsigned int i; |
| 43 |
|
| 44 |
if ( (part < 0x000040) |
| 45 |
|| (part > 0x010000) |
| 46 |
|| (part & (part - 1)) |
| 47 |
|| (size > 0x400000) |
| 48 |
|| (size > 0x1000 * part)) abort (); |
| 49 |
|
| 50 |
#ifndef USE_SSE_AND_3DN |
| 51 |
_opts = 0; |
| 52 |
#endif |
| 53 |
|
| 54 |
_npar = (size + part - 1) / part; |
| 55 |
_norm = 0.5f / part; |
| 56 |
_fftd = new fftwf_complex * [_npar]; |
| 57 |
for (i = 0; i < _npar; i++) _fftd [i] = 0; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
Convdata::~Convdata (void) |
| 62 |
{ |
| 63 |
unsigned int i; |
| 64 |
|
| 65 |
prepare_done (); |
| 66 |
for (i = 0; i < _npar; i++) free (_fftd [i]); |
| 67 |
delete[] _fftd; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
void Convdata::prepare_part (unsigned int ipar, float gain, float *data, int step) |
| 72 |
{ |
| 73 |
unsigned int i; |
| 74 |
|
| 75 |
if (ipar >= _npar) abort (); |
| 76 |
|
| 77 |
if (data) |
| 78 |
{ |
| 79 |
gain *= _norm; |
| 80 |
if (! _plan) |
| 81 |
{ |
| 82 |
posix_memalign ((void **)(&_buff), 16, 2 * _part * sizeof (float)); |
| 83 |
memset (_buff + _part, 0, _part * sizeof (float)); |
| 84 |
_plan = fftwf_plan_dft_r2c_1d (2 * _part, _buff, _fftd [ipar], FFTW_ESTIMATE); |
| 85 |
} |
| 86 |
for (i = 0; i < _part; i++) _buff [i] = data [i * step] * gain; |
| 87 |
if (! _fftd [ipar]); |
| 88 |
{ |
| 89 |
posix_memalign ((void **)(_fftd + ipar), 16, (_part + 1) * sizeof (fftwf_complex)); |
| 90 |
_nact++; |
| 91 |
} |
| 92 |
fftwf_execute_dft_r2c (_plan, _buff, _fftd [ipar]); |
| 93 |
if (_opts) swap (_fftd [ipar], _part); |
| 94 |
} |
| 95 |
else if (_fftd [ipar]) |
| 96 |
{ |
| 97 |
free (_fftd [ipar]); |
| 98 |
_fftd [ipar] = 0; |
| 99 |
_nact--; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
void Convdata::prepare_done (void) |
| 105 |
{ |
| 106 |
if (_plan) |
| 107 |
{ |
| 108 |
fftwf_free (_buff); |
| 109 |
fftwf_destroy_plan (_plan); |
| 110 |
_buff = 0; |
| 111 |
_plan = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void Convdata::swap (fftwf_complex *p, size_t n) |
| 117 |
{ |
| 118 |
float a, b; |
| 119 |
|
| 120 |
while (n) |
| 121 |
{ |
| 122 |
a = p [2][0]; |
| 123 |
b = p [3][0]; |
| 124 |
p [2][0] = p [0][1]; |
| 125 |
p [3][0] = p [1][1]; |
| 126 |
p [0][1] = a; |
| 127 |
p [1][1] = b; |
| 128 |
p += 4; |
| 129 |
n -= 4; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
Convolver::Convolver (size_t part, size_t size, unsigned int opts, unsigned int nip, unsigned int nop) : |
| 137 |
_part (part), _opts (opts), _nip (nip), _nop (nop) |
| 138 |
{ |
| 139 |
unsigned int i; |
| 140 |
|
| 141 |
if ( (part < 0x000040) |
| 142 |
|| (part > 0x010000) |
| 143 |
|| (part & (part - 1)) |
| 144 |
|| (size > 0x400000) |
| 145 |
|| (size > 4096 * part) |
| 146 |
|| (nip > 256) |
| 147 |
|| (nop > 256) |
| 148 |
|| (nip * nop > 1024)) abort (); |
| 149 |
|
| 150 |
#ifndef USE_SSE_AND_3DN |
| 151 |
_opts = 0; |
| 152 |
#endif |
| 153 |
|
| 154 |
_npar = (size + part - 1) / part; |
| 155 |
_conv = new Convdata *[_nip * _nop]; |
| 156 |
for (i = 0; i < _nip * _nop; i++) _conv [i] = 0; |
| 157 |
posix_memalign ((void **)(&_ip_buff), 16, (_nip + 1) * _part * sizeof (float)); |
| 158 |
posix_memalign ((void **)(&_oA_buff), 16, 2 * _nop * _part * sizeof (float)); |
| 159 |
posix_memalign ((void **)(&_oB_buff), 16, 2 * _nop * _part * sizeof (float)); |
| 160 |
posix_memalign ((void **)(&_mac_data), 16, (_part + 1) * sizeof (fftwf_complex)); |
| 161 |
_fwd_data = new fftwf_complex * [_nip * _npar]; |
| 162 |
for (i = 0; i < _nip * _npar; i++) |
| 163 |
{ |
| 164 |
posix_memalign ((void **)(_fwd_data + i), 16, (_part + 1) * sizeof (fftwf_complex)); |
| 165 |
} |
| 166 |
_fwd_plan = fftwf_plan_dft_r2c_1d (2 * part, _ip_buff, _mac_data, FFTW_ESTIMATE); |
| 167 |
_rev_plan = fftwf_plan_dft_c2r_1d (2 * part, _mac_data, _oA_buff, FFTW_ESTIMATE); |
| 168 |
reset (); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
Convolver::~Convolver (void) |
| 174 |
{ |
| 175 |
unsigned int i; |
| 176 |
|
| 177 |
fftwf_destroy_plan (_fwd_plan); |
| 178 |
fftwf_destroy_plan (_rev_plan); |
| 179 |
for (i = 0; i < _nip * _npar; i++) free (_fwd_data [i]); |
| 180 |
delete[] _fwd_data; |
| 181 |
free (_ip_buff); |
| 182 |
free (_oA_buff); |
| 183 |
free (_oB_buff); |
| 184 |
free (_mac_data); |
| 185 |
for (i = 0; i < _nip * _nop; i++) |
| 186 |
{ |
| 187 |
if (_conv [i] && (_conv [i]->dec_refc () == 0)) delete _conv [i]; |
| 188 |
} |
| 189 |
delete[] _conv; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
int Convolver::set_conv (unsigned int ip, unsigned int op, Convdata *C) |
| 194 |
{ |
| 195 |
unsigned int i; |
| 196 |
|
| 197 |
if ((ip >= _nip) || (op >= _nop)) return -1; |
| 198 |
i = ip + op * _nip; |
| 199 |
|
| 200 |
if (_conv [i] && (_conv [i]->dec_refc () == 0)) |
| 201 |
{ |
| 202 |
delete _conv [i]; |
| 203 |
_conv [i] = 0; |
| 204 |
} |
| 205 |
if (C) |
| 206 |
{ |
| 207 |
if (C->_part != _part) return -1; |
| 208 |
C->inc_refc (); |
| 209 |
_conv [i] = C; |
| 210 |
} |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
void Convolver::reset () |
| 216 |
{ |
| 217 |
unsigned int i; |
| 218 |
|
| 219 |
memset (_ip_buff, 0, (_nip + 1) * _part * sizeof (float)); |
| 220 |
memset (_oA_buff, 0, 2 * _nop * _part * sizeof (float)); |
| 221 |
memset (_oB_buff, 0, 2 * _nop * _part * sizeof (float)); |
| 222 |
for (i = 0; i < _nip * _npar; i++) |
| 223 |
{ |
| 224 |
memset (_fwd_data [i], 0, (_part + 1) * sizeof (fftwf_complex)); |
| 225 |
} |
| 226 |
_iter = 0; |
| 227 |
_offs = 0; |
| 228 |
_op_buff = _oB_buff; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
void Convolver::process () |
| 233 |
{ |
| 234 |
unsigned int g, h, i, j; |
| 235 |
float *p1, *p2; |
| 236 |
fftwf_complex *q1, *q2; |
| 237 |
Convdata *C; |
| 238 |
|
| 239 |
if (_offs == 0) _offs = _npar; |
| 240 |
_offs--; |
| 241 |
|
| 242 |
p1 = _ip_buff + _nip * _part; |
| 243 |
for (i = 0; i < _nip; i++) |
| 244 |
{ |
| 245 |
memset (p1, 0, _part * sizeof (float)); |
| 246 |
p1 -= _part; |
| 247 |
q1 = _fwd_data [_offs + (_nip - 1 - i) * _npar]; |
| 248 |
fftwf_execute_dft_r2c (_fwd_plan, p1, q1); |
| 249 |
if (_opts) Convdata::swap (q1, _part); |
| 250 |
} |
| 251 |
|
| 252 |
if (++_iter & 1) |
| 253 |
{ |
| 254 |
p1 = _oA_buff; |
| 255 |
p2 = _oB_buff + _part; |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
p1 = _oB_buff; |
| 260 |
p2 = _oA_buff + _part; |
| 261 |
} |
| 262 |
_op_buff = p1; |
| 263 |
for (j = 0; j < _nop; j++) |
| 264 |
{ |
| 265 |
memset (_mac_data, 0, (_part + 1) * sizeof (fftwf_complex)); |
| 266 |
for (i = 0; i < _nip; i++) |
| 267 |
{ |
| 268 |
C = _conv [i + j * _nip]; |
| 269 |
if (C && C->_nact) |
| 270 |
{ |
| 271 |
for (h = 0; h < _npar; h++) |
| 272 |
{ |
| 273 |
q2 = C->_fftd [h]; |
| 274 |
if (q2) |
| 275 |
{ |
| 276 |
g = h + _offs; |
| 277 |
if (g >= _npar) g -= _npar; |
| 278 |
q1 = _fwd_data [g + i * _npar]; |
| 279 |
if (_opts == CONV_OPT_SSE) mac_sse (q1, q2, _mac_data, _part >> 2); |
| 280 |
else if (_opts == CONV_OPT_3DN) mac_3dn (q1, q2, _mac_data, _part >> 2); |
| 281 |
else for (g = 0; g <= _part; g++) |
| 282 |
{ |
| 283 |
_mac_data [g][0] += q1 [g][0] * q2 [g][0] - q1 [g][1] * q2 [g][1]; |
| 284 |
_mac_data [g][1] += q1 [g][0] * q2 [g][1] + q1 [g][1] * q2 [g][0]; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (_opts) Convdata::swap (_mac_data, _part); |
| 292 |
fftwf_execute_dft_c2r (_rev_plan, _mac_data, p1); |
| 293 |
for (g = 0; g < _part; g++) *p1++ += *p2++; |
| 294 |
p1 += _part; |
| 295 |
p2 += _part; |
| 296 |
} |
| 297 |
} |