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.

iamin_vector.c 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 ABS fabs
  32. #define VSETVL(n) vsetvl_e64m8(n)
  33. #define VSETVL_MAX vsetvlmax_e64m1()
  34. #define FLOAT_V_T vfloat64m8_t
  35. #define FLOAT_V_T_M1 vfloat64m1_t
  36. #define VLEV_FLOAT vle64_v_f64m8
  37. #define VLSEV_FLOAT vlse64_v_f64m8
  38. #define VFREDMINVS_FLOAT vfredmin_vs_f64m8_f64m1
  39. #define MASK_T vbool8_t
  40. #define VMFLTVF_FLOAT vmflt_vf_f64m8_b8
  41. #define VMFLTVV_FLOAT vmflt_vv_f64m8_b8
  42. #define VFMVVF_FLOAT vfmv_v_f_f64m8
  43. #define VFMVVF_FLOAT_M1 vfmv_v_f_f64m1
  44. #define VFRSUBVF_MASK_FLOAT vfrsub_vf_f64m8_m
  45. #define VFMINVV_FLOAT vfmin_vv_f64m8
  46. #define VMFLEVF_FLOAT vmfle_vf_f64m8_b8
  47. #define VMFIRSTM vmfirst_m_b8
  48. #define UINT_V_T vuint64m8_t
  49. #define VIDV_MASK_UINT vid_v_u64m8_m
  50. #define VIDV_UINT vid_v_u64m8
  51. #define VADDVX_MASK_UINT vadd_vx_u64m8_m
  52. #define VADDVX_UINT vadd_vx_u64m8
  53. #define VMVVX_UINT vmv_v_x_u64m8
  54. #else
  55. #define ABS fabsf
  56. #define VSETVL(n) vsetvl_e32m8(n)
  57. #define VSETVL_MAX vsetvlmax_e32m1()
  58. #define FLOAT_V_T vfloat32m8_t
  59. #define FLOAT_V_T_M1 vfloat32m1_t
  60. #define VLEV_FLOAT vle32_v_f32m8
  61. #define VLSEV_FLOAT vlse32_v_f32m8
  62. #define VFREDMINVS_FLOAT vfredmin_vs_f32m8_f32m1
  63. #define MASK_T vbool4_t
  64. #define VMFLTVF_FLOAT vmflt_vf_f32m8_b4
  65. #define VMFLTVV_FLOAT vmflt_vv_f32m8_b4
  66. #define VFMVVF_FLOAT vfmv_v_f_f32m8
  67. #define VFMVVF_FLOAT_M1 vfmv_v_f_f32m1
  68. #define VFRSUBVF_MASK_FLOAT vfrsub_vf_f32m8_m
  69. #define VFMINVV_FLOAT vfmin_vv_f32m8
  70. #define VMFLEVF_FLOAT vmfle_vf_f32m8_b4
  71. #define VMFIRSTM vmfirst_m_b4
  72. #define UINT_V_T vuint32m8_t
  73. #define VIDV_MASK_UINT vid_v_u32m8_m
  74. #define VIDV_UINT vid_v_u32m8
  75. #define VADDVX_MASK_UINT vadd_vx_u32m8_m
  76. #define VADDVX_UINT vadd_vx_u32m8
  77. #define VMVVX_UINT vmv_v_x_u32m8
  78. #endif
  79. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  80. {
  81. BLASLONG i=0, j=0;
  82. FLOAT minf=FLT_MAX;
  83. unsigned int min_index = 0;
  84. if (n <= 0 || inc_x <= 0) return(min_index);
  85. FLOAT_V_T vx, v_min;
  86. UINT_V_T v_min_index;
  87. MASK_T mask;
  88. unsigned int gvl = 0;
  89. FLOAT_V_T_M1 v_res, v_max;
  90. gvl = VSETVL_MAX;
  91. v_res = VFMVVF_FLOAT_M1(0, gvl);
  92. v_max = VFMVVF_FLOAT_M1(FLT_MAX, gvl);
  93. if(inc_x == 1){
  94. gvl = VSETVL(n);
  95. v_min = VFMVVF_FLOAT(FLT_MAX, gvl);
  96. v_min_index = VMVVX_UINT(0, gvl);
  97. for(i=0,j=0; i < n/gvl; i++){
  98. vx = VLEV_FLOAT(&x[j], gvl);
  99. //fabs(vector)
  100. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  101. vx = VFRSUBVF_MASK_FLOAT(mask, vx, vx, 0, gvl);
  102. //index where element less than v_min
  103. mask = VMFLTVV_FLOAT(vx, v_min, gvl);
  104. v_min_index = VIDV_MASK_UINT(mask, v_min_index, gvl);
  105. v_min_index = VADDVX_MASK_UINT(mask, v_min_index, v_min_index, j, gvl);
  106. //update v_min and start_index j
  107. v_min = VFMINVV_FLOAT(v_min, vx, gvl);
  108. j += gvl;
  109. }
  110. v_res = VFREDMINVS_FLOAT(v_res, v_min, v_max, gvl);
  111. minf = *((FLOAT*)&v_res);
  112. mask = VMFLEVF_FLOAT(v_min, minf, gvl);
  113. min_index = VMFIRSTM(mask,gvl);
  114. min_index = *((unsigned int*)&v_min_index+min_index);
  115. if(j < n){
  116. gvl = VSETVL(n-j);
  117. vx = VLEV_FLOAT(&x[j], gvl);
  118. //fabs(vector)
  119. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  120. v_min = VFRSUBVF_MASK_FLOAT(mask, vx, vx, 0, gvl);
  121. v_res = VFREDMINVS_FLOAT(v_res, v_min, v_max, gvl);
  122. FLOAT cur_minf = *((FLOAT*)&v_res);
  123. if(cur_minf < minf){
  124. //tail index
  125. v_min_index = VIDV_UINT(gvl);
  126. v_min_index = VADDVX_UINT(v_min_index, j, gvl);
  127. mask = VMFLEVF_FLOAT(v_min, cur_minf, gvl);
  128. min_index = VMFIRSTM(mask,gvl);
  129. min_index = *((unsigned int*)&v_min_index+min_index);
  130. }
  131. }
  132. }else{
  133. gvl = VSETVL(n);
  134. unsigned int stride_x = inc_x * sizeof(FLOAT);
  135. unsigned int idx = 0, inc_v = gvl * inc_x;
  136. v_min = VFMVVF_FLOAT(FLT_MAX, gvl);
  137. v_min_index = VMVVX_UINT(0, gvl);
  138. for(i=0,j=0; i < n/gvl; i++){
  139. vx = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  140. //fabs(vector)
  141. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  142. vx = VFRSUBVF_MASK_FLOAT(mask, vx, vx, 0, gvl);
  143. //index where element less than v_min
  144. mask = VMFLTVV_FLOAT(vx, v_min, gvl);
  145. v_min_index = VIDV_MASK_UINT(mask, v_min_index, gvl);
  146. v_min_index = VADDVX_MASK_UINT(mask, v_min_index, v_min_index, j, gvl);
  147. //update v_min and start_index j
  148. v_min = VFMINVV_FLOAT(v_min, vx, gvl);
  149. j += gvl;
  150. idx += inc_v;
  151. }
  152. v_res = VFREDMINVS_FLOAT(v_res, v_min, v_max, gvl);
  153. minf = *((FLOAT*)&v_res);
  154. mask = VMFLEVF_FLOAT(v_min, minf, gvl);
  155. min_index = VMFIRSTM(mask,gvl);
  156. min_index = *((unsigned int*)&v_min_index+min_index);
  157. if(j < n){
  158. gvl = VSETVL(n-j);
  159. vx = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  160. //fabs(vector)
  161. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  162. v_min = VFRSUBVF_MASK_FLOAT(mask, vx, vx, 0, gvl);
  163. v_res = VFREDMINVS_FLOAT(v_res, v_min, v_max, gvl);
  164. FLOAT cur_minf = *((FLOAT*)&v_res);
  165. if(cur_minf < minf){
  166. //tail index
  167. v_min_index = VIDV_UINT(gvl);
  168. v_min_index = VADDVX_UINT(v_min_index, j, gvl);
  169. mask = VMFLEVF_FLOAT(v_min, cur_minf, gvl);
  170. min_index = VMFIRSTM(mask,gvl);
  171. min_index = *((unsigned int*)&v_min_index+min_index);
  172. }
  173. }
  174. }
  175. return(min_index+1);
  176. }