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 
28 #include "common.h"
29 #include <math.h>
30 #include <float.h>
31 #if !defined(DOUBLE)
32 #define VSETVL(n) vsetvl_e32m8(n)
33 #define VSETVL_MAX vsetvlmax_e32m1()
34 #define FLOAT_V_T vfloat32m8_t
35 #define FLOAT_V_T_M1 vfloat32m1_t
36 #define VLEV_FLOAT vle_v_f32m8
37 #define VLSEV_FLOAT vlse_v_f32m8
38 #define VFREDMINVS_FLOAT vfredmin_vs_f32m8_f32m1
39 #define VFMVVF_FLOAT vfmv_v_f_f32m8
40 #define VFMVVF_FLOAT_M1 vfmv_v_f_f32m1
41 #define VFMINVV_FLOAT vfmin_vv_f32m8
42 #else
43 #define VSETVL(n) vsetvl_e64m8(n)
44 #define VSETVL_MAX vsetvlmax_e64m1()
45 #define FLOAT_V_T vfloat64m8_t
46 #define FLOAT_V_T_M1 vfloat64m1_t
47 #define VLEV_FLOAT vle_v_f64m8
48 #define VLSEV_FLOAT vlse_v_f64m8
49 #define VFREDMINVS_FLOAT vfredmin_vs_f64m8_f64m1
50 #define VFMVVF_FLOAT vfmv_v_f_f64m8
51 #define VFMVVF_FLOAT_M1 vfmv_v_f_f64m1
52 #define VFMINVV_FLOAT vfmin_vv_f64m8
53 #endif
54 
CNAME(BLASLONG n,FLOAT * x,BLASLONG inc_x)55 FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
56 {
57 	BLASLONG i=0, j=0;
58 	if (n <= 0 || inc_x <= 0) return(0.0);
59 	FLOAT minf=FLT_MAX;
60         unsigned int gvl = 0;
61         FLOAT_V_T v0, v1, v_min;
62         FLOAT_V_T_M1 v_res, v_max;
63         gvl = VSETVL_MAX;
64         v_res = VFMVVF_FLOAT_M1(0, gvl);
65         v_max = VFMVVF_FLOAT_M1(FLT_MAX, gvl);
66 
67         if(inc_x == 1){
68                 gvl = VSETVL(n);
69                 if(gvl <= n/2){
70                         v_min = VFMVVF_FLOAT(FLT_MAX, gvl);
71                         for(i=0,j=0; i<n/(gvl*2); i++){
72                                 v0 = VLEV_FLOAT(&x[j], gvl);
73                                 v_min = VFMINVV_FLOAT(v_min, v0, gvl);
74 
75                                 v1 = VLEV_FLOAT(&x[j+gvl], gvl);
76                                 v_min = VFMINVV_FLOAT(v_min, v1, gvl);
77                                 j += gvl * 2;
78                         }
79                         v_res = VFREDMINVS_FLOAT(v_res, v_min, v_max, gvl);
80                         minf = v_res[0];
81                 }
82                 for(;j<n;){
83                         gvl = VSETVL(n-j);
84                         v0 = VLEV_FLOAT(&x[j], gvl);
85                         v_res = VFREDMINVS_FLOAT(v_res, v0, v_max, gvl);
86                         if(v_res[0] < minf)
87                                 minf = v_res[0];
88                         j += gvl;
89                 }
90         }else{
91                 gvl = VSETVL(n);
92                 BLASLONG stride_x = inc_x * sizeof(FLOAT);
93                 if(gvl <= n/2){
94                         v_min = VFMVVF_FLOAT(FLT_MAX, gvl);
95                         BLASLONG idx = 0, inc_xv = inc_x * gvl;
96                         for(i=0,j=0; i<n/(gvl*2); i++){
97                                 v0 = VLSEV_FLOAT(&x[idx], stride_x, gvl);
98                                 v_min = VFMINVV_FLOAT(v_min, v0, gvl);
99 
100                                 v1 = VLSEV_FLOAT(&x[idx+inc_xv], stride_x, gvl);
101                                 v_min = VFMINVV_FLOAT(v_min, v1, gvl);
102                                 j += gvl * 2;
103                                 idx += inc_xv * 2;
104                         }
105                         v_res = VFREDMINVS_FLOAT(v_res, v_min, v_max, gvl);
106                         minf = v_res[0];
107                 }
108                 for(;j<n;){
109                         gvl = VSETVL(n-j);
110                         v0 = VLSEV_FLOAT(&x[j*inc_x], stride_x, gvl);
111                         v_res = VFREDMINVS_FLOAT(v_res, v0, v_max, gvl);
112                         if(v_res[0] < minf)
113                                 minf = v_res[0];
114                         j += gvl;
115                 }
116         }
117 	return(minf);
118 }
119 
120 
121