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 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: LGPL-2.1+ |
| 6 | * |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <malloc.h> |
| 19 | #include <string.h> |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | /* Base multiply and accumulate complex-real */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 27 | static void mac_real(const float *x, const float *h, float *y) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 28 | { |
| 29 | y[0] += x[0] * h[0]; |
| 30 | y[1] += x[1] * h[0]; |
| 31 | } |
| 32 | |
| 33 | /* Base multiply and accumulate complex-complex */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 34 | static void mac_cmplx(const float *x, const float *h, float *y) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 35 | { |
| 36 | y[0] += x[0] * h[0] - x[1] * h[1]; |
| 37 | y[1] += x[0] * h[1] + x[1] * h[0]; |
| 38 | } |
| 39 | |
| 40 | /* Base vector complex-complex multiply and accumulate */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 41 | static void mac_real_vec_n(const float *x, const float *h, float *y, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 42 | int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 43 | { |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 44 | for (int i=0; i<len; i++) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 45 | mac_real(&x[2 * i], &h[2 * i], y); |
| 46 | } |
| 47 | |
| 48 | /* Base vector complex-complex multiply and accumulate */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 49 | static void mac_cmplx_vec_n(const float *x, const float *h, float *y, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 50 | int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 51 | { |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 52 | for (int i=0; i<len; i++) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 53 | mac_cmplx(&x[2 * i], &h[2 * i], y); |
| 54 | } |
| 55 | |
| 56 | /* Base complex-real convolution */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 57 | int _base_convolve_real(const float *x, int x_len, |
| 58 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 59 | float *y, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 60 | int start, int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 61 | { |
| 62 | for (int i = 0; i < len; i++) { |
| 63 | mac_real_vec_n(&x[2 * (i - (h_len - 1) + start)], |
| 64 | h, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 65 | &y[2 * i], h_len); |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return len; |
| 69 | } |
| 70 | |
| 71 | /* Base complex-complex convolution */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 72 | int _base_convolve_complex(const float *x, int x_len, |
| 73 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 74 | float *y, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 75 | int start, int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 76 | { |
| 77 | for (int i = 0; i < len; i++) { |
| 78 | mac_cmplx_vec_n(&x[2 * (i - (h_len - 1) + start)], |
| 79 | h, |
| 80 | &y[2 * i], |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 81 | h_len); |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return len; |
| 85 | } |
| 86 | |
| 87 | /* Buffer validity checks */ |
| 88 | int bounds_check(int x_len, int h_len, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 89 | int start, int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 90 | { |
| 91 | if ((x_len < 1) || (h_len < 1) || |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 92 | (y_len < 1) || (len < 1)) { |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 93 | fprintf(stderr, "Convolve: Invalid input\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | if ((start + len > x_len) || (len > y_len) || (x_len < h_len)) { |
| 98 | fprintf(stderr, "Convolve: Boundary exception\n"); |
| 99 | fprintf(stderr, "start: %i, len: %i, x: %i, h: %i, y: %i\n", |
| 100 | start, len, x_len, h_len, y_len); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* API: Non-aligned (no SSE) complex-real */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 108 | int base_convolve_real(const float *x, int x_len, |
| 109 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 110 | float *y, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 111 | int start, int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 112 | { |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 113 | if (bounds_check(x_len, h_len, y_len, start, len) < 0) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 114 | return -1; |
| 115 | |
| 116 | memset(y, 0, len * 2 * sizeof(float)); |
| 117 | |
| 118 | return _base_convolve_real(x, x_len, |
| 119 | h, h_len, |
| 120 | y, y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 121 | start, len); |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* API: Non-aligned (no SSE) complex-complex */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 125 | int base_convolve_complex(const float *x, int x_len, |
| 126 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 127 | float *y, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 128 | int start, int len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 129 | { |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 130 | if (bounds_check(x_len, h_len, y_len, start, len) < 0) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 131 | return -1; |
| 132 | |
| 133 | memset(y, 0, len * 2 * sizeof(float)); |
| 134 | |
| 135 | return _base_convolve_complex(x, x_len, |
| 136 | h, h_len, |
| 137 | y, y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 138 | start, len); |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* Aligned filter tap allocation */ |
Pau Espin Pedrol | f733176 | 2018-12-03 17:46:04 +0100 | [diff] [blame] | 142 | void *convolve_h_alloc(size_t len) |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 143 | { |
| 144 | #ifdef HAVE_SSE3 |
| 145 | return memalign(16, len * 2 * sizeof(float)); |
| 146 | #else |
| 147 | return malloc(len * 2 * sizeof(float)); |
| 148 | #endif |
| 149 | } |