Debian Codegraph @

src/f32-vrsqrt/gen/f32-vrsqrt-neon-rsqrt-u4.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f32-vrsqrt/neon-rsqrt.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2020 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9
10 #include <assert.h>
11 #include <math.h>
12
13 #include <arm_neon.h>
14
15 #include "xnnpack/common.h"
16 #include "xnnpack/vunary.h"
17
18
19 // In the following, we do a single Newton-Raphson step on the equation
20 // $x^{-2} - a$, which expands to:
21 //
22 // $$x_{k+1} = 0.5 * x_k * (3.0 - a * x_k^2)$$
23 //
24 // So we do the following steps:
25 //
26 // 1. t0 = x_k
27 // 2. t1 = t0 * t0 (x_k^2)
28 // 3. t2 = a * t1 (a * x_k^2)
29 // 4. t3 = 3.0 - t2 (3.0 - a * x_k^2)
30 // 5. t4 = 0.5 * t0 (0.5 * x_k)
31 // 6. y = t3 * t4 (0.5 * x_k * (3.0 - a * x_k^2))
32 //
33 // Where $x_k$ is the original approximation and `y` contains the improved
34 // approximation $x_{k+1}$.
35 //
36 // Note that Arm/NEON provides the `vrsqrtsq_f32` instruction, which does steps
37 // 3-5 in the above in a single instruction.
38 //
39 // Note also that the initial approximation computed by the `vrsqrteq_f32`
40 // instruction is only accurate to 9 bits (as opposed to 12 or 14 for x86_64),
41 // which requires us to do two steps of the above.
42
43
44 void xnn_f32_vrsqrt_ukernel__neon_rsqrt_u4(
45 size_t batch,
46 const float* input,
47 float* output,
48 const struct xnn_f32_default_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
49 {
50 assert(batch != 0);
51 assert(batch % sizeof(float) == 0);
52 assert(input != NULL);
53 assert(output != NULL);
54
55 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
56 const float32x4_t vx = vld1q_f32(input); input += 4;
57
58 const float32x4_t vt0 = vrsqrteq_f32(vx);
59 const float32x4_t vt1 = vmulq_f32(vt0, vt0);
60 const float32x4_t vt2 = vrsqrtsq_f32(vx, vt1);
61 const float32x4_t vt3 = vmulq_f32(vt0, vt2);
62 const float32x4_t vt4 = vmulq_f32(vt3, vt3);
63 const float32x4_t vt5 = vrsqrtsq_f32(vx, vt4);
64 const float32x4_t vy = vmulq_f32(vt3, vt5);
65
66 vst1q_f32(output, vy); output += 4;
67 }
68 if XNN_UNLIKELY(batch != 0) {
69 const float32x4_t vx = vld1q_f32(input);
70 const float32x2_t vx_lo = vget_low_f32(vx);
71 const float32x2_t vx_hi = vget_high_f32(vx);
72
73 const float32x2_t vt0 = vrsqrte_f32(vx_lo);
74 const float32x2_t vt1 = vmul_f32(vt0, vt0);
75 const float32x2_t vt2 = vrsqrts_f32(vx_lo, vt1);
76 const float32x2_t vt3 = vmul_f32(vt0, vt2);
77 const float32x2_t vt4 = vmul_f32(vt3, vt3);
78 const float32x2_t vt5 = vrsqrts_f32(vx_lo, vt4);
79 float32x2_t vy_lo = vmul_f32(vt3, vt5);
80
81 if (batch & (2 * sizeof(float))) {
82 vst1_f32(output, vy_lo); output += 2;
83
84 const float32x2_t vt0 = vrsqrte_f32(vx_hi);
85 const float32x2_t vt1 = vmul_f32(vt0, vt0);
86 const float32x2_t vt2 = vrsqrts_f32(vx_hi, vt1);
87 const float32x2_t vt3 = vmul_f32(vt0, vt2);
88 const float32x2_t vt4 = vmul_f32(vt3, vt3);
89 const float32x2_t vt5 = vrsqrts_f32(vx_hi, vt4);
90 vy_lo = vmul_f32(vt3, vt5);
91 }
92
93 if (batch & (1 * sizeof(float))) {
94 vst1_lane_f32(output, vy_lo, 0);
95 }
96 }
97 }