arm_fully_connected_q15.c 5.3 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. #if defined (ARM_MATH_DSP)
  71. /* Run the following code for Cortex-M4 and Cortex-M7 */
  72. const q15_t *pB = pM;
  73. const q15_t *pB2 = pB + dim_vec;
  74. q15_t *pO = pOut;
  75. const q15_t *pA;
  76. const q15_t *pBias = bias;
  77. uint16_t rowCnt = num_of_rows >> 1;
  78. /* this loop loops over different output */
  79. while (rowCnt) {
  80. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  81. q31_t sum2 = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  82. uint16_t colCnt = dim_vec >> 2;
  83. pA = pV;
  84. pB2 = pB + dim_vec;
  85. while (colCnt)
  86. {
  87. q31_t inV1, inM1, inM2;
  88. inV1 = *__SIMD32(pA)++;
  89. inM1 = *__SIMD32(pB)++;
  90. sum = __SMLAD(inV1, inM1, sum);
  91. inM2 = *__SIMD32(pB2)++;
  92. sum2 = __SMLAD(inV1, inM2, sum2);
  93. inV1 = *__SIMD32(pA)++;
  94. inM1 = *__SIMD32(pB)++;
  95. sum = __SMLAD(inV1, inM1, sum);
  96. inM2 = *__SIMD32(pB2)++;
  97. sum2 = __SMLAD(inV1, inM2, sum2);
  98. colCnt--;
  99. }
  100. colCnt = dim_vec & 0x3;
  101. while (colCnt)
  102. {
  103. q15_t inV = *pA++;
  104. q15_t inM = *pB++;
  105. q15_t inM2 = *pB2++;
  106. sum += inV * inM;
  107. sum2 += inV * inM2;
  108. colCnt--;
  109. } /* while over colCnt */
  110. *pO++ = (q15_t) (__SSAT((sum >> out_shift), 16));
  111. *pO++ = (q15_t) (__SSAT((sum2>> out_shift), 16));
  112. /* adjust the pointers and counters */
  113. pB = pB + dim_vec;
  114. rowCnt --;
  115. }
  116. rowCnt = num_of_rows & 0x1;
  117. while (rowCnt) {
  118. q31_t sum = ((q31_t)(*pBias++) << bias_shift) + NN_ROUND(out_shift);
  119. uint16_t colCnt = dim_vec >> 2;
  120. pA = pV;
  121. while (colCnt) {
  122. q31_t inV1, inM1;
  123. inV1 = *__SIMD32(pA)++;
  124. inM1 = *__SIMD32(pB)++;
  125. sum = __SMLAD(inV1, inM1, sum);
  126. inV1 = *__SIMD32(pA)++;
  127. inM1 = *__SIMD32(pB)++;
  128. sum = __SMLAD(inV1, inM1, sum);
  129. colCnt--;
  130. }
  131. /* left-over of the vector */
  132. colCnt = dim_vec & 0x3;
  133. while(colCnt) {
  134. q15_t inV = *pA++;
  135. q15_t inM = *pB++;
  136. sum += inV * inM;
  137. colCnt--;
  138. }
  139. *pO++ = (q15_t) (__SSAT((sum >> out_shift), 16));
  140. rowCnt --;
  141. }
  142. #else
  143. int i, j;
  144. /* Run the following code as reference implementation for Cortex-M0 and Cortex-M3 */
  145. for (i = 0; i < num_of_rows; i++)
  146. {
  147. int ip_out = ((q31_t)(bias[i]) << bias_shift) + NN_ROUND(out_shift);
  148. for (j = 0; j < dim_vec; j++)
  149. {
  150. ip_out += pV[j] * pM[i * dim_vec + j];
  151. }
  152. pOut[i] = (q15_t) __SSAT((ip_out >> out_shift), 16);
  153. }
  154. #endif /* ARM_MATH_DSP */
  155. /* Return to application */
  156. return (ARM_MATH_SUCCESS);
  157. }
  158. /**
  159. * @} end of FC group
  160. */