arm_max_pool_s8.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2010-2019 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_max_pool_s8.c
  21. * Description: Pure C max pool implementation
  22. *
  23. * $Date: August 2019
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include <arm_math.h>
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup Pooling
  36. * @{
  37. */
  38. arm_status arm_max_pool_s8(const uint16_t input_y,
  39. const uint16_t input_x,
  40. const uint16_t output_y,
  41. const uint16_t output_x,
  42. const uint16_t stride_y,
  43. const uint16_t stride_x,
  44. const uint16_t kernel_y,
  45. const uint16_t kernel_x,
  46. const uint16_t pad_y,
  47. const uint16_t pad_x,
  48. const int8_t act_min,
  49. const int8_t act_max,
  50. const uint16_t channel_in,
  51. int8_t *input,
  52. int16_t *tmp_buffer,
  53. int8_t *output)
  54. {
  55. int32_t i_ch_in, i_out_x, i_out_y;
  56. int32_t i_ker_x, i_ker_y;
  57. (void)tmp_buffer;
  58. for (i_out_y = 0; i_out_y < output_y; i_out_y++)
  59. {
  60. for (i_out_x = 0; i_out_x < output_x; i_out_x++)
  61. {
  62. for (i_ch_in = 0; i_ch_in < channel_in; i_ch_in++)
  63. {
  64. /* Native data type for inner loop variables */
  65. int32_t max_val = (int8_t)Q7_MIN;
  66. /* Condition for kernel start dimension: (base_idx_<x,y> + ker_<x,y>_start) >= 0 */
  67. const int32_t base_idx_y = (i_out_y * stride_y) - pad_y;
  68. const int32_t base_idx_x = (i_out_x * stride_x) - pad_x;
  69. const int32_t ker_y_start = MAX(0, -base_idx_y);
  70. const int32_t ker_x_start = MAX(0, -base_idx_x);
  71. /* Condition for kernel end dimension: (base_idx_<x,y> + ker_<x,y>_end) < input_<x,y> */
  72. const int32_t ker_y_end = MIN(kernel_y, input_y - base_idx_y);
  73. const int32_t ker_x_end = MIN(kernel_x, input_x - base_idx_x);
  74. for (i_ker_y = ker_y_start; i_ker_y < ker_y_end; i_ker_y++)
  75. {
  76. for (i_ker_x = ker_x_start; i_ker_x < ker_x_end; i_ker_x++)
  77. {
  78. const int32_t col_idx = base_idx_x + i_ker_x;
  79. const int32_t row_idx = base_idx_y + i_ker_y;
  80. max_val = MAX(input[(row_idx * input_x + col_idx) * channel_in + i_ch_in], max_val);
  81. }
  82. }
  83. /* Activation function */
  84. max_val = MAX(max_val, act_min);
  85. max_val = MIN(max_val, act_max);
  86. output[i_ch_in + channel_in * (i_out_x + i_out_y * output_x)] = (int8_t)max_val;
  87. }
  88. }
  89. }
  90. return ARM_MATH_SUCCESS;
  91. }
  92. /**
  93. * @} end of Pooling group
  94. */