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_9x__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,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_9x__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 uint8_t* output,
23 size_t input_increment,
24 size_t output_increment,
25 const union xnn_qu8_avgpool_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
26 {
27 assert(output_pixels != 0);
28 assert(kernel_elements != 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 const uint8_t* i0 = input[0];
41 assert(i0 != NULL);
42 const uint8_t* i1 = input[1];
43 const uint8_t* i2 = input[2];
44 const uint8_t* i3 = input[3];
45 const uint8_t* i4 = input[4];
46 const uint8_t* i5 = input[5];
47 const uint8_t* i6 = input[6];
48 const uint8_t* i7 = input[7];
49 const uint8_t* i8 = input[8];
50 input = (const uint8_t**) ((uintptr_t) input + input_increment);
51 if (kernel_elements < 2) {
52 i1 = zero;
53 }
54 assert(i1 != NULL);
55 if (kernel_elements <= 2) {
56 i2 = zero;
57 }
58 assert(i2 != NULL);
59 if (kernel_elements < 4) {
60 i3 = zero;
61 }
62 assert(i3 != NULL);
63 if (kernel_elements <= 4) {
64 i4 = zero;
65 }
66 assert(i4 != NULL);
67 if (kernel_elements < 6) {
68 i5 = zero;
69 }
70 assert(i5 != NULL);
71 if (kernel_elements <= 6) {
72 i6 = zero;
73 }
74 assert(i6 != NULL);
75 if (kernel_elements < 8) {
76 i7 = zero;
77 }
78 assert(i7 != NULL);
79 if (kernel_elements <= 8) {
80 i8 = zero;
81 }
82 assert(i8 != NULL);
83 if XNN_UNPREDICTABLE(i0 != zero) {
84 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
85 }
86 if XNN_UNPREDICTABLE(i1 != zero) {
87 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
88 }
89 if XNN_UNPREDICTABLE(i2 != zero) {
90 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
91 }
92 if XNN_UNPREDICTABLE(i3 != zero) {
93 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
94 }
95 if XNN_UNPREDICTABLE(i4 != zero) {
96 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
97 }
98 if XNN_UNPREDICTABLE(i5 != zero) {
99 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
100 }
101 if XNN_UNPREDICTABLE(i6 != zero) {
102 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
103 }
104 if XNN_UNPREDICTABLE(i7 != zero) {
105 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
106 }
107 if XNN_UNPREDICTABLE(i8 != zero) {
108 i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset);
109 }
110
111 size_t c = channels;
112 do {
113 const uint32_t vi0 = (uint32_t) *i0++;
114 const uint32_t vi1 = (uint32_t) *i1++;
115 const uint32_t vi2 = (uint32_t) *i2++;
116 const uint32_t vi3 = (uint32_t) *i3++;
117 const uint32_t vi4 = (uint32_t) *i4++;
118 const uint32_t vi5 = (uint32_t) *i5++;
119 const uint32_t vi6 = (uint32_t) *i6++;
120 const uint32_t vi7 = (uint32_t) *i7++;
121 const uint32_t vi8 = (uint32_t) *i8++;
122
123 const uint32_t vsum01 = vi0 + vi1;
124 const uint32_t vsum23 = vi2 + vi3;
125 const uint32_t vsum45 = vi4 + vi5;
126 const uint32_t vsum67 = vi6 + vi7;
127 const uint32_t vsum018 = vsum01 + vi8;
128 const uint32_t vsum2345 = vsum23 + vsum45;
129 const uint32_t vsum01678 = vsum018 + vsum67;
130 const uint32_t vsum = vsum2345 + vsum01678;
131
132 const int32_t vacc = vbias + (int32_t) vsum;
133
134 const int64_t vproduct = (int64_t) vacc * (int64_t) vmultiplier;
135 const int64_t vadjusted_product = vproduct - (int64_t) (vacc < 0);
136 int32_t vout = (int32_t) math_asr_s64(vadjusted_product + vrounding, vshift);
137 vout = vout < voutput_min ? voutput_min : vout;
138 vout = vout > voutput_max ? voutput_max : vout;
139 vout += voutput_zero_point;
140
141 *output++ = (uint8_t) vout;
142 } while (--c != 0);
143 output = (uint8_t*) ((uintptr_t) output + output_increment);
144 } while (--output_pixels != 0);
145 }
146