• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-vsigmoid/avx2-rr1-p5.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 
12 #include <immintrin.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/vunary.h>
16 
17 
xnn_f32_vsigmoid_ukernel__avx2_rr1_p5_div_x24(size_t n,const float * x,float * y,const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f32_vsigmoid_ukernel__avx2_rr1_p5_div_x24(
19     size_t n,
20     const float* x,
21     float* y,
22     const union xnn_f32_sigmoid_params params[restrict XNN_MIN_ELEMENTS(1)])
23 {
24   assert(n % sizeof(float) == 0);
25 
26   const __m256 vsign_mask = _mm256_load_ps(params->avx2_rr1_p5.sign_mask);
27   const __m256 vmagic_bias = _mm256_load_ps(params->avx2_rr1_p5.magic_bias);
28   const __m256 vlog2e = _mm256_load_ps(params->avx2_rr1_p5.log2e);
29   const __m256 vminus_ln2 = _mm256_load_ps(params->avx2_rr1_p5.minus_ln2);
30   const __m256 vc5 = _mm256_load_ps(params->avx2_rr1_p5.c5);
31   const __m256 vc4 = _mm256_load_ps(params->avx2_rr1_p5.c4);
32   const __m256 vc3 = _mm256_load_ps(params->avx2_rr1_p5.c3);
33   const __m256 vc2 = _mm256_load_ps(params->avx2_rr1_p5.c2);
34   const __m256 vc1 = _mm256_load_ps(params->avx2_rr1_p5.c1);
35   const __m256 vone = _mm256_load_ps(params->avx2_rr1_p5.one);
36   const __m256 vdenorm_cutoff = _mm256_load_ps(params->avx2_rr1_p5.denorm_cutoff);
37 
38   for (; n >= 24 * sizeof(float); n -= 24 * sizeof(float)) {
39     const __m256 vx0 = _mm256_loadu_ps(x);
40     const __m256 vx1 = _mm256_loadu_ps(x + 8);
41     const __m256 vx2 = _mm256_loadu_ps(x + 16);
42     x += 24;
43 
44     const __m256 vz0 = _mm256_or_ps(vx0, vsign_mask);
45     const __m256 vz1 = _mm256_or_ps(vx1, vsign_mask);
46     const __m256 vz2 = _mm256_or_ps(vx2, vsign_mask);
47 
48     __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
49     __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
50     __m256 vn2 = _mm256_fmadd_ps(vz2, vlog2e, vmagic_bias);
51 
52     const __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
53     const __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
54     const __m256 vs2 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn2), 23));
55 
56     vn0 = _mm256_sub_ps(vn0, vmagic_bias);
57     vn1 = _mm256_sub_ps(vn1, vmagic_bias);
58     vn2 = _mm256_sub_ps(vn2, vmagic_bias);
59 
60     __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
61     __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
62     __m256 vt2 = _mm256_fmadd_ps(vn2, vminus_ln2, vz2);
63 
64     __m256 vp0 = _mm256_fmadd_ps(vc5, vt0, vc4);
65     __m256 vp1 = _mm256_fmadd_ps(vc5, vt1, vc4);
66     __m256 vp2 = _mm256_fmadd_ps(vc5, vt2, vc4);
67 
68     vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
69     vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
70     vp2 = _mm256_fmadd_ps(vp2, vt2, vc3);
71 
72     vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
73     vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
74     vp2 = _mm256_fmadd_ps(vp2, vt2, vc2);
75 
76     vp0 = _mm256_fmadd_ps(vp0, vt0, vc1);
77     vp1 = _mm256_fmadd_ps(vp1, vt1, vc1);
78     vp2 = _mm256_fmadd_ps(vp2, vt2, vc1);
79 
80     vt0 = _mm256_mul_ps(vt0, vs0);
81     vt1 = _mm256_mul_ps(vt1, vs1);
82     vt2 = _mm256_mul_ps(vt2, vs2);
83 
84     const __m256 ve0 = _mm256_fmadd_ps(vt0, vp0, vs0);
85     const __m256 ve1 = _mm256_fmadd_ps(vt1, vp1, vs1);
86     const __m256 ve2 = _mm256_fmadd_ps(vt2, vp2, vs2);
87 
88     const __m256 vd0 = _mm256_add_ps(ve0, vone);
89     const __m256 vd1 = _mm256_add_ps(ve1, vone);
90     const __m256 vd2 = _mm256_add_ps(ve2, vone);
91 
92     __m256 vf0 = _mm256_div_ps(ve0, vd0);
93     __m256 vf1 = _mm256_div_ps(ve1, vd1);
94     __m256 vf2 = _mm256_div_ps(ve2, vd2);
95 
96     vf0 = _mm256_andnot_ps(_mm256_cmp_ps(vz0, vdenorm_cutoff, _CMP_LT_OS), vf0);
97     vf1 = _mm256_andnot_ps(_mm256_cmp_ps(vz1, vdenorm_cutoff, _CMP_LT_OS), vf1);
98     vf2 = _mm256_andnot_ps(_mm256_cmp_ps(vz2, vdenorm_cutoff, _CMP_LT_OS), vf2);
99 
100     vf0 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf0), vf0, vx0);
101     vf1 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf1), vf1, vx1);
102     vf2 = _mm256_blendv_ps(_mm256_sub_ps(vone, vf2), vf2, vx2);
103 
104     _mm256_storeu_ps(y, vf0);
105     _mm256_storeu_ps(y + 8, vf1);
106     _mm256_storeu_ps(y + 16, vf2);
107     y += 24;
108   }
109   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
110     const __m256 vx = _mm256_loadu_ps(x);
111     x += 8;
112 
113     const __m256 vz = _mm256_or_ps(vx, vsign_mask);
114 
115     __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
116     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
117     vn = _mm256_sub_ps(vn, vmagic_bias);
118 
119     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
120 
121     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
122     vp = _mm256_fmadd_ps(vp, vt, vc3);
123     vp = _mm256_fmadd_ps(vp, vt, vc2);
124     vp = _mm256_fmadd_ps(vp, vt, vc1);
125 
126     vt = _mm256_mul_ps(vt, vs);
127     const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
128 
129     const __m256 vd = _mm256_add_ps(ve, vone);
130     __m256 vf = _mm256_div_ps(ve, vd);
131 
132     vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
133     vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
134 
135     _mm256_storeu_ps(y, vf);
136     y += 8;
137   }
138   if XNN_UNLIKELY(n != 0) {
139     assert(n >= 1 * sizeof(float));
140     assert(n <= 7 * sizeof(float));
141     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &params->avx2_rr1_p5.mask_table[7] - n));
142 
143     const __m256 vx = _mm256_maskload_ps(x, vmask);
144 
145     const __m256 vz = _mm256_or_ps(vx, vsign_mask);
146 
147     __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
148     const __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
149     vn = _mm256_sub_ps(vn, vmagic_bias);
150 
151     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
152 
153     __m256 vp = _mm256_fmadd_ps(vc5, vt, vc4);
154     vp = _mm256_fmadd_ps(vp, vt, vc3);
155     vp = _mm256_fmadd_ps(vp, vt, vc2);
156     vp = _mm256_fmadd_ps(vp, vt, vc1);
157 
158     vt = _mm256_mul_ps(vt, vs);
159     const __m256 ve = _mm256_fmadd_ps(vt, vp, vs);
160 
161     const __m256 vd = _mm256_add_ps(ve, vone);
162     __m256 vf = _mm256_div_ps(ve, vd);
163 
164     vf = _mm256_andnot_ps(_mm256_cmp_ps(vz, vdenorm_cutoff, _CMP_LT_OS), vf);
165     vf = _mm256_blendv_ps(_mm256_sub_ps(vone, vf), vf, vx);
166 
167     __m128 vf_lo = _mm256_castps256_ps128(vf);
168     if (n & (4 * sizeof(float))) {
169       _mm_storeu_ps(y, vf_lo);
170       vf_lo = _mm256_extractf128_ps(vf, 1);
171       y += 4;
172     }
173     if (n & (2 * sizeof(float))) {
174       _mm_storel_pi((__m64*) y, vf_lo);
175       vf_lo = _mm_movehl_ps(vf_lo, vf_lo);
176       y += 2;
177     }
178     if (n & (1 * sizeof(float))) {
179       _mm_store_ss(y, vf_lo);
180     }
181   }
182 }
183