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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. BLASLONG n2 = n * 2;
  85. gvl = VSETVL(n2);
  86. vr = VFMVVF_FLOAT(0, gvl);
  87. v_zero = VFMVVF_FLOAT(0, gvl);
  88. for(i=0,j=0; i<n2/gvl; i++){
  89. v0 = VLEV_FLOAT(&x[j], gvl);
  90. //fabs(vector)
  91. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  92. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  93. //if scale change
  94. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  95. index = VMFIRSTM(mask, gvl);
  96. if(index == -1){//no elements greater than scale
  97. if(scale != 0.0){
  98. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  99. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  100. }
  101. }else{//found greater element
  102. //ssq in vector vr: vr[0]
  103. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  104. //total ssq before current vector
  105. ssq += v_res[0];
  106. //find max
  107. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  108. //update ssq before max_index
  109. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  110. //update scale
  111. scale = v_res[0];
  112. //ssq in vector vr
  113. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  114. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  115. }
  116. j += gvl;
  117. }
  118. //ssq in vector vr: vr[0]
  119. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  120. //total ssq now
  121. ssq += v_res[0];
  122. //tail
  123. if(j < n2){
  124. gvl = VSETVL(n2-j);
  125. v0 = VLEV_FLOAT(&x[j], gvl);
  126. //fabs(vector)
  127. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  128. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  129. //if scale change
  130. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  131. index = VMFIRSTM(mask, gvl);
  132. if(index == -1){//no elements greater than scale
  133. if(scale != 0.0)
  134. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  135. }else{//found greater element
  136. //find max
  137. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  138. //update ssq before max_index
  139. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  140. //update scale
  141. scale = v_res[0];
  142. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  143. }
  144. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  145. //ssq in vector vr: vr[0]
  146. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  147. //total ssq now
  148. ssq += v_res[0];
  149. }
  150. }else{
  151. gvl = VSETVL(n);
  152. vr = VFMVVF_FLOAT(0, gvl);
  153. v_zero = VFMVVF_FLOAT(0, gvl);
  154. unsigned int stride_x = inc_x * sizeof(FLOAT) * 2;
  155. int idx = 0, inc_v = inc_x * gvl * 2;
  156. for(i=0,j=0; i<n/gvl; i++){
  157. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  158. //fabs(vector)
  159. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  160. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  161. //if scale change
  162. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  163. index = VMFIRSTM(mask, gvl);
  164. if(index == -1){//no elements greater than scale
  165. if(scale != 0.0){
  166. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  167. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  168. }
  169. }else{//found greater element
  170. //ssq in vector vr: vr[0]
  171. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  172. //total ssq before current vector
  173. ssq += v_res[0];
  174. //find max
  175. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  176. //update ssq before max_index
  177. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  178. //update scale
  179. scale = v_res[0];
  180. //ssq in vector vr
  181. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  182. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  183. }
  184. v0 = VLSEV_FLOAT(&x[idx+1], stride_x, gvl);
  185. //fabs(vector)
  186. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  187. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  188. //if scale change
  189. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  190. index = VMFIRSTM(mask, gvl);
  191. if(index == -1){//no elements greater than scale
  192. if(scale != 0.0){
  193. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  194. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  195. }
  196. }else{//found greater element
  197. //ssq in vector vr: vr[0]
  198. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  199. //total ssq before current vector
  200. ssq += v_res[0];
  201. //find max
  202. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  203. //update ssq before max_index
  204. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  205. //update scale
  206. scale = v_res[0];
  207. //ssq in vector vr
  208. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  209. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  210. }
  211. j += gvl;
  212. idx += inc_v;
  213. }
  214. //ssq in vector vr: vr[0]
  215. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  216. //total ssq now
  217. ssq += v_res[0];
  218. //tail
  219. if(j < n){
  220. gvl = VSETVL(n-j);
  221. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  222. //fabs(vector)
  223. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  224. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  225. //if scale change
  226. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  227. index = VMFIRSTM(mask, gvl);
  228. if(index == -1){//no elements greater than scale
  229. if(scale != 0.0){
  230. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  231. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  232. }
  233. }else{//found greater element
  234. //find max
  235. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  236. //update ssq before max_index
  237. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  238. //update scale
  239. scale = v_res[0];
  240. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  241. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  242. }
  243. v0 = VLSEV_FLOAT(&x[idx+1], stride_x, gvl);
  244. //fabs(vector)
  245. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  246. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  247. //if scale change
  248. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  249. index = VMFIRSTM(mask, gvl);
  250. if(index == -1){//no elements greater than scale
  251. if(scale != 0.0){
  252. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  253. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  254. }
  255. }else{//found greater element
  256. //ssq in vector vr: vr[0]
  257. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  258. //total ssq before current vector
  259. ssq += v_res[0];
  260. //find max
  261. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  262. //update ssq before max_index
  263. ssq = ssq * (scale/v_res[0])*(scale/v_res[0]);
  264. //update scale
  265. scale = v_res[0];
  266. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  267. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  268. }
  269. //ssq in vector vr: vr[0]
  270. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  271. //total ssq now
  272. ssq += v_res[0];
  273. }
  274. }
  275. return(scale * sqrt(ssq));
  276. }