arm_svm_polynomial_predict_f32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_svm_polynomial_predict_f32.c
  4. * Description: SVM Polynomial Classifier
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/svm_functions.h"
  29. #include <limits.h>
  30. #include <math.h>
  31. #if defined(ARM_MATH_NEON) && !defined(ARM_MATH_AUTOVECTORIZE)
  32. #include "arm_vec_math.h"
  33. #endif
  34. /**
  35. * @addtogroup polysvm
  36. * @{
  37. */
  38. /**
  39. * @brief SVM polynomial prediction
  40. * @param[in] S Pointer to an instance of the polynomial SVM structure.
  41. * @param[in] in Pointer to input vector
  42. * @param[out] pResult Decision value
  43. * @return none.
  44. *
  45. */
  46. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  47. #include "arm_helium_utils.h"
  48. #include "arm_vec_math.h"
  49. void arm_svm_polynomial_predict_f32(
  50. const arm_svm_polynomial_instance_f32 *S,
  51. const float32_t * in,
  52. int32_t * pResult)
  53. {
  54. /* inlined Matrix x Vector function interleaved with dot prod */
  55. uint32_t numRows = S->nbOfSupportVectors;
  56. uint32_t numCols = S->vectorDimension;
  57. const float32_t *pSupport = S->supportVectors;
  58. const float32_t *pSrcA = pSupport;
  59. const float32_t *pInA0;
  60. const float32_t *pInA1;
  61. uint32_t row;
  62. uint32_t blkCnt; /* loop counters */
  63. const float32_t *pDualCoef = S->dualCoefficients;
  64. float32_t sum = S->intercept;
  65. f32x4_t vSum = vdupq_n_f32(0.0f);
  66. row = numRows;
  67. /*
  68. * compute 4 rows in parrallel
  69. */
  70. while (row >= 4) {
  71. const float32_t *pInA2, *pInA3;
  72. float32_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec, *pInVec;
  73. f32x4_t vecIn, acc0, acc1, acc2, acc3;
  74. float32_t const *pSrcVecPtr = in;
  75. /*
  76. * Initialize the pointers to 4 consecutive MatrixA rows
  77. */
  78. pInA0 = pSrcA;
  79. pInA1 = pInA0 + numCols;
  80. pInA2 = pInA1 + numCols;
  81. pInA3 = pInA2 + numCols;
  82. /*
  83. * Initialize the vector pointer
  84. */
  85. pInVec = pSrcVecPtr;
  86. /*
  87. * reset accumulators
  88. */
  89. acc0 = vdupq_n_f32(0.0f);
  90. acc1 = vdupq_n_f32(0.0f);
  91. acc2 = vdupq_n_f32(0.0f);
  92. acc3 = vdupq_n_f32(0.0f);
  93. pSrcA0Vec = pInA0;
  94. pSrcA1Vec = pInA1;
  95. pSrcA2Vec = pInA2;
  96. pSrcA3Vec = pInA3;
  97. blkCnt = numCols >> 2;
  98. while (blkCnt > 0U) {
  99. f32x4_t vecA;
  100. vecIn = vld1q(pInVec);
  101. pInVec += 4;
  102. vecA = vld1q(pSrcA0Vec);
  103. pSrcA0Vec += 4;
  104. acc0 = vfmaq(acc0, vecIn, vecA);
  105. vecA = vld1q(pSrcA1Vec);
  106. pSrcA1Vec += 4;
  107. acc1 = vfmaq(acc1, vecIn, vecA);
  108. vecA = vld1q(pSrcA2Vec);
  109. pSrcA2Vec += 4;
  110. acc2 = vfmaq(acc2, vecIn, vecA);
  111. vecA = vld1q(pSrcA3Vec);
  112. pSrcA3Vec += 4;
  113. acc3 = vfmaq(acc3, vecIn, vecA);
  114. blkCnt--;
  115. }
  116. /*
  117. * tail
  118. * (will be merged thru tail predication)
  119. */
  120. blkCnt = numCols & 3;
  121. if (blkCnt > 0U) {
  122. mve_pred16_t p0 = vctp32q(blkCnt);
  123. f32x4_t vecA;
  124. vecIn = vldrwq_z_f32(pInVec, p0);
  125. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  126. acc0 = vfmaq(acc0, vecIn, vecA);
  127. vecA = vldrwq_z_f32(pSrcA1Vec, p0);
  128. acc1 = vfmaq(acc1, vecIn, vecA);
  129. vecA = vldrwq_z_f32(pSrcA2Vec, p0);
  130. acc2 = vfmaq(acc2, vecIn, vecA);
  131. vecA = vldrwq_z_f32(pSrcA3Vec, p0);
  132. acc3 = vfmaq(acc3, vecIn, vecA);
  133. }
  134. /*
  135. * Sum the partial parts
  136. */
  137. f32x4_t vtmp = vuninitializedq_f32();
  138. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  139. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc1), vtmp, 1);
  140. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc2), vtmp, 2);
  141. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc3), vtmp, 3);
  142. vSum = vfmaq_f32(vSum, vld1q(pDualCoef),
  143. arm_vec_exponent_f32
  144. (vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0), S->degree));
  145. pDualCoef += 4;
  146. pSrcA += numCols * 4;
  147. /*
  148. * Decrement the row loop counter
  149. */
  150. row -= 4;
  151. }
  152. /*
  153. * compute 2 rows in parrallel
  154. */
  155. if (row >= 2) {
  156. float32_t const *pSrcA0Vec, *pSrcA1Vec, *pInVec;
  157. f32x4_t vecIn, acc0, acc1;
  158. float32_t const *pSrcVecPtr = in;
  159. /*
  160. * Initialize the pointers to 2 consecutive MatrixA rows
  161. */
  162. pInA0 = pSrcA;
  163. pInA1 = pInA0 + numCols;
  164. /*
  165. * Initialize the vector pointer
  166. */
  167. pInVec = pSrcVecPtr;
  168. /*
  169. * reset accumulators
  170. */
  171. acc0 = vdupq_n_f32(0.0f);
  172. acc1 = vdupq_n_f32(0.0f);
  173. pSrcA0Vec = pInA0;
  174. pSrcA1Vec = pInA1;
  175. blkCnt = numCols >> 2;
  176. while (blkCnt > 0U) {
  177. f32x4_t vecA;
  178. vecIn = vld1q(pInVec);
  179. pInVec += 4;
  180. vecA = vld1q(pSrcA0Vec);
  181. pSrcA0Vec += 4;
  182. acc0 = vfmaq(acc0, vecIn, vecA);
  183. vecA = vld1q(pSrcA1Vec);
  184. pSrcA1Vec += 4;
  185. acc1 = vfmaq(acc1, vecIn, vecA);
  186. blkCnt--;
  187. }
  188. /*
  189. * tail
  190. * (will be merged thru tail predication)
  191. */
  192. blkCnt = numCols & 3;
  193. if (blkCnt > 0U) {
  194. mve_pred16_t p0 = vctp32q(blkCnt);
  195. f32x4_t vecA;
  196. vecIn = vldrwq_z_f32(pInVec, p0);
  197. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  198. acc0 = vfmaq(acc0, vecIn, vecA);
  199. vecA = vldrwq_z_f32(pSrcA1Vec, p0);
  200. acc1 = vfmaq(acc1, vecIn, vecA);
  201. }
  202. /*
  203. * Sum the partial parts
  204. */
  205. f32x4_t vtmp = vuninitializedq_f32();
  206. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  207. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc1), vtmp, 1);
  208. vSum = vfmaq_m_f32(vSum, vld1q(pDualCoef),
  209. arm_vec_exponent_f32
  210. (vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0), S->degree),
  211. vctp32q(2));
  212. pDualCoef += 2;
  213. pSrcA += numCols * 2;
  214. row -= 2;
  215. }
  216. if (row >= 1) {
  217. f32x4_t vecIn, acc0;
  218. float32_t const *pSrcA0Vec, *pInVec;
  219. float32_t const *pSrcVecPtr = in;
  220. /*
  221. * Initialize the pointers to last MatrixA row
  222. */
  223. pInA0 = pSrcA;
  224. /*
  225. * Initialize the vector pointer
  226. */
  227. pInVec = pSrcVecPtr;
  228. /*
  229. * reset accumulators
  230. */
  231. acc0 = vdupq_n_f32(0.0f);
  232. pSrcA0Vec = pInA0;
  233. blkCnt = numCols >> 2;
  234. while (blkCnt > 0U) {
  235. f32x4_t vecA;
  236. vecIn = vld1q(pInVec);
  237. pInVec += 4;
  238. vecA = vld1q(pSrcA0Vec);
  239. pSrcA0Vec += 4;
  240. acc0 = vfmaq(acc0, vecIn, vecA);
  241. blkCnt--;
  242. }
  243. /*
  244. * tail
  245. * (will be merged thru tail predication)
  246. */
  247. blkCnt = numCols & 3;
  248. if (blkCnt > 0U) {
  249. mve_pred16_t p0 = vctp32q(blkCnt);
  250. f32x4_t vecA;
  251. vecIn = vldrwq_z_f32(pInVec, p0);
  252. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  253. acc0 = vfmaq(acc0, vecIn, vecA);
  254. }
  255. /*
  256. * Sum the partial parts
  257. */
  258. f32x4_t vtmp = vuninitializedq_f32();
  259. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  260. vSum = vfmaq_m_f32(vSum, vld1q(pDualCoef),
  261. arm_vec_exponent_f32
  262. (vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0), S->degree),
  263. vctp32q(1));
  264. }
  265. sum += vecAddAcrossF32Mve(vSum);
  266. *pResult = S->classes[STEP(sum)];
  267. }
  268. #else
  269. #if defined(ARM_MATH_NEON)
  270. void arm_svm_polynomial_predict_f32(
  271. const arm_svm_polynomial_instance_f32 *S,
  272. const float32_t * in,
  273. int32_t * pResult)
  274. {
  275. float32_t sum = S->intercept;
  276. float32_t dot;
  277. float32x4_t dotV;
  278. float32x4_t accuma,accumb,accumc,accumd,accum;
  279. float32x2_t accum2;
  280. float32x4_t vec1;
  281. float32x4_t coef0 = vdupq_n_f32(S->coef0);
  282. float32x4_t vec2,vec2a,vec2b,vec2c,vec2d;
  283. uint32_t blkCnt;
  284. uint32_t vectorBlkCnt;
  285. const float32_t *pIn = in;
  286. const float32_t *pSupport = S->supportVectors;
  287. const float32_t *pSupporta = S->supportVectors;
  288. const float32_t *pSupportb;
  289. const float32_t *pSupportc;
  290. const float32_t *pSupportd;
  291. pSupportb = pSupporta + S->vectorDimension;
  292. pSupportc = pSupportb + S->vectorDimension;
  293. pSupportd = pSupportc + S->vectorDimension;
  294. const float32_t *pDualCoefs = S->dualCoefficients;
  295. vectorBlkCnt = S->nbOfSupportVectors >> 2;
  296. while (vectorBlkCnt > 0U)
  297. {
  298. accuma = vdupq_n_f32(0);
  299. accumb = vdupq_n_f32(0);
  300. accumc = vdupq_n_f32(0);
  301. accumd = vdupq_n_f32(0);
  302. pIn = in;
  303. blkCnt = S->vectorDimension >> 2;
  304. while (blkCnt > 0U)
  305. {
  306. vec1 = vld1q_f32(pIn);
  307. vec2a = vld1q_f32(pSupporta);
  308. vec2b = vld1q_f32(pSupportb);
  309. vec2c = vld1q_f32(pSupportc);
  310. vec2d = vld1q_f32(pSupportd);
  311. pIn += 4;
  312. pSupporta += 4;
  313. pSupportb += 4;
  314. pSupportc += 4;
  315. pSupportd += 4;
  316. accuma = vmlaq_f32(accuma, vec1,vec2a);
  317. accumb = vmlaq_f32(accumb, vec1,vec2b);
  318. accumc = vmlaq_f32(accumc, vec1,vec2c);
  319. accumd = vmlaq_f32(accumd, vec1,vec2d);
  320. blkCnt -- ;
  321. }
  322. accum2 = vpadd_f32(vget_low_f32(accuma),vget_high_f32(accuma));
  323. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,0);
  324. accum2 = vpadd_f32(vget_low_f32(accumb),vget_high_f32(accumb));
  325. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,1);
  326. accum2 = vpadd_f32(vget_low_f32(accumc),vget_high_f32(accumc));
  327. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,2);
  328. accum2 = vpadd_f32(vget_low_f32(accumd),vget_high_f32(accumd));
  329. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,3);
  330. blkCnt = S->vectorDimension & 3;
  331. while (blkCnt > 0U)
  332. {
  333. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,0) + *pIn * *pSupporta++, dotV,0);
  334. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,1) + *pIn * *pSupportb++, dotV,1);
  335. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,2) + *pIn * *pSupportc++, dotV,2);
  336. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,3) + *pIn * *pSupportd++, dotV,3);
  337. pIn++;
  338. blkCnt -- ;
  339. }
  340. vec1 = vld1q_f32(pDualCoefs);
  341. pDualCoefs += 4;
  342. // To vectorize later
  343. dotV = vmulq_n_f32(dotV, S->gamma);
  344. dotV = vaddq_f32(dotV, coef0);
  345. dotV = arm_vec_exponent_f32(dotV,S->degree);
  346. accum = vmulq_f32(vec1,dotV);
  347. accum2 = vpadd_f32(vget_low_f32(accum),vget_high_f32(accum));
  348. sum += vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1);
  349. pSupporta += 3*S->vectorDimension;
  350. pSupportb += 3*S->vectorDimension;
  351. pSupportc += 3*S->vectorDimension;
  352. pSupportd += 3*S->vectorDimension;
  353. vectorBlkCnt -- ;
  354. }
  355. pSupport = pSupporta;
  356. vectorBlkCnt = S->nbOfSupportVectors & 3;
  357. while (vectorBlkCnt > 0U)
  358. {
  359. accum = vdupq_n_f32(0);
  360. dot = 0.0f;
  361. pIn = in;
  362. blkCnt = S->vectorDimension >> 2;
  363. while (blkCnt > 0U)
  364. {
  365. vec1 = vld1q_f32(pIn);
  366. vec2 = vld1q_f32(pSupport);
  367. pIn += 4;
  368. pSupport += 4;
  369. accum = vmlaq_f32(accum, vec1,vec2);
  370. blkCnt -- ;
  371. }
  372. accum2 = vpadd_f32(vget_low_f32(accum),vget_high_f32(accum));
  373. dot = vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1);
  374. blkCnt = S->vectorDimension & 3;
  375. while (blkCnt > 0U)
  376. {
  377. dot = dot + *pIn++ * *pSupport++;
  378. blkCnt -- ;
  379. }
  380. sum += *pDualCoefs++ * arm_exponent_f32(S->gamma * dot + S->coef0, S->degree);
  381. vectorBlkCnt -- ;
  382. }
  383. *pResult=S->classes[STEP(sum)];
  384. }
  385. #else
  386. void arm_svm_polynomial_predict_f32(
  387. const arm_svm_polynomial_instance_f32 *S,
  388. const float32_t * in,
  389. int32_t * pResult)
  390. {
  391. float32_t sum=S->intercept;
  392. float32_t dot=0;
  393. uint32_t i,j;
  394. const float32_t *pSupport = S->supportVectors;
  395. for(i=0; i < S->nbOfSupportVectors; i++)
  396. {
  397. dot=0;
  398. for(j=0; j < S->vectorDimension; j++)
  399. {
  400. dot = dot + in[j]* *pSupport++;
  401. }
  402. sum += S->dualCoefficients[i] * arm_exponent_f32(S->gamma * dot + S->coef0, S->degree);
  403. }
  404. *pResult=S->classes[STEP(sum)];
  405. }
  406. #endif
  407. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  408. /**
  409. * @} end of polysvm group
  410. */