1 /*
2  * Copyright (C) 2010-2020 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 /* ----------------------------------------------------------------------
20  * Project:      CMSIS NN Library
21  * Title:        arm_fully_connected_q15.c
22  * Description:  Q15 basic fully-connected layer function
23  *
24  * $Date:        09. October 2020
25  * $Revision:    V.1.0.1
26  *
27  * Target Processor:  Cortex-M cores
28  *
29  * -------------------------------------------------------------------- */
30 
31 #include "arm_nnfunctions.h"
32 #include "arm_nnsupportfunctions.h"
33 
34 /**
35  *  @ingroup groupNN
36  */
37 
38 /**
39  * @addtogroup FC
40  * @{
41  */
42 
43 /**
44  * @brief Q15 opt fully-connected layer function
45  * @param[in]       pV          pointer to input vector
46  * @param[in]       pM          pointer to matrix weights
47  * @param[in]       dim_vec     length of the vector
48  * @param[in]       num_of_rows number of rows in weight matrix
49  * @param[in]       bias_shift  amount of left-shift for bias
50  * @param[in]       out_shift   amount of right-shift for output
51  * @param[in]       bias        pointer to bias
52  * @param[in,out]   pOut        pointer to output vector
53  * @param[in,out]   vec_buffer  pointer to buffer space for input
54  * @return     The function returns <code>ARM_MATH_SUCCESS</code>
55  *
56  *
57  * @details
58  *
59  * <b>Buffer size:</b>
60  *
61  * vec_buffer size: 0
62  *
63  */
64 
arm_fully_connected_q15(const q15_t * pV,const q15_t * pM,const uint16_t dim_vec,const uint16_t num_of_rows,const uint16_t bias_shift,const uint16_t out_shift,const q15_t * bias,q15_t * pOut,q15_t * vec_buffer)65 arm_status arm_fully_connected_q15(const q15_t *pV,
66                                    const q15_t *pM,
67                                    const uint16_t dim_vec,
68                                    const uint16_t num_of_rows,
69                                    const uint16_t bias_shift,
70                                    const uint16_t out_shift,
71                                    const q15_t *bias,
72                                    q15_t *pOut,
73                                    q15_t *vec_buffer)
74 {
75     (void)vec_buffer;
76 #if defined(ARM_MATH_DSP)
77     /* Run the following code for Cortex-M4 and Cortex-M7 */
78 
79     const q15_t *pB = pM;
80     const q15_t *pB2 = pB + dim_vec;
81     q15_t *pO = pOut;
82     const q15_t *pA;
83     const q15_t *pBias = bias;
84     uint16_t rowCnt = num_of_rows >> 1;
85 
86     /* this loop loops over different output */
87     while (rowCnt)
88     {
89         q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
90         q31_t sum2 = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
91 
92         uint16_t colCnt = dim_vec >> 2;
93 
94         pA = pV;
95         pB2 = pB + dim_vec;
96 
97         while (colCnt)
98         {
99             q31_t inV1, inM1, inM2;
100             inV1 = arm_nn_read_q15x2_ia(&pA);
101             inM1 = arm_nn_read_q15x2_ia(&pB);
102             sum = __SMLAD(inV1, inM1, sum);
103             inM2 = arm_nn_read_q15x2_ia(&pB2);
104             sum2 = __SMLAD(inV1, inM2, sum2);
105 
106             inV1 = arm_nn_read_q15x2_ia(&pA);
107             inM1 = arm_nn_read_q15x2_ia(&pB);
108             sum = __SMLAD(inV1, inM1, sum);
109             inM2 = arm_nn_read_q15x2_ia(&pB2);
110             sum2 = __SMLAD(inV1, inM2, sum2);
111 
112             colCnt--;
113         }
114         colCnt = dim_vec & 0x3;
115         while (colCnt)
116         {
117             q15_t inV = *pA++;
118             q15_t inM = *pB++;
119             q15_t inM2 = *pB2++;
120 
121             sum += inV * inM;
122             sum2 += inV * inM2;
123             colCnt--;
124         } /* while over colCnt */
125         *pO++ = (q15_t)(__SSAT((sum >> out_shift), 16));
126         *pO++ = (q15_t)(__SSAT((sum2 >> out_shift), 16));
127 
128         /* adjust the pointers and counters */
129         pB = pB + dim_vec;
130         rowCnt--;
131     }
132 
133     rowCnt = num_of_rows & 0x1;
134 
135     while (rowCnt)
136     {
137         q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
138 
139         uint16_t colCnt = dim_vec >> 2;
140 
141         pA = pV;
142 
143         while (colCnt)
144         {
145             q31_t inV1, inM1;
146             inV1 = arm_nn_read_q15x2_ia(&pA);
147             inM1 = arm_nn_read_q15x2_ia(&pB);
148             sum = __SMLAD(inV1, inM1, sum);
149 
150             inV1 = arm_nn_read_q15x2_ia(&pA);
151             inM1 = arm_nn_read_q15x2_ia(&pB);
152             sum = __SMLAD(inV1, inM1, sum);
153 
154             colCnt--;
155         }
156 
157         /* left-over of the vector */
158         colCnt = dim_vec & 0x3;
159         while (colCnt)
160         {
161             q15_t inV = *pA++;
162             q15_t inM = *pB++;
163 
164             sum += inV * inM;
165 
166             colCnt--;
167         }
168 
169         *pO++ = (q15_t)(__SSAT((sum >> out_shift), 16));
170 
171         rowCnt--;
172     }
173 
174 #else
175     int i, j;
176     /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
177     for (i = 0; i < num_of_rows; i++)
178     {
179         int ip_out = ((q31_t)(bias[i]) << bias_shift) + NN_ROUND(out_shift);
180         for (j = 0; j < dim_vec; j++)
181         {
182             ip_out += pV[j] * pM[i * dim_vec + j];
183         }
184         pOut[i] = (q15_t)__SSAT((ip_out >> out_shift), 16);
185     }
186 
187 #endif /* ARM_MATH_DSP */
188 
189     /* Return to application */
190     return (ARM_MATH_SUCCESS);
191 }
192 
193 /**
194  * @} end of FC group
195  */
196