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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 RVV_EFLOAT RVV_E64
  33. #define RVV_M RVV_M8
  34. #define FLOAT_V_T float64xm8_t
  35. #define VLEV_FLOAT vlev_float64xm8
  36. #define VLSEV_FLOAT vlsev_float64xm8
  37. #define VFREDMINVS_FLOAT vfredminvs_float64xm8
  38. #define MASK_T e64xm8_t
  39. #define VMFLTVF_FLOAT vmfltvf_e64xm8_float64xm8
  40. #define VMFLTVV_FLOAT vmfltvv_e64xm8_float64xm8
  41. #define VFMVVF_FLOAT vfmvvf_float64xm8
  42. #define VFRSUBVF_MASK_FLOAT vfrsubvf_mask_float64xm8
  43. #define VFMINVV_FLOAT vfminvv_float64xm8
  44. #define VMFLEVF_FLOAT vmflevf_e64xm8_float64xm8
  45. #define VMFIRSTM vmfirstm_e64xm8
  46. #define UINT_V_T uint64xm8_t
  47. #define VIDV_MASK_UINT vidv_mask_uint64xm8
  48. #define VIDV_UINT vidv_uint64xm8
  49. #define VADDVX_MASK_UINT vaddvx_mask_uint64xm8
  50. #define VADDVX_UINT vaddvx_uint64xm8
  51. #define VMVVX_UINT vmvvx_uint64xm8
  52. #else
  53. #define ABS fabsf
  54. #define RVV_EFLOAT RVV_E32
  55. #define RVV_M RVV_M8
  56. #define FLOAT_V_T float32xm8_t
  57. #define VLEV_FLOAT vlev_float32xm8
  58. #define VLSEV_FLOAT vlsev_float32xm8
  59. #define VFREDMINVS_FLOAT vfredminvs_float32xm8
  60. #define MASK_T e32xm8_t
  61. #define VMFLTVF_FLOAT vmfltvf_e32xm8_float32xm8
  62. #define VMFLTVV_FLOAT vmfltvv_e32xm8_float32xm8
  63. #define VFMVVF_FLOAT vfmvvf_float32xm8
  64. #define VFRSUBVF_MASK_FLOAT vfrsubvf_mask_float32xm8
  65. #define VFMINVV_FLOAT vfminvv_float32xm8
  66. #define VMFLEVF_FLOAT vmflevf_e32xm8_float32xm8
  67. #define VMFIRSTM vmfirstm_e32xm8
  68. #define UINT_V_T uint32xm8_t
  69. #define VIDV_MASK_UINT vidv_mask_uint32xm8
  70. #define VIDV_UINT vidv_uint32xm8
  71. #define VADDVX_MASK_UINT vaddvx_mask_uint32xm8
  72. #define VADDVX_UINT vaddvx_uint32xm8
  73. #define VMVVX_UINT vmvvx_uint32xm8
  74. #endif
  75. BLASLONG CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  76. {
  77. BLASLONG i=0, j=0;
  78. FLOAT minf=FLT_MAX;
  79. unsigned int min_index = 0;
  80. if (n <= 0 || inc_x <= 0) return(min_index);
  81. FLOAT_V_T vx, v_min;
  82. UINT_V_T v_min_index;
  83. MASK_T mask;
  84. unsigned int gvl = 0;
  85. if(inc_x == 1){
  86. gvl = vsetvli(n, RVV_EFLOAT, RVV_M);
  87. v_min = VFMVVF_FLOAT(FLT_MAX, gvl);
  88. v_min_index = VMVVX_UINT(0, gvl);
  89. for(i=0,j=0; i < n/gvl; i++){
  90. vx = VLEV_FLOAT(&x[j], gvl);
  91. //fabs(vector)
  92. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  93. vx = VFRSUBVF_MASK_FLOAT(vx, vx, 0, mask, gvl);
  94. //index where element less than v_min
  95. mask = VMFLTVV_FLOAT(vx, v_min, gvl);
  96. v_min_index = VIDV_MASK_UINT(v_min_index, mask, gvl);
  97. v_min_index = VADDVX_MASK_UINT(v_min_index, v_min_index, j, mask, gvl);
  98. //update v_min and start_index j
  99. v_min = VFMINVV_FLOAT(v_min, vx, gvl);
  100. j += gvl;
  101. }
  102. vx = VFMVVF_FLOAT(FLT_MAX, gvl);
  103. vx = VFREDMINVS_FLOAT(v_min, vx, gvl);
  104. minf = vx[0];
  105. mask = VMFLEVF_FLOAT(v_min, minf, gvl);
  106. min_index = VMFIRSTM(mask,gvl);
  107. min_index = v_min_index[min_index];
  108. if(j < n){
  109. gvl = vsetvli(n-j, RVV_EFLOAT, RVV_M);
  110. vx = VLEV_FLOAT(&x[j], gvl);
  111. //fabs(vector)
  112. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  113. v_min = VFRSUBVF_MASK_FLOAT(vx, vx, 0, mask, gvl);
  114. vx = VFMVVF_FLOAT(FLT_MAX, gvl);
  115. vx = VFREDMINVS_FLOAT(v_min, vx, gvl);
  116. FLOAT cur_minf = vx[0];
  117. if(cur_minf < minf){
  118. //tail index
  119. v_min_index = VIDV_UINT(gvl);
  120. v_min_index = VADDVX_UINT(v_min_index, j, gvl);
  121. mask = VMFLEVF_FLOAT(v_min, cur_minf, gvl);
  122. min_index = VMFIRSTM(mask,gvl);
  123. min_index = v_min_index[min_index];
  124. }
  125. }
  126. }else{
  127. gvl = vsetvli(n, RVV_EFLOAT, RVV_M);
  128. unsigned int stride_x = inc_x * sizeof(FLOAT);
  129. unsigned int idx = 0, inc_v = gvl * inc_x;
  130. v_min = VFMVVF_FLOAT(FLT_MAX, gvl);
  131. v_min_index = VMVVX_UINT(0, gvl);
  132. for(i=0,j=0; i < n/gvl; i++){
  133. vx = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  134. //fabs(vector)
  135. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  136. vx = VFRSUBVF_MASK_FLOAT(vx, vx, 0, mask, gvl);
  137. //index where element less than v_min
  138. mask = VMFLTVV_FLOAT(vx, v_min, gvl);
  139. v_min_index = VIDV_MASK_UINT(v_min_index, mask, gvl);
  140. v_min_index = VADDVX_MASK_UINT(v_min_index, v_min_index, j, mask, gvl);
  141. //update v_min and start_index j
  142. v_min = VFMINVV_FLOAT(v_min, vx, gvl);
  143. j += gvl;
  144. idx += inc_v;
  145. }
  146. vx = VFMVVF_FLOAT(FLT_MAX, gvl);
  147. vx = VFREDMINVS_FLOAT(v_min, vx, gvl);
  148. minf = vx[0];
  149. mask = VMFLEVF_FLOAT(v_min, minf, gvl);
  150. min_index = VMFIRSTM(mask,gvl);
  151. min_index = v_min_index[min_index];
  152. if(j < n){
  153. gvl = vsetvli(n-j, RVV_EFLOAT, RVV_M);
  154. vx = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  155. //fabs(vector)
  156. mask = VMFLTVF_FLOAT(vx, 0, gvl);
  157. v_min = VFRSUBVF_MASK_FLOAT(vx, vx, 0, mask, gvl);
  158. vx = VFMVVF_FLOAT(FLT_MAX, gvl);
  159. vx = VFREDMINVS_FLOAT(v_min, vx, gvl);
  160. FLOAT cur_minf = vx[0];
  161. if(cur_minf < minf){
  162. //tail index
  163. v_min_index = VIDV_UINT(gvl);
  164. v_min_index = VADDVX_UINT(v_min_index, j, gvl);
  165. mask = VMFLEVF_FLOAT(v_min, cur_minf, gvl);
  166. min_index = VMFIRSTM(mask,gvl);
  167. min_index = v_min_index[min_index];
  168. }
  169. }
  170. }
  171. return(min_index+1);
  172. }