Marat Dukhan | 8400076 | 2020-06-29 18:38:43 -0700 | [diff] [blame^] | 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 | |
| 15 | void xnn_math_f32_sqrt__neon_nr1rsqrts( |
| 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: vrsqrtsq_f32(x, y) := (3 - x * y) / 2 |
| 30 | vrsqrtx = vmulq_f32(vrsqrtx, vrsqrtsq_f32(vx, vmulq_f32(vrsqrtx, vrsqrtx))); |
| 31 | |
| 32 | // Reconstruct sqrt(x) = rsqrt(x) * x |
| 33 | const float32x4_t vy = vmulq_f32(vrsqrtx, vx); |
| 34 | |
| 35 | vst1q_f32(output, vy); output += 4; |
| 36 | } |
| 37 | } |