|
| 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 fp_manipulation_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 array_type = std::array<value_type, size>; |
| 22 | + using int_value_type = xsimd::as_integer_t<value_type>; |
| 23 | + using int_batch_type = xsimd::batch<int_value_type, size>; |
| 24 | + |
| 25 | + array_type input; |
| 26 | + int_value_type exponent; |
| 27 | + |
| 28 | + fp_manipulation_test() |
| 29 | + { |
| 30 | + exponent = 5; |
| 31 | + for (size_t i = 0; i < size; ++i) |
| 32 | + { |
| 33 | + input[i] = value_type(i) / 4 + value_type(1.2) * std::sqrt(value_type(i + 0.25)); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + void test_fp_manipulations() const |
| 38 | + { |
| 39 | + int_batch_type bexp(exponent); |
| 40 | + // ldexp |
| 41 | + { |
| 42 | + array_type expected; |
| 43 | + std::transform(input.cbegin(), input.cend(), expected.begin(), |
| 44 | + [this](const value_type& v) { return std::ldexp(v, exponent); }); |
| 45 | + batch_type res = xsimd::ldexp(batch_input(), bexp); |
| 46 | + EXPECT_BATCH_EQ(res, expected) << print_function_name("ldexp"); |
| 47 | + } |
| 48 | + // frexp |
| 49 | + { |
| 50 | + array_type expected; |
| 51 | + std::transform(input.cbegin(), input.cend(), expected.begin(), |
| 52 | + [](const value_type& v) { int tmp; return std::frexp(v, &tmp); }); |
| 53 | + batch_type res = xsimd::frexp(batch_input(), bexp); |
| 54 | + EXPECT_BATCH_EQ(res, expected) << print_function_name("frexp"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +private: |
| 59 | + |
| 60 | + batch_type batch_input() const |
| 61 | + { |
| 62 | + return batch_type(input.data()); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | +TYPED_TEST_SUITE(fp_manipulation_test, batch_float_types, simd_test_names); |
| 68 | + |
| 69 | +TYPED_TEST(fp_manipulation_test, fp_manipulations) |
| 70 | +{ |
| 71 | + this->test_fp_manipulations(); |
| 72 | +} |
| 73 | + |
0 commit comments