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_rvv.c 12 kB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /***************************************************************************
  2. Copyright (c) 2022, 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) __riscv_vsetvl_e32m4(n)
  30. #define VSETVL_MAX __riscv_vsetvlmax_e32m4()
  31. #define FLOAT_V_T vfloat32m4_t
  32. #define FLOAT_V_T_M1 vfloat32m1_t
  33. #define MASK_T vbool8_t
  34. #define VLEV_FLOAT __riscv_vle32_v_f32m4
  35. #define VLSEV_FLOAT __riscv_vlse32_v_f32m4
  36. #define VFREDSUM_FLOAT __riscv_vfredusum_vs_f32m4_f32m1_tu
  37. #define VFMACCVV_FLOAT_TU __riscv_vfmacc_vv_f32m4_tu
  38. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f32m4
  39. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f32m1
  40. #define VMFIRSTM __riscv_vfirst_m_b8
  41. #define VFREDMAXVS_FLOAT_TU __riscv_vfredmax_vs_f32m4_f32m1_tu
  42. #define VFMVFS_FLOAT __riscv_vfmv_f_s_f32m1_f32
  43. #define VMFGTVF_FLOAT __riscv_vmfgt_vf_f32m4_b8
  44. #define VFDIVVF_FLOAT __riscv_vfdiv_vf_f32m4
  45. #define VFABSV_FLOAT __riscv_vfabs_v_f32m4
  46. #else
  47. #define VSETVL(n) __riscv_vsetvl_e64m4(n)
  48. #define VSETVL_MAX __riscv_vsetvlmax_e64m4()
  49. #define FLOAT_V_T vfloat64m4_t
  50. #define FLOAT_V_T_M1 vfloat64m1_t
  51. #define MASK_T vbool16_t
  52. #define VLEV_FLOAT __riscv_vle64_v_f64m4
  53. #define VLSEV_FLOAT __riscv_vlse64_v_f64m4
  54. #define VFREDSUM_FLOAT __riscv_vfredusum_vs_f64m4_f64m1_tu
  55. #define VFMACCVV_FLOAT_TU __riscv_vfmacc_vv_f64m4_tu
  56. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f64m4
  57. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f64m1
  58. #define VMFIRSTM __riscv_vfirst_m_b16
  59. #define VFREDMAXVS_FLOAT_TU __riscv_vfredmax_vs_f64m4_f64m1_tu
  60. #define VFMVFS_FLOAT __riscv_vfmv_f_s_f64m1_f64
  61. #define VMFGTVF_FLOAT __riscv_vmfgt_vf_f64m4_b16
  62. #define VFDIVVF_FLOAT __riscv_vfdiv_vf_f64m4
  63. #define VFABSV_FLOAT __riscv_vfabs_v_f64m4
  64. #endif
  65. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  66. {
  67. BLASLONG i=0, j=0;
  68. if (n <= 0 || inc_x == 0) return(0.0);
  69. FLOAT_V_T vr, v0, v_zero;
  70. unsigned int gvl = 0;
  71. FLOAT_V_T_M1 v_res, v_z0;
  72. gvl = VSETVL_MAX;
  73. v_res = VFMVVF_FLOAT_M1(0, gvl);
  74. v_z0 = VFMVVF_FLOAT_M1(0, gvl);
  75. FLOAT scale = 0.0, ssq = 0.0;
  76. MASK_T mask;
  77. BLASLONG index = 0;
  78. if (inc_x == 1) {
  79. BLASLONG n2 = n * 2;
  80. gvl = VSETVL(n2);
  81. vr = VFMVVF_FLOAT(0, gvl);
  82. v_zero = VFMVVF_FLOAT(0, gvl);
  83. for (i=0,j=0; i<n2/gvl; i++) {
  84. v0 = VLEV_FLOAT(&x[j], gvl);
  85. //fabs(vector)
  86. v0 = VFABSV_FLOAT(v0, gvl);
  87. //if scale change
  88. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  89. index = VMFIRSTM(mask, gvl);
  90. if (index == -1) { //no elements greater than scale
  91. if (scale != 0.0) {
  92. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  93. vr = VFMACCVV_FLOAT_TU(vr, v0, v0, gvl);
  94. }
  95. } else { // found greater element
  96. //ssq in vector vr: vr[0]
  97. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  98. //total ssq before current vector
  99. ssq += VFMVFS_FLOAT(v_res);
  100. //find max
  101. v_res = VFREDMAXVS_FLOAT_TU(v_res, v0, v_z0, gvl);
  102. //update ssq before max_index
  103. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  104. //update scale
  105. scale = VFMVFS_FLOAT(v_res);
  106. //ssq in vector vr
  107. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  108. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  109. }
  110. j += gvl;
  111. }
  112. //ssq in vector vr: vr[0]
  113. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  114. //total ssq now
  115. ssq += VFMVFS_FLOAT(v_res);
  116. //tail
  117. if(j < n2){
  118. gvl = VSETVL(n2-j);
  119. v0 = VLEV_FLOAT(&x[j], gvl);
  120. // fabs(vector)
  121. v0 = VFABSV_FLOAT(v0, gvl);
  122. // if scale change
  123. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  124. index = VMFIRSTM(mask, gvl);
  125. if (index == -1) {//no elements greater than scale
  126. if(scale != 0.0)
  127. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  128. } else { //found greater element
  129. //find max
  130. v_res = VFREDMAXVS_FLOAT_TU(v_res, v0, v_z0, gvl);
  131. //update ssq before max_index
  132. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  133. //update scale
  134. scale = VFMVFS_FLOAT(v_res);
  135. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  136. }
  137. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  138. //ssq in vector vr: vr[0]
  139. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  140. //total ssq now
  141. ssq += VFMVFS_FLOAT(v_res);
  142. }
  143. } else {
  144. gvl = VSETVL(n);
  145. vr = VFMVVF_FLOAT(0, gvl);
  146. v_zero = VFMVVF_FLOAT(0, gvl);
  147. unsigned int stride_x = inc_x * sizeof(FLOAT) * 2;
  148. int idx = 0, inc_v = inc_x * gvl * 2;
  149. for (i=0,j=0; i<n/gvl; i++) {
  150. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  151. // fabs(vector)
  152. v0 = VFABSV_FLOAT(v0, gvl);
  153. //if scale change
  154. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  155. index = VMFIRSTM(mask, gvl);
  156. if (index == -1) { // no elements greater than scale
  157. if(scale != 0.0){
  158. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  159. vr = VFMACCVV_FLOAT_TU(vr, v0, v0, gvl);
  160. }
  161. } else {//found greater element
  162. //ssq in vector vr: vr[0]
  163. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  164. //total ssq before current vector
  165. ssq += VFMVFS_FLOAT(v_res);
  166. //find max
  167. v_res = VFREDMAXVS_FLOAT_TU(v_res, v0, v_z0, gvl);
  168. //update ssq before max_index
  169. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  170. //update scale
  171. scale = VFMVFS_FLOAT(v_res);
  172. //ssq in vector vr
  173. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  174. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  175. }
  176. v0 = VLSEV_FLOAT(&x[idx+1], stride_x, gvl);
  177. //fabs(vector)
  178. v0 = VFABSV_FLOAT(v0, gvl);
  179. //if scale change
  180. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  181. index = VMFIRSTM(mask, gvl);
  182. if (index == -1) { // no elements greater than scale
  183. if(scale != 0.0) {
  184. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  185. vr = VFMACCVV_FLOAT_TU(vr, v0, v0, gvl);
  186. }
  187. } else { // found greater element
  188. //ssq in vector vr: vr[0]
  189. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  190. //total ssq before current vector
  191. ssq += VFMVFS_FLOAT(v_res);
  192. //find max
  193. v_res = VFREDMAXVS_FLOAT_TU(v_res, v0, v_z0, gvl);
  194. //update ssq before max_index
  195. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  196. //update scale
  197. scale = VFMVFS_FLOAT(v_res);
  198. //ssq in vector vr
  199. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  200. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  201. }
  202. j += gvl;
  203. idx += inc_v;
  204. }
  205. //ssq in vector vr: vr[0]
  206. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  207. //total ssq now
  208. ssq += VFMVFS_FLOAT(v_res);
  209. //tail
  210. if (j < n) {
  211. gvl = VSETVL(n-j);
  212. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  213. //fabs(vector)
  214. v0 = VFABSV_FLOAT(v0, gvl);
  215. //if scale change
  216. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  217. index = VMFIRSTM(mask, gvl);
  218. if(index == -1) { // no elements greater than scale
  219. if(scale != 0.0) {
  220. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  221. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  222. }
  223. } else { // found greater element
  224. //find max
  225. v_res = VFREDMAXVS_FLOAT_TU(v_res, v0, v_z0, gvl);
  226. //update ssq before max_index
  227. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  228. //update scale
  229. scale = VFMVFS_FLOAT(v_res);
  230. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  231. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  232. }
  233. v0 = VLSEV_FLOAT(&x[idx+1], stride_x, gvl);
  234. //fabs(vector)
  235. v0 = VFABSV_FLOAT(v0, gvl);
  236. //if scale change
  237. mask = VMFGTVF_FLOAT(v0, scale, gvl);
  238. index = VMFIRSTM(mask, gvl);
  239. if (index == -1) {//no elements greater than scale
  240. if(scale != 0.0) {
  241. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  242. vr = VFMACCVV_FLOAT_TU(vr, v0, v0, gvl);
  243. }
  244. } else { // found greater element
  245. //ssq in vector vr: vr[0]
  246. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  247. //total ssq before current vector
  248. ssq += VFMVFS_FLOAT(v_res);
  249. //find max
  250. v_res = VFREDMAXVS_FLOAT_TU(v_res, v0, v_z0, gvl);
  251. //update ssq before max_index
  252. ssq = ssq * (scale/VFMVFS_FLOAT(v_res))*(scale/VFMVFS_FLOAT(v_res));
  253. //update scale
  254. scale = VFMVFS_FLOAT(v_res);
  255. v0 = VFDIVVF_FLOAT(v0, scale, gvl);
  256. vr = VFMACCVV_FLOAT_TU(v_zero, v0, v0, gvl);
  257. }
  258. //ssq in vector vr: vr[0]
  259. v_res = VFREDSUM_FLOAT(v_res, vr, v_z0, gvl);
  260. //total ssq now
  261. ssq += VFMVFS_FLOAT(v_res);
  262. }
  263. }
  264. return(scale * sqrt(ssq));
  265. }