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.

iamax_thunderx2t99.c 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /***************************************************************************
  2. Copyright (c) 2017, 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. #define N "x0" /* vector length */
  29. #define X "x1" /* "X" vector address */
  30. #define INC_X "x2" /* "X" stride */
  31. #define INDEX "x3" /* index of max/min value */
  32. #define Z "x4" /* vector index */
  33. #define J "x5" /* loop variable */
  34. #if !defined(DOUBLE)
  35. #define MAXF "s0"
  36. #define TMPF0 "s1"
  37. #define TMPF1 "s4"
  38. #define N_KERNEL_SIZE "64"
  39. #define SZ "4"
  40. #define N_DIV_SHIFT "6"
  41. #define N_REM_MASK "63"
  42. #define INC_SHIFT "2"
  43. #else
  44. #define MAXF "d0"
  45. #define TMPF0 "d1"
  46. #define TMPF1 "d4"
  47. #define N_KERNEL_SIZE "32"
  48. #define SZ "8"
  49. #define N_DIV_SHIFT "5"
  50. #define N_REM_MASK "31"
  51. #define INC_SHIFT "3"
  52. #endif
  53. /******************************************************************************/
  54. #if !defined(DOUBLE)
  55. #define KERNEL_F \
  56. "ldp q2, q3, ["X"] \n" \
  57. "ldp q4, q5, ["X", #32] \n" \
  58. "ldp q6, q7, ["X", #64] \n" \
  59. "ldp q16, q17, ["X", #96] \n" \
  60. "ldp q18, q19, ["X", #128] \n" \
  61. "ldp q20, q21, ["X", #160] \n" \
  62. "ldp q22, q23, ["X", #192] \n" \
  63. "ldp q24, q25, ["X", #224] \n" \
  64. "add "X", "X", #256 \n" \
  65. "fabs v2.4s, v2.4s \n" \
  66. "fabs v3.4s, v3.4s \n" \
  67. "fabs v4.4s, v4.4s \n" \
  68. "fabs v5.4s, v5.4s \n" \
  69. "fabs v6.4s, v6.4s \n" \
  70. "fabs v7.4s, v7.4s \n" \
  71. "fabs v16.4s, v16.4s \n" \
  72. "fabs v17.4s, v17.4s \n" \
  73. "fabs v18.4s, v18.4s \n" \
  74. "fabs v19.4s, v19.4s \n" \
  75. "fabs v20.4s, v20.4s \n" \
  76. "fabs v21.4s, v21.4s \n" \
  77. "fabs v22.4s, v22.4s \n" \
  78. "fabs v23.4s, v23.4s \n" \
  79. "fabs v24.4s, v24.4s \n" \
  80. "fabs v25.4s, v25.4s \n" \
  81. "fmax v2.4s, v2.4s, v3.4s \n" \
  82. "fmax v4.4s, v4.4s, v5.4s \n" \
  83. "fmax v6.4s, v6.4s, v7.4s \n" \
  84. "fmax v16.4s, v16.4s, v17.4s \n" \
  85. "fmax v18.4s, v18.4s, v19.4s \n" \
  86. "fmax v20.4s, v20.4s, v21.4s \n" \
  87. "fmax v22.4s, v22.4s, v23.4s \n" \
  88. "fmax v24.4s, v24.4s, v25.4s \n" \
  89. "PRFM PLDL1KEEP, ["X", #1024] \n" \
  90. "PRFM PLDL1KEEP, ["X", #1024+64] \n" \
  91. "PRFM PLDL1KEEP, ["X", #1024+128] \n" \
  92. "PRFM PLDL1KEEP, ["X", #1024+192] \n" \
  93. "fmax v2.4s, v2.4s, v4.4s \n" \
  94. "fmax v6.4s, v6.4s, v16.4s \n" \
  95. "fmax v18.4s, v18.4s, v20.4s \n" \
  96. "fmax v22.4s, v22.4s, v24.4s \n" \
  97. "fmax v2.4s, v2.4s, v6.4s \n" \
  98. "fmax v18.4s, v18.4s, v22.4s \n" \
  99. "fmax v2.4s, v2.4s, v18.4s \n" \
  100. "fmaxv "TMPF0", v2.4s \n" \
  101. "fcmp "MAXF", "TMPF0" \n" \
  102. "fcsel "MAXF", "MAXF", "TMPF0", ge \n" \
  103. "csel "INDEX", "INDEX", "Z", ge \n" \
  104. "add "Z", "Z", #"N_KERNEL_SIZE" \n"
  105. #else
  106. #define KERNEL_F \
  107. "ldp q2, q3, ["X"] \n" \
  108. "ldp q4, q5, ["X", #32] \n" \
  109. "ldp q6, q7, ["X", #64] \n" \
  110. "ldp q16, q17, ["X", #96] \n" \
  111. "ldp q18, q19, ["X", #128] \n" \
  112. "ldp q20, q21, ["X", #160] \n" \
  113. "ldp q22, q23, ["X", #192] \n" \
  114. "ldp q24, q25, ["X", #224] \n" \
  115. "add "X", "X", #256 \n" \
  116. "fabs v2.2d, v2.2d \n" \
  117. "fabs v3.2d, v3.2d \n" \
  118. "fabs v4.2d, v4.2d \n" \
  119. "fabs v5.2d, v5.2d \n" \
  120. "fabs v6.2d, v6.2d \n" \
  121. "fabs v7.2d, v7.2d \n" \
  122. "fabs v16.2d, v16.2d \n" \
  123. "fabs v17.2d, v17.2d \n" \
  124. "fabs v18.2d, v18.2d \n" \
  125. "fabs v19.2d, v19.2d \n" \
  126. "fabs v20.2d, v20.2d \n" \
  127. "fabs v21.2d, v21.2d \n" \
  128. "fabs v22.2d, v22.2d \n" \
  129. "fabs v23.2d, v23.2d \n" \
  130. "fabs v24.2d, v24.2d \n" \
  131. "fabs v25.2d, v25.2d \n" \
  132. "fmax v2.2d, v2.2d, v3.2d \n" \
  133. "fmax v4.2d, v4.2d, v5.2d \n" \
  134. "fmax v6.2d, v6.2d, v7.2d \n" \
  135. "fmax v16.2d, v16.2d, v17.2d \n" \
  136. "fmax v18.2d, v18.2d, v19.2d \n" \
  137. "fmax v20.2d, v20.2d, v21.2d \n" \
  138. "fmax v22.2d, v22.2d, v23.2d \n" \
  139. "fmax v24.2d, v24.2d, v25.2d \n" \
  140. "PRFM PLDL1KEEP, ["X", #1024] \n" \
  141. "PRFM PLDL1KEEP, ["X", #1024+64] \n" \
  142. "PRFM PLDL1KEEP, ["X", #1024+128] \n" \
  143. "PRFM PLDL1KEEP, ["X", #1024+192] \n" \
  144. "fmax v2.2d, v2.2d, v4.2d \n" \
  145. "fmax v6.2d, v6.2d, v16.2d \n" \
  146. "fmax v18.2d, v18.2d, v20.2d \n" \
  147. "fmax v22.2d, v22.2d, v24.2d \n" \
  148. "fmax v2.2d, v2.2d, v6.2d \n" \
  149. "fmax v18.2d, v18.2d, v22.2d \n" \
  150. "fmax v2.2d, v2.2d, v18.2d \n" \
  151. "ins v3.d[0], v2.d[1] \n" \
  152. "fmax "TMPF0", d3, d2 \n" \
  153. "fcmp "MAXF", "TMPF0" \n" \
  154. "fcsel "MAXF", "MAXF", "TMPF0", ge \n" \
  155. "csel "INDEX", "INDEX", "Z", ge \n" \
  156. "add "Z", "Z", #"N_KERNEL_SIZE" \n"
  157. #endif
  158. #define KERNEL_F_FINALIZE \
  159. "sub x6, "INDEX", #1 \n" \
  160. "lsl x6, x6, #"INC_SHIFT" \n" \
  161. "add x7, x7, x6 \n" \
  162. "mov x6, #0 \n" \
  163. "1: \n" \
  164. "add x6, x6, #1 \n" \
  165. "cmp x6, #"N_KERNEL_SIZE" \n" \
  166. "bge 2f \n" \
  167. "ldr "TMPF1", [x7] \n" \
  168. "fabs "TMPF1", "TMPF1" \n" \
  169. "fcmp "MAXF", "TMPF1" \n" \
  170. "add x7, x7, #"SZ" \n" \
  171. "bne 1b \n" \
  172. "2: \n" \
  173. "sub x6, x6, #1 \n" \
  174. "add "INDEX", "INDEX", x6 \n"
  175. #define INIT \
  176. "lsl "INC_X", "INC_X", #"INC_SHIFT" \n" \
  177. "ldr "MAXF", ["X"] \n" \
  178. "add "X", "X", "INC_X" \n" \
  179. "mov "Z", #1 \n" \
  180. "mov "INDEX", "Z" \n" \
  181. "fabs "MAXF", "MAXF" \n"
  182. #define KERNEL_S1 \
  183. "ldr "TMPF0", ["X"] \n" \
  184. "add "X", "X", "INC_X" \n" \
  185. "add "Z", "Z", #1 \n" \
  186. "fabs "TMPF0", "TMPF0" \n" \
  187. "fcmp "MAXF", "TMPF0" \n" \
  188. "fcsel "MAXF", "MAXF", "TMPF0", ge \n" \
  189. "csel "INDEX", "INDEX", "Z", ge \n"
  190. #if defined(SMP)
  191. extern int blas_level1_thread_with_return_value(int mode, BLASLONG m, BLASLONG n,
  192. BLASLONG k, void *alpha, void *a, BLASLONG lda, void *b, BLASLONG ldb,
  193. void *c, BLASLONG ldc, int (*function)(), int nthreads);
  194. #endif
  195. static BLASLONG __attribute__((noinline)) iamax_compute(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  196. {
  197. BLASLONG index = 0;
  198. if ( n < 0 ) return index;
  199. __asm__ __volatile__ (
  200. " mov "N", %[N_] \n"
  201. " mov "X", %[X_] \n"
  202. " mov "INC_X", %[INCX_] \n"
  203. " cmp "N", xzr \n"
  204. " ble 10f //iamax_kernel_zero \n"
  205. " cmp "INC_X", xzr \n"
  206. " ble 10f //iamax_kernel_zero \n"
  207. " cmp "INC_X", #1 \n"
  208. " bne 5f //iamax_kernel_S_BEGIN \n"
  209. " mov x7, "X" \n"
  210. "1: //iamax_kernel_F_BEGIN: \n"
  211. " "INIT" \n"
  212. " subs "N", "N", #1 \n"
  213. " ble 9f //iamax_kernel_L999 \n"
  214. " asr "J", "N", #"N_DIV_SHIFT" \n"
  215. " cmp "J", xzr \n"
  216. " beq 3f //iamax_kernel_F1 \n"
  217. " add "Z", "Z", #1 \n"
  218. "2: //iamax_kernel_F: \n"
  219. " "KERNEL_F" \n"
  220. " subs "J", "J", #1 \n"
  221. " bne 2b //iamax_kernel_F \n"
  222. " "KERNEL_F_FINALIZE" \n"
  223. " sub "Z", "Z", #1 \n"
  224. "3: //iamax_kernel_F1: \n"
  225. " ands "J", "N", #"N_REM_MASK" \n"
  226. " ble 9f //iamax_kernel_L999 \n"
  227. "4: //iamax_kernel_F10: \n"
  228. " "KERNEL_S1" \n"
  229. " subs "J", "J", #1 \n"
  230. " bne 4b //iamax_kernel_F10 \n"
  231. " b 9f //iamax_kernel_L999 \n"
  232. "5: //iamax_kernel_S_BEGIN: \n"
  233. " "INIT" \n"
  234. " subs "N", "N", #1 \n"
  235. " ble 9f //iamax_kernel_L999 \n"
  236. " asr "J", "N", #2 \n"
  237. " cmp "J", xzr \n"
  238. " ble 7f //iamax_kernel_S1 \n"
  239. "6: //iamax_kernel_S4: \n"
  240. " "KERNEL_S1" \n"
  241. " "KERNEL_S1" \n"
  242. " "KERNEL_S1" \n"
  243. " "KERNEL_S1" \n"
  244. " subs "J", "J", #1 \n"
  245. " bne 6b //iamax_kernel_S4 \n"
  246. "7: //iamax_kernel_S1: \n"
  247. " ands "J", "N", #3 \n"
  248. " ble 9f //iamax_kernel_L999 \n"
  249. "8: //iamax_kernel_S10: \n"
  250. " "KERNEL_S1" \n"
  251. " subs "J", "J", #1 \n"
  252. " bne 8b //iamax_kernel_S10 \n"
  253. "9: //iamax_kernel_L999: \n"
  254. " mov x0, "INDEX" \n"
  255. " b 11f //iamax_kernel_DONE \n"
  256. "10: //iamax_kernel_zero: \n"
  257. " mov x0, xzr \n"
  258. "11: //iamax_kernel_DONE: \n"
  259. " mov %[INDEX_], "INDEX" \n"
  260. : [INDEX_] "=r" (index) //%0
  261. : [N_] "r" (n), //%1
  262. [X_] "r" (x), //%2
  263. [INCX_] "r" (inc_x) //%3
  264. : "cc",
  265. "memory",
  266. "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
  267. "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7"
  268. );
  269. return index;
  270. }
  271. #if defined(SMP)
  272. static int iamax_thread_function(BLASLONG n, BLASLONG dummy0,
  273. BLASLONG dummy1, FLOAT dummy2, FLOAT *x, BLASLONG inc_x, FLOAT *y,
  274. BLASLONG inc_y, FLOAT *result, BLASLONG dummy3)
  275. {
  276. *(BLASLONG *)result = iamax_compute(n, x, inc_x);
  277. return 0;
  278. }
  279. #endif
  280. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  281. {
  282. #if defined(SMP)
  283. int nthreads;
  284. FLOAT dummy_alpha;
  285. #endif
  286. BLASLONG max_index = 0;
  287. #if defined(SMP)
  288. if (inc_x == 0 || n <= 10000)
  289. nthreads = 1;
  290. else
  291. nthreads = num_cpu_avail(1);
  292. if (nthreads == 1) {
  293. max_index = iamax_compute(n, x, inc_x);
  294. } else {
  295. BLASLONG i, width, cur_index;
  296. int num_cpu;
  297. int mode;
  298. char result[MAX_CPU_NUMBER * sizeof(double) * 2];
  299. FLOAT max = -1.0;
  300. #if !defined(DOUBLE)
  301. mode = BLAS_SINGLE;
  302. #else
  303. mode = BLAS_DOUBLE;
  304. #endif
  305. blas_level1_thread_with_return_value(mode, n, 0, 0, &dummy_alpha,
  306. x, inc_x, NULL, 0, result, 0,
  307. ( void *)iamax_thread_function, nthreads);
  308. num_cpu = 0;
  309. i = n;
  310. cur_index = 0;
  311. while (i > 0) {
  312. FLOAT elem;
  313. BLASLONG cur_max_index;
  314. cur_max_index = *(BLASLONG *)&result[num_cpu * sizeof(double) * 2];
  315. elem = x[((cur_index + cur_max_index - 1) * inc_x)];
  316. elem = fabs(elem);
  317. if (elem >= max) {
  318. max = elem;
  319. max_index = cur_index + cur_max_index;
  320. }
  321. width = blas_quickdivide(i + nthreads - num_cpu - 1,
  322. nthreads - num_cpu);
  323. i -= width;
  324. cur_index += width;
  325. num_cpu ++;
  326. }
  327. }
  328. #else
  329. max_index = iamax_compute(n, x, inc_x);
  330. #endif
  331. return max_index;
  332. }