1 /*
2  * SPDX-FileCopyrightText: Copyright 2023-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
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_vector_sum_s8
22  * Description:  Generic function for calculating vector sums
23  *
24  * $Date:        15 February 2024
25  * $Revision:    V.2.0.1
26  *
27  * Target :  Arm(R) M-Profile Architecture
28  *
29  * -------------------------------------------------------------------- */
30 
31 #include "arm_nnfunctions.h"
32 #include "arm_nnsupportfunctions.h"
33 /**
34  *  @ingroup Public
35  */
36 
37 /**
38  * @addtogroup FC
39  * @{
40  */
41 
42 /*
43  * S8 vector sum fuction in preparation for e.g. kernel sums in fully connected and matrix multiplication layer function
44  *
45  * Refer header file for details.
46  *
47  */
arm_vector_sum_s8(int32_t * vector_sum_buf,const int32_t vector_cols,const int32_t vector_rows,const int8_t * vector_data,const int32_t lhs_offset,const int32_t * bias_data)48 arm_cmsis_nn_status arm_vector_sum_s8(int32_t *vector_sum_buf,
49                                       const int32_t vector_cols,
50                                       const int32_t vector_rows,
51                                       const int8_t *vector_data,
52                                       const int32_t lhs_offset,
53                                       const int32_t *bias_data)
54 {
55 
56     if (bias_data)
57     {
58         memcpy(vector_sum_buf, bias_data, vector_rows * sizeof(int32_t));
59     }
60     else
61     {
62         memset(vector_sum_buf, 0, vector_rows * sizeof(int32_t));
63     }
64 
65     if (lhs_offset)
66     {
67 #if defined(ARM_MATH_MVEI)
68 
69         const int32_t row_loop_cnt = vector_rows / 5;
70         for (int i_row_loop_cnt = 0; i_row_loop_cnt < row_loop_cnt; i_row_loop_cnt++)
71         {
72             const int32_t col_loop_cnt = (vector_cols + 15) / 16;
73             const int8_t *vector_0 = vector_data;
74             const int8_t *vector_1 = vector_data + vector_cols;
75             const int8_t *vector_2 = vector_data + 2 * vector_cols;
76             const int8_t *vector_3 = vector_data + 3 * vector_cols;
77             const int8_t *vector_4 = vector_data + 4 * vector_cols;
78             int32_t vector_sum_0 = 0;
79             int32_t vector_sum_1 = 0;
80             int32_t vector_sum_2 = 0;
81             int32_t vector_sum_3 = 0;
82             int32_t vector_sum_4 = 0;
83             uint32_t col_cnt = (uint32_t)vector_cols;
84             for (int i = 0; i < col_loop_cnt; i++)
85             {
86                 mve_pred16_t p = vctp8q(col_cnt);
87                 col_cnt -= 16;
88                 const int8x16_t ker_0 = vldrbq_z_s8(vector_0, p);
89                 vector_sum_0 = vaddvaq_s8(vector_sum_0, ker_0);
90                 const int8x16_t ker_1 = vldrbq_z_s8(vector_1, p);
91                 vector_sum_1 = vaddvaq_s8(vector_sum_1, ker_1);
92                 const int8x16_t ker_2 = vldrbq_z_s8(vector_2, p);
93                 vector_sum_2 = vaddvaq_s8(vector_sum_2, ker_2);
94                 const int8x16_t ker_3 = vldrbq_z_s8(vector_3, p);
95                 vector_sum_3 = vaddvaq_s8(vector_sum_3, ker_3);
96                 const int8x16_t ker_4 = vldrbq_z_s8(vector_4, p);
97                 vector_sum_4 = vaddvaq_s8(vector_sum_4, ker_4);
98                 vector_0 += 16;
99                 vector_1 += 16;
100                 vector_2 += 16;
101                 vector_3 += 16;
102                 vector_4 += 16;
103             }
104             vector_data += 5 * vector_cols;
105 
106             vector_sum_0 *= lhs_offset;
107             vector_sum_1 *= lhs_offset;
108             vector_sum_2 *= lhs_offset;
109             vector_sum_3 *= lhs_offset;
110             vector_sum_4 *= lhs_offset;
111 
112             vector_sum_buf[0] += vector_sum_0;
113             vector_sum_buf[1] += vector_sum_1;
114             vector_sum_buf[2] += vector_sum_2;
115             vector_sum_buf[3] += vector_sum_3;
116             vector_sum_buf[4] += vector_sum_4;
117             vector_sum_buf += 5;
118         }
119         const int32_t loop_cnt = vector_rows % 5;
120         for (int i_row_loop_cnt = 0; i_row_loop_cnt < loop_cnt; i_row_loop_cnt++)
121         {
122             const int32_t col_loop_cnt = (vector_cols + 15) / 16;
123             const int8_t *vector_0 = vector_data;
124             int32_t vector_sum_0 = 0;
125             uint32_t col_cnt = (uint32_t)vector_cols;
126             for (int i = 0; i < col_loop_cnt; i++)
127             {
128                 mve_pred16_t p = vctp8q(col_cnt);
129                 col_cnt -= 16;
130                 const int8x16_t ker_0 = vldrbq_z_s8(vector_0, p);
131                 vector_sum_0 = vaddvaq_s8(vector_sum_0, ker_0);
132                 vector_0 += 16;
133             }
134             vector_data += vector_cols;
135             vector_sum_0 *= lhs_offset;
136 
137             vector_sum_buf[i_row_loop_cnt] += vector_sum_0;
138         }
139 #else
140         for (int i = 0; i < vector_rows; i++)
141         {
142             int32_t sum = 0;
143             for (int j = 0; j < vector_cols; j++)
144             {
145                 sum += *vector_data++;
146             }
147             *vector_sum_buf++ += sum * lhs_offset;
148         }
149 #endif
150     }
151 
152     return (ARM_CMSIS_NN_SUCCESS);
153 }
154 
155 /**
156  * @} end of FC group
157  */
158