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_s64
22  * Description:  Generic function for calculating vector sums
23  *
24  * $Date:        26 March 2024
25  * $Revision:    V.1.0.0
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_s64(int64_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 int64_t * bias_data)48 arm_cmsis_nn_status arm_vector_sum_s8_s64(int64_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 int64_t *bias_data)
54 {
55 
56     if (bias_data)
57     {
58         memcpy(vector_sum_buf, bias_data, vector_rows * sizeof(int64_t));
59     }
60     else
61     {
62         memset(vector_sum_buf, 0, vector_rows * sizeof(int64_t));
63     }
64     if (lhs_offset)
65     {
66 #if defined(ARM_MATH_MVEI)
67 
68         const int32_t row_loop_cnt = vector_rows / 5;
69         for (int i_row_loop_cnt = 0; i_row_loop_cnt < row_loop_cnt; i_row_loop_cnt++)
70         {
71             const int32_t col_loop_cnt = (vector_cols + 15) / 16;
72             const int8_t *vector_0 = vector_data;
73             const int8_t *vector_1 = vector_data + vector_cols;
74             const int8_t *vector_2 = vector_data + 2 * vector_cols;
75             const int8_t *vector_3 = vector_data + 3 * vector_cols;
76             const int8_t *vector_4 = vector_data + 4 * vector_cols;
77             int32_t vector_sum_0 = 0;
78             int32_t vector_sum_1 = 0;
79             int32_t vector_sum_2 = 0;
80             int32_t vector_sum_3 = 0;
81             int32_t vector_sum_4 = 0;
82             uint32_t col_cnt = (uint32_t)vector_cols;
83             for (int i = 0; i < col_loop_cnt; i++)
84             {
85                 mve_pred16_t p = vctp8q(col_cnt);
86                 col_cnt -= 16;
87                 const int8x16_t ker_0 = vldrbq_z_s8(vector_0, p);
88                 vector_sum_0 = vaddvaq_s8(vector_sum_0, ker_0);
89                 const int8x16_t ker_1 = vldrbq_z_s8(vector_1, p);
90                 vector_sum_1 = vaddvaq_s8(vector_sum_1, ker_1);
91                 const int8x16_t ker_2 = vldrbq_z_s8(vector_2, p);
92                 vector_sum_2 = vaddvaq_s8(vector_sum_2, ker_2);
93                 const int8x16_t ker_3 = vldrbq_z_s8(vector_3, p);
94                 vector_sum_3 = vaddvaq_s8(vector_sum_3, ker_3);
95                 const int8x16_t ker_4 = vldrbq_z_s8(vector_4, p);
96                 vector_sum_4 = vaddvaq_s8(vector_sum_4, ker_4);
97                 vector_0 += 16;
98                 vector_1 += 16;
99                 vector_2 += 16;
100                 vector_3 += 16;
101                 vector_4 += 16;
102             }
103             vector_data += 5 * vector_cols;
104 
105             vector_sum_0 *= lhs_offset;
106             vector_sum_1 *= lhs_offset;
107             vector_sum_2 *= lhs_offset;
108             vector_sum_3 *= lhs_offset;
109             vector_sum_4 *= lhs_offset;
110 
111             vector_sum_buf[0] += vector_sum_0;
112             vector_sum_buf[1] += vector_sum_1;
113             vector_sum_buf[2] += vector_sum_2;
114             vector_sum_buf[3] += vector_sum_3;
115             vector_sum_buf[4] += vector_sum_4;
116             vector_sum_buf += 5;
117         }
118         const int32_t loop_cnt = vector_rows % 5;
119         for (int i_row_loop_cnt = 0; i_row_loop_cnt < loop_cnt; i_row_loop_cnt++)
120         {
121             const int32_t col_loop_cnt = (vector_cols + 15) / 16;
122             const int8_t *vector_0 = vector_data;
123             int32_t vector_sum_0 = 0;
124             uint32_t col_cnt = (uint32_t)vector_cols;
125             for (int i = 0; i < col_loop_cnt; i++)
126             {
127                 mve_pred16_t p = vctp8q(col_cnt);
128                 col_cnt -= 16;
129                 const int8x16_t ker_0 = vldrbq_z_s8(vector_0, p);
130                 vector_sum_0 = vaddvaq_s8(vector_sum_0, ker_0);
131                 vector_0 += 16;
132             }
133             vector_data += vector_cols;
134             vector_sum_0 *= lhs_offset;
135 
136             vector_sum_buf[i_row_loop_cnt] += vector_sum_0;
137         }
138 #else
139         for (int i = 0; i < vector_rows; i++)
140         {
141             int64_t sum = 0;
142             for (int j = 0; j < vector_cols; j++)
143             {
144                 sum += *vector_data++;
145             }
146             *vector_sum_buf++ += sum * (int64_t)lhs_offset;
147         }
148 #endif
149     }
150 
151     return (ARM_CMSIS_NN_SUCCESS);
152 }
153 
154 /**
155  * @} end of FC group
156  */
157