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.c 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 VFMVSF_FLOAT JOIN(RISCV_RVV(vfmv), _s_f_f, ELEN, LMUL, _)
  58. #define VFMVVF_FLOAT_M1 JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, m1, _)
  59. #define MASK_T JOIN(vbool, MLEN, _t, _, _)
  60. #define VFABS JOIN(RISCV_RVV(vfabs), _v_f, ELEN, LMUL, _)
  61. #define VMFNE JOIN(RISCV_RVV(vmfne_vf_f),ELEN, LMUL, _b, MLEN)
  62. #define VMFGT JOIN(RISCV_RVV(vmfgt_vv_f),ELEN, LMUL, _b, MLEN)
  63. #define VMFEQ JOIN(RISCV_RVV(vmfeq_vf_f),ELEN, LMUL, _b, MLEN)
  64. #define VCPOP JOIN(RISCV_RVV(vcpop), _m_b, MLEN, _, _)
  65. #ifdef RISCV_0p10_INTRINSICS
  66. #define VFDIV_M JOIN(RISCV_RVV(vfdiv), _vv_f, ELEN, LMUL, _m)
  67. #define VFMUL_M JOIN(RISCV_RVV(vfmul), _vv_f, ELEN, LMUL, _m)
  68. #define VFMACC_M JOIN(RISCV_RVV(vfmacc), _vv_f, ELEN, LMUL, _m)
  69. #define VMERGE(a, b, mask, gvl) JOIN(RISCV_RVV(vmerge), _vvm_f, ELEN, LMUL, _)(mask, a, b, gvl)
  70. #else
  71. #define VFDIV_M JOIN(RISCV_RVV(vfdiv), _vv_f, ELEN, LMUL, _mu)
  72. #define VFMUL_M JOIN(RISCV_RVV(vfmul), _vv_f, ELEN, LMUL, _mu)
  73. #define VFMACC_M JOIN(RISCV_RVV(vfmacc), _vv_f, ELEN, LMUL, _mu)
  74. #define VMERGE JOIN(RISCV_RVV(vmerge), _vvm_f, ELEN, LMUL, _)
  75. #endif
  76. #define VFIRST JOIN(RISCV_RVV(vfirst), _m_b, MLEN, _, _)
  77. #define VRGATHER JOIN(RISCV_RVV(vrgather), _vx_f, ELEN, LMUL, _)
  78. #define VFDIV JOIN(RISCV_RVV(vfdiv), _vv_f, ELEN, LMUL, _)
  79. #define VFMUL JOIN(RISCV_RVV(vfmul), _vv_f, ELEN, LMUL, _)
  80. #define VFMACC JOIN(RISCV_RVV(vfmacc), _vv_f, ELEN, LMUL, _)
  81. #define VMSBF JOIN(RISCV_RVV(vmsbf), _m_b, MLEN, _, _)
  82. #define VMSOF JOIN(RISCV_RVV(vmsof), _m_b, MLEN, _, _)
  83. #define VMAND JOIN(RISCV_RVV(vmand), _mm_b, MLEN, _, _)
  84. #define VMANDN JOIN(RISCV_RVV(vmandn), _mm_b, MLEN, _, _)
  85. #define VSEV_FLOAT JOIN(RISCV_RVV(vse), ELEN, _v_f, ELEN, LMUL)
  86. #if defined(DOUBLE)
  87. #define ABS fabs
  88. #else
  89. #define ABS fabsf
  90. #endif
  91. #define EXTRACT_FLOAT0_V(v) JOIN(RISCV_RVV(vfmv_f_s_f), ELEN, LMUL, _f, ELEN)(v)
  92. //#define DUMP( label, v0, gvl )
  93. #define DUMP( label, v0, gvl ) do{ FLOAT x[16]; VSEV_FLOAT( x, v0, gvl ); printf ("%s(%d): %s [ ", __FILE__, __LINE__, label); for( int xxx = 0; xxx < gvl; ++xxx ) { printf("%f, ", x[xxx]); } printf(" ]\n"); } while(0)
  94. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  95. {
  96. BLASLONG i=0;
  97. if (n <= 0 || inc_x <= 0) return(0.0);
  98. if(n == 1) return (ABS(x[0]));
  99. unsigned int gvl = 0;
  100. MASK_T nonzero_mask;
  101. MASK_T scale_mask;
  102. gvl = VSETVL(n);
  103. FLOAT_V_T v0;
  104. FLOAT_V_T v_ssq = VFMVVF_FLOAT(0, gvl);
  105. FLOAT_V_T v_scale = VFMVVF_FLOAT(0, gvl);
  106. FLOAT scale = 0;
  107. FLOAT ssq = 0;
  108. unsigned int stride_x = inc_x * sizeof(FLOAT);
  109. int idx = 0;
  110. if( n >= gvl ) // don't pay overheads if we're not doing useful work
  111. {
  112. for(i=0; i<n/gvl; i++){
  113. v0 = VLSEV_FLOAT( &x[idx], stride_x, gvl );
  114. nonzero_mask = VMFNE( v0, 0, gvl );
  115. v0 = VFABS( v0, gvl );
  116. scale_mask = VMFGT( v0, v_scale, gvl );
  117. // assume scale changes are relatively infrequent
  118. // unclear if the vcpop+branch is actually a win
  119. // since the operations being skipped are predicated anyway
  120. // need profiling to confirm
  121. if( VCPOP(scale_mask, gvl) )
  122. {
  123. v_scale = VFDIV_M( scale_mask, v_scale, v_scale, v0, gvl );
  124. v_scale = VFMUL_M( scale_mask, v_scale, v_scale, v_scale, gvl );
  125. v_ssq = VFMUL_M( scale_mask, v_ssq, v_ssq, v_scale, gvl );
  126. v_scale = VMERGE( v_scale, v0, scale_mask, gvl );
  127. }
  128. v0 = VFDIV_M( nonzero_mask, v0, v0, v_scale, gvl );
  129. v_ssq = VFMACC_M( nonzero_mask, v_ssq, v0, v0, gvl );
  130. idx += inc_x * gvl;
  131. }
  132. // we have gvl elements which we accumulated independently, with independent scales
  133. // we need to combine these
  134. // naive sort so we process small values first to avoid losing information
  135. // could use vector sort extensions where available, but we're dealing with gvl elts at most
  136. FLOAT * out_ssq = alloca(gvl*sizeof(FLOAT));
  137. FLOAT * out_scale = alloca(gvl*sizeof(FLOAT));
  138. VSEV_FLOAT( out_ssq, v_ssq, gvl );
  139. VSEV_FLOAT( out_scale, v_scale, gvl );
  140. for( int a = 0; a < (gvl-1); ++a )
  141. {
  142. int smallest = a;
  143. for( size_t b = a+1; b < gvl; ++b )
  144. if( out_scale[b] < out_scale[smallest] )
  145. smallest = b;
  146. if( smallest != a )
  147. {
  148. FLOAT tmp1 = out_ssq[a];
  149. FLOAT tmp2 = out_scale[a];
  150. out_ssq[a] = out_ssq[smallest];
  151. out_scale[a] = out_scale[smallest];
  152. out_ssq[smallest] = tmp1;
  153. out_scale[smallest] = tmp2;
  154. }
  155. }
  156. int a = 0;
  157. while( a<gvl && out_scale[a] == 0 )
  158. ++a;
  159. if( a < gvl )
  160. {
  161. ssq = out_ssq[a];
  162. scale = out_scale[a];
  163. ++a;
  164. for( ; a < gvl; ++a )
  165. {
  166. ssq = ssq * ( scale / out_scale[a] ) * ( scale / out_scale[a] ) + out_ssq[a];
  167. scale = out_scale[a];
  168. }
  169. }
  170. }
  171. //finish any tail using scalar ops
  172. i*=gvl*inc_x;
  173. n*=inc_x;
  174. while(i < n){
  175. if ( x[i] != 0.0 ){
  176. FLOAT absxi = ABS( x[i] );
  177. if ( scale < absxi ){
  178. ssq = 1 + ssq * ( scale / absxi ) * ( scale / absxi );
  179. scale = absxi ;
  180. }
  181. else{
  182. ssq += ( absxi/scale ) * ( absxi/scale );
  183. }
  184. }
  185. i += inc_x;
  186. }
  187. return(scale * sqrt(ssq));
  188. }