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_invsqrt_32f.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013, 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_invsqrt_32f
25  *
26  * \b Overview
27  *
28  * Computes the inverse square root of the input vector and stores
29  * result in the output vector.
30  *
31  * <b>Dispatcher Prototype</b>
32  * \code
33  * void volk_32f_invsqrt_32f(float* cVector, const float* aVector, unsigned int num_points)
34  * \endcode
35  *
36  * \b Inputs
37  * \li aVector: the input vector of floats.
38  * \li num_points: The number of data points.
39  *
40  * \b Outputs
41  * \li cVector: The output vector.
42  *
43  * \b Example
44  * \code
45  * int N = 10;
46  * unsigned int alignment = volk_get_alignment();
47  * float* in = (float*)volk_malloc(sizeof(float)*N, alignment);
48  * float* out = (float*)volk_malloc(sizeof(float)*N, alignment);
49  *
50  * for(unsigned int ii = 0; ii < N; ++ii){
51  * in[ii] = 1.0 / (float)(ii*ii);
52  * }
53  *
54  * volk_32f_invsqrt_32f(out, in, N);
55  *
56  * for(unsigned int ii = 0; ii < N; ++ii){
57  * printf("out(%i) = %f\n", ii, out[ii]);
58  * }
59  *
60  * volk_free(in);
61  * volk_free(out);
62  * \endcode
63  */
64 
65 #ifndef INCLUDED_volk_32f_invsqrt_32f_a_H
66 #define INCLUDED_volk_32f_invsqrt_32f_a_H
67 
68 #include <inttypes.h>
69 #include <stdio.h>
70 #include <math.h>
71 #include <string.h>
72 
73 static inline float
74 Q_rsqrt(float number)
75 {
76  float x2;
77  const float threehalfs = 1.5F;
78  union f32_to_i32 {
79  int32_t i;
80  float f;
81  } u;
82 
83  x2 = number * 0.5F;
84  u.f = number;
85  u.i = 0x5f3759df - ( u.i >> 1 ); // what the fuck?
86  u.f = u.f * ( threehalfs - ( x2 * u.f * u.f ) ); // 1st iteration
87  //u.f = u.f * ( threehalfs - ( x2 * u.f * u.f ) ); // 2nd iteration, this can be removed
88 
89  return u.f;
90 }
91 
92 #ifdef LV_HAVE_AVX
93 #include <immintrin.h>
94 
95 static inline void
96 volk_32f_invsqrt_32f_a_avx(float* cVector, const float* aVector, unsigned int num_points)
97 {
98  unsigned int number = 0;
99  const unsigned int eighthPoints = num_points / 8;
100 
101  float* cPtr = cVector;
102  const float* aPtr = aVector;
103  __m256 aVal, cVal;
104  for (; number < eighthPoints; number++) {
105  aVal = _mm256_load_ps(aPtr);
106  cVal = _mm256_rsqrt_ps(aVal);
107  _mm256_store_ps(cPtr, cVal);
108  aPtr += 8;
109  cPtr += 8;
110  }
111 
112  number = eighthPoints * 8;
113  for(;number < num_points; number++)
114  *cPtr++ = Q_rsqrt(*aPtr++);
115 
116 }
117 #endif /* LV_HAVE_AVX */
118 
119 
120 #ifdef LV_HAVE_SSE
121 #include <xmmintrin.h>
122 
123 static inline void
124 volk_32f_invsqrt_32f_a_sse(float* cVector, const float* aVector, unsigned int num_points)
125 {
126  unsigned int number = 0;
127  const unsigned int quarterPoints = num_points / 4;
128 
129  float* cPtr = cVector;
130  const float* aPtr = aVector;
131 
132  __m128 aVal, cVal;
133  for(;number < quarterPoints; number++){
134 
135  aVal = _mm_load_ps(aPtr);
136 
137  cVal = _mm_rsqrt_ps(aVal);
138 
139  _mm_store_ps(cPtr,cVal); // Store the results back into the C container
140 
141  aPtr += 4;
142  cPtr += 4;
143  }
144 
145  number = quarterPoints * 4;
146  for(;number < num_points; number++) {
147  *cPtr++ = Q_rsqrt(*aPtr++);
148  }
149 }
150 #endif /* LV_HAVE_SSE */
151 
152 
153 #ifdef LV_HAVE_NEON
154 #include <arm_neon.h>
155 
156 static inline void
157 volk_32f_invsqrt_32f_neon(float* cVector, const float* aVector, unsigned int num_points)
158 {
159  unsigned int number;
160  const unsigned int quarter_points = num_points / 4;
161 
162  float* cPtr = cVector;
163  const float* aPtr = aVector;
164  float32x4_t a_val, c_val;
165  for (number = 0; number < quarter_points; ++number) {
166  a_val = vld1q_f32(aPtr);
167  c_val = vrsqrteq_f32(a_val);
168  vst1q_f32(cPtr, c_val);
169  aPtr += 4;
170  cPtr += 4;
171  }
172 
173  for(number=quarter_points * 4;number < num_points; number++)
174  *cPtr++ = Q_rsqrt(*aPtr++);
175 }
176 #endif /* LV_HAVE_NEON */
177 
178 
179 #ifdef LV_HAVE_GENERIC
180 
181 static inline void
182 volk_32f_invsqrt_32f_generic(float* cVector, const float* aVector, unsigned int num_points)
183 {
184  float* cPtr = cVector;
185  const float* aPtr = aVector;
186  unsigned int number = 0;
187  for(number = 0; number < num_points; number++) {
188  *cPtr++ = Q_rsqrt(*aPtr++);
189  }
190 }
191 #endif /* LV_HAVE_GENERIC */
192 
193 #ifdef LV_HAVE_AVX
194 #include <immintrin.h>
195 
196 static inline void
197 volk_32f_invsqrt_32f_u_avx(float* cVector, const float* aVector, unsigned int num_points)
198 {
199  unsigned int number = 0;
200  const unsigned int eighthPoints = num_points / 8;
201 
202  float* cPtr = cVector;
203  const float* aPtr = aVector;
204  __m256 aVal, cVal;
205  for (; number < eighthPoints; number++) {
206  aVal = _mm256_loadu_ps(aPtr);
207  cVal = _mm256_rsqrt_ps(aVal);
208  _mm256_storeu_ps(cPtr, cVal);
209  aPtr += 8;
210  cPtr += 8;
211  }
212 
213  number = eighthPoints * 8;
214  for(;number < num_points; number++)
215  *cPtr++ = Q_rsqrt(*aPtr++);
216 
217 }
218 #endif /* LV_HAVE_AVX */
219 
220 #endif /* INCLUDED_volk_32f_invsqrt_32f_a_H */
static float Q_rsqrt(float number)
Definition: volk_32f_invsqrt_32f.h:74
signed int int32_t
Definition: stdint.h:77