Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_nn_activations_q7.c Source File

arm_nn_activations_q7.c

00001 /*
00002  * Copyright (C) 2010-2018 Arm Limited or its affiliates. All rights reserved.
00003  *
00004  * SPDX-License-Identifier: Apache-2.0
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the License); you may
00007  * not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  * www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00014  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 /* ----------------------------------------------------------------------
00020  * Project:      CMSIS NN Library
00021  * Title:        arm_nn_activations_q7.c
00022  * Description:  Q7 neural network activation function using direct table look-up
00023  *
00024  * $Date:        17. January 2018
00025  * $Revision:    V.1.0.0
00026  *
00027  * Target Processor:  Cortex-M cores
00028  *
00029  * -------------------------------------------------------------------- */
00030 
00031 #include "arm_math.h"
00032 #include "arm_common_tables.h"
00033 #include "arm_nnfunctions.h"
00034 
00035 /**
00036  *  @ingroup groupNN
00037  */
00038 
00039 /**
00040  * @addtogroup Acti
00041  * @{
00042  */
00043 
00044   /**
00045    * @brief Q7 neural network activation function using direct table look-up
00046    * @param[in,out]   data        pointer to input
00047    * @param[in]       size        number of elements
00048    * @param[in]       int_width   bit-width of the integer part, assume to be smaller than 3
00049    * @param[in]       type        type of activation functions
00050    * @return none.
00051    *
00052    * @details
00053    * 
00054    * This is the direct table look-up approach.
00055    *
00056    * Assume here the integer part of the fixed-point is <= 3.
00057    * More than 3 just not making much sense, makes no difference with
00058    * saturation followed by any of these activation functions. 
00059    */
00060 
00061 void arm_nn_activations_direct_q7(q7_t * data, uint16_t size, uint16_t int_width, arm_nn_activation_type type)
00062 {
00063     uint16_t  i = size;
00064     q7_t     *pIn = data;
00065     q7_t     *pOut = data;
00066     q7_t      in;
00067     q7_t      out;
00068     uint16_t  shift_size = 3 - int_width;
00069     const q7_t *lookup_table;
00070     switch (type)
00071     {
00072     case ARM_SIGMOID:
00073         lookup_table = sigmoidTable_q7;
00074         break;
00075     case ARM_TANH:
00076     default:
00077         lookup_table = tanhTable_q7;
00078         break;
00079     }
00080     while (i)
00081     {
00082         in = *pIn++;
00083         out = lookup_table[(uint8_t) in >> shift_size];
00084         *pOut++ = out;
00085         i--;
00086     }
00087 }
00088 
00089 /**
00090  * @} end of Acti group
00091  */
00092