1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-sigmoid/scalar-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/vunary.h>
15 
16 #include <fp16/bitcasts.h>
17 
18 
19 // Note redefine as uint32[] to avoid redundant bitcasts.
20 extern XNN_INTERNAL const uint32_t xnn_table_exp2minus_k_over_64[64];
21 
xnn_f32_sigmoid_ukernel__scalar_lut64_p2_div_x2(size_t n,const float * x,float * y,const void * params)22 void xnn_f32_sigmoid_ukernel__scalar_lut64_p2_div_x2(
23     size_t n,
24     const float* x,
25     float* y,
26     const void* params)
27 {
28   assert(n % sizeof(float) == 0);
29 
30   const float vmagic_bias = 0x1.800000p17f;
31   const float vminus_log2e = -0x1.715476p0f;
32   const uint32_t vindex_mask = UINT32_C(0x3F);
33   const float vln2_hi =  0x1.630000p-1f;
34   const float vln2_lo = -0x1.BD0106p-13f;
35   const float vc2 = 0x1.FFFF0Ap-2f;
36   const float vone = 1.0f;
37   const float vdenorm_cutoff = 0x1.5D589Ep+6f;
38 
39   for (; n >= 2 * sizeof(float); n -= 2 * sizeof(float)) {
40     const float vx0 = x[0];
41     const float vx1 = x[1];
42     x += 2;
43 
44     const float vz0 = fabsf(vx0);
45     const float vz1 = fabsf(vx1);
46 
47     float vn0 = vz0 * vminus_log2e + vmagic_bias;
48     float vn1 = vz1 * vminus_log2e + vmagic_bias;
49 
50     const uint32_t ve0 = fp32_to_bits(vn0) << 17;
51     const uint32_t ve1 = fp32_to_bits(vn1) << 17;
52 
53     const uint32_t vidx0 = fp32_to_bits(vn0) & vindex_mask;
54     const float vs0 = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx0] + ve0);
55     const uint32_t vidx1 = fp32_to_bits(vn1) & vindex_mask;
56     const float vs1 = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx1] + ve1);
57 
58     vn0 -= vmagic_bias;
59     vn1 -= vmagic_bias;
60 
61     float vt0 = vn0 * vln2_hi + vz0;
62     float vt1 = vn1 * vln2_hi + vz1;
63 
64     vt0 = vn0 * vln2_lo + vt0;
65     vt1 = vn1 * vln2_lo + vt1;
66 
67     float vp0 = vt0 * vc2;
68     float vp1 = vt1 * vc2;
69 
70     vp0 = vt0 - vp0 * vt0;
71     vp1 = vt1 - vp1 * vt1;
72 
73     const float vy0 = vs0 - vs0 * vp0;
74     const float vy1 = vs1 - vs1 * vp1;
75 
76     const float vd0 = vy0 + vone;
77     const float vd1 = vy1 + vone;
78 
79     float vf0 = vy0 / vd0;
80     float vf1 = vy1 / vd1;
81 
82     if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
83       vf0 = 0.0f;
84     }
85     if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
86       vf1 = 0.0f;
87     }
88 
89     if XNN_UNPREDICTABLE(vx0 > 0.0f) {
90       vf0 = vone - vf0;
91     }
92     if XNN_UNPREDICTABLE(vx1 > 0.0f) {
93       vf1 = vone - vf1;
94     }
95 
96     y[0] = vf0;
97     y[1] = vf1;
98     y += 2;
99   }
100   if XNN_UNLIKELY(n != 0) {
101     const float vx = *x;
102 
103     const float vz = fabsf(vx);
104 
105     float vn = vz * vminus_log2e + vmagic_bias;
106     const uint32_t ve = fp32_to_bits(vn) << 17;
107     const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
108     const float vs = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx] + ve);
109     vn -= vmagic_bias;
110 
111     float vt = vn * vln2_hi + vz;
112     vt = vn * vln2_lo + vt;
113 
114     float vp = vt * vc2;
115     vp = vt - vp * vt;
116 
117     const float vy = vs - vs * vp;
118     const float vd = vy + vone;
119 
120     float vf = vy / vd;
121     if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
122       vf = 0.0f;
123     }
124     if XNN_UNPREDICTABLE(vx > 0.0f) {
125       vf = vone - vf;
126     }
127 
128     *y = vf;
129   }
130 }
131