arm_elementwise_add_s16.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_elementwise_add_s16
  21. * Description: Elementwise add
  22. *
  23. * $Date: 14 Februari 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. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup BasicMath
  36. * @{
  37. */
  38. /*
  39. * s16 elementwise add
  40. *
  41. * Refer header file for details.
  42. *
  43. */
  44. /* Note: __SHIFT is expected to be <=0 */
  45. arm_status arm_elementwise_add_s16(const int16_t *input_1_vect,
  46. const int16_t *input_2_vect,
  47. const int32_t input_1_offset,
  48. const int32_t input_1_mult,
  49. const int32_t input_1_shift,
  50. const int32_t input_2_offset,
  51. const int32_t input_2_mult,
  52. const int32_t input_2_shift,
  53. const int32_t left_shift,
  54. int16_t *output,
  55. const int32_t out_offset,
  56. const int32_t out_mult,
  57. const int32_t out_shift,
  58. const int32_t out_activation_min,
  59. const int32_t out_activation_max,
  60. const int32_t block_size)
  61. {
  62. (void)input_1_offset;
  63. (void)input_2_offset;
  64. (void)out_offset;
  65. int32_t loop_count;
  66. int32_t input_1;
  67. int32_t input_2;
  68. int32_t sum;
  69. loop_count = block_size;
  70. while (loop_count > 0)
  71. {
  72. /* C = A + B */
  73. input_1 = *input_1_vect++ << left_shift;
  74. input_2 = *input_2_vect++ << left_shift;
  75. input_1 = arm_nn_requantize(input_1, input_1_mult, input_1_shift);
  76. input_2 = arm_nn_requantize(input_2, input_2_mult, input_2_shift);
  77. sum = input_1 + input_2;
  78. sum = arm_nn_requantize(sum, out_mult, out_shift);
  79. sum = MAX(sum, out_activation_min);
  80. sum = MIN(sum, out_activation_max);
  81. *output++ = (int16_t)sum;
  82. /* Decrement loop counter */
  83. loop_count--;
  84. }
  85. return (ARM_MATH_SUCCESS);
  86. }
  87. /**
  88. * @} end of BasicMath group
  89. */