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/dwconv.h>
9 #include <xnnpack/math.h>
10
11
xnn_f32_dwconv_spchw_ukernel_5x5s2p2__scalar(size_t m,size_t n,const float * input,const float * weights,float * output,size_t input_tuple_stride,size_t output_tuple_stride,size_t input_width_stride,size_t output_width_stride,const union xnn_f32_spchw_params params[restrict static1])12 void xnn_f32_dwconv_spchw_ukernel_5x5s2p2__scalar(
13 size_t m,
14 size_t n,
15 const float* input,
16 const float* weights,
17 float* output,
18 size_t input_tuple_stride,
19 size_t output_tuple_stride,
20 size_t input_width_stride,
21 size_t output_width_stride,
22 const union xnn_f32_spchw_params params[restrict static 1])
23 {
24 assert(n != 0);
25
26 const float params_max = params->scalar.max;
27 const float params_min = params->scalar.min;
28
29 const size_t input_width_increment = input_width_stride * 2 - (1 + 2 * ((n - 1) / 2)) * input_tuple_stride;
30 const size_t output_width_increment = output_width_stride - (n - 1) / 2 * output_tuple_stride;
31
32 // No vertical padding.
33 const float* i0 = input;
34 const float* i1 = (const float*) ((uintptr_t) i0 + input_width_stride);
35 const float* i2 = (const float*) ((uintptr_t) i1 + input_width_stride);
36 const float* i3 = (const float*) ((uintptr_t) i2 + input_width_stride);
37 const float* i4 = (const float*) ((uintptr_t) i3 + input_width_stride);
38
39 float* output0 = output;
40
41 // this almost certainly will use too many scalar registers
42 // hope the compiler is good at spilling...
43 const float vw0 = weights[0];
44 const float vw1 = weights[1];
45 const float vw2 = weights[2];
46 const float vw3 = weights[3];
47 const float vw4 = weights[4];
48 const float vw5 = weights[5];
49 const float vw6 = weights[6];
50 const float vw7 = weights[7];
51 const float vw8 = weights[8];
52 const float vw9 = weights[9];
53 const float vw10 = weights[10];
54 const float vw11 = weights[11];
55 const float vw12 = weights[12];
56 const float vw13 = weights[13];
57 const float vw14 = weights[14];
58 const float vw15 = weights[15];
59 const float vw16 = weights[16];
60 const float vw17 = weights[17];
61 const float vw18 = weights[18];
62 const float vw19 = weights[19];
63 const float vw20 = weights[20];
64 const float vw21 = weights[21];
65 const float vw22 = weights[22];
66 const float vw23 = weights[23];
67 const float vw24 = weights[24];
68 const float vw25 = weights[25];
69
70 do {
71 float vi0x0 = 0.0f;
72 float vi1x0 = 0.0f;
73 float vi2x0 = 0.0f;
74 float vi3x0 = 0.0f;
75 float vi4x0 = 0.0f;
76 float vi0x1 = 0.0f;
77 float vi1x1 = 0.0f;
78 float vi2x1 = 0.0f;
79 float vi3x1 = 0.0f;
80 float vi4x1 = 0.0f;
81 float vi0x2 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride);
82 float vi1x2 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride);
83 float vi2x2 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride);
84 float vi3x2 = *i3; i3 = (const float*) ((uintptr_t) i3 + input_tuple_stride);
85 float vi4x2 = *i4; i4 = (const float*) ((uintptr_t) i4 + input_tuple_stride);
86
87
88 size_t k = n;
89 for (; k > 2; k -= 2) {
90 const float vi0x3 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride);
91 const float vi1x3 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride);
92 const float vi2x3 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride);
93 const float vi3x3 = *i3; i3 = (const float*) ((uintptr_t) i3 + input_tuple_stride);
94 const float vi4x3 = *i4; i4 = (const float*) ((uintptr_t) i4 + input_tuple_stride);
95
96 const float vi0x4 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride);
97 const float vi1x4 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride);
98 const float vi2x4 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride);
99 const float vi3x4 = *i3; i3 = (const float*) ((uintptr_t) i3 + input_tuple_stride);
100 const float vi4x4 = *i4; i4 = (const float*) ((uintptr_t) i4 + input_tuple_stride);
101
102 const float vrow0_accum = vw1 * vi0x0 + vw2 * vi0x1 + vw3 * vi0x2 + vw4 * vi0x3 + vw5 * vi0x4;
103 vi0x0 = vi0x2;
104 vi0x1 = vi0x3;
105 vi0x2 = vi0x4;
106 const float vrow1_accum = vw6 * vi1x0 + vw7 * vi1x1 + vw8 * vi1x2 + vw9 * vi1x3 + vw10 * vi1x4;
107 vi1x0 = vi1x2;
108 vi1x1 = vi1x3;
109 vi1x2 = vi1x4;
110 const float vrow2_accum = vw11 * vi2x0 + vw12 * vi2x1 + vw13 * vi2x2 + vw14 * vi2x3 + vw15 * vi2x4;
111 vi2x0 = vi2x2;
112 vi2x1 = vi2x3;
113 vi2x2 = vi2x4;
114 const float vrow3_accum = vw16 * vi3x0 + vw17 * vi3x1 + vw18 * vi3x2 + vw19 * vi3x3 + vw20 * vi3x4;
115 vi3x0 = vi3x2;
116 vi3x1 = vi3x3;
117 vi3x2 = vi3x4;
118 const float vrow4_accum = vw21 * vi4x0 + vw22 * vi4x1 + vw23 * vi4x2 + vw24 * vi4x3 + vw25 * vi4x4;
119 vi4x0 = vi4x2;
120 vi4x1 = vi4x3;
121 vi4x2 = vi4x4;
122
123 float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum) + (vrow3_accum + vrow4_accum);
124
125 voutput = math_max_f32(voutput, params_min);
126 voutput = math_min_f32(voutput, params_max);
127
128 *output0 = voutput; output0 = (float*) ((uintptr_t) output0 + output_tuple_stride);
129 }
130 if XNN_UNPREDICTABLE(k == 2) {
131 const float vi0x3 = *i0;
132 const float vi1x3 = *i1;
133 const float vi2x3 = *i2;
134 const float vi3x3 = *i3;
135 const float vi4x3 = *i4;
136
137 const float vrow0_accum = vw1 * vi0x0 + vw2 * vi0x1 + vw3 * vi0x2 + vw4 * vi0x3;
138 const float vrow1_accum = vw6 * vi1x0 + vw7 * vi1x1 + vw8 * vi1x2 + vw9 * vi1x3;
139 const float vrow2_accum = vw11 * vi2x0 + vw12 * vi2x1 + vw13 * vi2x2 + vw14 * vi2x3;
140 const float vrow3_accum = vw16 * vi3x0 + vw17 * vi3x1 + vw18 * vi3x2 + vw19 * vi3x3;
141 const float vrow4_accum = vw21 * vi4x0 + vw22 * vi4x1 + vw23 * vi4x2 + vw24 * vi4x3;
142
143 float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum) + (vrow3_accum + vrow4_accum);
144
145 voutput = math_max_f32(voutput, params_min);
146 voutput = math_min_f32(voutput, params_max);
147
148 *output0 = voutput;
149 }
150 else {
151 const float vrow0_accum = vw1 * vi0x0 + vw2 * vi0x1 + vw3 * vi0x2;
152 const float vrow1_accum = vw6 * vi1x0 + vw7 * vi1x1 + vw8 * vi1x2;
153 const float vrow2_accum = vw11 * vi2x0 + vw12 * vi2x1 + vw13 * vi2x2;
154 const float vrow3_accum = vw16 * vi3x0 + vw17 * vi3x1 + vw18 * vi3x2;
155 const float vrow4_accum = vw21 * vi4x0 + vw22 * vi4x1 + vw23 * vi4x2;
156
157 float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum) + (vrow3_accum + vrow4_accum);
158
159 voutput = math_max_f32(voutput, params_min);
160 voutput = math_min_f32(voutput, params_max);
161
162 *output0 = voutput;
163 }
164
165 i0 = (const float*) ((uintptr_t) i0 + input_width_increment);
166 i1 = (const float*) ((uintptr_t) i1 + input_width_increment);
167 i2 = (const float*) ((uintptr_t) i2 + input_width_increment);
168 i3 = (const float*) ((uintptr_t) i3 + input_width_increment);
169 i4 = (const float*) ((uintptr_t) i4 + input_width_increment);
170 output0 = (float*) ((uintptr_t) output0 + output_width_increment);
171 m -= 1;
172 } while (m > 0);
173 }
174