1 /*
2  * SPDX-FileCopyrightText: Copyright 2022, 2024 Arm Limited and/or its affiliates <open-source-office.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_nn_lstm_calculate_gate_s16.c
22  * Description:  Update single gate for an incremental step of LSTM function.
23  *
24  * $Date:        26 March 2024
25  * $Revision:    V.1.0.0
26  *
27  * Target Processor:  Cortex-M cores
28  *
29  * -------------------------------------------------------------------- */
30 
31 #include "arm_nn_tables.h"
32 #include "arm_nnfunctions.h"
33 #include "arm_nnsupportfunctions.h"
34 /**
35  * @ingroup groupSupport
36  */
37 
38 /**
39  * @addtogroup supportLSTM
40  * @{
41  */
42 
43 /*
44  * Calculates a single LSTM gate, int16x8_16 version.
45  * Refer to header file for details
46  */
arm_nn_lstm_calculate_gate_s16(const int16_t * data_in,const int16_t * hidden_in,const cmsis_nn_lstm_gate * gate,const cmsis_nn_lstm_params * params,int16_t * output,const int32_t batch_offset)47 arm_cmsis_nn_status arm_nn_lstm_calculate_gate_s16(const int16_t *data_in,
48                                                    const int16_t *hidden_in,
49                                                    const cmsis_nn_lstm_gate *gate,
50                                                    const cmsis_nn_lstm_params *params,
51                                                    int16_t *output,
52                                                    const int32_t batch_offset)
53 {
54 
55     memset(output, 0, params->hidden_size * params->batch_size * sizeof(int16_t));
56 
57     arm_nn_vec_mat_mul_result_acc_s16(data_in,
58                                       gate->input_weights,
59                                       gate->input_effective_bias,
60                                       output,
61                                       gate->input_multiplier,
62                                       gate->input_shift,
63                                       params->input_size,
64                                       params->hidden_size,
65                                       params->batch_size,
66                                       batch_offset);
67 
68     if (hidden_in)
69     {
70 
71         arm_nn_vec_mat_mul_result_acc_s16(hidden_in,
72                                           gate->hidden_weights,
73                                           gate->hidden_effective_bias,
74                                           output,
75                                           gate->hidden_multiplier,
76                                           gate->hidden_shift,
77                                           params->hidden_size,
78                                           params->hidden_size,
79                                           params->batch_size,
80                                           batch_offset);
81     }
82 
83     arm_nn_activation_s16(output, output, params->hidden_size * params->batch_size, 0, gate->activation_type);
84 
85     return ARM_CMSIS_NN_SUCCESS;
86 }
87 /**
88  * @} end of supportLSTM group
89  */