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 <xnnpack/scalar-utils.h>
9 #include <xnnpack/gavgpool.h>
10
11
xnn_q8_gavgpool_ukernel_up7__scalar(size_t m,size_t n,const uint8_t * input,size_t input_stride,const uint8_t * zero,uint8_t * output,const union xnn_q8_avgpool_params params[restrict static1])12 void xnn_q8_gavgpool_ukernel_up7__scalar(
13 size_t m,
14 size_t n,
15 const uint8_t* input,
16 size_t input_stride,
17 const uint8_t* zero,
18 uint8_t* output,
19 const union xnn_q8_avgpool_params params[restrict static 1])
20 {
21 assert(m != 0);
22 assert(m <= 7);
23 assert(n != 0);
24
25 const uint8_t* i0 = input;
26 const uint8_t* i1 = (const uint8_t*) ((uintptr_t) i0 + input_stride);
27 if (m < 2) {
28 i1 = zero;
29 }
30 const uint8_t* i2 = (const uint8_t*) ((uintptr_t) i1 + input_stride);
31 if (m <= 2) {
32 i2 = zero;
33 }
34 const uint8_t* i3 = (const uint8_t*) ((uintptr_t) i2 + input_stride);
35 if (m < 4) {
36 i3 = zero;
37 }
38 const uint8_t* i4 = (const uint8_t*) ((uintptr_t) i3 + input_stride);
39 if (m <= 4) {
40 i4 = zero;
41 }
42 const uint8_t* i5 = (const uint8_t*) ((uintptr_t) i4 + input_stride);
43 if (m < 6) {
44 i5 = zero;
45 }
46 const uint8_t* i6 = (const uint8_t*) ((uintptr_t) i5 + input_stride);
47 if (m <= 6) {
48 i6 = zero;
49 }
50
51 const int32_t vbias = params->scalar.bias;
52 const int32_t vmultiplier = params->scalar.multiplier;
53 const int64_t vrounding = params->scalar.rounding;
54 const uint32_t vshift = params->scalar.right_shift;
55 const int32_t voutput_min = params->scalar.output_min_less_zero_point;
56 const int32_t voutput_max = params->scalar.output_max_less_zero_point;
57 const int32_t voutput_zero_point = params->scalar.output_zero_point;
58 do {
59 const uint32_t vi0 = (uint32_t) *i0++;
60 const uint32_t vi1 = (uint32_t) *i1++;
61 const uint32_t vi2 = (uint32_t) *i2++;
62 const uint32_t vi3 = (uint32_t) *i3++;
63 const uint32_t vi4 = (uint32_t) *i4++;
64 const uint32_t vi5 = (uint32_t) *i5++;
65 const uint32_t vi6 = (uint32_t) *i6++;
66
67 const uint32_t vsum01 = vi0 + vi1;
68 const uint32_t vsum23 = vi2 + vi3;
69 const uint32_t vsum45 = vi4 + vi5;
70
71 const uint32_t vsum016 = vsum01 + vi6;
72 const uint32_t vsum2345 = vsum23 + vsum45;
73
74 const uint32_t vsum = vsum016 + vsum2345;
75 const int32_t vacc = vbias + (int32_t) vsum;
76
77 const int64_t vproduct = (int64_t) vacc * (int64_t) vmultiplier;
78 const int64_t vadjusted_product = vproduct - (int64_t) (vacc < 0);
79 int32_t vout = (int32_t) asr_s64(vadjusted_product + vrounding, vshift);
80 vout = vout < voutput_min ? voutput_min : vout;
81 vout = vout > voutput_max ? voutput_max : vout;
82 vout += voutput_zero_point;
83
84 *output++ = (uint8_t) vout;
85 } while (--n != 0);
86 }
87