PageRenderTime 26ms CodeModel.GetById 65ms RepoModel.GetById 0ms app.codeStats 1ms

/volk/kernels/volk/volk_32f_invsqrt_32f.h

https://github.com/trondeau/gnuradio-old
C Header | 143 lines | 94 code | 24 blank | 25 comment | 7 complexity | 433596cb0ff8ac866aa2eb9d534d4c9c MD5 | raw file
  1. #ifndef INCLUDED_volk_32f_invsqrt_32f_a_H
  2. #define INCLUDED_volk_32f_invsqrt_32f_a_H
  3. #include <inttypes.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <string.h>
  7. static inline float Q_rsqrt( float number )
  8. {
  9. float x2;
  10. const float threehalfs = 1.5F;
  11. union f32_to_i32 {
  12. int32_t i;
  13. float f;
  14. } u;
  15. x2 = number * 0.5F;
  16. u.f = number;
  17. u.i = 0x5f3759df - ( u.i >> 1 ); // what the fuck?
  18. u.f = u.f * ( threehalfs - ( x2 * u.f * u.f ) ); // 1st iteration
  19. //u.f = u.f * ( threehalfs - ( x2 * u.f * u.f ) ); // 2nd iteration, this can be removed
  20. return u.f;
  21. }
  22. #ifdef LV_HAVE_AVX
  23. #include <immintrin.h>
  24. /*!
  25. \brief Sqrts the two input vectors and store their results in the third vector
  26. \param cVector The vector where the results will be stored
  27. \param aVector One of the vectors to be invsqrted
  28. \param num_points The number of values in aVector and bVector to be invsqrted together and stored into cVector
  29. */
  30. static inline void volk_32f_invsqrt_32f_a_avx(float* cVector, const float* aVector, unsigned int num_points){
  31. unsigned int number = 0;
  32. const unsigned int eighthPoints = num_points / 8;
  33. float* cPtr = cVector;
  34. const float* aPtr = aVector;
  35. __m256 aVal, cVal;
  36. for (; number < eighthPoints; number++)
  37. {
  38. aVal = _mm256_load_ps(aPtr);
  39. cVal = _mm256_rsqrt_ps(aVal);
  40. _mm256_store_ps(cPtr, cVal);
  41. aPtr += 8;
  42. cPtr += 8;
  43. }
  44. number = eighthPoints * 8;
  45. for(;number < num_points; number++)
  46. *cPtr++ = Q_rsqrt(*aPtr++);
  47. }
  48. #endif /* LV_HAVE_AVX */
  49. #ifdef LV_HAVE_SSE
  50. #include <xmmintrin.h>
  51. /*!
  52. \brief Sqrts the two input vectors and store their results in the third vector
  53. \param cVector The vector where the results will be stored
  54. \param aVector One of the vectors to be invsqrted
  55. \param num_points The number of values in aVector and bVector to be invsqrted together and stored into cVector
  56. */
  57. static inline void volk_32f_invsqrt_32f_a_sse(float* cVector, const float* aVector, unsigned int num_points){
  58. unsigned int number = 0;
  59. const unsigned int quarterPoints = num_points / 4;
  60. float* cPtr = cVector;
  61. const float* aPtr = aVector;
  62. __m128 aVal, cVal;
  63. for(;number < quarterPoints; number++){
  64. aVal = _mm_load_ps(aPtr);
  65. cVal = _mm_rsqrt_ps(aVal);
  66. _mm_store_ps(cPtr,cVal); // Store the results back into the C container
  67. aPtr += 4;
  68. cPtr += 4;
  69. }
  70. number = quarterPoints * 4;
  71. for(;number < num_points; number++){
  72. *cPtr++ = Q_rsqrt(*aPtr++);
  73. }
  74. }
  75. #endif /* LV_HAVE_SSE */
  76. #ifdef LV_HAVE_GENERIC
  77. /*!
  78. \brief Sqrts the two input vectors and store their results in the third vector
  79. \param cVector The vector where the results will be stored
  80. \param aVector One of the vectors to be invsqrted
  81. \param num_points The number of values in aVector and bVector to be invsqrted together and stored into cVector
  82. */
  83. static inline void volk_32f_invsqrt_32f_generic(float* cVector, const float* aVector, unsigned int num_points){
  84. float* cPtr = cVector;
  85. const float* aPtr = aVector;
  86. unsigned int number = 0;
  87. for(number = 0; number < num_points; number++){
  88. *cPtr++ = Q_rsqrt(*aPtr++);
  89. }
  90. }
  91. #endif /* LV_HAVE_GENERIC */
  92. #ifdef LV_HAVE_AVX
  93. #include <immintrin.h>
  94. /*!
  95. \brief Sqrts the two input vectors and store their results in the third vector
  96. \param cVector The vector where the results will be stored
  97. \param aVector One of the vectors to be invsqrted
  98. \param num_points The number of values in aVector and bVector to be invsqrted together and stored into cVector
  99. */
  100. static inline void volk_32f_invsqrt_32f_u_avx(float* cVector, const float* aVector, unsigned int num_points){
  101. unsigned int number = 0;
  102. const unsigned int eighthPoints = num_points / 8;
  103. float* cPtr = cVector;
  104. const float* aPtr = aVector;
  105. __m256 aVal, cVal;
  106. for (; number < eighthPoints; number++)
  107. {
  108. aVal = _mm256_loadu_ps(aPtr);
  109. cVal = _mm256_rsqrt_ps(aVal);
  110. _mm256_storeu_ps(cPtr, cVal);
  111. aPtr += 8;
  112. cPtr += 8;
  113. }
  114. number = eighthPoints * 8;
  115. for(;number < num_points; number++)
  116. *cPtr++ = Q_rsqrt(*aPtr++);
  117. }
  118. #endif /* LV_HAVE_AVX */
  119. #endif /* INCLUDED_volk_32f_invsqrt_32f_a_H */