1 /*
2 * Copyright (C) 2010-2018 Arm Limited or its affiliates. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "ref_functions.h"
20
arm_convolve_HWC_q7_ref(const q7_t * Im_in,const uint16_t dim_im_in,const uint16_t ch_im_in,const q7_t * wt,const uint16_t ch_im_out,const uint16_t dim_kernel,const uint16_t padding,const uint16_t stride,const q7_t * bias,const uint16_t bias_shift,const uint16_t out_shift,q7_t * Im_out,const uint16_t dim_im_out,q15_t * bufferA,q7_t * bufferB)21 void arm_convolve_HWC_q7_ref(const q7_t * Im_in, // input image
22 const uint16_t dim_im_in, // input image dimention
23 const uint16_t ch_im_in, // number of input image channels
24 const q7_t * wt, // kernel weights
25 const uint16_t ch_im_out, // number of filters, i.e., output image channels
26 const uint16_t dim_kernel, // filter kernel size
27 const uint16_t padding, // padding sizes
28 const uint16_t stride, // stride
29 const q7_t * bias, // bias
30 const uint16_t bias_shift, const uint16_t out_shift, q7_t * Im_out, // output image
31 const uint16_t dim_im_out, // output image dimension
32 q15_t * bufferA, //buffer space for input
33 q7_t * bufferB //buffer space for output
34 )
35 {
36 int i, j, k, l, m, n;
37 int conv_out;
38 int in_row, in_col;
39
40 for (i = 0; i < ch_im_out; i++)
41 {
42 for (j = 0; j < dim_im_out; j++)
43 {
44 for (k = 0; k < dim_im_out; k++)
45 {
46 #ifndef ARM_NN_TRUNCATE
47 conv_out = ((q31_t) (bias[i]) << bias_shift) + (0x1 << (out_shift - 1));
48 #else
49 conv_out = bias[i] << bias_shift;
50 #endif
51 for (m = 0; m < dim_kernel; m++)
52 {
53 for (n = 0; n < dim_kernel; n++)
54 {
55 // if-for implementation
56 in_row = stride * j + m - padding;
57 in_col = stride * k + n - padding;
58 if (in_row >= 0 && in_col >= 0 && in_row < dim_im_in && in_col < dim_im_in)
59 {
60 for (l = 0; l < ch_im_in; l++)
61 {
62 conv_out += Im_in[(in_row * dim_im_in + in_col) * ch_im_in + l] *
63 wt[i * ch_im_in * dim_kernel * dim_kernel + (m * dim_kernel + n) * ch_im_in + l];
64 }
65 }
66 }
67 }
68 Im_out[i + (j * dim_im_out + k) * ch_im_out] = (q7_t) __SSAT((conv_out >> out_shift), 8);
69 }
70 }
71 }
72 }
73