| 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 |
#include <lsp-plug.in/common/types.h> |
| 23 |
#include <lsp-plug.in/stdlib/math.h> |
| 24 |
#include <lsp-plug.in/test-fw/utest.h> |
| 25 |
#include <lsp-plug.in/test-fw/helpers.h> |
| 26 |
|
| 27 |
namespace lsp |
| 28 |
{ |
| 29 |
namespace generic |
| 30 |
{ |
| 31 |
float ipowf(float x, int deg); |
| 32 |
float irootf(float x, int deg); |
| 33 |
} |
| 34 |
|
| 35 |
IF_ARCH_X86 ( |
| 36 |
namespace sse |
| 37 |
{ |
| 38 |
float ipowf(float x, int deg); |
| 39 |
float irootf(float x, int deg); |
| 40 |
} |
| 41 |
) |
| 42 |
|
| 43 |
typedef float (* ipowrootf_t)(float x, int deg); |
| 44 |
} |
| 45 |
|
| 46 |
UTEST_BEGIN("dsp.smath", ipowroot) |
| 47 |
|
| 48 |
void check_generic() |
| 49 |
{ |
| 50 |
for (int i=-16; i <= 16; ++i) |
| 51 |
{ |
| 52 |
float x1 = generic::ipowf(M_PI, i); |
| 53 |
float x2 = 1; |
| 54 |
for (size_t j=0, k=(i<0) ? -i: i; j<k; ++j) |
| 55 |
x2 *= M_PI; |
| 56 |
if (i < 0) |
| 57 |
x2 = 1.0f / x2; |
| 58 |
|
| 59 |
UTEST_ASSERT_MSG(float_equals_relative(x1, x2, 1e-4), "Failed ipowf(PI, %d): x1=%f, x2=%f", int(i), x1, x2); |
| 60 |
} |
| 61 |
|
| 62 |
for (int i=1; i <= 16; ++i) |
| 63 |
{ |
| 64 |
float x1 = generic::ipowf(M_PI, i); |
| 65 |
float y = generic::irootf(x1, i); |
| 66 |
|
| 67 |
UTEST_ASSERT_MSG(float_equals_relative(M_PI, y, 1e-4), "Failed irootf(%f, %d) = %f, expected %f", x1, int(i), y, M_PI); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void call(const char *label, int start, int end, ipowrootf_t func1, ipowrootf_t func2) |
| 72 |
{ |
| 73 |
if (!UTEST_SUPPORTED(func1)) |
| 74 |
return; |
| 75 |
if (!UTEST_SUPPORTED(func2)) |
| 76 |
return; |
| 77 |
|
| 78 |
printf("Testing %s", label); |
| 79 |
for (int i=start; i <= end; ++i) |
| 80 |
{ |
| 81 |
float x1 = func1(M_PI, i); |
| 82 |
float x2 = func2(M_PI, i); |
| 83 |
|
| 84 |
UTEST_ASSERT_MSG(float_equals_relative(x1, x2, 1e-4), "Failed %s(PI, %d): x1=%f, x2=%f", label, int(i), x1, x2); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
UTEST_MAIN |
| 89 |
{ |
| 90 |
check_generic(); |
| 91 |
IF_ARCH_X86(call("irootf_sse", -16, 16, generic::irootf, sse::irootf)); |
| 92 |
IF_ARCH_X86(call("ipowf_sse", 1, 16, generic::ipowf, sse::ipowf)); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
UTEST_END |
| 97 |
|
| 98 |
|
| 99 |
|