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/avgpool.h>
11
12
xnn_f32_avgpool_ukernel_up9__sse(size_t n,size_t ks,size_t kc,const float ** input,const float * zero,float * output,size_t input_increment,size_t output_increment,const union xnn_f32_avgpool_params params[restrict static1])13 void xnn_f32_avgpool_ukernel_up9__sse(
14 size_t n,
15 size_t ks,
16 size_t kc,
17 const float** input,
18 const float* zero,
19 float* output,
20 size_t input_increment,
21 size_t output_increment,
22 const union xnn_f32_avgpool_params params[restrict static 1])
23 {
24 assert(n != 0);
25 assert(ks != 0);
26 assert(ks <= 9);
27 assert(kc != 0);
28
29 const __m128 vmultiplier = _mm_load_ps(params->sse2.multiplier);
30 const __m128 voutput_min = _mm_load_ps(params->sse2.output_min);
31 const __m128 voutput_max = _mm_load_ps(params->sse2.output_max);
32
33 do {
34 const float* i0 = input[0];
35 const float* i1 = input[1];
36 const float* i2 = input[2];
37 const float* i3 = input[3];
38 const float* i4 = input[4];
39 const float* i5 = input[5];
40 const float* i6 = input[6];
41 const float* i7 = input[7];
42 const float* i8 = input[8];
43 input = (const float**) ((uintptr_t) input + input_increment);
44 if (ks < 2) {
45 i1 = zero;
46 }
47 if (ks <= 2) {
48 i2 = zero;
49 }
50 if (ks < 4) {
51 i3 = zero;
52 }
53 if (ks <= 4) {
54 i4 = zero;
55 }
56 if (ks < 6) {
57 i5 = zero;
58 }
59 if (ks <= 6) {
60 i6 = zero;
61 }
62 if (ks < 8) {
63 i7 = zero;
64 }
65 if (ks <= 8) {
66 i8 = zero;
67 }
68
69 size_t k = kc;
70 while (k >= 4) {
71 const __m128 vi0 = _mm_loadu_ps(i0);
72 i0 += 4;
73 const __m128 vi1 = _mm_loadu_ps(i1);
74 i1 += 4;
75 const __m128 vi2 = _mm_loadu_ps(i2);
76 i2 += 4;
77 const __m128 vi3 = _mm_loadu_ps(i3);
78 i3 += 4;
79 const __m128 vi4 = _mm_loadu_ps(i4);
80 i4 += 4;
81 const __m128 vi5 = _mm_loadu_ps(i5);
82 i5 += 4;
83 const __m128 vi6 = _mm_loadu_ps(i6);
84 i6 += 4;
85 const __m128 vi7 = _mm_loadu_ps(i7);
86 i7 += 4;
87 const __m128 vi8 = _mm_loadu_ps(i8);
88 i8 += 4;
89
90 const __m128 vsum018 = _mm_add_ps(_mm_add_ps(vi0, vi1), vi8);
91 const __m128 vsum23 = _mm_add_ps(vi2, vi3);
92 const __m128 vsum45 = _mm_add_ps(vi4, vi5);
93 const __m128 vsum67 = _mm_add_ps(vi6, vi7);
94
95 const __m128 vsum2345 = _mm_add_ps(vsum23, vsum45);
96 const __m128 vsum01678 = _mm_add_ps(vsum018, vsum67);
97 const __m128 vsum = _mm_add_ps(vsum2345, vsum01678);
98
99 __m128 vout = _mm_mul_ps(vsum, vmultiplier);
100 vout = _mm_max_ps(vout, voutput_min);
101 vout = _mm_min_ps(vout, voutput_max);
102
103 _mm_storeu_ps(output, vout); output += 4;
104
105 k -= 4;
106 }
107 if (k != 0) {
108 const __m128 vi0 = _mm_loadu_ps(i0);
109 const __m128 vi1 = _mm_loadu_ps(i1);
110 const __m128 vi2 = _mm_loadu_ps(i2);
111 const __m128 vi3 = _mm_loadu_ps(i3);
112 const __m128 vi4 = _mm_loadu_ps(i4);
113 const __m128 vi5 = _mm_loadu_ps(i5);
114 const __m128 vi6 = _mm_loadu_ps(i6);
115 const __m128 vi7 = _mm_loadu_ps(i7);
116 const __m128 vi8 = _mm_loadu_ps(i8);
117
118 const __m128 vsum01 = _mm_add_ps(vi0, vi1);
119 const __m128 vsum23 = _mm_add_ps(vi2, vi3);
120 const __m128 vsum45 = _mm_add_ps(vi4, vi5);
121 const __m128 vsum67 = _mm_add_ps(vi6, vi7);
122 const __m128 vsum018 = _mm_add_ps(vsum01, vi8);
123 const __m128 vsum2345 = _mm_add_ps(vsum23, vsum45);
124 const __m128 vsum01678 = _mm_add_ps(vsum018, vsum67);
125 const __m128 vsum = _mm_add_ps(vsum2345, vsum01678);
126
127 __m128 vout = _mm_mul_ps(vsum, vmultiplier);
128 vout = _mm_max_ps(vout, voutput_min);
129 vout = _mm_min_ps(vout, voutput_max);
130
131 if (k & 2) {
132 _mm_storel_pi((__m64*) output, vout);
133 vout = _mm_movehl_ps(vout, vout);
134 output += 2;
135 }
136 if (k & 1) {
137 _mm_store_ss(output, vout);
138 output += 1;
139 }
140 }
141 output = (float*) ((uintptr_t) output + output_increment);
142 } while (--n != 0);
143 }
144