Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 1 | /* |
| 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 29 | static void mac_real(const float *x, const float *h, float *y) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 30 | { |
| 31 | y[0] += x[0] * h[0]; |
| 32 | y[1] += x[1] * h[0]; |
| 33 | } |
| 34 | |
| 35 | /* Base multiply and accumulate complex-complex */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 36 | static void mac_cmplx(const float *x, const float *h, float *y) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 37 | { |
| 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 43 | static void mac_real_vec_n(const float *x, const float *h, float *y, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 44 | 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 51 | static void mac_cmplx_vec_n(const float *x, const float *h, float *y, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 52 | 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 59 | int _base_convolve_real(const float *x, int x_len, |
| 60 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 61 | 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 76 | int _base_convolve_complex(const float *x, int x_len, |
| 77 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 78 | 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 */ |
| 93 | int 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 113 | int base_convolve_real(const float *x, int x_len, |
| 114 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 115 | 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 Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 131 | int base_convolve_complex(const float *x, int x_len, |
| 132 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 133 | 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 */ |
| 149 | void *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 | } |