arm_avgpool_s16.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2022 Arm Limited or its affiliates.
  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_avgpool_s16.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 3. February 2022
  24. * $Revision: V.1.0.1
  25. *
  26. * Target Processor: Cortex-M CPUs
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup Pooling
  36. * @{
  37. */
  38. /*
  39. * s16 average pooling function
  40. *
  41. * Refer to header file for details.
  42. *
  43. */
  44. arm_status arm_avgpool_s16(const cmsis_nn_context *ctx,
  45. const cmsis_nn_pool_params *pool_params,
  46. const cmsis_nn_dims *input_dims,
  47. const q15_t *src,
  48. const cmsis_nn_dims *filter_dims,
  49. const cmsis_nn_dims *output_dims,
  50. q15_t *dst)
  51. {
  52. (void)ctx;
  53. const int32_t input_y = input_dims->h;
  54. const int32_t input_x = input_dims->w;
  55. const int32_t output_y = output_dims->h;
  56. const int32_t output_x = output_dims->w;
  57. const int32_t stride_y = pool_params->stride.h;
  58. const int32_t stride_x = pool_params->stride.w;
  59. const int32_t kernel_y = filter_dims->h;
  60. const int32_t kernel_x = filter_dims->w;
  61. const int32_t pad_y = pool_params->padding.h;
  62. const int32_t pad_x = pool_params->padding.w;
  63. const int32_t act_min = pool_params->activation.min;
  64. const int32_t act_max = pool_params->activation.max;
  65. const int32_t ch_src = input_dims->c;
  66. /* Reference C code adapted from CMSIS-NN arm_avgpool_s8.c.
  67. */
  68. for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
  69. {
  70. for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
  71. {
  72. /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  73. const int32_t ker_y_start = MAX(0, -base_idx_y);
  74. const int32_t ker_x_start = MAX(0, -base_idx_x);
  75. /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  76. const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
  77. const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
  78. for (int i_ch_in = 0; i_ch_in < ch_src; i_ch_in++)
  79. {
  80. int sum = 0;
  81. int count = 0;
  82. for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
  83. {
  84. for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
  85. {
  86. sum += src[i_ch_in + ch_src * (k_x + base_idx_x + (k_y + base_idx_y) * input_x)];
  87. count++;
  88. }
  89. }
  90. // Prevent static code issue DIVIDE_BY_ZERO.
  91. if (count == 0)
  92. {
  93. return ARM_MATH_ARGUMENT_ERROR;
  94. }
  95. sum = sum > 0 ? (sum + count / 2) / count : (sum - count / 2) / count;
  96. sum = MAX(sum, act_min);
  97. sum = MIN(sum, act_max);
  98. dst[i_ch_in + ch_src * (i_x + i_y * output_x)] = sum;
  99. }
  100. }
  101. }
  102. return ARM_MATH_SUCCESS;
  103. }
  104. int32_t arm_avgpool_s16_get_buffer_size(const int output_x, const int ch_src)
  105. {
  106. (void)output_x;
  107. (void)ch_src;
  108. return 0;
  109. }
  110. /**
  111. * @} end of Pooling group
  112. */