Debian Codegraph @

src/f32-vrsqrt/gen/f32-vrsqrt-neon-rsqrt-u16.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_u16(
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 >= 16 * sizeof(float); batch -= 16 * sizeof(float)) {
56 const float32x4_t vx0123 = vld1q_f32(input); input += 4;
57 const float32x4_t vx4567 = vld1q_f32(input); input += 4;
58 const float32x4_t vx89AB = vld1q_f32(input); input += 4;
59 const float32x4_t vxCDEF = vld1q_f32(input); input += 4;
60
61 const float32x4_t vt0_0123 = vrsqrteq_f32(vx0123);
62 const float32x4_t vt0_4567 = vrsqrteq_f32(vx4567);
63 const float32x4_t vt0_89AB = vrsqrteq_f32(vx89AB);
64 const float32x4_t vt0_CDEF = vrsqrteq_f32(vxCDEF);
65 const float32x4_t vt1_0123 = vmulq_f32(vt0_0123, vt0_0123);
66 const float32x4_t vt1_4567 = vmulq_f32(vt0_4567, vt0_4567);
67 const float32x4_t vt1_89AB = vmulq_f32(vt0_89AB, vt0_89AB);
68 const float32x4_t vt1_CDEF = vmulq_f32(vt0_CDEF, vt0_CDEF);
69 const float32x4_t vt2_0123 = vrsqrtsq_f32(vx0123, vt1_0123);
70 const float32x4_t vt2_4567 = vrsqrtsq_f32(vx4567, vt1_4567);
71 const float32x4_t vt2_89AB = vrsqrtsq_f32(vx89AB, vt1_89AB);
72 const float32x4_t vt2_CDEF = vrsqrtsq_f32(vxCDEF, vt1_CDEF);
73 const float32x4_t vt3_0123 = vmulq_f32(vt0_0123, vt2_0123);
74 const float32x4_t vt3_4567 = vmulq_f32(vt0_4567, vt2_4567);
75 const float32x4_t vt3_89AB = vmulq_f32(vt0_89AB, vt2_89AB);
76 const float32x4_t vt3_CDEF = vmulq_f32(vt0_CDEF, vt2_CDEF);
77 const float32x4_t vt4_0123 = vmulq_f32(vt3_0123, vt3_0123);
78 const float32x4_t vt4_4567 = vmulq_f32(vt3_4567, vt3_4567);
79 const float32x4_t vt4_89AB = vmulq_f32(vt3_89AB, vt3_89AB);
80 const float32x4_t vt4_CDEF = vmulq_f32(vt3_CDEF, vt3_CDEF);
81 const float32x4_t vt5_0123 = vrsqrtsq_f32(vx0123, vt4_0123);
82 const float32x4_t vt5_4567 = vrsqrtsq_f32(vx4567, vt4_4567);
83 const float32x4_t vt5_89AB = vrsqrtsq_f32(vx89AB, vt4_89AB);
84 const float32x4_t vt5_CDEF = vrsqrtsq_f32(vxCDEF, vt4_CDEF);
85 const float32x4_t vy0123 = vmulq_f32(vt3_0123, vt5_0123);
86 const float32x4_t vy4567 = vmulq_f32(vt3_4567, vt5_4567);
87 const float32x4_t vy89AB = vmulq_f32(vt3_89AB, vt5_89AB);
88 const float32x4_t vyCDEF = vmulq_f32(vt3_CDEF, vt5_CDEF);
89
90 vst1q_f32(output, vy0123); output += 4;
91 vst1q_f32(output, vy4567); output += 4;
92 vst1q_f32(output, vy89AB); output += 4;
93 vst1q_f32(output, vyCDEF); output += 4;
94 }
95 for (; batch >= 4 * sizeof(float); batch -= 4 * sizeof(float)) {
96 const float32x4_t vx = vld1q_f32(input); input += 4;
97
98 const float32x4_t vt0 = vrsqrteq_f32(vx);
99 const float32x4_t vt1 = vmulq_f32(vt0, vt0);
100 const float32x4_t vt2 = vrsqrtsq_f32(vx, vt1);
101 const float32x4_t vt3 = vmulq_f32(vt0, vt2);
102 const float32x4_t vt4 = vmulq_f32(vt3, vt3);
103 const float32x4_t vt5 = vrsqrtsq_f32(vx, vt4);
104 const float32x4_t vy = vmulq_f32(vt3, vt5);
105
106 vst1q_f32(output, vy); output += 4;
107 }
108 if XNN_UNLIKELY(batch != 0) {
109 const float32x4_t vx = vld1q_f32(input);
110 const float32x2_t vx_lo = vget_low_f32(vx);
111 const float32x2_t vx_hi = vget_high_f32(vx);
112
113 const float32x2_t vt0 = vrsqrte_f32(vx_lo);
114 const float32x2_t vt1 = vmul_f32(vt0, vt0);
115 const float32x2_t vt2 = vrsqrts_f32(vx_lo, vt1);
116 const float32x2_t vt3 = vmul_f32(vt0, vt2);
117 const float32x2_t vt4 = vmul_f32(vt3, vt3);
118 const float32x2_t vt5 = vrsqrts_f32(vx_lo, vt4);
119 float32x2_t vy_lo = vmul_f32(vt3, vt5);
120
121 if (batch & (2 * sizeof(float))) {
122 vst1_f32(output, vy_lo); output += 2;
123
124 const float32x2_t vt0 = vrsqrte_f32(vx_hi);
125 const float32x2_t vt1 = vmul_f32(vt0, vt0);
126 const float32x2_t vt2 = vrsqrts_f32(vx_hi, vt1);
127 const float32x2_t vt3 = vmul_f32(vt0, vt2);
128 const float32x2_t vt4 = vmul_f32(vt3, vt3);
129 const float32x2_t vt5 = vrsqrts_f32(vx_hi, vt4);
130 vy_lo = vmul_f32(vt3, vt5);
131 }
132
133 if (batch & (1 * sizeof(float))) {
134 vst1_lane_f32(output, vy_lo, 0);
135 }
136 }
137 }