2 Copyright (C) 2007 Paul sDavis
3 Written by Sampo Savolainen
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.
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.
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.
21 #include <xmmintrin.h>
22 #include <immintrin.h>
27 x86_sse_avx_find_peaks(const float* buf, uint32_t nframes, float *min, float *max)
29 __m256 current_max, current_min, work;
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);
35 // Work input until "buf" reaches 16 byte alignment
36 while ( ((intptr_t)buf) % 32 != 0 && nframes > 0) {
38 // Load the next float into the work buffer
39 work = _mm256_set1_ps(*buf);
41 current_min = _mm256_min_ps(current_min, work);
42 current_max = _mm256_max_ps(current_max, work);
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) );
54 __builtin_prefetch(buf+64,0,0);
56 work = _mm256_load_ps(buf);
57 current_min = _mm256_min_ps(current_min, work);
58 current_max = _mm256_max_ps(current_max, work);
60 work = _mm256_load_ps(buf);
61 current_min = _mm256_min_ps(current_min, work);
62 current_max = _mm256_max_ps(current_max, work);
68 // work through 32 bytes aligned buffers
69 while (nframes >= 8) {
71 work = _mm256_load_ps(buf);
73 current_min = _mm256_min_ps(current_min, work);
74 current_max = _mm256_max_ps(current_max, work);
80 // work through the rest < 4 samples
81 while ( nframes > 0) {
83 // Load the next float into the work buffer
84 work = _mm256_set1_ps(*buf);
86 current_min = _mm256_min_ps(current_min, work);
87 current_max = _mm256_max_ps(current_max, work);
93 // Find min & max value in current_max through shuffle tricks
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);
103 *min = current_min[0];
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);
113 *max = current_max[0];
115 // zero upper 128 bit of 256 bit ymm register to avoid penalties using non-AVX instructions