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 <stddef.h>
7 #include <stdint.h>
8 #include <string.h>
9 
10 #include <xnnpack/im2col.h>
11 
12 
xnn_im2col_conv2d(size_t output_height,size_t output_width,size_t kernel_height,size_t kernel_width,size_t subsampling_height,size_t subsampling_width,size_t dilation_height,size_t dilation_width,size_t input_width,size_t input_padding_top,size_t input_padding_left,size_t group_input_channels_in_bytes,size_t input_pixel_stride_in_bytes,const void * input,void * output)13 void xnn_im2col_conv2d(
14   size_t output_height,
15   size_t output_width,
16   size_t kernel_height,
17   size_t kernel_width,
18   size_t subsampling_height,
19   size_t subsampling_width,
20   size_t dilation_height,
21   size_t dilation_width,
22   size_t input_width,
23   size_t input_padding_top,
24   size_t input_padding_left,
25   size_t group_input_channels_in_bytes,
26   size_t input_pixel_stride_in_bytes,
27   const void* input,
28   void* output)
29 {
30   for (size_t output_y = 0; output_y < output_height; output_y++) {
31     for (size_t output_x = 0; output_x < output_width; output_x++) {
32       for (size_t kernel_y = 0; kernel_y < kernel_height; kernel_y++) {
33         const size_t input_y = output_y * subsampling_height + kernel_y * dilation_height - input_padding_top;
34         if (input_y < output_height) {
35           for (size_t kernel_x = 0; kernel_x < kernel_width; kernel_x++) {
36             const size_t input_x = output_x * subsampling_width + kernel_x * dilation_width - input_padding_left;
37             if (input_x < output_width) {
38               memcpy(output,
39                 (const void*) ((uintptr_t) input + (input_y * input_width + input_x) * input_pixel_stride_in_bytes),
40                 group_input_channels_in_bytes);
41             } else {
42               memset(output, 0, group_input_channels_in_bytes);
43             }
44             output = (void*) ((uintptr_t) output + group_input_channels_in_bytes);
45           }
46         } else {
47           memset(output, 0, kernel_width * group_input_channels_in_bytes);
48           output = (void*) ((uintptr_t) output + kernel_width * group_input_channels_in_bytes);
49         }
50       }
51     }
52   }
53 }
54