• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 
8 #include <xmmintrin.h>
9 
10 #include <xnnpack/pavgpool.h>
11 
12 
xnn_f32_pavgpool_minmax_ukernel_9x__sse_c4(size_t output_pixels,size_t kernel_elements,size_t channels,const float ** input,size_t input_offset,const float * zero,const float * multiplier,float * output,size_t input_increment,size_t output_increment,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])13 void xnn_f32_pavgpool_minmax_ukernel_9x__sse_c4(
14     size_t output_pixels,
15     size_t kernel_elements,
16     size_t channels,
17     const float** input,
18     size_t input_offset,
19     const float* zero,
20     const float* multiplier,
21     float* output,
22     size_t input_increment,
23     size_t output_increment,
24     const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
25 {
26   assert(output_pixels != 0);
27   assert(kernel_elements != 0);
28   assert(kernel_elements <= 9);
29   assert(channels != 0);
30 
31   const __m128 voutput_min = _mm_load_ps(params->sse.min);
32   const __m128 voutput_max = _mm_load_ps(params->sse.max);
33 
34   do {
35     const float* i0 = input[0];
36     assert(i0 != NULL);
37     const float* i1 = input[1];
38     const float* i2 = input[2];
39     const float* i3 = input[3];
40     const float* i4 = input[4];
41     const float* i5 = input[5];
42     const float* i6 = input[6];
43     const float* i7 = input[7];
44     const float* i8 = input[8];
45     input = (const float**) ((uintptr_t) input + input_increment);
46     if (kernel_elements < 2) {
47       i1 = zero;
48     }
49     assert(i1 != NULL);
50     if (kernel_elements <= 2) {
51       i2 = zero;
52     }
53     assert(i2 != NULL);
54     if (kernel_elements < 4) {
55       i3 = zero;
56     }
57     assert(i3 != NULL);
58     if (kernel_elements <= 4) {
59       i4 = zero;
60     }
61     assert(i4 != NULL);
62     if (kernel_elements < 6) {
63       i5 = zero;
64     }
65     assert(i5 != NULL);
66     if (kernel_elements <= 6) {
67       i6 = zero;
68     }
69     assert(i6 != NULL);
70     if (kernel_elements < 8) {
71       i7 = zero;
72     }
73     assert(i7 != NULL);
74     if (kernel_elements <= 8) {
75       i8 = zero;
76     }
77     assert(i8 != NULL);
78     if XNN_UNPREDICTABLE(i0 != zero) {
79       i0 = (const float*) ((uintptr_t) i0 + input_offset);
80     }
81     if XNN_UNPREDICTABLE(i1 != zero) {
82       i1 = (const float*) ((uintptr_t) i1 + input_offset);
83     }
84     if XNN_UNPREDICTABLE(i2 != zero) {
85       i2 = (const float*) ((uintptr_t) i2 + input_offset);
86     }
87     if XNN_UNPREDICTABLE(i3 != zero) {
88       i3 = (const float*) ((uintptr_t) i3 + input_offset);
89     }
90     if XNN_UNPREDICTABLE(i4 != zero) {
91       i4 = (const float*) ((uintptr_t) i4 + input_offset);
92     }
93     if XNN_UNPREDICTABLE(i5 != zero) {
94       i5 = (const float*) ((uintptr_t) i5 + input_offset);
95     }
96     if XNN_UNPREDICTABLE(i6 != zero) {
97       i6 = (const float*) ((uintptr_t) i6 + input_offset);
98     }
99     if XNN_UNPREDICTABLE(i7 != zero) {
100       i7 = (const float*) ((uintptr_t) i7 + input_offset);
101     }
102     if XNN_UNPREDICTABLE(i8 != zero) {
103       i8 = (const float*) ((uintptr_t) i8 + input_offset);
104     }
105 
106     const __m128 vmultiplier = _mm_load1_ps(multiplier);
107     multiplier += 1;
108 
109     size_t c = channels;
110     while (c >= 4) {
111       const __m128 vi0 = _mm_loadu_ps(i0);
112       i0 += 4;
113       const __m128 vi1 = _mm_loadu_ps(i1);
114       i1 += 4;
115       const __m128 vi2 = _mm_loadu_ps(i2);
116       i2 += 4;
117       const __m128 vi3 = _mm_loadu_ps(i3);
118       i3 += 4;
119       const __m128 vi4 = _mm_loadu_ps(i4);
120       i4 += 4;
121       const __m128 vi5 = _mm_loadu_ps(i5);
122       i5 += 4;
123       const __m128 vi6 = _mm_loadu_ps(i6);
124       i6 += 4;
125       const __m128 vi7 = _mm_loadu_ps(i7);
126       i7 += 4;
127       const __m128 vi8 = _mm_loadu_ps(i8);
128       i8 += 4;
129 
130       const __m128 vsum018 = _mm_add_ps(_mm_add_ps(vi0, vi1), vi8);
131       const __m128 vsum23 = _mm_add_ps(vi2, vi3);
132       const __m128 vsum45 = _mm_add_ps(vi4, vi5);
133       const __m128 vsum67 = _mm_add_ps(vi6, vi7);
134 
135       const __m128 vsum2345 = _mm_add_ps(vsum23, vsum45);
136       const __m128 vsum01678 = _mm_add_ps(vsum018, vsum67);
137       const __m128 vsum = _mm_add_ps(vsum2345, vsum01678);
138 
139       __m128 vout = _mm_mul_ps(vsum, vmultiplier);
140       vout = _mm_max_ps(vout, voutput_min);
141       vout = _mm_min_ps(vout, voutput_max);
142 
143       _mm_storeu_ps(output, vout); output += 4;
144 
145       c -= 4;
146     }
147     if (c != 0) {
148       const __m128 vi0 = _mm_loadu_ps(i0);
149       const __m128 vi1 = _mm_loadu_ps(i1);
150       const __m128 vi2 = _mm_loadu_ps(i2);
151       const __m128 vi3 = _mm_loadu_ps(i3);
152       const __m128 vi4 = _mm_loadu_ps(i4);
153       const __m128 vi5 = _mm_loadu_ps(i5);
154       const __m128 vi6 = _mm_loadu_ps(i6);
155       const __m128 vi7 = _mm_loadu_ps(i7);
156       const __m128 vi8 = _mm_loadu_ps(i8);
157 
158       const __m128 vsum01 = _mm_add_ps(vi0, vi1);
159       const __m128 vsum23 = _mm_add_ps(vi2, vi3);
160       const __m128 vsum45 = _mm_add_ps(vi4, vi5);
161       const __m128 vsum67 = _mm_add_ps(vi6, vi7);
162       const __m128 vsum018 = _mm_add_ps(vsum01, vi8);
163       const __m128 vsum2345 = _mm_add_ps(vsum23, vsum45);
164       const __m128 vsum01678 = _mm_add_ps(vsum018, vsum67);
165       const __m128 vsum = _mm_add_ps(vsum2345, vsum01678);
166 
167       __m128 vout = _mm_mul_ps(vsum, vmultiplier);
168       vout = _mm_max_ps(vout, voutput_min);
169       vout = _mm_min_ps(vout, voutput_max);
170 
171       if (c & 2) {
172         _mm_storel_pi((__m64*) output, vout);
173         vout = _mm_movehl_ps(vout, vout);
174         output += 2;
175       }
176       if (c & 1) {
177         _mm_store_ss(output, vout);
178         output += 1;
179       }
180     }
181     output = (float*) ((uintptr_t) output + output_increment);
182   } while (--output_pixels != 0);
183 }
184