Debian Codegraph @

modules/lsp-dsp-lib/include/private/dsp/arch/x86/sse2/pmath/lanczos.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: 16 апр. 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_SSE2_PMATH_LANCZOS_H_
23 #define PRIVATE_DSP_ARCH_X86_SSE2_PMATH_LANCZOS_H_
24
25 #ifndef PRIVATE_DSP_ARCH_X86_SSE2_IMPL
26 #error "This header should not be included directly"
27 #endif /* PRIVATE_DSP_ARCH_X86_SSE2_IMPL */
28
29 #include <private/dsp/arch/x86/sse2/pmath/sin.h>
30
31 namespace lsp
32 {
33 namespace sse2
34 {
35 IF_ARCH_X86(
36 static const uint32_t lanczos_const[] __lsp_aligned16 =
37 {
38 LSP_DSP_VEC4(0x7fffffff), // +0x00: Mask for fabsf
39 LSP_DSP_VEC4(0x38d1b717), // +0x10: Sinc threshold = 1e-4
40 LSP_DSP_VEC4(0x3f800000), // +0x20: 1.0
41 };
42 )
43
44 typedef struct lanczos_gen_t
45 {
46 float k[4]; // +0x00: Multiplier
47 float p[4]; // +0x10: Initial phase
48 float a[4]; // +0x20: Number of lobes
49 float i[4]; // +0x30: Indices
50 float x1[4]; // +0x40: Computed X1
51 float d[4]; // +0x50: Denominator
52 float t[4]; // +0x60: Time range
53 } lanczos_gen_t;
54
55 #define LANCZOS_GEN_FUNC_X4 \
56 /* xmm0 = x1 */ \
57 __ASM_EMIT("movaps %%xmm0, %%xmm4") /* xmm4 = x1 */ \
58 __ASM_EMIT("movaps %%xmm0, %%xmm2") /* xmm2 = x1 */ \
59 __ASM_EMIT("mulps 0x20(%[state]), %%xmm4") /* xmm4 = x2 = x1*a */ \
60 __ASM_EMIT("movaps %%xmm0, 0x40(%[state])") /* *x = x1 */ \
61 __ASM_EMIT("mulps %%xmm4, %%xmm2") /* xmm2 = d = x1 * x2 */ \
62 __ASM_EMIT("addps 0x00 + %[S2C], %%xmm0") /* xmm0 = x1 + PI/2 */ \
63 __ASM_EMIT("addps 0x00 + %[S2C], %%xmm4") /* xmm4 = x2 + PI/2 */ \
64 __ASM_EMIT("movaps %%xmm2, 0x50(%[state])") /* *d = d */ \
65 SINF_X_PLUS_PI_2_CORE_X8 /* xmm0 = sinf(x1), xmm4 = sinf(x2) */ \
66 __ASM_EMIT("movaps 0x40(%[state]), %%xmm1") /* xmm1 = x */ \
67 __ASM_EMIT("mulps %%xmm4, %%xmm0") /* xmm0 = sinf(x1)*sinf(x2) */ \
68 __ASM_EMIT("andps 0x00 + %[LC], %%xmm1") /* xmm1 = fabsf(x) */ \
69 __ASM_EMIT("divps 0x50(%[state]), %%xmm0") /* xmm0 = F = (sinf(x1)*sinf(x2)) / (x1 * x2) */ \
70 __ASM_EMIT("movaps %%xmm1, %%xmm2") /* xmm2 = fabsf(x) */ \
71 __ASM_EMIT("cmpps $5, 0x10 + %[LC], %%xmm1") /* xmm1 = [ fabsf(x) >= 1e-4 ] */ \
72 __ASM_EMIT("andps %%xmm1, %%xmm0") /* xmm0 = [ fabsf(x) >= 1e-4 ] & f */ \
73 __ASM_EMIT("cmpps $1, 0x60(%[state]), %%xmm2") /* xmm2 = [ fabsf(x) < t ] */ \
74 __ASM_EMIT("andnps 0x20 + %[LC], %%xmm1") /* xmm1 = [ fabsf(x) < 1e-4 ] & 1.0 */ \
75 __ASM_EMIT("orps %%xmm1, %%xmm0") /* xmm0 = [ fabsf(x) >= 1e-4 ] ? f : 1.0 */ \
76 __ASM_EMIT("andps %%xmm2, %%xmm0") /* xmm0 = [ fabsf(x) < t ] ? ([ fabsf(x) >= 1e-4 ] ? f : 1.0) : 0.0 */
77
78 void lanczos1(float *dst, float k, float p, float t, float a, size_t count)
79 {
80 IF_ARCH_X86(
81 lanczos_gen_t state __lsp_aligned16;
82 float stub[4] __lsp_aligned16;
83 );
84
85 ARCH_X86_ASM(
86 // Prepare
87 __ASM_EMIT("movss %[k], %%xmm4") // xmm4 = k
88 __ASM_EMIT("movss %[p], %%xmm5") // xmm5 = p
89 __ASM_EMIT("movss %[a], %%xmm6") // xmm6 = a
90 __ASM_EMIT("movss %[t], %%xmm7") // xmm7 = t
91 __ASM_EMIT("shufps $0x00, %%xmm4, %%xmm4") // xmm4 = k k k k
92 __ASM_EMIT("shufps $0x00, %%xmm5, %%xmm5") // xmm5 = p p p p
93 __ASM_EMIT("shufps $0x00, %%xmm6, %%xmm6") // xmm6 = a a a a
94 __ASM_EMIT("shufps $0x00, %%xmm7, %%xmm7") // xmm7 = t t t t
95 __ASM_EMIT("movaps %%xmm4, 0x00(%[state])")
96 __ASM_EMIT("movaps %%xmm5, 0x10(%[state])")
97 __ASM_EMIT("movaps %%xmm6, 0x20(%[state])")
98 __ASM_EMIT("movaps %%xmm7, 0x60(%[state])")
99 __ASM_EMIT("movaps 0x00 + %[LGEN], %%xmm1") // xmm1 = i = 0 1 2 3
100 // x4 blocks
101 __ASM_EMIT("sub $4, %[count]")
102 __ASM_EMIT("jb 2f")
103 __ASM_EMIT("1:")
104 __ASM_EMIT("movaps %%xmm1, %%xmm0") // xmm0 = i
105 __ASM_EMIT("mulps 0x00(%[state]), %%xmm0") // xmm0 = k*i
106 __ASM_EMIT("addps 0x10 + %[LGEN], %%xmm1") // xmm1 = i' = i + step
107 __ASM_EMIT("subps 0x10(%[state]), %%xmm0") // xmm0 = x1 = k*i - p
108 __ASM_EMIT("movaps %%xmm1, 0x30(%[state])") // *i = i'
109 LANCZOS_GEN_FUNC_X4
110 __ASM_EMIT("movaps 0x30(%[state]), %%xmm1") // xmm1 = i'
111 __ASM_EMIT("movups %%xmm0, 0x00(%[dst])")
112 __ASM_EMIT("add $0x10, %[dst]")
113 __ASM_EMIT("sub $4, %[count]")
114 __ASM_EMIT("jae 1b")
115 __ASM_EMIT("2:")
116 // Tail: 1x-3x block
117 __ASM_EMIT("add $4, %[count]")
118 __ASM_EMIT("jle 6f")
119 __ASM_EMIT("movaps %%xmm1, %%xmm0") // xmm0 = i
120 __ASM_EMIT("mulps 0x00(%[state]), %%xmm0") // xmm0 = k*i
121 __ASM_EMIT("subps 0x10(%[state]), %%xmm0") // xmm0 = x1 = k*i - p
122 LANCZOS_GEN_FUNC_X4
123 __ASM_EMIT("test $1, %[count]")
124 __ASM_EMIT("jz 4f")
125 __ASM_EMIT("movss %%xmm0, 0x00(%[dst])")
126 __ASM_EMIT("shufps $0x39, %%xmm0, %%xmm0") // xmm0 = S1 S2 S3 S0
127 __ASM_EMIT("add $4, %[dst]")
128 __ASM_EMIT("4:")
129 __ASM_EMIT("test $2, %[count]")
130 __ASM_EMIT("jz 6f")
131 __ASM_EMIT("movlps %%xmm0, 0x00(%[dst])")
132 // End
133 __ASM_EMIT("6:")
134
135 : [dst] "+r" (dst), [count] "+r" (count)
136 : [state] "r" (&state),
137 [S2C] "o" (sinf_const),
138 [LGEN] "o" (kp_gen_const),
139 [LC] "o" (lanczos_const),
140 [stub] "o" (stub),
141 [k] "m" (k),
142 [p] "m" (p),
143 [a] "m" (a),
144 [t] "m" (t)
145 : "cc", "memory",
146 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
147 "%xmm4", "%xmm5", "%xmm6", "%xmm7"
148 );
149 }
150
151 } /* namespace sse2 */
152 } /* namespace lsp */
153
154
155
156
157 #endif /* PRIVATE_DSP_ARCH_X86_SSE2_PMATH_LANCZOS_H_ */