arm_fully_connected_q15.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2010-2018 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_fully_connected_q15.c
  21. * Description: Q15 basic fully-connected layer function
  22. *
  23. * $Date: 17. January 2018
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_math.h"
  30. #include "arm_nnfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup FC
  36. * @{
  37. */
  38. /**
  39. * @brief Q15 opt fully-connected layer function
  40. * @param[in] pV pointer to input vector
  41. * @param[in] pM pointer to matrix weights
  42. * @param[in] dim_vec length of the vector
  43. * @param[in] num_of_rows number of rows in weight matrix
  44. * @param[in] bias_shift amount of left-shift for bias
  45. * @param[in] out_shift amount of right-shift for output
  46. * @param[in] bias pointer to bias
  47. * @param[in,out] pOut pointer to output vector
  48. * @param[in,out] vec_buffer pointer to buffer space for input
  49. * @return The function returns <code>ARM_MATH_SUCCESS</code>
  50. *
  51. *
  52. * @details
  53. *
  54. * <b>Buffer size:</b>
  55. *
  56. * vec_buffer size: 0
  57. *
  58. */
  59. arm_status
  60. arm_fully_connected_q15(const q15_t * pV,
  61. const q15_t * pM,
  62. const uint16_t dim_vec,
  63. const uint16_t num_of_rows,
  64. const uint16_t bias_shift,
  65. const uint16_t out_shift,
  66. const q15_t * bias,
  67. q15_t * pOut,
  68. q15_t * vec_buffer)
  69. {
  70. (void)vec_buffer;
  71. #if defined (ARM_MATH_DSP)
  72. /* Run the following code for Cortex-M4 and Cortex-M7 */
  73. const q15_t *pB = pM;
  74. const q15_t *pB2 = pB + dim_vec;
  75. q15_t *pO = pOut;
  76. const q15_t *pA;
  77. const q15_t *pBias = bias;
  78. uint16_t rowCnt = num_of_rows >> 1;
  79. /* this loop loops over different output */
  80. while (rowCnt) {
  81. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  82. q31_t sum2 = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  83. uint16_t colCnt = dim_vec >> 2;
  84. pA = pV;
  85. pB2 = pB + dim_vec;
  86. while (colCnt)
  87. {
  88. q31_t inV1, inM1, inM2;
  89. inV1 = arm_nn_read_q15x2_ia(&pA);
  90. inM1 = arm_nn_read_q15x2_ia(&pB);
  91. sum = __SMLAD(inV1, inM1, sum);
  92. inM2 = arm_nn_read_q15x2_ia(&pB2);
  93. sum2 = __SMLAD(inV1, inM2, sum2);
  94. inV1 = arm_nn_read_q15x2_ia(&pA);
  95. inM1 = arm_nn_read_q15x2_ia(&pB);
  96. sum = __SMLAD(inV1, inM1, sum);
  97. inM2 = arm_nn_read_q15x2_ia(&pB2);
  98. sum2 = __SMLAD(inV1, inM2, sum2);
  99. colCnt--;
  100. }
  101. colCnt = dim_vec & 0x3;
  102. while (colCnt)
  103. {
  104. q15_t inV = *pA++;
  105. q15_t inM = *pB++;
  106. q15_t inM2 = *pB2++;
  107. sum += inV * inM;
  108. sum2 += inV * inM2;
  109. colCnt--;
  110. } /* while over colCnt */
  111. *pO++ = (q15_t) (__SSAT((sum >> out_shift), 16));
  112. *pO++ = (q15_t) (__SSAT((sum2>> out_shift), 16));
  113. /* adjust the pointers and counters */
  114. pB = pB + dim_vec;
  115. rowCnt --;
  116. }
  117. rowCnt = num_of_rows & 0x1;
  118. while (rowCnt) {
  119. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  120. uint16_t colCnt = dim_vec >> 2;
  121. pA = pV;
  122. while (colCnt) {
  123. q31_t inV1, inM1;
  124. inV1 = arm_nn_read_q15x2_ia(&pA);
  125. inM1 = arm_nn_read_q15x2_ia(&pB);
  126. sum = __SMLAD(inV1, inM1, sum);
  127. inV1 = arm_nn_read_q15x2_ia(&pA);
  128. inM1 = arm_nn_read_q15x2_ia(&pB);
  129. sum = __SMLAD(inV1, inM1, sum);
  130. colCnt--;
  131. }
  132. /* left-over of the vector */
  133. colCnt = dim_vec & 0x3;
  134. while(colCnt) {
  135. q15_t inV = *pA++;
  136. q15_t inM = *pB++;
  137. sum += inV * inM;
  138. colCnt--;
  139. }
  140. *pO++ = (q15_t) (__SSAT((sum >> out_shift), 16));
  141. rowCnt --;
  142. }
  143. #else
  144. int i, j;
  145. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  146. for (i = 0; i < num_of_rows; i++)
  147. {
  148. int ip_out = ((q31_t)(bias[i]) << bias_shift) + NN_ROUND(out_shift);
  149. for (j = 0; j < dim_vec; j++)
  150. {
  151. ip_out += pV[j] * pM[i * dim_vec + j];
  152. }
  153. pOut[i] = (q15_t) __SSAT((ip_out >> out_shift), 16);
  154. }
  155. #endif /* ARM_MATH_DSP */
  156. /* Return to application */
  157. return (ARM_MATH_SUCCESS);
  158. }
  159. /**
  160. * @} end of FC group
  161. */