|
| 1 | +/* -*- c++ -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2025 Johannes Demel |
| 4 | + * |
| 5 | + * This file is part of VOLK |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +#include "volk_test.h" |
| 11 | +#include <fmt/chrono.h> |
| 12 | +#include <fmt/core.h> |
| 13 | +#include <fmt/ranges.h> |
| 14 | +#include <gtest/gtest.h> |
| 15 | +#include <volk/volk.h> |
| 16 | +#include <volk/volk_alloc.hh> |
| 17 | +#include <chrono> |
| 18 | + |
| 19 | +class volk_32fc_s32fc_x2_rotator2_32fc_test : public VolkTest |
| 20 | +{ |
| 21 | +protected: |
| 22 | + void SetUp() override |
| 23 | + { |
| 24 | + initialize_test(GetParam()); |
| 25 | + initialize_data(vector_length); |
| 26 | + } |
| 27 | + |
| 28 | + void initialize_data(const size_t length) |
| 29 | + { |
| 30 | + // Be stricter for smaller vectors. Error accumulate slowly! |
| 31 | + if (length < 16) { |
| 32 | + absolute_error = 10.e-7; |
| 33 | + } else if (length < 128) { |
| 34 | + absolute_error = 10.e-6; |
| 35 | + } else if (length < 65536) { |
| 36 | + absolute_error = 10.e-5; |
| 37 | + } else { |
| 38 | + absolute_error = 10.e-3; |
| 39 | + } |
| 40 | + |
| 41 | + vector_length = length; |
| 42 | + input = volk::vector<lv_32fc_t>(length); |
| 43 | + result = volk::vector<lv_32fc_t>(length); |
| 44 | + result_magnitude = volk::vector<float>(length); |
| 45 | + |
| 46 | + const float initial_phase = initial_phase_steps * increment; |
| 47 | + phase_increment = std::polar(1.0f, increment); |
| 48 | + phase = std::polar(1.0f, initial_phase); |
| 49 | + |
| 50 | + for (size_t i = 0; i < length; ++i) { |
| 51 | + input[i] = |
| 52 | + std::complex<float>(2.0f * std::cos(2.0f * M_PI * i / length), |
| 53 | + 2.0f * std::sin(0.3f + 2.0f * M_PI * i / length)); |
| 54 | + } |
| 55 | + |
| 56 | + // Calculate expected results |
| 57 | + expected = volk::vector<lv_32fc_t>(length); |
| 58 | + for (size_t i = 0; i < length; ++i) { |
| 59 | + expected[i] = |
| 60 | + input[i] * |
| 61 | + std::polar(1.0f, initial_phase + static_cast<float>(i) * increment); |
| 62 | + } |
| 63 | + |
| 64 | + expected_magnitude = volk::vector<float>(length); |
| 65 | + for (size_t i = 0; i < length; ++i) { |
| 66 | + expected_magnitude[i] = std::abs(input[i]); |
| 67 | + } |
| 68 | + |
| 69 | + // This is a hacky solution to have unaligned tests. |
| 70 | + ua_result = result; |
| 71 | + ua_result.at(0) = expected.at(0); |
| 72 | + } |
| 73 | + |
| 74 | + void execute_aligned(const std::string impl_name) |
| 75 | + { |
| 76 | + volk_32fc_s32fc_x2_rotator2_32fc_manual(result.data(), |
| 77 | + input.data(), |
| 78 | + &phase_increment, |
| 79 | + &phase, |
| 80 | + vector_length, |
| 81 | + impl_name.c_str()); |
| 82 | + |
| 83 | + for (size_t i = 0; i < vector_length; ++i) { |
| 84 | + result_magnitude[i] = std::abs(result[i]); |
| 85 | + } |
| 86 | + EXPECT_TRUE(AreFloatingPointArraysEqualWithAbsoluteError( |
| 87 | + expected_magnitude, result_magnitude, absolute_magnitue_error)); |
| 88 | + EXPECT_TRUE(AreComplexFloatingPointArraysEqualWithAbsoluteError( |
| 89 | + expected, result, absolute_error)); |
| 90 | + } |
| 91 | + |
| 92 | + void execute_unaligned(const std::string impl_name) |
| 93 | + { |
| 94 | + lv_32fc_t unaligned_phase = |
| 95 | + std::polar(1.0f, (initial_phase_steps + 1.0f) * increment); |
| 96 | + volk_32fc_s32fc_x2_rotator2_32fc_manual(ua_result.data() + 1, |
| 97 | + input.data() + 1, |
| 98 | + &phase_increment, |
| 99 | + &unaligned_phase, |
| 100 | + vector_length - 1, |
| 101 | + impl_name.c_str()); |
| 102 | + for (size_t i = 0; i < vector_length; ++i) { |
| 103 | + result_magnitude[i] = std::abs(ua_result[i]); |
| 104 | + } |
| 105 | + result_magnitude[0] = expected_magnitude[0]; |
| 106 | + |
| 107 | + EXPECT_TRUE(AreFloatingPointArraysEqualWithAbsoluteError( |
| 108 | + expected_magnitude, result_magnitude, absolute_magnitue_error)); |
| 109 | + EXPECT_TRUE(AreComplexFloatingPointArraysEqualWithAbsoluteError( |
| 110 | + expected, ua_result, absolute_error)); |
| 111 | + } |
| 112 | + |
| 113 | + static constexpr float increment = 0.07f; |
| 114 | + static constexpr float initial_phase_steps = 0.0f; |
| 115 | + static constexpr float absolute_magnitue_error = 1.0e-4; |
| 116 | + float absolute_error{}; |
| 117 | + volk::vector<lv_32fc_t> input; |
| 118 | + volk::vector<lv_32fc_t> result; |
| 119 | + lv_32fc_t phase_increment; |
| 120 | + lv_32fc_t phase; |
| 121 | + volk::vector<lv_32fc_t> expected; |
| 122 | + volk::vector<float> expected_magnitude; |
| 123 | + volk::vector<lv_32fc_t> ua_result; |
| 124 | + volk::vector<float> result_magnitude; |
| 125 | +}; |
| 126 | + |
| 127 | +TEST_P(volk_32fc_s32fc_x2_rotator2_32fc_test, run) |
| 128 | +{ |
| 129 | + fmt::print("test {} implementation: {:>12}, size={} ...", |
| 130 | + is_aligned_implementation ? "aligned" : "unaligned", |
| 131 | + implementation_name, |
| 132 | + vector_length); |
| 133 | + auto start = std::chrono::steady_clock::now(); |
| 134 | + |
| 135 | + if (is_aligned_implementation) { |
| 136 | + execute_aligned(implementation_name); |
| 137 | + } else { |
| 138 | + execute_unaligned(implementation_name); |
| 139 | + } |
| 140 | + |
| 141 | + std::chrono::duration<double> elapsed = std::chrono::steady_clock::now() - start; |
| 142 | + fmt::print("\tduration={}\n", elapsed); |
| 143 | +} |
| 144 | + |
| 145 | +INSTANTIATE_TEST_SUITE_P( |
| 146 | + volk_32fc_s32fc_x2_rotator2_32fc, |
| 147 | + volk_32fc_s32fc_x2_rotator2_32fc_test, |
| 148 | + testing::Combine(testing::ValuesIn(get_kernel_implementation_name_list( |
| 149 | + volk_32fc_s32fc_x2_rotator2_32fc_get_func_desc())), |
| 150 | + testing::ValuesIn(default_vector_sizes)), |
| 151 | + generate_volk_test_name()); |
0 commit comments