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.

imax_vector.c 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include <math.h>
  29. #include <float.h>
  30. #if defined(DOUBLE)
  31. #define VSETVL(n) __riscv_vsetvl_e64m8(n)
  32. #define FLOAT_V_T vfloat64m8_t
  33. #define FLOAT_V_T_M1 vfloat64m1_t
  34. #define VLEV_FLOAT __riscv_vle64_v_f64m8
  35. #define VLSEV_FLOAT __riscv_vlse64_v_f64m8
  36. #define VFREDMAXVS_FLOAT __riscv_vfredmax_vs_f64m8_f64m1
  37. #define MASK_T vbool8_t
  38. #define VMFLTVV_FLOAT __riscv_vmflt_vv_f64m8_b8
  39. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f64m8
  40. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f64m1
  41. #define VFMAXVV_FLOAT __riscv_vfmax_vv_f64m8
  42. #define VMFGEVF_FLOAT __riscv_vmfge_vf_f64m8_b8
  43. #define VMFIRSTM __riscv_vfirst_m_b8
  44. #define UINT_V_T vuint64m8_t
  45. #define VIDV_MASK_UINT __riscv_vid_v_u64m8_mu
  46. #define VIDV_UINT __riscv_vid_v_u64m8
  47. #define VADDVX_MASK_UINT __riscv_vadd_vx_u64m8_mu
  48. #define VADDVX_UINT __riscv_vadd_vx_u64m8
  49. #define VMVVX_UINT __riscv_vmv_v_x_u64m8
  50. #define VCOMPRESS __riscv_vcompress_vm_u64m8
  51. #define VMV_X __riscv_vmv_x_s_u64m8_u64
  52. #else
  53. #define VSETVL(n) __riscv_vsetvl_e32m8(n)
  54. #define FLOAT_V_T vfloat32m8_t
  55. #define FLOAT_V_T_M1 vfloat32m1_t
  56. #define VLEV_FLOAT __riscv_vle32_v_f32m8
  57. #define VLSEV_FLOAT __riscv_vlse32_v_f32m8
  58. #define VFREDMAXVS_FLOAT __riscv_vfredmax_vs_f32m8_f32m1
  59. #define MASK_T vbool4_t
  60. #define VMFLTVV_FLOAT __riscv_vmflt_vv_f32m8_b4
  61. #define VFMVVF_FLOAT __riscv_vfmv_v_f_f32m8
  62. #define VFMVVF_FLOAT_M1 __riscv_vfmv_v_f_f32m1
  63. #define VFMAXVV_FLOAT __riscv_vfmax_vv_f32m8
  64. #define VMFGEVF_FLOAT __riscv_vmfge_vf_f32m8_b4
  65. #define VMFIRSTM __riscv_vfirst_m_b4
  66. #define UINT_V_T vuint32m8_t
  67. #define VIDV_MASK_UINT __riscv_vid_v_u32m8_mu
  68. #define VIDV_UINT __riscv_vid_v_u32m8
  69. #define VADDVX_MASK_UINT __riscv_vadd_vx_u32m8_mu
  70. #define VADDVX_UINT __riscv_vadd_vx_u32m8
  71. #define VMVVX_UINT __riscv_vmv_v_x_u32m8
  72. #define VCOMPRESS __riscv_vcompress_vm_u32m8
  73. #define VMV_X __riscv_vmv_x_s_u32m8_u32
  74. #endif
  75. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  76. {
  77. BLASLONG i=0, j=0;
  78. unsigned int max_index = 0;
  79. if (n <= 0 || inc_x <= 0) return(max_index);
  80. FLOAT maxf=-FLT_MAX;
  81. FLOAT_V_T vx, v_max;
  82. UINT_V_T v_max_index;
  83. MASK_T mask;
  84. unsigned int gvl = 0;
  85. FLOAT_V_T_M1 v_res;
  86. v_res = VFMVVF_FLOAT_M1(-FLT_MAX, 1);
  87. if(inc_x == 1){
  88. gvl = VSETVL(n);
  89. v_max_index = VMVVX_UINT(0, gvl);
  90. v_max = VFMVVF_FLOAT(-FLT_MAX, gvl);
  91. for(i=0,j=0; i < n/gvl; i++){
  92. vx = VLEV_FLOAT(&x[j], gvl);
  93. //index where element greater than v_max
  94. mask = VMFLTVV_FLOAT(v_max, vx, gvl);
  95. v_max_index = VIDV_MASK_UINT(mask, v_max_index, gvl);
  96. v_max_index = VADDVX_MASK_UINT(mask, v_max_index, v_max_index, j, gvl);
  97. //update v_max and start_index j
  98. v_max = VFMAXVV_FLOAT(v_max, vx, gvl);
  99. j += gvl;
  100. }
  101. v_res = VFREDMAXVS_FLOAT(v_max, v_res, gvl);
  102. maxf = EXTRACT_FLOAT(v_res);
  103. mask = VMFGEVF_FLOAT(v_max, maxf, gvl);
  104. UINT_V_T compressed;
  105. compressed = VCOMPRESS(v_max_index, mask, gvl);
  106. max_index = VMV_X(compressed);
  107. if(j < n){
  108. gvl = VSETVL(n-j);
  109. v_max = VLEV_FLOAT(&x[j], gvl);
  110. v_res = VFREDMAXVS_FLOAT(v_max, v_res, gvl);
  111. FLOAT cur_maxf = EXTRACT_FLOAT(v_res);
  112. if(cur_maxf > maxf){
  113. //tail index
  114. v_max_index = VIDV_UINT(gvl);
  115. v_max_index = VADDVX_UINT(v_max_index, j, gvl);
  116. mask = VMFGEVF_FLOAT(v_max, cur_maxf, gvl);
  117. UINT_V_T compressed;
  118. compressed = VCOMPRESS(v_max_index, mask, gvl);
  119. max_index = VMV_X(compressed);
  120. }
  121. }
  122. }else{
  123. gvl = VSETVL(n);
  124. unsigned int stride_x = inc_x * sizeof(FLOAT);
  125. unsigned int idx = 0, inc_v = gvl * inc_x;
  126. v_max = VFMVVF_FLOAT(-FLT_MAX, gvl);
  127. v_max_index = VMVVX_UINT(0, gvl);
  128. for(i=0,j=0; i < n/gvl; i++){
  129. vx = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  130. //index where element greater than v_max
  131. mask = VMFLTVV_FLOAT(v_max, vx, gvl);
  132. v_max_index = VIDV_MASK_UINT(mask, v_max_index, gvl);
  133. v_max_index = VADDVX_MASK_UINT(mask, v_max_index, v_max_index, j, gvl);
  134. //update v_max and start_index j
  135. v_max = VFMAXVV_FLOAT(v_max, vx, gvl);
  136. j += gvl;
  137. idx += inc_v;
  138. }
  139. v_res = VFREDMAXVS_FLOAT(v_max, v_res, gvl);
  140. maxf = EXTRACT_FLOAT(v_res);
  141. mask = VMFGEVF_FLOAT(v_max, maxf, gvl);
  142. UINT_V_T compressed;
  143. compressed = VCOMPRESS(v_max_index, mask, gvl);
  144. max_index = VMV_X(compressed);
  145. if(j < n){
  146. gvl = VSETVL(n-j);
  147. v_max = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  148. v_res = VFREDMAXVS_FLOAT(v_max, v_res, gvl);
  149. FLOAT cur_maxf = EXTRACT_FLOAT(v_res);
  150. if(cur_maxf > maxf){
  151. //tail index
  152. v_max_index = VIDV_UINT(gvl);
  153. v_max_index = VADDVX_UINT(v_max_index, j, gvl);
  154. mask = VMFGEVF_FLOAT(v_max, cur_maxf, gvl);
  155. UINT_V_T compressed;
  156. compressed = VCOMPRESS(v_max_index, mask, gvl);
  157. max_index = VMV_X(compressed);
  158. }
  159. }
  160. }
  161. return(max_index+1);
  162. }