• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-velu/avx2-rr1-p6.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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_velu_ukernel__avx2_rr1_p6_x16(size_t n,const float * x,float * y,const union xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f32_velu_ukernel__avx2_rr1_p6_x16(
19     size_t n,
20     const float* x,
21     float* y,
22     const union xnn_f32_elu_params params[restrict XNN_MIN_ELEMENTS(1)])
23 {
24   assert(n % sizeof(float) == 0);
25 
26   const __m256 vprescale = _mm256_load_ps(params->avx2_rr1_p6.prescale);
27   const __m256 valpha = _mm256_load_ps(params->avx2_rr1_p6.alpha);
28   const __m256 vbeta = _mm256_load_ps(params->avx2_rr1_p6.beta);
29   const __m256 vsat_cutoff = _mm256_load_ps(params->avx2_rr1_p6.sat_cutoff);
30   const __m256 vmagic_bias = _mm256_load_ps(params->avx2_rr1_p6.magic_bias);
31   const __m256 vlog2e = _mm256_load_ps(params->avx2_rr1_p6.log2e);
32   const __m256 vminus_ln2 = _mm256_load_ps(params->avx2_rr1_p6.minus_ln2);
33   const __m256 vc6 = _mm256_load_ps(params->avx2_rr1_p6.c6);
34   const __m256 vc5 = _mm256_load_ps(params->avx2_rr1_p6.c5);
35   const __m256 vc4 = _mm256_load_ps(params->avx2_rr1_p6.c4);
36   const __m256 vc3 = _mm256_load_ps(params->avx2_rr1_p6.c3);
37   const __m256 vc2 = _mm256_load_ps(params->avx2_rr1_p6.c2);
38 
39   for (; n >= 16 * sizeof(float); n -= 16 * sizeof(float)) {
40     __m256 vx0 = _mm256_loadu_ps(x);
41     __m256 vx1 = _mm256_loadu_ps(x + 8);
42     x += 16;
43 
44     const __m256 vz0 = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx0, vprescale));
45     const __m256 vz1 = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx1, vprescale));
46 
47     __m256 vn0 = _mm256_fmadd_ps(vz0, vlog2e, vmagic_bias);
48     __m256 vn1 = _mm256_fmadd_ps(vz1, vlog2e, vmagic_bias);
49 
50     __m256 vs0 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn0), 23));
51     vn0 = _mm256_sub_ps(vn0, vmagic_bias);
52     __m256 vs1 = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn1), 23));
53     vn1 = _mm256_sub_ps(vn1, vmagic_bias);
54 
55     __m256 vt0 = _mm256_fmadd_ps(vn0, vminus_ln2, vz0);
56     __m256 vt1 = _mm256_fmadd_ps(vn1, vminus_ln2, vz1);
57 
58     __m256 vp0 = _mm256_fmadd_ps(vc6, vt0, vc5);
59     __m256 vp1 = _mm256_fmadd_ps(vc6, vt1, vc5);
60 
61     vp0 = _mm256_fmadd_ps(vp0, vt0, vc4);
62     vp1 = _mm256_fmadd_ps(vp1, vt1, vc4);
63 
64     vp0 = _mm256_fmadd_ps(vp0, vt0, vc3);
65     vp1 = _mm256_fmadd_ps(vp1, vt1, vc3);
66 
67     vp0 = _mm256_fmadd_ps(vp0, vt0, vc2);
68     vp1 = _mm256_fmadd_ps(vp1, vt1, vc2);
69 
70     vp0 = _mm256_mul_ps(vp0, vt0);
71     vt0 = _mm256_mul_ps(vt0, vs0);
72     vp1 = _mm256_mul_ps(vp1, vt1);
73     vt1 = _mm256_mul_ps(vt1, vs1);
74 
75     vs0 = _mm256_fmsub_ps(vs0, valpha, valpha);
76     vp0 = _mm256_fmadd_ps(vp0, vt0, vt0);
77     vs1 = _mm256_fmsub_ps(vs1, valpha, valpha);
78     vp1 = _mm256_fmadd_ps(vp1, vt1, vt1);
79 
80     const __m256 ve0 = _mm256_fmadd_ps(vp0, valpha, vs0);
81     vx0 = _mm256_mul_ps(vx0, vbeta);
82     const __m256 ve1 = _mm256_fmadd_ps(vp1, valpha, vs1);
83     vx1 = _mm256_mul_ps(vx1, vbeta);
84 
85     const __m256 vy0 = _mm256_blendv_ps(vx0, ve0, vx0);
86     const __m256 vy1 = _mm256_blendv_ps(vx1, ve1, vx1);
87 
88     _mm256_storeu_ps(y, vy0);
89     _mm256_storeu_ps(y + 8, vy1);
90     y += 16;
91   }
92   for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) {
93     __m256 vx = _mm256_loadu_ps(x);
94     x += 8;
95 
96     const __m256 vz = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx, vprescale));
97 
98     __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
99     __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
100     vn = _mm256_sub_ps(vn, vmagic_bias);
101 
102     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
103 
104     __m256 vp = _mm256_fmadd_ps(vc6, vt, vc5);
105     vp = _mm256_fmadd_ps(vp, vt, vc4);
106     vp = _mm256_fmadd_ps(vp, vt, vc3);
107     vp = _mm256_fmadd_ps(vp, vt, vc2);
108     vp = _mm256_mul_ps(vp, vt);
109 
110     vt = _mm256_mul_ps(vt, vs);
111     vs = _mm256_fmsub_ps(vs, valpha, valpha);
112     vp = _mm256_fmadd_ps(vp, vt, vt);
113     const __m256 ve = _mm256_fmadd_ps(vp, valpha, vs);
114 
115     vx = _mm256_mul_ps(vx, vbeta);
116     const __m256 vy = _mm256_blendv_ps(vx, ve, vx);
117 
118     _mm256_storeu_ps(y, vy);
119     y += 8;
120   }
121   if XNN_UNLIKELY(n != 0) {
122     assert(n >= 1 * sizeof(float));
123     assert(n <= 7 * sizeof(float));
124     const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &params->avx2_rr1_p6.mask_table[7] - n));
125 
126     __m256 vx = _mm256_maskload_ps(x, vmask);
127 
128     const __m256 vz = _mm256_max_ps(vsat_cutoff, _mm256_mul_ps(vx, vprescale));
129 
130     __m256 vn = _mm256_fmadd_ps(vz, vlog2e, vmagic_bias);
131     __m256 vs = _mm256_castsi256_ps(_mm256_slli_epi32(_mm256_castps_si256(vn), 23));
132     vn = _mm256_sub_ps(vn, vmagic_bias);
133 
134     __m256 vt = _mm256_fmadd_ps(vn, vminus_ln2, vz);
135 
136     __m256 vp = _mm256_fmadd_ps(vc6, vt, vc5);
137     vp = _mm256_fmadd_ps(vp, vt, vc4);
138     vp = _mm256_fmadd_ps(vp, vt, vc3);
139     vp = _mm256_fmadd_ps(vp, vt, vc2);
140     vp = _mm256_mul_ps(vp, vt);
141 
142     vt = _mm256_mul_ps(vt, vs);
143     vs = _mm256_fmsub_ps(vs, valpha, valpha);
144     vp = _mm256_fmadd_ps(vp, vt, vt);
145     const __m256 ve = _mm256_fmadd_ps(vp, valpha, vs);
146 
147     vx = _mm256_mul_ps(vx, vbeta);
148     const __m256 vy = _mm256_blendv_ps(vx, ve, vx);
149 
150     __m128 vy_lo = _mm256_castps256_ps128(vy);
151     if (n & (4 * sizeof(float))) {
152       _mm_storeu_ps(y, vy_lo);
153       vy_lo = _mm256_extractf128_ps(vy, 1);
154       y += 4;
155     }
156     if (n & (2 * sizeof(float))) {
157       _mm_storel_pi((__m64*) y, vy_lo);
158       vy_lo = _mm_movehl_ps(vy_lo, vy_lo);
159       y += 2;
160     }
161     if (n & (1 * sizeof(float))) {
162       _mm_store_ss(y, vy_lo);
163     }
164   }
165 }
166