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.

nrm2_vector_dot.c 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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) vsetvl_e32m8(n)
  30. #define VSETVL_MAX vsetvlmax_e32m1()
  31. #define FLOAT_V_T vfloat32m8_t
  32. #define FLOAT_V_T_M1 vfloat32m1_t
  33. #define VLEV_FLOAT vle32_v_f32m8
  34. #define VLSEV_FLOAT vlse32_v_f32m8
  35. #define VFMVFS_FLOAT vfmv_f_s_f32m1_f32
  36. #define VFREDSUM_FLOAT vfredusum_vs_f32m8_f32m1
  37. #define VFMACCVV_FLOAT vfmacc_vv_f32m8
  38. #define VFMVVF_FLOAT vfmv_v_f_f32m8
  39. #define VFMVVF_FLOAT_M1 vfmv_v_f_f32m1
  40. #define VFDOTVV_FLOAT vfdot_vv_f32m8
  41. #define ABS fabsf
  42. #else
  43. #define VSETVL(n) vsetvl_e64m8(n)
  44. #define VSETVL_MAX vsetvlmax_e64m1()
  45. #define FLOAT_V_T vfloat64m8_t
  46. #define FLOAT_V_T_M1 vfloat64m1_t
  47. #define VLEV_FLOAT vle64_v_f64m8
  48. #define VLSEV_FLOAT vlse64_v_f64m8
  49. #define VFMVFS_FLOAT vfmv_f_s_f64m1_f64
  50. #define VFREDSUM_FLOAT vfredusum_vs_f64m8_f64m1
  51. #define VFMACCVV_FLOAT vfmacc_vv_f64m8
  52. #define VFMVVF_FLOAT vfmv_v_f_f64m8
  53. #define VFMVVF_FLOAT_M1 vfmv_v_f_f64m1
  54. #define VFDOTVV_FLOAT vfdot_vv_f64m8
  55. #define ABS fabs
  56. #endif
  57. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  58. {
  59. BLASLONG i=0, j=0;
  60. double len = 0.0 ;
  61. if ( n <= 0 ) return(0.0);
  62. if(n == 1) return (ABS(x[0]));
  63. FLOAT_V_T vr, v0, v1;
  64. unsigned int gvl = 0;
  65. FLOAT_V_T_M1 v_res, v_z0;
  66. gvl = VSETVL_MAX;
  67. v_res = VFMVVF_FLOAT_M1(0, gvl);
  68. v_z0 = VFMVVF_FLOAT_M1(0, gvl);
  69. if(inc_x == 1){
  70. gvl = VSETVL(n);
  71. if(gvl < n/2){
  72. vr = VFMVVF_FLOAT(0, gvl);
  73. for(i=0,j=0; i<n/(2*gvl); i++){
  74. v0 = VLEV_FLOAT(&x[j], gvl);
  75. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  76. j += gvl;
  77. v1 = VLEV_FLOAT(&x[j], gvl);
  78. vr = VFMACCVV_FLOAT(vr, v1, v1, gvl);
  79. j += gvl;
  80. }
  81. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  82. len += VFMVFS_FLOAT(v_res);
  83. }
  84. //tail
  85. for(;j < n;){
  86. gvl = VSETVL(n-j);
  87. v0 = VLEV_FLOAT(&x[j], gvl);
  88. //v1 = 0
  89. //v1 = VFMVVF_FLOAT(0, gvl);
  90. //vr = VFDOTVV_FLOAT(v0, v0, gvl);
  91. vr = VFMACCVV_FLOAT(v1, v0, v0, gvl);
  92. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  93. len += VFMVFS_FLOAT(v_res);
  94. j += gvl;
  95. }
  96. }else{
  97. gvl = VSETVL(n);
  98. unsigned int stride_x = inc_x * sizeof(FLOAT);
  99. if(gvl < n/2){
  100. vr = VFMVVF_FLOAT(0, gvl);
  101. for(i=0,j=0; i<n/(2*gvl); i++){
  102. v0 = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  103. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  104. j += gvl;
  105. v1 = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  106. vr = VFMACCVV_FLOAT(vr, v1, v1, gvl);
  107. j += gvl;
  108. }
  109. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  110. len += VFMVFS_FLOAT(v_res);
  111. }
  112. //tail
  113. for(;j < n;){
  114. gvl = VSETVL(n-j);
  115. v0 = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  116. //v1 = 0
  117. //v1 = VFMVVF_FLOAT(0, gvl);
  118. //vr = VFDOTVV_FLOAT(v0, v0, gvl);
  119. vr = VFMACCVV_FLOAT(v1, v0, v0, gvl);
  120. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  121. len += VFMVFS_FLOAT(v_res);
  122. j += gvl;
  123. }
  124. }
  125. return(sqrt(len));
  126. }