esp_nn_max_pool_ansi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include <common_functions.h>
  16. void esp_nn_max_pool_s8_ansi(const int8_t *input,
  17. const uint16_t input_wd,
  18. const uint16_t input_ht,
  19. int8_t *output,
  20. const uint16_t output_wd,
  21. const uint16_t output_ht,
  22. const uint16_t stride_wd,
  23. const uint16_t stride_ht,
  24. const uint16_t filter_wd,
  25. const uint16_t filter_ht,
  26. const uint16_t pad_wd,
  27. const uint16_t pad_ht,
  28. const int32_t activation_min,
  29. const int32_t activation_max,
  30. const uint16_t channels)
  31. {
  32. int32_t base_y = -pad_ht;
  33. for (int32_t out_y = 0; out_y < output_ht; out_y++, base_y += stride_ht) {
  34. int32_t base_x = -pad_wd;
  35. for (int32_t out_x = 0; out_x < output_wd; out_x++, base_x += stride_wd) {
  36. /* Make sure filter does not cross the input box */
  37. int32_t filter_y_start = max(0, -base_y);
  38. int32_t filter_x_start = max(0, -base_x);
  39. int32_t filter_y_end = min(filter_ht, input_ht - base_y);
  40. int32_t filter_x_end = min(filter_wd, input_wd - base_x);
  41. for (int32_t ch_idx = 0; ch_idx < channels; ch_idx++) {
  42. int8_t result = INT8_MIN;
  43. for (int32_t filter_y = filter_y_start; filter_y < filter_y_end; filter_y++) {
  44. for (int32_t filter_x = filter_x_start; filter_x < filter_x_end; filter_x++) {
  45. int32_t in_x_idx = base_x + filter_x;
  46. int32_t in_y_idx = base_y + filter_y;
  47. int32_t input_index = (in_y_idx * input_wd + in_x_idx) * channels + ch_idx;
  48. result = max(input[input_index], result);
  49. }
  50. }
  51. /* Activation function */
  52. result = max(result, activation_min);
  53. result = min(result, activation_max);
  54. int32_t output_index = (out_y * output_wd + out_x) * channels + ch_idx;
  55. output[output_index] = result;
  56. }
  57. }
  58. }
  59. }