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 /* ----------------------------------------------------------------------
20  * Project:      CMSIS NN Library
21  * Title:        arm_nn_activations_q15.c
22  * Description:  Q15 neural network activation function using direct table look-up
23  *
24  * $Date:        17. January 2018
25  * $Revision:    V.1.0.0
26  *
27  * Target Processor:  Cortex-M cores
28  *
29  * -------------------------------------------------------------------- */
30 
31 #include "arm_math.h"
32 #include "arm_common_tables.h"
33 #include "arm_nnfunctions.h"
34 
35 /**
36  *  @ingroup groupNN
37  */
38 
39 /**
40  * @addtogroup Acti
41  * @{
42  */
43 
44   /**
45    * @brief Q15 neural network activation function using direct table look-up
46    * @param[in,out]   data        pointer to input
47    * @param[in]       size        number of elements
48    * @param[in]       int_width   bit-width of the integer part, assume to be smaller than 3
49    * @param[in]       type        type of activation functions
50    * @return none.
51    *
52    * @details
53    *
54    * This is the direct table look-up approach.
55    *
56    * Assume here the integer part of the fixed-point is <= 3.
57    * More than 3 just not making much sense, makes no difference with
58    * saturation followed by any of these activation functions.
59    */
60 
arm_nn_activations_direct_q15(q15_t * data,uint16_t size,uint16_t int_width,arm_nn_activation_type type)61 void arm_nn_activations_direct_q15(q15_t * data, uint16_t size, uint16_t int_width, arm_nn_activation_type type)
62 {
63     uint16_t  i = size;
64     q15_t    *pIn = data;
65     q15_t    *pOut = data;
66     uint16_t  shift_size = 8 + 3 - int_width;
67     uint32_t  bit_mask = 0x7FF >> int_width;
68     uint32_t  full_frac = bit_mask + 1;
69     const q15_t *lookup_table;
70 
71     switch (type)
72     {
73     case ARM_SIGMOID:
74         lookup_table = sigmoidTable_q15;
75         break;
76     case ARM_TANH:
77     default:
78         lookup_table = tanhTable_q15;
79         break;
80     }
81 
82     while (i)
83     {
84         q15_t     out;
85         q15_t     in = *pIn++;
86         q15_t     frac = (uint32_t) in & bit_mask;
87         q15_t     value = lookup_table[__USAT(in >> shift_size, 8)];
88         q15_t     value2 = lookup_table[__USAT(1 + (in >> shift_size), 8)];
89 
90         /* doing the interpolation here for better accuracy */
91         out = ((q31_t) (full_frac - frac) * value + (q31_t) value2 * frac) >> shift_size;
92 
93         *pOut++ = out;
94         i--;
95     }
96 
97 }
98 
99 /**
100  * @} end of Acti group
101  */
102