|
| 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 CP> |
| 14 | +class bitwise_cast_test : public testing::Test |
| 15 | +{ |
| 16 | +protected: |
| 17 | + |
| 18 | + static constexpr size_t N = CP::size; |
| 19 | + static constexpr size_t A = CP::alignment; |
| 20 | + |
| 21 | + using int32_batch = xsimd::batch<int32_t, N * 2>; |
| 22 | + using int64_batch = xsimd::batch<int64_t, N>; |
| 23 | + using float_batch = xsimd::batch<float, N * 2>; |
| 24 | + using double_batch = xsimd::batch<double, N>; |
| 25 | + |
| 26 | + using int32_vector = std::vector<int32_t, xsimd::aligned_allocator<int32_t, A>>; |
| 27 | + using int64_vector = std::vector<int64_t, xsimd::aligned_allocator<int64_t, A>>; |
| 28 | + using float_vector = std::vector<float, xsimd::aligned_allocator<float, A>>; |
| 29 | + using double_vector = std::vector<double, xsimd::aligned_allocator<double, A>>; |
| 30 | + |
| 31 | + int32_vector ftoi32_res; |
| 32 | + int32_vector dtoi32_res; |
| 33 | + int64_vector ftoi64_res; |
| 34 | + int64_vector dtoi64_res; |
| 35 | + float_vector i32tof_res; |
| 36 | + float_vector i64tof_res; |
| 37 | + float_vector dtof_res; |
| 38 | + double_vector i32tod_res; |
| 39 | + double_vector i64tod_res; |
| 40 | + double_vector ftod_res; |
| 41 | + |
| 42 | + bitwise_cast_test() |
| 43 | + : ftoi32_res(2 * N), dtoi32_res(2 * N), ftoi64_res(N), dtoi64_res(N), |
| 44 | + i32tof_res(2 * N), i64tof_res(2 * N), dtof_res(2 * N), |
| 45 | + i32tod_res(N), i64tod_res(N), ftod_res(N) |
| 46 | + { |
| 47 | + { |
| 48 | + int32_batch input = i32_input(); |
| 49 | + bitcast b; |
| 50 | + b.i32[0] = input[0]; |
| 51 | + b.i32[1] = input[1]; |
| 52 | + std::fill(i32tof_res.begin(), i32tof_res.end(), b.f[0]); |
| 53 | + std::fill(i32tod_res.begin(), i32tod_res.end(), b.d); |
| 54 | + } |
| 55 | + { |
| 56 | + int64_batch input = i64_input(); |
| 57 | + bitcast b; |
| 58 | + b.i64 = input[0]; |
| 59 | + std::fill(i64tod_res.begin(), i64tod_res.end(), b.d); |
| 60 | + for (size_t i = 0; i < N; ++i) |
| 61 | + { |
| 62 | + i64tof_res[2 * i] = b.f[0]; |
| 63 | + i64tof_res[2 * i + 1] = b.f[1]; |
| 64 | + } |
| 65 | + } |
| 66 | + { |
| 67 | + float_batch input = f_input(); |
| 68 | + bitcast b; |
| 69 | + b.f[0] = input[0]; |
| 70 | + b.f[1] = input[1]; |
| 71 | + std::fill(ftoi32_res.begin(), ftoi32_res.end(), b.i32[0]); |
| 72 | + std::fill(ftoi64_res.begin(), ftoi64_res.end(), b.i64); |
| 73 | + std::fill(ftod_res.begin(), ftod_res.end(), b.d); |
| 74 | + } |
| 75 | + { |
| 76 | + double_batch input = d_input(); |
| 77 | + bitcast b; |
| 78 | + b.d = input[0]; |
| 79 | + //std::fill(dtoi32_res.begin(), dtoi32_res.end(), b.i32[0]); |
| 80 | + std::fill(dtoi64_res.begin(), dtoi64_res.end(), b.i64); |
| 81 | + for (size_t i = 0; i < N; ++i) |
| 82 | + { |
| 83 | + dtoi32_res[2 * i] = b.i32[0]; |
| 84 | + dtoi32_res[2 * i + 1] = b.i32[1]; |
| 85 | + dtof_res[2 * i] = b.f[0]; |
| 86 | + dtof_res[2 * i + 1] = b.f[1]; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + void test_to_int32() |
| 92 | + { |
| 93 | + int32_vector i32vres(int32_batch::size); |
| 94 | + { |
| 95 | + int32_batch i32bres = xsimd::bitwise_cast<int32_batch>(f_input()); |
| 96 | + i32bres.store_aligned(i32vres.data()); |
| 97 | + EXPECT_VECTOR_EQ(i32vres, ftoi32_res) << print_function_name("to_int32(float)"); |
| 98 | + } |
| 99 | + { |
| 100 | + int32_batch i32bres = xsimd::bitwise_cast<int32_batch>(d_input()); |
| 101 | + i32bres.store_aligned(i32vres.data()); |
| 102 | + EXPECT_VECTOR_EQ(i32vres, dtoi32_res) << print_function_name("to_int32(double)"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + void test_to_int64() |
| 107 | + { |
| 108 | + int64_vector i64vres(int64_batch::size); |
| 109 | + { |
| 110 | + int64_batch i64bres = xsimd::bitwise_cast<int64_batch>(f_input()); |
| 111 | + i64bres.store_aligned(i64vres.data()); |
| 112 | + EXPECT_VECTOR_EQ(i64vres, ftoi64_res) << print_function_name("to_int64(float)"); |
| 113 | + } |
| 114 | + { |
| 115 | + int64_batch i64bres = xsimd::bitwise_cast<int64_batch>(d_input()); |
| 116 | + i64bres.store_aligned(i64vres.data()); |
| 117 | + EXPECT_VECTOR_EQ(i64vres, dtoi64_res) << print_function_name("to_int64(double)"); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + void test_to_float() |
| 122 | + { |
| 123 | + float_vector fvres(float_batch::size); |
| 124 | + { |
| 125 | + float_batch fbres = xsimd::bitwise_cast<float_batch>(i32_input()); |
| 126 | + fbres.store_aligned(fvres.data()); |
| 127 | + EXPECT_VECTOR_EQ(fvres, i32tof_res) << print_function_name("to_float(int32_t)"); |
| 128 | + } |
| 129 | + { |
| 130 | + float_batch fbres = xsimd::bitwise_cast<float_batch>(i64_input()); |
| 131 | + fbres.store_aligned(fvres.data()); |
| 132 | + EXPECT_VECTOR_EQ(fvres, i64tof_res) << print_function_name("to_float(int64_t)"); |
| 133 | + } |
| 134 | + { |
| 135 | + float_batch fbres = xsimd::bitwise_cast<float_batch>(d_input()); |
| 136 | + fbres.store_aligned(fvres.data()); |
| 137 | + EXPECT_VECTOR_EQ(fvres, dtof_res) << print_function_name("to_float(double)"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + void test_to_double() |
| 142 | + { |
| 143 | + double_vector dvres(double_batch::size); |
| 144 | + { |
| 145 | + double_batch dbres = xsimd::bitwise_cast<double_batch>(i32_input()); |
| 146 | + dbres.store_aligned(dvres.data()); |
| 147 | + EXPECT_VECTOR_EQ(dvres, i32tod_res) << print_function_name("to_double(int32_t)"); |
| 148 | + } |
| 149 | + { |
| 150 | + double_batch dbres = xsimd::bitwise_cast<double_batch>(i64_input()); |
| 151 | + dbres.store_aligned(dvres.data()); |
| 152 | + EXPECT_VECTOR_EQ(dvres, i64tod_res) << print_function_name("to_double(int64_t)"); |
| 153 | + } |
| 154 | + { |
| 155 | + double_batch dbres = xsimd::bitwise_cast<double_batch>(f_input()); |
| 156 | + dbres.store_aligned(dvres.data()); |
| 157 | + EXPECT_VECTOR_EQ(dvres, ftod_res) << print_function_name("to_double(float)"); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +private: |
| 162 | + |
| 163 | + int32_batch i32_input() const |
| 164 | + { |
| 165 | + return int32_batch(2); |
| 166 | + } |
| 167 | + |
| 168 | + int64_batch i64_input() const |
| 169 | + { |
| 170 | + return int64_batch(2); |
| 171 | + } |
| 172 | + |
| 173 | + float_batch f_input() const |
| 174 | + { |
| 175 | + return float_batch(3.); |
| 176 | + } |
| 177 | + |
| 178 | + double_batch d_input() const |
| 179 | + { |
| 180 | + return double_batch(2.5e17); |
| 181 | + } |
| 182 | + |
| 183 | + union bitcast |
| 184 | + { |
| 185 | + float f[2]; |
| 186 | + int32_t i32[2]; |
| 187 | + int64_t i64; |
| 188 | + double d; |
| 189 | + }; |
| 190 | +}; |
| 191 | + |
| 192 | +TYPED_TEST_SUITE(bitwise_cast_test, conversion_types, conversion_test_names); |
| 193 | + |
| 194 | +TYPED_TEST(bitwise_cast_test, to_int32) |
| 195 | +{ |
| 196 | + this->test_to_int32(); |
| 197 | +} |
| 198 | + |
| 199 | +TYPED_TEST(bitwise_cast_test, to_int64) |
| 200 | +{ |
| 201 | + this->test_to_int64(); |
| 202 | +} |
| 203 | + |
| 204 | +TYPED_TEST(bitwise_cast_test, to_float) |
| 205 | +{ |
| 206 | + this->test_to_float(); |
| 207 | +} |
| 208 | + |
| 209 | +TYPED_TEST(bitwise_cast_test, to_double) |
| 210 | +{ |
| 211 | + this->test_to_double(); |
| 212 | +} |
0 commit comments