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.

icamin.c 9.0 kB

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