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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 VFMVFS_FLOAT vfmv_f_s_f32m1_f32
  34. #define VLEV_FLOAT vle32_v_f32m4
  35. #define VLSEV_FLOAT vlse32_v_f32m4
  36. #define VFREDSUM_FLOAT vfredusum_vs_f32m4_f32m1
  37. #define VFMACCVV_FLOAT vfmacc_vv_f32m4
  38. #define VFMVVF_FLOAT vfmv_v_f_f32m4
  39. #define VFMVVF_FLOAT_M1 vfmv_v_f_f32m1
  40. #define VFDOTVV_FLOAT vfdot_vv_f32m4
  41. #define ABS fabsf
  42. #define MASK_T vbool8_t
  43. #define VFRSUBVF_MASK_FLOAT vfrsub_vf_f32m4_m
  44. #define VMFGTVF_FLOAT vmfgt_vf_f32m4_b8
  45. #define VMFIRSTM vmfirst_m_b8
  46. #define VFDIVVF_FLOAT vfdiv_vf_f32m4
  47. #define VMFLTVF_FLOAT vmflt_vf_f32m4_b8
  48. #define VFREDMAXVS_FLOAT vfredmax_vs_f32m4_f32m1
  49. #else
  50. #define VSETVL(n) vsetvl_e64m4(n)
  51. #define VSETVL_MAX vsetvlmax_e64m1()
  52. #define FLOAT_V_T vfloat64m4_t
  53. #define FLOAT_V_T_M1 vfloat64m1_t
  54. #define VFMVFS_FLOAT vfmv_f_s_f64m1_f64
  55. #define VLEV_FLOAT vle64_v_f64m4
  56. #define VLSEV_FLOAT vlse64_v_f64m4
  57. #define VFREDSUM_FLOAT vfredusum_vs_f64m4_f64m1
  58. #define VFMACCVV_FLOAT vfmacc_vv_f64m4
  59. #define VFMVVF_FLOAT vfmv_v_f_f64m4
  60. #define VFMVVF_FLOAT_M1 vfmv_v_f_f64m1
  61. #define VFDOTVV_FLOAT vfdot_vv_f64m4
  62. #define ABS fabs
  63. #define MASK_T vbool16_t
  64. #define VFRSUBVF_MASK_FLOAT vfrsub_vf_f64m4_m
  65. #define VMFGTVF_FLOAT vmfgt_vf_f64m4_b16
  66. #define VMFIRSTM vmfirst_m_b16
  67. #define VFDIVVF_FLOAT vfdiv_vf_f64m4
  68. #define VMFLTVF_FLOAT vmflt_vf_f64m4_b16
  69. #define VFREDMAXVS_FLOAT vfredmax_vs_f64m4_f64m1
  70. #endif
  71. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  72. {
  73. BLASLONG i=0, j=0;
  74. if ( n < 0 ) return(0.0);
  75. // if(n == 1) return (ABS(x[0]));
  76. FLOAT_V_T vr, v0, v_zero;
  77. unsigned int gvl = 0;
  78. FLOAT_V_T_M1 v_res, v_z0;
  79. gvl = VSETVL_MAX;
  80. v_res = VFMVVF_FLOAT_M1(0, gvl);
  81. v_z0 = VFMVVF_FLOAT_M1(0, gvl);
  82. FLOAT scale = 0.0, ssq = 0.0;
  83. MASK_T mask;
  84. BLASLONG index = 0;
  85. if(inc_x == 1){
  86. BLASLONG n2 = n * 2;
  87. gvl = VSETVL(n2);
  88. vr = VFMVVF_FLOAT(0, gvl);
  89. v_zero = VFMVVF_FLOAT(0, gvl);
  90. for(i=0,j=0; i<n2/gvl; i++){
  91. v0 = VLEV_FLOAT(&x[j], gvl);
  92. //fabs(vector)
  93. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  94. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  95. //if scale change
  96. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  97. index = VMFIRSTM(mask, gvl);
  98. if(index == -1){//no elements greater than scale
  99. if(scale != 0.0){
  100. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  101. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  102. }
  103. }else{//found greater element
  104. //ssq in vector vr: vr[0]
  105. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  106. //total ssq before current vector
  107. ssq += VFMVFS_FLOAT(v_res);
  108. //find max
  109. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  110. //update ssq before max_index
  111. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  112. //update scale
  113. scale = VFMVFS_FLOAT(v_res);
  114. //ssq in vector vr
  115. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  116. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  117. }
  118. j += gvl;
  119. }
  120. //ssq in vector vr: vr[0]
  121. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  122. //total ssq now
  123. ssq += VFMVFS_FLOAT(v_res);
  124. //tail
  125. if(j < n2){
  126. gvl = VSETVL(n2-j);
  127. v0 = VLEV_FLOAT(&x[j], gvl);
  128. //fabs(vector)
  129. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  130. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  131. //if scale change
  132. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  133. index = VMFIRSTM(mask, gvl);
  134. if(index == -1){//no elements greater than scale
  135. if(scale != 0.0)
  136. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  137. }else{//found greater element
  138. //find max
  139. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  140. //update ssq before max_index
  141. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  142. //update scale
  143. scale = VFMVFS_FLOAT(v_res);
  144. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  145. }
  146. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  147. //ssq in vector vr: vr[0]
  148. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  149. //total ssq now
  150. ssq += VFMVFS_FLOAT(v_res);
  151. }
  152. }else{
  153. gvl = VSETVL(n);
  154. vr = VFMVVF_FLOAT(0, gvl);
  155. v_zero = VFMVVF_FLOAT(0, gvl);
  156. unsigned int stride_x = inc_x * sizeof(FLOAT) * 2;
  157. int idx = 0, inc_v = inc_x * gvl * 2;
  158. for(i=0,j=0; i<n/gvl; i++){
  159. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  160. //fabs(vector)
  161. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  162. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  163. //if scale change
  164. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  165. index = VMFIRSTM(mask, gvl);
  166. if(index == -1){//no elements greater than scale
  167. if(scale != 0.0){
  168. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  169. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  170. }
  171. }else{//found greater element
  172. //ssq in vector vr: vr[0]
  173. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  174. //total ssq before current vector
  175. ssq += VFMVFS_FLOAT(v_res);
  176. //find max
  177. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  178. //update ssq before max_index
  179. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  180. //update scale
  181. scale = VFMVFS_FLOAT(v_res);
  182. //ssq in vector vr
  183. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  184. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  185. }
  186. v0 = VLSEV_FLOAT(&x[idx+1], stride_x, gvl);
  187. //fabs(vector)
  188. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  189. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  190. //if scale change
  191. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  192. index = VMFIRSTM(mask, gvl);
  193. if(index == -1){//no elements greater than scale
  194. if(scale != 0.0){
  195. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  196. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  197. }
  198. }else{//found greater element
  199. //ssq in vector vr: vr[0]
  200. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  201. //total ssq before current vector
  202. ssq += VFMVFS_FLOAT(v_res);
  203. //find max
  204. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  205. //update ssq before max_index
  206. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  207. //update scale
  208. scale = VFMVFS_FLOAT(v_res);
  209. //ssq in vector vr
  210. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  211. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  212. }
  213. j += gvl;
  214. idx += inc_v;
  215. }
  216. //ssq in vector vr: vr[0]
  217. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  218. //total ssq now
  219. ssq += VFMVFS_FLOAT(v_res);
  220. //tail
  221. if(j < n){
  222. gvl = VSETVL(n-j);
  223. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  224. //fabs(vector)
  225. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  226. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  227. //if scale change
  228. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  229. index = VMFIRSTM(mask, gvl);
  230. if(index == -1){//no elements greater than scale
  231. if(scale != 0.0){
  232. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  233. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  234. }
  235. }else{//found greater element
  236. //find max
  237. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  238. //update ssq before max_index
  239. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  240. //update scale
  241. scale = VFMVFS_FLOAT(v_res);
  242. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  243. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  244. }
  245. v0 = VLSEV_FLOAT(&x[idx+1], stride_x, gvl);
  246. //fabs(vector)
  247. mask = VMFLTVF_FLOAT(v0, 0, gvl);
  248. v0 = VFRSUBVF_MASK_FLOAT(mask, v0, v0, 0, gvl);
  249. //if scale change
  250. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  251. index = VMFIRSTM(mask, gvl);
  252. if(index == -1){//no elements greater than scale
  253. if(scale != 0.0){
  254. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  255. vr = VFMACCVV_FLOAT(vr, v0, v0, gvl);
  256. }
  257. }else{//found greater element
  258. //ssq in vector vr: vr[0]
  259. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  260. //total ssq before current vector
  261. ssq += VFMVFS_FLOAT(v_res);
  262. //find max
  263. v_res = VFREDMAXVS_FLOAT(v_res, v0, v_z0, gvl);
  264. //update ssq before max_index
  265. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  266. //update scale
  267. scale = VFMVFS_FLOAT(v_res);
  268. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  269. vr = VFMACCVV_FLOAT(v_zero, v0, v0, gvl);
  270. }
  271. //ssq in vector vr: vr[0]
  272. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  273. //total ssq now
  274. ssq += VFMVFS_FLOAT(v_res);
  275. }
  276. }
  277. return(scale * sqrt(ssq));
  278. }