1 // Copyright (c) Facebook, Inc. and its affiliates.
2 // All rights reserved.
3 //
4 // Copyright 2019 Google LLC
5 //
6 // This source code is licensed under the BSD-style license found in the
7 // LICENSE file in the root directory of this source tree.
8
9 #include <assert.h>
10
11 #include <xnnpack/avgpool.h>
12 #include <xnnpack/math.h>
13
14
xnn_qu8_avgpool_minmax_ukernel_9p8x__scalar_c1(size_t output_pixels,size_t kernel_elements,size_t channels,const uint8_t ** input,size_t input_offset,const uint8_t * zero,int32_t * buffer,uint8_t * output,size_t input_increment,size_t output_increment,const union xnn_qu8_avgpool_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])15 void xnn_qu8_avgpool_minmax_ukernel_9p8x__scalar_c1(
16 size_t output_pixels,
17 size_t kernel_elements,
18 size_t channels,
19 const uint8_t** input,
20 size_t input_offset,
21 const uint8_t* zero,
22 int32_t* buffer,
23 uint8_t* output,
24 size_t input_increment,
25 size_t output_increment,
26 const union xnn_qu8_avgpool_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28 assert(output_pixels != 0);
29 assert(kernel_elements > 9);
30 assert(channels != 0);
31
32 const int32_t vbias = params->scalar.bias;
33 const int32_t vmultiplier = params->scalar.multiplier;
34 const int64_t vrounding = params->scalar.rounding;
35 const uint32_t vshift = params->scalar.right_shift;
36 const int32_t voutput_min = params->scalar.output_min_less_zero_point;
37 const int32_t voutput_max = params->scalar.output_max_less_zero_point;
38 const int32_t voutput_zero_point = params->scalar.output_zero_point;
39 do {
40 // First pass.
41 {
42 const uint8_t* i0 = *input++;
43 assert(i0 != NULL);
44 if XNN_UNPREDICTABLE(i0 != zero) {
45 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
46 }
47 const uint8_t* i1 = *input++;
48 assert(i1 != NULL);
49 if XNN_UNPREDICTABLE(i1 != zero) {
50 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
51 }
52 const uint8_t* i2 = *input++;
53 assert(i2 != NULL);
54 if XNN_UNPREDICTABLE(i2 != zero) {
55 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
56 }
57 const uint8_t* i3 = *input++;
58 assert(i3 != NULL);
59 if XNN_UNPREDICTABLE(i3 != zero) {
60 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
61 }
62 const uint8_t* i4 = *input++;
63 assert(i4 != NULL);
64 if XNN_UNPREDICTABLE(i4 != zero) {
65 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
66 }
67 const uint8_t* i5 = *input++;
68 assert(i5 != NULL);
69 if XNN_UNPREDICTABLE(i5 != zero) {
70 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
71 }
72 const uint8_t* i6 = *input++;
73 assert(i6 != NULL);
74 if XNN_UNPREDICTABLE(i6 != zero) {
75 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
76 }
77 const uint8_t* i7 = *input++;
78 assert(i7 != NULL);
79 if XNN_UNPREDICTABLE(i7 != zero) {
80 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
81 }
82 const uint8_t* i8 = *input++;
83 assert(i8 != NULL);
84 if XNN_UNPREDICTABLE(i8 != zero) {
85 i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset);
86 }
87
88 int32_t* b = buffer;
89 size_t c = channels;
90 do {
91 const uint32_t vi0 = (uint32_t) *i0++;
92 const uint32_t vi1 = (uint32_t) *i1++;
93 const uint32_t vi2 = (uint32_t) *i2++;
94 const uint32_t vi3 = (uint32_t) *i3++;
95 const uint32_t vi4 = (uint32_t) *i4++;
96 const uint32_t vi5 = (uint32_t) *i5++;
97 const uint32_t vi6 = (uint32_t) *i6++;
98 const uint32_t vi7 = (uint32_t) *i7++;
99 const uint32_t vi8 = (uint32_t) *i8++;
100
101 const uint32_t vsum01 = vi0 + vi1;
102 const uint32_t vsum23 = vi2 + vi3;
103 const uint32_t vsum45 = vi4 + vi5;
104 const uint32_t vsum67 = vi6 + vi7;
105 const uint32_t vsum018 = vsum01 + vi8;
106 const uint32_t vsum2345 = vsum23 + vsum45;
107 const uint32_t vsum01678 = vsum018 + vsum67;
108 int32_t vacc = vbias + (int32_t) vsum2345;
109 vacc += (int32_t) vsum01678;
110 *b++ = vacc;
111 } while (--c != 0);
112 }
113
114 size_t k = kernel_elements;
115 // Intermediate passes.
116 for (k -= 9; k > 8; k -= 8) {
117 const uint8_t* i0 = *input++;
118 assert(i0 != NULL);
119 if XNN_UNPREDICTABLE(i0 != zero) {
120 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
121 }
122 const uint8_t* i1 = *input++;
123 assert(i1 != NULL);
124 if XNN_UNPREDICTABLE(i1 != zero) {
125 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
126 }
127 const uint8_t* i2 = *input++;
128 assert(i2 != NULL);
129 if XNN_UNPREDICTABLE(i2 != zero) {
130 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
131 }
132 const uint8_t* i3 = *input++;
133 assert(i3 != NULL);
134 if XNN_UNPREDICTABLE(i3 != zero) {
135 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
136 }
137 const uint8_t* i4 = *input++;
138 assert(i4 != NULL);
139 if XNN_UNPREDICTABLE(i4 != zero) {
140 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
141 }
142 const uint8_t* i5 = *input++;
143 assert(i5 != NULL);
144 if XNN_UNPREDICTABLE(i5 != zero) {
145 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
146 }
147 const uint8_t* i6 = *input++;
148 assert(i6 != NULL);
149 if XNN_UNPREDICTABLE(i6 != zero) {
150 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
151 }
152 const uint8_t* i7 = *input++;
153 assert(i7 != NULL);
154 if XNN_UNPREDICTABLE(i7 != zero) {
155 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
156 }
157
158 int32_t* b = buffer;
159 size_t c = channels;
160 do {
161 int32_t vacc = *b;
162
163 const uint32_t vi0 = (uint32_t) *i0++;
164 const uint32_t vi1 = (uint32_t) *i1++;
165 const uint32_t vi2 = (uint32_t) *i2++;
166 const uint32_t vi3 = (uint32_t) *i3++;
167 const uint32_t vi4 = (uint32_t) *i4++;
168 const uint32_t vi5 = (uint32_t) *i5++;
169 const uint32_t vi6 = (uint32_t) *i6++;
170 const uint32_t vi7 = (uint32_t) *i7++;
171
172 const uint32_t vsum01 = vi0 + vi1;
173 const uint32_t vsum23 = vi2 + vi3;
174 const uint32_t vsum45 = vi4 + vi5;
175 const uint32_t vsum67 = vi6 + vi7;
176 const uint32_t vsum0123 = vsum01 + vsum23;
177 const uint32_t vsum4567 = vsum45 + vsum67;
178 vacc += (int32_t) vsum0123;
179 vacc += (int32_t) vsum4567;
180
181 *b++ = vacc;
182 } while (--c != 0);
183 }
184
185 // Last pass.
186 {
187 const uint8_t* i0 = input[0];
188 assert(i0 != NULL);
189 const uint8_t* i1 = input[1];
190 const uint8_t* i2 = input[2];
191 const uint8_t* i3 = input[3];
192 const uint8_t* i4 = input[4];
193 const uint8_t* i5 = input[5];
194 const uint8_t* i6 = input[6];
195 const uint8_t* i7 = input[7];
196 input = (const uint8_t**) ((uintptr_t) input + input_increment);
197 if (k < 2) {
198 i1 = zero;
199 }
200 assert(i1 != NULL);
201 if (k <= 2) {
202 i2 = zero;
203 }
204 assert(i2 != NULL);
205 if (k < 4) {
206 i3 = zero;
207 }
208 assert(i3 != NULL);
209 if (k <= 4) {
210 i4 = zero;
211 }
212 assert(i4 != NULL);
213 if (k < 6) {
214 i5 = zero;
215 }
216 assert(i5 != NULL);
217 if (k <= 6) {
218 i6 = zero;
219 }
220 assert(i6 != NULL);
221 if (k < 8) {
222 i7 = zero;
223 }
224 assert(i7 != NULL);
225 if XNN_UNPREDICTABLE(i0 != zero) {
226 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
227 }
228 if XNN_UNPREDICTABLE(i1 != zero) {
229 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
230 }
231 if XNN_UNPREDICTABLE(i2 != zero) {
232 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
233 }
234 if XNN_UNPREDICTABLE(i3 != zero) {
235 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
236 }
237 if XNN_UNPREDICTABLE(i4 != zero) {
238 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
239 }
240 if XNN_UNPREDICTABLE(i5 != zero) {
241 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
242 }
243 if XNN_UNPREDICTABLE(i6 != zero) {
244 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
245 }
246 if XNN_UNPREDICTABLE(i7 != zero) {
247 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
248 }
249
250 size_t c = channels;
251 int32_t* b = buffer;
252 do {
253 int32_t vacc = *b++;
254
255 const uint32_t vi0 = (uint32_t) *i0++;
256 const uint32_t vi1 = (uint32_t) *i1++;
257 const uint32_t vi2 = (uint32_t) *i2++;
258 const uint32_t vi3 = (uint32_t) *i3++;
259 const uint32_t vi4 = (uint32_t) *i4++;
260 const uint32_t vi5 = (uint32_t) *i5++;
261 const uint32_t vi6 = (uint32_t) *i6++;
262 const uint32_t vi7 = (uint32_t) *i7++;
263
264 const uint32_t vsum01 = vi0 + vi1;
265 const uint32_t vsum23 = vi2 + vi3;
266 const uint32_t vsum45 = vi4 + vi5;
267 const uint32_t vsum67 = vi6 + vi7;
268 const uint32_t vsum0123 = vsum01 + vsum23;
269 const uint32_t vsum4567 = vsum45 + vsum67;
270 vacc += (int32_t) vsum0123;
271 vacc += (int32_t) vsum4567;
272
273 const int64_t vproduct = (int64_t) vacc * (int64_t) vmultiplier;
274 const int64_t vadjusted_product = vproduct - (int64_t) (vacc < 0);
275 int32_t vout = (int32_t) math_asr_s64(vadjusted_product + vrounding, vshift);
276 vout = vout < voutput_min ? voutput_min : vout;
277 vout = vout > voutput_max ? voutput_max : vout;
278 vout += voutput_zero_point;
279
280 *output++ = (uint8_t) vout;
281 } while (--c != 0);
282 }
283 output = (uint8_t*) ((uintptr_t) output + output_increment);
284 } while (--output_pixels != 0);
285 }
286