xref: /aosp_15_r20/external/XNNPACK/src/f32-vsigmoid/gen/vsigmoid-scalar-rr2-lut2048-p1-div-x4.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vsigmoid/scalar-rr2-lut2048-p1-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 
18 // Note redefine as uint32[] to avoid redundant bitcasts.
19 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_2048[2048];
20 
xnn_f32_vsigmoid_ukernel__scalar_rr2_lut2048_p1_div_x4(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])21 void xnn_f32_vsigmoid_ukernel__scalar_rr2_lut2048_p1_div_x4(
22     size_t n,
23     const float* x,
24     float* y,
25     const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
26 {
27   assert(n % sizeof(float) == 0);
28 
29   const float vmagic_bias = params->scalar_rr2_lut2048_p1.magic_bias;
30   const float vminus_log2e = params->scalar_rr2_lut2048_p1.minus_log2e;
31   const uint32_t vindex_mask = UINT32_C(0x7FF);
32   const float vln2_hi = params->scalar_rr2_lut2048_p1.ln2_hi;
33   const float vln2_lo = params->scalar_rr2_lut2048_p1.ln2_lo;
34   const float vc1 = params->scalar_rr2_lut2048_p1.c1;
35   const float vone = params->scalar_rr2_lut2048_p1.one;
36   const float vdenorm_cutoff = params->scalar_rr2_lut2048_p1.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 uint32_t ve0 = float_as_uint32(vn0) << 12;
56     const uint32_t ve1 = float_as_uint32(vn1) << 12;
57     const uint32_t ve2 = float_as_uint32(vn2) << 12;
58     const uint32_t ve3 = float_as_uint32(vn3) << 12;
59 
60     const uint32_t vidx0 = float_as_uint32(vn0) & vindex_mask;
61     const float vs0 = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx0] + ve0);
62     const uint32_t vidx1 = float_as_uint32(vn1) & vindex_mask;
63     const float vs1 = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx1] + ve1);
64     const uint32_t vidx2 = float_as_uint32(vn2) & vindex_mask;
65     const float vs2 = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx2] + ve2);
66     const uint32_t vidx3 = float_as_uint32(vn3) & vindex_mask;
67     const float vs3 = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx3] + ve3);
68 
69     vn0 -= vmagic_bias;
70     vn1 -= vmagic_bias;
71     vn2 -= vmagic_bias;
72     vn3 -= vmagic_bias;
73 
74     float vt0 = vn0 * vln2_hi + vz0;
75     float vt1 = vn1 * vln2_hi + vz1;
76     float vt2 = vn2 * vln2_hi + vz2;
77     float vt3 = vn3 * vln2_hi + vz3;
78 
79     vt0 = vn0 * vln2_lo + vt0;
80     vt1 = vn1 * vln2_lo + vt1;
81     vt2 = vn2 * vln2_lo + vt2;
82     vt3 = vn3 * vln2_lo + vt3;
83 
84     const float vp0 = vt0 * vc1;
85     const float vp1 = vt1 * vc1;
86     const float vp2 = vt2 * vc1;
87     const float vp3 = vt3 * vc1;
88 
89     const float vy0 = vp0 * vs0 + vs0;
90     const float vy1 = vp1 * vs1 + vs1;
91     const float vy2 = vp2 * vs2 + vs2;
92     const float vy3 = vp3 * vs3 + vs3;
93 
94     const float vd0 = vy0 + vone;
95     const float vd1 = vy1 + vone;
96     const float vd2 = vy2 + vone;
97     const float vd3 = vy3 + vone;
98 
99     float vf0 = vy0 / vd0;
100     float vf1 = vy1 / vd1;
101     float vf2 = vy2 / vd2;
102     float vf3 = vy3 / vd3;
103 
104     if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
105       vf0 = 0.0f;
106     }
107     if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
108       vf1 = 0.0f;
109     }
110     if XNN_UNPREDICTABLE(vz2 > vdenorm_cutoff) {
111       vf2 = 0.0f;
112     }
113     if XNN_UNPREDICTABLE(vz3 > vdenorm_cutoff) {
114       vf3 = 0.0f;
115     }
116 
117     if XNN_UNPREDICTABLE(vx0 > 0.0f) {
118       vf0 = vone - vf0;
119     }
120     if XNN_UNPREDICTABLE(vx1 > 0.0f) {
121       vf1 = vone - vf1;
122     }
123     if XNN_UNPREDICTABLE(vx2 > 0.0f) {
124       vf2 = vone - vf2;
125     }
126     if XNN_UNPREDICTABLE(vx3 > 0.0f) {
127       vf3 = vone - vf3;
128     }
129 
130     y[0] = vf0;
131     y[1] = vf1;
132     y[2] = vf2;
133     y[3] = vf3;
134     y += 4;
135   }
136   if XNN_UNLIKELY(n != 0) {
137     do {
138       const float vx = *x++;
139 
140       const float vz = fabsf(vx);
141 
142       float vn = vz * vminus_log2e + vmagic_bias;
143       const uint32_t ve = float_as_uint32(vn) << 12;
144       const uint32_t vidx = float_as_uint32(vn) & vindex_mask;
145       const float vs = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx] + ve);
146       vn -= vmagic_bias;
147 
148       float vt = vn * vln2_hi + vz;
149       vt = vn * vln2_lo + vt;
150 
151       const float vp = vt * vc1;
152       const float vy = vp * vs + vs;
153 
154       const float vd = vy + vone;
155       float vf = vy / vd;
156       if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
157         vf = 0.0f;
158       }
159       if XNN_UNPREDICTABLE(vx > 0.0f) {
160         vf = vone - vf;
161       }
162 
163       *y++ = vf;
164 
165       n -= sizeof(float);
166     } while (n != 0);
167   }
168 }
169