| 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_SMATH_H_ |
| 23 |
#define PRIVATE_DSP_ARCH_X86_SSE_SMATH_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 ipowf(float x, int deg) |
| 34 |
{ |
| 35 |
float res = 1.0f; |
| 36 |
float one; |
| 37 |
|
| 38 |
ARCH_X86_ASM |
| 39 |
( |
| 40 |
__ASM_EMIT("cmp $0, %[deg]") |
| 41 |
__ASM_EMIT("jz 100f") |
| 42 |
__ASM_EMIT("jl 10f") |
| 43 |
|
| 44 |
__ASM_EMIT("1:") |
| 45 |
__ASM_EMIT("test $1, %[deg]") |
| 46 |
__ASM_EMIT("jz 2f") |
| 47 |
__ASM_EMIT("mulss %[x], %[res]") |
| 48 |
__ASM_EMIT("dec %[deg]") |
| 49 |
__ASM_EMIT("jnz 1b") |
| 50 |
__ASM_EMIT("jmp 100f") |
| 51 |
__ASM_EMIT("2:") |
| 52 |
__ASM_EMIT("mulss %[x], %[x]") |
| 53 |
__ASM_EMIT("shr $1, %[deg]") |
| 54 |
__ASM_EMIT("jnz 1b") |
| 55 |
__ASM_EMIT("jmp 100f") |
| 56 |
|
| 57 |
__ASM_EMIT("10:") |
| 58 |
__ASM_EMIT("neg %[deg]") |
| 59 |
__ASM_EMIT("movaps %[res], %[one]") |
| 60 |
__ASM_EMIT("1:") |
| 61 |
__ASM_EMIT("test $1, %[deg]") |
| 62 |
__ASM_EMIT("jz 2f") |
| 63 |
__ASM_EMIT("mulss %[x], %[res]") |
| 64 |
__ASM_EMIT("dec %[deg]") |
| 65 |
__ASM_EMIT("jnz 1b") |
| 66 |
__ASM_EMIT("jmp 20f") |
| 67 |
__ASM_EMIT("2:") |
| 68 |
__ASM_EMIT("mulss %[x], %[x]") |
| 69 |
__ASM_EMIT("shr $1, %[deg]") |
| 70 |
__ASM_EMIT("jnz 1b") |
| 71 |
|
| 72 |
__ASM_EMIT("20:") |
| 73 |
__ASM_EMIT("divss %[res], %[one]") |
| 74 |
__ASM_EMIT("movaps %[one], %[res]") |
| 75 |
|
| 76 |
__ASM_EMIT("100:") |
| 77 |
: [x] "+x" (x), [res] "+x" (res), [deg] "+r" (deg), [one] "=&x" (one) |
| 78 |
: |
| 79 |
: "cc" |
| 80 |
); |
| 81 |
|
| 82 |
return res; |
| 83 |
} |
| 84 |
|
| 85 |
IF_ARCH_X86( |
| 86 |
static const uint32_t irootf_const[] __lsp_aligned16 = |
| 87 |
{ |
| 88 |
LSP_DSP_VEC4(0x3f800000), |
| 89 |
LSP_DSP_VEC4(0x7fffffff), |
| 90 |
LSP_DSP_VEC4(0x3727c5ac) |
| 91 |
}; |
| 92 |
); |
| 93 |
|
| 94 |
float irootf(float x, int deg) |
| 95 |
{ |
| 96 |
float a, tmp, k, sign, xp; |
| 97 |
float tres, tx; |
| 98 |
int tdeg; |
| 99 |
|
| 100 |
ARCH_X86_ASM |
| 101 |
( |
| 102 |
__ASM_EMIT("cmp $1, %[deg]") |
| 103 |
__ASM_EMIT("jl 100f") |
| 104 |
|
| 105 |
// While deg is odd, just calculate sqrt function |
| 106 |
__ASM_EMIT("test $1, %[deg]") |
| 107 |
__ASM_EMIT("jne 2f") |
| 108 |
__ASM_EMIT("1:") |
| 109 |
__ASM_EMIT("shr $1, %[deg]") |
| 110 |
__ASM_EMIT("sqrtss %[x], %[x]") |
| 111 |
__ASM_EMIT("test $1, %[deg]") |
| 112 |
__ASM_EMIT("jz 1b") |
| 113 |
__ASM_EMIT("cmp $1, %[deg]") |
| 114 |
__ASM_EMIT("jbe 100f") |
| 115 |
|
| 116 |
// Prepare calculations |
| 117 |
__ASM_EMIT("2:") |
| 118 |
__ASM_EMIT("movaps 0x00(%[CC]), %[a]") |
| 119 |
__ASM_EMIT("cvtsi2ss %[deg], %[tmp]") |
| 120 |
__ASM_EMIT("movaps 0x10(%[CC]), %[sign]") |
| 121 |
__ASM_EMIT("divss %[tmp], %[a]") |
| 122 |
__ASM_EMIT("dec %[deg]") |
| 123 |
__ASM_EMIT("cvtsi2ss %[deg], %[k]") |
| 124 |
__ASM_EMIT("mulss %[a], %[k]") |
| 125 |
__ASM_EMIT("mulss %[x], %[a]") |
| 126 |
|
| 127 |
// Newton method |
| 128 |
__ASM_EMIT("1:") |
| 129 |
__ASM_EMIT("movaps %[x], %[xp]") // xp = x |
| 130 |
__ASM_EMIT("movaps 0x00(%[CC]), %[tres]") // tres = 1 |
| 131 |
__ASM_EMIT("movaps %[x], %[tx]") // tx = x |
| 132 |
__ASM_EMIT("mov %[deg], %[tdeg]") // tdeg = deg |
| 133 |
|
| 134 |
// Calculate tres = x ^ deg |
| 135 |
__ASM_EMIT("10:") |
| 136 |
__ASM_EMIT("test $1, %[tdeg]") // if (tdeg & 1) |
| 137 |
__ASM_EMIT("jz 2f") |
| 138 |
__ASM_EMIT("mulss %[tx], %[tres]") // tres *= tx |
| 139 |
__ASM_EMIT("dec %[tdeg]") // tdeg-- |
| 140 |
__ASM_EMIT("jnz 10b") |
| 141 |
__ASM_EMIT("jmp 20f") |
| 142 |
__ASM_EMIT("2:") |
| 143 |
__ASM_EMIT("mulss %[tx], %[tx]") // tx = tx * tx |
| 144 |
__ASM_EMIT("shr $1, %[tdeg]") // tdeg >>= 1 |
| 145 |
__ASM_EMIT("jnz 10b") |
| 146 |
__ASM_EMIT("20:") |
| 147 |
|
| 148 |
// Calculate x = k*x + a/tres |
| 149 |
__ASM_EMIT("movaps %[a], %[tx]") // tx = a |
| 150 |
__ASM_EMIT("mulss %[k], %[x]") // x = x*k |
| 151 |
__ASM_EMIT("divss %[tres], %[tx]") // tx = a/tres |
| 152 |
__ASM_EMIT("addss %[tx], %[x]") // x = x*k + a/tres |
| 153 |
|
| 154 |
// Estimate tolerance |
| 155 |
__ASM_EMIT("movaps %[x], %[tx]") // tx = x |
| 156 |
__ASM_EMIT("subss %[x], %[xp]") // xp = xp - x |
| 157 |
__ASM_EMIT("mulss 0x20(%[CC]), %[tx]") // tx = x * TOL |
| 158 |
__ASM_EMIT("andps %[sign], %[x]") // xp = abs(xp - x) |
| 159 |
__ASM_EMIT("andps %[sign], %[tx]") // xp = abs(xp - x) |
| 160 |
__ASM_EMIT("ucomiss %[tx], %[xp]") // abs(xp - x) <> x * TOL |
| 161 |
__ASM_EMIT("ja 1b") // Repeat loop if tolerance is not enough |
| 162 |
|
| 163 |
__ASM_EMIT("100:") |
| 164 |
|
| 165 |
: [x] "+x" (x), [a] "=&x" (a), [tmp] "=&x" (tmp), [k] "=&x" (k), |
| 166 |
[sign] "=&x" (sign), [xp] "=&x" (xp), [tres] "=&x" (tres), [tx] "=&x" (tx), |
| 167 |
[deg] "+r" (deg), [tdeg] "=&r" (tdeg) |
| 168 |
: [CC] "r" (irootf_const) |
| 169 |
: "cc" |
| 170 |
); |
| 171 |
|
| 172 |
return x; |
| 173 |
|
| 174 |
// // Check validity of arguments |
| 175 |
// if (deg <= 1) |
| 176 |
// return x; |
| 177 |
// |
| 178 |
// // While root is odd, simple calc square root |
| 179 |
// if (!(deg & 1)) |
| 180 |
// { |
| 181 |
// do |
| 182 |
// { |
| 183 |
// x = sqrtf(x); |
| 184 |
// deg >>= 1; |
| 185 |
// } while (!(deg & 1)); |
| 186 |
// |
| 187 |
// if (deg <= 1) |
| 188 |
// return x; |
| 189 |
// } |
| 190 |
// |
| 191 |
// // Newton method |
| 192 |
// float xp; |
| 193 |
// float a = 1.0f / float(deg); |
| 194 |
// float k = float(--deg) * a; |
| 195 |
// a *= x; |
| 196 |
// |
| 197 |
// do |
| 198 |
// { |
| 199 |
// xp = x; |
| 200 |
// x = k*x + a / ipospowf(x, deg); |
| 201 |
// } while (fabs(x - xp) > (1e-5 * x)); |
| 202 |
// |
| 203 |
// return x; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#endif /* PRIVATE_DSP_ARCH_X86_SSE_SMATH_H_ */ |