Vector Optimized Library of Kernels  2.0
Architecture-tuned implementations of math kernels
volk_avx_intrinsics.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015 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  * This file is intended to hold AVX intrinsics of intrinsics.
25  * They should be used in VOLK kernels to avoid copy-pasta.
26  */
27 
28 #ifndef INCLUDE_VOLK_VOLK_AVX_INTRINSICS_H_
29 #define INCLUDE_VOLK_VOLK_AVX_INTRINSICS_H_
30 #include <immintrin.h>
31 
32 static inline __m256
33 _mm256_complexmul_ps(__m256 x, __m256 y)
34 {
35  __m256 yl, yh, tmp1, tmp2;
36  yl = _mm256_moveldup_ps(y); // Load yl with cr,cr,dr,dr ...
37  yh = _mm256_movehdup_ps(y); // Load yh with ci,ci,di,di ...
38  tmp1 = _mm256_mul_ps(x, yl); // tmp1 = ar*cr,ai*cr,br*dr,bi*dr ...
39  x = _mm256_shuffle_ps(x, x, 0xB1); // Re-arrange x to be ai,ar,bi,br ...
40  tmp2 = _mm256_mul_ps(x, yh); // tmp2 = ai*ci,ar*ci,bi*di,br*di
41  return _mm256_addsub_ps(tmp1, tmp2); // ar*cr-ai*ci, ai*cr+ar*ci, br*dr-bi*di, bi*dr+br*di
42 }
43 
44 static inline __m256
46  const __m256 conjugator = _mm256_setr_ps(0, -0.f, 0, -0.f, 0, -0.f, 0, -0.f);
47  return _mm256_xor_ps(x, conjugator); // conjugate y
48 }
49 
50 static inline __m256
51 _mm256_complexconjugatemul_ps(__m256 x, __m256 y){
52  y = _mm256_conjugate_ps(y);
53  return _mm256_complexmul_ps(x, y);
54 }
55 
56 static inline __m256
57 _mm256_magnitudesquared_ps(__m256 cplxValue1, __m256 cplxValue2){
58  __m256 complex1, complex2;
59  cplxValue1 = _mm256_mul_ps(cplxValue1, cplxValue1); // Square the values
60  cplxValue2 = _mm256_mul_ps(cplxValue2, cplxValue2); // Square the Values
61  complex1 = _mm256_permute2f128_ps(cplxValue1, cplxValue2, 0x20);
62  complex2 = _mm256_permute2f128_ps(cplxValue1, cplxValue2, 0x31);
63  return _mm256_hadd_ps(complex1, complex2); // Add the I2 and Q2 values
64 }
65 
66 static inline __m256
67 _mm256_magnitude_ps(__m256 cplxValue1, __m256 cplxValue2){
68  return _mm256_sqrt_ps(_mm256_magnitudesquared_ps(cplxValue1, cplxValue2));
69 }
70 
71 static inline __m256
72 _mm256_polar_sign_mask(__m128i fbits){
73  __m256 sign_mask_dummy = _mm256_setzero_ps();
74  const __m128i zeros = _mm_set1_epi8(0x00);
75  const __m128i sign_extract = _mm_set1_epi8(0x80);
76  const __m128i shuffle_mask0 = _mm_setr_epi8(0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0x02, 0xff, 0xff, 0xff, 0x03);
77  const __m128i shuffle_mask1 = _mm_setr_epi8(0xff, 0xff, 0xff, 0x04, 0xff, 0xff, 0xff, 0x05, 0xff, 0xff, 0xff, 0x06, 0xff, 0xff, 0xff, 0x07);
78 
79  fbits = _mm_cmpgt_epi8(fbits, zeros);
80  fbits = _mm_and_si128(fbits, sign_extract);
81  __m128i sign_bits0 = _mm_shuffle_epi8(fbits, shuffle_mask0);
82  __m128i sign_bits1 = _mm_shuffle_epi8(fbits, shuffle_mask1);
83 
84  __m256 sign_mask = _mm256_insertf128_ps(sign_mask_dummy, _mm_castsi128_ps(sign_bits0), 0x0);
85  return _mm256_insertf128_ps(sign_mask, _mm_castsi128_ps(sign_bits1), 0x1);
86 // // This is the desired function call. Though it seems to be missing in GCC.
87 // // Compare: https://software.intel.com/sites/landingpage/IntrinsicsGuide/#
88 // return _mm256_set_m128(_mm_castsi128_ps(sign_bits1), _mm_castsi128_ps(sign_bits0));
89 }
90 
91 static inline void
92 _mm256_polar_deinterleave(__m256 *llr0, __m256 *llr1, __m256 src0, __m256 src1){
93  // deinterleave values
94  __m256 part0 = _mm256_permute2f128_ps(src0, src1, 0x20);
95  __m256 part1 = _mm256_permute2f128_ps(src0, src1, 0x31);
96  *llr0 = _mm256_shuffle_ps(part0, part1, 0x88);
97  *llr1 = _mm256_shuffle_ps(part0, part1, 0xdd);
98 }
99 
100 static inline __m256
101 _mm256_polar_minsum_llrs(__m256 src0, __m256 src1){
102  const __m256 sign_mask = _mm256_set1_ps(-0.0f);
103  const __m256 abs_mask = _mm256_andnot_ps(sign_mask, _mm256_castsi256_ps(_mm256_set1_epi8(0xff)));
104 
105  __m256 llr0, llr1;
106  _mm256_polar_deinterleave(&llr0, &llr1, src0, src1);
107 
108  // calculate result
109  __m256 sign = _mm256_xor_ps(_mm256_and_ps(llr0, sign_mask), _mm256_and_ps(llr1, sign_mask));
110  __m256 dst = _mm256_min_ps(_mm256_and_ps(llr0, abs_mask), _mm256_and_ps(llr1, abs_mask));
111  return _mm256_or_ps(dst, sign);
112 }
113 
114 static inline __m256
115 _mm256_polar_fsign_add_llrs(__m256 src0, __m256 src1, __m128i fbits){
116  // prepare sign mask for correct +-
117  __m256 sign_mask = _mm256_polar_sign_mask(fbits);
118 
119  __m256 llr0, llr1;
120  _mm256_polar_deinterleave(&llr0, &llr1, src0, src1);
121 
122  // calculate result
123  llr0 = _mm256_xor_ps(llr0, sign_mask);
124  __m256 dst = _mm256_add_ps(llr0, llr1);
125  return dst;
126 }
127 
128 #endif /* INCLUDE_VOLK_VOLK_AVX_INTRINSICS_H_ */
static void _mm256_polar_deinterleave(__m256 *llr0, __m256 *llr1, __m256 src0, __m256 src1)
Definition: volk_avx_intrinsics.h:92
static __m256 _mm256_complexmul_ps(__m256 x, __m256 y)
Definition: volk_avx_intrinsics.h:33
static __m256 _mm256_complexconjugatemul_ps(__m256 x, __m256 y)
Definition: volk_avx_intrinsics.h:51
static __m256 _mm256_conjugate_ps(__m256 x)
Definition: volk_avx_intrinsics.h:45
static __m256 _mm256_magnitude_ps(__m256 cplxValue1, __m256 cplxValue2)
Definition: volk_avx_intrinsics.h:67
static __m256 _mm256_polar_minsum_llrs(__m256 src0, __m256 src1)
Definition: volk_avx_intrinsics.h:101
static __m256 _mm256_magnitudesquared_ps(__m256 cplxValue1, __m256 cplxValue2)
Definition: volk_avx_intrinsics.h:57
static __m256 _mm256_polar_sign_mask(__m128i fbits)
Definition: volk_avx_intrinsics.h:72
static __m256 _mm256_polar_fsign_add_llrs(__m256 src0, __m256 src1, __m128i fbits)
Definition: volk_avx_intrinsics.h:115