1 /***************************************************************************
2 Copyright (c) 2017, 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 
30 #include <arm_neon.h>
31 
32 #define	N	"x0"	/* vector length */
33 #define	X	"x1"	/* "X" vector address */
34 #define	INC_X	"x2"	/* "X" stride */
35 #define J	"x5"	/* loop variable */
36 
37 #define REG0	"wzr"
38 #define SUMF	"s0"
39 #define SUMFD	"d0"
40 
41 /******************************************************************************/
42 
43 #define KERNEL_F1					\
44 	"ldr	d1, ["X"]			\n"	\
45 	"add	"X", "X", #8			\n"	\
46 	"fabs	v1.2s, v1.2s			\n"	\
47 	"ext	v2.8b, v1.8b, v1.8b, #4		\n"	\
48 	"fadd	s1, s1, s2			\n"	\
49 	"fadd	"SUMF", "SUMF", s1		\n"
50 
51 #define KERNEL_F32					\
52 	"ldr	q16, ["X"]			\n"	\
53 	"ldr	q17, ["X", #16]			\n"	\
54 	"ldr	q18, ["X", #32]			\n"	\
55 	"ldr	q19, ["X", #48]			\n"	\
56 	"ldp	q20, q21, ["X", #64]		\n"	\
57 	"ldp	q22, q23, ["X", #96]		\n"	\
58 	"fabs	v16.4s, v16.4s			\n"	\
59 	"fabs	v17.4s, v17.4s			\n"	\
60 	"fabs	v18.4s, v18.4s			\n"	\
61 	"fabs	v19.4s, v19.4s			\n"	\
62 	"ldp	q24, q25, ["X", #128]		\n"	\
63 	"ldp	q26, q27, ["X", #160]		\n"	\
64 	"fabs	v20.4s, v20.4s			\n"	\
65 	"fabs	v21.4s, v21.4s			\n"	\
66 	"fabs	v22.4s, v22.4s			\n"	\
67 	"fabs	v23.4s, v23.4s			\n"	\
68 	"fadd	v16.4s, v16.4s, v17.4s		\n"	\
69 	"fadd	v18.4s, v18.4s, v19.4s		\n"	\
70 	"ldp	q28, q29, ["X", #192]		\n"	\
71 	"ldp	q30, q31, ["X", #224]		\n"	\
72 	"fabs	v24.4s, v24.4s			\n"	\
73 	"fabs	v25.4s, v25.4s			\n"	\
74 	"fabs	v26.4s, v26.4s			\n"	\
75 	"fabs	v27.4s, v27.4s			\n"	\
76 	"add	"X", "X", #256			\n"	\
77 	"fadd	v20.4s, v20.4s, v21.4s		\n"	\
78 	"fadd	v22.4s, v22.4s, v23.4s		\n"	\
79 	"fabs	v28.4s, v28.4s			\n"	\
80 	"fabs	v29.4s, v29.4s			\n"	\
81 	"fabs	v30.4s, v30.4s			\n"	\
82 	"fabs	v31.4s, v31.4s			\n"	\
83 	"PRFM	PLDL1KEEP, ["X", #1024]		\n"	\
84 	"PRFM	PLDL1KEEP, ["X", #1024+64]	\n"	\
85 	"fadd	v24.4s, v24.4s, v25.4s		\n"	\
86 	"fadd	v26.4s, v26.4s, v27.4s		\n"	\
87 	"fadd	v0.4s, v0.4s, v16.4s		\n"	\
88 	"fadd	v1.4s, v1.4s, v18.4s		\n"	\
89 	"fadd	v2.4s, v2.4s, v20.4s		\n"	\
90 	"fadd	v3.4s, v3.4s, v22.4s		\n"	\
91 	"PRFM	PLDL1KEEP, ["X", #1024+128]	\n"	\
92 	"PRFM	PLDL1KEEP, ["X", #1024+192]	\n"	\
93 	"fadd	v28.4s, v28.4s, v29.4s		\n"	\
94 	"fadd	v30.4s, v30.4s, v31.4s		\n"	\
95 	"fadd	v4.4s, v4.4s, v24.4s		\n"	\
96 	"fadd	v5.4s, v5.4s, v26.4s		\n"	\
97 	"fadd	v6.4s, v6.4s, v28.4s		\n"	\
98 	"fadd	v7.4s, v7.4s, v30.4s		\n"
99 
100 #define KERNEL_F32_FINALIZE				\
101 	"fadd	v0.4s, v0.4s, v1.4s		\n"	\
102 	"fadd	v2.4s, v2.4s, v3.4s		\n"	\
103 	"fadd	v4.4s, v4.4s, v5.4s		\n"	\
104 	"fadd	v6.4s, v6.4s, v7.4s		\n"	\
105 	"fadd	v0.4s, v0.4s, v2.4s		\n"	\
106 	"fadd	v4.4s, v4.4s, v6.4s		\n"	\
107 	"fadd	v0.4s, v0.4s, v4.4s		\n"	\
108 	"ext	v1.16b, v0.16b, v0.16b, #8	\n"	\
109 	"fadd	v0.2s, v0.2s, v1.2s		\n"	\
110 	"faddp	"SUMF", v0.2s			\n"
111 
112 #define INIT_S						\
113 	"lsl	"INC_X", "INC_X", #3		\n"
114 
115 #define KERNEL_S1					\
116 	"ldr	d1, ["X"]			\n"	\
117 	"add	"X", "X", "INC_X"		\n"	\
118 	"fabs	v1.2s, v1.2s			\n"	\
119 	"ext	v2.8b, v1.8b, v1.8b, #4		\n"	\
120 	"fadd	s1, s1, s2			\n"	\
121 	"fadd	"SUMF", "SUMF", s1		\n"
122 
123 
124 #if defined(SMP)
125 extern int blas_level1_thread_with_return_value(int mode, BLASLONG m, BLASLONG n,
126 	BLASLONG k, void *alpha, void *a, BLASLONG lda, void *b, BLASLONG ldb,
127 	void *c, BLASLONG ldc, int (*function)(), int nthreads);
128 #endif
129 
130 
casum_compute(BLASLONG n,FLOAT * x,BLASLONG inc_x)131 static FLOAT casum_compute(BLASLONG n, FLOAT *x, BLASLONG inc_x)
132 {
133 	FLOAT  asum = 0.0 ;
134 
135 	if ( n < 0 )  return(asum);
136 
137 	__asm__ __volatile__ (
138 	"	mov	"N", %[N_]			\n"
139 	"	mov	"X", %[X_]			\n"
140 	"	mov	"INC_X", %[INCX_]		\n"
141 	"	fmov	"SUMF", "REG0"			\n"
142 	"	fmov	s1, "REG0"			\n"
143 	"	fmov	s2, "REG0"			\n"
144 	"	fmov	s3, "REG0"			\n"
145 	"	fmov	s4, "REG0"			\n"
146 	"	fmov	s5, "REG0"			\n"
147 	"	fmov	s6, "REG0"			\n"
148 	"	fmov	s7, "REG0"			\n"
149 	"	cmp	"N", xzr			\n"
150 	"	ble	9f //asum_kernel_L999		\n"
151 	"	cmp	"INC_X", xzr			\n"
152 	"	ble	9f //asum_kernel_L999		\n"
153 	"	cmp	"INC_X", #1			\n"
154 	"	bne	5f //asum_kernel_S_BEGIN	\n"
155 
156 	"1: //asum_kernel_F_BEGIN:			\n"
157 	"	asr	"J", "N", #5			\n"
158 	"	cmp	"J", xzr			\n"
159 	"	beq	3f //asum_kernel_F1		\n"
160 
161 	"2: //asum_kernel_F32:				\n"
162 	"	"KERNEL_F32"				\n"
163 	"	subs	"J", "J", #1			\n"
164 	"	bne	2b //asum_kernel_F32		\n"
165 	"	"KERNEL_F32_FINALIZE"			\n"
166 
167 	"3: //asum_kernel_F1:				\n"
168 	"	ands	"J", "N", #31			\n"
169 	"	ble	9f //asum_kernel_L999		\n"
170 
171 	"4: //asum_kernel_F10:				\n"
172 	"	"KERNEL_F1"				\n"
173 	"	subs    "J", "J", #1			\n"
174 	"	bne	4b //asum_kernel_F10		\n"
175 	"	b	9f //asum_kernel_L999		\n"
176 
177 	"5: //asum_kernel_S_BEGIN:			\n"
178 	"	"INIT_S"				\n"
179 	"	asr	"J", "N", #2			\n"
180 	"	cmp	"J", xzr			\n"
181 	"	ble	7f //asum_kernel_S1		\n"
182 
183 	"6: //asum_kernel_S4:				\n"
184 	"	"KERNEL_S1"				\n"
185 	"	"KERNEL_S1"				\n"
186 	"	"KERNEL_S1"				\n"
187 	"	"KERNEL_S1"				\n"
188 	"	subs	"J", "J", #1			\n"
189 	"	bne	6b //asum_kernel_S4		\n"
190 
191 	"7: //asum_kernel_S1:				\n"
192 	"	ands	"J", "N", #3			\n"
193 	"	ble	9f //asum_kernel_L999		\n"
194 
195 	"8: //asum_kernel_S10:				\n"
196 	"	"KERNEL_S1"				\n"
197 	"	subs	"J", "J", #1			\n"
198 	"	bne	8b //asum_kernel_S10		\n"
199 
200 	"9: //asum_kernel_L999:				\n"
201 	"	fmov	%[ASUM_], "SUMFD"		\n"
202 
203 	: [ASUM_] "=r" (asum)		//%0
204 	: [N_]    "r"  (n),		//%1
205 	  [X_]    "r"  (x),		//%2
206 	  [INCX_] "r"  (inc_x)		//%3
207 	: "cc",
208 	  "memory",
209 	  "x0", "x1", "x2", "x3", "x4", "x5",
210 	  "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7"
211 	);
212 
213 	return asum;
214 }
215 
216 #if defined(SMP)
casum_thread_function(BLASLONG n,BLASLONG dummy0,BLASLONG dummy1,FLOAT dummy2,FLOAT * x,BLASLONG inc_x,FLOAT * y,BLASLONG inc_y,FLOAT * result,BLASLONG dummy3)217 static int casum_thread_function(BLASLONG n, BLASLONG dummy0,
218 	BLASLONG dummy1, FLOAT dummy2, FLOAT *x, BLASLONG inc_x, FLOAT *y,
219 	BLASLONG inc_y, FLOAT *result, BLASLONG dummy3)
220 {
221 	*result = casum_compute(n, x, inc_x);
222 
223 	return 0;
224 }
225 #endif
226 
CNAME(BLASLONG n,FLOAT * x,BLASLONG inc_x)227 FLOAT CNAME(BLASLONG n, FLOAT *x, BLASLONG inc_x)
228 {
229 #if defined(SMP)
230 	int nthreads;
231 	FLOAT dummy_alpha;
232 #endif
233 	FLOAT asum = 0.0;
234 
235 #if defined(SMP)
236 	if (inc_x == 0 || n <= 10000)
237 		nthreads = 1;
238 	else
239 		nthreads = num_cpu_avail(1);
240 
241 	if (nthreads == 1) {
242 		asum = casum_compute(n, x, inc_x);
243 	} else {
244 		int mode, i;
245 		char result[MAX_CPU_NUMBER * sizeof(double) * 2];
246 		FLOAT *ptr;
247 
248 		mode = BLAS_SINGLE  | BLAS_COMPLEX;
249 
250 		blas_level1_thread_with_return_value(mode, n, 0, 0, &dummy_alpha,
251 				   x, inc_x, NULL, 0, result, 0,
252 				   ( void *)casum_thread_function, nthreads);
253 
254 		ptr = (FLOAT *)result;
255 		for (i = 0; i < nthreads; i++) {
256 			asum = asum + (*ptr);
257 			ptr = (FLOAT *)(((char *)ptr) + sizeof(double) * 2);
258 		}
259 	}
260 #else
261 	asum = casum_compute(n, x, inc_x);
262 #endif
263 
264 	return asum;
265 }
266