• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 #include <stddef.h>
8 
9 #include <xmmintrin.h>
10 
11 #include <xnnpack/math.h>
12 #include <xnnpack/math-stubs.h>
13 
14 
xnn_math_f32_sqrt__sse_hh1mac(size_t n,const float * input,float * output)15 void xnn_math_f32_sqrt__sse_hh1mac(
16     size_t n,
17     const float* input,
18     float* output)
19 {
20   assert(n % (4 * sizeof(float)) == 0);
21 
22   const __m128 vc1875 = _mm_set1_ps(1.875f);
23   const __m128 vc0375 = _mm_set1_ps(0.375f);
24   const __m128 vc1250 = _mm_set1_ps(1.250f);
25   for (; n != 0; n -= 4 * sizeof(float)) {
26     const __m128 vx = _mm_load_ps(input);
27     input += 4;
28 
29     // Initial approximation
30     __m128 vrsqrtx = _mm_rsqrt_ps(vx);
31 
32     // Householder (order 2) iteration:
33     //   rsqrt_x <- rsqrt_x * (1.875 + t * (0.375 * t - 1.25)) where t = x * rsqrt_x * rsqrt_x
34     // Note: half_x * (rsqrt_x * rsqrt_x) is less accurate than (half_x * rsqrt_x) * rsqrt_x
35     const __m128 vt = _mm_mul_ps(_mm_mul_ps(vx, vrsqrtx), vrsqrtx);
36     vrsqrtx = _mm_mul_ps(vrsqrtx, _mm_add_ps(_mm_mul_ps(vt, _mm_sub_ps(_mm_mul_ps(vt, vc0375), vc1250)), vc1875));
37 
38     // Reconstruct sqrt(x) = rsqrt(x) * x
39     const __m128 vy = _mm_mul_ps(vrsqrtx, vx);
40 
41     _mm_store_ps(output, vy);
42     output += 4;
43   }
44 }
45