xref: /aosp_15_r20/external/XNNPACK/src/f16-vsigmoid/gen/vsigmoid-neonfp16arith-rr2-p2-nr1recps-x8.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f16-vsigmoid/neonfp16arith.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2022 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 
xnn_f16_vsigmoid_ukernel__neonfp16arith_rr2_p2_nr1recps_x8(size_t batch,const void * input,void * output,const union xnn_f16_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_vsigmoid_ukernel__neonfp16arith_rr2_p2_nr1recps_x8(
19     size_t batch,
20     const void* input,
21     void* output,
22     const union xnn_f16_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
23 {
24   assert(batch % sizeof(__fp16) == 0);
25 
26   const float16x8_t vmagic_bias = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.magic_bias));
27   const float16x8_t vminus_log2e = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.minus_log2e));
28   const float16x8_t vln2_hi = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.ln2_hi));
29   const float16x8_t vln2_lo = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.ln2_lo));
30   const float16x8_t vc2 = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.c2));
31   const float16x8_t vc1 = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.c1));
32   const float16x8_t vone = vmovq_n_f16(1.0f);
33   const float16x8_t vdenorm_cutoff = vreinterpretq_f16_u16(vld1q_dup_u16(&params->neonfp16arith_rr2_p2.denorm_cutoff));
34 
35   const __fp16* i = (const __fp16*) input;
36   __fp16* o = (__fp16*) output;
37   for (; batch >= 8 * sizeof(__fp16); batch -= 8 * sizeof(__fp16)) {
38     const float16x8_t vx = vld1q_f16(i); i += 8;
39 
40     const float16x8_t vz = vabsq_f16(vx);
41 
42     float16x8_t vn = vfmaq_f16(vmagic_bias, vz, vminus_log2e);
43     const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10));
44     vn = vsubq_f16(vn, vmagic_bias);
45 
46     float16x8_t vt = vfmaq_f16(vz, vn, vln2_hi);
47     vt = vfmaq_f16(vt, vn, vln2_lo);
48 
49     const float16x8_t vp = vfmaq_f16(vc1, vc2, vt);
50     vt = vmulq_f16(vt, vs);
51     const float16x8_t ve = vfmaq_f16(vs, vp, vt);
52     const float16x8_t vd = vaddq_f16(ve, vone);
53 
54     float16x8_t vr = vrecpeq_f16(vd);
55     const float16x8_t vadj = vrecpsq_f16(vr, vd);
56     vr = vmulq_f16(vr, vadj);
57 
58     float16x8_t vf = vmulq_f16(ve, vr);
59     vf = vreinterpretq_f16_u16(vbicq_u16(vreinterpretq_u16_f16(vf), vcagtq_f16(vx, vdenorm_cutoff)));
60     const uint16x8_t vm = vcltq_f16(vx, vmovq_n_f16(0.0f));
61     vf = vbslq_f16(vm, vf, vsubq_f16(vone, vf));
62 
63     vst1q_f16(o, vf); o += 8;
64   }
65   if XNN_UNLIKELY(batch != 0) {
66     const float16x8_t vx = vld1q_f16(i);
67 
68     const float16x8_t vz = vabsq_f16(vx);
69 
70     float16x8_t vn = vfmaq_f16(vmagic_bias, vz, vminus_log2e);
71     const float16x8_t vs = vreinterpretq_f16_s16(vshlq_n_s16(vreinterpretq_s16_f16(vn), 10));
72     vn = vsubq_f16(vn, vmagic_bias);
73 
74     float16x8_t vt = vfmaq_f16(vz, vn, vln2_hi);
75     vt = vfmaq_f16(vt, vn, vln2_lo);
76 
77     const float16x8_t vp = vfmaq_f16(vc1, vc2, vt);
78     vt = vmulq_f16(vt, vs);
79     const float16x8_t ve = vfmaq_f16(vs, vp, vt);
80     const float16x8_t vd = vaddq_f16(ve, vone);
81 
82     float16x8_t vr = vrecpeq_f16(vd);
83     const float16x8_t vadj = vrecpsq_f16(vr, vd);
84     vr = vmulq_f16(vr, vadj);
85 
86     float16x8_t vf = vmulq_f16(ve, vr);
87     vf = vreinterpretq_f16_u16(vbicq_u16(vreinterpretq_u16_f16(vf), vcagtq_f16(vx, vdenorm_cutoff)));
88     const uint16x8_t vm = vcltq_f16(vx, vmovq_n_f16(0.0f));
89     vf = vbslq_f16(vm, vf, vsubq_f16(vone, vf));
90 
91     float16x4_t vf_lo = vget_low_f16(vf);
92     if (batch & (4 * sizeof(__fp16))) {
93       vst1_f16(o, vf_lo); o += 4;
94       vf_lo = vget_high_f16(vf);
95     }
96     if (batch & (2 * sizeof(__fp16))) {
97       vst1_f16(o, vf_lo); o += 2;
98       vf_lo = vext_f16(vf_lo, vf_lo, 2);
99     }
100     if (batch & (1 * sizeof(__fp16))) {
101       vst1_lane_f16(o, vf_lo, 0);
102     }
103   }
104 }
105