Debian Codegraph @

modules/lsp-dsp-lib/include/private/dsp/arch/aarch64/asimd/convolution.h

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-dsp-lib
6 * Created on: 31 мар. 2020 г.
7 *
8 * lsp-dsp-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-dsp-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-dsp-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef PRIVATE_DSP_ARCH_AARCH64_ASIMD_CONVOLUTION_H_
23 #define PRIVATE_DSP_ARCH_AARCH64_ASIMD_CONVOLUTION_H_
24
25 #ifndef PRIVATE_DSP_ARCH_AARCH64_ASIMD_IMPL
26 #error "This header should not be included directly"
27 #endif /* PRIVATE_DSP_ARCH_AARCH64_ASIMD_IMPL */
28
29 namespace lsp
30 {
31 namespace asimd
32 {
33 void convolve(float *dst, const float *src, const float *conv, size_t length, size_t count)
34 {
35 IF_ARCH_AARCH64(
36 const float *c;
37 float *d;
38 size_t clen;
39 )
40
41 ARCH_AARCH64_ASM(
42 // 4x blocks
43 __ASM_EMIT("subs %[count], %[count], #4")
44 __ASM_EMIT("b.lo 200f")
45 __ASM_EMIT("eor v17.16b, v17.16b, v17.16b") // v17 = 0 0 0 0
46 __ASM_EMIT("100:")
47 __ASM_EMIT("mov %[d], %[dst]")
48 __ASM_EMIT("mov %[c], %[conv]")
49 __ASM_EMIT("ld4r {v4.4s, v5.4s, v6.4s, v7.4s}, [%[k]]")
50 __ASM_EMIT("eor v16.16b, v16.16b, v16.16b") // v16 = p0 p0 p0 p0 = 0 0 0 0 (history)
51 // v4 = k0 k0 k0 k0
52 // v5 = k1 k1 k1 k1
53 // v6 = k2 k2 k2 k2
54 // v7 = k3 k3 k3 k3
55 // 8x convolution
56 __ASM_EMIT("subs %[clen], %[length], #8")
57 __ASM_EMIT("b.lo 10f")
58 __ASM_EMIT("11:")
59 __ASM_EMIT("ldp q8, q9, [%[c]]") // v8 = c0 c1 c2 c3, v9 = c4 c5 c6 c7
60 __ASM_EMIT("ldp q0, q1, [%[d]]") // v0 = d0 d1 d2 d3, v1 = d4 d5 d6 d7
61 __ASM_EMIT("ext v10.16b, v16.16b, v8.16b, #12") // v10 = p3 c0 c1 c2
62 __ASM_EMIT("ext v11.16b, v8.16b, v9.16b, #12") // v11 = c3 c4 c5 c6
63 __ASM_EMIT("ext v12.16b, v16.16b, v8.16b, #8") // v12 = p2 p3 c0 c1
64 __ASM_EMIT("ext v13.16b, v8.16b, v9.16b, #8") // v13 = c2 c3 c4 c5
65 __ASM_EMIT("ext v14.16b, v16.16b, v8.16b, #4") // v14 = p1 p2 p3 c0
66 __ASM_EMIT("ext v15.16b, v8.16b, v9.16b, #4") // v15 = c1 c2 c3 c4
67 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s") // v0 = d0+k0*c0 d1+k0*c1 d2+k0*c2 d3+k0*c3
68 __ASM_EMIT("fmla v1.4s, v4.4s, v9.4s") // v1 = d4+k0*c4 d5+k0*c5 d6+k0*c6 d7+k0*c7
69 __ASM_EMIT("fmla v0.4s, v5.4s, v10.4s") // v0 = d0 + k0*c0 + k1*p3 ...
70 __ASM_EMIT("fmla v1.4s, v5.4s, v11.4s") // v1 = d1 + k0*c4 + k1*c3 ...
71 __ASM_EMIT("fmla v0.4s, v6.4s, v12.4s") // v0 = d0 + k0*c0 + k1*p3 + k2*p2 ...
72 __ASM_EMIT("fmla v1.4s, v6.4s, v13.4s") // v1 = d1 + k0*c4 + k1*c3 + k2*c2 ...
73 __ASM_EMIT("fmla v0.4s, v7.4s, v14.4s") // v0 = d0 + k0*c0 + k1*p3 + k2*p2 + k3*p1 ...
74 __ASM_EMIT("fmla v1.4s, v7.4s, v15.4s") // v1 = d1 + k0*c4 + k1*c3 + k2*c2 + k3*c1 ...
75 __ASM_EMIT("mov v16.16b, v9.16b") // v16 = c4 c5 c6 c7
76 __ASM_EMIT("stp q0, q1, [%[d]]") // v0 = d0 d1 d2 d3, v1 = d4 d5 d6 d7
77 __ASM_EMIT("subs %[clen], %[clen], #8")
78 __ASM_EMIT("add %[c], %[c], #0x20") // c += 8
79 __ASM_EMIT("add %[d], %[d], #0x20") // d += 8
80 __ASM_EMIT("b.hs 11b")
81 __ASM_EMIT("10:")
82 // 4x convolution
83 __ASM_EMIT("adds %[clen], %[clen], #4")
84 __ASM_EMIT("b.lo 12f")
85 __ASM_EMIT("ldr q8, [%[c]]") // v8 = c0 c1 c2 c3
86 __ASM_EMIT("ldr q0, [%[d]]") // v0 = d0 d1 d2 d3
87 __ASM_EMIT("ext v10.16b, v16.16b, v8.16b, #12") // v10 = p3 c0 c1 c2
88 __ASM_EMIT("ext v12.16b, v16.16b, v8.16b, #8") // v12 = p2 p3 c0 c1
89 __ASM_EMIT("ext v14.16b, v16.16b, v8.16b, #4") // v14 = p1 p2 p3 c0
90 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s") // v0 = d0+k0*c0 d1+k0*c1 d2+k0*c2 d3+k0*c3
91 __ASM_EMIT("fmla v0.4s, v5.4s, v10.4s") // v0 = d0 + k0*c0 + k1*p3 ...
92 __ASM_EMIT("fmla v0.4s, v6.4s, v12.4s") // v0 = d0 + k0*c0 + k1*p3 + k2*p2 ...
93 __ASM_EMIT("fmla v0.4s, v7.4s, v14.4s") // v0 = d0 + k0*c0 + k1*p3 + k2*p2 + k3*p1 ...
94 __ASM_EMIT("mov v16.16b, v8.16b") // v16 = c0 c1 c2 c3
95 __ASM_EMIT("str q0, [%[d]]")
96 __ASM_EMIT("sub %[clen], %[clen], #4")
97 __ASM_EMIT("add %[c], %[c], #0x10") // c += 4
98 __ASM_EMIT("add %[d], %[d], #0x10") // d += 4
99 __ASM_EMIT("12:")
100 // Apply tail: v16 = p0 p1 p2 p3
101 __ASM_EMIT("ldr d0, [%[d], #0x00]") // v0 = d0 d1
102 __ASM_EMIT("ldr s1, [%[d], #0x08]") // v1 = d2
103 __ASM_EMIT("ext v10.16b, v16.16b, v17.16b, #12") // v10 = p3 0 0 0
104 __ASM_EMIT("mov v0.s[2], v1.s[0]") // v0 = d0 d1 d2
105 __ASM_EMIT("ext v12.16b, v16.16b, v17.16b, #8") // v12 = p2 p3 0 0
106 __ASM_EMIT("ext v14.16b, v16.16b, v17.16b, #4") // v14 = p1 p2 p3 0
107 __ASM_EMIT("fmla v0.4s, v5.4s, v10.4s") // v0 = d0 + k1*p3 ...
108 __ASM_EMIT("fmla v0.4s, v6.4s, v12.4s") // v0 = d0 + k1*p3 + k2*p2 ...
109 __ASM_EMIT("fmla v0.4s, v7.4s, v14.4s") // v0 = d0 + k1*p3 + k2*p2 + k3*p1 ...
110 __ASM_EMIT("str d0, [%[d], #0x00]")
111 __ASM_EMIT("mov v1.s[0], v0.s[2]")
112 __ASM_EMIT("str s1, [%[d], #0x08]")
113 // 1x convolution
114 __ASM_EMIT("adds %[clen], %[clen], #3")
115 __ASM_EMIT("b.lo 14f")
116 __ASM_EMIT("ldr q4, [%[k]]") // q4 = k0 k1 k2 k3
117 __ASM_EMIT("13:")
118 __ASM_EMIT("ld1r {v8.4s}, [%[c]]") // v8 = c0 c0 c0 c0
119 __ASM_EMIT("ldr q0, [%[d]]") // v0 = d0 d1 d2 d3
120 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s") // v0 = d0+k0*c0 d1+k1*c0 d2+k2*c0 d3+k3*c0
121 __ASM_EMIT("str q0, [%[d]]")
122 __ASM_EMIT("subs %[clen], %[clen], #1")
123 __ASM_EMIT("add %[c], %[c], #0x04")
124 __ASM_EMIT("add %[d], %[d], #0x04")
125 __ASM_EMIT("b.ge 13b")
126 __ASM_EMIT("14:")
127 __ASM_EMIT("subs %[count], %[count], #4")
128 __ASM_EMIT("add %[dst], %[dst], #0x10")
129 __ASM_EMIT("add %[k], %[k], #0x10")
130 __ASM_EMIT("b.ge 100b")
131
132 // 1x blocks
133 __ASM_EMIT("200:")
134 __ASM_EMIT("adds %[count], %[count], #3")
135 __ASM_EMIT("b.lt 400f")
136 __ASM_EMIT("300:")
137 __ASM_EMIT("ld1r {v8.4s}, [%[k]]") // v8 = k0 k0 k0 k0
138 __ASM_EMIT("mov %[d], %[dst]")
139 __ASM_EMIT("mov v9.16b, v8.16b") // v9 = k0 k0 k0 k0
140 __ASM_EMIT("mov %[c], %[conv]")
141 // 16x convolution
142 __ASM_EMIT("subs %[clen], %[length], #16")
143 __ASM_EMIT("b.lo 20f")
144 __ASM_EMIT("21:")
145 __ASM_EMIT("ldp q0, q1, [%[d], #0x00]")
146 __ASM_EMIT("ldp q2, q3, [%[d], #0x20]")
147 __ASM_EMIT("ldp q4, q5, [%[c], #0x00]")
148 __ASM_EMIT("ldp q6, q7, [%[c], #0x20]")
149 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s")
150 __ASM_EMIT("fmla v1.4s, v5.4s, v9.4s")
151 __ASM_EMIT("fmla v2.4s, v6.4s, v8.4s")
152 __ASM_EMIT("fmla v3.4s, v7.4s, v9.4s")
153 __ASM_EMIT("stp q0, q1, [%[d], #0x00]")
154 __ASM_EMIT("stp q2, q3, [%[d], #0x20]")
155 __ASM_EMIT("subs %[clen], %[clen], #16")
156 __ASM_EMIT("add %[c], %[c], #0x40")
157 __ASM_EMIT("add %[d], %[d], #0x40")
158 __ASM_EMIT("b.hs 21b")
159 __ASM_EMIT("20:")
160 // 8x convolution
161 __ASM_EMIT("adds %[clen], %[clen], #8")
162 __ASM_EMIT("b.lo 22f")
163 __ASM_EMIT("ldp q0, q1, [%[d], #0x00]")
164 __ASM_EMIT("ldp q4, q5, [%[c], #0x00]")
165 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s")
166 __ASM_EMIT("fmla v1.4s, v5.4s, v9.4s")
167 __ASM_EMIT("stp q0, q1, [%[d], #0x00]")
168 __ASM_EMIT("sub %[clen], %[clen], #8")
169 __ASM_EMIT("add %[c], %[c], #0x20")
170 __ASM_EMIT("add %[d], %[d], #0x20")
171 __ASM_EMIT("22:")
172 // 4x convolution
173 __ASM_EMIT("adds %[clen], %[clen], #4")
174 __ASM_EMIT("b.lo 24f")
175 __ASM_EMIT("ldr q0, [%[d], #0x00]")
176 __ASM_EMIT("ldr q4, [%[c], #0x00]")
177 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s")
178 __ASM_EMIT("str q0, [%[d], #0x00]")
179 __ASM_EMIT("sub %[clen], %[clen], #4")
180 __ASM_EMIT("add %[c], %[c], #0x10")
181 __ASM_EMIT("add %[d], %[d], #0x10")
182 __ASM_EMIT("24:")
183 // 1x tail convolution
184 __ASM_EMIT("adds %[clen], %[clen], #3")
185 __ASM_EMIT("b.lo 26f")
186 __ASM_EMIT("25:")
187 __ASM_EMIT("ld1r {v0.4s}, [%[d]]")
188 __ASM_EMIT("ld1r {v4.4s}, [%[c]]")
189 __ASM_EMIT("fmla v0.4s, v4.4s, v8.4s")
190 __ASM_EMIT("st1 {v0.s}[0], [%[d]]")
191 __ASM_EMIT("subs %[clen], %[clen], #1")
192 __ASM_EMIT("add %[c], %[c], #0x04")
193 __ASM_EMIT("add %[d], %[d], #0x04")
194 __ASM_EMIT("b.ge 25b")
195 __ASM_EMIT("26:")
196 __ASM_EMIT("subs %[count], %[count], #1")
197 __ASM_EMIT("add %[dst], %[dst], #0x04")
198 __ASM_EMIT("add %[k], %[k], #0x04")
199 __ASM_EMIT("b.ge 300b")
200
201 __ASM_EMIT("400:")
202 : [dst] "+r" (dst),
203 [k] "+r" (src), [count] "+r" (count),
204 [c] "=&r" (c), [d] "=&r" (d),
205 [clen] "=&r" (clen)
206 : [conv] "r" (conv), [length] "r" (length)
207 : "cc", "memory",
208 "v0", "v1", "v2", "v3",
209 "v4", "v5", "v6", "v7",
210 "v8", "v9", "v10", "v11",
211 "v12", "v13", "v14", "v15",
212 "v16", "v17"
213 );
214 }
215 }
216 }
217
218 #endif /* PRIVATE_DSP_ARCH_AARCH64_ASIMD_CONVOLUTION_H_ */