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.

zasum_vector.c 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <math.h>
  29. #ifdef RISCV64_ZVL256B
  30. # define LMUL m2
  31. # if defined(DOUBLE)
  32. # define ELEN 64
  33. # define MLEN _b32
  34. # else
  35. # define ELEN 32
  36. # define MLEN _b16
  37. # endif
  38. #else
  39. # define LMUL m8
  40. # if defined(DOUBLE)
  41. # define ELEN 64
  42. # define MLEN _b8
  43. # else
  44. # define ELEN 32
  45. # define MLEN _b4
  46. # endif
  47. #endif
  48. #define _
  49. #define JOIN2_X(x, y) x ## y
  50. #define JOIN2(x, y) JOIN2_X(x, y)
  51. #define JOIN(v, w, x, y, z) JOIN2( JOIN2( JOIN2( JOIN2( v, w ), x), y), z)
  52. #define VSETVL JOIN(RISCV_RVV(vsetvl), _e, ELEN, LMUL, _)
  53. #define FLOAT_V_T JOIN(vfloat, ELEN, LMUL, _t, _)
  54. #define FLOAT_V_T_M1 JOIN(vfloat, ELEN, m1, _t, _)
  55. #define VLEV_FLOAT JOIN(RISCV_RVV(vle), ELEN, _v_f, ELEN, LMUL)
  56. #define VLSEV_FLOAT JOIN(RISCV_RVV(vlse), ELEN, _v_f, ELEN, LMUL)
  57. #ifdef RISCV_0p10_INTRINSICS
  58. #define VFREDSUMVS_FLOAT(va, vb, gvl) JOIN(RISCV_RVV(vfredusum_vs_f), ELEN, LMUL, _f, JOIN2( ELEN, m1))(v_res, va, vb, gvl)
  59. #else
  60. #define VFREDSUMVS_FLOAT JOIN(RISCV_RVV(vfredusum_vs_f), ELEN, LMUL, _f, JOIN2( ELEN, m1))
  61. #endif
  62. #define VFABS_FLOAT JOIN(RISCV_RVV(vfabs), _v_f, ELEN, LMUL, _)
  63. #define VFMVVF_FLOAT JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, LMUL, _)
  64. #define VFMVVF_FLOAT_M1 JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, m1, _)
  65. #define VFADDVV_FLOAT JOIN(RISCV_RVV(vfadd), _vv_f, ELEN, LMUL, _)
  66. #define VMFLTVF_FLOAT JOIN(RISCV_RVV(vmflt), _vf_f, ELEN, LMUL, MLEN)
  67. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  68. {
  69. BLASLONG i=0, j=0;
  70. BLASLONG ix=0;
  71. FLOAT asumf=0.0;
  72. if (n <= 0 || inc_x <= 0) return(asumf);
  73. unsigned int gvl = 0;
  74. FLOAT_V_T v0, v1, v_zero,v_sum;
  75. FLOAT_V_T_M1 v_res;
  76. v_res = VFMVVF_FLOAT_M1(0, 1);
  77. if(inc_x == 1){
  78. BLASLONG n2 = n * 2;
  79. gvl = VSETVL(n2);
  80. v_zero = VFMVVF_FLOAT(0, gvl);
  81. if(gvl <= n2/2){
  82. v_sum = VFMVVF_FLOAT(0, gvl);
  83. for(i=0,j=0; i<n2/(gvl*2); i++){
  84. v0 = VLEV_FLOAT(&x[j], gvl);
  85. v0 = VFABS_FLOAT(v0, gvl);
  86. v_sum = VFADDVV_FLOAT(v_sum, v0, gvl);
  87. v1 = VLEV_FLOAT(&x[j+gvl], gvl);
  88. v1 = VFABS_FLOAT(v1, gvl);
  89. v_sum = VFADDVV_FLOAT(v_sum, v1, gvl);
  90. j += gvl * 2;
  91. }
  92. v_res = VFREDSUMVS_FLOAT(v_sum, v_res, gvl);
  93. }
  94. for(;j<n2;){
  95. gvl = VSETVL(n2-j);
  96. v0 = VLEV_FLOAT(&x[j], gvl);
  97. v0 = VFABS_FLOAT(v0, gvl);
  98. v_res = VFREDSUMVS_FLOAT(v0, v_res, gvl);
  99. j += gvl;
  100. }
  101. }else{
  102. gvl = VSETVL(n);
  103. unsigned int stride_x = inc_x * sizeof(FLOAT) * 2;
  104. v_zero = VFMVVF_FLOAT(0, gvl);
  105. BLASLONG inc_xv = inc_x * 2 * gvl;
  106. v_sum = VFMVVF_FLOAT(0, gvl);
  107. for(i=0,j=0; i<n/gvl; i++){
  108. v0 = VLSEV_FLOAT(&x[ix], stride_x, gvl);
  109. v0 = VFABS_FLOAT(v0, gvl);
  110. v_sum = VFADDVV_FLOAT(v_sum, v0, gvl);
  111. v1 = VLSEV_FLOAT(&x[ix+1], stride_x, gvl);
  112. v1 = VFABS_FLOAT(v1, gvl);
  113. v_sum = VFADDVV_FLOAT(v_sum, v1, gvl);
  114. j += gvl;
  115. ix += inc_xv;
  116. }
  117. v_res = VFREDSUMVS_FLOAT(v_sum, v_res, gvl);
  118. if(j<n){
  119. gvl = VSETVL(n-j);
  120. v0 = VLSEV_FLOAT(&x[ix], stride_x, gvl);
  121. v0 = VFABS_FLOAT(v0, gvl);
  122. v1 = VLSEV_FLOAT(&x[ix+1], stride_x, gvl);
  123. v1 = VFABS_FLOAT(v1, gvl);
  124. v_sum = VFADDVV_FLOAT(v0, v1, gvl);
  125. v_res = VFREDSUMVS_FLOAT(v_sum, v_res, gvl);
  126. }
  127. }
  128. asumf = EXTRACT_FLOAT(v_res);
  129. return(asumf);
  130. }