fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / sse_functions_avx.cc
1 /*
2     Copyright (C) 2007 Paul sDavis
3     Written by Sampo Savolainen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program 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
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <xmmintrin.h>
22 #include <immintrin.h>
23 #include <stdint.h>
24
25
26 void
27 x86_sse_avx_find_peaks(const float* buf, uint32_t nframes, float *min, float *max)
28 {
29         __m256 current_max, current_min, work;
30
31         // Load max and min values into all eight slots of the YMM registers
32         current_min = _mm256_set1_ps(*min);
33         current_max = _mm256_set1_ps(*max);
34
35         // Work input until "buf" reaches 16 byte alignment
36         while ( ((intptr_t)buf) % 32 != 0 && nframes > 0) {
37
38                 // Load the next float into the work buffer
39                 work = _mm256_set1_ps(*buf);
40
41                 current_min = _mm256_min_ps(current_min, work);
42                 current_max = _mm256_max_ps(current_max, work);
43
44                 buf++;
45                 nframes--;
46         }
47
48         // use 64 byte prefetch for quadruple quads:
49                 // load each 64 bytes into cash before processing
50         while (nframes >= 16) {
51 #if defined(COMPILER_MSVC) || defined(COMPILER_MINGW)
52                                 _mm_prefetch(((char*)buf+64), _mm_hint(0) );
53 #else
54                 __builtin_prefetch(buf+64,0,0);
55 #endif
56                 work = _mm256_load_ps(buf);
57                 current_min = _mm256_min_ps(current_min, work);
58                 current_max = _mm256_max_ps(current_max, work);
59                 buf+=8;
60                 work = _mm256_load_ps(buf);
61                 current_min = _mm256_min_ps(current_min, work);
62                 current_max = _mm256_max_ps(current_max, work);
63                 buf+=8;
64
65                 nframes-=16;
66         }
67
68         // work through 32 bytes aligned buffers
69         while (nframes >= 8) {
70
71                 work = _mm256_load_ps(buf);
72
73                 current_min = _mm256_min_ps(current_min, work);
74                 current_max = _mm256_max_ps(current_max, work);
75
76                 buf+=8;
77                 nframes-=8;
78         }
79
80         // work through the rest < 4 samples
81         while ( nframes > 0) {
82
83                 // Load the next float into the work buffer
84                 work = _mm256_set1_ps(*buf);
85
86                 current_min = _mm256_min_ps(current_min, work);
87                 current_max = _mm256_max_ps(current_max, work);
88
89                 buf++;
90                 nframes--;
91         }
92
93         // Find min & max value in current_max through shuffle tricks
94
95         work = current_min;
96         work =        _mm256_shuffle_ps (current_min, current_min, _MM_SHUFFLE(2, 3, 0, 1));
97         current_min = _mm256_min_ps (work, current_min);
98         work =        _mm256_shuffle_ps (current_min, current_min, _MM_SHUFFLE(1, 0, 3, 2));
99         current_min = _mm256_min_ps (work, current_min);
100         work =        _mm256_permute2f128_ps( current_min, current_min, 1);
101         current_min = _mm256_min_ps (work, current_min);
102
103         *min = current_min[0];
104
105         work = current_max;
106         work =        _mm256_shuffle_ps(current_max, current_max, _MM_SHUFFLE(2, 3, 0, 1));
107         current_max = _mm256_max_ps (work, current_max);
108         work =        _mm256_shuffle_ps(current_max, current_max, _MM_SHUFFLE(1, 0, 3, 2));
109         current_max = _mm256_max_ps (work, current_max);
110         work =        _mm256_permute2f128_ps( current_max, current_max, 1);
111         current_max = _mm256_max_ps (work, current_max);
112
113         *max = current_max[0];
114
115         // zero upper 128 bit of 256 bit ymm register to avoid penalties using non-AVX instructions
116         _mm256_zeroupper ();
117 }
118
119