• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-prelu/avx.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/math.h>
15 #include <xnnpack/prelu.h>
16 
17 
18 static const int32_t mask_table[14] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0};
19 
xnn_f32_prelu_ukernel__avx_2x8(size_t rows,size_t channels,const float * restrict input,size_t input_stride,const float * restrict weights,float * restrict output,size_t output_stride)20 void xnn_f32_prelu_ukernel__avx_2x8(
21     size_t rows,
22     size_t channels,
23     const float*restrict input,
24     size_t input_stride,
25     const float*restrict weights,
26     float*restrict output,
27     size_t output_stride)
28 {
29   assert(rows != 0);
30   assert(channels != 0);
31   assert(channels % sizeof(float) == 0);
32 
33   const float* i0 = input;
34   float* o0 = output;
35   const float* i1 = (const float*) ((uintptr_t) i0 + input_stride);
36   float* o1 = (float*) ((uintptr_t) o0 + output_stride);
37   if XNN_UNPREDICTABLE(rows < 2) {
38     i1 = i0;
39     o1 = o0;
40   }
41 
42   const size_t input_increment = input_stride * 2 - channels;
43   const size_t output_increment = output_stride * 2 - channels;
44 
45   do {
46     const float* w = weights;
47     size_t c = channels;
48     for (; c >= 8 * sizeof(float); c -= 8 * sizeof(float)) {
49       const __m256 vw01234567 = _mm256_load_ps(w);
50       w += 8;
51 
52       const __m256 vi0x01234567 = _mm256_loadu_ps(i0);
53       i0 += 8;
54       const __m256 vi1x01234567 = _mm256_loadu_ps(i1);
55       i1 += 8;
56 
57       const __m256 vprod0x01234567 = _mm256_mul_ps(vi0x01234567, vw01234567);
58       const __m256 vprod1x01234567 = _mm256_mul_ps(vi1x01234567, vw01234567);
59 
60       const __m256 vacc0x01234567 = _mm256_blendv_ps(vi0x01234567, vprod0x01234567, vi0x01234567);
61       const __m256 vacc1x01234567 = _mm256_blendv_ps(vi1x01234567, vprod1x01234567, vi1x01234567);
62 
63       _mm256_storeu_ps(o0, vacc0x01234567);
64       o0 += 8;
65       _mm256_storeu_ps(o1, vacc1x01234567);
66       o1 += 8;
67     }
68     if XNN_UNLIKELY(c != 0) {
69       assert(c >= 1 * sizeof(float));
70       assert(c <= 7 * sizeof(float));
71       __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) &mask_table[7] - c));
72 
73       const __m256 vw = _mm256_maskload_ps(w, vmask);
74 
75       const __m256 vi0 = _mm256_maskload_ps(i0, vmask);
76       i0 = (const float*) ((uintptr_t) i0 + c);
77       const __m256 vi1 = _mm256_maskload_ps(i1, vmask);
78       i1 = (const float*) ((uintptr_t) i1 + c);
79 
80       const __m256 vprod0 = _mm256_mul_ps(vi0, vw);
81       const __m256 vprod1 = _mm256_mul_ps(vi1, vw);
82 
83       __m256 vacc0 = _mm256_blendv_ps(vi0, vprod0, vi0);
84       __m256 vacc1 = _mm256_blendv_ps(vi1, vprod1, vi1);
85 
86       // _mm256_maskstore_ps(o1, vmask, vacc1) could be used here, but triggers msan failures (probably an msan bug).
87       __m128 vacc0_lo = _mm256_castps256_ps128(vacc0);
88       __m128 vacc1_lo = _mm256_castps256_ps128(vacc1);
89       if (c & (4 * sizeof(float))) {
90         _mm_storeu_ps(o0, vacc0_lo);
91         _mm_storeu_ps(o1, vacc1_lo);
92 
93         vacc0_lo = _mm256_extractf128_ps(vacc0, 1);
94         vacc1_lo = _mm256_extractf128_ps(vacc1, 1);
95 
96         o0 += 4;
97         o1 += 4;
98       }
99       if (c & (2 * sizeof(float))) {
100         _mm_storel_pi((__m64*) o0, vacc0_lo);
101         _mm_storel_pi((__m64*) o1, vacc1_lo);
102 
103         vacc0_lo = _mm_movehl_ps(vacc0_lo, vacc0_lo);
104         vacc1_lo = _mm_movehl_ps(vacc1_lo, vacc1_lo);
105 
106         o0 += 2;
107         o1 += 2;
108       }
109       if (c & (1 * sizeof(float))) {
110         _mm_store_ss(o0, vacc0_lo);
111         _mm_store_ss(o1, vacc1_lo);
112 
113         o0 += 1;
114         o1 += 1;
115       }
116     }
117     i0 = (const float*) ((uintptr_t) i0 + input_increment);
118     o0 = (float*) ((uintptr_t) o0 + output_increment);
119     i1 = (const float*) ((uintptr_t) i1 + input_increment);
120     o1 = (float*) ((uintptr_t) o1 + output_increment);
121     if XNN_UNPREDICTABLE(rows < 4) {
122       i1 = i0;
123       o1 = o0;
124     }
125     rows = doz(rows, 2);
126   } while (rows != 0);
127 }
128