Debian Codegraph @

src/f32-vrsqrt/gen/f32-vrsqrt-neon-rsqrt-u8.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_u8(
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 >= 8 * sizeof(float); batch -= 8 * sizeof(float)) {
56 const float32x4_t vx0123 = vld1q_f32(input); input += 4;
57 const float32x4_t vx4567 = vld1q_f32(input); input += 4;
58
59 const float32x4_t vt0_0123 = vrsqrteq_f32(vx0123);
60 const float32x4_t vt0_4567 = vrsqrteq_f32(vx4567);
61 const float32x4_t vt1_0123 = vmulq_f32(vt0_0123, vt0_0123);
62 const float32x4_t vt1_4567 = vmulq_f32(vt0_4567, vt0_4567);
63 const float32x4_t vt2_0123 = vrsqrtsq_f32(vx0123, vt1_0123);
64 const float32x4_t vt2_4567 = vrsqrtsq_f32(vx4567, vt1_4567);
65 const float32x4_t vt3_0123 = vmulq_f32(vt0_0123, vt2_0123);
66 const float32x4_t vt3_4567 = vmulq_f32(vt0_4567, vt2_4567);
67 const float32x4_t vt4_0123 = vmulq_f32(vt3_0123, vt3_0123);
68 const float32x4_t vt4_4567 = vmulq_f32(vt3_4567, vt3_4567);
69 const float32x4_t vt5_0123 = vrsqrtsq_f32(vx0123, vt4_0123);
70 const float32x4_t vt5_4567 = vrsqrtsq_f32(vx4567, vt4_4567);
71 const float32x4_t vy0123 = vmulq_f32(vt3_0123, vt5_0123);
72 const float32x4_t vy4567 = vmulq_f32(vt3_4567, vt5_4567);
73
74 vst1q_f32(output, vy0123); output += 4;
75 vst1q_f32(output, vy4567); output += 4;
76 }
77 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
78 const float32x4_t vx = vld1q_f32(input); input += 4;
79
80 const float32x4_t vt0 = vrsqrteq_f32(vx);
81 const float32x4_t vt1 = vmulq_f32(vt0, vt0);
82 const float32x4_t vt2 = vrsqrtsq_f32(vx, vt1);
83 const float32x4_t vt3 = vmulq_f32(vt0, vt2);
84 const float32x4_t vt4 = vmulq_f32(vt3, vt3);
85 const float32x4_t vt5 = vrsqrtsq_f32(vx, vt4);
86 const float32x4_t vy = vmulq_f32(vt3, vt5);
87
88 vst1q_f32(output, vy); output += 4;
89 }
90 if XNN_UNLIKELY(batch != 0) {
91 const float32x4_t vx = vld1q_f32(input);
92 const float32x2_t vx_lo = vget_low_f32(vx);
93 const float32x2_t vx_hi = vget_high_f32(vx);
94
95 const float32x2_t vt0 = vrsqrte_f32(vx_lo);
96 const float32x2_t vt1 = vmul_f32(vt0, vt0);
97 const float32x2_t vt2 = vrsqrts_f32(vx_lo, vt1);
98 const float32x2_t vt3 = vmul_f32(vt0, vt2);
99 const float32x2_t vt4 = vmul_f32(vt3, vt3);
100 const float32x2_t vt5 = vrsqrts_f32(vx_lo, vt4);
101 float32x2_t vy_lo = vmul_f32(vt3, vt5);
102
103 if (batch & (2 * sizeof(float))) {
104 vst1_f32(output, vy_lo); output += 2;
105
106 const float32x2_t vt0 = vrsqrte_f32(vx_hi);
107 const float32x2_t vt1 = vmul_f32(vt0, vt0);
108 const float32x2_t vt2 = vrsqrts_f32(vx_hi, vt1);
109 const float32x2_t vt3 = vmul_f32(vt0, vt2);
110 const float32x2_t vt4 = vmul_f32(vt3, vt3);
111 const float32x2_t vt5 = vrsqrts_f32(vx_hi, vt4);
112 vy_lo = vmul_f32(vt3, vt5);
113 }
114
115 if (batch & (1 * sizeof(float))) {
116 vst1_lane_f32(output, vy_lo, 0);
117 }
118 }
119 }