• 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 <arm_neon.h>
10 
11 #include <xnnpack/math.h>
12 #include <xnnpack/math-stubs.h>
13 
14 
xnn_math_f32_sqrt__neon_nr3rsqrts(size_t n,const float * input,float * output)15 void xnn_math_f32_sqrt__neon_nr3rsqrts(
16     size_t n,
17     const float* input,
18     float* output)
19 {
20   assert(n % (4 * sizeof(float)) == 0);
21 
22   for (; n != 0; n -= 4 * sizeof(float)) {
23     const float32x4_t vx = vld1q_f32(input); input += 4;
24 
25     // Initial approximation
26     float32x4_t vrsqrtx = vrsqrteq_f32(vx);
27 
28     // Netwon-Raphson iteration: rsqrt_x <- rsqrt_x * ((3 - x * rsqrt_x * rsqrt_x) / 2)
29     // Note: x * (rsqrt_x * rsqrt_x) for the first iteration and (x * rsqrt_x) * rsqrt_x for the next two improves accuracy
30     // Note: vrsqrtsq_f32(x, y) := (3 - x * y) / 2
31     vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vx, vmulq_f32(vrsqrtx, vrsqrtx)));
32     vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vmulq_f32(vrsqrtx, vx), vrsqrtx));
33     vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vmulq_f32(vrsqrtx, vx), vrsqrtx));
34 
35     // Reconstruct sqrt(x) = rsqrt(x) * x
36     const float32x4_t vy = vmulq_f32(vrsqrtx, vx);
37 
38     vst1q_f32(output, vy); output += 4;
39   }
40 }
41