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.

izamin_rvv.c 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include <float.h>
  29. #if defined(DOUBLE)
  30. #define VSETVL(n) __riscv_vsetvl_e64m4(n)
  31. #define VSETVL_MAX __riscv_vsetvlmax_e64m4()
  32. #define FLOAT_V_T vfloat64m4_t
  33. #define FLOAT_V_T_M1 vfloat64m1_t
  34. #define VLSEG_FLOAT __riscv_vlseg2e64_v_f64m4
  35. #define VLSSEG_FLOAT __riscv_vlsseg2e64_v_f64m4
  36. #define VFREDMINVS_FLOAT __riscv_vfredmin_vs_f64m4_f64m1
  37. #define MASK_T vbool16_t
  38. #define VMFLTVF_FLOAT __riscv_vmflt_vf_f64m4_b16
  39. #define VMFLTVV_FLOAT __riscv_vmflt_vv_f64m4_b16
  40. #define VMFLEVF_FLOAT __riscv_vmfle_vf_f64m4_b16
  41. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f64m4
  42. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f64m1
  43. #define VFABSV_FLOAT __riscv_vfabs_v_f64m4
  44. #define VFMINVV_FLOAT_TU __riscv_vfmin_vv_f64m4_tu
  45. #define VFADDVV_FLOAT __riscv_vfadd_vv_f64m4
  46. #define VFIRSTM __riscv_vfirst_m_b16
  47. #define UINT_V_T vuint64m4_t
  48. #define VIDV_MASK_UINT_TU __riscv_vid_v_u64m4_tumu
  49. #define VIDV_UINT __riscv_vid_v_u64m4
  50. #define VADDVX_MASK_UINT_TU __riscv_vadd_vx_u64m4_tumu
  51. #define VADDVX_UINT __riscv_vadd_vx_u64m4
  52. #define VMVVX_UINT __riscv_vmv_v_x_u64m4
  53. #define VFMVFS_FLOAT_M1 __riscv_vfmv_f_s_f64m1_f64
  54. #define VSLIDEDOWN_UINT __riscv_vslidedown_vx_u64m4
  55. #define VMVVXS_UINT __riscv_vmv_x_s_u64m4_u64
  56. #else
  57. #define VSETVL(n) __riscv_vsetvl_e32m4(n)
  58. #define VSETVL_MAX __riscv_vsetvlmax_e32m4()
  59. #define FLOAT_V_T vfloat32m4_t
  60. #define FLOAT_V_T_M1 vfloat32m1_t
  61. #define VLSEG_FLOAT __riscv_vlseg2e32_v_f32m4
  62. #define VLSSEG_FLOAT __riscv_vlsseg2e32_v_f32m4
  63. #define VFREDMINVS_FLOAT __riscv_vfredmin_vs_f32m4_f32m1
  64. #define MASK_T vbool8_t
  65. #define VMFLTVF_FLOAT __riscv_vmflt_vf_f32m4_b8
  66. #define VMFLTVV_FLOAT __riscv_vmflt_vv_f32m4_b8
  67. #define VMFLEVF_FLOAT __riscv_vmfle_vf_f32m4_b8
  68. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f32m4
  69. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f32m1
  70. #define VFABSV_FLOAT __riscv_vfabs_v_f32m4
  71. #define VFMINVV_FLOAT_TU __riscv_vfmin_vv_f32m4_tu
  72. #define VFADDVV_FLOAT __riscv_vfadd_vv_f32m4
  73. #define VFIRSTM __riscv_vfirst_m_b8
  74. #define UINT_V_T vuint32m4_t
  75. #define VIDV_MASK_UINT_TU __riscv_vid_v_u32m4_tumu
  76. #define VIDV_UINT __riscv_vid_v_u32m4
  77. #define VADDVX_MASK_UINT_TU __riscv_vadd_vx_u32m4_tumu
  78. #define VADDVX_UINT __riscv_vadd_vx_u32m4
  79. #define VMVVX_UINT __riscv_vmv_v_x_u32m4
  80. #define VFMVFS_FLOAT_M1 __riscv_vfmv_f_s_f32m1_f32
  81. #define VSLIDEDOWN_UINT __riscv_vslidedown_vx_u32m4
  82. #define VMVVXS_UINT __riscv_vmv_x_s_u32m4_u32
  83. #endif
  84. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  85. {
  86. unsigned int min_index = 0;
  87. if (n <= 0 || inc_x <= 0) return(min_index);
  88. FLOAT_V_T vx0, vx1, v_min;
  89. UINT_V_T v_min_index;
  90. MASK_T mask;
  91. size_t vlmax = VSETVL_MAX;
  92. v_min_index = VMVVX_UINT(0, vlmax);
  93. v_min = VFMVVF_FLOAT(FLT_MAX, vlmax);
  94. BLASLONG j=0;
  95. FLOAT minf=0.0;
  96. if(inc_x == 1) {
  97. for (size_t vl; n > 0; n -= vl, x += vl*2, j += vl) {
  98. vl = VSETVL(n);
  99. VLSEG_FLOAT(&vx0, &vx1, x, vl);
  100. vx0 = VFABSV_FLOAT(vx0, vl);
  101. vx1 = VFABSV_FLOAT(vx1, vl);
  102. vx0 = VFADDVV_FLOAT(vx0, vx1, vl);
  103. // index where element less than v_min
  104. mask = VMFLTVV_FLOAT(vx0, v_min, vl);
  105. v_min_index = VIDV_MASK_UINT_TU(mask, v_min_index, vl);
  106. v_min_index = VADDVX_MASK_UINT_TU(mask, v_min_index, v_min_index, j, vl);
  107. //update v_min and start_index j
  108. v_min = VFMINVV_FLOAT_TU(v_min, v_min, vx0, vl);
  109. }
  110. } else {
  111. BLASLONG stride_x = inc_x * sizeof(FLOAT) * 2;
  112. for (size_t vl; n > 0; n -= vl, x += vl*inc_x*2, j += vl) {
  113. vl = VSETVL(n);
  114. VLSSEG_FLOAT(&vx0, &vx1, x, stride_x, vl);
  115. vx0 = VFABSV_FLOAT(vx0, vl);
  116. vx1 = VFABSV_FLOAT(vx1, vl);
  117. vx0 = VFADDVV_FLOAT(vx0, vx1, vl);
  118. // index where element less than v_min
  119. mask = VMFLTVV_FLOAT(vx0, v_min, vl);
  120. v_min_index = VIDV_MASK_UINT_TU(mask, v_min_index, vl);
  121. v_min_index = VADDVX_MASK_UINT_TU(mask, v_min_index, v_min_index, j, vl);
  122. //update v_min and start_index j
  123. v_min = VFMINVV_FLOAT_TU(v_min, v_min, vx0, vl);
  124. }
  125. }
  126. FLOAT_V_T_M1 v_res;
  127. v_res = VFMVVF_FLOAT_M1(FLT_MAX, vlmax);
  128. v_res = VFREDMINVS_FLOAT(v_min, v_res, vlmax);
  129. minf = VFMVFS_FLOAT_M1(v_res);
  130. mask = VMFLEVF_FLOAT(v_min, minf, vlmax);
  131. min_index = VFIRSTM(mask, vlmax);
  132. v_min_index = VSLIDEDOWN_UINT(v_min_index, min_index, vlmax);
  133. min_index = VMVVXS_UINT(v_min_index);
  134. return(min_index+1);
  135. }