GNU Radio 3.6.5 C++ API

volk_32fc_x2_dot_prod_32fc_a.h

Go to the documentation of this file.
00001 #ifndef INCLUDED_volk_32fc_x2_dot_prod_32fc_a_H
00002 #define INCLUDED_volk_32fc_x2_dot_prod_32fc_a_H
00003 
00004 #include <volk/volk_common.h>
00005 #include <volk/volk_complex.h>
00006 #include <stdio.h>
00007 #include <string.h>
00008 
00009 
00010 #ifdef LV_HAVE_GENERIC
00011 
00012 
00013 static inline void volk_32fc_x2_dot_prod_32fc_a_generic(lv_32fc_t* result, const lv_32fc_t* input, const lv_32fc_t* taps, unsigned int num_bytes) {
00014 
00015   float * res = (float*) result;
00016   float * in = (float*) input;
00017   float * tp = (float*) taps;
00018   unsigned int n_2_ccomplex_blocks = num_bytes >> 4;
00019   unsigned int isodd = (num_bytes >> 3) &1;
00020 
00021   float sum0[2] = {0,0};
00022   float sum1[2] = {0,0};
00023   unsigned int i = 0;
00024 
00025   for(i = 0; i < n_2_ccomplex_blocks; ++i) {
00026     sum0[0] += in[0] * tp[0] - in[1] * tp[1];
00027     sum0[1] += in[0] * tp[1] + in[1] * tp[0];
00028     sum1[0] += in[2] * tp[2] - in[3] * tp[3];
00029     sum1[1] += in[2] * tp[3] + in[3] * tp[2];
00030 
00031     in += 4;
00032     tp += 4;
00033   }
00034 
00035   res[0] = sum0[0] + sum1[0];
00036   res[1] = sum0[1] + sum1[1];
00037 
00038   for(i = 0; i < isodd; ++i) {
00039     *result += input[(num_bytes >> 3) - 1] * taps[(num_bytes >> 3) - 1];
00040   }
00041 }
00042 
00043 #endif /*LV_HAVE_GENERIC*/
00044 
00045 
00046 #if LV_HAVE_SSE && LV_HAVE_64
00047 
00048 
00049 static inline void volk_32fc_x2_dot_prod_32fc_a_sse_64(lv_32fc_t* result, const lv_32fc_t* input, const lv_32fc_t* taps, unsigned int num_bytes) {
00050 
00051 
00052   asm
00053     (
00054      "#  ccomplex_dotprod_generic (float* result, const float *input,\n\t"
00055      "#                         const float *taps, unsigned num_bytes)\n\t"
00056      "#    float sum0 = 0;\n\t"
00057      "#    float sum1 = 0;\n\t"
00058      "#    float sum2 = 0;\n\t"
00059      "#    float sum3 = 0;\n\t"
00060      "#    do {\n\t"
00061      "#      sum0 += input[0] * taps[0] - input[1] * taps[1];\n\t"
00062      "#      sum1 += input[0] * taps[1] + input[1] * taps[0];\n\t"
00063      "#      sum2 += input[2] * taps[2] - input[3] * taps[3];\n\t"
00064      "#      sum3 += input[2] * taps[3] + input[3] * taps[2];\n\t"
00065      "#      input += 4;\n\t"
00066      "#      taps += 4;  \n\t"
00067      "#    } while (--n_2_ccomplex_blocks != 0);\n\t"
00068      "#    result[0] = sum0 + sum2;\n\t"
00069      "#    result[1] = sum1 + sum3;\n\t"
00070      "# TODO: prefetch and better scheduling\n\t"
00071      "  xor    %%r9,  %%r9\n\t"
00072      "  xor    %%r10, %%r10\n\t"
00073      "  movq   %%rcx, %%rax\n\t"
00074      "  movq   %%rcx, %%r8\n\t"
00075      "  movq   %[rsi],  %%r9\n\t"
00076      "  movq   %[rdx], %%r10\n\t"
00077      "  xorps   %%xmm6, %%xmm6          # zero accumulators\n\t"
00078      "  movaps  0(%%r9), %%xmm0\n\t"
00079      "  xorps   %%xmm7, %%xmm7          # zero accumulators\n\t"
00080      "  movaps  0(%%r10), %%xmm2\n\t"
00081      "  shr     $5, %%rax               # rax = n_2_ccomplex_blocks / 2\n\t"
00082      "  shr     $4, %%r8\n\t"
00083      "  jmp     .%=L1_test\n\t"
00084      "  # 4 taps / loop\n\t"
00085      "  # something like ?? cycles / loop\n\t"
00086      ".%=Loop1: \n\t"
00087      "# complex prod: C += A * B,  w/ temp Z & Y (or B), xmmPN=$0x8000000080000000\n\t"
00088      "# movaps  (%%r9), %%xmmA\n\t"
00089      "# movaps  (%%r10), %%xmmB\n\t"
00090      "# movaps  %%xmmA, %%xmmZ\n\t"
00091      "# shufps  $0xb1, %%xmmZ, %%xmmZ   # swap internals\n\t"
00092      "# mulps   %%xmmB, %%xmmA\n\t"
00093      "# mulps   %%xmmZ, %%xmmB\n\t"
00094      "# # SSE replacement for: pfpnacc %%xmmB, %%xmmA\n\t"
00095      "# xorps   %%xmmPN, %%xmmA\n\t"
00096      "# movaps  %%xmmA, %%xmmZ\n\t"
00097      "# unpcklps %%xmmB, %%xmmA\n\t"
00098      "# unpckhps %%xmmB, %%xmmZ\n\t"
00099      "# movaps  %%xmmZ, %%xmmY\n\t"
00100      "# shufps  $0x44, %%xmmA, %%xmmZ   # b01000100\n\t"
00101      "# shufps  $0xee, %%xmmY, %%xmmA   # b11101110\n\t"
00102      "# addps   %%xmmZ, %%xmmA\n\t"
00103      "# addps   %%xmmA, %%xmmC\n\t"
00104      "# A=xmm0, B=xmm2, Z=xmm4\n\t"
00105      "# A'=xmm1, B'=xmm3, Z'=xmm5\n\t"
00106      "  movaps  16(%%r9), %%xmm1\n\t"
00107      "  movaps  %%xmm0, %%xmm4\n\t"
00108      "  mulps   %%xmm2, %%xmm0\n\t"
00109      "  shufps  $0xb1, %%xmm4, %%xmm4   # swap internals\n\t"
00110      "  movaps  16(%%r10), %%xmm3\n\t"
00111      "  movaps  %%xmm1, %%xmm5\n\t"
00112      "  addps   %%xmm0, %%xmm6\n\t"
00113      "  mulps   %%xmm3, %%xmm1\n\t"
00114      "  shufps  $0xb1, %%xmm5, %%xmm5   # swap internals\n\t"
00115      "  addps   %%xmm1, %%xmm6\n\t"
00116      "  mulps   %%xmm4, %%xmm2\n\t"
00117      "  movaps  32(%%r9), %%xmm0\n\t"
00118      "  addps   %%xmm2, %%xmm7\n\t"
00119      "  mulps   %%xmm5, %%xmm3\n\t"
00120      "  add     $32, %%r9\n\t"
00121      "  movaps  32(%%r10), %%xmm2\n\t"
00122      "  addps   %%xmm3, %%xmm7\n\t"
00123      "  add     $32, %%r10\n\t"
00124      ".%=L1_test:\n\t"
00125      "  dec     %%rax\n\t"
00126      "  jge     .%=Loop1\n\t"
00127      "  # We've handled the bulk of multiplies up to here.\n\t"
00128      "  # Let's sse if original n_2_ccomplex_blocks was odd.\n\t"
00129      "  # If so, we've got 2 more taps to do.\n\t"
00130      "  and     $1, %%r8\n\t"
00131      "  je      .%=Leven\n\t"
00132      "  # The count was odd, do 2 more taps.\n\t"
00133      "  # Note that we've already got mm0/mm2 preloaded\n\t"
00134      "  # from the main loop.\n\t"
00135      "  movaps  %%xmm0, %%xmm4\n\t"
00136      "  mulps   %%xmm2, %%xmm0\n\t"
00137      "  shufps  $0xb1, %%xmm4, %%xmm4   # swap internals\n\t"
00138      "  addps   %%xmm0, %%xmm6\n\t"
00139      "  mulps   %%xmm4, %%xmm2\n\t"
00140      "  addps   %%xmm2, %%xmm7\n\t"
00141      ".%=Leven:\n\t"
00142      "  # neg inversor\n\t"
00143      "  xorps   %%xmm1, %%xmm1\n\t"
00144      "  mov     $0x80000000, %%r9\n\t"
00145      "  movd    %%r9, %%xmm1\n\t"
00146      "  shufps  $0x11, %%xmm1, %%xmm1   # b00010001 # 0 -0 0 -0\n\t"
00147      "  # pfpnacc\n\t"
00148      "  xorps   %%xmm1, %%xmm6\n\t"
00149      "  movaps  %%xmm6, %%xmm2\n\t"
00150      "  unpcklps %%xmm7, %%xmm6\n\t"
00151      "  unpckhps %%xmm7, %%xmm2\n\t"
00152      "  movaps  %%xmm2, %%xmm3\n\t"
00153      "  shufps  $0x44, %%xmm6, %%xmm2   # b01000100\n\t"
00154      "  shufps  $0xee, %%xmm3, %%xmm6   # b11101110\n\t"
00155      "  addps   %%xmm2, %%xmm6\n\t"
00156      "                                  # xmm6 = r1 i2 r3 i4\n\t"
00157      "  movhlps %%xmm6, %%xmm4          # xmm4 = r3 i4 ?? ??\n\t"
00158      "  addps   %%xmm4, %%xmm6          # xmm6 = r1+r3 i2+i4 ?? ??\n\t"
00159      "  movlps  %%xmm6, (%[rdi])                # store low 2x32 bits (complex) to memory\n\t"
00160      :
00161      :[rsi] "r" (input), [rdx] "r" (taps), "c" (num_bytes), [rdi] "r" (result)
00162      :"rax", "r8", "r9", "r10"
00163      );
00164 
00165 
00166   if(((num_bytes >> 3) & 1)) {
00167     *result += (input[(num_bytes >> 3) - 1] * taps[(num_bytes >> 3) - 1]);
00168   }
00169 
00170   return;
00171 
00172 }
00173 
00174 #endif
00175 
00176 #if LV_HAVE_SSE && LV_HAVE_32
00177 
00178 static inline void volk_32fc_x2_dot_prod_32fc_a_sse_32(lv_32fc_t* result, const lv_32fc_t* input, const lv_32fc_t* taps, unsigned int num_bytes) {
00179 
00180   volk_32fc_x2_dot_prod_32fc_a_generic(result, input, taps, num_bytes);
00181 
00182 #if 0
00183   asm volatile
00184     (
00185      "  #pushl  %%ebp\n\t"
00186      "  #movl   %%esp, %%ebp\n\t"
00187      "  movl    12(%%ebp), %%eax                # input\n\t"
00188      "  movl    16(%%ebp), %%edx                # taps\n\t"
00189      "  movl    20(%%ebp), %%ecx                # n_bytes\n\t"
00190      "  xorps   %%xmm6, %%xmm6          # zero accumulators\n\t"
00191      "  movaps  0(%%eax), %%xmm0\n\t"
00192      "  xorps   %%xmm7, %%xmm7          # zero accumulators\n\t"
00193      "  movaps  0(%%edx), %%xmm2\n\t"
00194      "  shrl    $5, %%ecx               # ecx = n_2_ccomplex_blocks / 2\n\t"
00195      "  jmp     .%=L1_test\n\t"
00196      "  # 4 taps / loop\n\t"
00197      "  # something like ?? cycles / loop\n\t"
00198      ".%=Loop1: \n\t"
00199      "# complex prod: C += A * B,  w/ temp Z & Y (or B), xmmPN=$0x8000000080000000\n\t"
00200      "# movaps  (%%eax), %%xmmA\n\t"
00201      "# movaps  (%%edx), %%xmmB\n\t"
00202      "# movaps  %%xmmA, %%xmmZ\n\t"
00203      "# shufps  $0xb1, %%xmmZ, %%xmmZ   # swap internals\n\t"
00204      "# mulps   %%xmmB, %%xmmA\n\t"
00205      "# mulps   %%xmmZ, %%xmmB\n\t"
00206      "# # SSE replacement for: pfpnacc %%xmmB, %%xmmA\n\t"
00207      "# xorps   %%xmmPN, %%xmmA\n\t"
00208      "# movaps  %%xmmA, %%xmmZ\n\t"
00209      "# unpcklps %%xmmB, %%xmmA\n\t"
00210      "# unpckhps %%xmmB, %%xmmZ\n\t"
00211      "# movaps  %%xmmZ, %%xmmY\n\t"
00212      "# shufps  $0x44, %%xmmA, %%xmmZ   # b01000100\n\t"
00213      "# shufps  $0xee, %%xmmY, %%xmmA   # b11101110\n\t"
00214      "# addps   %%xmmZ, %%xmmA\n\t"
00215      "# addps   %%xmmA, %%xmmC\n\t"
00216      "# A=xmm0, B=xmm2, Z=xmm4\n\t"
00217      "# A'=xmm1, B'=xmm3, Z'=xmm5\n\t"
00218      "  movaps  16(%%eax), %%xmm1\n\t"
00219      "  movaps  %%xmm0, %%xmm4\n\t"
00220      "  mulps   %%xmm2, %%xmm0\n\t"
00221      "  shufps  $0xb1, %%xmm4, %%xmm4   # swap internals\n\t"
00222      "  movaps  16(%%edx), %%xmm3\n\t"
00223      "  movaps  %%xmm1, %%xmm5\n\t"
00224      "  addps   %%xmm0, %%xmm6\n\t"
00225      "  mulps   %%xmm3, %%xmm1\n\t"
00226      "  shufps  $0xb1, %%xmm5, %%xmm5   # swap internals\n\t"
00227      "  addps   %%xmm1, %%xmm6\n\t"
00228      "  mulps   %%xmm4, %%xmm2\n\t"
00229      "  movaps  32(%%eax), %%xmm0\n\t"
00230      "  addps   %%xmm2, %%xmm7\n\t"
00231      "  mulps   %%xmm5, %%xmm3\n\t"
00232      "  addl    $32, %%eax\n\t"
00233      "  movaps  32(%%edx), %%xmm2\n\t"
00234      "  addps   %%xmm3, %%xmm7\n\t"
00235      "  addl    $32, %%edx\n\t"
00236      ".%=L1_test:\n\t"
00237      "  decl    %%ecx\n\t"
00238      "  jge     .%=Loop1\n\t"
00239      "  # We've handled the bulk of multiplies up to here.\n\t"
00240      "  # Let's sse if original n_2_ccomplex_blocks was odd.\n\t"
00241      "  # If so, we've got 2 more taps to do.\n\t"
00242      "  movl    20(%%ebp), %%ecx                # n_2_ccomplex_blocks\n\t"
00243      "  shrl    $4, %%ecx\n\t"
00244      "  andl    $1, %%ecx\n\t"
00245      "  je      .%=Leven\n\t"
00246      "  # The count was odd, do 2 more taps.\n\t"
00247      "  # Note that we've already got mm0/mm2 preloaded\n\t"
00248      "  # from the main loop.\n\t"
00249      "  movaps  %%xmm0, %%xmm4\n\t"
00250      "  mulps   %%xmm2, %%xmm0\n\t"
00251      "  shufps  $0xb1, %%xmm4, %%xmm4   # swap internals\n\t"
00252      "  addps   %%xmm0, %%xmm6\n\t"
00253      "  mulps   %%xmm4, %%xmm2\n\t"
00254      "  addps   %%xmm2, %%xmm7\n\t"
00255      ".%=Leven:\n\t"
00256      "  # neg inversor\n\t"
00257      "  movl 8(%%ebp), %%eax \n\t"
00258      "  xorps   %%xmm1, %%xmm1\n\t"
00259      "  movl    $0x80000000, (%%eax)\n\t"
00260      "  movss   (%%eax), %%xmm1\n\t"
00261      "  shufps  $0x11, %%xmm1, %%xmm1   # b00010001 # 0 -0 0 -0\n\t"
00262      "  # pfpnacc\n\t"
00263      "  xorps   %%xmm1, %%xmm6\n\t"
00264      "  movaps  %%xmm6, %%xmm2\n\t"
00265      "  unpcklps %%xmm7, %%xmm6\n\t"
00266      "  unpckhps %%xmm7, %%xmm2\n\t"
00267      "  movaps  %%xmm2, %%xmm3\n\t"
00268      "  shufps  $0x44, %%xmm6, %%xmm2   # b01000100\n\t"
00269      "  shufps  $0xee, %%xmm3, %%xmm6   # b11101110\n\t"
00270      "  addps   %%xmm2, %%xmm6\n\t"
00271      "                                  # xmm6 = r1 i2 r3 i4\n\t"
00272      "  #movl   8(%%ebp), %%eax         # @result\n\t"
00273      "  movhlps %%xmm6, %%xmm4          # xmm4 = r3 i4 ?? ??\n\t"
00274      "  addps   %%xmm4, %%xmm6          # xmm6 = r1+r3 i2+i4 ?? ??\n\t"
00275      "  movlps  %%xmm6, (%%eax)         # store low 2x32 bits (complex) to memory\n\t"
00276      "  #popl   %%ebp\n\t"
00277      :
00278      :
00279      : "eax", "ecx", "edx"
00280      );
00281 
00282 
00283   int getem = num_bytes % 16;
00284 
00285   for(; getem > 0; getem -= 8) {
00286 
00287 
00288     *result += (input[(num_bytes >> 3) - 1] * taps[(num_bytes >> 3) - 1]);
00289 
00290   }
00291 
00292   return;
00293 #endif
00294 }
00295 
00296 #endif /*LV_HAVE_SSE*/
00297 
00298 #ifdef LV_HAVE_SSE3
00299 
00300 #include <pmmintrin.h>
00301 
00302 static inline void volk_32fc_x2_dot_prod_32fc_a_sse3(lv_32fc_t* result, const lv_32fc_t* input, const lv_32fc_t* taps, unsigned int num_bytes) {
00303 
00304 
00305   lv_32fc_t dotProduct;
00306   memset(&dotProduct, 0x0, 2*sizeof(float));
00307 
00308   unsigned int number = 0;
00309   const unsigned int halfPoints = num_bytes >> 4;
00310 
00311   __m128 x, y, yl, yh, z, tmp1, tmp2, dotProdVal;
00312 
00313   const lv_32fc_t* a = input;
00314   const lv_32fc_t* b = taps;
00315 
00316   dotProdVal = _mm_setzero_ps();
00317 
00318   for(;number < halfPoints; number++){
00319 
00320     x = _mm_load_ps((float*)a); // Load the ar + ai, br + bi as ar,ai,br,bi
00321     y = _mm_load_ps((float*)b); // Load the cr + ci, dr + di as cr,ci,dr,di
00322 
00323     yl = _mm_moveldup_ps(y); // Load yl with cr,cr,dr,dr
00324     yh = _mm_movehdup_ps(y); // Load yh with ci,ci,di,di
00325 
00326     tmp1 = _mm_mul_ps(x,yl); // tmp1 = ar*cr,ai*cr,br*dr,bi*dr
00327 
00328     x = _mm_shuffle_ps(x,x,0xB1); // Re-arrange x to be ai,ar,bi,br
00329 
00330     tmp2 = _mm_mul_ps(x,yh); // tmp2 = ai*ci,ar*ci,bi*di,br*di
00331 
00332     z = _mm_addsub_ps(tmp1,tmp2); // ar*cr-ai*ci, ai*cr+ar*ci, br*dr-bi*di, bi*dr+br*di
00333 
00334     dotProdVal = _mm_add_ps(dotProdVal, z); // Add the complex multiplication results together
00335 
00336     a += 2;
00337     b += 2;
00338   }
00339 
00340   __VOLK_ATTR_ALIGNED(16) lv_32fc_t dotProductVector[2];
00341 
00342   _mm_store_ps((float*)dotProductVector,dotProdVal); // Store the results back into the dot product vector
00343 
00344   dotProduct += ( dotProductVector[0] + dotProductVector[1] );
00345 
00346   if(((num_bytes >> 3) & 1) != 0) {
00347     dotProduct += (*a) * (*b);
00348   }
00349 
00350   *result = dotProduct;
00351 }
00352 
00353 #endif /*LV_HAVE_SSE3*/
00354 
00355 #ifdef LV_HAVE_SSE4_1
00356 
00357 #include <smmintrin.h>
00358 
00359 static inline void volk_32fc_x2_dot_prod_32fc_a_sse4_1(lv_32fc_t* result, const lv_32fc_t* input, const lv_32fc_t* taps, unsigned int num_bytes) {
00360 
00361     __m128 xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, real0, real1, im0, im1;
00362     float *p_input, *p_taps;
00363     __m64 *p_result;
00364 
00365     p_result = (__m64*)result;
00366     p_input = (float*)input;
00367     p_taps = (float*)taps;
00368 
00369     static const __m128i neg = {0x000000000000000080000000};
00370 
00371     int i = 0;
00372 
00373     int bound = (num_bytes >> 5);
00374     int leftovers = (num_bytes & 24) >> 3;
00375 
00376     real0 = _mm_sub_ps(real0, real0);
00377     real1 = _mm_sub_ps(real1, real1);
00378     im0 = _mm_sub_ps(im0, im0);
00379     im1 = _mm_sub_ps(im1, im1);
00380 
00381     for(; i < bound; ++i) {
00382 
00383 
00384     xmm0 = _mm_load_ps(p_input);
00385     xmm1 = _mm_load_ps(p_taps);
00386 
00387     p_input += 4;
00388     p_taps += 4;
00389 
00390     xmm2 = _mm_load_ps(p_input);
00391     xmm3 = _mm_load_ps(p_taps);
00392 
00393     p_input += 4;
00394     p_taps += 4;
00395 
00396     xmm4 = _mm_unpackhi_ps(xmm0, xmm2);
00397     xmm5 = _mm_unpackhi_ps(xmm1, xmm3);
00398     xmm0 = _mm_unpacklo_ps(xmm0, xmm2);
00399     xmm2 = _mm_unpacklo_ps(xmm1, xmm3);
00400 
00401     //imaginary vector from input
00402     xmm1 = _mm_unpackhi_ps(xmm0, xmm4);
00403     //real vector from input
00404     xmm3 = _mm_unpacklo_ps(xmm0, xmm4);
00405     //imaginary vector from taps
00406     xmm0 = _mm_unpackhi_ps(xmm2, xmm5);
00407     //real vector from taps
00408     xmm2 = _mm_unpacklo_ps(xmm2, xmm5);
00409 
00410     xmm4 = _mm_dp_ps(xmm3, xmm2, 0xf1);
00411     xmm5 = _mm_dp_ps(xmm1, xmm0, 0xf1);
00412 
00413     xmm6 = _mm_dp_ps(xmm3, xmm0, 0xf2);
00414     xmm7 = _mm_dp_ps(xmm1, xmm2, 0xf2);
00415 
00416     real0 = _mm_add_ps(xmm4, real0);
00417     real1 = _mm_add_ps(xmm5, real1);
00418     im0 = _mm_add_ps(xmm6, im0);
00419     im1 = _mm_add_ps(xmm7, im1);
00420 
00421     }
00422 
00423     real1 = _mm_xor_ps(real1, bit128_p(&neg)->float_vec);
00424 
00425     im0 = _mm_add_ps(im0, im1);
00426     real0 = _mm_add_ps(real0, real1);
00427 
00428     im0 = _mm_add_ps(im0, real0);
00429 
00430     _mm_storel_pi(p_result, im0);
00431 
00432     for(i = bound * 4; i < (bound * 4) + leftovers; ++i) {
00433 
00434     *result += input[i] * taps[i];
00435     }
00436 }
00437 
00438 #endif /*LV_HAVE_SSE4_1*/
00439 
00440 #endif /*INCLUDED_volk_32fc_x2_dot_prod_32fc_a_H*/