|
| 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 <cstddef> |
| 12 | +#include <vector> |
| 13 | +#include <numeric> |
| 14 | + |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +#include "xsimd/config/xsimd_instruction_set.hpp" |
| 18 | + |
| 19 | +#ifdef XSIMD_INSTR_SET_AVAILABLE |
| 20 | + |
| 21 | +#include "xsimd/xsimd.hpp" |
| 22 | + |
| 23 | +struct interface_tester |
| 24 | +{ |
| 25 | + std::vector<float, xsimd::aligned_allocator<float, 64>> fvec; |
| 26 | + std::vector<int32_t, xsimd::aligned_allocator<int32_t, 64>> ivec; |
| 27 | + std::vector<float, xsimd::aligned_allocator<float, 64>> fres; |
| 28 | + std::vector<int32_t, xsimd::aligned_allocator<int32_t, 64>> ires; |
| 29 | + |
| 30 | + interface_tester(); |
| 31 | + |
| 32 | + static const std::size_t SIZE = xsimd::simd_traits<float>::size; |
| 33 | +}; |
| 34 | + |
| 35 | +interface_tester::interface_tester() |
| 36 | + : fvec(SIZE), ivec(SIZE), fres(SIZE), ires(SIZE) |
| 37 | +{ |
| 38 | + std::iota(fvec.begin(), fvec.end(), 1.f); |
| 39 | + std::iota(ivec.begin(), ivec.end(), 1); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(xsimd, set_simd) |
| 43 | +{ |
| 44 | + interface_tester t; |
| 45 | + xsimd::simd_type<float> r1 = xsimd::set_simd(t.fvec[0]); |
| 46 | + EXPECT_EQ(r1[0], t.fvec[0]); |
| 47 | + |
| 48 | + xsimd::simd_type<float> r2 = xsimd::set_simd<int32_t, float>(t.ivec[0]); |
| 49 | + EXPECT_EQ(r2[0], t.fvec[0]); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(xsimd, load_store_aligned) |
| 53 | +{ |
| 54 | + interface_tester t; |
| 55 | + xsimd::simd_type<float> r1 = xsimd::load_aligned(&t.fvec[0]); |
| 56 | + xsimd::store_aligned(&t.fres[0], r1); |
| 57 | + EXPECT_EQ(t.fvec, t.fres); |
| 58 | + |
| 59 | + xsimd::simd_type<float> r2 = xsimd::load_aligned<int32_t, float>(&t.ivec[0]); |
| 60 | + xsimd::store_aligned(&t.fres[0], r2); |
| 61 | + EXPECT_EQ(t.fvec, t.fres); |
| 62 | + |
| 63 | + xsimd::simd_type<float> r3 = xsimd::load_aligned(&t.fvec[0]); |
| 64 | + xsimd::store_aligned<int32_t, float>(&t.ires[0], r3); |
| 65 | + EXPECT_EQ(t.ivec, t.ires); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(xsimd, load_store_unaligned) |
| 69 | +{ |
| 70 | + interface_tester t; |
| 71 | + xsimd::simd_type<float> r1 = xsimd::load_unaligned(&t.fvec[0]); |
| 72 | + xsimd::store_unaligned(&t.fres[0], r1); |
| 73 | + EXPECT_EQ(t.fvec, t.fres); |
| 74 | + |
| 75 | + xsimd::simd_type<float> r2 = xsimd::load_unaligned<int32_t, float>(&t.ivec[0]); |
| 76 | + xsimd::store_unaligned(&t.fres[0], r2); |
| 77 | + EXPECT_EQ(t.fvec, t.fres); |
| 78 | + |
| 79 | + xsimd::simd_type<float> r3 = xsimd::load_unaligned(&t.fvec[0]); |
| 80 | + xsimd::store_unaligned<int32_t, float>(&t.ires[0], r3); |
| 81 | + EXPECT_EQ(t.ivec, t.ires); |
| 82 | +} |
| 83 | + |
| 84 | +TEST(xsimd, load_store_simd_aligned) |
| 85 | +{ |
| 86 | + interface_tester t; |
| 87 | + xsimd::simd_type<float> r1 = xsimd::load_simd(&t.fvec[0], xsimd::aligned_mode()); |
| 88 | + xsimd::store_simd(&t.fres[0], r1, xsimd::aligned_mode()); |
| 89 | + EXPECT_EQ(t.fvec, t.fres); |
| 90 | + |
| 91 | + xsimd::simd_type<float> r2 = xsimd::load_simd<int32_t, float>(&t.ivec[0], xsimd::aligned_mode()); |
| 92 | + xsimd::store_simd(&t.fres[0], r2, xsimd::aligned_mode()); |
| 93 | + EXPECT_EQ(t.fvec, t.fres); |
| 94 | + |
| 95 | + xsimd::simd_type<float> r3 = xsimd::load_simd(&t.fvec[0], xsimd::aligned_mode()); |
| 96 | + xsimd::store_simd<int32_t, float>(&t.ires[0], r3, xsimd::aligned_mode()); |
| 97 | + EXPECT_EQ(t.ivec, t.ires); |
| 98 | +} |
| 99 | + |
| 100 | +TEST(xsimd, load_store_simd_unaligned) |
| 101 | +{ |
| 102 | + interface_tester t; |
| 103 | + xsimd::simd_type<float> r1 = xsimd::load_simd(&t.fvec[0], xsimd::unaligned_mode()); |
| 104 | + xsimd::store_simd(&t.fres[0], r1, xsimd::unaligned_mode()); |
| 105 | + EXPECT_EQ(t.fvec, t.fres); |
| 106 | + |
| 107 | + xsimd::simd_type<float> r2 = xsimd::load_simd<int32_t, float>(&t.ivec[0], xsimd::unaligned_mode()); |
| 108 | + xsimd::store_simd(&t.fres[0], r2, xsimd::unaligned_mode()); |
| 109 | + EXPECT_EQ(t.fvec, t.fres); |
| 110 | + |
| 111 | + xsimd::simd_type<float> r3 = xsimd::load_simd(&t.fvec[0], xsimd::unaligned_mode()); |
| 112 | + xsimd::store_simd<int32_t, float>(&t.ires[0], r3, xsimd::unaligned_mode()); |
| 113 | + EXPECT_EQ(t.ivec, t.ires); |
| 114 | +} |
| 115 | +#endif // XSIMD_INSTR_SET_AVAILABLE |
0 commit comments