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