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_vector.c 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /***************************************************************************
  2. Copyright (c) 2020, 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(DOUBLE)
  29. #define VSETVL(n) __riscv_vsetvl_e32m4(n)
  30. #define VSETVL_MAX __riscv_vsetvlmax_e32m1()
  31. #define FLOAT_V_T vfloat32m4_t
  32. #define FLOAT_V_T_M1 vfloat32m1_t
  33. #define VLEV_FLOAT __riscv_vle32_v_f32m4
  34. #define VLSEV_FLOAT __riscv_vlse32_v_f32m4
  35. #define VFREDSUM_FLOAT __riscv_vfredusum_vs_f32m4_f32m1
  36. #define VFMACCVV_FLOAT __riscv_vfmacc_vv_f32m4
  37. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f32m4
  38. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f32m1
  39. #define VFDOTVV_FLOAT __riscv_vfdot_vv_f32m4
  40. #else
  41. #define VSETVL(n) __riscv_vsetvl_e64m4(n)
  42. #define VSETVL_MAX __riscv_vsetvlmax_e64m1()
  43. #define FLOAT_V_T vfloat64m4_t
  44. #define FLOAT_V_T_M1 vfloat64m1_t
  45. #define VLEV_FLOAT __riscv_vle64_v_f64m4
  46. #define VLSEV_FLOAT __riscv_vlse64_v_f64m4
  47. #define VFREDSUM_FLOAT __riscv_vfredusum_vs_f64m4_f64m1
  48. #define VFMACCVV_FLOAT __riscv_vfmacc_vv_f64m4
  49. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f64m4
  50. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f64m1
  51. #define VFDOTVV_FLOAT __riscv_vfdot_vv_f64m4
  52. #endif
  53. #if defined(DSDOT)
  54. double CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y)
  55. #else
  56. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x, FLOAT *y, BLASLONG inc_y)
  57. #endif
  58. {
  59. BLASLONG i=0, j=0;
  60. double dot = 0.0 ;
  61. if ( n < 1 ) return(dot);
  62. FLOAT_V_T vr, vx, vy;
  63. unsigned int gvl = 0;
  64. FLOAT_V_T_M1 v_res, v_z0;
  65. gvl = VSETVL_MAX;
  66. v_res = VFMVVF_FLOAT_M1(0, gvl);
  67. v_z0 = VFMVVF_FLOAT_M1(0, gvl);
  68. if(inc_x == 1 && inc_y == 1){
  69. gvl = VSETVL(n);
  70. vr = VFMVVF_FLOAT(0, gvl);
  71. for(i=0,j=0; i<n/gvl; i++){
  72. vx = VLEV_FLOAT(&x[j], gvl);
  73. vy = VLEV_FLOAT(&y[j], gvl);
  74. vr = VFMACCVV_FLOAT(vr, vx, vy, gvl);
  75. j += gvl;
  76. }
  77. if(j > 0){
  78. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  79. dot += (double)EXTRACT_FLOAT(v_res);
  80. }
  81. //tail
  82. if(j < n){
  83. gvl = VSETVL(n-j);
  84. vx = VLEV_FLOAT(&x[j], gvl);
  85. vy = VLEV_FLOAT(&y[j], gvl);
  86. FLOAT_V_T vz = VFMVVF_FLOAT(0, gvl);
  87. //vr = VFDOTVV_FLOAT(vx, vy, gvl);
  88. vr = VFMACCVV_FLOAT(vz, vx, vy, gvl);
  89. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  90. dot += (double)EXTRACT_FLOAT(v_res);
  91. }
  92. }else if(inc_y == 1){
  93. gvl = VSETVL(n);
  94. vr = VFMVVF_FLOAT(0, gvl);
  95. BLASLONG stride_x = inc_x * sizeof(FLOAT);
  96. for(i=0,j=0; i<n/gvl; i++){
  97. vx = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  98. vy = VLEV_FLOAT(&y[j], gvl);
  99. vr = VFMACCVV_FLOAT(vr, vx, vy, gvl);
  100. j += gvl;
  101. }
  102. if(j > 0){
  103. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  104. dot += (double)EXTRACT_FLOAT(v_res);
  105. }
  106. //tail
  107. if(j < n){
  108. gvl = VSETVL(n-j);
  109. vx = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  110. vy = VLEV_FLOAT(&y[j], gvl);
  111. FLOAT_V_T vz = VFMVVF_FLOAT(0, gvl);
  112. //vr = VFDOTVV_FLOAT(vx, vy, gvl);
  113. vr = VFMACCVV_FLOAT(vz, vx, vy, gvl);
  114. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  115. dot += (double)EXTRACT_FLOAT(v_res);
  116. }
  117. }else if(inc_x == 1){
  118. gvl = VSETVL(n);
  119. vr = VFMVVF_FLOAT(0, gvl);
  120. BLASLONG stride_y = inc_y * sizeof(FLOAT);
  121. for(i=0,j=0; i<n/gvl; i++){
  122. vx = VLEV_FLOAT(&x[j], gvl);
  123. vy = VLSEV_FLOAT(&y[j*inc_y], stride_y, gvl);
  124. vr = VFMACCVV_FLOAT(vr, vx, vy, gvl);
  125. j += gvl;
  126. }
  127. if(j > 0){
  128. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  129. dot += (double)EXTRACT_FLOAT(v_res);
  130. }
  131. //tail
  132. if(j < n){
  133. gvl = VSETVL(n-j);
  134. vx = VLEV_FLOAT(&x[j], gvl);
  135. vy = VLSEV_FLOAT(&y[j*inc_y], stride_y, gvl);
  136. FLOAT_V_T vz = VFMVVF_FLOAT(0, gvl);
  137. //vr = VFDOTVV_FLOAT(vx, vy, gvl);
  138. vr = VFMACCVV_FLOAT(vz, vx, vy, gvl);
  139. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  140. dot += (double)EXTRACT_FLOAT(v_res);
  141. }
  142. }else{
  143. gvl = VSETVL(n);
  144. vr = VFMVVF_FLOAT(0, gvl);
  145. BLASLONG stride_x = inc_x * sizeof(FLOAT);
  146. BLASLONG stride_y = inc_y * sizeof(FLOAT);
  147. for(i=0,j=0; i<n/gvl; i++){
  148. vx = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  149. vy = VLSEV_FLOAT(&y[j*inc_y], stride_y, gvl);
  150. vr = VFMACCVV_FLOAT(vr, vx, vy, gvl);
  151. j += gvl;
  152. }
  153. if(j > 0){
  154. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  155. dot += (double)EXTRACT_FLOAT(v_res);
  156. }
  157. //tail
  158. if(j < n){
  159. gvl = VSETVL(n-j);
  160. vx = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  161. vy = VLSEV_FLOAT(&y[j*inc_y], stride_y, gvl);
  162. FLOAT_V_T vz = VFMVVF_FLOAT(0, gvl);
  163. //vr = VFDOTVV_FLOAT(vx, vy, gvl);
  164. vr = VFMACCVV_FLOAT(vz, vx, vy, gvl);
  165. v_res = VFREDSUM_FLOAT(vr, v_z0, gvl);
  166. dot += (double)EXTRACT_FLOAT(v_res);
  167. }
  168. }
  169. return(dot);
  170. }