Debian Codegraph @

modules/lsp-dsp-lib/include/private/dsp/arch/x86/sse/hmath/hdotp.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_X86_SSE_HMATH_HDOTP_H_
23 #define PRIVATE_DSP_ARCH_X86_SSE_HMATH_HDOTP_H_
24
25 #ifndef PRIVATE_DSP_ARCH_X86_SSE_IMPL
26 #error "This header should not be included directly"
27 #endif /* PRIVATE_DSP_ARCH_X86_SSE_IMPL */
28
29 namespace lsp
30 {
31 namespace sse
32 {
33 float h_dotp(const float *a, const float *b, size_t count)
34 {
35 IF_ARCH_X86(
36 float result;
37 size_t off;
38 );
39 ARCH_X86_ASM
40 (
41 __ASM_EMIT("xorps %%xmm0, %%xmm0")
42 __ASM_EMIT("xor %[off], %[off]")
43 __ASM_EMIT("xorps %%xmm1, %%xmm1")
44 __ASM_EMIT("sub $12, %[count]")
45 __ASM_EMIT("jl 2f")
46
47 /* x12 Blocks */
48 __ASM_EMIT("1:")
49 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm2")
50 __ASM_EMIT("movups 0x10(%[a], %[off]), %%xmm3")
51 __ASM_EMIT("movups 0x20(%[a], %[off]), %%xmm4")
52 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm5")
53 __ASM_EMIT("movups 0x10(%[b], %[off]), %%xmm6")
54 __ASM_EMIT("movups 0x20(%[b], %[off]), %%xmm7")
55 __ASM_EMIT("mulps %%xmm5, %%xmm2")
56 __ASM_EMIT("mulps %%xmm6, %%xmm3")
57 __ASM_EMIT("mulps %%xmm7, %%xmm4")
58 __ASM_EMIT("addps %%xmm2, %%xmm0")
59 __ASM_EMIT("addps %%xmm3, %%xmm1")
60 __ASM_EMIT("addps %%xmm4, %%xmm0")
61 __ASM_EMIT("add $0x30, %[off]")
62 __ASM_EMIT("sub $12, %[count]")
63 __ASM_EMIT("jae 1b")
64 /* x8 Block */
65 __ASM_EMIT("2:")
66 __ASM_EMIT("add $4, %[count]")
67 __ASM_EMIT("jl 4f")
68 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm2")
69 __ASM_EMIT("movups 0x10(%[a], %[off]), %%xmm3")
70 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm5")
71 __ASM_EMIT("movups 0x10(%[b], %[off]), %%xmm6")
72 __ASM_EMIT("mulps %%xmm5, %%xmm2")
73 __ASM_EMIT("mulps %%xmm6, %%xmm3")
74 __ASM_EMIT("addps %%xmm2, %%xmm0")
75 __ASM_EMIT("addps %%xmm3, %%xmm1")
76 __ASM_EMIT("sub $8, %[count]")
77 __ASM_EMIT("add $0x20, %[off]")
78 /* x4 Block */
79 __ASM_EMIT("4:")
80 __ASM_EMIT("add $4, %[count]")
81 __ASM_EMIT("jl 6f")
82 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm2")
83 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm5")
84 __ASM_EMIT("mulps %%xmm5, %%xmm2")
85 __ASM_EMIT("addps %%xmm2, %%xmm0")
86 __ASM_EMIT("sub $4, %[count]")
87 __ASM_EMIT("add $0x10, %[off]")
88 /* x1 Blocks */
89 __ASM_EMIT("6:")
90 __ASM_EMIT("add $3, %[count]")
91 __ASM_EMIT("jl 8f")
92 __ASM_EMIT("7:")
93 __ASM_EMIT("movss 0x00(%[a], %[off]), %%xmm2")
94 __ASM_EMIT("movss 0x00(%[b], %[off]), %%xmm5")
95 __ASM_EMIT("mulss %%xmm5, %%xmm2")
96 __ASM_EMIT("addss %%xmm2, %%xmm0")
97 __ASM_EMIT("add $0x04, %[off]")
98 __ASM_EMIT("dec %[count]")
99 __ASM_EMIT("jge 7b")
100 /* x1 End */
101 __ASM_EMIT("8:")
102 __ASM_EMIT("addps %%xmm1, %%xmm0")
103 __ASM_EMIT("movhlps %%xmm0, %%xmm1")
104 __ASM_EMIT("addps %%xmm1, %%xmm0")
105 __ASM_EMIT("unpcklps %%xmm1, %%xmm0")
106 __ASM_EMIT("movhlps %%xmm0, %%xmm1")
107 __ASM_EMIT("addps %%xmm1, %%xmm0")
108
109 : [count] "+r" (count), [off] "=&r" (off),
110 "=Yz" (result)
111 : [a] "r" (a), [b] "r" (b)
112 : "cc",
113 "%xmm1", "%xmm2", "%xmm3",
114 "%xmm4", "%xmm5", "%xmm6", "%xmm7"
115 );
116
117 return result;
118 }
119
120 float h_sqr_dotp(const float *a, const float *b, size_t count)
121 {
122 IF_ARCH_X86(
123 float result;
124 size_t off;
125 );
126 ARCH_X86_ASM
127 (
128 __ASM_EMIT("xorps %%xmm0, %%xmm0")
129 __ASM_EMIT("xor %[off], %[off]")
130 __ASM_EMIT("xorps %%xmm1, %%xmm1")
131 __ASM_EMIT("sub $12, %[count]")
132 __ASM_EMIT("jl 2f")
133
134 /* x12 Blocks */
135 __ASM_EMIT("1:")
136 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm2")
137 __ASM_EMIT("movups 0x10(%[a], %[off]), %%xmm3")
138 __ASM_EMIT("movups 0x20(%[a], %[off]), %%xmm4")
139 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm5")
140 __ASM_EMIT("movups 0x10(%[b], %[off]), %%xmm6")
141 __ASM_EMIT("movups 0x20(%[b], %[off]), %%xmm7")
142 __ASM_EMIT("mulps %%xmm2, %%xmm2")
143 __ASM_EMIT("mulps %%xmm3, %%xmm3")
144 __ASM_EMIT("mulps %%xmm4, %%xmm4")
145 __ASM_EMIT("mulps %%xmm5, %%xmm5")
146 __ASM_EMIT("mulps %%xmm6, %%xmm6")
147 __ASM_EMIT("mulps %%xmm7, %%xmm7")
148 __ASM_EMIT("mulps %%xmm5, %%xmm2")
149 __ASM_EMIT("mulps %%xmm6, %%xmm3")
150 __ASM_EMIT("mulps %%xmm7, %%xmm4")
151 __ASM_EMIT("addps %%xmm2, %%xmm0")
152 __ASM_EMIT("addps %%xmm3, %%xmm1")
153 __ASM_EMIT("addps %%xmm4, %%xmm0")
154 __ASM_EMIT("add $0x30, %[off]")
155 __ASM_EMIT("sub $12, %[count]")
156 __ASM_EMIT("jae 1b")
157 /* x8 Block */
158 __ASM_EMIT("2:")
159 __ASM_EMIT("add $4, %[count]")
160 __ASM_EMIT("jl 4f")
161 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm2")
162 __ASM_EMIT("movups 0x10(%[a], %[off]), %%xmm3")
163 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm5")
164 __ASM_EMIT("movups 0x10(%[b], %[off]), %%xmm6")
165 __ASM_EMIT("mulps %%xmm2, %%xmm2")
166 __ASM_EMIT("mulps %%xmm3, %%xmm3")
167 __ASM_EMIT("mulps %%xmm5, %%xmm5")
168 __ASM_EMIT("mulps %%xmm6, %%xmm6")
169 __ASM_EMIT("mulps %%xmm5, %%xmm2")
170 __ASM_EMIT("mulps %%xmm6, %%xmm3")
171 __ASM_EMIT("addps %%xmm2, %%xmm0")
172 __ASM_EMIT("addps %%xmm3, %%xmm1")
173 __ASM_EMIT("sub $8, %[count]")
174 __ASM_EMIT("add $0x20, %[off]")
175 /* x4 Block */
176 __ASM_EMIT("4:")
177 __ASM_EMIT("add $4, %[count]")
178 __ASM_EMIT("jl 6f")
179 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm2")
180 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm5")
181 __ASM_EMIT("mulps %%xmm2, %%xmm2")
182 __ASM_EMIT("mulps %%xmm5, %%xmm5")
183 __ASM_EMIT("mulps %%xmm5, %%xmm2")
184 __ASM_EMIT("addps %%xmm2, %%xmm0")
185 __ASM_EMIT("sub $4, %[count]")
186 __ASM_EMIT("add $0x10, %[off]")
187 /* x1 Blocks */
188 __ASM_EMIT("6:")
189 __ASM_EMIT("add $3, %[count]")
190 __ASM_EMIT("jl 8f")
191 __ASM_EMIT("7:")
192 __ASM_EMIT("movss 0x00(%[a], %[off]), %%xmm2")
193 __ASM_EMIT("movss 0x00(%[b], %[off]), %%xmm5")
194 __ASM_EMIT("mulss %%xmm2, %%xmm2")
195 __ASM_EMIT("mulss %%xmm5, %%xmm5")
196 __ASM_EMIT("mulss %%xmm5, %%xmm2")
197 __ASM_EMIT("addss %%xmm2, %%xmm0")
198 __ASM_EMIT("add $0x04, %[off]")
199 __ASM_EMIT("dec %[count]")
200 __ASM_EMIT("jge 7b")
201 /* x1 End */
202 __ASM_EMIT("8:")
203 __ASM_EMIT("addps %%xmm1, %%xmm0")
204 __ASM_EMIT("movhlps %%xmm0, %%xmm1")
205 __ASM_EMIT("addps %%xmm1, %%xmm0")
206 __ASM_EMIT("unpcklps %%xmm1, %%xmm0")
207 __ASM_EMIT("movhlps %%xmm0, %%xmm1")
208 __ASM_EMIT("addps %%xmm1, %%xmm0")
209
210 : [count] "+r" (count), [off] "=&r" (off),
211 "=Yz" (result)
212 : [a] "r" (a), [b] "r" (b)
213 : "cc",
214 "%xmm1", "%xmm2", "%xmm3",
215 "%xmm4", "%xmm5", "%xmm6", "%xmm7"
216 );
217
218 return result;
219 }
220
221 IF_ARCH_X86(
222 static const uint32_t h_abs_dotp_const[] __lsp_aligned16 =
223 {
224 LSP_DSP_VEC4(0x7fffffff)
225 };
226 );
227
228 float h_abs_dotp(const float *a, const float *b, size_t count)
229 {
230 IF_ARCH_X86(
231 float result;
232 size_t off;
233 );
234 ARCH_X86_ASM
235 (
236 __ASM_EMIT("xorps %%xmm0, %%xmm0")
237 __ASM_EMIT("xor %[off], %[off]")
238 __ASM_EMIT("xorps %%xmm1, %%xmm1")
239 __ASM_EMIT("movaps %[SIGN], %%xmm2")
240 __ASM_EMIT("sub $8, %[count]")
241 __ASM_EMIT("movaps %%xmm2, %%xmm3")
242 __ASM_EMIT("jl 2f")
243
244 /* x8 Block */
245 __ASM_EMIT("1:")
246 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm4")
247 __ASM_EMIT("movups 0x10(%[a], %[off]), %%xmm5")
248 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm6")
249 __ASM_EMIT("movups 0x10(%[b], %[off]), %%xmm7")
250 __ASM_EMIT("andps %%xmm2, %%xmm4")
251 __ASM_EMIT("andps %%xmm3, %%xmm5")
252 __ASM_EMIT("andps %%xmm2, %%xmm6")
253 __ASM_EMIT("andps %%xmm3, %%xmm7")
254 __ASM_EMIT("mulps %%xmm6, %%xmm4")
255 __ASM_EMIT("mulps %%xmm7, %%xmm5")
256 __ASM_EMIT("addps %%xmm4, %%xmm0")
257 __ASM_EMIT("addps %%xmm5, %%xmm1")
258 __ASM_EMIT("add $0x20, %[off]")
259 __ASM_EMIT("sub $8, %[count]")
260 __ASM_EMIT("jae 1b")
261 /* x4 Block */
262 __ASM_EMIT("2:")
263 __ASM_EMIT("add $4, %[count]")
264 __ASM_EMIT("jl 4f")
265 __ASM_EMIT("movups 0x00(%[a], %[off]), %%xmm4")
266 __ASM_EMIT("movups 0x00(%[b], %[off]), %%xmm6")
267 __ASM_EMIT("andps %%xmm2, %%xmm4")
268 __ASM_EMIT("andps %%xmm3, %%xmm6")
269 __ASM_EMIT("mulps %%xmm6, %%xmm4")
270 __ASM_EMIT("addps %%xmm4, %%xmm0")
271 __ASM_EMIT("sub $4, %[count]")
272 __ASM_EMIT("add $0x10, %[off]")
273 /* x1 Blocks */
274 __ASM_EMIT("4:")
275 __ASM_EMIT("add $3, %[count]")
276 __ASM_EMIT("jl 6f")
277 __ASM_EMIT("5:")
278 __ASM_EMIT("movss 0x00(%[a], %[off]), %%xmm4")
279 __ASM_EMIT("movss 0x00(%[b], %[off]), %%xmm6")
280 __ASM_EMIT("andps %%xmm2, %%xmm4")
281 __ASM_EMIT("andps %%xmm3, %%xmm6")
282 __ASM_EMIT("mulss %%xmm6, %%xmm4")
283 __ASM_EMIT("addss %%xmm4, %%xmm0")
284 __ASM_EMIT("add $0x04, %[off]")
285 __ASM_EMIT("dec %[count]")
286 __ASM_EMIT("jge 5b")
287 /* x1 End */
288 __ASM_EMIT("6:")
289 __ASM_EMIT("addps %%xmm1, %%xmm0")
290 __ASM_EMIT("movhlps %%xmm0, %%xmm1")
291 __ASM_EMIT("addps %%xmm1, %%xmm0")
292 __ASM_EMIT("unpcklps %%xmm1, %%xmm0")
293 __ASM_EMIT("movhlps %%xmm0, %%xmm1")
294 __ASM_EMIT("addps %%xmm1, %%xmm0")
295
296 : [count] "+r" (count), [off] "=&r" (off),
297 "=Yz" (result)
298 : [a] "r" (a), [b] "r" (b),
299 [SIGN] "m" (h_abs_dotp_const)
300 : "cc", "memory",
301 "%xmm1", "%xmm2", "%xmm3",
302 "%xmm4", "%xmm5", "%xmm6", "%xmm7"
303 );
304
305 return result;
306 }
307 }
308 }
309
310 #endif /* PRIVATE_DSP_ARCH_X86_SSE_HMATH_HDOTP_H_ */