Vector Optimized Library of Kernels  3.0.0
Architecture-tuned implementations of math kernels
volk_16ic_magnitude_16i.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 VOLK
6  *
7  * SPDX-License-Identifier: LGPL-3.0-or-later
8  */
9 
41 #ifndef INCLUDED_volk_16ic_magnitude_16i_a_H
42 #define INCLUDED_volk_16ic_magnitude_16i_a_H
43 
44 #include <inttypes.h>
45 #include <limits.h>
46 #include <math.h>
47 #include <stdio.h>
48 #include <volk/volk_common.h>
49 
50 #ifdef LV_HAVE_AVX2
51 #include <immintrin.h>
52 
53 static inline void volk_16ic_magnitude_16i_a_avx2(int16_t* magnitudeVector,
54  const lv_16sc_t* complexVector,
55  unsigned int num_points)
56 {
57  unsigned int number = 0;
58  const unsigned int eighthPoints = num_points / 8;
59 
60  const int16_t* complexVectorPtr = (const int16_t*)complexVector;
61  int16_t* magnitudeVectorPtr = magnitudeVector;
62 
63  __m256 vScalar = _mm256_set1_ps(SHRT_MAX);
64  __m256 invScalar = _mm256_set1_ps(1.0f / SHRT_MAX);
65  __m256i int1, int2;
66  __m128i short1, short2;
67  __m256 cplxValue1, cplxValue2, result;
68  __m256i idx = _mm256_set_epi32(0, 0, 0, 0, 5, 1, 4, 0);
69 
70  for (; number < eighthPoints; number++) {
71 
72  int1 = _mm256_load_si256((__m256i*)complexVectorPtr);
73  complexVectorPtr += 16;
74  short1 = _mm256_extracti128_si256(int1, 0);
75  short2 = _mm256_extracti128_si256(int1, 1);
76 
77  int1 = _mm256_cvtepi16_epi32(short1);
78  int2 = _mm256_cvtepi16_epi32(short2);
79  cplxValue1 = _mm256_cvtepi32_ps(int1);
80  cplxValue2 = _mm256_cvtepi32_ps(int2);
81 
82  cplxValue1 = _mm256_mul_ps(cplxValue1, invScalar);
83  cplxValue2 = _mm256_mul_ps(cplxValue2, invScalar);
84 
85  cplxValue1 = _mm256_mul_ps(cplxValue1, cplxValue1); // Square the values
86  cplxValue2 = _mm256_mul_ps(cplxValue2, cplxValue2); // Square the Values
87 
88  result = _mm256_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
89 
90  result = _mm256_sqrt_ps(result); // Square root the values
91 
92  result = _mm256_mul_ps(result, vScalar); // Scale the results
93 
94  int1 = _mm256_cvtps_epi32(result);
95  int1 = _mm256_packs_epi32(int1, int1);
96  int1 = _mm256_permutevar8x32_epi32(
97  int1, idx); // permute to compensate for shuffling in hadd and packs
98  short1 = _mm256_extracti128_si256(int1, 0);
99  _mm_store_si128((__m128i*)magnitudeVectorPtr, short1);
100  magnitudeVectorPtr += 8;
101  }
102 
103  number = eighthPoints * 8;
104  magnitudeVectorPtr = &magnitudeVector[number];
105  complexVectorPtr = (const int16_t*)&complexVector[number];
106  for (; number < num_points; number++) {
107  const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
108  const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
109  const float val1Result =
110  sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
111  *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
112  }
113 }
114 #endif /* LV_HAVE_AVX2 */
115 
116 #ifdef LV_HAVE_SSE3
117 #include <pmmintrin.h>
118 
119 static inline void volk_16ic_magnitude_16i_a_sse3(int16_t* magnitudeVector,
120  const lv_16sc_t* complexVector,
121  unsigned int num_points)
122 {
123  unsigned int number = 0;
124  const unsigned int quarterPoints = num_points / 4;
125 
126  const int16_t* complexVectorPtr = (const int16_t*)complexVector;
127  int16_t* magnitudeVectorPtr = magnitudeVector;
128 
129  __m128 vScalar = _mm_set_ps1(SHRT_MAX);
130  __m128 invScalar = _mm_set_ps1(1.0f / SHRT_MAX);
131 
132  __m128 cplxValue1, cplxValue2, result;
133 
134  __VOLK_ATTR_ALIGNED(16) float inputFloatBuffer[8];
135  __VOLK_ATTR_ALIGNED(16) float outputFloatBuffer[4];
136 
137  for (; number < quarterPoints; number++) {
138 
139  inputFloatBuffer[0] = (float)(complexVectorPtr[0]);
140  inputFloatBuffer[1] = (float)(complexVectorPtr[1]);
141  inputFloatBuffer[2] = (float)(complexVectorPtr[2]);
142  inputFloatBuffer[3] = (float)(complexVectorPtr[3]);
143 
144  inputFloatBuffer[4] = (float)(complexVectorPtr[4]);
145  inputFloatBuffer[5] = (float)(complexVectorPtr[5]);
146  inputFloatBuffer[6] = (float)(complexVectorPtr[6]);
147  inputFloatBuffer[7] = (float)(complexVectorPtr[7]);
148 
149  cplxValue1 = _mm_load_ps(&inputFloatBuffer[0]);
150  cplxValue2 = _mm_load_ps(&inputFloatBuffer[4]);
151 
152  complexVectorPtr += 8;
153 
154  cplxValue1 = _mm_mul_ps(cplxValue1, invScalar);
155  cplxValue2 = _mm_mul_ps(cplxValue2, invScalar);
156 
157  cplxValue1 = _mm_mul_ps(cplxValue1, cplxValue1); // Square the values
158  cplxValue2 = _mm_mul_ps(cplxValue2, cplxValue2); // Square the Values
159 
160  result = _mm_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
161 
162  result = _mm_sqrt_ps(result); // Square root the values
163 
164  result = _mm_mul_ps(result, vScalar); // Scale the results
165 
166  _mm_store_ps(outputFloatBuffer, result);
167  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[0]);
168  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[1]);
169  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[2]);
170  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[3]);
171  }
172 
173  number = quarterPoints * 4;
174  magnitudeVectorPtr = &magnitudeVector[number];
175  complexVectorPtr = (const int16_t*)&complexVector[number];
176  for (; number < num_points; number++) {
177  const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
178  const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
179  const float val1Result =
180  sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
181  *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
182  }
183 }
184 #endif /* LV_HAVE_SSE3 */
185 
186 #ifdef LV_HAVE_SSE
187 #include <xmmintrin.h>
188 
189 static inline void volk_16ic_magnitude_16i_a_sse(int16_t* magnitudeVector,
190  const lv_16sc_t* complexVector,
191  unsigned int num_points)
192 {
193  unsigned int number = 0;
194  const unsigned int quarterPoints = num_points / 4;
195 
196  const int16_t* complexVectorPtr = (const int16_t*)complexVector;
197  int16_t* magnitudeVectorPtr = magnitudeVector;
198 
199  __m128 vScalar = _mm_set_ps1(SHRT_MAX);
200  __m128 invScalar = _mm_set_ps1(1.0f / SHRT_MAX);
201 
202  __m128 cplxValue1, cplxValue2, iValue, qValue, result;
203 
204  __VOLK_ATTR_ALIGNED(16) float inputFloatBuffer[4];
205  __VOLK_ATTR_ALIGNED(16) float outputFloatBuffer[4];
206 
207  for (; number < quarterPoints; number++) {
208 
209  inputFloatBuffer[0] = (float)(complexVectorPtr[0]);
210  inputFloatBuffer[1] = (float)(complexVectorPtr[1]);
211  inputFloatBuffer[2] = (float)(complexVectorPtr[2]);
212  inputFloatBuffer[3] = (float)(complexVectorPtr[3]);
213 
214  cplxValue1 = _mm_load_ps(inputFloatBuffer);
215  complexVectorPtr += 4;
216 
217  inputFloatBuffer[0] = (float)(complexVectorPtr[0]);
218  inputFloatBuffer[1] = (float)(complexVectorPtr[1]);
219  inputFloatBuffer[2] = (float)(complexVectorPtr[2]);
220  inputFloatBuffer[3] = (float)(complexVectorPtr[3]);
221 
222  cplxValue2 = _mm_load_ps(inputFloatBuffer);
223  complexVectorPtr += 4;
224 
225  cplxValue1 = _mm_mul_ps(cplxValue1, invScalar);
226  cplxValue2 = _mm_mul_ps(cplxValue2, invScalar);
227 
228  // Arrange in i1i2i3i4 format
229  iValue = _mm_shuffle_ps(cplxValue1, cplxValue2, _MM_SHUFFLE(2, 0, 2, 0));
230  // Arrange in q1q2q3q4 format
231  qValue = _mm_shuffle_ps(cplxValue1, cplxValue2, _MM_SHUFFLE(3, 1, 3, 1));
232 
233  iValue = _mm_mul_ps(iValue, iValue); // Square the I values
234  qValue = _mm_mul_ps(qValue, qValue); // Square the Q Values
235 
236  result = _mm_add_ps(iValue, qValue); // Add the I2 and Q2 values
237 
238  result = _mm_sqrt_ps(result); // Square root the values
239 
240  result = _mm_mul_ps(result, vScalar); // Scale the results
241 
242  _mm_store_ps(outputFloatBuffer, result);
243  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[0]);
244  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[1]);
245  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[2]);
246  *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[3]);
247  }
248 
249  number = quarterPoints * 4;
250  magnitudeVectorPtr = &magnitudeVector[number];
251  complexVectorPtr = (const int16_t*)&complexVector[number];
252  for (; number < num_points; number++) {
253  const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
254  const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
255  const float val1Result =
256  sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
257  *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
258  }
259 }
260 #endif /* LV_HAVE_SSE */
261 
262 #ifdef LV_HAVE_GENERIC
263 
264 static inline void volk_16ic_magnitude_16i_generic(int16_t* magnitudeVector,
265  const lv_16sc_t* complexVector,
266  unsigned int num_points)
267 {
268  const int16_t* complexVectorPtr = (const int16_t*)complexVector;
269  int16_t* magnitudeVectorPtr = magnitudeVector;
270  unsigned int number = 0;
271  const float scalar = SHRT_MAX;
272  for (number = 0; number < num_points; number++) {
273  float real = ((float)(*complexVectorPtr++)) / scalar;
274  float imag = ((float)(*complexVectorPtr++)) / scalar;
275  *magnitudeVectorPtr++ =
276  (int16_t)rintf(sqrtf((real * real) + (imag * imag)) * scalar);
277  }
278 }
279 #endif /* LV_HAVE_GENERIC */
280 
281 #ifdef LV_HAVE_ORC_DISABLED
282 extern void volk_16ic_magnitude_16i_a_orc_impl(int16_t* magnitudeVector,
283  const lv_16sc_t* complexVector,
284  float scalar,
285  unsigned int num_points);
286 
287 static inline void volk_16ic_magnitude_16i_u_orc(int16_t* magnitudeVector,
288  const lv_16sc_t* complexVector,
289  unsigned int num_points)
290 {
291  volk_16ic_magnitude_16i_a_orc_impl(
292  magnitudeVector, complexVector, SHRT_MAX, num_points);
293 }
294 #endif /* LV_HAVE_ORC */
295 
296 
297 #endif /* INCLUDED_volk_16ic_magnitude_16i_a_H */
298 
299 
300 #ifndef INCLUDED_volk_16ic_magnitude_16i_u_H
301 #define INCLUDED_volk_16ic_magnitude_16i_u_H
302 
303 #include <inttypes.h>
304 #include <math.h>
305 #include <stdio.h>
306 #include <volk/volk_common.h>
307 
308 #ifdef LV_HAVE_AVX2
309 #include <immintrin.h>
310 
311 static inline void volk_16ic_magnitude_16i_u_avx2(int16_t* magnitudeVector,
312  const lv_16sc_t* complexVector,
313  unsigned int num_points)
314 {
315  unsigned int number = 0;
316  const unsigned int eighthPoints = num_points / 8;
317 
318  const int16_t* complexVectorPtr = (const int16_t*)complexVector;
319  int16_t* magnitudeVectorPtr = magnitudeVector;
320 
321  __m256 vScalar = _mm256_set1_ps(SHRT_MAX);
322  __m256 invScalar = _mm256_set1_ps(1.0f / SHRT_MAX);
323  __m256i int1, int2;
324  __m128i short1, short2;
325  __m256 cplxValue1, cplxValue2, result;
326  __m256i idx = _mm256_set_epi32(0, 0, 0, 0, 5, 1, 4, 0);
327 
328  for (; number < eighthPoints; number++) {
329 
330  int1 = _mm256_loadu_si256((__m256i*)complexVectorPtr);
331  complexVectorPtr += 16;
332  short1 = _mm256_extracti128_si256(int1, 0);
333  short2 = _mm256_extracti128_si256(int1, 1);
334 
335  int1 = _mm256_cvtepi16_epi32(short1);
336  int2 = _mm256_cvtepi16_epi32(short2);
337  cplxValue1 = _mm256_cvtepi32_ps(int1);
338  cplxValue2 = _mm256_cvtepi32_ps(int2);
339 
340  cplxValue1 = _mm256_mul_ps(cplxValue1, invScalar);
341  cplxValue2 = _mm256_mul_ps(cplxValue2, invScalar);
342 
343  cplxValue1 = _mm256_mul_ps(cplxValue1, cplxValue1); // Square the values
344  cplxValue2 = _mm256_mul_ps(cplxValue2, cplxValue2); // Square the Values
345 
346  result = _mm256_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
347 
348  result = _mm256_sqrt_ps(result); // Square root the values
349 
350  result = _mm256_mul_ps(result, vScalar); // Scale the results
351 
352  int1 = _mm256_cvtps_epi32(result);
353  int1 = _mm256_packs_epi32(int1, int1);
354  int1 = _mm256_permutevar8x32_epi32(
355  int1, idx); // permute to compensate for shuffling in hadd and packs
356  short1 = _mm256_extracti128_si256(int1, 0);
357  _mm_storeu_si128((__m128i*)magnitudeVectorPtr, short1);
358  magnitudeVectorPtr += 8;
359  }
360 
361  number = eighthPoints * 8;
362  magnitudeVectorPtr = &magnitudeVector[number];
363  complexVectorPtr = (const int16_t*)&complexVector[number];
364  for (; number < num_points; number++) {
365  const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
366  const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
367  const float val1Result =
368  sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
369  *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
370  }
371 }
372 #endif /* LV_HAVE_AVX2 */
373 
374 #ifdef LV_HAVE_NEONV7
375 #include <arm_neon.h>
377 
378 static inline void volk_16ic_magnitude_16i_neonv7(int16_t* magnitudeVector,
379  const lv_16sc_t* complexVector,
380  unsigned int num_points)
381 {
382  unsigned int number = 0;
383  unsigned int quarter_points = num_points / 4;
384 
385  const float scalar = SHRT_MAX;
386  const float inv_scalar = 1.0f / scalar;
387 
388  int16_t* magnitudeVectorPtr = magnitudeVector;
389  const lv_16sc_t* complexVectorPtr = complexVector;
390 
391  float32x4_t mag_vec;
392  float32x4x2_t c_vec;
393 
394  for (number = 0; number < quarter_points; number++) {
395  const int16x4x2_t c16_vec = vld2_s16((int16_t*)complexVectorPtr);
396  __VOLK_PREFETCH(complexVectorPtr + 4);
397  c_vec.val[0] = vcvtq_f32_s32(vmovl_s16(c16_vec.val[0]));
398  c_vec.val[1] = vcvtq_f32_s32(vmovl_s16(c16_vec.val[1]));
399  // Scale to close to 0-1
400  c_vec.val[0] = vmulq_n_f32(c_vec.val[0], inv_scalar);
401  c_vec.val[1] = vmulq_n_f32(c_vec.val[1], inv_scalar);
402  // vsqrtq_f32 is armv8
403  const float32x4_t mag_vec_squared = _vmagnitudesquaredq_f32(c_vec);
404  mag_vec = vmulq_f32(mag_vec_squared, _vinvsqrtq_f32(mag_vec_squared));
405  // Reconstruct
406  mag_vec = vmulq_n_f32(mag_vec, scalar);
407  // Add 0.5 for correct rounding because vcvtq_s32_f32 truncates.
408  // This works because the magnitude is always positive.
409  mag_vec = vaddq_f32(mag_vec, vdupq_n_f32(0.5));
410  const int16x4_t mag16_vec = vmovn_s32(vcvtq_s32_f32(mag_vec));
411  vst1_s16(magnitudeVectorPtr, mag16_vec);
412  // Advance pointers
413  magnitudeVectorPtr += 4;
414  complexVectorPtr += 4;
415  }
416 
417  // Deal with the rest
418  for (number = quarter_points * 4; number < num_points; number++) {
419  const float real = lv_creal(*complexVectorPtr) * inv_scalar;
420  const float imag = lv_cimag(*complexVectorPtr) * inv_scalar;
421  *magnitudeVectorPtr =
422  (int16_t)rintf(sqrtf((real * real) + (imag * imag)) * scalar);
423  complexVectorPtr++;
424  magnitudeVectorPtr++;
425  }
426 }
427 #endif /* LV_HAVE_NEONV7 */
428 
429 #endif /* INCLUDED_volk_16ic_magnitude_16i_u_H */
static float rintf(float x)
Definition: config.h:45
FORCE_INLINE void _mm_store_si128(__m128i *p, __m128i a)
Definition: sse2neon.h:5937
float32x4_t __m128
Definition: sse2neon.h:235
#define _mm_shuffle_ps(a, b, imm)
Definition: sse2neon.h:2586
FORCE_INLINE __m128 _mm_hadd_ps(__m128 a, __m128 b)
Definition: sse2neon.h:6527
FORCE_INLINE __m128 _mm_mul_ps(__m128 a, __m128 b)
Definition: sse2neon.h:2205
FORCE_INLINE __m128 _mm_set_ps1(float)
Definition: sse2neon.h:2437
FORCE_INLINE void _mm_storeu_si128(__m128i *p, __m128i a)
Definition: sse2neon.h:6010
FORCE_INLINE __m128 _mm_add_ps(__m128 a, __m128 b)
Definition: sse2neon.h:1039
#define _MM_SHUFFLE(fp3, fp2, fp1, fp0)
Definition: sse2neon.h:195
FORCE_INLINE __m128 _mm_load_ps(const float *p)
Definition: sse2neon.h:1858
int64x2_t __m128i
Definition: sse2neon.h:244
FORCE_INLINE void _mm_store_ps(float *p, __m128 a)
Definition: sse2neon.h:2704
FORCE_INLINE __m128 _mm_sqrt_ps(__m128 in)
Definition: sse2neon.h:2659
static void volk_16ic_magnitude_16i_generic(int16_t *magnitudeVector, const lv_16sc_t *complexVector, unsigned int num_points)
Definition: volk_16ic_magnitude_16i.h:264
static void volk_16ic_magnitude_16i_a_sse(int16_t *magnitudeVector, const lv_16sc_t *complexVector, unsigned int num_points)
Definition: volk_16ic_magnitude_16i.h:189
static void volk_16ic_magnitude_16i_a_sse3(int16_t *magnitudeVector, const lv_16sc_t *complexVector, unsigned int num_points)
Definition: volk_16ic_magnitude_16i.h:119
#define __VOLK_PREFETCH(addr)
Definition: volk_common.h:71
#define __VOLK_ATTR_ALIGNED(x)
Definition: volk_common.h:65
#define lv_cimag(x)
Definition: volk_complex.h:98
#define lv_creal(x)
Definition: volk_complex.h:96
short complex lv_16sc_t
Definition: volk_complex.h:71
static float32x4_t _vinvsqrtq_f32(float32x4_t x)
Definition: volk_neon_intrinsics.h:83
static float32x4_t _vmagnitudesquaredq_f32(float32x4x2_t cmplxValue)
Definition: volk_neon_intrinsics.h:73