• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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_vsigmoid_ukernel__scalar_rr2_lut64_p2_div_x4(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])22 void xnn_f32_vsigmoid_ukernel__scalar_rr2_lut64_p2_div_x4(
23     size_t n,
24     const float* x,
25     float* y,
26     const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28   assert(n % sizeof(float) == 0);
29 
30   const float vmagic_bias = params->scalar_rr2_lut64_p2.magic_bias;
31   const float vminus_log2e = params->scalar_rr2_lut64_p2.minus_log2e;
32   const uint32_t vindex_mask = UINT32_C(0x3F);
33   const float vln2_hi = params->scalar_rr2_lut64_p2.ln2_hi;
34   const float vln2_lo = params->scalar_rr2_lut64_p2.ln2_lo;
35   const float vc2 = params->scalar_rr2_lut64_p2.c2;
36   const float vone = params->scalar_rr2_lut64_p2.one;
37   const float vdenorm_cutoff = params->scalar_rr2_lut64_p2.denorm_cutoff;
38 
39   for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
40     const float vx0 = x[0];
41     const float vx1 = x[1];
42     const float vx2 = x[2];
43     const float vx3 = x[3];
44     x += 4;
45 
46     const float vz0 = fabsf(vx0);
47     const float vz1 = fabsf(vx1);
48     const float vz2 = fabsf(vx2);
49     const float vz3 = fabsf(vx3);
50 
51     float vn0 = vz0 * vminus_log2e + vmagic_bias;
52     float vn1 = vz1 * vminus_log2e + vmagic_bias;
53     float vn2 = vz2 * vminus_log2e + vmagic_bias;
54     float vn3 = vz3 * vminus_log2e + vmagic_bias;
55 
56     const uint32_t ve0 = fp32_to_bits(vn0) << 17;
57     const uint32_t ve1 = fp32_to_bits(vn1) << 17;
58     const uint32_t ve2 = fp32_to_bits(vn2) << 17;
59     const uint32_t ve3 = fp32_to_bits(vn3) << 17;
60 
61     const uint32_t vidx0 = fp32_to_bits(vn0) & vindex_mask;
62     const float vs0 = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx0] + ve0);
63     const uint32_t vidx1 = fp32_to_bits(vn1) & vindex_mask;
64     const float vs1 = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx1] + ve1);
65     const uint32_t vidx2 = fp32_to_bits(vn2) & vindex_mask;
66     const float vs2 = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx2] + ve2);
67     const uint32_t vidx3 = fp32_to_bits(vn3) & vindex_mask;
68     const float vs3 = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx3] + ve3);
69 
70     vn0 -= vmagic_bias;
71     vn1 -= vmagic_bias;
72     vn2 -= vmagic_bias;
73     vn3 -= vmagic_bias;
74 
75     float vt0 = vn0 * vln2_hi + vz0;
76     float vt1 = vn1 * vln2_hi + vz1;
77     float vt2 = vn2 * vln2_hi + vz2;
78     float vt3 = vn3 * vln2_hi + vz3;
79 
80     vt0 = vn0 * vln2_lo + vt0;
81     vt1 = vn1 * vln2_lo + vt1;
82     vt2 = vn2 * vln2_lo + vt2;
83     vt3 = vn3 * vln2_lo + vt3;
84 
85     float vp0 = vt0 * vc2;
86     float vp1 = vt1 * vc2;
87     float vp2 = vt2 * vc2;
88     float vp3 = vt3 * vc2;
89 
90     vp0 = vt0 - vp0 * vt0;
91     vp1 = vt1 - vp1 * vt1;
92     vp2 = vt2 - vp2 * vt2;
93     vp3 = vt3 - vp3 * vt3;
94 
95     const float vy0 = vs0 - vs0 * vp0;
96     const float vy1 = vs1 - vs1 * vp1;
97     const float vy2 = vs2 - vs2 * vp2;
98     const float vy3 = vs3 - vs3 * vp3;
99 
100     const float vd0 = vy0 + vone;
101     const float vd1 = vy1 + vone;
102     const float vd2 = vy2 + vone;
103     const float vd3 = vy3 + vone;
104 
105     float vf0 = vy0 / vd0;
106     float vf1 = vy1 / vd1;
107     float vf2 = vy2 / vd2;
108     float vf3 = vy3 / vd3;
109 
110     if XNN_UNPREDICTABLE(vz0 > vdenorm_cutoff) {
111       vf0 = 0.0f;
112     }
113     if XNN_UNPREDICTABLE(vz1 > vdenorm_cutoff) {
114       vf1 = 0.0f;
115     }
116     if XNN_UNPREDICTABLE(vz2 > vdenorm_cutoff) {
117       vf2 = 0.0f;
118     }
119     if XNN_UNPREDICTABLE(vz3 > vdenorm_cutoff) {
120       vf3 = 0.0f;
121     }
122 
123     if XNN_UNPREDICTABLE(vx0 > 0.0f) {
124       vf0 = vone - vf0;
125     }
126     if XNN_UNPREDICTABLE(vx1 > 0.0f) {
127       vf1 = vone - vf1;
128     }
129     if XNN_UNPREDICTABLE(vx2 > 0.0f) {
130       vf2 = vone - vf2;
131     }
132     if XNN_UNPREDICTABLE(vx3 > 0.0f) {
133       vf3 = vone - vf3;
134     }
135 
136     y[0] = vf0;
137     y[1] = vf1;
138     y[2] = vf2;
139     y[3] = vf3;
140     y += 4;
141   }
142   if XNN_UNLIKELY(n != 0) {
143     do {
144       const float vx = *x++;
145 
146       const float vz = fabsf(vx);
147 
148       float vn = vz * vminus_log2e + vmagic_bias;
149       const uint32_t ve = fp32_to_bits(vn) << 17;
150       const uint32_t vidx = fp32_to_bits(vn) & vindex_mask;
151       const float vs = fp32_from_bits(xnn_table_exp2minus_k_over_64[vidx] + ve);
152       vn -= vmagic_bias;
153 
154       float vt = vn * vln2_hi + vz;
155       vt = vn * vln2_lo + vt;
156 
157       float vp = vt * vc2;
158       vp = vt - vp * vt;
159 
160       const float vy = vs - vs * vp;
161       const float vd = vy + vone;
162 
163       float vf = vy / vd;
164       if XNN_UNPREDICTABLE(vz > vdenorm_cutoff) {
165         vf = 0.0f;
166       }
167       if XNN_UNPREDICTABLE(vx > 0.0f) {
168         vf = vone - vf;
169       }
170 
171       *y++ = vf;
172 
173       n -= sizeof(float);
174     } while (n != 0);
175   }
176 }
177