1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "xsimd/xsimd.hpp"
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE
#include "test_utils.hpp"
template <class B>
struct power_test
{
using batch_type = B;
using value_type = typename B::value_type;
static constexpr size_t size = B::size;
using vector_type = std::vector<value_type>;
size_t nb_input;
vector_type zero_input;
vector_type zlhs_input;
vector_type lhs_input;
vector_type rhs_input;
vector_type expected;
vector_type res;
power_test()
{
nb_input = size * 10000;
zero_input.resize(nb_input);
zlhs_input.resize(nb_input);
lhs_input.resize(nb_input);
rhs_input.resize(nb_input);
for (size_t i = 0; i < nb_input; ++i)
{
zero_input[i] = 0;
lhs_input[i] = value_type(i) / 4 + value_type(1.2) * std::sqrt(value_type(i + 0.25));
zlhs_input[i] = lhs_input[i] * (i % 2);
rhs_input[i] = value_type(10.2) / (i + 2) + value_type(0.25);
}
expected.resize(nb_input);
res.resize(nb_input);
}
void test_power_functions()
{
// pow
{
std::transform(lhs_input.cbegin(), lhs_input.cend(), rhs_input.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::pow(l, r); });
batch_type lhs_in, rhs_in, out;
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(lhs_in, lhs_input, i);
detail::load_batch(rhs_in, rhs_input, i);
out = pow(lhs_in, rhs_in);
detail::store_batch(out, res, i);
}
size_t diff = detail::get_nb_diff(res, expected);
INFO("pow");
CHECK_EQ(diff, 0);
}
// pow zero
{
std::transform(zlhs_input.cbegin(), zlhs_input.cend(), rhs_input.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::pow(l, r); });
batch_type zlhs_in, rhs_in, out;
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(zlhs_in, zlhs_input, i);
detail::load_batch(rhs_in, rhs_input, i);
out = pow(zlhs_in, rhs_in);
detail::store_batch(out, res, i);
}
size_t diff = detail::get_nb_diff(res, expected);
INFO("0 ^ x");
CHECK_EQ(diff, 0);
// use of undeclared identifier '_MM_SET_EXCEPTION_MASK for emscripten
#if defined(__SSE__) && !defined(EMSCRIPTEN)
// Test with FE_INVALID...
unsigned mask = _MM_GET_EXCEPTION_MASK();
_MM_SET_EXCEPTION_MASK(mask & ~_MM_MASK_INVALID);
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(zlhs_in, zlhs_input, i);
detail::load_batch(rhs_in, rhs_input, i);
out = pow(zlhs_in, rhs_in);
detail::store_batch(out, res, i);
}
_MM_SET_EXCEPTION_MASK(mask);
diff = detail::get_nb_diff(res, expected);
INFO("0 ^ x with exception");
CHECK_EQ(diff, 0);
#endif
}
// pow 0^-x
{
std::transform(zero_input.cbegin(), zero_input.cend(), rhs_input.cbegin(), expected.begin(),
[](const value_type& z, const value_type& r)
{ return std::pow(z, -r); });
batch_type zero_in, rhs_in, out;
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(zero_in, zero_input, i);
detail::load_batch(rhs_in, rhs_input, i);
out = pow(zero_in, -rhs_in);
detail::store_batch(out, res, i);
}
size_t diff = detail::get_nb_diff(res, expected);
INFO("pow(0, -x)");
CHECK_EQ(diff, 0);
}
// ipow
{
long k = 0;
std::transform(lhs_input.cbegin(), lhs_input.cend(), expected.begin(),
[&k, this](const value_type& l)
{ auto arg = k / size - nb_input / size / 2; ++k; return std::pow(l, arg); });
batch_type lhs_in, out;
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(lhs_in, lhs_input, i);
out = pow(lhs_in, i / size - nb_input / size / 2);
detail::store_batch(out, res, i);
}
size_t diff = detail::get_nb_diff(res, expected);
INFO("ipow");
CHECK_EQ(diff, 0);
}
// hypot
{
std::transform(lhs_input.cbegin(), lhs_input.cend(), rhs_input.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::hypot(l, r); });
batch_type lhs_in, rhs_in, out;
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(lhs_in, lhs_input, i);
detail::load_batch(rhs_in, rhs_input, i);
out = hypot(lhs_in, rhs_in);
detail::store_batch(out, res, i);
}
size_t diff = detail::get_nb_diff(res, expected);
INFO("hypot");
CHECK_EQ(diff, 0);
}
// cbrt
{
std::transform(lhs_input.cbegin(), lhs_input.cend(), expected.begin(),
[](const value_type& l)
{ return std::cbrt(l); });
batch_type lhs_in, out;
for (size_t i = 0; i < nb_input; i += size)
{
detail::load_batch(lhs_in, lhs_input, i);
out = cbrt(lhs_in);
detail::store_batch(out, res, i);
}
size_t diff = detail::get_nb_diff(res, expected);
INFO("cbrt");
CHECK_EQ(diff, 0);
}
}
};
TEST_CASE_TEMPLATE("[power]", B, BATCH_FLOAT_TYPES)
{
power_test<B> Test;
Test.test_power_functions();
}
#endif
|