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.

dgemv_t_4.c 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /***************************************************************************
  2. Copyright (c) 2014, 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. #if defined(HASWELL) || defined(ZEN) || defined(STEAMROLLER) || defined(EXCAVATOR) || defined (SKYLAKEX) || defined (COOPERLAKE)
  29. #include "dgemv_t_microk_haswell-4.c"
  30. #endif
  31. #define NBMAX 2048
  32. #ifndef HAVE_KERNEL_4x4
  33. static void dgemv_kernel_4x4(BLASLONG n, FLOAT **ap, FLOAT *x, FLOAT *y)
  34. {
  35. BLASLONG i;
  36. FLOAT *a0,*a1,*a2,*a3;
  37. a0 = ap[0];
  38. a1 = ap[1];
  39. a2 = ap[2];
  40. a3 = ap[3];
  41. FLOAT temp0 = 0.0;
  42. FLOAT temp1 = 0.0;
  43. FLOAT temp2 = 0.0;
  44. FLOAT temp3 = 0.0;
  45. for ( i=0; i< n; i+=4 )
  46. {
  47. temp0 += a0[i]*x[i] + a0[i+1]*x[i+1] + a0[i+2]*x[i+2] + a0[i+3]*x[i+3];
  48. temp1 += a1[i]*x[i] + a1[i+1]*x[i+1] + a1[i+2]*x[i+2] + a1[i+3]*x[i+3];
  49. temp2 += a2[i]*x[i] + a2[i+1]*x[i+1] + a2[i+2]*x[i+2] + a2[i+3]*x[i+3];
  50. temp3 += a3[i]*x[i] + a3[i+1]*x[i+1] + a3[i+2]*x[i+2] + a3[i+3]*x[i+3];
  51. }
  52. y[0] = temp0;
  53. y[1] = temp1;
  54. y[2] = temp2;
  55. y[3] = temp3;
  56. }
  57. #endif
  58. static void dgemv_kernel_4x2(BLASLONG n, FLOAT *ap0, FLOAT *ap1, FLOAT *x, FLOAT *y) __attribute__ ((noinline));
  59. static void dgemv_kernel_4x2(BLASLONG n, FLOAT *ap0, FLOAT *ap1, FLOAT *x, FLOAT *y)
  60. {
  61. BLASLONG i;
  62. i=0;
  63. __asm__ __volatile__
  64. (
  65. "xorpd %%xmm10 , %%xmm10 \n\t"
  66. "xorpd %%xmm11 , %%xmm11 \n\t"
  67. "testq $2 , %1 \n\t"
  68. "jz 2f \n\t"
  69. "movups (%5,%0,8) , %%xmm14 \n\t" // x
  70. "movups (%3,%0,8) , %%xmm12 \n\t" // ap0
  71. "movups (%4,%0,8) , %%xmm13 \n\t" // ap1
  72. "mulpd %%xmm14 , %%xmm12 \n\t"
  73. "mulpd %%xmm14 , %%xmm13 \n\t"
  74. "addq $2 , %0 \n\t"
  75. "addpd %%xmm12 , %%xmm10 \n\t"
  76. "subq $2 , %1 \n\t"
  77. "addpd %%xmm13 , %%xmm11 \n\t"
  78. "2: \n\t"
  79. "cmpq $0, %1 \n\t"
  80. "je 3f \n\t"
  81. // ".align 16 \n\t"
  82. "1: \n\t"
  83. "movups (%5,%0,8) , %%xmm14 \n\t" // x
  84. "movups (%3,%0,8) , %%xmm12 \n\t" // ap0
  85. "movups (%4,%0,8) , %%xmm13 \n\t" // ap1
  86. "mulpd %%xmm14 , %%xmm12 \n\t"
  87. "mulpd %%xmm14 , %%xmm13 \n\t"
  88. "addpd %%xmm12 , %%xmm10 \n\t"
  89. "addpd %%xmm13 , %%xmm11 \n\t"
  90. "movups 16(%5,%0,8) , %%xmm14 \n\t" // x
  91. "movups 16(%3,%0,8) , %%xmm12 \n\t" // ap0
  92. "movups 16(%4,%0,8) , %%xmm13 \n\t" // ap1
  93. "mulpd %%xmm14 , %%xmm12 \n\t"
  94. "mulpd %%xmm14 , %%xmm13 \n\t"
  95. "addpd %%xmm12 , %%xmm10 \n\t"
  96. "addpd %%xmm13 , %%xmm11 \n\t"
  97. "addq $4 , %0 \n\t"
  98. "subq $4 , %1 \n\t"
  99. "jnz 1b \n\t"
  100. "3: \n\t"
  101. "haddpd %%xmm10, %%xmm10 \n\t"
  102. "haddpd %%xmm11, %%xmm11 \n\t"
  103. "movsd %%xmm10, (%2) \n\t"
  104. "movsd %%xmm11,8(%2) \n\t"
  105. :
  106. "+r" (i), // 0
  107. "+r" (n) // 1
  108. :
  109. "r" (y), // 2
  110. "r" (ap0), // 3
  111. "r" (ap1), // 4
  112. "r" (x) // 5
  113. : "cc",
  114. "%xmm4", "%xmm5", "%xmm10", "%xmm11",
  115. "%xmm12", "%xmm13", "%xmm14", "%xmm15",
  116. "memory"
  117. );
  118. }
  119. static void dgemv_kernel_4x1(BLASLONG n, FLOAT *ap, FLOAT *x, FLOAT *y) __attribute__ ((noinline));
  120. static void dgemv_kernel_4x1(BLASLONG n, FLOAT *ap, FLOAT *x, FLOAT *y)
  121. {
  122. BLASLONG i;
  123. i=0;
  124. __asm__ __volatile__
  125. (
  126. "xorpd %%xmm9 , %%xmm9 \n\t"
  127. "xorpd %%xmm10 , %%xmm10 \n\t"
  128. "testq $2 , %1 \n\t"
  129. "jz 2f \n\t"
  130. "movups (%3,%0,8) , %%xmm12 \n\t"
  131. "movups (%4,%0,8) , %%xmm11 \n\t"
  132. "mulpd %%xmm11 , %%xmm12 \n\t"
  133. "addq $2 , %0 \n\t"
  134. "addpd %%xmm12 , %%xmm10 \n\t"
  135. "subq $2 , %1 \n\t"
  136. "2: \n\t"
  137. "cmpq $0, %1 \n\t"
  138. "je 3f \n\t"
  139. // ".align 16 \n\t"
  140. "1: \n\t"
  141. "movups (%3,%0,8) , %%xmm12 \n\t"
  142. "movups 16(%3,%0,8) , %%xmm14 \n\t"
  143. "movups (%4,%0,8) , %%xmm11 \n\t"
  144. "movups 16(%4,%0,8) , %%xmm13 \n\t"
  145. "mulpd %%xmm11 , %%xmm12 \n\t"
  146. "mulpd %%xmm13 , %%xmm14 \n\t"
  147. "addq $4 , %0 \n\t"
  148. "addpd %%xmm12 , %%xmm10 \n\t"
  149. "subq $4 , %1 \n\t"
  150. "addpd %%xmm14 , %%xmm9 \n\t"
  151. "jnz 1b \n\t"
  152. "3: \n\t"
  153. "addpd %%xmm9 , %%xmm10 \n\t"
  154. "haddpd %%xmm10, %%xmm10 \n\t"
  155. "movsd %%xmm10, (%2) \n\t"
  156. :
  157. "+r" (i), // 0
  158. "+r" (n) // 1
  159. :
  160. "r" (y), // 2
  161. "r" (ap), // 3
  162. "r" (x) // 4
  163. : "cc",
  164. "%xmm9", "%xmm10" ,
  165. "%xmm11", "%xmm12", "%xmm13", "%xmm14",
  166. "memory"
  167. );
  168. }
  169. static void copy_x(BLASLONG n, FLOAT *src, FLOAT *dest, BLASLONG inc_src)
  170. {
  171. BLASLONG i;
  172. for ( i=0; i<n; i++ )
  173. {
  174. *dest = *src;
  175. dest++;
  176. src += inc_src;
  177. }
  178. }
  179. static void add_y(BLASLONG n, FLOAT da , FLOAT *src, FLOAT *dest, BLASLONG inc_dest) __attribute__ ((noinline));
  180. static void add_y(BLASLONG n, FLOAT da , FLOAT *src, FLOAT *dest, BLASLONG inc_dest)
  181. {
  182. BLASLONG i;
  183. if ( inc_dest != 1 )
  184. {
  185. for ( i=0; i<n; i++ )
  186. {
  187. *dest += src[i] * da;
  188. dest += inc_dest;
  189. }
  190. return;
  191. }
  192. i=0;
  193. __asm__ __volatile__
  194. (
  195. "movsd (%2) , %%xmm10 \n\t"
  196. "shufpd $0 , %%xmm10 , %%xmm10 \n\t"
  197. // ".align 16 \n\t"
  198. "1: \n\t"
  199. "movups (%3,%0,8) , %%xmm12 \n\t"
  200. "movups (%4,%0,8) , %%xmm11 \n\t"
  201. "mulpd %%xmm10 , %%xmm12 \n\t"
  202. "addq $2 , %0 \n\t"
  203. "addpd %%xmm12 , %%xmm11 \n\t"
  204. "subq $2 , %1 \n\t"
  205. "movups %%xmm11, -16(%4,%0,8) \n\t"
  206. "jnz 1b \n\t"
  207. :
  208. "+r" (i), // 0
  209. "+r" (n) // 1
  210. :
  211. "r" (&da), // 2
  212. "r" (src), // 3
  213. "r" (dest) // 4
  214. : "cc",
  215. "%xmm10", "%xmm11", "%xmm12",
  216. "memory"
  217. );
  218. }
  219. int CNAME(BLASLONG m, BLASLONG n, BLASLONG dummy1, FLOAT alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y, FLOAT *buffer)
  220. {
  221. BLASLONG register i;
  222. BLASLONG register j;
  223. FLOAT *a_ptr;
  224. FLOAT *x_ptr;
  225. FLOAT *y_ptr;
  226. BLASLONG n0;
  227. BLASLONG n1;
  228. BLASLONG m1;
  229. BLASLONG m2;
  230. BLASLONG m3;
  231. BLASLONG n2;
  232. FLOAT ybuffer[4],*xbuffer;
  233. FLOAT *ytemp;
  234. if ( m < 1 ) return(0);
  235. if ( n < 1 ) return(0);
  236. xbuffer = buffer;
  237. ytemp = buffer + (m < NBMAX ? m : NBMAX);
  238. n0 = n / NBMAX;
  239. n1 = (n % NBMAX) >> 2 ;
  240. n2 = n & 3 ;
  241. m3 = m & 3 ;
  242. m1 = m & -4 ;
  243. m2 = (m & (NBMAX-1)) - m3 ;
  244. BLASLONG NB = NBMAX;
  245. while ( NB == NBMAX )
  246. {
  247. m1 -= NB;
  248. if ( m1 < 0)
  249. {
  250. if ( m2 == 0 ) break;
  251. NB = m2;
  252. }
  253. y_ptr = y;
  254. a_ptr = a;
  255. x_ptr = x;
  256. if ( inc_x == 1 )
  257. xbuffer = x_ptr;
  258. else
  259. copy_x(NB,x_ptr,xbuffer,inc_x);
  260. FLOAT *ap[4];
  261. FLOAT *yp;
  262. BLASLONG register lda4 = 4 * lda;
  263. ap[0] = a_ptr;
  264. ap[1] = a_ptr + lda;
  265. ap[2] = ap[1] + lda;
  266. ap[3] = ap[2] + lda;
  267. if ( n0 > 0 )
  268. {
  269. BLASLONG nb1 = NBMAX / 4;
  270. for( j=0; j<n0; j++)
  271. {
  272. yp = ytemp;
  273. for( i = 0; i < nb1 ; i++)
  274. {
  275. dgemv_kernel_4x4(NB,ap,xbuffer,yp);
  276. ap[0] += lda4 ;
  277. ap[1] += lda4 ;
  278. ap[2] += lda4 ;
  279. ap[3] += lda4 ;
  280. yp += 4;
  281. }
  282. add_y(nb1*4, alpha, ytemp, y_ptr, inc_y );
  283. y_ptr += nb1 * inc_y * 4;
  284. a_ptr += nb1 * lda4 ;
  285. }
  286. }
  287. yp = ytemp;
  288. for( i = 0; i < n1 ; i++)
  289. {
  290. dgemv_kernel_4x4(NB,ap,xbuffer,yp);
  291. ap[0] += lda4 ;
  292. ap[1] += lda4 ;
  293. ap[2] += lda4 ;
  294. ap[3] += lda4 ;
  295. yp += 4;
  296. }
  297. if ( n1 > 0 )
  298. {
  299. add_y(n1*4, alpha, ytemp, y_ptr, inc_y );
  300. y_ptr += n1 * inc_y * 4;
  301. a_ptr += n1 * lda4 ;
  302. }
  303. if ( n2 & 2 )
  304. {
  305. dgemv_kernel_4x2(NB,ap[0],ap[1],xbuffer,ybuffer);
  306. a_ptr += lda * 2;
  307. *y_ptr += ybuffer[0] * alpha;
  308. y_ptr += inc_y;
  309. *y_ptr += ybuffer[1] * alpha;
  310. y_ptr += inc_y;
  311. }
  312. if ( n2 & 1 )
  313. {
  314. dgemv_kernel_4x1(NB,a_ptr,xbuffer,ybuffer);
  315. // a_ptr += lda;
  316. *y_ptr += ybuffer[0] * alpha;
  317. // y_ptr += inc_y;
  318. }
  319. a += NB;
  320. x += NB * inc_x;
  321. }
  322. if ( m3 == 0 ) return(0);
  323. x_ptr = x;
  324. a_ptr = a;
  325. if ( m3 == 3 )
  326. {
  327. FLOAT xtemp0 = *x_ptr * alpha;
  328. x_ptr += inc_x;
  329. FLOAT xtemp1 = *x_ptr * alpha;
  330. x_ptr += inc_x;
  331. FLOAT xtemp2 = *x_ptr * alpha;
  332. FLOAT *aj = a_ptr;
  333. y_ptr = y;
  334. if ( lda == 3 && inc_y == 1 )
  335. {
  336. for ( j=0; j< ( n & -4) ; j+=4 )
  337. {
  338. y_ptr[j] += aj[0] * xtemp0 + aj[1] * xtemp1 + aj[2] * xtemp2;
  339. y_ptr[j+1] += aj[3] * xtemp0 + aj[4] * xtemp1 + aj[5] * xtemp2;
  340. y_ptr[j+2] += aj[6] * xtemp0 + aj[7] * xtemp1 + aj[8] * xtemp2;
  341. y_ptr[j+3] += aj[9] * xtemp0 + aj[10] * xtemp1 + aj[11] * xtemp2;
  342. aj += 12;
  343. }
  344. for ( ; j<n; j++ )
  345. {
  346. y_ptr[j] += aj[0] * xtemp0 + aj[1] * xtemp1 + aj[2] * xtemp2;
  347. aj += 3;
  348. }
  349. }
  350. else
  351. {
  352. if ( inc_y == 1 )
  353. {
  354. BLASLONG register lda2 = lda << 1;
  355. BLASLONG register lda4 = lda << 2;
  356. BLASLONG register lda3 = lda2 + lda;
  357. for ( j=0; j< ( n & -4 ); j+=4 )
  358. {
  359. y_ptr[j] += *aj * xtemp0 + *(aj+1) * xtemp1 + *(aj+2) * xtemp2;
  360. y_ptr[j+1] += *(aj+lda) * xtemp0 + *(aj+lda+1) * xtemp1 + *(aj+lda+2) * xtemp2;
  361. y_ptr[j+2] += *(aj+lda2) * xtemp0 + *(aj+lda2+1) * xtemp1 + *(aj+lda2+2) * xtemp2;
  362. y_ptr[j+3] += *(aj+lda3) * xtemp0 + *(aj+lda3+1) * xtemp1 + *(aj+lda3+2) * xtemp2;
  363. aj += lda4;
  364. }
  365. for ( ; j< n ; j++ )
  366. {
  367. y_ptr[j] += *aj * xtemp0 + *(aj+1) * xtemp1 + *(aj+2) * xtemp2 ;
  368. aj += lda;
  369. }
  370. }
  371. else
  372. {
  373. for ( j=0; j<n; j++ )
  374. {
  375. *y_ptr += *aj * xtemp0 + *(aj+1) * xtemp1 + *(aj+2) * xtemp2;
  376. y_ptr += inc_y;
  377. aj += lda;
  378. }
  379. }
  380. }
  381. return(0);
  382. }
  383. if ( m3 == 2 )
  384. {
  385. FLOAT xtemp0 = *x_ptr * alpha;
  386. x_ptr += inc_x;
  387. FLOAT xtemp1 = *x_ptr * alpha;
  388. FLOAT *aj = a_ptr;
  389. y_ptr = y;
  390. if ( lda == 2 && inc_y == 1 )
  391. {
  392. for ( j=0; j< ( n & -4) ; j+=4 )
  393. {
  394. y_ptr[j] += aj[0] * xtemp0 + aj[1] * xtemp1 ;
  395. y_ptr[j+1] += aj[2] * xtemp0 + aj[3] * xtemp1 ;
  396. y_ptr[j+2] += aj[4] * xtemp0 + aj[5] * xtemp1 ;
  397. y_ptr[j+3] += aj[6] * xtemp0 + aj[7] * xtemp1 ;
  398. aj += 8;
  399. }
  400. for ( ; j<n; j++ )
  401. {
  402. y_ptr[j] += aj[0] * xtemp0 + aj[1] * xtemp1 ;
  403. aj += 2;
  404. }
  405. }
  406. else
  407. {
  408. if ( inc_y == 1 )
  409. {
  410. BLASLONG register lda2 = lda << 1;
  411. BLASLONG register lda4 = lda << 2;
  412. BLASLONG register lda3 = lda2 + lda;
  413. for ( j=0; j< ( n & -4 ); j+=4 )
  414. {
  415. y_ptr[j] += *aj * xtemp0 + *(aj+1) * xtemp1 ;
  416. y_ptr[j+1] += *(aj+lda) * xtemp0 + *(aj+lda+1) * xtemp1 ;
  417. y_ptr[j+2] += *(aj+lda2) * xtemp0 + *(aj+lda2+1) * xtemp1 ;
  418. y_ptr[j+3] += *(aj+lda3) * xtemp0 + *(aj+lda3+1) * xtemp1 ;
  419. aj += lda4;
  420. }
  421. for ( ; j< n ; j++ )
  422. {
  423. y_ptr[j] += *aj * xtemp0 + *(aj+1) * xtemp1 ;
  424. aj += lda;
  425. }
  426. }
  427. else
  428. {
  429. for ( j=0; j<n; j++ )
  430. {
  431. *y_ptr += *aj * xtemp0 + *(aj+1) * xtemp1 ;
  432. y_ptr += inc_y;
  433. aj += lda;
  434. }
  435. }
  436. }
  437. return(0);
  438. }
  439. FLOAT xtemp = *x_ptr * alpha;
  440. FLOAT *aj = a_ptr;
  441. y_ptr = y;
  442. if ( lda == 1 && inc_y == 1 )
  443. {
  444. for ( j=0; j< ( n & -4) ; j+=4 )
  445. {
  446. y_ptr[j] += aj[j] * xtemp;
  447. y_ptr[j+1] += aj[j+1] * xtemp;
  448. y_ptr[j+2] += aj[j+2] * xtemp;
  449. y_ptr[j+3] += aj[j+3] * xtemp;
  450. }
  451. for ( ; j<n ; j++ )
  452. {
  453. y_ptr[j] += aj[j] * xtemp;
  454. }
  455. }
  456. else
  457. {
  458. if ( inc_y == 1 )
  459. {
  460. BLASLONG register lda2 = lda << 1;
  461. BLASLONG register lda4 = lda << 2;
  462. BLASLONG register lda3 = lda2 + lda;
  463. for ( j=0; j< ( n & -4 ); j+=4 )
  464. {
  465. y_ptr[j] += *aj * xtemp;
  466. y_ptr[j+1] += *(aj+lda) * xtemp;
  467. y_ptr[j+2] += *(aj+lda2) * xtemp;
  468. y_ptr[j+3] += *(aj+lda3) * xtemp;
  469. aj += lda4 ;
  470. }
  471. for ( ; j<n; j++ )
  472. {
  473. y_ptr[j] += *aj * xtemp;
  474. aj += lda;
  475. }
  476. }
  477. else
  478. {
  479. for ( j=0; j<n; j++ )
  480. {
  481. *y_ptr += *aj * xtemp;
  482. y_ptr += inc_y;
  483. aj += lda;
  484. }
  485. }
  486. }
  487. return(0);
  488. }