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.

isamin.c 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /***************************************************************************
  2. Copyright (c) 2013-2019, 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 minimum index
  37. * Warning: requirements n>0 and n % 64 == 0
  38. * @param n
  39. * @param x pointer to the vector
  40. * @param minf (out) minimum absolute value .( only for output )
  41. * @return index
  42. */
  43. static BLASLONG siamin_kernel_64(BLASLONG n, FLOAT *x, FLOAT *minf) {
  44. BLASLONG index;
  45. BLASLONG i=0;
  46. register __vector unsigned int static_index0 = {0,1,2,3};
  47. register __vector unsigned int temp0 = {4,4,4, 4}; //temporary vector register
  48. register __vector unsigned int temp1= temp0<<1; //{8,8,8,8}
  49. register __vector unsigned int static_index1=static_index0 +temp0;//{4,5,6,7};
  50. register __vector unsigned int static_index2=static_index0 +temp1;//{8,9,10,11};
  51. register __vector unsigned int static_index3=static_index1 +temp1; //{12,13,14,15};
  52. temp0=vec_xor(temp0,temp0);
  53. temp1=temp1 <<1 ; //{16,16,16,16}
  54. register __vector unsigned int quadruple_indices=static_index0;//{0,1,2,3};
  55. register __vector float * v_ptrx=(__vector float *)x;
  56. register __vector float quadruple_values=vec_abs(v_ptrx[0]);
  57. for(; i<n; i+=64){
  58. //absolute temporary vectors
  59. register __vector float v0=vec_abs(v_ptrx[0]);
  60. register __vector float v1=vec_abs(v_ptrx[1]);
  61. register __vector float v2=vec_abs(v_ptrx[2]);
  62. register __vector float v3=vec_abs(v_ptrx[3]);
  63. register __vector float v4=vec_abs(v_ptrx[4]);
  64. register __vector float v5=vec_abs(v_ptrx[5]);
  65. register __vector float v6=vec_abs(v_ptrx[6]);
  66. register __vector float v7=vec_abs(v_ptrx[7]);
  67. //cmp quadruple pairs
  68. register __vector bool int r1=vec_cmpgt(v0,v1);
  69. register __vector bool int r2=vec_cmpgt(v2,v3);
  70. register __vector bool int r3=vec_cmpgt(v4,v5);
  71. register __vector bool int r4=vec_cmpgt(v6,v7);
  72. //select
  73. register __vector unsigned int ind0_first= vec_sel(static_index0,static_index1,r1);
  74. register __vector float vf0= vec_sel(v0,v1,r1);
  75. register __vector unsigned int ind1= vec_sel(static_index2,static_index3,r2);
  76. register __vector float vf1= vec_sel(v2,v3,r2);
  77. register __vector unsigned int ind2= vec_sel(static_index0,static_index1,r3);
  78. v0=vec_sel(v4,v5,r3);
  79. register __vector unsigned int ind3= vec_sel(static_index2,static_index3,r4);
  80. v1=vec_sel(v6,v7,r4);
  81. // cmp selected
  82. r1=vec_cmpgt(vf0,vf1);
  83. r2=vec_cmpgt(v0,v1);
  84. v_ptrx+=8;
  85. //select from above
  86. ind0_first= vec_sel(ind0_first,ind1,r1);
  87. vf0= vec_sel(vf0,vf1,r1) ;
  88. ind2= vec_sel(ind2,ind3,r2);
  89. vf1= vec_sel(v0,v1,r2);
  90. //second indices actually should be within [16,31] so ind2+16
  91. ind2 +=temp1;
  92. //final cmp and select index and value for the first 32 values
  93. r1=vec_cmpgt(vf0,vf1);
  94. ind0_first = vec_sel(ind0_first,ind2,r1);
  95. vf0= vec_sel(vf0,vf1,r1);
  96. ind0_first+=temp0; //get absolute index
  97. temp0+=temp1;
  98. temp0+=temp1; //temp0+32
  99. //second part of 32
  100. // absolute temporary vectors
  101. v0=vec_abs(v_ptrx[0]);
  102. v1=vec_abs(v_ptrx[1]);
  103. v2=vec_abs(v_ptrx[2]);
  104. v3=vec_abs(v_ptrx[3]);
  105. v4=vec_abs(v_ptrx[4]);
  106. v5=vec_abs(v_ptrx[5]);
  107. v6=vec_abs(v_ptrx[6]);
  108. v7=vec_abs(v_ptrx[7]);
  109. //cmp quadruple pairs
  110. r1=vec_cmpgt(v0,v1);
  111. r2=vec_cmpgt(v2,v3);
  112. r3=vec_cmpgt(v4,v5);
  113. r4=vec_cmpgt(v6,v7);
  114. //select
  115. register __vector unsigned int ind0_second= vec_sel(static_index0,static_index1,r1);
  116. register __vector float vv0= vec_sel(v0,v1,r1);
  117. ind1= vec_sel(static_index2,static_index3,r2);
  118. register __vector float vv1= vec_sel(v2,v3,r2);
  119. ind2= vec_sel(static_index0,static_index1,r3);
  120. v0=vec_sel(v4,v5,r3);
  121. ind3= vec_sel(static_index2,static_index3,r4);
  122. v1=vec_sel(v6,v7,r4);
  123. // cmp selected
  124. r1=vec_cmpgt(vv0,vv1);
  125. r2=vec_cmpgt(v0,v1);
  126. v_ptrx+=8;
  127. //select from above
  128. ind0_second= vec_sel(ind0_second,ind1,r1);
  129. vv0= vec_sel(vv0,vv1,r1) ;
  130. ind2= vec_sel(ind2,ind3,r2);
  131. vv1= vec_sel(v0,v1,r2) ;
  132. //second indices actually should be within [16,31] so ind2+16
  133. ind2 +=temp1;
  134. //final cmp and select index and value for the second 32 values
  135. r1=vec_cmpgt(vv0,vv1);
  136. ind0_second = vec_sel(ind0_second,ind2,r1);
  137. vv0= vec_sel(vv0,vv1,r1);
  138. ind0_second+=temp0; //get absolute index
  139. //find final quadruple from 64 elements
  140. r2=vec_cmpgt(vf0,vv0);
  141. ind2 = vec_sel( ind0_first,ind0_second,r2);
  142. vv0= vec_sel(vf0,vv0,r2);
  143. //compare with old quadruple and update
  144. r3=vec_cmpgt( quadruple_values,vv0);
  145. quadruple_indices = vec_sel( quadruple_indices,ind2,r3);
  146. quadruple_values= vec_sel(quadruple_values,vv0,r3);
  147. temp0+=temp1;
  148. temp0+=temp1; //temp0+32
  149. }
  150. //now we have to chose from 4 values and 4 different indices
  151. // we will compare pairwise if pairs are exactly the same we will choose minimum between index
  152. // otherwise we will assign index of the minimum value
  153. float a1,a2,a3,a4;
  154. unsigned int i1,i2,i3,i4;
  155. a1=vec_extract(quadruple_values,0);
  156. a2=vec_extract(quadruple_values,1);
  157. a3=vec_extract(quadruple_values,2);
  158. a4=vec_extract(quadruple_values,3);
  159. i1=vec_extract(quadruple_indices,0);
  160. i2=vec_extract(quadruple_indices,1);
  161. i3=vec_extract(quadruple_indices,2);
  162. i4=vec_extract(quadruple_indices,3);
  163. if(a1==a2){
  164. index=i1>i2?i2:i1;
  165. }else if(a2<a1){
  166. index=i2;
  167. a1=a2;
  168. }else{
  169. index= i1;
  170. }
  171. if(a4==a3){
  172. i1=i3>i4?i4:i3;
  173. }else if(a4<a3){
  174. i1=i4;
  175. a3=a4;
  176. }else{
  177. i1= i3;
  178. }
  179. if(a1==a3){
  180. index=i1>index?index:i1;
  181. *minf=a1;
  182. }else if(a3<a1){
  183. index=i1;
  184. *minf=a3;
  185. }else{
  186. *minf=a1;
  187. }
  188. return index;
  189. }
  190. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x) {
  191. BLASLONG i = 0;
  192. BLASLONG j = 0;
  193. BLASLONG min = 0;
  194. FLOAT minf = 0.0;
  195. if (n <= 0 || inc_x <= 0) return (min);
  196. minf = ABS(x[0]); //index's not incremented
  197. if (inc_x == 1) {
  198. BLASLONG n1 = n & -64;
  199. if (n1 > 0) {
  200. min = siamin_kernel_64(n1, x, &minf);
  201. i = n1;
  202. }
  203. while (i < n) {
  204. if (ABS(x[i]) < minf) {
  205. min = i;
  206. minf = ABS(x[i]);
  207. }
  208. i++;
  209. }
  210. return (min + 1);
  211. } else {
  212. BLASLONG n1 = n & -4;
  213. while (j < n1) {
  214. if (ABS(x[i]) < minf) {
  215. min = j;
  216. minf = ABS(x[i]);
  217. }
  218. if (ABS(x[i + inc_x]) < minf) {
  219. min = j + 1;
  220. minf = ABS(x[i + inc_x]);
  221. }
  222. if (ABS(x[i + 2 * inc_x]) < minf) {
  223. min = j + 2;
  224. minf = ABS(x[i + 2 * inc_x]);
  225. }
  226. if (ABS(x[i + 3 * inc_x]) < minf) {
  227. min = j + 3;
  228. minf = ABS(x[i + 3 * inc_x]);
  229. }
  230. i += inc_x * 4;
  231. j += 4;
  232. }
  233. while (j < n) {
  234. if (ABS(x[i]) < minf) {
  235. min = j;
  236. minf = ABS(x[i]);
  237. }
  238. i += inc_x;
  239. j++;
  240. }
  241. return (min + 1);
  242. }
  243. }