Skip to content

Commit 8bc1b55

Browse files
committed
Test refactoring: conversion
1 parent e41a75b commit 8bc1b55

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ set(XSIMD_TESTS
153153
test_complex_hyperbolic.cpp
154154
test_complex_power.cpp
155155
test_complex_trigonometric.cpp
156+
test_conversion.cpp
156157
test_error_gamma.cpp
157158
test_exponential.cpp
158159
test_fp_manipulation.cpp

test/test_conversion.cpp

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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 <size_t N, size_t A>
14+
struct conversion_param
15+
{
16+
static constexpr size_t size = N;
17+
static constexpr size_t alignment = A;
18+
};
19+
20+
class conversion_test_names
21+
{
22+
public:
23+
24+
template <class T>
25+
static std::string GetName(int)
26+
{
27+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION
28+
if (T::size == 2) { return "sse"; }
29+
if (T::size == 4) { return "avx"; }
30+
if (T::size == 8) { return "avx512";}
31+
return "fallback";
32+
#elif XSIMD_ARM_INSTR_SET >= XSIMD_ARM7_NEON_VERSION
33+
if (T::size == 2) { return "neon"; }
34+
return "fallback";
35+
#else
36+
return "fallback";
37+
#endif
38+
}
39+
};
40+
41+
template <class CP>
42+
class conversion_test : public testing::Test
43+
{
44+
protected:
45+
46+
static constexpr size_t N = CP::size;
47+
static constexpr size_t A = CP::alignment;
48+
49+
using int32_batch = xsimd::batch<int32_t, N * 2>;
50+
using int64_batch = xsimd::batch<int64_t, N>;
51+
using float_batch = xsimd::batch<float, N * 2>;
52+
using double_batch = xsimd::batch<double, N>;
53+
54+
using int32_vector = std::vector<int32_t, xsimd::aligned_allocator<int32_t, A>>;
55+
using int64_vector = std::vector<int64_t, xsimd::aligned_allocator<int64_t, A>>;
56+
using float_vector = std::vector<float, xsimd::aligned_allocator<float, A>>;
57+
using double_vector = std::vector<double, xsimd::aligned_allocator<double, A>>;
58+
59+
int32_batch i32pos;
60+
int32_batch i32neg;
61+
int64_batch i64pos;
62+
int64_batch i64neg;
63+
float_batch fpos;
64+
float_batch fneg;
65+
double_batch dpos;
66+
double_batch dneg;
67+
68+
int32_vector fposres;
69+
int32_vector fnegres;
70+
int64_vector dposres;
71+
int64_vector dnegres;
72+
float_vector i32posres;
73+
float_vector i32negres;
74+
double_vector i64posres;
75+
double_vector i64negres;
76+
77+
conversion_test()
78+
: fposres(2 * N, 7), fnegres(2 * N, -6), dposres(N, 5), dnegres(N, -1),
79+
i32posres(2 * N, float(2)), i32negres(2 * N, float(-3)),
80+
i64posres(N, double(2)), i64negres(N, double(-3))
81+
{
82+
}
83+
84+
void test_to_int32()
85+
{
86+
float_batch fpos(float(7.4)), fneg(float(-6.2));
87+
int32_vector fvres(int32_batch::size);
88+
{
89+
int32_batch fbres = to_int(fpos);
90+
std::cout << "coincoin" << std::endl;
91+
fbres.store_aligned(fvres.data());
92+
std::cout << "coincoin" << std::endl;
93+
EXPECT_VECTOR_EQ(fvres, fposres) << print_function_name("to_int(positive float)");
94+
}
95+
{
96+
int32_batch fbres = to_int(fneg);
97+
fbres.store_aligned(fvres.data());
98+
EXPECT_VECTOR_EQ(fvres, fnegres) << print_function_name("to_int(negative float)");
99+
}
100+
}
101+
102+
void test_to_int64()
103+
{
104+
double_batch dpos(double(5.4)), dneg(double(-1.2));
105+
int64_vector dvres(int64_batch::size);
106+
{
107+
int64_batch dbres = to_int(dpos);
108+
dbres.store_aligned(dvres.data());
109+
EXPECT_VECTOR_EQ(dvres, dposres) << print_function_name("to_int(positive double)");
110+
}
111+
{
112+
int64_batch dbres = to_int(dneg);
113+
dbres.store_aligned(dvres.data());
114+
EXPECT_VECTOR_EQ(dvres, dnegres) << print_function_name("to_int(negative double)");
115+
}
116+
}
117+
118+
void test_to_float()
119+
{
120+
int32_batch i32pos(2), i32neg(-3);
121+
float_vector i32vres(float_batch::size);
122+
{
123+
float_batch i32bres = to_float(i32pos);
124+
i32bres.store_aligned(i32vres.data());
125+
EXPECT_VECTOR_EQ(i32vres, i32posres) << print_function_name("to_float(positive int32)");
126+
}
127+
{
128+
float_batch i32bres = to_float(i32neg);
129+
i32bres.store_aligned(i32vres.data());
130+
EXPECT_VECTOR_EQ(i32vres, i32negres) << print_function_name("to_float(negative int32)");
131+
}
132+
}
133+
134+
void test_to_double()
135+
{
136+
int64_batch i64pos(2), i64neg(-3);
137+
double_vector i64vres(double_batch::size);
138+
{
139+
double_batch i64bres = to_float(i64pos);
140+
i64bres.store_aligned(i64vres.data());
141+
EXPECT_VECTOR_EQ(i64vres, i64posres) << print_function_name("to_float(positive int64)");
142+
}
143+
{
144+
double_batch i64bres = to_float(i64neg);
145+
i64bres.store_aligned(i64vres.data());
146+
EXPECT_VECTOR_EQ(i64vres, i64negres) << print_function_name("to_float(negative int64)");
147+
}
148+
}
149+
};
150+
151+
using conversion_type_list = xsimd::mpl::type_list<
152+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION
153+
conversion_param<2, 16>
154+
#endif
155+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX_VERSION
156+
,
157+
conversion_param<4, 32>
158+
#endif
159+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX512_VERSION
160+
,
161+
conversion_param<8, 64>
162+
#endif
163+
#if XSIMD_ARM_INSTR_SET >= XSIMD_ARM8_64_NEON_VERSION
164+
conversion_param<2, 16>
165+
#endif
166+
#if defined(XSIMD_ENABLE_FALLBACK)
167+
,
168+
conversion_param<3, 32>
169+
#endif
170+
>;
171+
using conversion_types = to_testing_types<conversion_type_list>;
172+
173+
TYPED_TEST_SUITE(conversion_test, conversion_types, conversion_test_names);
174+
175+
TYPED_TEST(conversion_test, to_int32)
176+
{
177+
this->test_to_int32();
178+
}
179+
180+
TYPED_TEST(conversion_test, to_int64)
181+
{
182+
this->test_to_int64();
183+
}
184+
185+
TYPED_TEST(conversion_test, to_float)
186+
{
187+
this->test_to_float();
188+
}
189+
190+
TYPED_TEST(conversion_test, to_double)
191+
{
192+
this->test_to_double();
193+
}

0 commit comments

Comments
 (0)