xref: /aosp_15_r20/external/XNNPACK/src/f32-vsigmoid/gen/vsigmoid-scalar-rr2-lut2048-p1-div-x2.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_x2(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_x2(
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 >= 2 * sizeof(float); n -= 2 * sizeof(float)) {
39     const float vx0 = x[0];
40     const float vx1 = x[1];
41     x += 2;
42 
43     const float vz0 = fabsf(vx0);
44     const float vz1 = fabsf(vx1);
45 
46     float vn0 = vz0 * vminus_log2e + vmagic_bias;
47     float vn1 = vz1 * vminus_log2e + vmagic_bias;
48 
49     const uint32_t ve0 = float_as_uint32(vn0) << 12;
50     const uint32_t ve1 = float_as_uint32(vn1) << 12;
51 
52     const uint32_t vidx0 = float_as_uint32(vn0) & vindex_mask;
53     const float vs0 = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx0] + ve0);
54     const uint32_t vidx1 = float_as_uint32(vn1) & vindex_mask;
55     const float vs1 = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx1] + ve1);
56 
57     vn0 -= vmagic_bias;
58     vn1 -= vmagic_bias;
59 
60     float vt0 = vn0 * vln2_hi + vz0;
61     float vt1 = vn1 * vln2_hi + vz1;
62 
63     vt0 = vn0 * vln2_lo + vt0;
64     vt1 = vn1 * vln2_lo + vt1;
65 
66     const float vp0 = vt0 * vc1;
67     const float vp1 = vt1 * vc1;
68 
69     const float vy0 = vp0 * vs0 + vs0;
70     const float vy1 = vp1 * vs1 + vs1;
71 
72     const float vd0 = vy0 + vone;
73     const float vd1 = vy1 + vone;
74 
75     float vf0 = vy0 / vd0;
76     float vf1 = vy1 / vd1;
77 
78     if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
79       vf0 = 0.0f;
80     }
81     if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
82       vf1 = 0.0f;
83     }
84 
85     if XNN_UNPREDICTABLE(vx0 > 0.0f) {
86       vf0 = vone - vf0;
87     }
88     if XNN_UNPREDICTABLE(vx1 > 0.0f) {
89       vf1 = vone - vf1;
90     }
91 
92     y[0] = vf0;
93     y[1] = vf1;
94     y += 2;
95   }
96   if XNN_UNLIKELY(n != 0) {
97     const float vx = *x;
98 
99     const float vz = fabsf(vx);
100 
101     float vn = vz * vminus_log2e + vmagic_bias;
102     const uint32_t ve = float_as_uint32(vn) << 12;
103     const uint32_t vidx = float_as_uint32(vn) & vindex_mask;
104     const float vs = uint32_as_float(xnn_table_exp2minus_k_over_2048[vidx] + ve);
105     vn -= vmagic_bias;
106 
107     float vt = vn * vln2_hi + vz;
108     vt = vn * vln2_lo + vt;
109 
110     const float vp = vt * vc1;
111     const float vy = vp * vs + vs;
112 
113     const float vd = vy + vone;
114     float vf = vy / vd;
115     if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
116       vf = 0.0f;
117     }
118     if XNN_UNPREDICTABLE(vx > 0.0f) {
119       vf = vone - vf;
120     }
121 
122     *y = vf;
123   }
124 }
125