arm_svm_polynomial_predict_f32.c 14 KB

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