• 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/gavgpool.h>
11 #include <xnnpack/math.h>
12 
13 
xnn_f32_gavgpool_cw_ukernel__sse_x4(size_t elements,size_t channels,const float * input,float * output,const union xnn_f32_gavgpool_params params[restrict XNN_MIN_ELEMENTS (1)])14 void xnn_f32_gavgpool_cw_ukernel__sse_x4(
15     size_t elements,
16     size_t channels,
17     const float* input,
18     float* output,
19     const union xnn_f32_gavgpool_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
20 {
21   assert(elements != 0);
22   assert(elements % sizeof(float) == 0);
23   assert(channels != 0);
24 
25   const float* i0 = input;
26   const float* i1 = (const float*) ((uintptr_t) i0 + elements);
27   const float* i2 = (const float*) ((uintptr_t) i1 + elements);
28   const float* i3 = (const float*) ((uintptr_t) i2 + elements);
29 
30   const __m128 vmask = _mm_load_ps((const float*) params->sse.mask);
31   const __m128 vmultiplier = _mm_load_ps(params->sse.multiplier);
32   const __m128 voutput_min = _mm_load_ps(params->sse.output_min);
33   const __m128 voutput_max = _mm_load_ps(params->sse.output_max);
34 
35   while (channels >= 4) {
36     __m128 vsum0 = _mm_setzero_ps();
37     __m128 vsum1 = _mm_setzero_ps();
38     __m128 vsum2 = _mm_setzero_ps();
39     __m128 vsum3 = _mm_setzero_ps();
40     size_t n = elements;
41     while (n >= 4 * sizeof(float)) {
42       const __m128 vi0 = _mm_loadu_ps(i0);
43       i0 += 4;
44       const __m128 vi1 = _mm_loadu_ps(i1);
45       i1 += 4;
46       const __m128 vi2 = _mm_loadu_ps(i2);
47       i2 += 4;
48       const __m128 vi3 = _mm_loadu_ps(i3);
49       i3 += 4;
50 
51       vsum0 = _mm_add_ps(vsum0, vi0);
52       vsum1 = _mm_add_ps(vsum1, vi1);
53       vsum2 = _mm_add_ps(vsum2, vi2);
54       vsum3 = _mm_add_ps(vsum3, vi3);
55       n -= 4 * sizeof(float);
56     }
57 
58     if XNN_UNLIKELY(n != 0) {
59       const __m128 vi0 = _mm_and_ps(_mm_loadu_ps(i0), vmask);
60       i0 = (const float*) ((uintptr_t) i0 + n);
61       const __m128 vi1 = _mm_and_ps(_mm_loadu_ps(i1), vmask);
62       i1 = (const float*) ((uintptr_t) i1 + n);
63       const __m128 vi2 = _mm_and_ps(_mm_loadu_ps(i2), vmask);
64       i2 = (const float*) ((uintptr_t) i2 + n);
65       const __m128 vi3 = _mm_and_ps(_mm_loadu_ps(i3), vmask);
66       i3 = (const float*) ((uintptr_t) i3 + n);
67 
68       vsum0 = _mm_add_ps(vsum0, vi0);
69       vsum1 = _mm_add_ps(vsum1, vi1);
70       vsum2 = _mm_add_ps(vsum2, vi2);
71       vsum3 = _mm_add_ps(vsum3, vi3);
72     }
73 
74     // Having exactly 4 rows makes this work out nicely as we end up with
75     // the 4 totals in 4 different lanes of the same vector.
76     const __m128 vsum01 = _mm_add_ps(_mm_unpacklo_ps(vsum0, vsum1), _mm_unpackhi_ps(vsum0, vsum1));
77     const __m128 vsum23 = _mm_add_ps(_mm_unpacklo_ps(vsum2, vsum3), _mm_unpackhi_ps(vsum2, vsum3));
78     const __m128 vsum = _mm_add_ps(_mm_movelh_ps(vsum01, vsum23), _mm_movehl_ps(vsum23, vsum01));
79     __m128 vout = _mm_mul_ps(vsum, vmultiplier);
80 
81     vout = _mm_max_ps(vout, voutput_min);
82     vout = _mm_min_ps(vout, voutput_max);
83 
84     _mm_storeu_ps(output, vout);
85     output += 4;
86     i0 = i3;
87     i1 = (const float*) ((uintptr_t) i0 + elements);
88     i2 = (const float*) ((uintptr_t) i1 + elements);
89     i3 = (const float*) ((uintptr_t) i2 + elements);
90     channels -= 4;
91   }
92 
93   while (channels != 0) {
94     __m128 vsum = _mm_setzero_ps();
95     size_t n = elements;
96     while (n >= 4 * sizeof(float)) {
97       const __m128 vi0 = _mm_loadu_ps(i0);
98       i0 += 4;
99       vsum = _mm_add_ps(vsum, vi0);
100       n -= 4 * sizeof(float);
101     }
102 
103     if XNN_UNLIKELY(n != 0) {
104       __m128 vi0 = _mm_and_ps(_mm_loadu_ps(i0), vmask);
105       i0 = (const float*) ((uintptr_t) i0 + n);
106       vsum = _mm_add_ps(vsum, vi0);
107     }
108 
109     vsum = _mm_add_ps(vsum, _mm_movehl_ps(vsum, vsum));
110     vsum = _mm_add_ss(vsum, _mm_shuffle_ps(vsum, vsum, _MM_SHUFFLE(3, 2, 1, 1)));
111 
112     __m128 vout = _mm_mul_ss(vsum, vmultiplier);
113 
114     vout = _mm_max_ss(vout, voutput_min);
115     vout = _mm_min_ss(vout, voutput_max);
116 
117     _mm_store_ss(output, vout);
118     output += 1;
119     channels -= 1;
120   }
121 }
122