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.

icamax.c 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #define USE_MASK_PERMUTATIONS 1 //with this type of permutation gcc output a little faster code
  37. #if !defined(USE_MASK_PERMUTATIONS)
  38. static inline __attribute__((always_inline)) __vector float mvec_mergee(__vector float a,__vector float b ){
  39. __vector float result;
  40. __asm__ (
  41. "vmrgew %0,%1,%2;\n"
  42. : "=v" (result)
  43. : "v" (a),
  44. "v" (b)
  45. : );
  46. return result;
  47. }
  48. static inline __attribute__((always_inline)) __vector float mvec_mergeo(__vector float a,__vector float b ){
  49. __vector float result;
  50. __asm__ (
  51. "vmrgow %0,%1,%2;\n"
  52. : "=v" (result)
  53. : "v" (a),
  54. "v" (b)
  55. : );
  56. return result;
  57. }
  58. #endif
  59. /**
  60. * Find maximum index
  61. * Warning: requirements n>0 and n % 32 == 0
  62. * @param n
  63. * @param x pointer to the vector
  64. * @param maxf (out) maximum absolute value .( only for output )
  65. * @return index
  66. */
  67. static BLASLONG ciamax_kernel_32(BLASLONG n, FLOAT *x, FLOAT *maxf) {
  68. BLASLONG index;
  69. BLASLONG i;
  70. #if defined(USE_MASK_PERMUTATIONS)
  71. register __vector unsigned int static_index0 = {0,1,2,3};
  72. #else
  73. register __vector unsigned int static_index0 = {2,0,3,1};
  74. #endif
  75. register __vector unsigned int temp0 = {4,4,4, 4}; //temporary vector register
  76. register __vector unsigned int temp1= temp0<<1; //{8,8,8,8}
  77. register __vector unsigned int static_index1=static_index0 +temp0;
  78. register __vector unsigned int static_index2=static_index0 +temp1;
  79. register __vector unsigned int static_index3=static_index1 +temp1;
  80. temp0=vec_xor(temp0,temp0);
  81. temp1=temp1 <<1 ; //{16,16,16,16}
  82. register __vector unsigned int temp_add=temp1 <<1; //{32,32,32,32}
  83. register __vector unsigned int quadruple_indices=temp0;//{0,0,0,0}
  84. register __vector float quadruple_values={0,0,0,0};
  85. register __vector float * v_ptrx=(__vector float *)x;
  86. #if defined(USE_MASK_PERMUTATIONS)
  87. register __vector unsigned char real_pack_mask = { 0,1,2,3,8,9,10,11,16,17,18,19, 24,25,26,27};
  88. register __vector unsigned char image_pack_mask= {4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31};
  89. #endif
  90. for(; i<n; i+=32 ){
  91. //absolute temporary complex vectors
  92. register __vector float v0=vec_abs(v_ptrx[0]);
  93. register __vector float v1=vec_abs(v_ptrx[1]);
  94. register __vector float v2=vec_abs(v_ptrx[2]);
  95. register __vector float v3=vec_abs(v_ptrx[3]);
  96. register __vector float v4=vec_abs(v_ptrx[4]);
  97. register __vector float v5=vec_abs(v_ptrx[5]);
  98. register __vector float v6=vec_abs(v_ptrx[6]);
  99. register __vector float v7=vec_abs(v_ptrx[7]);
  100. //pack complex real and imaginary parts together to sum real+image
  101. #if defined(USE_MASK_PERMUTATIONS)
  102. register __vector float t1=vec_perm(v0,v1,real_pack_mask);
  103. register __vector float ti=vec_perm(v0,v1,image_pack_mask);
  104. v0=t1+ti; //sum quadruple real with quadruple image
  105. register __vector float t2=vec_perm(v2,v3,real_pack_mask);
  106. register __vector float ti2=vec_perm(v2,v3,image_pack_mask);
  107. v1=t2+ti2;
  108. t1=vec_perm(v4,v5,real_pack_mask);
  109. ti=vec_perm(v4,v5,image_pack_mask);
  110. v2=t1+ti; //sum
  111. t2=vec_perm(v6,v7,real_pack_mask);
  112. ti2=vec_perm(v6,v7,image_pack_mask);
  113. v3=t2+ti2;
  114. #else
  115. register __vector float t1=mvec_mergee(v0,v1);
  116. register __vector float ti=mvec_mergeo(v0,v1);
  117. v0=t1+ti; //sum quadruple real with quadruple image
  118. register __vector float t2= mvec_mergee(v2,v3);
  119. register __vector float ti2=mvec_mergeo(v2,v3);
  120. v1=t2+ti2;
  121. t1=mvec_mergee(v4,v5);
  122. ti=mvec_mergeo(v4,v5);
  123. v2=t1+ti; //sum
  124. t2=mvec_mergee(v6,v7);
  125. ti2=mvec_mergeo(v6,v7);
  126. v3=t2+ti2;
  127. #endif
  128. // now we have 16 summed elements . lets compare them
  129. v_ptrx+=8;
  130. register __vector bool int r1=vec_cmpgt(v1,v0);
  131. register __vector bool int r2=vec_cmpgt(v3,v2);
  132. register __vector unsigned int ind2= vec_sel(static_index0,static_index1,r1);
  133. v0=vec_sel(v0,v1,r1);
  134. register __vector unsigned int ind3= vec_sel(static_index2,static_index3,r2);
  135. v1=vec_sel(v2,v3,r2);
  136. //final cmp and select index and value for first 16 values
  137. r1=vec_cmpgt(v1,v0);
  138. register __vector unsigned int indf0 = vec_sel(ind2,ind3,r1);
  139. register __vector float vf0= vec_sel(v0,v1,r1);
  140. //absolute temporary complex vectors
  141. v0=vec_abs(v_ptrx[0]);
  142. v1=vec_abs(v_ptrx[1]);
  143. v2=vec_abs(v_ptrx[2]);
  144. v3=vec_abs(v_ptrx[3]);
  145. v4=vec_abs(v_ptrx[4]);
  146. v5=vec_abs(v_ptrx[5]);
  147. v6=vec_abs(v_ptrx[6]);
  148. v7=vec_abs(v_ptrx[7]);
  149. //pack complex real and imaginary parts together to sum real+image
  150. #if defined(USE_MASK_PERMUTATIONS)
  151. t1=vec_perm(v0,v1,real_pack_mask);
  152. ti=vec_perm(v0,v1,image_pack_mask);
  153. v0=t1+ti; //sum quadruple real with quadruple image
  154. t2=vec_perm(v2,v3,real_pack_mask);
  155. ti2=vec_perm(v2,v3,image_pack_mask);
  156. v1=t2+ti2;
  157. t1=vec_perm(v4,v5,real_pack_mask);
  158. ti=vec_perm(v4,v5,image_pack_mask);
  159. v2=t1+ti; //sum
  160. t2=vec_perm(v6,v7,real_pack_mask);
  161. ti2=vec_perm(v6,v7,image_pack_mask);
  162. v3=t2+ti2;
  163. #else
  164. t1=mvec_mergee(v0,v1);
  165. ti=mvec_mergeo(v0,v1);
  166. v0=t1+ti; //sum quadruple real with quadruple image
  167. t2=mvec_mergee(v2,v3);
  168. ti2=mvec_mergeo(v2,v3);
  169. v1=t2+ti2;
  170. t1=mvec_mergee(v4,v5);
  171. ti=mvec_mergeo(v4,v5);
  172. v2=t1+ti; //sum
  173. t2=mvec_mergee(v6,v7);
  174. ti2=mvec_mergeo(v6,v7);
  175. v3=t2+ti2;
  176. #endif
  177. // now we have 16 summed elements {from 16 to 31} . lets compare them
  178. v_ptrx+=8;
  179. r1=vec_cmpgt(v1,v0);
  180. r2=vec_cmpgt(v3,v2);
  181. ind2= vec_sel(static_index0,static_index1,r1);
  182. v0=vec_sel(v0,v1,r1);
  183. ind3= vec_sel(static_index2,static_index3,r2);
  184. v1=vec_sel(v2,v3,r2);
  185. //final cmp and select index and value for the second 16 values
  186. r1=vec_cmpgt(v1,v0);
  187. register __vector unsigned int indv0 = vec_sel(ind2,ind3,r1);
  188. register __vector float vv0= vec_sel(v0,v1,r1);
  189. indv0+=temp1; //make index from 16->31
  190. //find final quadruple from 32 elements
  191. r2=vec_cmpgt(vv0,vf0);
  192. ind2 = vec_sel( indf0,indv0,r2);
  193. vv0= vec_sel(vf0,vv0,r2);
  194. //get asbolute index
  195. ind2+=temp0;
  196. //compare with old quadruple and update
  197. r1=vec_cmpgt(vv0,quadruple_values);
  198. quadruple_indices = vec_sel( quadruple_indices,ind2,r1);
  199. quadruple_values= vec_sel(quadruple_values,vv0,r1);
  200. temp0+=temp_add;
  201. }
  202. //now we have to chose from 4 values and 4 different indices
  203. // we will compare pairwise if pairs are exactly the same we will choose minimum between index
  204. // otherwise we will assign index of the maximum value
  205. float a1,a2,a3,a4;
  206. unsigned int i1,i2,i3,i4;
  207. a1=vec_extract(quadruple_values,0);
  208. a2=vec_extract(quadruple_values,1);
  209. a3=vec_extract(quadruple_values,2);
  210. a4=vec_extract(quadruple_values,3);
  211. i1=vec_extract(quadruple_indices,0);
  212. i2=vec_extract(quadruple_indices,1);
  213. i3=vec_extract(quadruple_indices,2);
  214. i4=vec_extract(quadruple_indices,3);
  215. if(a1==a2){
  216. index=i1>i2?i2:i1;
  217. }else if(a2>a1){
  218. index=i2;
  219. a1=a2;
  220. }else{
  221. index= i1;
  222. }
  223. if(a4==a3){
  224. i1=i3>i4?i4:i3;
  225. }else if(a4>a3){
  226. i1=i4;
  227. a3=a4;
  228. }else{
  229. i1= i3;
  230. }
  231. if(a1==a3){
  232. index=i1>index?index:i1;
  233. *maxf=a1;
  234. }else if(a3>a1){
  235. index=i1;
  236. *maxf=a3;
  237. }else{
  238. *maxf=a1;
  239. }
  240. return index;
  241. }
  242. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  243. {
  244. BLASLONG i = 0;
  245. BLASLONG ix = 0;
  246. FLOAT maxf = 0;
  247. BLASLONG max = 0;
  248. BLASLONG inc_x2;
  249. if (n <= 0 || inc_x <= 0) return(max);
  250. if (inc_x == 1) {
  251. BLASLONG n1 = n & -32;
  252. if (n1 > 0) {
  253. max = ciamax_kernel_32(n1, x, &maxf);
  254. i = n1;
  255. ix = n1 << 1;
  256. }
  257. while(i < n)
  258. {
  259. if( CABS1(x,ix) > maxf )
  260. {
  261. max = i;
  262. maxf = CABS1(x,ix);
  263. }
  264. ix += 2;
  265. i++;
  266. }
  267. return (max + 1);
  268. } else {
  269. inc_x2 = 2 * inc_x;
  270. maxf = CABS1(x,0);
  271. ix += inc_x2;
  272. i++;
  273. while(i < n)
  274. {
  275. if( CABS1(x,ix) > maxf )
  276. {
  277. max = i;
  278. maxf = CABS1(x,ix);
  279. }
  280. ix += inc_x2;
  281. i++;
  282. }
  283. return (max + 1);
  284. }
  285. }