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.

izamin.c 11 kB

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