選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

97 行
2.5 KiB

  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. * 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: 09. October 2020
  24. * $Revision: V.1.0.1
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nn_tables.h"
  30. #include "arm_nnfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup Acti
  36. * @{
  37. */
  38. /**
  39. * @brief neural network activation function using direct table look-up
  40. *
  41. * @note Refer header file for details.
  42. *
  43. */
  44. void arm_nn_activations_direct_q15(q15_t *data, uint16_t size, uint16_t int_width, arm_nn_activation_type type)
  45. {
  46. uint16_t i = size;
  47. q15_t *pIn = data;
  48. q15_t *pOut = data;
  49. uint16_t shift_size = 8 + 3 - int_width;
  50. uint32_t bit_mask = 0x7FF >> int_width;
  51. uint32_t full_frac = bit_mask + 1;
  52. const q15_t *lookup_table;
  53. switch (type)
  54. {
  55. case ARM_SIGMOID:
  56. lookup_table = sigmoidTable_q15;
  57. break;
  58. case ARM_TANH:
  59. default:
  60. lookup_table = tanhTable_q15;
  61. break;
  62. }
  63. while (i)
  64. {
  65. q15_t out;
  66. q15_t in = *pIn++;
  67. q15_t frac = (uint32_t)in & bit_mask;
  68. q15_t value = lookup_table[(uint8_t)(in >> shift_size)];
  69. if ((in >> shift_size) != 0x7f)
  70. {
  71. q15_t value2 = lookup_table[(uint8_t)(1 + ((uint8_t)(in >> shift_size)))];
  72. /* doing the interpolation here for better accuracy */
  73. out = ((q31_t)(full_frac - frac) * value + (q31_t)value2 * frac) >> shift_size;
  74. }
  75. else
  76. {
  77. /* the largest positive value does not have a right side for linear interpolation */
  78. out = value;
  79. }
  80. *pOut++ = out;
  81. i--;
  82. }
  83. }
  84. /**
  85. * @} end of Acti group
  86. */