GNU Radio Manual and C++ API Reference  3.7.7
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
volk_32f_index_max_16u.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012, 2014 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /*!
24  * \page volk_32f_index_max_16u
25  *
26  * \b Overview
27  *
28  * Returns Argmax_i x[i]. Finds and returns the index which contains the maximum value in the given vector.
29  *
30  * <b>Dispatcher Prototype</b>
31  * \code
32  * void volk_32f_index_max_16u_a_sse4_1(unsigned int* target, const float* src0, unsigned int num_points)
33  * \endcode
34  *
35  * \b Inputs
36  * \li src0: The input vector of floats.
37  * \li num_points: The number of data points.
38  *
39  * \b Outputs
40  * \li target: The index of the maximum value in the input buffer.
41  *
42  * \b Example
43  * \code
44  * int N = 10;
45  * unsigned int alignment = volk_get_alignment();
46  * float* in = (float*)volk_malloc(sizeof(float)*N, alignment);
47  * uint32_t* out = (uint32_t*)volk_malloc(sizeof(uint32_t), alignment);
48  *
49  * for(unsigned int ii = 0; ii < N; ++ii){
50  * float x = (float)ii;
51  * // a parabola with a maximum at x=4
52  * in[ii] = -(x-4) * (x-4) + 5;
53  * }
54  *
55  * volk_32f_index_max_16u(out, in, N);
56  *
57  * printf("maximum is %1.2f at index %u\n", in[*out], *out);
58  *
59  * volk_free(in);
60  * volk_free(out);
61  * \endcode
62  */
63 
64 #ifndef INCLUDED_volk_32f_index_max_16u_a_H
65 #define INCLUDED_volk_32f_index_max_16u_a_H
66 
67 #include <volk/volk_common.h>
68 #include <volk/volk_common.h>
69 #include <inttypes.h>
70 #include <stdio.h>
71 
72 #ifdef LV_HAVE_SSE4_1
73 #include<smmintrin.h>
74 
75 static inline void
76 volk_32f_index_max_16u_a_sse4_1(unsigned int* target, const float* src0, unsigned int num_points)
77 {
78  if(num_points > 0){
79  unsigned int number = 0;
80  const unsigned int quarterPoints = num_points / 4;
81 
82  float* inputPtr = (float*)src0;
83 
84  __m128 indexIncrementValues = _mm_set1_ps(4);
85  __m128 currentIndexes = _mm_set_ps(-1,-2,-3,-4);
86 
87  float max = src0[0];
88  float index = 0;
89  __m128 maxValues = _mm_set1_ps(max);
90  __m128 maxValuesIndex = _mm_setzero_ps();
91  __m128 compareResults;
92  __m128 currentValues;
93 
94  __VOLK_ATTR_ALIGNED(16) float maxValuesBuffer[4];
95  __VOLK_ATTR_ALIGNED(16) float maxIndexesBuffer[4];
96 
97  for(;number < quarterPoints; number++){
98 
99  currentValues = _mm_load_ps(inputPtr); inputPtr += 4;
100  currentIndexes = _mm_add_ps(currentIndexes, indexIncrementValues);
101 
102  compareResults = _mm_cmpgt_ps(maxValues, currentValues);
103 
104  maxValuesIndex = _mm_blendv_ps(currentIndexes, maxValuesIndex, compareResults);
105  maxValues = _mm_blendv_ps(currentValues, maxValues, compareResults);
106  }
107 
108  // Calculate the largest value from the remaining 4 points
109  _mm_store_ps(maxValuesBuffer, maxValues);
110  _mm_store_ps(maxIndexesBuffer, maxValuesIndex);
111 
112  for(number = 0; number < 4; number++){
113  if(maxValuesBuffer[number] > max){
114  index = maxIndexesBuffer[number];
115  max = maxValuesBuffer[number];
116  }
117  }
118 
119  number = quarterPoints * 4;
120  for(;number < num_points; number++){
121  if(src0[number] > max){
122  index = number;
123  max = src0[number];
124  }
125  }
126  target[0] = (unsigned int)index;
127  }
128 }
129 
130 #endif /*LV_HAVE_SSE4_1*/
131 
132 
133 #ifdef LV_HAVE_SSE
134 
135 #include<xmmintrin.h>
136 
137 static inline void
138 volk_32f_index_max_16u_a_sse(unsigned int* target, const float* src0, unsigned int num_points)
139 {
140  if(num_points > 0){
141  unsigned int number = 0;
142  const unsigned int quarterPoints = num_points / 4;
143 
144  float* inputPtr = (float*)src0;
145 
146  __m128 indexIncrementValues = _mm_set1_ps(4);
147  __m128 currentIndexes = _mm_set_ps(-1,-2,-3,-4);
148 
149  float max = src0[0];
150  float index = 0;
151  __m128 maxValues = _mm_set1_ps(max);
152  __m128 maxValuesIndex = _mm_setzero_ps();
153  __m128 compareResults;
154  __m128 currentValues;
155 
156  __VOLK_ATTR_ALIGNED(16) float maxValuesBuffer[4];
157  __VOLK_ATTR_ALIGNED(16) float maxIndexesBuffer[4];
158 
159  for(;number < quarterPoints; number++){
160 
161  currentValues = _mm_load_ps(inputPtr); inputPtr += 4;
162  currentIndexes = _mm_add_ps(currentIndexes, indexIncrementValues);
163 
164  compareResults = _mm_cmpgt_ps(maxValues, currentValues);
165 
166  maxValuesIndex = _mm_or_ps(_mm_and_ps(compareResults, maxValuesIndex) , _mm_andnot_ps(compareResults, currentIndexes));
167 
168  maxValues = _mm_or_ps(_mm_and_ps(compareResults, maxValues) , _mm_andnot_ps(compareResults, currentValues));
169  }
170 
171  // Calculate the largest value from the remaining 4 points
172  _mm_store_ps(maxValuesBuffer, maxValues);
173  _mm_store_ps(maxIndexesBuffer, maxValuesIndex);
174 
175  for(number = 0; number < 4; number++){
176  if(maxValuesBuffer[number] > max){
177  index = maxIndexesBuffer[number];
178  max = maxValuesBuffer[number];
179  }
180  }
181 
182  number = quarterPoints * 4;
183  for(;number < num_points; number++){
184  if(src0[number] > max){
185  index = number;
186  max = src0[number];
187  }
188  }
189  target[0] = (unsigned int)index;
190  }
191 }
192 
193 #endif /*LV_HAVE_SSE*/
194 
195 
196 #ifdef LV_HAVE_GENERIC
197 
198 static inline void
199 volk_32f_index_max_16u_generic(unsigned int* target, const float* src0, unsigned int num_points)
200 {
201  if(num_points > 0){
202  float max = src0[0];
203  unsigned int index = 0;
204 
205  unsigned int i = 1;
206 
207  for(; i < num_points; ++i) {
208  if(src0[i] > max){
209  index = i;
210  max = src0[i];
211  }
212  }
213  target[0] = index;
214  }
215 }
216 
217 #endif /*LV_HAVE_GENERIC*/
218 
219 
220 #endif /*INCLUDED_volk_32f_index_max_16u_a_H*/
#define __VOLK_ATTR_ALIGNED(x)
Definition: volk_common.h:27