Skip to content

Commit 83b900c

Browse files
committed
Test refactoring: bitwise cast
1 parent 8bc1b55 commit 83b900c

File tree

4 files changed

+270
-54
lines changed

4 files changed

+270
-54
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ set(XSIMD_TESTS
149149
test_batch_complex.cpp
150150
test_batch_float.cpp
151151
test_batch_int.cpp
152+
test_bitwise_cast.cpp
152153
test_complex_exponential.cpp
153154
test_complex_hyperbolic.cpp
154155
test_complex_power.cpp

test/test_bitwise_cast.cpp

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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+
}

test/test_conversion.cpp

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,6 @@
1010

1111
#include "test_utils.hpp"
1212

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-
4113
template <class CP>
4214
class conversion_test : public testing::Test
4315
{
@@ -56,14 +28,14 @@ class conversion_test : public testing::Test
5628
using float_vector = std::vector<float, xsimd::aligned_allocator<float, A>>;
5729
using double_vector = std::vector<double, xsimd::aligned_allocator<double, A>>;
5830

59-
int32_batch i32pos;
31+
/*int32_batch i32pos;
6032
int32_batch i32neg;
6133
int64_batch i64pos;
6234
int64_batch i64neg;
6335
float_batch fpos;
6436
float_batch fneg;
6537
double_batch dpos;
66-
double_batch dneg;
38+
double_batch dneg;*/
6739

6840
int32_vector fposres;
6941
int32_vector fnegres;
@@ -87,9 +59,7 @@ class conversion_test : public testing::Test
8759
int32_vector fvres(int32_batch::size);
8860
{
8961
int32_batch fbres = to_int(fpos);
90-
std::cout << "coincoin" << std::endl;
9162
fbres.store_aligned(fvres.data());
92-
std::cout << "coincoin" << std::endl;
9363
EXPECT_VECTOR_EQ(fvres, fposres) << print_function_name("to_int(positive float)");
9464
}
9565
{
@@ -148,28 +118,6 @@ class conversion_test : public testing::Test
148118
}
149119
};
150120

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-
173121
TYPED_TEST_SUITE(conversion_test, conversion_types, conversion_test_names);
174122

175123
TYPED_TEST(conversion_test, to_int32)

test/test_utils.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,5 +693,60 @@ using batch_complex_types = to_testing_types<xsimd::batch_complex_type_list>;
693693
using batch_math_types = to_testing_types<xsimd::batch_math_type_list>;
694694
using batch_types = to_testing_types<xsimd::batch_type_list>;
695695

696+
697+
/********************
698+
* conversion utils *
699+
********************/
700+
701+
template <size_t N, size_t A>
702+
struct conversion_param
703+
{
704+
static constexpr size_t size = N;
705+
static constexpr size_t alignment = A;
706+
};
707+
708+
class conversion_test_names
709+
{
710+
public:
711+
712+
template <class T>
713+
static std::string GetName(int)
714+
{
715+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION
716+
if (T::size == 2) { return "sse"; }
717+
if (T::size == 4) { return "avx"; }
718+
if (T::size == 8) { return "avx512";}
719+
return "fallback";
720+
#elif XSIMD_ARM_INSTR_SET >= XSIMD_ARM7_NEON_VERSION
721+
if (T::size == 2) { return "neon"; }
722+
return "fallback";
723+
#else
724+
return "fallback";
725+
#endif
726+
}
727+
};
728+
729+
using conversion_type_list = xsimd::mpl::type_list<
730+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION
731+
conversion_param<2, 16>
732+
#endif
733+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX_VERSION
734+
,
735+
conversion_param<4, 32>
736+
#endif
737+
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX512_VERSION
738+
,
739+
conversion_param<8, 64>
740+
#endif
741+
#if XSIMD_ARM_INSTR_SET >= XSIMD_ARM8_64_NEON_VERSION
742+
conversion_param<2, 16>
743+
#endif
744+
#if defined(XSIMD_ENABLE_FALLBACK)
745+
,
746+
conversion_param<3, 32>
747+
#endif
748+
>;
749+
using conversion_types = to_testing_types<conversion_type_list>;
750+
696751
#endif // XXSIMD_TEST_UTILS_HPP
697752

0 commit comments

Comments
 (0)