• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_5x5p2__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_5x5p2__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_single = input_width_stride - n * input_tuple_stride;
30   const size_t output_width_increment_single = output_width_stride - (n - 1) * 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     float vi0x3;
88     float vi1x3;
89     float vi2x3;
90     float vi3x3;
91     float vi4x3;
92     if XNN_LIKELY(n > 1) {
93       vi0x3 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride);
94       vi1x3 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride);
95       vi2x3 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride);
96       vi3x3 = *i3; i3 = (const float*) ((uintptr_t) i3 + input_tuple_stride);
97       vi4x3 = *i4; i4 = (const float*) ((uintptr_t) i4 + input_tuple_stride);
98     }
99 
100     size_t k = n;
101     for (; k > 2; k -= 1) {
102       const float vi0x4 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride);
103       const float vi1x4 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride);
104       const float vi2x4 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride);
105       const float vi3x4 = *i3; i3 = (const float*) ((uintptr_t) i3 + input_tuple_stride);
106       const float vi4x4 = *i4; i4 = (const float*) ((uintptr_t) i4 + input_tuple_stride);
107 
108       const float vrow0_accum = vw1  * vi0x0 + vw2  * vi0x1 + vw3  * vi0x2 + vw4  * vi0x3 + vw5  * vi0x4;
109       vi0x0 = vi0x1;
110       vi0x1 = vi0x2;
111       vi0x2 = vi0x3;
112       vi0x3 = vi0x4;
113       const float vrow1_accum = vw6  * vi1x0 + vw7  * vi1x1 + vw8  * vi1x2 + vw9  * vi1x3 + vw10 * vi1x4;
114       vi1x0 = vi1x1;
115       vi1x1 = vi1x2;
116       vi1x2 = vi1x3;
117       vi1x3 = vi1x4;
118       const float vrow2_accum = vw11 * vi2x0 + vw12 * vi2x1 + vw13 * vi2x2 + vw14 * vi2x3 + vw15 * vi2x4;
119       vi2x0 = vi2x1;
120       vi2x1 = vi2x2;
121       vi2x2 = vi2x3;
122       vi2x3 = vi2x4;
123       const float vrow3_accum = vw16 * vi3x0 + vw17 * vi3x1 + vw18 * vi3x2 + vw19 * vi3x3 + vw20 * vi3x4;
124       vi3x0 = vi3x1;
125       vi3x1 = vi3x2;
126       vi3x2 = vi3x3;
127       vi3x3 = vi3x4;
128       const float vrow4_accum = vw21 * vi4x0 + vw22 * vi4x1 + vw23 * vi4x2 + vw24 * vi4x3 + vw25 * vi4x4;
129       vi4x0 = vi4x1;
130       vi4x1 = vi4x2;
131       vi4x2 = vi4x3;
132       vi4x3 = vi4x4;
133 
134       float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum) + (vrow3_accum + vrow4_accum);
135 
136       voutput = math_max_f32(voutput, params_min);
137       voutput = math_min_f32(voutput, params_max);
138 
139       *output0 = voutput; output0 = (float*) ((uintptr_t) output0 + output_tuple_stride);
140     }
141     if XNN_LIKELY(k > 1) {
142       const float vrow0_accum = vw1  * vi0x0 + vw2  * vi0x1 + vw3  * vi0x2 + vw4  * vi0x3;
143       vi0x0 = vi0x1;
144       vi0x1 = vi0x2;
145       vi0x2 = vi0x3;
146       const float vrow1_accum = vw6  * vi1x0 + vw7  * vi1x1 + vw8  * vi1x2 + vw9  * vi1x3;
147       vi1x0 = vi1x1;
148       vi1x1 = vi1x2;
149       vi1x2 = vi1x3;
150       const float vrow2_accum = vw11 * vi2x0 + vw12 * vi2x1 + vw13 * vi2x2 + vw14 * vi2x3;
151       vi2x0 = vi2x1;
152       vi2x1 = vi2x2;
153       vi2x2 = vi2x3;
154       const float vrow3_accum = vw16 * vi3x0 + vw17 * vi3x1 + vw18 * vi3x2 + vw19 * vi3x3;
155       vi3x0 = vi3x1;
156       vi3x1 = vi3x2;
157       vi3x2 = vi3x3;
158       const float vrow4_accum = vw21 * vi4x0 + vw22 * vi4x1 + vw23 * vi4x2 + vw24 * vi4x3;
159       vi4x0 = vi4x1;
160       vi4x1 = vi4x2;
161       vi4x2 = vi4x3;
162 
163       float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum) + (vrow3_accum + vrow4_accum);
164 
165       voutput = math_max_f32(voutput, params_min);
166       voutput = math_min_f32(voutput, params_max);
167 
168       *output0 = voutput; output0 = (float*) ((uintptr_t) output0 + output_tuple_stride);
169       k -= 1;
170     }
171     assert(k == 1);
172     {
173       const float vrow0_accum = vw1  * vi0x0 + vw2  * vi0x1 + vw3  * vi0x2;
174       const float vrow1_accum = vw6  * vi1x0 + vw7  * vi1x1 + vw8  * vi1x2;
175       const float vrow2_accum = vw11 * vi2x0 + vw12 * vi2x1 + vw13 * vi2x2;
176       const float vrow3_accum = vw16 * vi3x0 + vw17 * vi3x1 + vw18 * vi3x2;
177       const float vrow4_accum = vw21 * vi4x0 + vw22 * vi4x1 + vw23 * vi4x2;
178 
179       float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum) + (vrow3_accum + vrow4_accum);
180 
181       voutput = math_max_f32(voutput, params_min);
182       voutput = math_min_f32(voutput, params_max);
183 
184       *output0 = voutput;;
185     }
186 
187     i0 = (const float*) ((uintptr_t) i0 + input_width_increment_single);
188     i1 = (const float*) ((uintptr_t) i1 + input_width_increment_single);
189     i2 = (const float*) ((uintptr_t) i2 + input_width_increment_single);
190     i3 = (const float*) ((uintptr_t) i3 + input_width_increment_single);
191     i4 = (const float*) ((uintptr_t) i4 + input_width_increment_single);
192     output0 = (float*) ((uintptr_t) output0 + output_width_increment_single);
193     m -= 1;
194   } while (m > 0);
195 }
196