ref_conv.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "ref_conv.h"
  2. void ref_conv_f32(float32_t* pSrcA, uint32_t srcALen, float32_t* pSrcB,
  3. uint32_t srcBLen, float32_t* pDst)
  4. {
  5. float32_t sum; /* Accumulator */
  6. uint32_t i, j; /* loop counters */
  7. /* Loop to calculate convolution for output length number of times */
  8. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  9. /* Initialize sum with zero to carry out MAC operations */
  10. sum = 0.0f;
  11. /* Loop to perform MAC operations according to convolution equation */
  12. for (j = 0; j <= i; j++) {
  13. /* Check the array limitations */
  14. if ((i - j < srcBLen) && (j < srcALen)) {
  15. /* z[i] += x[i-j] * y[j] */
  16. sum += pSrcB[i - j] * pSrcA[j];
  17. }
  18. }
  19. /* Store the output in the destination buffer */
  20. pDst[i] = sum;
  21. }
  22. }
  23. riscv_status ref_conv_partial_f32(float32_t* pSrcA, uint32_t srcALen,
  24. float32_t* pSrcB, uint32_t srcBLen,
  25. float32_t* pDst, uint32_t firstIndex,
  26. uint32_t numPoints)
  27. {
  28. ref_conv_f32(pSrcA, srcALen, pSrcB, srcBLen, pDst);
  29. return RISCV_MATH_SUCCESS;
  30. }
  31. void ref_conv_q31(q31_t* pSrcA, uint32_t srcALen, q31_t* pSrcB,
  32. uint32_t srcBLen, q31_t* pDst)
  33. {
  34. q63_t sum; /* Accumulator */
  35. uint32_t i, j; /* loop counter */
  36. /* Loop to calculate output of convolution for output length number of times
  37. */
  38. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  39. /* Initialize sum with zero to carry on MAC operations */
  40. sum = 0;
  41. /* Loop to perform MAC operations according to convolution equation */
  42. for (j = 0; j <= i; j++) {
  43. /* Check the array limitations */
  44. if ((i - j < srcBLen) && (j < srcALen)) {
  45. /* z[i] += x[i-j] * y[j] */
  46. sum += (q63_t)pSrcA[j] * (pSrcB[i - j]);
  47. }
  48. }
  49. /* Store the output in the destination buffer */
  50. pDst[i] = (q31_t)(sum >> 31U);
  51. }
  52. }
  53. void ref_conv_fast_q31(q31_t* pSrcA, uint32_t srcALen, q31_t* pSrcB,
  54. uint32_t srcBLen, q31_t* pDst)
  55. {
  56. q31_t sum; /* Accumulator */
  57. uint32_t i, j; /* loop counter */
  58. /* Loop to calculate output of convolution for output length number of times
  59. */
  60. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  61. /* Initialize sum with zero to carry on MAC operations */
  62. sum = 0;
  63. /* Loop to perform MAC operations according to convolution equation */
  64. for (j = 0; j <= i; j++) {
  65. /* Check the array limitations */
  66. if ((i - j < srcBLen) && (j < srcALen)) {
  67. /* z[i] += x[i-j] * y[j] */
  68. sum = (q31_t)(
  69. (((q63_t)sum << 32) + ((q63_t)pSrcA[j] * pSrcB[i - j])) >>
  70. 32);
  71. }
  72. }
  73. /* Store the output in the destination buffer */
  74. pDst[i] = (q31_t)(sum << 1U);
  75. }
  76. }
  77. riscv_status ref_conv_partial_q31(q31_t* pSrcA, uint32_t srcALen, q31_t* pSrcB,
  78. uint32_t srcBLen, q31_t* pDst,
  79. uint32_t firstIndex, uint32_t numPoints)
  80. {
  81. ref_conv_q31(pSrcA, srcALen, pSrcB, srcBLen, pDst);
  82. return RISCV_MATH_SUCCESS;
  83. }
  84. riscv_status ref_conv_partial_fast_q31(q31_t* pSrcA, uint32_t srcALen,
  85. q31_t* pSrcB, uint32_t srcBLen,
  86. q31_t* pDst, uint32_t firstIndex,
  87. uint32_t numPoints)
  88. {
  89. ref_conv_fast_q31(pSrcA, srcALen, pSrcB, srcBLen, pDst);
  90. return RISCV_MATH_SUCCESS;
  91. }
  92. void ref_conv_q15(q15_t* pSrcA, uint32_t srcALen, q15_t* pSrcB,
  93. uint32_t srcBLen, q15_t* pDst)
  94. {
  95. q63_t sum; /* Accumulator */
  96. uint32_t i, j; /* loop counter */
  97. /* Loop to calculate output of convolution for output length number of times
  98. */
  99. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  100. /* Initialize sum with zero to carry on MAC operations */
  101. sum = 0;
  102. /* Loop to perform MAC operations according to convolution equation */
  103. for (j = 0; j <= i; j++) {
  104. /* Check the array limitations */
  105. if ((i - j < srcBLen) && (j < srcALen)) {
  106. /* z[i] += x[i-j] * y[j] */
  107. sum += (q31_t)pSrcA[j] * pSrcB[i - j];
  108. }
  109. }
  110. /* Store the output in the destination buffer */
  111. pDst[i] = ref_sat_q15(sum >> 15U);
  112. }
  113. }
  114. riscv_status ref_conv_partial_fast_opt_q15(q15_t* pSrcA, uint32_t srcALen,
  115. q15_t* pSrcB, uint32_t srcBLen,
  116. q15_t* pDst, uint32_t firstIndex,
  117. uint32_t numPoints, q15_t* pScratch1,
  118. q15_t* pScratch2)
  119. {
  120. q31_t sum; /* Accumulator */
  121. uint32_t i, j; /* loop counter */
  122. /* Loop to calculate output of convolution for output length number of times
  123. */
  124. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  125. /* Initialize sum with zero to carry on MAC operations */
  126. sum = 0;
  127. /* Loop to perform MAC operations according to convolution equation */
  128. for (j = 0; j <= i; j++) {
  129. /* Check the array limitations */
  130. if ((i - j < srcBLen) && (j < srcALen)) {
  131. /* z[i] += x[i-j] * y[j] */
  132. sum += (q31_t)pSrcA[j] * pSrcB[i - j];
  133. }
  134. }
  135. /* Store the output in the destination buffer */
  136. pDst[i] = ref_sat_q15(sum >> 15U);
  137. }
  138. return RISCV_MATH_SUCCESS;
  139. }
  140. void ref_conv_fast_q15(q15_t* pSrcA, uint32_t srcALen, q15_t* pSrcB,
  141. uint32_t srcBLen, q15_t* pDst)
  142. {
  143. q31_t sum; /* Accumulator */
  144. uint32_t i, j; /* loop counter */
  145. /* Loop to calculate output of convolution for output length number of times
  146. */
  147. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  148. /* Initialize sum with zero to carry on MAC operations */
  149. sum = 0;
  150. /* Loop to perform MAC operations according to convolution equation */
  151. for (j = 0; j <= i; j++) {
  152. /* Check the array limitations */
  153. if ((i - j < srcBLen) && (j < srcALen)) {
  154. /* z[i] += x[i-j] * y[j] */
  155. sum += (q31_t)pSrcA[j] * pSrcB[i - j];
  156. }
  157. }
  158. /* Store the output in the destination buffer */
  159. pDst[i] = sum >> 15U;
  160. }
  161. }
  162. void ref_conv_fast_opt_q15(q15_t* pSrcA, uint32_t srcALen, q15_t* pSrcB,
  163. uint32_t srcBLen, q15_t* pDst, q15_t* pScratch1,
  164. q15_t* pScratch2)
  165. {
  166. q31_t sum; /* Accumulator */
  167. uint32_t i, j; /* loop counter */
  168. /* Loop to calculate output of convolution for output length number of times
  169. */
  170. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  171. /* Initialize sum with zero to carry on MAC operations */
  172. sum = 0;
  173. /* Loop to perform MAC operations according to convolution equation */
  174. for (j = 0; j <= i; j++) {
  175. /* Check the array limitations */
  176. if ((i - j < srcBLen) && (j < srcALen)) {
  177. /* z[i] += x[i-j] * y[j] */
  178. sum += (q31_t)pSrcA[j] * pSrcB[i - j];
  179. }
  180. }
  181. /* Store the output in the destination buffer */
  182. pDst[i] = ref_sat_q15(sum >> 15U);
  183. }
  184. }
  185. riscv_status ref_conv_partial_q15(q15_t* pSrcA, uint32_t srcALen, q15_t* pSrcB,
  186. uint32_t srcBLen, q15_t* pDst,
  187. uint32_t firstIndex, uint32_t numPoints)
  188. {
  189. ref_conv_q15(pSrcA, srcALen, pSrcB, srcBLen, pDst);
  190. return RISCV_MATH_SUCCESS;
  191. }
  192. riscv_status ref_conv_partial_fast_q15(q15_t* pSrcA, uint32_t srcALen,
  193. q15_t* pSrcB, uint32_t srcBLen,
  194. q15_t* pDst, uint32_t firstIndex,
  195. uint32_t numPoints)
  196. {
  197. ref_conv_fast_q15(pSrcA, srcALen, pSrcB, srcBLen, pDst);
  198. return RISCV_MATH_SUCCESS;
  199. }
  200. void ref_conv_q7(q7_t* pSrcA, uint32_t srcALen, q7_t* pSrcB, uint32_t srcBLen,
  201. q7_t* pDst)
  202. {
  203. q31_t sum; /* Accumulator */
  204. uint32_t i, j; /* loop counter */
  205. /* Loop to calculate output of convolution for output length number of times
  206. */
  207. for (i = 0; i < srcALen + srcBLen - 1; i++) {
  208. /* Initialize sum with zero to carry on MAC operations */
  209. sum = 0;
  210. /* Loop to perform MAC operations according to convolution equation */
  211. for (j = 0; j <= i; j++) {
  212. /* Check the array limitations */
  213. if ((i - j < srcBLen) && (j < srcALen)) {
  214. /* z[i] += x[i-j] * y[j] */
  215. sum += (q15_t)pSrcA[j] * pSrcB[i - j];
  216. }
  217. }
  218. /* Store the output in the destination buffer */
  219. pDst[i] = (q7_t)ref_sat_q7(sum >> 7);
  220. }
  221. }
  222. riscv_status ref_conv_partial_q7(q7_t* pSrcA, uint32_t srcALen, q7_t* pSrcB,
  223. uint32_t srcBLen, q7_t* pDst,
  224. uint32_t firstIndex, uint32_t numPoints)
  225. {
  226. ref_conv_q7(pSrcA, srcALen, pSrcB, srcBLen, pDst);
  227. return RISCV_MATH_SUCCESS;
  228. }