|
| 1 | +/*************************************************************************** |
| 2 | +* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * |
| 3 | +* Martin Renou * |
| 4 | +* Copyright (c) QuantStack * |
| 5 | +* * |
| 6 | +* Distributed under the terms of the BSD 3-Clause License. * |
| 7 | +* * |
| 8 | +* The full license is in the file LICENSE, distributed with this software. * |
| 9 | +****************************************************************************/ |
| 10 | + |
| 11 | +#include "test_utils.hpp" |
| 12 | + |
| 13 | +template <class B> |
| 14 | +class power_test : public testing::Test |
| 15 | +{ |
| 16 | +protected: |
| 17 | + |
| 18 | + using batch_type = B; |
| 19 | + using value_type = typename B::value_type; |
| 20 | + static constexpr size_t size = B::size; |
| 21 | + using vector_type = std::vector<value_type>; |
| 22 | + |
| 23 | + size_t nb_input; |
| 24 | + vector_type lhs_input; |
| 25 | + vector_type rhs_input; |
| 26 | + vector_type expected; |
| 27 | + vector_type res; |
| 28 | + |
| 29 | + power_test() |
| 30 | + { |
| 31 | + nb_input = size * 10000; |
| 32 | + lhs_input.resize(nb_input); |
| 33 | + rhs_input.resize(nb_input); |
| 34 | + for (size_t i = 0; i < nb_input; ++i) |
| 35 | + { |
| 36 | + lhs_input[i] = value_type(i) / 4 + value_type(1.2) * std::sqrt(value_type(i + 0.25)); |
| 37 | + rhs_input[i] = value_type(10.2) / (i + 2) + value_type(0.25); |
| 38 | + } |
| 39 | + expected.resize(nb_input); |
| 40 | + res.resize(nb_input); |
| 41 | + } |
| 42 | + |
| 43 | + void test_power_functions() |
| 44 | + { |
| 45 | + // pow |
| 46 | + { |
| 47 | + std::transform(lhs_input.cbegin(), lhs_input.cend(), rhs_input.cbegin(), expected.begin(), |
| 48 | + [](const value_type& l, const value_type& r) { return std::pow(l, r); }); |
| 49 | + batch_type lhs_in, rhs_in, out; |
| 50 | + for (size_t i = 0; i < nb_input; i += size) |
| 51 | + { |
| 52 | + detail::load_batch(lhs_in, lhs_input, i); |
| 53 | + detail::load_batch(rhs_in, rhs_input, i); |
| 54 | + out = pow(lhs_in, rhs_in); |
| 55 | + detail::store_batch(out, res, i); |
| 56 | + } |
| 57 | + size_t diff = detail::get_nb_diff(res, expected); |
| 58 | + EXPECT_EQ(diff, 0) << print_function_name("pow"); |
| 59 | + } |
| 60 | + // ipow |
| 61 | + { |
| 62 | + long k = 0; |
| 63 | + std::transform(lhs_input.cbegin(), lhs_input.cend(), expected.begin(), |
| 64 | + [&k, this](const value_type& l) { auto arg = k / size - nb_input / size / 2; ++k; return std::pow(l, arg); }); |
| 65 | + batch_type lhs_in, out; |
| 66 | + for (size_t i = 0; i < nb_input; i += size) |
| 67 | + { |
| 68 | + detail::load_batch(lhs_in, lhs_input, i); |
| 69 | + out = pow(lhs_in, i/size - nb_input / size / 2); |
| 70 | + detail::store_batch(out, res, i); |
| 71 | + } |
| 72 | + size_t diff = detail::get_nb_diff(res, expected); |
| 73 | + EXPECT_EQ(diff, 0) << print_function_name("ipow"); |
| 74 | + } |
| 75 | + // hypot |
| 76 | + { |
| 77 | + std::transform(lhs_input.cbegin(), lhs_input.cend(), rhs_input.cbegin(), expected.begin(), |
| 78 | + [](const value_type& l, const value_type& r) { return std::hypot(l, r); }); |
| 79 | + batch_type lhs_in, rhs_in, out; |
| 80 | + for (size_t i = 0; i < nb_input; i += size) |
| 81 | + { |
| 82 | + detail::load_batch(lhs_in, lhs_input, i); |
| 83 | + detail::load_batch(rhs_in, rhs_input, i); |
| 84 | + out = hypot(lhs_in, rhs_in); |
| 85 | + detail::store_batch(out, res, i); |
| 86 | + } |
| 87 | + size_t diff = detail::get_nb_diff(res, expected); |
| 88 | + EXPECT_EQ(diff, 0) << print_function_name("hypot"); |
| 89 | + } |
| 90 | + // cbrt |
| 91 | + { |
| 92 | + std::transform(lhs_input.cbegin(), lhs_input.cend(), expected.begin(), |
| 93 | + [](const value_type& l) { return std::cbrt(l); }); |
| 94 | + batch_type lhs_in, out; |
| 95 | + for (size_t i = 0; i < nb_input; i += size) |
| 96 | + { |
| 97 | + detail::load_batch(lhs_in, lhs_input, i); |
| 98 | + out = cbrt(lhs_in); |
| 99 | + detail::store_batch(out, res, i); |
| 100 | + } |
| 101 | + size_t diff = detail::get_nb_diff(res, expected); |
| 102 | + EXPECT_EQ(diff, 0) << print_function_name("cbrt"); |
| 103 | + } |
| 104 | + // hypot |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +TYPED_TEST_SUITE(power_test, batch_float_types, simd_test_names); |
| 109 | + |
| 110 | +TYPED_TEST(power_test, power) |
| 111 | +{ |
| 112 | + this->test_power_functions(); |
| 113 | +} |
0 commit comments