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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /***************************************************************************
  2. Copyright (c) 2015, 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. #if (defined(__GNUC__) && __GNUC__ > 11)
  29. #pragma GCC optimize("no-tree-vectorize")
  30. #endif
  31. #if defined(BULLDOZER)
  32. #include "zdot_microk_bulldozer-2.c"
  33. #elif defined(STEAMROLLER) || defined(PILEDRIVER) || defined(EXCAVATOR)
  34. #include "zdot_microk_steamroller-2.c"
  35. #elif defined(HASWELL) || defined(ZEN) || defined (SKYLAKEX) || defined (COOPERLAKE) || defined (SAPPHIRERAPIDS)
  36. #include "zdot_microk_haswell-2.c"
  37. #elif defined(SANDYBRIDGE)
  38. #include "zdot_microk_sandy-2.c"
  39. #endif
  40. #ifndef HAVE_KERNEL_8
  41. static void zdot_kernel_8(BLASLONG n, FLOAT *x, FLOAT *y, FLOAT *d) __attribute__ ((noinline));
  42. static void zdot_kernel_8(BLASLONG n, FLOAT *x, FLOAT *y, FLOAT *d)
  43. {
  44. BLASLONG register i = 0;
  45. FLOAT dot[4] = { 0.0, 0.0, 0.0, 0.0 };
  46. BLASLONG j=0;
  47. while( i < n )
  48. {
  49. dot[0] += x[j] * y[j] ;
  50. dot[1] += x[j+1] * y[j+1] ;
  51. dot[2] += x[j] * y[j+1] ;
  52. dot[3] += x[j+1] * y[j] ;
  53. dot[0] += x[j+2] * y[j+2] ;
  54. dot[1] += x[j+3] * y[j+3] ;
  55. dot[2] += x[j+2] * y[j+3] ;
  56. dot[3] += x[j+3] * y[j+2] ;
  57. dot[0] += x[j+4] * y[j+4] ;
  58. dot[1] += x[j+5] * y[j+5] ;
  59. dot[2] += x[j+4] * y[j+5] ;
  60. dot[3] += x[j+5] * y[j+4] ;
  61. dot[0] += x[j+6] * y[j+6] ;
  62. dot[1] += x[j+7] * y[j+7] ;
  63. dot[2] += x[j+6] * y[j+7] ;
  64. dot[3] += x[j+7] * y[j+6] ;
  65. j+=8;
  66. i+=4;
  67. }
  68. d[0] = dot[0];
  69. d[1] = dot[1];
  70. d[2] = dot[2];
  71. d[3] = dot[3];
  72. }
  73. #endif
  74. #if defined(SMP)
  75. extern int blas_level1_thread_with_return_value(int mode, BLASLONG m, BLASLONG n,
  76. BLASLONG k, void *alpha, void *a, BLASLONG lda, void *b, BLASLONG ldb,
  77. void *c, BLASLONG ldc, int (*function)(void), int nthreads);
  78. #endif
  79. static void zdot_compute (BLASLONG n, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y,OPENBLAS_COMPLEX_FLOAT *result)
  80. {
  81. BLASLONG i;
  82. BLASLONG ix,iy;
  83. FLOAT dot[4] = { 0.0, 0.0, 0.0 , 0.0 } ;
  84. if ( n <= 0 )
  85. {
  86. OPENBLAS_COMPLEX_FLOAT res=OPENBLAS_MAKE_COMPLEX_FLOAT(0.0,0.0);
  87. *result=res;
  88. return;
  89. }
  90. if ( (inc_x == 1) && (inc_y == 1) )
  91. {
  92. BLASLONG n1 = n & -8;
  93. if ( n1 )
  94. zdot_kernel_8(n1, x, y , dot );
  95. i = n1;
  96. BLASLONG j = i * 2;
  97. while( i < n )
  98. {
  99. dot[0] += x[j] * y[j] ;
  100. dot[1] += x[j+1] * y[j+1] ;
  101. dot[2] += x[j] * y[j+1] ;
  102. dot[3] += x[j+1] * y[j] ;
  103. j+=2;
  104. i++ ;
  105. }
  106. }
  107. else
  108. {
  109. i=0;
  110. ix=0;
  111. iy=0;
  112. inc_x *= 2;
  113. inc_y *= 2;
  114. while(i < n)
  115. {
  116. dot[0] += x[ix] * y[iy] ;
  117. dot[1] += x[ix+1] * y[iy+1] ;
  118. dot[2] += x[ix] * y[iy+1] ;
  119. dot[3] += x[ix+1] * y[iy] ;
  120. ix += inc_x ;
  121. iy += inc_y ;
  122. i++ ;
  123. }
  124. }
  125. #if !defined(CONJ)
  126. OPENBLAS_COMPLEX_FLOAT res=OPENBLAS_MAKE_COMPLEX_FLOAT(dot[0]-dot[1],dot[2]+dot[3]);
  127. #else
  128. OPENBLAS_COMPLEX_FLOAT res=OPENBLAS_MAKE_COMPLEX_FLOAT(dot[0]+dot[1],dot[2]-dot[3]);
  129. #endif
  130. *result=res;
  131. return;
  132. }
  133. #if defined(SMP)
  134. static int zdot_thread_function(BLASLONG n, BLASLONG dummy0,
  135. BLASLONG dummy1, FLOAT dummy2r, FLOAT dummy2i, FLOAT *x, BLASLONG inc_x, FLOAT *y,
  136. BLASLONG inc_y, FLOAT *result, BLASLONG dummy3)
  137. {
  138. zdot_compute(n, x, inc_x, y, inc_y, (void *)result);
  139. return 0;
  140. }
  141. #endif
  142. OPENBLAS_COMPLEX_FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y)
  143. {
  144. #if defined(SMP)
  145. int nthreads;
  146. FLOAT dummy_alpha;
  147. #if defined(C_PGI) || defined(C_SUN)
  148. FLOAT zdotr=0., zdoti=0.;
  149. #endif
  150. #endif
  151. OPENBLAS_COMPLEX_FLOAT zdot;
  152. #if defined(C_PGI) || defined(C_SUN)
  153. zdot=OPENBLAS_MAKE_COMPLEX_FLOAT(0.0,0.0);
  154. #else
  155. CREAL(zdot) = 0.0;
  156. CIMAG(zdot) = 0.0;
  157. #endif
  158. #if defined(SMP)
  159. if (inc_x == 0 || inc_y == 0 || n <= 10000)
  160. nthreads = 1;
  161. else
  162. nthreads = num_cpu_avail(1);
  163. if (nthreads == 1) {
  164. zdot_compute(n, x, inc_x, y, inc_y, &zdot);
  165. } else {
  166. int mode, i;
  167. char result[MAX_CPU_NUMBER * sizeof(double) * 2];
  168. OPENBLAS_COMPLEX_FLOAT *ptr;
  169. #if !defined(DOUBLE)
  170. mode = BLAS_SINGLE | BLAS_COMPLEX;
  171. #else
  172. mode = BLAS_DOUBLE | BLAS_COMPLEX;
  173. #endif
  174. blas_level1_thread_with_return_value(mode, n, 0, 0, &dummy_alpha,
  175. x, inc_x, y, inc_y, result, 0,
  176. (int (*)(void))zdot_thread_function, nthreads);
  177. ptr = (OPENBLAS_COMPLEX_FLOAT *)result;
  178. for (i = 0; i < nthreads; i++) {
  179. #if defined(C_PGI) || defined(C_SUN)
  180. zdotr += CREAL(*ptr);
  181. zdoti += CIMAG(*ptr);
  182. #else
  183. CREAL(zdot) = CREAL(zdot) + CREAL(*ptr);
  184. CIMAG(zdot) = CIMAG(zdot) + CIMAG(*ptr);
  185. #endif
  186. ptr = (void *)(((char *)ptr) + sizeof(double) * 2);
  187. }
  188. #if defined(C_PGI) || defined(C_SUN)
  189. zdot = OPENBLAS_MAKE_COMPLEX_FLOAT(zdotr,zdoti);
  190. #endif
  191. }
  192. #else
  193. zdot_compute(n, x, inc_x, y, inc_y, &zdot);
  194. #endif
  195. return zdot;
  196. }