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.

znrm2_vector.c 8.3 kB

1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifdef RISCV64_ZVL256B
  29. # define LMUL m1
  30. # if defined(DOUBLE)
  31. # define ELEN 64
  32. # define MLEN 64
  33. # else
  34. # define ELEN 32
  35. # define MLEN 32
  36. # endif
  37. #else
  38. # define LMUL m4
  39. # if defined(DOUBLE)
  40. # define ELEN 64
  41. # define MLEN 16
  42. # else
  43. # define ELEN 32
  44. # define MLEN 8
  45. # endif
  46. #endif
  47. #define _
  48. #define JOIN2_X(x, y) x ## y
  49. #define JOIN2(x, y) JOIN2_X(x, y)
  50. #define JOIN(v, w, x, y, z) JOIN2( JOIN2( JOIN2( JOIN2( v, w ), x), y), z)
  51. #define VSETVL JOIN(RISCV_RVV(vsetvl), _e, ELEN, LMUL, _)
  52. #define FLOAT_V_T JOIN(vfloat, ELEN, LMUL, _t, _)
  53. #define FLOAT_V_T_M1 JOIN(vfloat, ELEN, m1, _t, _)
  54. #define VLEV_FLOAT JOIN(RISCV_RVV(vle), ELEN, _v_f, ELEN, LMUL)
  55. #define VLSEV_FLOAT JOIN(RISCV_RVV(vlse), ELEN, _v_f, ELEN, LMUL)
  56. #define VFMVVF_FLOAT JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, LMUL, _)
  57. #define VFMVVF_FLOAT_M1 JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, m1, _)
  58. #define MASK_T JOIN(vbool, MLEN, _t, _, _)
  59. #define VFABS JOIN(RISCV_RVV(vfabs), _v_f, ELEN, LMUL, _)
  60. #define VMFNE JOIN(RISCV_RVV(vmfne_vf_f),ELEN, LMUL, _b, MLEN)
  61. #define VMFGT JOIN(RISCV_RVV(vmfgt_vv_f),ELEN, LMUL, _b, MLEN)
  62. #define VMFEQ JOIN(RISCV_RVV(vmfeq_vv_f),ELEN, LMUL, _b, MLEN)
  63. #define VCPOP JOIN(RISCV_RVV(vcpop), _m_b, MLEN, _, _)
  64. #ifdef RISCV_0p10_INTRINSICS
  65. #define VFREDMAX(va, vb, gvl) JOIN(RISCV_RVV(vfredmax_vs_f),ELEN,LMUL, JOIN2(_f, ELEN), m1)(v_res, va, vb, gvl)
  66. #define VFREDUSUM(va, vb, gvl) JOIN(RISCV_RVV(vfredusum_vs_f),ELEN,LMUL, JOIN2(_f, ELEN), m1)(v_res, va, vb, gvl)
  67. #define VFDIV_M JOIN(RISCV_RVV(vfdiv), _vv_f, ELEN, LMUL, _m)
  68. #define VFMACC_M JOIN(RISCV_RVV(vfmacc), _vv_f, ELEN, LMUL, _m)
  69. #else
  70. #define VFREDMAX JOIN(RISCV_RVV(vfredmax_vs_f),ELEN,LMUL, JOIN2(_f, ELEN), m1)
  71. #define VFREDUSUM JOIN(RISCV_RVV(vfredusum_vs_f),ELEN,LMUL, JOIN2(_f, ELEN), m1)
  72. #define VFDIV_M JOIN(RISCV_RVV(vfdiv), _vv_f, ELEN, LMUL, _mu)
  73. #define VFMACC_M JOIN(RISCV_RVV(vfmacc), _vv_f, ELEN, LMUL, _mu)
  74. #endif
  75. #define VFIRST JOIN(RISCV_RVV(vfirst), _m_b, MLEN, _, _)
  76. #define VRGATHER JOIN(RISCV_RVV(vrgather), _vx_f, ELEN, LMUL, _)
  77. #define VFDIV JOIN(RISCV_RVV(vfdiv), _vf_f, ELEN, LMUL, _)
  78. #define VFMUL JOIN(RISCV_RVV(vfmul), _vv_f, ELEN, LMUL, _)
  79. #define VFMACC JOIN(RISCV_RVV(vfmacc), _vv_f, ELEN, LMUL, _)
  80. #define VMSOF JOIN(RISCV_RVV(vmsof), _m_b, MLEN, _, _)
  81. #define VMANDN JOIN(RISCV_RVV(vmandn), _mm_b, MLEN, _, _)
  82. #if defined(DOUBLE)
  83. #define ABS fabs
  84. #else
  85. #define ABS fabsf
  86. #endif
  87. #define EXTRACT_FLOAT0_V(v) JOIN(RISCV_RVV(vfmv_f_s_f), ELEN, LMUL, _f, ELEN)(v)
  88. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  89. {
  90. BLASLONG i=0;
  91. if (n <= 0 || inc_x == 0) return(0.0);
  92. FLOAT_V_T v_ssq, v_scale, v0, v1, v_zero;
  93. unsigned int gvl = 0;
  94. FLOAT_V_T_M1 v_res, v_z0;
  95. v_res = VFMVVF_FLOAT_M1(0, 1);
  96. v_z0 = VFMVVF_FLOAT_M1(0, 1);
  97. gvl = VSETVL(n);
  98. v_ssq = VFMVVF_FLOAT(0, gvl);
  99. v_scale = VFMVVF_FLOAT(0, gvl);
  100. v_zero = VFMVVF_FLOAT(0, gvl);
  101. unsigned int stride_x = inc_x * sizeof(FLOAT) * 2;
  102. int idx = 0;
  103. for(i=0; i<n/gvl; i++){
  104. v0 = VLSEV_FLOAT( &x[idx], stride_x, gvl );
  105. v1 = VLSEV_FLOAT( &x[idx+1], stride_x, gvl );
  106. v0 = VFABS( v0, gvl );
  107. v1 = VFABS( v1, gvl );
  108. MASK_T scale_mask0 = VMFGT( v0, v_scale, gvl );
  109. MASK_T scale_mask1 = VMFGT( v1, v_scale, gvl );
  110. if( VCPOP( scale_mask0, gvl ) + VCPOP( scale_mask1, gvl ) > 0 ){ // scale change?
  111. // find largest element in v0 and v1
  112. v_res = VFREDMAX( v0, v_z0, gvl );
  113. v_res = VFREDMAX( v1, v_res, gvl );
  114. FLOAT const largest_elt = EXTRACT_FLOAT( v_res );
  115. v_scale = VFDIV( v_scale, largest_elt, gvl ); // scale/largest_elt
  116. v_scale = VFMUL( v_scale, v_scale, gvl ); // (scale/largest_elt)*(scale/largest_elt)
  117. v_ssq = VFMUL( v_scale, v_ssq, gvl ); // ssq*(scale/largest_elt)*(scale/largest_elt)
  118. v_scale = VFMVVF_FLOAT( largest_elt, gvl ); // splated largest_elt becomes new scale
  119. }
  120. MASK_T nonzero_mask0 = VMFNE( v0, 0, gvl );
  121. MASK_T nonzero_mask1 = VMFNE( v1, 0, gvl );
  122. v0 = VFDIV_M( nonzero_mask0, v_zero, v0, v_scale, gvl );
  123. v1 = VFDIV_M( nonzero_mask1, v_zero, v1, v_scale, gvl );
  124. v_ssq = VFMACC_M( nonzero_mask0, v_ssq, v0, v0, gvl );
  125. v_ssq = VFMACC_M( nonzero_mask1, v_ssq, v1, v1, gvl );
  126. idx += inc_x * gvl * 2;
  127. }
  128. v_res = VFREDUSUM(v_ssq, v_z0, gvl);
  129. FLOAT ssq = EXTRACT_FLOAT(v_res);
  130. FLOAT scale = EXTRACT_FLOAT0_V(v_scale);
  131. //finish any tail using scalar ops
  132. i*=gvl;
  133. if(i<n){
  134. i *= inc_x*2;
  135. n *= inc_x*2;
  136. FLOAT temp;
  137. do{
  138. if ( x[i] != 0.0 ){
  139. temp = ABS( x[i] );
  140. if ( scale < temp ){
  141. ssq = 1 + ssq * ( scale / temp ) * ( scale / temp );
  142. scale = temp ;
  143. }else{
  144. ssq += ( temp / scale ) * ( temp / scale );
  145. }
  146. }
  147. if ( x[i+1] != 0.0 ){
  148. temp = ABS( x[i+1] );
  149. if ( scale < temp ){
  150. ssq = 1 + ssq * ( scale / temp ) * ( scale / temp );
  151. scale = temp ;
  152. }else{
  153. ssq += ( temp / scale ) * ( temp / scale );
  154. }
  155. }
  156. i += inc_x*2;
  157. }while(abs(i)<abs(n));
  158. }
  159. return(scale * sqrt(ssq));
  160. }