blob: 5b5bce5110e0f25f29bd355bde9441846a613bbc [file] [log] [blame]
Thomas Tsou7e4e5362013-10-30 21:18:55 -04001/*
2 * NEON 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/* Forward declarations from base implementation */
29int _base_convolve_real(float *x, int x_len,
30 float *h, int h_len,
31 float *y, int y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +010032 int start, int len);
Thomas Tsou7e4e5362013-10-30 21:18:55 -040033
34int _base_convolve_complex(float *x, int x_len,
35 float *h, int h_len,
36 float *y, int y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +010037 int start, int len);
Thomas Tsou7e4e5362013-10-30 21:18:55 -040038
39int bounds_check(int x_len, int h_len, int y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +010040 int start, int len);
Thomas Tsou7e4e5362013-10-30 21:18:55 -040041
42#ifdef HAVE_NEON
43/* Calls into NEON assembler */
44void neon_conv_real4(float *x, float *h, float *y, int len);
45void neon_conv_real8(float *x, float *h, float *y, int len);
46void neon_conv_real12(float *x, float *h, float *y, int len);
47void neon_conv_real16(float *x, float *h, float *y, int len);
48void neon_conv_real20(float *x, float *h, float *y, int len);
49void mac_cx_neon4(float *x, float *h, float *y, int len);
50
51/* Complex-complex convolution */
52static void neon_conv_cmplx_4n(float *x, float *h, float *y, int h_len, int len)
53{
54 for (int i = 0; i < len; i++)
55 mac_cx_neon4(&x[2 * i], h, &y[2 * i], h_len >> 2);
56}
57#endif
58
Philipp Maier7e07cf22017-03-15 18:09:35 +010059/* API: Initalize convolve module */
60void convolve_init(void)
61{
62 /* Stub */
63 return;
64}
65
Thomas Tsou7e4e5362013-10-30 21:18:55 -040066/* API: Aligned complex-real */
67int convolve_real(float *x, int x_len,
68 float *h, int h_len,
69 float *y, int y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +010070 int start, int len)
Thomas Tsou7e4e5362013-10-30 21:18:55 -040071{
72 void (*conv_func)(float *, float *, float *, int) = NULL;
73
Sylvain Munauta3934a12018-12-20 19:10:26 +010074 if (bounds_check(x_len, h_len, y_len, start, len) < 0)
Thomas Tsou7e4e5362013-10-30 21:18:55 -040075 return -1;
76
77 memset(y, 0, len * 2 * sizeof(float));
78
79#ifdef HAVE_NEON
Sylvain Munauta3934a12018-12-20 19:10:26 +010080 switch (h_len) {
81 case 4:
82 conv_func = neon_conv_real4;
83 break;
84 case 8:
85 conv_func = neon_conv_real8;
86 break;
87 case 12:
88 conv_func = neon_conv_real12;
89 break;
90 case 16:
91 conv_func = neon_conv_real16;
92 break;
93 case 20:
94 conv_func = neon_conv_real20;
95 break;
Thomas Tsou7e4e5362013-10-30 21:18:55 -040096 }
97#endif
98 if (conv_func) {
99 conv_func(&x[2 * (-(h_len - 1) + start)],
100 h, y, len);
101 } else {
102 _base_convolve_real(x, x_len,
103 h, h_len,
104 y, y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +0100105 start, len);
Thomas Tsou7e4e5362013-10-30 21:18:55 -0400106 }
107
108 return len;
109}
110
111
112/* API: Aligned complex-complex */
113int convolve_complex(float *x, int x_len,
114 float *h, int h_len,
115 float *y, int y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +0100116 int start, int len)
Thomas Tsou7e4e5362013-10-30 21:18:55 -0400117{
118 void (*conv_func)(float *, float *, float *, int, int) = NULL;
119
Sylvain Munauta3934a12018-12-20 19:10:26 +0100120 if (bounds_check(x_len, h_len, y_len, start, len) < 0)
Thomas Tsou7e4e5362013-10-30 21:18:55 -0400121 return -1;
122
123 memset(y, 0, len * 2 * sizeof(float));
124
125#ifdef HAVE_NEON
Sylvain Munauta3934a12018-12-20 19:10:26 +0100126 if (!(h_len % 4))
Thomas Tsou7e4e5362013-10-30 21:18:55 -0400127 conv_func = neon_conv_cmplx_4n;
128#endif
129 if (conv_func) {
130 conv_func(&x[2 * (-(h_len - 1) + start)],
131 h, y, h_len, len);
132 } else {
133 _base_convolve_complex(x, x_len,
134 h, h_len,
135 y, y_len,
Sylvain Munauta3934a12018-12-20 19:10:26 +0100136 start, len);
Thomas Tsou7e4e5362013-10-30 21:18:55 -0400137 }
138
139 return len;
140}