GCC Code Coverage Report


Directory: ./
File: kernels/volk/volk_16ic_magnitude_16i.h
Date: 2023-10-23 23:10:04
Exec Total Coverage
Lines: 172 172 100.0%
Functions: 5 5 100.0%
Branches: 18 18 100.0%

Line Branch Exec Source
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
10 /*!
11 * \page volk_16ic_magnitude_16i
12 *
13 * \b Overview
14 *
15 * Computes the magnitude of the complexVector and stores the results
16 * in the magnitudeVector.
17 *
18 * <b>Dispatcher Prototype</b>
19 * \code
20 * void volk_16ic_magnitude_16i(int16_t* magnitudeVector, const lv_16sc_t* complexVector,
21 * unsigned int num_points) \endcode
22 *
23 * \b Inputs
24 * \li complexVector: The complex input vector.
25 * \li num_points: The number of samples.
26 *
27 * \b Outputs
28 * \li magnitudeVector: The magnitude of the complex values.
29 *
30 * \b Example
31 * \code
32 * int N = 10000;
33 *
34 * volk_16ic_magnitude_16i();
35 *
36 * volk_free(x);
37 * volk_free(t);
38 * \endcode
39 */
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
53 2 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 2 unsigned int number = 0;
58 2 const unsigned int eighthPoints = num_points / 8;
59
60 2 const int16_t* complexVectorPtr = (const int16_t*)complexVector;
61 2 int16_t* magnitudeVectorPtr = magnitudeVector;
62
63 2 __m256 vScalar = _mm256_set1_ps(SHRT_MAX);
64 2 __m256 invScalar = _mm256_set1_ps(1.0f / SHRT_MAX);
65 __m256i int1, int2;
66 __m128i short1, short2;
67 __m256 cplxValue1, cplxValue2, result;
68 2 __m256i idx = _mm256_set_epi32(0, 0, 0, 0, 5, 1, 4, 0);
69
70
2/2
✓ Branch 0 taken 32766 times.
✓ Branch 1 taken 2 times.
32768 for (; number < eighthPoints; number++) {
71
72 32766 int1 = _mm256_load_si256((__m256i*)complexVectorPtr);
73 32766 complexVectorPtr += 16;
74 32766 short1 = _mm256_extracti128_si256(int1, 0);
75 32766 short2 = _mm256_extracti128_si256(int1, 1);
76
77 32766 int1 = _mm256_cvtepi16_epi32(short1);
78 32766 int2 = _mm256_cvtepi16_epi32(short2);
79 32766 cplxValue1 = _mm256_cvtepi32_ps(int1);
80 32766 cplxValue2 = _mm256_cvtepi32_ps(int2);
81
82 32766 cplxValue1 = _mm256_mul_ps(cplxValue1, invScalar);
83 32766 cplxValue2 = _mm256_mul_ps(cplxValue2, invScalar);
84
85 32766 cplxValue1 = _mm256_mul_ps(cplxValue1, cplxValue1); // Square the values
86 32766 cplxValue2 = _mm256_mul_ps(cplxValue2, cplxValue2); // Square the Values
87
88 32766 result = _mm256_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
89
90 32766 result = _mm256_sqrt_ps(result); // Square root the values
91
92 32766 result = _mm256_mul_ps(result, vScalar); // Scale the results
93
94 32766 int1 = _mm256_cvtps_epi32(result);
95 32766 int1 = _mm256_packs_epi32(int1, int1);
96 32766 int1 = _mm256_permutevar8x32_epi32(
97 int1, idx); // permute to compensate for shuffling in hadd and packs
98 32766 short1 = _mm256_extracti128_si256(int1, 0);
99 _mm_store_si128((__m128i*)magnitudeVectorPtr, short1);
100 32766 magnitudeVectorPtr += 8;
101 }
102
103 2 number = eighthPoints * 8;
104 2 magnitudeVectorPtr = &magnitudeVector[number];
105 2 complexVectorPtr = (const int16_t*)&complexVector[number];
106
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 for (; number < num_points; number++) {
107 14 const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
108 14 const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
109 14 const float val1Result =
110 14 sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
111 14 *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
112 }
113 2 }
114 #endif /* LV_HAVE_AVX2 */
115
116 #ifdef LV_HAVE_SSE3
117 #include <pmmintrin.h>
118
119 2 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 2 unsigned int number = 0;
124 2 const unsigned int quarterPoints = num_points / 4;
125
126 2 const int16_t* complexVectorPtr = (const int16_t*)complexVector;
127 2 int16_t* magnitudeVectorPtr = magnitudeVector;
128
129 2 __m128 vScalar = _mm_set_ps1(SHRT_MAX);
130 2 __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
2/2
✓ Branch 0 taken 65534 times.
✓ Branch 1 taken 2 times.
65536 for (; number < quarterPoints; number++) {
138
139 65534 inputFloatBuffer[0] = (float)(complexVectorPtr[0]);
140 65534 inputFloatBuffer[1] = (float)(complexVectorPtr[1]);
141 65534 inputFloatBuffer[2] = (float)(complexVectorPtr[2]);
142 65534 inputFloatBuffer[3] = (float)(complexVectorPtr[3]);
143
144 65534 inputFloatBuffer[4] = (float)(complexVectorPtr[4]);
145 65534 inputFloatBuffer[5] = (float)(complexVectorPtr[5]);
146 65534 inputFloatBuffer[6] = (float)(complexVectorPtr[6]);
147 65534 inputFloatBuffer[7] = (float)(complexVectorPtr[7]);
148
149 65534 cplxValue1 = _mm_load_ps(&inputFloatBuffer[0]);
150 65534 cplxValue2 = _mm_load_ps(&inputFloatBuffer[4]);
151
152 65534 complexVectorPtr += 8;
153
154 65534 cplxValue1 = _mm_mul_ps(cplxValue1, invScalar);
155 65534 cplxValue2 = _mm_mul_ps(cplxValue2, invScalar);
156
157 65534 cplxValue1 = _mm_mul_ps(cplxValue1, cplxValue1); // Square the values
158 65534 cplxValue2 = _mm_mul_ps(cplxValue2, cplxValue2); // Square the Values
159
160 65534 result = _mm_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
161
162 65534 result = _mm_sqrt_ps(result); // Square root the values
163
164 65534 result = _mm_mul_ps(result, vScalar); // Scale the results
165
166 _mm_store_ps(outputFloatBuffer, result);
167 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[0]);
168 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[1]);
169 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[2]);
170 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[3]);
171 }
172
173 2 number = quarterPoints * 4;
174 2 magnitudeVectorPtr = &magnitudeVector[number];
175 2 complexVectorPtr = (const int16_t*)&complexVector[number];
176
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (; number < num_points; number++) {
177 6 const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
178 6 const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
179 6 const float val1Result =
180 6 sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
181 6 *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
182 }
183 2 }
184 #endif /* LV_HAVE_SSE3 */
185
186 #ifdef LV_HAVE_SSE
187 #include <xmmintrin.h>
188
189 2 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 2 unsigned int number = 0;
194 2 const unsigned int quarterPoints = num_points / 4;
195
196 2 const int16_t* complexVectorPtr = (const int16_t*)complexVector;
197 2 int16_t* magnitudeVectorPtr = magnitudeVector;
198
199 2 __m128 vScalar = _mm_set_ps1(SHRT_MAX);
200 2 __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
2/2
✓ Branch 0 taken 65534 times.
✓ Branch 1 taken 2 times.
65536 for (; number < quarterPoints; number++) {
208
209 65534 inputFloatBuffer[0] = (float)(complexVectorPtr[0]);
210 65534 inputFloatBuffer[1] = (float)(complexVectorPtr[1]);
211 65534 inputFloatBuffer[2] = (float)(complexVectorPtr[2]);
212 65534 inputFloatBuffer[3] = (float)(complexVectorPtr[3]);
213
214 65534 cplxValue1 = _mm_load_ps(inputFloatBuffer);
215 65534 complexVectorPtr += 4;
216
217 65534 inputFloatBuffer[0] = (float)(complexVectorPtr[0]);
218 65534 inputFloatBuffer[1] = (float)(complexVectorPtr[1]);
219 65534 inputFloatBuffer[2] = (float)(complexVectorPtr[2]);
220 65534 inputFloatBuffer[3] = (float)(complexVectorPtr[3]);
221
222 65534 cplxValue2 = _mm_load_ps(inputFloatBuffer);
223 65534 complexVectorPtr += 4;
224
225 65534 cplxValue1 = _mm_mul_ps(cplxValue1, invScalar);
226 65534 cplxValue2 = _mm_mul_ps(cplxValue2, invScalar);
227
228 // Arrange in i1i2i3i4 format
229 65534 iValue = _mm_shuffle_ps(cplxValue1, cplxValue2, _MM_SHUFFLE(2, 0, 2, 0));
230 // Arrange in q1q2q3q4 format
231 65534 qValue = _mm_shuffle_ps(cplxValue1, cplxValue2, _MM_SHUFFLE(3, 1, 3, 1));
232
233 65534 iValue = _mm_mul_ps(iValue, iValue); // Square the I values
234 65534 qValue = _mm_mul_ps(qValue, qValue); // Square the Q Values
235
236 65534 result = _mm_add_ps(iValue, qValue); // Add the I2 and Q2 values
237
238 65534 result = _mm_sqrt_ps(result); // Square root the values
239
240 65534 result = _mm_mul_ps(result, vScalar); // Scale the results
241
242 _mm_store_ps(outputFloatBuffer, result);
243 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[0]);
244 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[1]);
245 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[2]);
246 65534 *magnitudeVectorPtr++ = (int16_t)rintf(outputFloatBuffer[3]);
247 }
248
249 2 number = quarterPoints * 4;
250 2 magnitudeVectorPtr = &magnitudeVector[number];
251 2 complexVectorPtr = (const int16_t*)&complexVector[number];
252
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (; number < num_points; number++) {
253 6 const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
254 6 const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
255 6 const float val1Result =
256 6 sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
257 6 *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
258 }
259 2 }
260 #endif /* LV_HAVE_SSE */
261
262 #ifdef LV_HAVE_GENERIC
263
264 2 static inline void volk_16ic_magnitude_16i_generic(int16_t* magnitudeVector,
265 const lv_16sc_t* complexVector,
266 unsigned int num_points)
267 {
268 2 const int16_t* complexVectorPtr = (const int16_t*)complexVector;
269 2 int16_t* magnitudeVectorPtr = magnitudeVector;
270 2 unsigned int number = 0;
271 2 const float scalar = SHRT_MAX;
272
2/2
✓ Branch 0 taken 262142 times.
✓ Branch 1 taken 2 times.
262144 for (number = 0; number < num_points; number++) {
273 262142 float real = ((float)(*complexVectorPtr++)) / scalar;
274 262142 float imag = ((float)(*complexVectorPtr++)) / scalar;
275 262142 *magnitudeVectorPtr++ =
276 262142 (int16_t)rintf(sqrtf((real * real) + (imag * imag)) * scalar);
277 }
278 2 }
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
296 2 static inline void volk_16ic_magnitude_16i_u_avx2(int16_t* magnitudeVector,
297 const lv_16sc_t* complexVector,
298 unsigned int num_points)
299 {
300 2 unsigned int number = 0;
301 2 const unsigned int eighthPoints = num_points / 8;
302
303 2 const int16_t* complexVectorPtr = (const int16_t*)complexVector;
304 2 int16_t* magnitudeVectorPtr = magnitudeVector;
305
306 2 __m256 vScalar = _mm256_set1_ps(SHRT_MAX);
307 2 __m256 invScalar = _mm256_set1_ps(1.0f / SHRT_MAX);
308 __m256i int1, int2;
309 __m128i short1, short2;
310 __m256 cplxValue1, cplxValue2, result;
311 2 __m256i idx = _mm256_set_epi32(0, 0, 0, 0, 5, 1, 4, 0);
312
313
2/2
✓ Branch 0 taken 32766 times.
✓ Branch 1 taken 2 times.
32768 for (; number < eighthPoints; number++) {
314
315 32766 int1 = _mm256_loadu_si256((__m256i*)complexVectorPtr);
316 32766 complexVectorPtr += 16;
317 32766 short1 = _mm256_extracti128_si256(int1, 0);
318 32766 short2 = _mm256_extracti128_si256(int1, 1);
319
320 32766 int1 = _mm256_cvtepi16_epi32(short1);
321 32766 int2 = _mm256_cvtepi16_epi32(short2);
322 32766 cplxValue1 = _mm256_cvtepi32_ps(int1);
323 32766 cplxValue2 = _mm256_cvtepi32_ps(int2);
324
325 32766 cplxValue1 = _mm256_mul_ps(cplxValue1, invScalar);
326 32766 cplxValue2 = _mm256_mul_ps(cplxValue2, invScalar);
327
328 32766 cplxValue1 = _mm256_mul_ps(cplxValue1, cplxValue1); // Square the values
329 32766 cplxValue2 = _mm256_mul_ps(cplxValue2, cplxValue2); // Square the Values
330
331 32766 result = _mm256_hadd_ps(cplxValue1, cplxValue2); // Add the I2 and Q2 values
332
333 32766 result = _mm256_sqrt_ps(result); // Square root the values
334
335 32766 result = _mm256_mul_ps(result, vScalar); // Scale the results
336
337 32766 int1 = _mm256_cvtps_epi32(result);
338 32766 int1 = _mm256_packs_epi32(int1, int1);
339 32766 int1 = _mm256_permutevar8x32_epi32(
340 int1, idx); // permute to compensate for shuffling in hadd and packs
341 32766 short1 = _mm256_extracti128_si256(int1, 0);
342 _mm_storeu_si128((__m128i*)magnitudeVectorPtr, short1);
343 32766 magnitudeVectorPtr += 8;
344 }
345
346 2 number = eighthPoints * 8;
347 2 magnitudeVectorPtr = &magnitudeVector[number];
348 2 complexVectorPtr = (const int16_t*)&complexVector[number];
349
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 for (; number < num_points; number++) {
350 14 const float val1Real = (float)(*complexVectorPtr++) / SHRT_MAX;
351 14 const float val1Imag = (float)(*complexVectorPtr++) / SHRT_MAX;
352 14 const float val1Result =
353 14 sqrtf((val1Real * val1Real) + (val1Imag * val1Imag)) * SHRT_MAX;
354 14 *magnitudeVectorPtr++ = (int16_t)rintf(val1Result);
355 }
356 2 }
357 #endif /* LV_HAVE_AVX2 */
358
359 #ifdef LV_HAVE_NEONV7
360 #include <arm_neon.h>
361 #include <volk/volk_neon_intrinsics.h>
362
363 static 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 #endif /* INCLUDED_volk_16ic_magnitude_16i_u_H */
415