You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgemv_n_msa.c 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*******************************************************************************
  2. Copyright (c) 2016, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *******************************************************************************/
  27. #include "common.h"
  28. #include "macros_msa.h"
  29. #define SGEMV_N_8x8() \
  30. { \
  31. LD_SP2(pa0 + k, 4, t0, t1); \
  32. LD_SP2(pa1 + k, 4, t2, t3); \
  33. LD_SP2(pa2 + k, 4, t4, t5); \
  34. LD_SP2(pa3 + k, 4, t6, t7); \
  35. LD_SP2(pa4 + k, 4, t8, t9); \
  36. LD_SP2(pa5 + k, 4, t10, t11); \
  37. LD_SP2(pa6 + k, 4, t12, t13); \
  38. LD_SP2(pa7 + k, 4, t14, t15); \
  39. \
  40. y0 += tp0 * t0; \
  41. y1 += tp0 * t1; \
  42. \
  43. y0 += tp1 * t2; \
  44. y1 += tp1 * t3; \
  45. \
  46. y0 += tp2 * t4; \
  47. y1 += tp2 * t5; \
  48. \
  49. y0 += tp3 * t6; \
  50. y1 += tp3 * t7; \
  51. \
  52. y0 += tp4 * t8; \
  53. y1 += tp4 * t9; \
  54. \
  55. y0 += tp5 * t10; \
  56. y1 += tp5 * t11; \
  57. \
  58. y0 += tp6 * t12; \
  59. y1 += tp6 * t13; \
  60. \
  61. y0 += tp7 * t14; \
  62. y1 += tp7 * t15; \
  63. }
  64. #define SGEMV_N_4x8() \
  65. { \
  66. t0 = LD_SP(pa0 + k); \
  67. t2 = LD_SP(pa1 + k); \
  68. t4 = LD_SP(pa2 + k); \
  69. t6 = LD_SP(pa3 + k); \
  70. t8 = LD_SP(pa4 + k); \
  71. t10 = LD_SP(pa5 + k); \
  72. t12 = LD_SP(pa6 + k); \
  73. t14 = LD_SP(pa7 + k); \
  74. \
  75. y0 += tp0 * t0; \
  76. y0 += tp1 * t2; \
  77. y0 += tp2 * t4; \
  78. y0 += tp3 * t6; \
  79. y0 += tp4 * t8; \
  80. y0 += tp5 * t10; \
  81. y0 += tp6 * t12; \
  82. y0 += tp7 * t14; \
  83. }
  84. #define SGEMV_N_8x4() \
  85. { \
  86. LD_SP2(pa0 + k, 4, t0, t1); \
  87. LD_SP2(pa1 + k, 4, t2, t3); \
  88. LD_SP2(pa2 + k, 4, t4, t5); \
  89. LD_SP2(pa3 + k, 4, t6, t7); \
  90. \
  91. y0 += tp0 * t0; \
  92. y1 += tp0 * t1; \
  93. \
  94. y0 += tp1 * t2; \
  95. y1 += tp1 * t3; \
  96. \
  97. y0 += tp2 * t4; \
  98. y1 += tp2 * t5; \
  99. \
  100. y0 += tp3 * t6; \
  101. y1 += tp3 * t7; \
  102. }
  103. #define SGEMV_N_4x4() \
  104. { \
  105. t0 = LD_SP(pa0 + k); \
  106. t2 = LD_SP(pa1 + k); \
  107. t4 = LD_SP(pa2 + k); \
  108. t6 = LD_SP(pa3 + k); \
  109. \
  110. y0 += tp0 * t0; \
  111. y0 += tp1 * t2; \
  112. y0 += tp2 * t4; \
  113. y0 += tp3 * t6; \
  114. }
  115. #define SGEMV_N_8x2() \
  116. { \
  117. LD_SP2(pa0 + k, 4, t0, t1); \
  118. LD_SP2(pa1 + k, 4, t2, t3); \
  119. \
  120. y0 += tp0 * t0; \
  121. y1 += tp0 * t1; \
  122. \
  123. y0 += tp1 * t2; \
  124. y1 += tp1 * t3; \
  125. }
  126. #define SGEMV_N_4x2() \
  127. { \
  128. t0 = LD_SP(pa0 + k); \
  129. t2 = LD_SP(pa1 + k); \
  130. \
  131. y0 += tp0 * t0; \
  132. y0 += tp1 * t2; \
  133. }
  134. #define SLOAD_X8_SCALE_GP() \
  135. temp0 = alpha * x[0 * inc_x]; \
  136. temp1 = alpha * x[1 * inc_x]; \
  137. temp2 = alpha * x[2 * inc_x]; \
  138. temp3 = alpha * x[3 * inc_x]; \
  139. temp4 = alpha * x[4 * inc_x]; \
  140. temp5 = alpha * x[5 * inc_x]; \
  141. temp6 = alpha * x[6 * inc_x]; \
  142. temp7 = alpha * x[7 * inc_x]; \
  143. \
  144. tp0 = COPY_FLOAT_TO_VECTOR(temp0); \
  145. tp1 = COPY_FLOAT_TO_VECTOR(temp1); \
  146. tp2 = COPY_FLOAT_TO_VECTOR(temp2); \
  147. tp3 = COPY_FLOAT_TO_VECTOR(temp3); \
  148. tp4 = COPY_FLOAT_TO_VECTOR(temp4); \
  149. tp5 = COPY_FLOAT_TO_VECTOR(temp5); \
  150. tp6 = COPY_FLOAT_TO_VECTOR(temp6); \
  151. tp7 = COPY_FLOAT_TO_VECTOR(temp7); \
  152. #define SLOAD_X4_SCALE_GP() \
  153. temp0 = alpha * x[0 * inc_x]; \
  154. temp1 = alpha * x[1 * inc_x]; \
  155. temp2 = alpha * x[2 * inc_x]; \
  156. temp3 = alpha * x[3 * inc_x]; \
  157. \
  158. tp0 = COPY_FLOAT_TO_VECTOR(temp0); \
  159. tp1 = COPY_FLOAT_TO_VECTOR(temp1); \
  160. tp2 = COPY_FLOAT_TO_VECTOR(temp2); \
  161. tp3 = COPY_FLOAT_TO_VECTOR(temp3); \
  162. #define SLOAD_X8_SCALE_VECTOR() \
  163. LD_SP2(x, 4, x0, x1); \
  164. \
  165. x0 = x0 * v_alpha; \
  166. x1 = x1 * v_alpha; \
  167. \
  168. SPLATI_W4_SP(x0, tp0, tp1, tp2, tp3); \
  169. SPLATI_W4_SP(x1, tp4, tp5, tp6, tp7); \
  170. #define SLOAD_X4_SCALE_VECTOR() \
  171. x0 = LD_SP(x); \
  172. x0 = x0 * v_alpha; \
  173. SPLATI_W4_SP(x0, tp0, tp1, tp2, tp3); \
  174. #define SLOAD_Y8_GP() \
  175. y0 = (v4f32) __msa_insert_w((v4i32) tp0, 0, *((int *)(y + 0 * inc_y))); \
  176. y0 = (v4f32) __msa_insert_w((v4i32) y0, 1, *((int *)(y + 1 * inc_y))); \
  177. y0 = (v4f32) __msa_insert_w((v4i32) y0, 2, *((int *)(y + 2 * inc_y))); \
  178. y0 = (v4f32) __msa_insert_w((v4i32) y0, 3, *((int *)(y + 3 * inc_y))); \
  179. y1 = (v4f32) __msa_insert_w((v4i32) tp0, 0, *((int *)(y + 4 * inc_y))); \
  180. y1 = (v4f32) __msa_insert_w((v4i32) y1, 1, *((int *)(y + 5 * inc_y))); \
  181. y1 = (v4f32) __msa_insert_w((v4i32) y1, 2, *((int *)(y + 6 * inc_y))); \
  182. y1 = (v4f32) __msa_insert_w((v4i32) y1, 3, *((int *)(y + 7 * inc_y))); \
  183. #define SLOAD_Y4_GP() \
  184. y0 = (v4f32) __msa_insert_w((v4i32) tp0, 0, *((int *)(y + 0 * inc_y))); \
  185. y0 = (v4f32) __msa_insert_w((v4i32) y0, 1, *((int *)(y + 1 * inc_y))); \
  186. y0 = (v4f32) __msa_insert_w((v4i32) y0, 2, *((int *)(y + 2 * inc_y))); \
  187. y0 = (v4f32) __msa_insert_w((v4i32) y0, 3, *((int *)(y + 3 * inc_y))); \
  188. #define SLOAD_Y8_VECTOR() LD_SP2(y, 4, y0, y1);
  189. #define SLOAD_Y4_VECTOR() y0 = LD_SP(y);
  190. #define SSTORE_Y8_GP() \
  191. *((int *)(y + 0 * inc_y)) = __msa_copy_s_w((v4i32) y0, 0); \
  192. *((int *)(y + 1 * inc_y)) = __msa_copy_s_w((v4i32) y0, 1); \
  193. *((int *)(y + 2 * inc_y)) = __msa_copy_s_w((v4i32) y0, 2); \
  194. *((int *)(y + 3 * inc_y)) = __msa_copy_s_w((v4i32) y0, 3); \
  195. *((int *)(y + 4 * inc_y)) = __msa_copy_s_w((v4i32) y1, 0); \
  196. *((int *)(y + 5 * inc_y)) = __msa_copy_s_w((v4i32) y1, 1); \
  197. *((int *)(y + 6 * inc_y)) = __msa_copy_s_w((v4i32) y1, 2); \
  198. *((int *)(y + 7 * inc_y)) = __msa_copy_s_w((v4i32) y1, 3); \
  199. #define SSTORE_Y4_GP() \
  200. *((int *)(y + 0 * inc_y)) = __msa_copy_s_w((v4i32) y0, 0); \
  201. *((int *)(y + 1 * inc_y)) = __msa_copy_s_w((v4i32) y0, 1); \
  202. *((int *)(y + 2 * inc_y)) = __msa_copy_s_w((v4i32) y0, 2); \
  203. *((int *)(y + 3 * inc_y)) = __msa_copy_s_w((v4i32) y0, 3); \
  204. #define SSTORE_Y8_VECTOR() ST_SP2(y0, y1, y, 4);
  205. #define SSTORE_Y4_VECTOR() ST_SP(y0, y);
  206. #define SGEMV_N_MSA() \
  207. for (j = (n >> 3); j--;) \
  208. { \
  209. SLOAD_X8_SCALE(); \
  210. \
  211. k = 0; \
  212. y = y_org; \
  213. \
  214. for (i = (m >> 3); i--;) \
  215. { \
  216. SLOAD_Y8(); \
  217. SGEMV_N_8x8(); \
  218. SSTORE_Y8(); \
  219. \
  220. y += 8 * inc_y; \
  221. k += 8; \
  222. } \
  223. \
  224. if (m & 4) \
  225. { \
  226. SLOAD_Y4(); \
  227. SGEMV_N_4x8(); \
  228. SSTORE_Y4(); \
  229. \
  230. y += 4 * inc_y; \
  231. k += 4; \
  232. } \
  233. \
  234. if (m & 3) \
  235. { \
  236. temp0 = alpha * x[0 * inc_x]; \
  237. temp1 = alpha * x[1 * inc_x]; \
  238. temp2 = alpha * x[2 * inc_x]; \
  239. temp3 = alpha * x[3 * inc_x]; \
  240. temp4 = alpha * x[4 * inc_x]; \
  241. temp5 = alpha * x[5 * inc_x]; \
  242. temp6 = alpha * x[6 * inc_x]; \
  243. temp7 = alpha * x[7 * inc_x]; \
  244. \
  245. for (i = (m & 3); i--;) \
  246. { \
  247. temp = y[0]; \
  248. temp += temp0 * pa0[k]; \
  249. temp += temp1 * pa1[k]; \
  250. temp += temp2 * pa2[k]; \
  251. temp += temp3 * pa3[k]; \
  252. temp += temp4 * pa4[k]; \
  253. temp += temp5 * pa5[k]; \
  254. temp += temp6 * pa6[k]; \
  255. temp += temp7 * pa7[k]; \
  256. y[0] = temp; \
  257. \
  258. y += inc_y; \
  259. k++; \
  260. } \
  261. } \
  262. pa0 += 8 * lda; \
  263. pa1 += 8 * lda; \
  264. pa2 += 8 * lda; \
  265. pa3 += 8 * lda; \
  266. pa4 += 8 * lda; \
  267. pa5 += 8 * lda; \
  268. pa6 += 8 * lda; \
  269. pa7 += 8 * lda; \
  270. \
  271. x += 8 * inc_x; \
  272. } \
  273. \
  274. if (n & 4) \
  275. { \
  276. SLOAD_X4_SCALE(); \
  277. \
  278. k = 0; \
  279. y = y_org; \
  280. \
  281. for (i = (m >> 3); i--;) \
  282. { \
  283. SLOAD_Y8(); \
  284. SGEMV_N_8x4(); \
  285. SSTORE_Y8(); \
  286. \
  287. y += 8 * inc_y; \
  288. k += 8; \
  289. } \
  290. \
  291. if (m & 4) \
  292. { \
  293. SLOAD_Y4(); \
  294. SGEMV_N_4x4(); \
  295. SSTORE_Y4(); \
  296. \
  297. y += 4 * inc_y; \
  298. k += 4; \
  299. } \
  300. \
  301. if (m & 3) \
  302. { \
  303. temp0 = alpha * x[0 * inc_x]; \
  304. temp1 = alpha * x[1 * inc_x]; \
  305. temp2 = alpha * x[2 * inc_x]; \
  306. temp3 = alpha * x[3 * inc_x]; \
  307. \
  308. for (i = (m & 3); i--;) \
  309. { \
  310. temp = y[0]; \
  311. temp += temp0 * pa0[k]; \
  312. temp += temp1 * pa1[k]; \
  313. temp += temp2 * pa2[k]; \
  314. temp += temp3 * pa3[k]; \
  315. y[0] = temp; \
  316. \
  317. y += inc_y; \
  318. k++; \
  319. } \
  320. } \
  321. \
  322. pa0 += 4 * lda; \
  323. pa1 += 4 * lda; \
  324. pa2 += 4 * lda; \
  325. pa3 += 4 * lda; \
  326. \
  327. x += 4 * inc_x; \
  328. } \
  329. \
  330. if (n & 2) \
  331. { \
  332. temp0 = alpha * x[0 * inc_x]; \
  333. temp1 = alpha * x[1 * inc_x]; \
  334. \
  335. tp0 = COPY_FLOAT_TO_VECTOR(temp0); \
  336. tp1 = COPY_FLOAT_TO_VECTOR(temp1); \
  337. \
  338. k = 0; \
  339. y = y_org; \
  340. \
  341. for (i = (m >> 3); i--;) \
  342. { \
  343. SLOAD_Y8(); \
  344. SGEMV_N_8x2(); \
  345. SSTORE_Y8(); \
  346. \
  347. y += 8 * inc_y; \
  348. k += 8; \
  349. } \
  350. \
  351. if (m & 4) \
  352. { \
  353. SLOAD_Y4(); \
  354. SGEMV_N_4x2(); \
  355. SSTORE_Y4(); \
  356. \
  357. y += 4 * inc_y; \
  358. k += 4; \
  359. } \
  360. \
  361. if (m & 3) \
  362. { \
  363. temp0 = alpha * x[0 * inc_x]; \
  364. temp1 = alpha * x[1 * inc_x]; \
  365. \
  366. for (i = (m & 3); i--;) \
  367. { \
  368. temp = y[0]; \
  369. temp += temp0 * pa0[k]; \
  370. temp += temp1 * pa1[k]; \
  371. y[0] = temp; \
  372. \
  373. y += inc_y; \
  374. k++; \
  375. } \
  376. } \
  377. \
  378. pa0 += 2 * lda; \
  379. pa1 += 2 * lda; \
  380. \
  381. x += 2 * inc_x; \
  382. } \
  383. \
  384. if (n & 1) \
  385. { \
  386. temp = alpha * x[0]; \
  387. \
  388. k = 0; \
  389. y = y_org; \
  390. \
  391. for (i = m; i--;) \
  392. { \
  393. y[0] += temp * pa0[k]; \
  394. \
  395. y += inc_y; \
  396. k++; \
  397. } \
  398. } \
  399. int CNAME(BLASLONG m, BLASLONG n, BLASLONG dummy1, FLOAT alpha, FLOAT *A,
  400. BLASLONG lda, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y,
  401. FLOAT *buffer)
  402. {
  403. BLASLONG i, j, k;
  404. FLOAT *y_org = y;
  405. FLOAT *pa0, *pa1, *pa2, *pa3, *pa4, *pa5, *pa6, *pa7;
  406. FLOAT temp, temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
  407. v4f32 v_alpha, x0, x1, y0, y1;
  408. v4f32 t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15;
  409. v4f32 tp0, tp1, tp2, tp3, tp4, tp5, tp6, tp7;
  410. v_alpha = COPY_FLOAT_TO_VECTOR(alpha);
  411. pa0 = A;
  412. pa1 = A + lda;
  413. pa2 = A + 2 * lda;
  414. pa3 = A + 3 * lda;
  415. pa4 = A + 4 * lda;
  416. pa5 = A + 5 * lda;
  417. pa6 = A + 6 * lda;
  418. pa7 = A + 7 * lda;
  419. if ((1 == inc_x) && (1 == inc_y))
  420. {
  421. #define SLOAD_X8_SCALE SLOAD_X8_SCALE_VECTOR
  422. #define SLOAD_X4_SCALE SLOAD_X4_SCALE_VECTOR
  423. #define SLOAD_Y8 SLOAD_Y8_VECTOR
  424. #define SLOAD_Y4 SLOAD_Y4_VECTOR
  425. #define SSTORE_Y8 SSTORE_Y8_VECTOR
  426. #define SSTORE_Y4 SSTORE_Y4_VECTOR
  427. SGEMV_N_MSA();
  428. #undef SLOAD_X8_SCALE
  429. #undef SLOAD_X4_SCALE
  430. #undef SLOAD_Y8
  431. #undef SLOAD_Y4
  432. #undef SSTORE_Y8
  433. #undef SSTORE_Y4
  434. }
  435. else if (1 == inc_y)
  436. {
  437. #define SLOAD_X8_SCALE SLOAD_X8_SCALE_GP
  438. #define SLOAD_X4_SCALE SLOAD_X4_SCALE_GP
  439. #define SLOAD_Y8 SLOAD_Y8_VECTOR
  440. #define SLOAD_Y4 SLOAD_Y4_VECTOR
  441. #define SSTORE_Y8 SSTORE_Y8_VECTOR
  442. #define SSTORE_Y4 SSTORE_Y4_VECTOR
  443. SGEMV_N_MSA();
  444. #undef SLOAD_X8_SCALE
  445. #undef SLOAD_X4_SCALE
  446. #undef SLOAD_Y8
  447. #undef SLOAD_Y4
  448. #undef SSTORE_Y8
  449. #undef SSTORE_Y4
  450. }
  451. else if (1 == inc_x)
  452. {
  453. #define SLOAD_X8_SCALE SLOAD_X8_SCALE_VECTOR
  454. #define SLOAD_X4_SCALE SLOAD_X4_SCALE_VECTOR
  455. #define SLOAD_Y8 SLOAD_Y8_GP
  456. #define SLOAD_Y4 SLOAD_Y4_GP
  457. #define SSTORE_Y8 SSTORE_Y8_GP
  458. #define SSTORE_Y4 SSTORE_Y4_GP
  459. SGEMV_N_MSA();
  460. #undef SLOAD_X8_SCALE
  461. #undef SLOAD_X4_SCALE
  462. #undef SLOAD_Y8
  463. #undef SLOAD_Y4
  464. #undef SSTORE_Y8
  465. #undef SSTORE_Y4
  466. }
  467. else
  468. {
  469. #define SLOAD_X8_SCALE SLOAD_X8_SCALE_GP
  470. #define SLOAD_X4_SCALE SLOAD_X4_SCALE_GP
  471. #define SLOAD_Y8 SLOAD_Y8_GP
  472. #define SLOAD_Y4 SLOAD_Y4_GP
  473. #define SSTORE_Y8 SSTORE_Y8_GP
  474. #define SSTORE_Y4 SSTORE_Y4_GP
  475. SGEMV_N_MSA();
  476. #undef SLOAD_X8_SCALE
  477. #undef SLOAD_X4_SCALE
  478. #undef SLOAD_Y8
  479. #undef SLOAD_Y4
  480. #undef SSTORE_Y8
  481. #undef SSTORE_Y4
  482. }
  483. return(0);
  484. }