| 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: 21 апр. 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_ARM_NEON_D32_PMATH_LANCZOS_H_ |
| 23 |
#define PRIVATE_DSP_ARCH_ARM_NEON_D32_PMATH_LANCZOS_H_ |
| 24 |
|
| 25 |
#ifndef PRIVATE_DSP_ARCH_ARM_NEON_D32_IMPL |
| 26 |
#error "This header should not be included directly" |
| 27 |
#endif /* PRIVATE_DSP_ARCH_ARM_NEON_D32_IMPL */ |
| 28 |
|
| 29 |
#include <private/dsp/arch/arm/neon-d32/pmath/sin.h> |
| 30 |
|
| 31 |
namespace lsp |
| 32 |
{ |
| 33 |
namespace neon_d32 |
| 34 |
{ |
| 35 |
IF_ARCH_ARM( |
| 36 |
static const uint32_t lanczos_const[] __lsp_aligned16 = |
| 37 |
{ |
| 38 |
LSP_DSP_VEC4(0x38d1b717), // +0x00: Sinc threshold = 1e-4 |
| 39 |
LSP_DSP_VEC4(0x3f800000), // +0x10: 1.0 |
| 40 |
}; |
| 41 |
) |
| 42 |
|
| 43 |
typedef struct lanczos_gen_t |
| 44 |
{ |
| 45 |
float k[4]; // +0x00: Multiplier |
| 46 |
float p[4]; // +0x10: Initial phase |
| 47 |
float a[4]; // +0x20: Number of lobes |
| 48 |
float i[4]; // +0x30: Indices |
| 49 |
float t[4]; // +0x40: Time range |
| 50 |
float x1[4]; // +0x50: Computed X1 |
| 51 |
float n[4]; // +0x60: Numerator |
| 52 |
float step[4]; // +0x70: Step |
| 53 |
} lanczos_gen_t; |
| 54 |
|
| 55 |
#define LANCZOS_GEN_FUNC_X4 \ |
| 56 |
/* q0 = x1 */ \ |
| 57 |
__ASM_EMIT("vmul.f32 q1, q0, q10") /* q1 = x2 = x1*a */ \ |
| 58 |
__ASM_EMIT("vabs.f32 q13, q0") /* q13 = fabsf(x1) */ \ |
| 59 |
__ASM_EMIT("vmul.f32 q14, q0, q1") /* q14 = x1*x2 */ \ |
| 60 |
__ASM_EMIT("vrecpe.f32 q2, q14") /* q2 = s2 */ \ |
| 61 |
__ASM_EMIT("vrecps.f32 q4, q2, q14") /* q4 = (2 - R*s2) */ \ |
| 62 |
__ASM_EMIT("vmul.f32 q2, q4, q2") /* q2 = s2' = s2 * (2 - R*s2) */ \ |
| 63 |
__ASM_EMIT("vrecps.f32 q4, q2, q14") /* q4 = (2 - R*s2') */ \ |
| 64 |
__ASM_EMIT("vmul.f32 q14, q4, q2") /* q14 = n = 1/(x1 * x2) */ \ |
| 65 |
__ASM_EMIT("vstm %[state], {q8-q15}") /* save state */ \ |
| 66 |
__ASM_EMIT("vldm %[S2C], {q8-q15}") /* load sine coefficients */ \ |
| 67 |
__ASM_EMIT("vadd.f32 q0, q0, q8") /* q0 = x1 + PI/2 */ \ |
| 68 |
__ASM_EMIT("vadd.f32 q1, q1, q8") /* q1 = x2 + PI/2 */ \ |
| 69 |
SINF_X_PLUS_PI_2_CORE_X8 /* q0 = sinf(x1), q1 = sinf(x2) */ \ |
| 70 |
__ASM_EMIT("vldm %[state], {q8-q15}") /* load state */ \ |
| 71 |
__ASM_EMIT("vldm %[LC], {q6-q7}") /* q6 = 1e-4, q7 = 1.0 */ \ |
| 72 |
__ASM_EMIT("vmul.f32 q0, q0, q1") /* q0 = sinf(x1)*sinf(x2) */ \ |
| 73 |
__ASM_EMIT("vcge.f32 q2, q13, q6") /* q2 = [ fabsf(x1) >= 1e-4 ] */ \ |
| 74 |
__ASM_EMIT("vmul.f32 q0, q0, q14") /* q0 = f = sinf(x1)*sinf(x2)/(x1*x2) */ \ |
| 75 |
__ASM_EMIT("vcgt.f32 q3, q12, q13") /* q3 = [ fabsf(x1) < t ] */ \ |
| 76 |
__ASM_EMIT("vbif q0, q7, q2") /* q0 = [ fabsf(x1) >= 1e-4 ] ? f : 1.0 */ \ |
| 77 |
__ASM_EMIT("vand q0, q0, q3") /* q0 = [ fabsf(x1) < t ] ? ([ fabsf(x1) >= 1e-4 ] ? f : 1.0) : 0.0 */ |
| 78 |
|
| 79 |
void lanczos1(float *dst, float k, float p, float t, float a, size_t count) |
| 80 |
{ |
| 81 |
IF_ARCH_ARM( |
| 82 |
lanczos_gen_t state __lsp_aligned16; |
| 83 |
); |
| 84 |
|
| 85 |
ARCH_ARM_ASM( |
| 86 |
__ASM_EMIT("vldm %[S2KP], {q11-q12}") // q11 = i = 0 1 2 3, q12 = 4 ... |
| 87 |
__ASM_EMIT("vld1.f32 {d16[], d17[]}, [%[k]]") // q8 = k |
| 88 |
__ASM_EMIT("vld1.f32 {d18[], d19[]}, [%[p]]") // q9 = p |
| 89 |
__ASM_EMIT("vmov q15, q12") // q15 = step = 4 |
| 90 |
__ASM_EMIT("vld1.f32 {d20[], d21[]}, [%[a]]") // q10 = a |
| 91 |
__ASM_EMIT("vld1.f32 {d24[], d25[]}, [%[t]]") // q12 = t |
| 92 |
// x4 blocks |
| 93 |
__ASM_EMIT("subs %[count], #4") |
| 94 |
__ASM_EMIT("blo 2f") |
| 95 |
__ASM_EMIT("1:") |
| 96 |
__ASM_EMIT("vmul.f32 q0, q8, q11") // q0 = k * i |
| 97 |
__ASM_EMIT("vadd.f32 q11, q11, q15") // q11 = i' = i + 4 |
| 98 |
__ASM_EMIT("vsub.f32 q0, q0, q9") // q0 = k*i - p |
| 99 |
LANCZOS_GEN_FUNC_X4 |
| 100 |
__ASM_EMIT("subs %[count], #4") |
| 101 |
__ASM_EMIT("vst1.32 {q0}, [%[dst]]!") |
| 102 |
__ASM_EMIT("bhs 1b") |
| 103 |
__ASM_EMIT("2:") |
| 104 |
// Tail: 1x-3x block |
| 105 |
__ASM_EMIT("adds %[count], #4") |
| 106 |
__ASM_EMIT("bls 6f") |
| 107 |
__ASM_EMIT("vmul.f32 q0, q8, q11") // q0 = k * i |
| 108 |
__ASM_EMIT("vsub.f32 q0, q0, q9") // q0 = k*i - p |
| 109 |
LANCZOS_GEN_FUNC_X4 |
| 110 |
__ASM_EMIT("tst %[count], #1") |
| 111 |
__ASM_EMIT("beq 4f") |
| 112 |
__ASM_EMIT("vst1.32 {d0[0]}, [%[dst]]!") |
| 113 |
__ASM_EMIT("vext.32 q0, q0, q0, #1") |
| 114 |
__ASM_EMIT("4:") |
| 115 |
__ASM_EMIT("tst %[count], #2") |
| 116 |
__ASM_EMIT("beq 6f") |
| 117 |
__ASM_EMIT("vst1.32 {d0}, [%[dst]]") |
| 118 |
// End |
| 119 |
__ASM_EMIT("6:") |
| 120 |
|
| 121 |
: [dst] "+r" (dst), [count] "+r" (count) |
| 122 |
: [k] "r" (&k), |
| 123 |
[p] "r" (&p), |
| 124 |
[t] "r" (&t), |
| 125 |
[a] "r" (&a), |
| 126 |
[S2C] "r" (&sinf_const[0]), |
| 127 |
[S2KP] "r" (&kp_gen_const[0]), |
| 128 |
[LC] "r" (&lanczos_const[0]), |
| 129 |
[state] "r" (&state) |
| 130 |
: "cc", "memory", |
| 131 |
"q0", "q1", "q2", "q3", |
| 132 |
"q4", "q5", "q6", "q7", |
| 133 |
"q8", "q9", "q10", "q11", |
| 134 |
"q12", "q13", "q14", "q15" |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
} /* namespace neon_d32 */ |
| 139 |
} /* namespace lsp */ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#endif /* PRIVATE_DSP_ARCH_ARM_NEON_D32_PMATH_LANCZOS_H_ */ |