Debian Codegraph @

modules/lsp-dsp-lib/include/private/dsp/arch/x86/sse/pmath/pmix.h

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-dsp-lib
6 * Created on: 27 нояб. 2025 г.
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_PMATH_PMIX_H_
23 #define PRIVATE_DSP_ARCH_X86_SSE_PMATH_PMIX_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
34 void pmix_v1(float *dst, const float *src, const float *k, size_t count)
35 {
36 IF_ARCH_X86(size_t off);
37 ARCH_X86_ASM
38 (
39 __ASM_EMIT("xor %[off], %[off]")
40 // 8x blocks
41 __ASM_EMIT("sub $8, %[count]")
42 __ASM_EMIT("jb 2f")
43 __ASM_EMIT("1:")
44 __ASM_EMIT("movups 0x00(%[src],%[off]), %%xmm0") /* xmm0 = s */
45 __ASM_EMIT("movups 0x10(%[src],%[off]), %%xmm1")
46 __ASM_EMIT("movups 0x00(%[dst],%[off]), %%xmm2") /* xmm2 = d */
47 __ASM_EMIT("movups 0x10(%[dst],%[off]), %%xmm3")
48 __ASM_EMIT("movups 0x00(%[k],%[off]), %%xmm4") /* xmm4 = k */
49 __ASM_EMIT("movups 0x10(%[k],%[off]), %%xmm5")
50 __ASM_EMIT("subps %%xmm2, %%xmm0") /* xmm0 = s - d */
51 __ASM_EMIT("subps %%xmm3, %%xmm1")
52 __ASM_EMIT("mulps %%xmm4, %%xmm0") /* xmm0 = (s-d) * k */
53 __ASM_EMIT("mulps %%xmm5, %%xmm1")
54 __ASM_EMIT("addps %%xmm2, %%xmm0") /* xmm0 = mix = d + (s-d) * k */
55 __ASM_EMIT("addps %%xmm3, %%xmm1")
56 __ASM_EMIT("movups %%xmm0, 0x00(%[dst],%[off])")
57 __ASM_EMIT("movups %%xmm1, 0x10(%[dst],%[off])")
58 __ASM_EMIT("add $0x20, %[off]")
59 __ASM_EMIT("sub $8, %[count]")
60 __ASM_EMIT("jae 1b")
61 // 4x block
62 __ASM_EMIT("2:")
63 __ASM_EMIT("add $4, %[count]")
64 __ASM_EMIT("jl 4f")
65 __ASM_EMIT("movups 0x00(%[src],%[off]), %%xmm0") /* xmm0 = s */
66 __ASM_EMIT("movups 0x00(%[dst],%[off]), %%xmm2") /* xmm2 = d */
67 __ASM_EMIT("movups 0x00(%[k],%[off]), %%xmm4") /* xmm4 = k */
68 __ASM_EMIT("subps %%xmm2, %%xmm0") /* xmm0 = s - d */
69 __ASM_EMIT("mulps %%xmm4, %%xmm0") /* xmm0 = (s-d) * k */
70 __ASM_EMIT("addps %%xmm2, %%xmm0") /* xmm0 = mix = d + (s-d) * k */
71 __ASM_EMIT("movups %%xmm0, 0x00(%[dst],%[off])")
72 __ASM_EMIT("sub $4, %[count]")
73 __ASM_EMIT("add $0x10, %[off]")
74 // 1x blocks
75 __ASM_EMIT("4:")
76 __ASM_EMIT("add $3, %[count]")
77 __ASM_EMIT("jl 6f")
78 __ASM_EMIT("5:")
79 __ASM_EMIT("movss 0x00(%[src],%[off]), %%xmm0") /* xmm0 = s */
80 __ASM_EMIT("movss 0x00(%[dst],%[off]), %%xmm2") /* xmm2 = d */
81 __ASM_EMIT("movss 0x00(%[k],%[off]), %%xmm4") /* xmm4 = k */
82 __ASM_EMIT("subss %%xmm2, %%xmm0") /* xmm0 = s - d */
83 __ASM_EMIT("mulss %%xmm4, %%xmm0") /* xmm0 = (s-d) * k */
84 __ASM_EMIT("addss %%xmm2, %%xmm0") /* xmm0 = mix = d + (s-d) * k */
85 __ASM_EMIT("movss %%xmm0, 0x00(%[dst],%[off])")
86 __ASM_EMIT("add $0x04, %[off]")
87 __ASM_EMIT("dec %[count]")
88 __ASM_EMIT("jge 5b")
89 // End
90 __ASM_EMIT("6:")
91 : [off] "=&r" (off), [count] "+r" (count)
92 : [dst] "r" (dst), [src] "r" (src), [k] "r" (k)
93 : "cc", "memory",
94 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
95 "%xmm4", "%xmm5"
96 );
97 }
98
99 void pmix_k1(float *dst, const float *src, float k, size_t count)
100 {
101 IF_ARCH_X86(size_t off);
102 ARCH_X86_ASM
103 (
104 __ASM_EMIT("shufps $0x00, %[k], %[k]")
105 __ASM_EMIT("xor %[off], %[off]")
106 __ASM_EMIT("movaps %[k], %%xmm4")
107 // 8x blocks
108 __ASM_EMIT("sub $8, %[count]")
109 __ASM_EMIT("jb 2f")
110 __ASM_EMIT("1:")
111 __ASM_EMIT("movups 0x00(%[src],%[off]), %%xmm0") /* xmm0 = s */
112 __ASM_EMIT("movups 0x10(%[src],%[off]), %%xmm1")
113 __ASM_EMIT("movups 0x00(%[dst],%[off]), %%xmm2") /* xmm2 = d */
114 __ASM_EMIT("movups 0x10(%[dst],%[off]), %%xmm3")
115 __ASM_EMIT("subps %%xmm2, %%xmm0") /* xmm0 = s - d */
116 __ASM_EMIT("subps %%xmm3, %%xmm1")
117 __ASM_EMIT("mulps %%xmm4, %%xmm0") /* xmm0 = (s-d) * k */
118 __ASM_EMIT("mulps %%xmm4, %%xmm1")
119 __ASM_EMIT("addps %%xmm2, %%xmm0") /* xmm0 = mix = d + (s-d) * k */
120 __ASM_EMIT("addps %%xmm3, %%xmm1")
121 __ASM_EMIT("movups %%xmm0, 0x00(%[dst],%[off])")
122 __ASM_EMIT("movups %%xmm1, 0x10(%[dst],%[off])")
123 __ASM_EMIT("add $0x20, %[off]")
124 __ASM_EMIT("sub $8, %[count]")
125 __ASM_EMIT("jae 1b")
126 // 4x block
127 __ASM_EMIT("2:")
128 __ASM_EMIT("add $4, %[count]")
129 __ASM_EMIT("jl 4f")
130 __ASM_EMIT("movups 0x00(%[src],%[off]), %%xmm0") /* xmm0 = s */
131 __ASM_EMIT("movups 0x00(%[dst],%[off]), %%xmm2") /* xmm2 = d */
132 __ASM_EMIT("subps %%xmm2, %%xmm0") /* xmm0 = s - d */
133 __ASM_EMIT("mulps %%xmm4, %%xmm0") /* xmm0 = (s-d) * k */
134 __ASM_EMIT("addps %%xmm2, %%xmm0") /* xmm0 = mix = d + (s-d) * k */
135 __ASM_EMIT("movups %%xmm0, 0x00(%[dst],%[off])")
136 __ASM_EMIT("sub $4, %[count]")
137 __ASM_EMIT("add $0x10, %[off]")
138 // 1x blocks
139 __ASM_EMIT("4:")
140 __ASM_EMIT("add $3, %[count]")
141 __ASM_EMIT("jl 6f")
142 __ASM_EMIT("5:")
143 __ASM_EMIT("movss 0x00(%[src],%[off]), %%xmm0") /* xmm0 = s */
144 __ASM_EMIT("movss 0x00(%[dst],%[off]), %%xmm2") /* xmm2 = d */
145 __ASM_EMIT("subss %%xmm2, %%xmm0") /* xmm0 = s - d */
146 __ASM_EMIT("mulss %%xmm4, %%xmm0") /* xmm0 = (s-d) * k */
147 __ASM_EMIT("addss %%xmm2, %%xmm0") /* xmm0 = mix = d + (s-d) * k */
148 __ASM_EMIT("movss %%xmm0, 0x00(%[dst],%[off])")
149 __ASM_EMIT("add $0x04, %[off]")
150 __ASM_EMIT("dec %[count]")
151 __ASM_EMIT("jge 5b")
152 // End
153 __ASM_EMIT("6:")
154 : [off] "=&r" (off), [count] "+r" (count), [k] "+Yz" (k)
155 : [dst] "r" (dst), [src] "r" (src)
156 : "cc", "memory",
157 "%xmm1", "%xmm2", "%xmm3",
158 "%xmm4"
159 );
160 }
161
162 } /* namespace sse */
163 } /* namespace lsp */
164
165
166
167 #endif /* PRIVATE_DSP_ARCH_X86_SSE_PMATH_PMIX_H_ */