arm_nn_activations_q15.c 2.9 KB

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