Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * SSE 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. |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <malloc.h> |
| 17 | #include <string.h> |
| 18 | #include <stdio.h> |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 19 | #include "convolve.h" |
Philipp Maier | e8ae9fc | 2017-03-20 12:08:42 +0100 | [diff] [blame] | 20 | #include "convolve_sse_3.h" |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include "config.h" |
| 24 | #endif |
| 25 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 26 | /* Architecture dependent function pointers */ |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 27 | struct convolve_cpu_context { |
| 28 | void (*conv_cmplx_4n) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 29 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 30 | void (*conv_cmplx_8n) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 31 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 32 | void (*conv_cmplx) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 33 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 34 | void (*conv_real4) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 35 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 36 | void (*conv_real8) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 37 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 38 | void (*conv_real12) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 39 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 40 | void (*conv_real16) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 41 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 42 | void (*conv_real20) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 43 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 44 | void (*conv_real4n) (const float *, int, const float *, int, float *, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 45 | int, int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 46 | void (*conv_real) (const float *, int, const float *, int, float *, int, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 47 | int, int); |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 48 | }; |
| 49 | static struct convolve_cpu_context c; |
| 50 | |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 51 | /* Forward declarations from base implementation */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 52 | int _base_convolve_real(const float *x, int x_len, |
| 53 | const float *h, int h_len, |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 54 | float *y, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 55 | int start, int len); |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 56 | |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 57 | int _base_convolve_complex(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 | int bounds_check(int x_len, int h_len, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 63 | int start, int len); |
Thomas Tsou | 17bbb9b | 2013-10-30 21:24:40 -0400 | [diff] [blame] | 64 | |
Martin Hauke | 066fd04 | 2019-10-13 19:08:00 +0200 | [diff] [blame] | 65 | /* API: Initialize convolve module */ |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 66 | void convolve_init(void) |
| 67 | { |
| 68 | c.conv_cmplx_4n = (void *)_base_convolve_complex; |
| 69 | c.conv_cmplx_8n = (void *)_base_convolve_complex; |
| 70 | c.conv_cmplx = (void *)_base_convolve_complex; |
| 71 | c.conv_real4 = (void *)_base_convolve_real; |
| 72 | c.conv_real8 = (void *)_base_convolve_real; |
| 73 | c.conv_real12 = (void *)_base_convolve_real; |
| 74 | c.conv_real16 = (void *)_base_convolve_real; |
| 75 | c.conv_real20 = (void *)_base_convolve_real; |
| 76 | c.conv_real4n = (void *)_base_convolve_real; |
| 77 | c.conv_real = (void *)_base_convolve_real; |
| 78 | |
Vadim Yanitskiy | 3bd763d | 2017-05-20 01:46:51 +0300 | [diff] [blame] | 79 | #if defined(HAVE_SSE3) && defined(HAVE___BUILTIN_CPU_SUPPORTS) |
Philipp Maier | 7e07cf2 | 2017-03-15 18:09:35 +0100 | [diff] [blame] | 80 | if (__builtin_cpu_supports("sse3")) { |
| 81 | c.conv_cmplx_4n = sse_conv_cmplx_4n; |
| 82 | c.conv_cmplx_8n = sse_conv_cmplx_8n; |
| 83 | c.conv_real4 = sse_conv_real4; |
| 84 | c.conv_real8 = sse_conv_real8; |
| 85 | c.conv_real12 = sse_conv_real12; |
| 86 | c.conv_real16 = sse_conv_real16; |
| 87 | c.conv_real20 = sse_conv_real20; |
| 88 | c.conv_real4n = sse_conv_real4n; |
| 89 | } |
| 90 | #endif |
| 91 | } |
| 92 | |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 93 | /* API: Aligned complex-real */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 94 | int convolve_real(const float *x, int x_len, |
| 95 | const float *h, int h_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 96 | float *y, int y_len, int start, int len) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 97 | { |
Eric | 7a52e42 | 2020-08-14 03:11:22 +0200 | [diff] [blame] | 98 | #ifndef __OPTIMIZE__ |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 99 | if (bounds_check(x_len, h_len, y_len, start, len) < 0) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 100 | return -1; |
Eric | 7a52e42 | 2020-08-14 03:11:22 +0200 | [diff] [blame] | 101 | #endif |
Pau Espin Pedrol | 0fbdfef | 2020-09-18 20:11:14 +0200 | [diff] [blame] | 102 | memset(y, 0, len * 2 * sizeof(float)); |
| 103 | |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 104 | switch (h_len) { |
| 105 | case 4: |
| 106 | c.conv_real4(x, x_len, h, h_len, y, y_len, start, len); |
| 107 | break; |
| 108 | case 8: |
| 109 | c.conv_real8(x, x_len, h, h_len, y, y_len, start, len); |
| 110 | break; |
| 111 | case 12: |
| 112 | c.conv_real12(x, x_len, h, h_len, y, y_len, start, len); |
| 113 | break; |
| 114 | case 16: |
| 115 | c.conv_real16(x, x_len, h, h_len, y, y_len, start, len); |
| 116 | break; |
| 117 | case 20: |
| 118 | c.conv_real20(x, x_len, h, h_len, y, y_len, start, len); |
| 119 | break; |
| 120 | default: |
| 121 | if (!(h_len % 4)) |
| 122 | c.conv_real4n(x, x_len, h, h_len, y, y_len, |
| 123 | start, len); |
| 124 | else |
| 125 | c.conv_real(x, x_len, h, h_len, y, y_len, start, |
| 126 | len); |
| 127 | } |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 128 | |
| 129 | return len; |
| 130 | } |
| 131 | |
| 132 | /* API: Aligned complex-complex */ |
Tom Tsou | f147b17 | 2015-03-25 12:55:11 -0700 | [diff] [blame] | 133 | int convolve_complex(const float *x, int x_len, |
| 134 | const float *h, int h_len, |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 135 | float *y, int y_len, |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 136 | int start, int len) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 137 | { |
Eric | 7a52e42 | 2020-08-14 03:11:22 +0200 | [diff] [blame] | 138 | #ifndef __OPTIMIZE__ |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 139 | if (bounds_check(x_len, h_len, y_len, start, len) < 0) |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 140 | return -1; |
Eric | 7a52e42 | 2020-08-14 03:11:22 +0200 | [diff] [blame] | 141 | #endif |
Pau Espin Pedrol | 0fbdfef | 2020-09-18 20:11:14 +0200 | [diff] [blame] | 142 | memset(y, 0, len * 2 * sizeof(float)); |
| 143 | |
Sylvain Munaut | a3934a1 | 2018-12-20 19:10:26 +0100 | [diff] [blame] | 144 | if (!(h_len % 8)) |
| 145 | c.conv_cmplx_8n(x, x_len, h, h_len, y, y_len, start, len); |
| 146 | else if (!(h_len % 4)) |
| 147 | c.conv_cmplx_4n(x, x_len, h, h_len, y, y_len, start, len); |
| 148 | else |
| 149 | c.conv_cmplx(x, x_len, h, h_len, y, y_len, start, len); |
Thomas Tsou | 3eaae80 | 2013-08-20 19:31:14 -0400 | [diff] [blame] | 150 | |
| 151 | return len; |
| 152 | } |