Debian Codegraph @

src/f16-vrsqrt/gen/f16-vrsqrt-neonfp16arith-rsqrt-u8.c

raw symbols SCIP
1 // Auto-generated file. Do not edit!
2 // Template: src/f16-vrsqrt/neonfp16arith-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
12 #include <arm_neon.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/vunary.h"
16
17
18 // In the following, we do a single Newton-Raphson step on the equation
19 // $x^{-2} - a$, which expands to:
20 //
21 // $$x_{k+1} = 0.5 * x_k * (3.0 - a * x_k^2)$$
22 //
23 // So we do the following steps:
24 //
25 // 1. t0 = x_k
26 // 2. t1 = t0 * t0 (x_k^2)
27 // 3. t2 = a * t1 (a * x_k^2)
28 // 4. t3 = 3.0 - t2 (3.0 - a * x_k^2)
29 // 5. t4 = 0.5 * t0 (0.5 * x_k)
30 // 6. y = t3 * t4 (0.5 * x_k * (3.0 - a * x_k^2))
31 //
32 // Where $x_k$ is the original approximation and `y` contains the improved
33 // approximation $x_{k+1}$.
34 //
35 // Note that Arm/NEON provides the `vrsqrtsq_f16` instruction, which does steps
36 // 3-5 in the above in a single instruction.
37
38
39 void xnn_f16_vrsqrt_ukernel__neonfp16arith_rsqrt_u8(
40 size_t batch,
41 const xnn_float16* input,
42 xnn_float16* output,
43 const struct xnn_f16_default_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
44 {
45 assert(batch != 0);
46 assert(batch % sizeof(uint16_t) == 0);
47 assert(input != NULL);
48 assert(output != NULL);
49
50 const uint16_t* i = (const uint16_t*) input;
51 uint16_t* o = (uint16_t*) output;
52
53 for (; batch >= 8 * sizeof(uint16_t); batch -= 8 * sizeof(uint16_t)) {
54 const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i)); i += 8;
55
56 const float16x8_t vt0 = vrsqrteq_f16(vx);
57 const float16x8_t vt1 = vmulq_f16(vt0, vt0);
58 const float16x8_t vt2 = vrsqrtsq_f16(vx, vt1);
59 const float16x8_t vy = vmulq_f16(vt0, vt2);
60
61 vst1q_u16(o, vreinterpretq_u16_f16(vy)); o += 8;
62 }
63 if XNN_UNLIKELY(batch != 0) {
64 const float16x8_t vx = vreinterpretq_f16_u16(vld1q_u16(i));
65 const float16x4_t vx_lo = vget_low_f16(vx);
66 const float16x4_t vx_hi = vget_high_f16(vx);
67
68 const float16x4_t vt0 = vrsqrte_f16(vx_lo);
69 const float16x4_t vt1 = vmul_f16(vt0, vt0);
70 const float16x4_t vt2 = vrsqrts_f16(vx_lo, vt1);
71 float16x4_t vy_lo = vmul_f16(vt0, vt2);
72
73 if (batch & (4 * sizeof(uint16_t))) {
74 vst1_u16(o, vreinterpret_u16_f16(vy_lo)); o += 4;
75
76 const float16x4_t vt0 = vrsqrte_f16(vx_hi);
77 const float16x4_t vt1 = vmul_f16(vt0, vt0);
78 const float16x4_t vt2 = vrsqrts_f16(vx_hi, vt1);
79 vy_lo = vmul_f16(vt0, vt2);
80 }
81
82 if (batch & (2 * sizeof(uint16_t))) {
83 vst1_lane_u32((void*) o, vreinterpret_u32_f16(vy_lo), 0); o += 2;
84 vy_lo = vext_f16(vy_lo, vy_lo, 2);
85 }
86
87 if (batch & (1 * sizeof(uint16_t))) {
88 vst1_lane_u16(o, vreinterpret_u16_f16(vy_lo), 0);
89 }
90 }
91 }