blob: 71453a1d054bf9099e674976aba93024f69255bd [file] [log] [blame]
Thomas Tsou17bbb9b2013-10-30 21:24:40 -04001/*
2 * Convolution
3 * Copyright (C) 2012, 2013 Thomas Tsou <tom@tsou.cc>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <malloc.h>
21#include <string.h>
22#include <stdio.h>
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28/* Base multiply and accumulate complex-real */
Tom Tsouf147b172015-03-25 12:55:11 -070029static void mac_real(const float *x, const float *h, float *y)
Thomas Tsou17bbb9b2013-10-30 21:24:40 -040030{
31 y[0] += x[0] * h[0];
32 y[1] += x[1] * h[0];
33}
34
35/* Base multiply and accumulate complex-complex */
Tom Tsouf147b172015-03-25 12:55:11 -070036static void mac_cmplx(const float *x, const float *h, float *y)
Thomas Tsou17bbb9b2013-10-30 21:24:40 -040037{
38 y[0] += x[0] * h[0] - x[1] * h[1];
39 y[1] += x[0] * h[1] + x[1] * h[0];
40}
41
42/* Base vector complex-complex multiply and accumulate */
Tom Tsouf147b172015-03-25 12:55:11 -070043static void mac_real_vec_n(const float *x, const float *h, float *y,
Thomas Tsou17bbb9b2013-10-30 21:24:40 -040044 int len, int step, int offset)
45{
46 for (int i = offset; i < len; i += step)
47 mac_real(&x[2 * i], &h[2 * i], y);
48}
49
50/* Base vector complex-complex multiply and accumulate */
Tom Tsouf147b172015-03-25 12:55:11 -070051static void mac_cmplx_vec_n(const float *x, const float *h, float *y,
Thomas Tsou17bbb9b2013-10-30 21:24:40 -040052 int len, int step, int offset)
53{
54 for (int i = offset; i < len; i += step)
55 mac_cmplx(&x[2 * i], &h[2 * i], y);
56}
57
58/* Base complex-real convolution */
Tom Tsouf147b172015-03-25 12:55:11 -070059int _base_convolve_real(const float *x, int x_len,
60 const float *h, int h_len,
Thomas Tsou17bbb9b2013-10-30 21:24:40 -040061 float *y, int y_len,
62 int start, int len,
63 int step, int offset)
64{
65 for (int i = 0; i < len; i++) {
66 mac_real_vec_n(&x[2 * (i - (h_len - 1) + start)],
67 h,
68 &y[2 * i], h_len,
69 step, offset);
70 }
71
72 return len;
73}
74
75/* Base complex-complex convolution */
Tom Tsouf147b172015-03-25 12:55:11 -070076int _base_convolve_complex(const float *x, int x_len,
77 const float *h, int h_len,
Thomas Tsou17bbb9b2013-10-30 21:24:40 -040078 float *y, int y_len,
79 int start, int len,
80 int step, int offset)
81{
82 for (int i = 0; i < len; i++) {
83 mac_cmplx_vec_n(&x[2 * (i - (h_len - 1) + start)],
84 h,
85 &y[2 * i],
86 h_len, step, offset);
87 }
88
89 return len;
90}
91
92/* Buffer validity checks */
93int bounds_check(int x_len, int h_len, int y_len,
94 int start, int len, int step)
95{
96 if ((x_len < 1) || (h_len < 1) ||
97 (y_len < 1) || (len < 1) || (step < 1)) {
98 fprintf(stderr, "Convolve: Invalid input\n");
99 return -1;
100 }
101
102 if ((start + len > x_len) || (len > y_len) || (x_len < h_len)) {
103 fprintf(stderr, "Convolve: Boundary exception\n");
104 fprintf(stderr, "start: %i, len: %i, x: %i, h: %i, y: %i\n",
105 start, len, x_len, h_len, y_len);
106 return -1;
107 }
108
109 return 0;
110}
111
112/* API: Non-aligned (no SSE) complex-real */
Tom Tsouf147b172015-03-25 12:55:11 -0700113int base_convolve_real(const float *x, int x_len,
114 const float *h, int h_len,
Thomas Tsou17bbb9b2013-10-30 21:24:40 -0400115 float *y, int y_len,
116 int start, int len,
117 int step, int offset)
118{
119 if (bounds_check(x_len, h_len, y_len, start, len, step) < 0)
120 return -1;
121
122 memset(y, 0, len * 2 * sizeof(float));
123
124 return _base_convolve_real(x, x_len,
125 h, h_len,
126 y, y_len,
127 start, len, step, offset);
128}
129
130/* API: Non-aligned (no SSE) complex-complex */
Tom Tsouf147b172015-03-25 12:55:11 -0700131int base_convolve_complex(const float *x, int x_len,
132 const float *h, int h_len,
Thomas Tsou17bbb9b2013-10-30 21:24:40 -0400133 float *y, int y_len,
134 int start, int len,
135 int step, int offset)
136{
137 if (bounds_check(x_len, h_len, y_len, start, len, step) < 0)
138 return -1;
139
140 memset(y, 0, len * 2 * sizeof(float));
141
142 return _base_convolve_complex(x, x_len,
143 h, h_len,
144 y, y_len,
145 start, len, step, offset);
146}
147
148/* Aligned filter tap allocation */
149void *convolve_h_alloc(int len)
150{
151#ifdef HAVE_SSE3
152 return memalign(16, len * 2 * sizeof(float));
153#else
154 return malloc(len * 2 * sizeof(float));
155#endif
156}