|
| 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 hyperbolic_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 input; |
| 25 | + vector_type acosh_input; |
| 26 | + vector_type atanh_input; |
| 27 | + vector_type expected; |
| 28 | + vector_type res; |
| 29 | + |
| 30 | + hyperbolic_test() |
| 31 | + { |
| 32 | + nb_input = size * 10000; |
| 33 | + input.resize(nb_input); |
| 34 | + acosh_input.resize(nb_input); |
| 35 | + atanh_input.resize(nb_input); |
| 36 | + for (size_t i = 0; i < nb_input; ++i) |
| 37 | + { |
| 38 | + input[i] = value_type(-1.5) + i * value_type(3) / nb_input; |
| 39 | + acosh_input[i] = value_type(1.) + i * value_type(3) / nb_input; |
| 40 | + atanh_input[i] = value_type(-0.95) + i * value_type(1.9) / nb_input; |
| 41 | + } |
| 42 | + expected.resize(nb_input); |
| 43 | + res.resize(nb_input); |
| 44 | + } |
| 45 | + |
| 46 | + void test_hyperbolic_functions() |
| 47 | + { |
| 48 | + // sinh |
| 49 | + { |
| 50 | + std::transform(input.cbegin(), input.cend(), expected.begin(), |
| 51 | + [](const value_type& v) { return std::sinh(v); }); |
| 52 | + batch_type in, out; |
| 53 | + for (size_t i = 0; i < nb_input; i += size) |
| 54 | + { |
| 55 | + detail::load_batch(in, input, i); |
| 56 | + out = sinh(in); |
| 57 | + detail::store_batch(out, res, i); |
| 58 | + } |
| 59 | + size_t diff = detail::get_nb_diff(res, expected); |
| 60 | + EXPECT_EQ(diff, 0) << print_function_name("sinh"); |
| 61 | + } |
| 62 | + // cosh |
| 63 | + { |
| 64 | + std::transform(input.cbegin(), input.cend(), expected.begin(), |
| 65 | + [](const value_type& v) { return std::cosh(v); }); |
| 66 | + batch_type in, out; |
| 67 | + for (size_t i = 0; i < nb_input; i += size) |
| 68 | + { |
| 69 | + detail::load_batch(in, input, i); |
| 70 | + out = cosh(in); |
| 71 | + detail::store_batch(out, res, i); |
| 72 | + } |
| 73 | + size_t diff = detail::get_nb_diff(res, expected); |
| 74 | + EXPECT_EQ(diff, 0) << print_function_name("cosh"); |
| 75 | + } |
| 76 | + // tanh |
| 77 | + { |
| 78 | + std::transform(input.cbegin(), input.cend(), expected.begin(), |
| 79 | + [](const value_type& v) { return std::tanh(v); }); |
| 80 | + batch_type in, out; |
| 81 | + for (size_t i = 0; i < nb_input; i += size) |
| 82 | + { |
| 83 | + detail::load_batch(in, input, i); |
| 84 | + out = tanh(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("tanh"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + void test_reciprocal_functions() |
| 93 | + { |
| 94 | + // asinh |
| 95 | + { |
| 96 | + std::transform(input.cbegin(), input.cend(), expected.begin(), |
| 97 | + [](const value_type& v) { return std::asinh(v); }); |
| 98 | + batch_type in, out; |
| 99 | + for (size_t i = 0; i < nb_input; i += size) |
| 100 | + { |
| 101 | + detail::load_batch(in, input, i); |
| 102 | + out = asinh(in); |
| 103 | + detail::store_batch(out, res, i); |
| 104 | + } |
| 105 | + size_t diff = detail::get_nb_diff(res, expected); |
| 106 | + EXPECT_EQ(diff, 0) << print_function_name("asinh"); |
| 107 | + } |
| 108 | + // acosh |
| 109 | + { |
| 110 | + std::transform(acosh_input.cbegin(), acosh_input.cend(), expected.begin(), |
| 111 | + [](const value_type& v) { return std::acosh(v); }); |
| 112 | + batch_type in, out; |
| 113 | + for (size_t i = 0; i < nb_input; i += size) |
| 114 | + { |
| 115 | + detail::load_batch(in, acosh_input, i); |
| 116 | + out = acosh(in); |
| 117 | + detail::store_batch(out, res, i); |
| 118 | + } |
| 119 | + size_t diff = detail::get_nb_diff(res, expected); |
| 120 | + EXPECT_EQ(diff, 0) << print_function_name("acosh"); |
| 121 | + } |
| 122 | + // atanh |
| 123 | + { |
| 124 | + std::transform(atanh_input.cbegin(), atanh_input.cend(), expected.begin(), |
| 125 | + [](const value_type& v) { return std::atanh(v); }); |
| 126 | + batch_type in, out; |
| 127 | + for (size_t i = 0; i < nb_input; i += size) |
| 128 | + { |
| 129 | + detail::load_batch(in, atanh_input, i); |
| 130 | + out = atanh(in); |
| 131 | + detail::store_batch(out, res, i); |
| 132 | + } |
| 133 | + size_t diff = detail::get_nb_diff(res, expected); |
| 134 | + EXPECT_EQ(diff, 0) << print_function_name("atanh"); |
| 135 | + } |
| 136 | + } |
| 137 | +}; |
| 138 | + |
| 139 | +TYPED_TEST_SUITE(hyperbolic_test, batch_float_types, simd_test_names); |
| 140 | + |
| 141 | +TYPED_TEST(hyperbolic_test, hyperbolic) |
| 142 | +{ |
| 143 | + this->test_hyperbolic_functions(); |
| 144 | +} |
| 145 | + |
| 146 | +TYPED_TEST(hyperbolic_test, reciprocal) |
| 147 | +{ |
| 148 | + this->test_reciprocal_functions(); |
| 149 | +} |
0 commit comments