Vector Optimized Library of Kernels 3.2.0
Architecture-tuned implementations of math kernels
Loading...
Searching...
No Matches
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
40
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
53static 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
119static 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
189static 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
264static 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
282#endif /* INCLUDED_volk_16ic_magnitude_16i_a_H */
283
284
285#ifndef INCLUDED_volk_16ic_magnitude_16i_u_H
286#define INCLUDED_volk_16ic_magnitude_16i_u_H
287
288#include <inttypes.h>
289#include <math.h>
290#include <stdio.h>
291#include <volk/volk_common.h>
292
293#ifdef LV_HAVE_AVX2
294#include <immintrin.h>
295
296static inline void volk_16ic_magnitude_16i_u_avx2(int16_t* magnitudeVector,
297 const lv_16sc_t* complexVector,
298 unsigned int num_points)
299{
300 unsigned int number = 0;
301 const unsigned int eighthPoints = num_points / 8;
302
303 const int16_t* complexVectorPtr = (const int16_t*)complexVector;
304 int16_t* magnitudeVectorPtr = magnitudeVector;
305
306 __m256 vScalar = _mm256_set1_ps(SHRT_MAX);
307 __m256 invScalar = _mm256_set1_ps(1.0f / SHRT_MAX);
308 __m256i int1, int2;
309 __m128i short1, short2;
310 __m256 cplxValue1, cplxValue2, result;
311 __m256i idx = _mm256_set_epi32(0, 0, 0, 0, 5, 1, 4, 0);
312
313 for (; number < eighthPoints; number++) {
314
315 int1 = _mm256_loadu_si256((__m256i*)complexVectorPtr);
316 complexVectorPtr += 16;
317 short1 = _mm256_extracti128_si256(int1, 0);
318 short2 = _mm256_extracti128_si256(int1, 1);
319
320 int1 = _mm256_cvtepi16_epi32(short1);
321 int2 = _mm256_cvtepi16_epi32(short2);
322 cplxValue1 = _mm256_cvtepi32_ps(int1);
323 cplxValue2 = _mm256_cvtepi32_ps(int2);
324
325 cplxValue1 = _mm256_mul_ps(cplxValue1, invScalar);
326 cplxValue2 = _mm256_mul_ps(cplxValue2, invScalar);
327
328 cplxValue1 = _mm256_mul_ps(cplxValue1, cplxValue1); // Square the values
329 cplxValue2 = _mm256_mul_ps(cplxValue2, cplxValue2); // Square the Values
330
331 result = _mm256_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
332
333 result = _mm256_sqrt_ps(result); // Square root the values
334
335 result = _mm256_mul_ps(result, vScalar); // Scale the results
336
337 int1 = _mm256_cvtps_epi32(result);
338 int1 = _mm256_packs_epi32(int1, int1);
339 int1 = _mm256_permutevar8x32_epi32(
340 int1, idx); // permute to compensate for shuffling in hadd and packs
341 short1 = _mm256_extracti128_si256(int1, 0);
342 _mm_storeu_si128((__m128i*)magnitudeVectorPtr, short1);
343 magnitudeVectorPtr += 8;
344 }
345
346 number = eighthPoints * 8;
347 magnitudeVectorPtr = &magnitudeVector[number];
348 complexVectorPtr = (const int16_t*)&complexVector[number];
349 for (; number < num_points; number++) {
350 const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
351 const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
352 const float val1Result =
353 sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
354 *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
355 }
356}
357#endif /* LV_HAVE_AVX2 */
358
359#ifdef LV_HAVE_NEONV7
360#include <arm_neon.h>
362
363static inline void volk_16ic_magnitude_16i_neonv7(int16_t* magnitudeVector,
364 const lv_16sc_t* complexVector,
365 unsigned int num_points)
366{
367 unsigned int number = 0;
368 unsigned int quarter_points = num_points / 4;
369
370 const float scalar = SHRT_MAX;
371 const float inv_scalar = 1.0f / scalar;
372
373 int16_t* magnitudeVectorPtr = magnitudeVector;
374 const lv_16sc_t* complexVectorPtr = complexVector;
375
376 float32x4_t mag_vec;
377 float32x4x2_t c_vec;
378
379 for (number = 0; number < quarter_points; number++) {
380 const int16x4x2_t c16_vec = vld2_s16((int16_t*)complexVectorPtr);
381 __VOLK_PREFETCH(complexVectorPtr + 4);
382 c_vec.val[0] = vcvtq_f32_s32(vmovl_s16(c16_vec.val[0]));
383 c_vec.val[1] = vcvtq_f32_s32(vmovl_s16(c16_vec.val[1]));
384 // Scale to close to 0-1
385 c_vec.val[0] = vmulq_n_f32(c_vec.val[0], inv_scalar);
386 c_vec.val[1] = vmulq_n_f32(c_vec.val[1], inv_scalar);
387 // vsqrtq_f32 is armv8
388 const float32x4_t mag_vec_squared = _vmagnitudesquaredq_f32(c_vec);
389 mag_vec = vmulq_f32(mag_vec_squared, _vinvsqrtq_f32(mag_vec_squared));
390 // Reconstruct
391 mag_vec = vmulq_n_f32(mag_vec, scalar);
392 // Add 0.5 for correct rounding because vcvtq_s32_f32 truncates.
393 // This works because the magnitude is always positive.
394 mag_vec = vaddq_f32(mag_vec, vdupq_n_f32(0.5));
395 const int16x4_t mag16_vec = vmovn_s32(vcvtq_s32_f32(mag_vec));
396 vst1_s16(magnitudeVectorPtr, mag16_vec);
397 // Advance pointers
398 magnitudeVectorPtr += 4;
399 complexVectorPtr += 4;
400 }
401
402 // Deal with the rest
403 for (number = quarter_points * 4; number < num_points; number++) {
404 const float real = lv_creal(*complexVectorPtr) * inv_scalar;
405 const float imag = lv_cimag(*complexVectorPtr) * inv_scalar;
406 *magnitudeVectorPtr =
407 (int16_t)rintf(sqrtf((real * real) + (imag * imag)) * scalar);
408 complexVectorPtr++;
409 magnitudeVectorPtr++;
410 }
411}
412#endif /* LV_HAVE_NEONV7 */
413
414#ifdef LV_HAVE_RVV
415#include <riscv_vector.h>
416
417static inline void volk_16ic_magnitude_16i_rvv(int16_t* magnitudeVector,
418 const lv_16sc_t* complexVector,
419 unsigned int num_points)
420{
421 const float scale = SHRT_MAX, iscale = 1.0f / scale;
422 size_t n = num_points;
423 for (size_t vl; n > 0; n -= vl, complexVector += vl, magnitudeVector += vl) {
424 vl = __riscv_vsetvl_e16m4(n);
425 vint32m8_t vc = __riscv_vle32_v_i32m8((const int32_t*)complexVector, vl);
426 vint16m4_t vr = __riscv_vnsra(vc, 0, vl);
427 vint16m4_t vi = __riscv_vnsra(vc, 16, vl);
428 vfloat32m8_t vrf = __riscv_vfmul(__riscv_vfwcvt_f(vr, vl), iscale, vl);
429 vfloat32m8_t vif = __riscv_vfmul(__riscv_vfwcvt_f(vi, vl), iscale, vl);
430 vfloat32m8_t vf = __riscv_vfmacc(__riscv_vfmul(vif, vif, vl), vrf, vrf, vl);
431 vf = __riscv_vfmul(__riscv_vfsqrt(vf, vl), scale, vl);
432 __riscv_vse16(magnitudeVector, __riscv_vfncvt_x(vf, vl), vl);
433 }
434}
435#endif /*LV_HAVE_RVV*/
436
437#ifdef LV_HAVE_RVVSEG
438#include <riscv_vector.h>
439
440static inline void volk_16ic_magnitude_16i_rvvseg(int16_t* magnitudeVector,
441 const lv_16sc_t* complexVector,
442 unsigned int num_points)
443{
444 const float scale = SHRT_MAX, iscale = 1.0f / scale;
445 size_t n = num_points;
446 for (size_t vl; n > 0; n -= vl, complexVector += vl, magnitudeVector += vl) {
447 vl = __riscv_vsetvl_e16m4(n);
448 vint16m4x2_t vc = __riscv_vlseg2e16_v_i16m4x2((const int16_t*)complexVector, vl);
449 vint16m4_t vr = __riscv_vget_i16m4(vc, 0);
450 vint16m4_t vi = __riscv_vget_i16m4(vc, 1);
451 vfloat32m8_t vrf = __riscv_vfmul(__riscv_vfwcvt_f(vr, vl), iscale, vl);
452 vfloat32m8_t vif = __riscv_vfmul(__riscv_vfwcvt_f(vi, vl), iscale, vl);
453 vfloat32m8_t vf = __riscv_vfmacc(__riscv_vfmul(vif, vif, vl), vrf, vrf, vl);
454 vf = __riscv_vfmul(__riscv_vfsqrt(vf, vl), scale, vl);
455 __riscv_vse16(magnitudeVector, __riscv_vfncvt_x(vf, vl), vl);
456 }
457}
458#endif /*LV_HAVE_RVVSEG*/
459
460#endif /* INCLUDED_volk_16ic_magnitude_16i_u_H */
static float rintf(float x)
Definition config.h:45
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:68
#define __VOLK_ATTR_ALIGNED(x)
Definition volk_common.h:62
#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