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

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