arm_max_pool_s16.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_max_pool_s16.c
  21. * Description: Pooling function implementations
  22. *
  23. * $Date: 24. January 2022
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M CPUs
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. static void compare_and_replace_if_larger(int16_t *base, const int16_t *target, int32_t length)
  32. {
  33. q15_t *dst = base;
  34. const q15_t *src = target;
  35. union arm_nnword ref_max;
  36. union arm_nnword comp_max;
  37. int32_t cnt = length >> 1;
  38. while (cnt > 0l)
  39. {
  40. ref_max.word = arm_nn_read_q15x2(dst);
  41. comp_max.word = arm_nn_read_q15x2_ia(&src);
  42. if (comp_max.half_words[0] > ref_max.half_words[0])
  43. {
  44. ref_max.half_words[0] = comp_max.half_words[0];
  45. }
  46. if (comp_max.half_words[1] > ref_max.half_words[1])
  47. {
  48. ref_max.half_words[1] = comp_max.half_words[1];
  49. }
  50. arm_nn_write_q15x2_ia(&dst, ref_max.word);
  51. cnt--;
  52. }
  53. if (length & 0x1)
  54. {
  55. if (*src > *dst)
  56. {
  57. *dst = *src;
  58. }
  59. }
  60. }
  61. static void clamp_output(int16_t *source, int32_t length, const int16_t act_min, const int16_t act_max)
  62. {
  63. union arm_nnword in;
  64. int32_t cnt = length >> 1;
  65. while (cnt > 0l)
  66. {
  67. in.word = arm_nn_read_q15x2(source);
  68. in.half_words[0] = MAX(in.half_words[0], act_min);
  69. in.half_words[0] = MIN(in.half_words[0], act_max);
  70. in.half_words[1] = MAX(in.half_words[1], act_min);
  71. in.half_words[1] = MIN(in.half_words[1], act_max);
  72. arm_nn_write_q15x2_ia(&source, in.word);
  73. cnt--;
  74. }
  75. if (length & 0x1)
  76. {
  77. int16_t comp = *source;
  78. comp = MAX(comp, act_min);
  79. comp = MIN(comp, act_max);
  80. *source = comp;
  81. }
  82. }
  83. /**
  84. * @ingroup groupNN
  85. */
  86. /**
  87. * @addtogroup Pooling
  88. * @{
  89. */
  90. /*
  91. * Optimized s16 max pooling function
  92. *
  93. * Refer to header file for details.
  94. *
  95. */
  96. arm_status arm_max_pool_s16(const cmsis_nn_context *ctx,
  97. const cmsis_nn_pool_params *pool_params,
  98. const cmsis_nn_dims *input_dims,
  99. const int16_t *src,
  100. const cmsis_nn_dims *filter_dims,
  101. const cmsis_nn_dims *output_dims,
  102. int16_t *dst)
  103. {
  104. const int32_t input_y = input_dims->h;
  105. const int32_t input_x = input_dims->w;
  106. const int32_t output_y = output_dims->h;
  107. const int32_t output_x = output_dims->w;
  108. const int32_t stride_y = pool_params->stride.h;
  109. const int32_t stride_x = pool_params->stride.w;
  110. const int32_t kernel_y = filter_dims->h;
  111. const int32_t kernel_x = filter_dims->w;
  112. const int32_t pad_y = pool_params->padding.h;
  113. const int32_t pad_x = pool_params->padding.w;
  114. const int16_t act_min = pool_params->activation.min;
  115. const int16_t act_max = pool_params->activation.max;
  116. const int32_t channel_in = input_dims->c;
  117. (void)ctx;
  118. int16_t *dst_base = dst;
  119. for (int i_y = 0, base_idx_y = -pad_y; i_y < output_y; base_idx_y += stride_y, i_y++)
  120. {
  121. for (int i_x = 0, base_idx_x = -pad_x; i_x < output_x; base_idx_x += stride_x, i_x++)
  122. {
  123. /* Condition for kernel start dimension: (base_idx_<x,y> + kernel_<x,y>_start) >= 0 */
  124. const int32_t ker_y_start = MAX(0, -base_idx_y);
  125. const int32_t ker_x_start = MAX(0, -base_idx_x);
  126. /* Condition for kernel end dimension: (base_idx_<x,y> + kernel_<x,y>_end) < dim_src_<width,height> */
  127. const int32_t kernel_y_end = MIN(kernel_y, input_y - base_idx_y);
  128. const int32_t kernel_x_end = MIN(kernel_x, input_x - base_idx_x);
  129. int count = 0;
  130. for (int k_y = ker_y_start; k_y < kernel_y_end; k_y++)
  131. {
  132. for (int k_x = ker_x_start; k_x < kernel_x_end; k_x++)
  133. {
  134. const int16_t *start = src + channel_in * (k_x + base_idx_x + (k_y + base_idx_y) * input_x);
  135. if (count == 0)
  136. {
  137. memcpy(dst, start, channel_in * sizeof(int16_t));
  138. count++;
  139. }
  140. else
  141. {
  142. compare_and_replace_if_larger(dst, start, channel_in);
  143. }
  144. }
  145. }
  146. /* 'count' is expected to be non-zero here. */
  147. dst += channel_in;
  148. }
  149. }
  150. clamp_output(dst_base, output_x * output_y * channel_in, act_min, act_max);
  151. return ARM_MATH_SUCCESS;
  152. }
  153. /**
  154. * @} end of Pooling group
  155. */