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.

dot.c 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /***************************************************************************
  2. Copyright (c) 2014, 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 "../simd/intrin.h"
  29. #if defined(DSDOT)
  30. double CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y)
  31. #else
  32. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y)
  33. #endif
  34. {
  35. BLASLONG i=0;
  36. BLASLONG ix=0,iy=0;
  37. #if defined(DSDOT)
  38. double dot = 0.0 ;
  39. #else
  40. FLOAT dot = 0.0 ;
  41. #endif
  42. if ( n < 0 ) return(dot);
  43. if ( (inc_x == 1) && (inc_y == 1) )
  44. {
  45. int n1 = n & -4;
  46. #if V_SIMD && !defined(DSDOT)
  47. const int vstep = v_nlanes_f32;
  48. const int unrollx4 = n & (-vstep * 4);
  49. const int unrollx = n & -vstep;
  50. v_f32 vsum0 = v_zero_f32();
  51. v_f32 vsum1 = v_zero_f32();
  52. v_f32 vsum2 = v_zero_f32();
  53. v_f32 vsum3 = v_zero_f32();
  54. while(i < unrollx4)
  55. {
  56. vsum0 = v_muladd_f32(
  57. v_loadu_f32(x + i), v_loadu_f32(y + i), vsum0
  58. );
  59. vsum1 = v_muladd_f32(
  60. v_loadu_f32(x + i + vstep), v_loadu_f32(y + i + vstep), vsum1
  61. );
  62. vsum2 = v_muladd_f32(
  63. v_loadu_f32(x + i + vstep*2), v_loadu_f32(y + i + vstep*2), vsum2
  64. );
  65. vsum3 = v_muladd_f32(
  66. v_loadu_f32(x + i + vstep*3), v_loadu_f32(y + i + vstep*3), vsum3
  67. );
  68. i += vstep*4;
  69. }
  70. vsum0 = v_add_f32(
  71. v_add_f32(vsum0, vsum1), v_add_f32(vsum2 , vsum3)
  72. );
  73. while(i < unrollx)
  74. {
  75. vsum0 = v_muladd_f32(
  76. v_loadu_f32(x + i), v_loadu_f32(y + i), vsum0
  77. );
  78. i += vstep;
  79. }
  80. dot = v_sum_f32(vsum0);
  81. #elif defined(DSDOT)
  82. for (; i < n1; i += 4)
  83. {
  84. dot += (double) y[i] * (double) x[i]
  85. + (double) y[i+1] * (double) x[i+1]
  86. + (double) y[i+2] * (double) x[i+2]
  87. + (double) y[i+3] * (double) x[i+3] ;
  88. }
  89. #else
  90. for (; i < n1; i += 4)
  91. {
  92. dot += y[i] * x[i]
  93. + y[i+1] * x[i+1]
  94. + y[i+2] * x[i+2]
  95. + y[i+3] * x[i+3] ;
  96. }
  97. #endif
  98. while(i < n)
  99. {
  100. #if defined(DSDOT)
  101. dot += (double) y[i] * (double) x[i] ;
  102. #else
  103. dot += y[i] * x[i] ;
  104. #endif
  105. i++ ;
  106. }
  107. return(dot);
  108. }
  109. while(i < n)
  110. {
  111. #if defined(DSDOT)
  112. dot += (double) y[iy] * (double) x[ix] ;
  113. #else
  114. dot += y[iy] * x[ix] ;
  115. #endif
  116. ix += inc_x ;
  117. iy += inc_y ;
  118. i++ ;
  119. }
  120. return(dot);
  121. }