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_nn_activations_q7.c
22  * Description:  Q7 neural network activation function using direct table look-up
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_nn_tables.h"
32 #include "arm_nnfunctions.h"
33 
34 /**
35  *  @ingroup groupNN
36  */
37 
38 /**
39  * @addtogroup Acti
40  * @{
41  */
42 
43 /**
44  * @brief Q7 neural network activation function using direct table look-up
45  * @param[in,out]   data        pointer to input
46  * @param[in]       size        number of elements
47  * @param[in]       int_width   bit-width of the integer part, assume to be smaller than 3
48  * @param[in]       type        type of activation functions
49  *
50  * @details
51  *
52  * This is the direct table look-up approach.
53  *
54  * Assume here the integer part of the fixed-point is <= 3.
55  * More than 3 just not making much sense, makes no difference with
56  * saturation followed by any of these activation functions.
57  */
58 
arm_nn_activations_direct_q7(q7_t * data,uint16_t size,uint16_t int_width,arm_nn_activation_type type)59 void arm_nn_activations_direct_q7(q7_t *data, uint16_t size, uint16_t int_width, arm_nn_activation_type type)
60 {
61     uint16_t i = size;
62     q7_t *pIn = data;
63     q7_t *pOut = data;
64     q7_t in;
65     q7_t out;
66     uint16_t shift_size = 3 - int_width;
67     const q7_t *lookup_table;
68     switch (type)
69     {
70     case ARM_SIGMOID:
71         lookup_table = sigmoidTable_q7;
72         break;
73     case ARM_TANH:
74     default:
75         lookup_table = tanhTable_q7;
76         break;
77     }
78     while (i)
79     {
80         in = *pIn++;
81         out = lookup_table[(uint8_t)(in >> shift_size)];
82         *pOut++ = out;
83         i--;
84     }
85 }
86 
87 /**
88  * @} end of Acti group
89  */
90