Skip to content

Commit f860e82

Browse files
authored
Merge pull request #4522 from martin-frbg/arm64scsum
Fix SCSUM on ARMV8 and add optimized CSUM/ZSUM for ARMV8SVE
2 parents 1c93e6a + 7d50698 commit f860e82

4 files changed

Lines changed: 496 additions & 0 deletions

File tree

kernel/arm64/KERNEL

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
CSUMKERNEL=csum.S
2+
13
ifndef SNRM2KERNEL
24
SNRM2KERNEL = ../arm/nrm2.c
35
endif

kernel/arm64/KERNEL.ARMV8SVE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
CSUMKERNEL = csum_thunderx2t99.c
3+
ZSUMKERNEL = zsum_thunderx2t99.c
14
SAMINKERNEL = ../arm/amin.c
25
DAMINKERNEL = ../arm/amin.c
36
CAMINKERNEL = ../arm/zamin.c

kernel/arm64/csum_thunderx2t99.c

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

0 commit comments

Comments
 (0)