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.

idamax.c 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /***************************************************************************
  2. Copyright (c) 2013-2018, 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 <math.h>
  29. #if defined(__VEC__) || defined(__ALTIVEC__)
  30. #include <altivec.h>
  31. #endif
  32. #if defined(DOUBLE)
  33. #define ABS fabs
  34. #else
  35. #define ABS fabsf
  36. #endif
  37. #if defined(__VEC__) || defined(__ALTIVEC__)
  38. /**
  39. * Find maximum index
  40. * Warning: requirements n>0 and n % 32 == 0
  41. * @param n
  42. * @param x pointer to the vector
  43. * @param maxf (out) maximum absolute value .( only for output )
  44. * @return index
  45. */
  46. static BLASLONG diamax_kernel_32(BLASLONG n, FLOAT *x, FLOAT *maxf) {
  47. BLASLONG index;
  48. register __vector long long start = {1,0};
  49. register __vector long long temp_add_index = {2, 2};
  50. __asm__(
  51. "lxvd2x 44, 0,%[ptr_tmp] \n\t"
  52. "lxvd2x 45, %[i16],%[ptr_tmp] \n\t"
  53. "lxvd2x 46, %[i32],%[ptr_tmp] \n\t"
  54. "lxvd2x 47, %[i48],%[ptr_tmp] \n\t"
  55. "lxvd2x 48, %[i64],%[ptr_tmp] \n\t"
  56. "lxvd2x 49, %[i80],%[ptr_tmp] \n\t"
  57. "lxvd2x 6, %[i96],%[ptr_tmp] \n\t"
  58. "lxvd2x 7,%[i112],%[ptr_tmp] \n\t"
  59. "xxlor 40,%x[start],%x[start] \n\t" //{ 1,0} vs40 | v8
  60. "vaddudm 9,8,%[adder] \n\t" //{3,2} vs41
  61. "xxlxor 37,37 ,37 \n\t" //v5 v37 index_count
  62. "vaddudm 10,9,%[adder] \n\t" //{5,4} vs42
  63. "xxlxor 38 ,38 ,38 \n\t" // v6 | vs38 vec_max_index
  64. "vaddudm 11,10,%[adder] \n\t" //{7,6} vs43
  65. "xxlxor 39,39,39 \n\t" // vs39 vec_max_value
  66. "vaddudm 4,11, %[adder] \n\t" // {9,8} -{8;8} vs36 | v4
  67. XXSPLTD_S(36,36,0)
  68. "xvabsdp 44, 44 \n\t"
  69. "xvabsdp 45, 45 \n\t"
  70. "xvabsdp 46, 46 \n\t"
  71. "xvabsdp 47, 47 \n\t"
  72. "xvabsdp 48, 48 \n\t"
  73. "xvabsdp 49, 49 \n\t"
  74. "xvabsdp 6, 6 \n\t"
  75. "xvabsdp 7, 7 \n\t"
  76. //jump first half forward
  77. "b two%= \n\t"
  78. //===================================================================
  79. ".align 5 \n\t"
  80. "one%=: \n\t"
  81. "xvcmpgtdp 2,45,44 \n\t "
  82. "xvcmpgtdp 3,47,46 \n\t "
  83. "xvcmpgtdp 4,49,48 \n\t "
  84. "xvcmpgtdp 5,7,6 \n\t"
  85. "xxsel 32,40,41,2 \n\t"
  86. "xxsel 0,44,45,2 \n\t"
  87. "xxsel 33,42,43,3 \n\t"
  88. "xxsel 1,46,47,3 \n\t"
  89. "xxsel 34,40,41,4 \n\t"
  90. "xxsel 45,48,49,4 \n\t"
  91. "xxsel 35,42,43,5 \n\t"
  92. "xxsel 47,6,7,5 \n\t"
  93. "xvcmpgtdp 2, 1,0 \n\t"
  94. "xvcmpgtdp 3,47, 45 \n\t"
  95. "addi %[ptr_tmp] ,%[ptr_tmp] , 128 \n\t"
  96. "xxsel 32,32,33,2 \n\t"
  97. "xxsel 0 ,0,1,2 \n\t"
  98. "xxsel 34,34,35,3 \n\t"
  99. "xxsel 5,45,47,3 \n\t"
  100. //load next 64
  101. "lxvd2x 44, 0,%[ptr_tmp] \n\t"
  102. "lxvd2x 45, %[i16],%[ptr_tmp] \n\t"
  103. // for {second 8 elements } we have to add 8 to each so that it became {from 8 to 16}
  104. "vaddudm 2,2,4 \n\t" // vs34=vs34 + vs36{8,8}
  105. "lxvd2x 46, %[i32],%[ptr_tmp] \n\t"
  106. "lxvd2x 47, %[i48],%[ptr_tmp] \n\t"
  107. //choose bigger from first and second part
  108. "xvcmpgtdp 4,5 , 0 \n\t"
  109. "xxsel 3, 0,5,4 \n\t"
  110. "xxsel 33,32,34,4 \n\t"
  111. //load next 64
  112. "lxvd2x 48, %[i64],%[ptr_tmp] \n\t"
  113. "lxvd2x 49, %[i80],%[ptr_tmp] \n\t"
  114. "vaddudm 1,1,5 \n\t" // get real index for first bigger
  115. "lxvd2x 6, %[i96],%[ptr_tmp] \n\t"
  116. "lxvd2x 7,%[i112],%[ptr_tmp] \n\t"
  117. //compare with previous to get vec_max_index(v6 | vs38 ) and vec_max_value (vs39)
  118. "xvcmpgtdp 2, 3,39 \n\t"
  119. "xxsel 39,39,3,2 \n\t"
  120. "xxsel 38,38,33,2 \n\t"
  121. //update index += 8
  122. "vaddudm 5,5,4 \n\t"
  123. "xvabsdp 44, 44 \n\t"
  124. "xvabsdp 45, 45 \n\t"
  125. "xvabsdp 46, 46 \n\t"
  126. "xvabsdp 47, 47 \n\t"
  127. //update index += 8
  128. "vaddudm 5,5,4 \n\t"
  129. "xvabsdp 48, 48 \n\t"
  130. "xvabsdp 49, 49 \n\t"
  131. "xvabsdp 6, 6 \n\t"
  132. "xvabsdp 7, 7 \n\t"
  133. //<-----------jump here from first load
  134. "two%=: \n\t"
  135. "xvcmpgtdp 2,45,44 \n\t "
  136. "xvcmpgtdp 3,47,46 \n\t "
  137. "xvcmpgtdp 4,49,48 \n\t "
  138. "xvcmpgtdp 5,7,6 \n\t"
  139. "xxsel 32,40,41,2 \n\t"
  140. "xxsel 0,44,45,2 \n\t"
  141. "xxsel 33,42,43,3 \n\t"
  142. "xxsel 1,46,47,3 \n\t"
  143. "xxsel 34,40,41,4 \n\t"
  144. "xxsel 45,48,49,4 \n\t"
  145. "xxsel 35,42,43,5 \n\t"
  146. "xxsel 47,6,7,5 \n\t"
  147. "xvcmpgtdp 2, 1,0 \n\t"
  148. "xvcmpgtdp 3,47, 45 \n\t"
  149. "xxsel 32,32,33,2 \n\t"
  150. "xxsel 0 ,0,1,2 \n\t"
  151. "xxsel 34,34,35,3 \n\t"
  152. "xxsel 5,45,47,3 \n\t"
  153. "addi %[ptr_tmp] ,%[ptr_tmp] , 128 \n\t"
  154. // for {second 8 elements } we have to add 8 to each so that it became {from 8 to 16}
  155. "vaddudm 2,2,4 \n\t" // vs34=vs34 + vs36{8,8}
  156. //load next 64
  157. "lxvd2x 44, 0,%[ptr_tmp] \n\t"
  158. "lxvd2x 45, %[i16],%[ptr_tmp] \n\t"
  159. "lxvd2x 46, %[i32],%[ptr_tmp] \n\t"
  160. "lxvd2x 47, %[i48],%[ptr_tmp] \n\t"
  161. //choose bigger from first and second part
  162. "xvcmpgtdp 4,5 , 0 \n\t"
  163. "xxsel 3, 0,5,4 \n\t"
  164. "xxsel 33,32,34,4 \n\t"
  165. //load next 64
  166. "lxvd2x 48, %[i64],%[ptr_tmp] \n\t"
  167. "lxvd2x 49, %[i80],%[ptr_tmp] \n\t"
  168. "vaddudm 1,1,5 \n\t" // get real index for first bigger
  169. "lxvd2x 6, %[i96],%[ptr_tmp] \n\t"
  170. "lxvd2x 7,%[i112],%[ptr_tmp] \n\t"
  171. //compare with previous to get vec_max_index(v6 | vs38 ) and vec_max_value (vs39)
  172. "xvcmpgtdp 2, 3,39 \n\t"
  173. "xxsel 39,39,3,2 \n\t"
  174. "xxsel 38,38,33,2 \n\t"
  175. //update index += 8
  176. "vaddudm 5,5,4 \n\t"
  177. "xvabsdp 44, 44 \n\t"
  178. "xvabsdp 45, 45 \n\t"
  179. "xvabsdp 46, 46 \n\t"
  180. "xvabsdp 47, 47 \n\t"
  181. //update index += 8
  182. "vaddudm 5,5,4 \n\t"
  183. "xvabsdp 48, 48 \n\t"
  184. "xvabsdp 49, 49 \n\t"
  185. "xvabsdp 6, 6 \n\t"
  186. "xvabsdp 7, 7 \n\t"
  187. //decrement n
  188. "addic. %[n], %[n], -32 \n\t"
  189. //Loop back if >0
  190. "bgt+ one%= \n\t"
  191. //==============================================================================
  192. "xvcmpgtdp 2,45,44 \n\t "
  193. "xvcmpgtdp 3,47,46 \n\t "
  194. "xvcmpgtdp 4,49,48 \n\t "
  195. "xvcmpgtdp 5,7,6 \n\t"
  196. "xxsel 32,40,41,2 \n\t"
  197. "xxsel 0,44,45,2 \n\t"
  198. "xxsel 33,42,43,3 \n\t"
  199. "xxsel 1,46,47,3 \n\t"
  200. "xxsel 34,40,41,4 \n\t"
  201. "xxsel 45,48,49,4 \n\t"
  202. "xxsel 35,42,43,5 \n\t"
  203. "xxsel 47,6,7,5 \n\t"
  204. "xvcmpgtdp 2, 1,0 \n\t"
  205. "xvcmpgtdp 3,47, 45 \n\t"
  206. "xxsel 32,32,33,2 \n\t"
  207. "xxsel 0 ,0,1,2 \n\t"
  208. "xxsel 34,34,35,3 \n\t"
  209. "xxsel 5,45,47,3 \n\t"
  210. // for {second 8 elements } we have to add 8 to each so that it became {from 8 to 16}
  211. "vaddudm 2,2,4 \n\t" // vs34=vs34 + vs36{8,8}
  212. //choose bigger from first and second part
  213. "xvcmpgtdp 4,5 , 0 \n\t"
  214. "xxsel 3, 0,5,4 \n\t"
  215. "xxsel 33,32,34,4 \n\t"
  216. "vaddudm 1,1,5 \n\t" // get real index for first bigger
  217. //compare with previous to get vec_max_index(v6 | vs38 ) and vec_max_value (vs39)
  218. "xvcmpgtdp 2, 3,39 \n\t"
  219. "xxsel 39,39,3,2 \n\t"
  220. "xxsel 38,38,33,2 \n\t"
  221. ///////extract max value and max index from vector
  222. XXSPLTD_S(32,38,1)
  223. XXSPLTD_S(40,39,1)
  224. "xvcmpeqdp. 2, 40,39 \n\t"
  225. //cr6 0 bit set if all true, cr6=4*6+bit_ind=24,0011at CR(BI)==1, at=10 hint that it occurs rarely
  226. //0b001110=14
  227. "bc 14,24, three%= \n\t"
  228. "xvcmpgtdp 4, 40,39 \n\t"
  229. "xxsel 0,39,40,4 \n\t"
  230. "xxsel 1,38,32,4 \n\t"
  231. "stxsdx 0,0,%[ptr_maxf] \n\t"
  232. "b four%= \n\t"
  233. "three%=: \n\t"
  234. //if elements value are equal then choose minimum index
  235. XXSPLTD_S(0,40,0)
  236. "vminud 0,0,6 \n\t" //vs32 vs38
  237. "xxlor 1,32,32 \n\t"
  238. "stxsdx 0,0,%[ptr_maxf] \n\t"
  239. "four%=: \n\t"
  240. "mfvsrd %[index],1 \n\t"
  241. : [maxf] "=m"(*maxf),[ptr_tmp] "+&b"(x),[index] "=r"(index), [n] "+&r"(n)
  242. : [mem] "m"(*(const double (*)[n])x), [ptr_x] "b"(x), [ptr_maxf] "b"(maxf) ,
  243. [i16] "b"(16), [i32] "b"(32), [i48] "b"(48),
  244. [i64] "b"(64), [i80] "b"(80), [i96] "b"(96), [i112] "b"(112),
  245. [start] "v"(start), [adder] "v"(temp_add_index)
  246. : "cc", "vs0", "vs1","vs2","vs3", "vs4","vs5","vs32", "vs33", "vs34", "vs35", "vs36",
  247. "vs37", "vs38", "vs39", "vs40", "vs41", "vs42", "vs43", "vs44", "vs45", "vs46", "vs47", "vs48", "vs49", "vs6", "vs7"
  248. );
  249. return index;
  250. }
  251. #endif
  252. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x) {
  253. BLASLONG i = 0;
  254. BLASLONG j = 0;
  255. FLOAT maxf = 0.0;
  256. BLASLONG max = 0;
  257. if (n <= 0 || inc_x <= 0) return (max);
  258. if (inc_x == 1) {
  259. #if defined(_CALL_ELF) && (_CALL_ELF == 2)
  260. #if defined(__VEC__) || defined(__ALTIVEC__)
  261. BLASLONG n1 = n & -32;
  262. if (n1 > 0) {
  263. max = diamax_kernel_32(n1, x, &maxf);
  264. i = n1;
  265. }
  266. #endif
  267. #endif
  268. while (i < n) {
  269. if (ABS(x[i]) > maxf) {
  270. max = i;
  271. maxf = ABS(x[i]);
  272. }
  273. i++;
  274. }
  275. return (max + 1);
  276. } else {
  277. BLASLONG n1 = n & -4;
  278. while (j < n1) {
  279. if (ABS(x[i]) > maxf) {
  280. max = j;
  281. maxf = ABS(x[i]);
  282. }
  283. if (ABS(x[i + inc_x]) > maxf) {
  284. max = j + 1;
  285. maxf = ABS(x[i + inc_x]);
  286. }
  287. if (ABS(x[i + 2 * inc_x]) > maxf) {
  288. max = j + 2;
  289. maxf = ABS(x[i + 2 * inc_x]);
  290. }
  291. if (ABS(x[i + 3 * inc_x]) > maxf) {
  292. max = j + 3;
  293. maxf = ABS(x[i + 3 * inc_x]);
  294. }
  295. i += inc_x * 4;
  296. j += 4;
  297. }
  298. while (j < n) {
  299. if (ABS(x[i]) > maxf) {
  300. max = j;
  301. maxf = ABS(x[i]);
  302. }
  303. i += inc_x;
  304. j++;
  305. }
  306. return (max + 1);
  307. }
  308. }