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 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_e32m4(n)
  30. #define VSETVL_MAX vsetvlmax_e32m1()
  31. #define FLOAT_V_T vfloat32m4_t
  32. #define FLOAT_V_T_M1 vfloat32m1_t
  33. #define VLEV_FLOAT vle_v_f32m4
  34. #define VLSEV_FLOAT vlse_v_f32m4
  35. #define VFREDSUM_FLOAT vfredsum_vs_f32m4_f32m1
  36. #define VFMACCVV_FLOAT vfmacc_vv_f32m4
  37. #define VFMVVF_FLOAT vfmv_v_f_f32m4
  38. #define VFMVVF_FLOAT_M1 vfmv_v_f_f32m1
  39. #define VFDOTVV_FLOAT vfdot_vv_f32m4
  40. #define ABS fabsf
  41. #define MASK_T vbool8_t
  42. #define VFRSUBVF_MASK_FLOAT vfrsub_vf_f32m4_m
  43. #define VMFGTVF_FLOAT vmfgt_vf_f32m4_b8
  44. #define VMFIRSTM vmfirst_m_b8
  45. #define VFDIVVF_FLOAT vfdiv_vf_f32m4
  46. #define VMFLTVF_FLOAT vmflt_vf_f32m4_b8
  47. #define VFREDMAXVS_FLOAT vfredmax_vs_f32m4_f32m1
  48. #else
  49. #define VSETVL(n) vsetvl_e64m4(n)
  50. #define VSETVL_MAX vsetvlmax_e64m1()
  51. #define FLOAT_V_T vfloat64m4_t
  52. #define FLOAT_V_T_M1 vfloat64m1_t
  53. #define VLEV_FLOAT vle_v_f64m4
  54. #define VLSEV_FLOAT vlse_v_f64m4
  55. #define VFREDSUM_FLOAT vfredsum_vs_f64m4_f64m1
  56. #define VFMACCVV_FLOAT vfmacc_vv_f64m4
  57. #define VFMVVF_FLOAT vfmv_v_f_f64m4
  58. #define VFMVVF_FLOAT_M1 vfmv_v_f_f64m1
  59. #define VFDOTVV_FLOAT vfdot_vv_f64m4
  60. #define ABS fabs
  61. #define MASK_T vbool16_t
  62. #define VFRSUBVF_MASK_FLOAT vfrsub_vf_f64m4_m
  63. #define VMFGTVF_FLOAT vmfgt_vf_f64m4_b16
  64. #define VMFIRSTM vmfirst_m_b16
  65. #define VFDIVVF_FLOAT vfdiv_vf_f64m4
  66. #define VMFLTVF_FLOAT vmflt_vf_f64m4_b16
  67. #define VFREDMAXVS_FLOAT vfredmax_vs_f64m4_f64m1
  68. #endif
  69. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  70. {
  71. BLASLONG i=0, j=0;
  72. if ( n < 0 ) return(0.0);
  73. if(n == 1) return (ABS(x[0]));
  74. FLOAT_V_T vr, v0, v_zero;
  75. unsigned int gvl = 0;
  76. FLOAT_V_T_M1 v_res, v_z0;
  77. gvl = VSETVL_MAX;
  78. v_res = VFMVVF_FLOAT_M1(0, gvl);
  79. v_z0 = VFMVVF_FLOAT_M1(0, gvl);
  80. FLOAT scale = 0.0, ssq = 0.0;
  81. MASK_T mask;
  82. BLASLONG index = 0;
  83. if(inc_x == 1){
  84. gvl = VSETVL(n);
  85. vr = VFMVVF_FLOAT(0, gvl);
  86. v_zero = VFMVVF_FLOAT(0, gvl);
  87. for(i=0,j=0; i<n/gvl; i++){
  88. v0 = VLEV_FLOAT(&x[j], gvl);
  89. //fabs(vector)
  90. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  91. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  92. //if scale change
  93. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  94. index = VMFIRSTM(mask, gvl);
  95. if(index == -1){//no elements greater than scale
  96. if(scale != 0.0){
  97. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  98. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  99. }
  100. }else{//found greater element
  101. //ssq in vector vr: vr[0]
  102. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  103. //total ssq before current vector
  104. ssq += v_res[0];
  105. //find max
  106. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  107. //update ssq before max_index
  108. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  109. //update scale
  110. scale = v_res[0];
  111. //ssq in vector vr
  112. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  113. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  114. }
  115. j += gvl;
  116. }
  117. //ssq in vector vr: vr[0]
  118. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  119. //total ssq now
  120. ssq += v_res[0];
  121. //tail
  122. if(j < n){
  123. gvl = VSETVL(n-j);
  124. v0 = VLEV_FLOAT(&x[j], gvl);
  125. //fabs(vector)
  126. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  127. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  128. //if scale change
  129. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  130. index = VMFIRSTM(mask, gvl);
  131. if(index == -1){//no elements greater than scale
  132. if(scale != 0.0)
  133. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  134. }else{//found greater element
  135. //find max
  136. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  137. //update ssq before max_index
  138. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  139. //update scale
  140. scale = v_res[0];
  141. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  142. }
  143. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  144. //ssq in vector vr: vr[0]
  145. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  146. //total ssq now
  147. ssq += v_res[0];
  148. }
  149. }else{
  150. gvl = VSETVL(n);
  151. vr = VFMVVF_FLOAT(0, gvl);
  152. v_zero = VFMVVF_FLOAT(0, gvl);
  153. unsigned int stride_x = inc_x * sizeof(FLOAT);
  154. int idx = 0, inc_v = inc_x * gvl;
  155. for(i=0,j=0; i<n/gvl; i++){
  156. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  157. //fabs(vector)
  158. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  159. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  160. //if scale change
  161. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  162. index = VMFIRSTM(mask, gvl);
  163. if(index == -1){//no elements greater than scale
  164. if(scale != 0.0){
  165. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  166. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  167. }
  168. }else{//found greater element
  169. //ssq in vector vr: vr[0]
  170. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  171. //total ssq before current vector
  172. ssq += v_res[0];
  173. //find max
  174. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  175. //update ssq before max_index
  176. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  177. //update scale
  178. scale = v_res[0];
  179. //ssq in vector vr
  180. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  181. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  182. }
  183. j += gvl;
  184. idx += inc_v;
  185. }
  186. //ssq in vector vr: vr[0]
  187. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  188. //total ssq now
  189. ssq += v_res[0];
  190. //tail
  191. if(j < n){
  192. gvl = VSETVL(n-j);
  193. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  194. //fabs(vector)
  195. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  196. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  197. //if scale change
  198. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  199. index = VMFIRSTM(mask, gvl);
  200. if(index == -1){//no elements greater than scale
  201. if(scale != 0.0)
  202. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  203. }else{//found greater element
  204. //find max
  205. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  206. //update ssq before max_index
  207. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  208. //update scale
  209. scale = vr[0];
  210. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  211. }
  212. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  213. //ssq in vector vr: vr[0]
  214. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  215. //total ssq now
  216. ssq += v_res[0];
  217. }
  218. }
  219. return(scale * sqrt(ssq));
  220. }