arm_svm_polynomial_predict_f32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. *
  44. */
  45. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  46. #include "arm_helium_utils.h"
  47. #include "arm_vec_math.h"
  48. void arm_svm_polynomial_predict_f32(
  49. const arm_svm_polynomial_instance_f32 *S,
  50. const float32_t * in,
  51. int32_t * pResult)
  52. {
  53. /* inlined Matrix x Vector function interleaved with dot prod */
  54. uint32_t numRows = S->nbOfSupportVectors;
  55. uint32_t numCols = S->vectorDimension;
  56. const float32_t *pSupport = S->supportVectors;
  57. const float32_t *pSrcA = pSupport;
  58. const float32_t *pInA0;
  59. const float32_t *pInA1;
  60. uint32_t row;
  61. uint32_t blkCnt; /* loop counters */
  62. const float32_t *pDualCoef = S->dualCoefficients;
  63. float32_t sum = S->intercept;
  64. f32x4_t vSum = vdupq_n_f32(0.0f);
  65. row = numRows;
  66. /*
  67. * compute 4 rows in parrallel
  68. */
  69. while (row >= 4) {
  70. const float32_t *pInA2, *pInA3;
  71. float32_t const *pSrcA0Vec, *pSrcA1Vec, *pSrcA2Vec, *pSrcA3Vec, *pInVec;
  72. f32x4_t vecIn, acc0, acc1, acc2, acc3;
  73. float32_t const *pSrcVecPtr = in;
  74. /*
  75. * Initialize the pointers to 4 consecutive MatrixA rows
  76. */
  77. pInA0 = pSrcA;
  78. pInA1 = pInA0 + numCols;
  79. pInA2 = pInA1 + numCols;
  80. pInA3 = pInA2 + numCols;
  81. /*
  82. * Initialize the vector pointer
  83. */
  84. pInVec = pSrcVecPtr;
  85. /*
  86. * reset accumulators
  87. */
  88. acc0 = vdupq_n_f32(0.0f);
  89. acc1 = vdupq_n_f32(0.0f);
  90. acc2 = vdupq_n_f32(0.0f);
  91. acc3 = vdupq_n_f32(0.0f);
  92. pSrcA0Vec = pInA0;
  93. pSrcA1Vec = pInA1;
  94. pSrcA2Vec = pInA2;
  95. pSrcA3Vec = pInA3;
  96. blkCnt = numCols >> 2;
  97. while (blkCnt > 0U) {
  98. f32x4_t vecA;
  99. vecIn = vld1q(pInVec);
  100. pInVec += 4;
  101. vecA = vld1q(pSrcA0Vec);
  102. pSrcA0Vec += 4;
  103. acc0 = vfmaq(acc0, vecIn, vecA);
  104. vecA = vld1q(pSrcA1Vec);
  105. pSrcA1Vec += 4;
  106. acc1 = vfmaq(acc1, vecIn, vecA);
  107. vecA = vld1q(pSrcA2Vec);
  108. pSrcA2Vec += 4;
  109. acc2 = vfmaq(acc2, vecIn, vecA);
  110. vecA = vld1q(pSrcA3Vec);
  111. pSrcA3Vec += 4;
  112. acc3 = vfmaq(acc3, vecIn, vecA);
  113. blkCnt--;
  114. }
  115. /*
  116. * tail
  117. * (will be merged thru tail predication)
  118. */
  119. blkCnt = numCols & 3;
  120. if (blkCnt > 0U) {
  121. mve_pred16_t p0 = vctp32q(blkCnt);
  122. f32x4_t vecA;
  123. vecIn = vldrwq_z_f32(pInVec, p0);
  124. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  125. acc0 = vfmaq(acc0, vecIn, vecA);
  126. vecA = vldrwq_z_f32(pSrcA1Vec, p0);
  127. acc1 = vfmaq(acc1, vecIn, vecA);
  128. vecA = vldrwq_z_f32(pSrcA2Vec, p0);
  129. acc2 = vfmaq(acc2, vecIn, vecA);
  130. vecA = vldrwq_z_f32(pSrcA3Vec, p0);
  131. acc3 = vfmaq(acc3, vecIn, vecA);
  132. }
  133. /*
  134. * Sum the partial parts
  135. */
  136. f32x4_t vtmp = vuninitializedq_f32();
  137. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  138. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc1), vtmp, 1);
  139. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc2), vtmp, 2);
  140. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc3), vtmp, 3);
  141. vSum = vfmaq_f32(vSum, vld1q(pDualCoef),
  142. arm_vec_exponent_f32
  143. (vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0), S->degree));
  144. pDualCoef += 4;
  145. pSrcA += numCols * 4;
  146. /*
  147. * Decrement the row loop counter
  148. */
  149. row -= 4;
  150. }
  151. /*
  152. * compute 2 rows in parrallel
  153. */
  154. if (row >= 2) {
  155. float32_t const *pSrcA0Vec, *pSrcA1Vec, *pInVec;
  156. f32x4_t vecIn, acc0, acc1;
  157. float32_t const *pSrcVecPtr = in;
  158. /*
  159. * Initialize the pointers to 2 consecutive MatrixA rows
  160. */
  161. pInA0 = pSrcA;
  162. pInA1 = pInA0 + numCols;
  163. /*
  164. * Initialize the vector pointer
  165. */
  166. pInVec = pSrcVecPtr;
  167. /*
  168. * reset accumulators
  169. */
  170. acc0 = vdupq_n_f32(0.0f);
  171. acc1 = vdupq_n_f32(0.0f);
  172. pSrcA0Vec = pInA0;
  173. pSrcA1Vec = pInA1;
  174. blkCnt = numCols >> 2;
  175. while (blkCnt > 0U) {
  176. f32x4_t vecA;
  177. vecIn = vld1q(pInVec);
  178. pInVec += 4;
  179. vecA = vld1q(pSrcA0Vec);
  180. pSrcA0Vec += 4;
  181. acc0 = vfmaq(acc0, vecIn, vecA);
  182. vecA = vld1q(pSrcA1Vec);
  183. pSrcA1Vec += 4;
  184. acc1 = vfmaq(acc1, vecIn, vecA);
  185. blkCnt--;
  186. }
  187. /*
  188. * tail
  189. * (will be merged thru tail predication)
  190. */
  191. blkCnt = numCols & 3;
  192. if (blkCnt > 0U) {
  193. mve_pred16_t p0 = vctp32q(blkCnt);
  194. f32x4_t vecA;
  195. vecIn = vldrwq_z_f32(pInVec, p0);
  196. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  197. acc0 = vfmaq(acc0, vecIn, vecA);
  198. vecA = vldrwq_z_f32(pSrcA1Vec, p0);
  199. acc1 = vfmaq(acc1, vecIn, vecA);
  200. }
  201. /*
  202. * Sum the partial parts
  203. */
  204. f32x4_t vtmp = vuninitializedq_f32();
  205. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  206. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc1), vtmp, 1);
  207. vSum = vfmaq_m_f32(vSum, vld1q(pDualCoef),
  208. arm_vec_exponent_f32
  209. (vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0), S->degree),
  210. vctp32q(2));
  211. pDualCoef += 2;
  212. pSrcA += numCols * 2;
  213. row -= 2;
  214. }
  215. if (row >= 1) {
  216. f32x4_t vecIn, acc0;
  217. float32_t const *pSrcA0Vec, *pInVec;
  218. float32_t const *pSrcVecPtr = in;
  219. /*
  220. * Initialize the pointers to last MatrixA row
  221. */
  222. pInA0 = pSrcA;
  223. /*
  224. * Initialize the vector pointer
  225. */
  226. pInVec = pSrcVecPtr;
  227. /*
  228. * reset accumulators
  229. */
  230. acc0 = vdupq_n_f32(0.0f);
  231. pSrcA0Vec = pInA0;
  232. blkCnt = numCols >> 2;
  233. while (blkCnt > 0U) {
  234. f32x4_t vecA;
  235. vecIn = vld1q(pInVec);
  236. pInVec += 4;
  237. vecA = vld1q(pSrcA0Vec);
  238. pSrcA0Vec += 4;
  239. acc0 = vfmaq(acc0, vecIn, vecA);
  240. blkCnt--;
  241. }
  242. /*
  243. * tail
  244. * (will be merged thru tail predication)
  245. */
  246. blkCnt = numCols & 3;
  247. if (blkCnt > 0U) {
  248. mve_pred16_t p0 = vctp32q(blkCnt);
  249. f32x4_t vecA;
  250. vecIn = vldrwq_z_f32(pInVec, p0);
  251. vecA = vldrwq_z_f32(pSrcA0Vec, p0);
  252. acc0 = vfmaq(acc0, vecIn, vecA);
  253. }
  254. /*
  255. * Sum the partial parts
  256. */
  257. f32x4_t vtmp = vuninitializedq_f32();
  258. vtmp = vsetq_lane(vecAddAcrossF32Mve(acc0), vtmp, 0);
  259. vSum = vfmaq_m_f32(vSum, vld1q(pDualCoef),
  260. arm_vec_exponent_f32
  261. (vaddq_n_f32(vmulq_n_f32(vtmp, S->gamma), S->coef0), S->degree),
  262. vctp32q(1));
  263. }
  264. sum += vecAddAcrossF32Mve(vSum);
  265. *pResult = S->classes[STEP(sum)];
  266. }
  267. #else
  268. #if defined(ARM_MATH_NEON)
  269. void arm_svm_polynomial_predict_f32(
  270. const arm_svm_polynomial_instance_f32 *S,
  271. const float32_t * in,
  272. int32_t * pResult)
  273. {
  274. float32_t sum = S->intercept;
  275. float32_t dot;
  276. float32x4_t dotV;
  277. float32x4_t accuma,accumb,accumc,accumd,accum;
  278. float32x2_t accum2;
  279. float32x4_t vec1;
  280. float32x4_t coef0 = vdupq_n_f32(S->coef0);
  281. float32x4_t vec2,vec2a,vec2b,vec2c,vec2d;
  282. uint32_t blkCnt;
  283. uint32_t vectorBlkCnt;
  284. const float32_t *pIn = in;
  285. const float32_t *pSupport = S->supportVectors;
  286. const float32_t *pSupporta = S->supportVectors;
  287. const float32_t *pSupportb;
  288. const float32_t *pSupportc;
  289. const float32_t *pSupportd;
  290. pSupportb = pSupporta + S->vectorDimension;
  291. pSupportc = pSupportb + S->vectorDimension;
  292. pSupportd = pSupportc + S->vectorDimension;
  293. const float32_t *pDualCoefs = S->dualCoefficients;
  294. vectorBlkCnt = S->nbOfSupportVectors >> 2;
  295. while (vectorBlkCnt > 0U)
  296. {
  297. accuma = vdupq_n_f32(0);
  298. accumb = vdupq_n_f32(0);
  299. accumc = vdupq_n_f32(0);
  300. accumd = vdupq_n_f32(0);
  301. pIn = in;
  302. blkCnt = S->vectorDimension >> 2;
  303. while (blkCnt > 0U)
  304. {
  305. vec1 = vld1q_f32(pIn);
  306. vec2a = vld1q_f32(pSupporta);
  307. vec2b = vld1q_f32(pSupportb);
  308. vec2c = vld1q_f32(pSupportc);
  309. vec2d = vld1q_f32(pSupportd);
  310. pIn += 4;
  311. pSupporta += 4;
  312. pSupportb += 4;
  313. pSupportc += 4;
  314. pSupportd += 4;
  315. accuma = vmlaq_f32(accuma, vec1,vec2a);
  316. accumb = vmlaq_f32(accumb, vec1,vec2b);
  317. accumc = vmlaq_f32(accumc, vec1,vec2c);
  318. accumd = vmlaq_f32(accumd, vec1,vec2d);
  319. blkCnt -- ;
  320. }
  321. accum2 = vpadd_f32(vget_low_f32(accuma),vget_high_f32(accuma));
  322. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,0);
  323. accum2 = vpadd_f32(vget_low_f32(accumb),vget_high_f32(accumb));
  324. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,1);
  325. accum2 = vpadd_f32(vget_low_f32(accumc),vget_high_f32(accumc));
  326. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,2);
  327. accum2 = vpadd_f32(vget_low_f32(accumd),vget_high_f32(accumd));
  328. dotV = vsetq_lane_f32(vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1),dotV,3);
  329. blkCnt = S->vectorDimension & 3;
  330. while (blkCnt > 0U)
  331. {
  332. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,0) + *pIn * *pSupporta++, dotV,0);
  333. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,1) + *pIn * *pSupportb++, dotV,1);
  334. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,2) + *pIn * *pSupportc++, dotV,2);
  335. dotV = vsetq_lane_f32(vgetq_lane_f32(dotV,3) + *pIn * *pSupportd++, dotV,3);
  336. pIn++;
  337. blkCnt -- ;
  338. }
  339. vec1 = vld1q_f32(pDualCoefs);
  340. pDualCoefs += 4;
  341. // To vectorize later
  342. dotV = vmulq_n_f32(dotV, S->gamma);
  343. dotV = vaddq_f32(dotV, coef0);
  344. dotV = arm_vec_exponent_f32(dotV,S->degree);
  345. accum = vmulq_f32(vec1,dotV);
  346. accum2 = vpadd_f32(vget_low_f32(accum),vget_high_f32(accum));
  347. sum += vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1);
  348. pSupporta += 3*S->vectorDimension;
  349. pSupportb += 3*S->vectorDimension;
  350. pSupportc += 3*S->vectorDimension;
  351. pSupportd += 3*S->vectorDimension;
  352. vectorBlkCnt -- ;
  353. }
  354. pSupport = pSupporta;
  355. vectorBlkCnt = S->nbOfSupportVectors & 3;
  356. while (vectorBlkCnt > 0U)
  357. {
  358. accum = vdupq_n_f32(0);
  359. dot = 0.0f;
  360. pIn = in;
  361. blkCnt = S->vectorDimension >> 2;
  362. while (blkCnt > 0U)
  363. {
  364. vec1 = vld1q_f32(pIn);
  365. vec2 = vld1q_f32(pSupport);
  366. pIn += 4;
  367. pSupport += 4;
  368. accum = vmlaq_f32(accum, vec1,vec2);
  369. blkCnt -- ;
  370. }
  371. accum2 = vpadd_f32(vget_low_f32(accum),vget_high_f32(accum));
  372. dot = vget_lane_f32(accum2, 0) + vget_lane_f32(accum2, 1);
  373. blkCnt = S->vectorDimension & 3;
  374. while (blkCnt > 0U)
  375. {
  376. dot = dot + *pIn++ * *pSupport++;
  377. blkCnt -- ;
  378. }
  379. sum += *pDualCoefs++ * arm_exponent_f32(S->gamma * dot + S->coef0, S->degree);
  380. vectorBlkCnt -- ;
  381. }
  382. *pResult=S->classes[STEP(sum)];
  383. }
  384. #else
  385. void arm_svm_polynomial_predict_f32(
  386. const arm_svm_polynomial_instance_f32 *S,
  387. const float32_t * in,
  388. int32_t * pResult)
  389. {
  390. float32_t sum=S->intercept;
  391. float32_t dot=0;
  392. uint32_t i,j;
  393. const float32_t *pSupport = S->supportVectors;
  394. for(i=0; i < S->nbOfSupportVectors; i++)
  395. {
  396. dot=0;
  397. for(j=0; j < S->vectorDimension; j++)
  398. {
  399. dot = dot + in[j]* *pSupport++;
  400. }
  401. sum += S->dualCoefficients[i] * arm_exponent_f32(S->gamma * dot + S->coef0, S->degree);
  402. }
  403. *pResult=S->classes[STEP(sum)];
  404. }
  405. #endif
  406. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  407. /**
  408. * @} end of polysvm group
  409. */