xref: /aosp_15_r20/external/XNNPACK/src/f32-vsigmoid/gen/vsigmoid-scalar-rr2-p5-div-x4.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vsigmoid/scalar-rr2-p5-div.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2019 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 <xnnpack/common.h>
14 #include <xnnpack/math.h>
15 #include <xnnpack/vunary.h>
16 
17 
xnn_f32_vsigmoid_ukernel__scalar_rr2_p5_div_x4(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f32_vsigmoid_ukernel__scalar_rr2_p5_div_x4(
19     size_t n,
20     const float* x,
21     float* y,
22     const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
23 {
24   assert(n % sizeof(float) == 0);
25 
26   const float vmagic_bias = params->scalar_rr2_p5.magic_bias;
27   const float vminus_log2e = params->scalar_rr2_p5.minus_log2e;
28   const float vln2_hi = params->scalar_rr2_p5.ln2_hi;
29   const float vln2_lo = params->scalar_rr2_p5.ln2_lo;
30   const float vc5 = params->scalar_rr2_p5.c5;
31   const float vc4 = params->scalar_rr2_p5.c4;
32   const float vc3 = params->scalar_rr2_p5.c3;
33   const float vc2 = params->scalar_rr2_p5.c2;
34   const float vc1 = params->scalar_rr2_p5.c1;
35   const float vone = params->scalar_rr2_p5.one;
36   const float vdenorm_cutoff = params->scalar_rr2_p5.denorm_cutoff;
37 
38   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
39     const float vx0 = x[0];
40     const float vx1 = x[1];
41     const float vx2 = x[2];
42     const float vx3 = x[3];
43     x += 4;
44 
45     const float vz0 = fabsf(vx0);
46     const float vz1 = fabsf(vx1);
47     const float vz2 = fabsf(vx2);
48     const float vz3 = fabsf(vx3);
49 
50     float vn0 = vz0 * vminus_log2e + vmagic_bias;
51     float vn1 = vz1 * vminus_log2e + vmagic_bias;
52     float vn2 = vz2 * vminus_log2e + vmagic_bias;
53     float vn3 = vz3 * vminus_log2e + vmagic_bias;
54 
55     const float vs0 = uint32_as_float(float_as_uint32(vn0) << 23);
56     const float vs1 = uint32_as_float(float_as_uint32(vn1) << 23);
57     const float vs2 = uint32_as_float(float_as_uint32(vn2) << 23);
58     const float vs3 = uint32_as_float(float_as_uint32(vn3) << 23);
59 
60     vn0 -= vmagic_bias;
61     vn1 -= vmagic_bias;
62     vn2 -= vmagic_bias;
63     vn3 -= vmagic_bias;
64 
65     float vt0 = vn0 * vln2_hi + vz0;
66     float vt1 = vn1 * vln2_hi + vz1;
67     float vt2 = vn2 * vln2_hi + vz2;
68     float vt3 = vn3 * vln2_hi + vz3;
69 
70     vt0 = vn0 * vln2_lo + vt0;
71     vt1 = vn1 * vln2_lo + vt1;
72     vt2 = vn2 * vln2_lo + vt2;
73     vt3 = vn3 * vln2_lo + vt3;
74 
75     float vp0 = vt0 * vc5 + vc4;
76     float vp1 = vt1 * vc5 + vc4;
77     float vp2 = vt2 * vc5 + vc4;
78     float vp3 = vt3 * vc5 + vc4;
79 
80     vp0 = vt0 * vp0 + vc3;
81     vp1 = vt1 * vp1 + vc3;
82     vp2 = vt2 * vp2 + vc3;
83     vp3 = vt3 * vp3 + vc3;
84 
85     vp0 = vt0 * vp0 + vc2;
86     vp1 = vt1 * vp1 + vc2;
87     vp2 = vt2 * vp2 + vc2;
88     vp3 = vt3 * vp3 + vc2;
89 
90     vp0 = vt0 * vp0 + vc1;
91     vp1 = vt1 * vp1 + vc1;
92     vp2 = vt2 * vp2 + vc1;
93     vp3 = vt3 * vp3 + vc1;
94 
95     vt0 *= vs0;
96     vt1 *= vs1;
97     vt2 *= vs2;
98     vt3 *= vs3;
99 
100     const float ve0 = vt0 * vp0 + vs0;
101     const float ve1 = vt1 * vp1 + vs1;
102     const float ve2 = vt2 * vp2 + vs2;
103     const float ve3 = vt3 * vp3 + vs3;
104 
105     const float vd0 = ve0 + vone;
106     const float vd1 = ve1 + vone;
107     const float vd2 = ve2 + vone;
108     const float vd3 = ve3 + vone;
109 
110     float vf0 = ve0 / vd0;
111     float vf1 = ve1 / vd1;
112     float vf2 = ve2 / vd2;
113     float vf3 = ve3 / vd3;
114 
115     if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
116       vf0 = 0.0f;
117     }
118     if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
119       vf1 = 0.0f;
120     }
121     if XNN_UNPREDICTABLE(vz2 > vdenorm_cutoff) {
122       vf2 = 0.0f;
123     }
124     if XNN_UNPREDICTABLE(vz3 > vdenorm_cutoff) {
125       vf3 = 0.0f;
126     }
127 
128     if XNN_UNPREDICTABLE(vx0 > 0.0f) {
129       vf0 = vone - vf0;
130     }
131     if XNN_UNPREDICTABLE(vx1 > 0.0f) {
132       vf1 = vone - vf1;
133     }
134     if XNN_UNPREDICTABLE(vx2 > 0.0f) {
135       vf2 = vone - vf2;
136     }
137     if XNN_UNPREDICTABLE(vx3 > 0.0f) {
138       vf3 = vone - vf3;
139     }
140 
141     y[0] = vf0;
142     y[1] = vf1;
143     y[2] = vf2;
144     y[3] = vf3;
145     y += 4;
146   }
147   if XNN_UNLIKELY(n != 0) {
148     do {
149       const float vx = *x++;
150 
151       const float vz = fabsf(vx);
152 
153       float vn = vz * vminus_log2e + vmagic_bias;
154       const float vs = uint32_as_float(float_as_uint32(vn) << 23);
155       vn -= vmagic_bias;
156 
157       float vt = vn * vln2_hi + vz;
158       vt = vn * vln2_lo + vt;
159 
160       float vp = vt * vc5 + vc4;
161       vp = vt * vp + vc3;
162       vp = vt * vp + vc2;
163       vp = vt * vp + vc1;
164 
165       vt *= vs;
166       const float ve = vt * vp + vs;
167       const float vd = ve + vone;
168 
169       float vf = ve / vd;
170       if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
171         vf = 0.0f;
172       }
173       if XNN_UNPREDICTABLE(vx > 0.0f) {
174         vf = vone - vf;
175       }
176 
177       *y++ = vf;
178 
179       n -= sizeof(float);
180     } while (n != 0);
181   }
182 }
183