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_fully_connected_q15_opt_ref(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)21 void arm_fully_connected_q15_opt_ref(const q15_t * pV,  // pointer to vector
22                                      const q15_t * pM,  // pointer to matrix
23                                      const uint16_t dim_vec,    // length of the vector
24                                      const uint16_t num_of_rows,    // numCol of A
25                                      const uint16_t bias_shift, // amount of left-shift for bias
26                                      const uint16_t out_shift,  // amount of right-shift for output
27                                      const q15_t * bias, q15_t * pOut,  // output operand
28                                      q15_t * vec_buffer)
29 {
30 
31     uint16_t  rowCnt = num_of_rows >> 2;
32     const q15_t *pB = pM;
33     const q15_t *pA;
34     q15_t    *pO = pOut;
35     const q15_t *pBias = bias;
36 
37     while (rowCnt)
38     {
39         pA = pV;
40 #ifndef ARM_NN_TRUNCATE
41         q31_t     sum = (*pBias++ << bias_shift) + (0x1 << (out_shift - 1));
42         q31_t     sum2 = (*pBias++ << bias_shift) + (0x1 << (out_shift - 1));
43         q31_t     sum3 = (*pBias++ << bias_shift) + (0x1 << (out_shift - 1));
44         q31_t     sum4 = (*pBias++ << bias_shift) + (0x1 << (out_shift - 1));
45 #else
46         q31_t     sum = *pBias++ << bias_shift;
47         q31_t     sum2 = *pBias++ << bias_shift;
48         q31_t     sum3 = *pBias++ << bias_shift;
49         q31_t     sum4 = *pBias++ << bias_shift;
50 #endif
51 
52         uint16_t  colCnt = dim_vec >> 1;
53 
54         while (colCnt)
55         {
56             q15_t     inA1 = *pA++;
57             q15_t     inA2 = *pA++;
58 
59             q15_t     inB1 = *pB++;
60             q15_t     inB2 = *pB++;
61             sum += inA1 * inB1 + inA2 * inB2;
62 
63             inB1 = *pB++;
64             inB2 = *pB++;
65             sum2 += inA1 * inB1 + inA2 * inB2;
66 
67             inB1 = *pB++;
68             inB2 = *pB++;
69             sum3 += inA1 * inB1 + inA2 * inB2;
70 
71             inB1 = *pB++;
72             inB2 = *pB++;
73             sum4 += inA1 * inB1 + inA2 * inB2;
74 
75             colCnt--;
76         }
77         colCnt = dim_vec & 0x1;
78         while (colCnt)
79         {
80             q15_t     inA = *pA++;
81             q15_t     inB = *pB++;
82             sum += inA * inB;
83             inB = *pB++;
84             sum2 += inA * inB;
85             inB = *pB++;
86             sum3 += inA * inB;
87             inB = *pB++;
88             sum4 += inA * inB;
89             colCnt--;
90         }
91         *pO++ = (q15_t) __SSAT((sum >> out_shift), 16);
92         *pO++ = (q15_t) __SSAT((sum2 >> out_shift), 16);
93         *pO++ = (q15_t) __SSAT((sum3 >> out_shift), 16);
94         *pO++ = (q15_t) __SSAT((sum4 >> out_shift), 16);
95 
96         rowCnt--;
97     }
98 
99     rowCnt = num_of_rows & 0x3;
100 
101     while (rowCnt)
102     {
103         pA = pV;
104 #ifndef ARM_NN_TRUNCATE
105         int       ip_out = (*pBias++ << bias_shift) + (0x1 << (out_shift - 1));
106 #else
107         int       ip_out = *pBias++ << bias_shift;
108 #endif
109         for (int j = 0; j < dim_vec; j++)
110         {
111             q15_t     inA = *pA++;
112             q15_t     inB = *pB++;
113             ip_out += inA * inB;
114         }
115         *pO++ = (q15_t) __SSAT((ip_out >> out_shift), 16);
116 
117         rowCnt--;
118     }
119 }
120