/kernels/volk/volk_32f_invsqrt_32f.h

https://github.com/gnuradio/volk · C Header · 219 lines · 122 code · 33 blank · 64 comment · 9 complexity · b930a8e354e5199cfb92a2d636a29bda MD5 · raw file

  1. /* -*- c++ -*- */
  2. /*
  3. * Copyright 2013, 2014 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. * \page volk_32f_invsqrt_32f
  24. *
  25. * \b Overview
  26. *
  27. * Computes the inverse square root of the input vector and stores
  28. * result in the output vector.
  29. *
  30. * <b>Dispatcher Prototype</b>
  31. * \code
  32. * void volk_32f_invsqrt_32f(float* cVector, const float* aVector, unsigned int
  33. * num_points) \endcode
  34. *
  35. * \b Inputs
  36. * \li aVector: the input vector of floats.
  37. * \li num_points: The number of data points.
  38. *
  39. * \b Outputs
  40. * \li cVector: The output vector.
  41. *
  42. * \b Example
  43. * \code
  44. * int N = 10;
  45. * unsigned int alignment = volk_get_alignment();
  46. * float* in = (float*)volk_malloc(sizeof(float)*N, alignment);
  47. * float* out = (float*)volk_malloc(sizeof(float)*N, alignment);
  48. *
  49. * for(unsigned int ii = 0; ii < N; ++ii){
  50. * in[ii] = 1.0 / (float)(ii*ii);
  51. * }
  52. *
  53. * volk_32f_invsqrt_32f(out, in, N);
  54. *
  55. * for(unsigned int ii = 0; ii < N; ++ii){
  56. * printf("out(%i) = %f\n", ii, out[ii]);
  57. * }
  58. *
  59. * volk_free(in);
  60. * volk_free(out);
  61. * \endcode
  62. */
  63. #ifndef INCLUDED_volk_32f_invsqrt_32f_a_H
  64. #define INCLUDED_volk_32f_invsqrt_32f_a_H
  65. #include <inttypes.h>
  66. #include <math.h>
  67. #include <stdio.h>
  68. #include <string.h>
  69. static inline float Q_rsqrt(float number)
  70. {
  71. float x2;
  72. const float threehalfs = 1.5F;
  73. union f32_to_i32 {
  74. int32_t i;
  75. float f;
  76. } u;
  77. x2 = number * 0.5F;
  78. u.f = number;
  79. u.i = 0x5f3759df - (u.i >> 1); // what the fuck?
  80. u.f = u.f * (threehalfs - (x2 * u.f * u.f)); // 1st iteration
  81. // u.f = u.f * ( threehalfs - ( x2 * u.f * u.f ) ); // 2nd iteration, this can be
  82. // removed
  83. return u.f;
  84. }
  85. #ifdef LV_HAVE_AVX
  86. #include <immintrin.h>
  87. static inline void
  88. volk_32f_invsqrt_32f_a_avx(float* cVector, const float* aVector, unsigned int num_points)
  89. {
  90. unsigned int number = 0;
  91. const unsigned int eighthPoints = num_points / 8;
  92. float* cPtr = cVector;
  93. const float* aPtr = aVector;
  94. __m256 aVal, cVal;
  95. for (; number < eighthPoints; number++) {
  96. aVal = _mm256_load_ps(aPtr);
  97. cVal = _mm256_rsqrt_ps(aVal);
  98. _mm256_store_ps(cPtr, cVal);
  99. aPtr += 8;
  100. cPtr += 8;
  101. }
  102. number = eighthPoints * 8;
  103. for (; number < num_points; number++)
  104. *cPtr++ = Q_rsqrt(*aPtr++);
  105. }
  106. #endif /* LV_HAVE_AVX */
  107. #ifdef LV_HAVE_SSE
  108. #include <xmmintrin.h>
  109. static inline void
  110. volk_32f_invsqrt_32f_a_sse(float* cVector, const float* aVector, unsigned int num_points)
  111. {
  112. unsigned int number = 0;
  113. const unsigned int quarterPoints = num_points / 4;
  114. float* cPtr = cVector;
  115. const float* aPtr = aVector;
  116. __m128 aVal, cVal;
  117. for (; number < quarterPoints; number++) {
  118. aVal = _mm_load_ps(aPtr);
  119. cVal = _mm_rsqrt_ps(aVal);
  120. _mm_store_ps(cPtr, cVal); // Store the results back into the C container
  121. aPtr += 4;
  122. cPtr += 4;
  123. }
  124. number = quarterPoints * 4;
  125. for (; number < num_points; number++) {
  126. *cPtr++ = Q_rsqrt(*aPtr++);
  127. }
  128. }
  129. #endif /* LV_HAVE_SSE */
  130. #ifdef LV_HAVE_NEON
  131. #include <arm_neon.h>
  132. static inline void
  133. volk_32f_invsqrt_32f_neon(float* cVector, const float* aVector, unsigned int num_points)
  134. {
  135. unsigned int number;
  136. const unsigned int quarter_points = num_points / 4;
  137. float* cPtr = cVector;
  138. const float* aPtr = aVector;
  139. float32x4_t a_val, c_val;
  140. for (number = 0; number < quarter_points; ++number) {
  141. a_val = vld1q_f32(aPtr);
  142. c_val = vrsqrteq_f32(a_val);
  143. vst1q_f32(cPtr, c_val);
  144. aPtr += 4;
  145. cPtr += 4;
  146. }
  147. for (number = quarter_points * 4; number < num_points; number++)
  148. *cPtr++ = Q_rsqrt(*aPtr++);
  149. }
  150. #endif /* LV_HAVE_NEON */
  151. #ifdef LV_HAVE_GENERIC
  152. static inline void volk_32f_invsqrt_32f_generic(float* cVector,
  153. const float* aVector,
  154. unsigned int num_points)
  155. {
  156. float* cPtr = cVector;
  157. const float* aPtr = aVector;
  158. unsigned int number = 0;
  159. for (number = 0; number < num_points; number++) {
  160. *cPtr++ = Q_rsqrt(*aPtr++);
  161. }
  162. }
  163. #endif /* LV_HAVE_GENERIC */
  164. #ifdef LV_HAVE_AVX
  165. #include <immintrin.h>
  166. static inline void
  167. volk_32f_invsqrt_32f_u_avx(float* cVector, const float* aVector, unsigned int num_points)
  168. {
  169. unsigned int number = 0;
  170. const unsigned int eighthPoints = num_points / 8;
  171. float* cPtr = cVector;
  172. const float* aPtr = aVector;
  173. __m256 aVal, cVal;
  174. for (; number < eighthPoints; number++) {
  175. aVal = _mm256_loadu_ps(aPtr);
  176. cVal = _mm256_rsqrt_ps(aVal);
  177. _mm256_storeu_ps(cPtr, cVal);
  178. aPtr += 8;
  179. cPtr += 8;
  180. }
  181. number = eighthPoints * 8;
  182. for (; number < num_points; number++)
  183. *cPtr++ = Q_rsqrt(*aPtr++);
  184. }
  185. #endif /* LV_HAVE_AVX */
  186. #endif /* INCLUDED_volk_32f_invsqrt_32f_a_H */