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.

max_vector.c 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifdef RISCV64_ZVL256B
  31. # define LMUL m2
  32. # if defined(DOUBLE)
  33. # define ELEN 64
  34. # define MLEN 32
  35. # else
  36. # define ELEN 32
  37. # define MLEN 16
  38. # endif
  39. #else
  40. # define LMUL m8
  41. # if defined(DOUBLE)
  42. # define ELEN 64
  43. # define MLEN 8
  44. # else
  45. # define ELEN 32
  46. # define MLEN 4
  47. # endif
  48. #endif
  49. #define _
  50. #define JOIN2_X(x, y) x ## y
  51. #define JOIN2(x, y) JOIN2_X(x, y)
  52. #define JOIN(v, w, x, y, z) JOIN2( JOIN2( JOIN2( JOIN2( v, w ), x), y), z)
  53. #define VSETVL JOIN(RISCV_RVV(vsetvl), _e, ELEN, LMUL, _)
  54. #define FLOAT_V_T JOIN(vfloat, ELEN, LMUL, _t, _)
  55. #define FLOAT_V_T_M1 JOIN(vfloat, ELEN, m1, _t, _)
  56. #define VLEV_FLOAT JOIN(RISCV_RVV(vle), ELEN, _v_f, ELEN, LMUL)
  57. #define VLSEV_FLOAT JOIN(RISCV_RVV(vlse), ELEN, _v_f, ELEN, LMUL)
  58. #ifdef RISCV_0p10_INTRINSICS
  59. #define VFREDMAXVS_FLOAT(va, vb, gvl) JOIN(RISCV_RVV(vfredmax_vs_f), ELEN, LMUL, _f, JOIN2( ELEN, m1))(v_res, va, vb, gvl)
  60. #else
  61. #define VFREDMAXVS_FLOAT JOIN(RISCV_RVV(vfredmax_vs_f), ELEN, LMUL, _f, JOIN2( ELEN, m1))
  62. #endif
  63. #define MASK_T JOIN(vbool, MLEN, _t, _, _)
  64. #define VMFLTVF_FLOAT JOIN(RISCV_RVV(vmflt_vf_f), ELEN, LMUL, _b, MLEN)
  65. #define VFMVVF_FLOAT JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, LMUL, _)
  66. #define VFMVVF_FLOAT_M1 JOIN(RISCV_RVV(vfmv), _v_f_f, ELEN, m1, _)
  67. #define VFMAXVV_FLOAT JOIN(RISCV_RVV(vfmax), _vv_f, ELEN, LMUL, _)
  68. FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
  69. {
  70. BLASLONG i=0, j=0;
  71. if (n <= 0 || inc_x <= 0) return(0.0);
  72. FLOAT maxf=-FLT_MAX;
  73. unsigned int gvl = 0;
  74. FLOAT_V_T v0, v1, v_max;
  75. FLOAT_V_T_M1 v_res;
  76. v_res = VFMVVF_FLOAT_M1(-FLT_MAX, 1);
  77. if(inc_x == 1){
  78. gvl = VSETVL(n);
  79. if(gvl <= n/2){
  80. v_max = VFMVVF_FLOAT(-FLT_MAX, gvl);
  81. for(i=0,j=0; i<n/(gvl*2); i++){
  82. v0 = VLEV_FLOAT(&x[j], gvl);
  83. v_max = VFMAXVV_FLOAT(v_max, v0, gvl);
  84. v1 = VLEV_FLOAT(&x[j+gvl], gvl);
  85. v_max = VFMAXVV_FLOAT(v_max, v1, gvl);
  86. j += gvl * 2;
  87. }
  88. v_res = VFREDMAXVS_FLOAT(v_max, v_res, gvl);
  89. }
  90. for(;j<n;){
  91. gvl = VSETVL(n-j);
  92. v0 = VLEV_FLOAT(&x[j], gvl);
  93. v_res = VFREDMAXVS_FLOAT(v0, v_res, gvl);
  94. j += gvl;
  95. }
  96. }else{
  97. gvl = VSETVL(n);
  98. BLASLONG stride_x = inc_x * sizeof(FLOAT);
  99. if(gvl <= n/2){
  100. v_max = VFMVVF_FLOAT(-FLT_MAX, gvl);
  101. BLASLONG idx = 0, inc_xv = inc_x * gvl;
  102. for(i=0,j=0; i<n/(gvl*2); i++){
  103. v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
  104. v_max = VFMAXVV_FLOAT(v_max, v0, gvl);
  105. v1 = VLSEV_FLOAT(&x[idx+inc_xv], stride_x, gvl);
  106. v_max = VFMAXVV_FLOAT(v_max, v1, gvl);
  107. j += gvl * 2;
  108. idx += inc_xv * 2;
  109. }
  110. v_res = VFREDMAXVS_FLOAT(v_max, v_res, gvl);
  111. }
  112. for(;j<n;){
  113. gvl = VSETVL(n-j);
  114. v0 = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
  115. v_res = VFREDMAXVS_FLOAT(v0, v_res, gvl);
  116. j += gvl;
  117. }
  118. }
  119. maxf = EXTRACT_FLOAT(v_res);
  120. return(maxf);
  121. }