xref: /aosp_15_r20/external/XNNPACK/src/f32-vsigmoid/gen/vsigmoid-scalar-rr2-lut64-p2-div-x2.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vsigmoid/scalar-rr2-lut64-p2-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_64[64];
20 
xnn_f32_vsigmoid_ukernel__scalar_rr2_lut64_p2_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_lut64_p2_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_lut64_p2.magic_bias;
30   const float vminus_log2e = params->scalar_rr2_lut64_p2.minus_log2e;
31   const uint32_t vindex_mask = UINT32_C(0x3F);
32   const float vln2_hi = params->scalar_rr2_lut64_p2.ln2_hi;
33   const float vln2_lo = params->scalar_rr2_lut64_p2.ln2_lo;
34   const float vc2 = params->scalar_rr2_lut64_p2.c2;
35   const float vone = params->scalar_rr2_lut64_p2.one;
36   const float vdenorm_cutoff = params->scalar_rr2_lut64_p2.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) << 17;
50     const uint32_t ve1 = float_as_uint32(vn1) << 17;
51 
52     const uint32_t vidx0 = float_as_uint32(vn0) & vindex_mask;
53     const float vs0 = uint32_as_float(xnn_table_exp2minus_k_over_64[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_64[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     float vp0 = vt0 * vc2;
67     float vp1 = vt1 * vc2;
68 
69     vp0 = vt0 - vp0 * vt0;
70     vp1 = vt1 - vp1 * vt1;
71 
72     const float vy0 = vs0 - vs0 * vp0;
73     const float vy1 = vs1 - vs1 * vp1;
74 
75     const float vd0 = vy0 + vone;
76     const float vd1 = vy1 + vone;
77 
78     float vf0 = vy0 / vd0;
79     float vf1 = vy1 / vd1;
80 
81     if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
82       vf0 = 0.0f;
83     }
84     if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
85       vf1 = 0.0f;
86     }
87 
88     if XNN_UNPREDICTABLE(vx0 > 0.0f) {
89       vf0 = vone - vf0;
90     }
91     if XNN_UNPREDICTABLE(vx1 > 0.0f) {
92       vf1 = vone - vf1;
93     }
94 
95     y[0] = vf0;
96     y[1] = vf1;
97     y += 2;
98   }
99   if XNN_UNLIKELY(n != 0) {
100     const float vx = *x;
101 
102     const float vz = fabsf(vx);
103 
104     float vn = vz * vminus_log2e + vmagic_bias;
105     const uint32_t ve = float_as_uint32(vn) << 17;
106     const uint32_t vidx = float_as_uint32(vn) & vindex_mask;
107     const float vs = uint32_as_float(xnn_table_exp2minus_k_over_64[vidx] + ve);
108     vn -= vmagic_bias;
109 
110     float vt = vn * vln2_hi + vz;
111     vt = vn * vln2_lo + vt;
112 
113     float vp = vt * vc2;
114     vp = vt - vp * vt;
115 
116     const float vy = vs - vs * vp;
117     const float vd = vy + vone;
118 
119     float vf = vy / vd;
120     if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
121       vf = 0.0f;
122     }
123     if XNN_UNPREDICTABLE(vx > 0.0f) {
124       vf = vone - vf;
125     }
126 
127     *y = vf;
128   }
129 }
130