Project
Loading...
Searching...
No Matches
test_ransSIMD.cxx
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#define BOOST_TEST_MODULE Utility test
17#define BOOST_TEST_MAIN
18#define BOOST_TEST_DYN_LINK
19
20#include <vector>
21#include <type_traits>
22#include <array>
23
24#include <boost/test/unit_test.hpp>
25#include <boost/mpl/list.hpp>
26
27#include <fairlogger/Logger.h>
28
30
31#if defined(RANS_SIMD)
32
35
36using namespace o2::rans::internal::simd;
37
38// clang-format off
39using pd_types = boost::mpl::list<pd_t<SIMDWidth::SSE>
40#ifdef RANS_AVX2
42#endif /* RANS_AVX2 */
43 >;
44
45using epi64_types = boost::mpl::list<epi64_t<SIMDWidth::SSE>
46#ifdef RANS_AVX2
48#endif /* RANS_AVX2 */
49 >;
50
51using epi32_types = boost::mpl::list<epi32_t<SIMDWidth::SSE>
52#ifdef RANS_AVX2
54#endif /* RANS_AVX2 */
55 >;
56// clang-format on
57
58BOOST_AUTO_TEST_CASE(test_getLaneWidthBits)
59{
60 BOOST_CHECK_EQUAL(getLaneWidthBits(SIMDWidth::SSE), 128);
61 BOOST_CHECK_EQUAL(getLaneWidthBits(SIMDWidth::AVX), 256);
62};
63
64BOOST_AUTO_TEST_CASE(test_getLaneWidthBytes)
65{
66 BOOST_CHECK_EQUAL(getLaneWidthBytes(SIMDWidth::SSE), 128 / 8);
67 BOOST_CHECK_EQUAL(getLaneWidthBytes(SIMDWidth::AVX), 256 / 8);
68};
69
70BOOST_AUTO_TEST_CASE(test_getAlignment)
71{
72 BOOST_CHECK_EQUAL(getAlignment(SIMDWidth::SSE), 16);
73 BOOST_CHECK_EQUAL(getAlignment(SIMDWidth::AVX), 32);
74};
75
76BOOST_AUTO_TEST_CASE(test_getElementCount)
77{
78 BOOST_CHECK_EQUAL(getAlignment(SIMDWidth::SSE), 16);
79 BOOST_CHECK_EQUAL(getAlignment(SIMDWidth::AVX), 32);
80};
81
82struct ConvertingFixture64 {
83 std::vector<uint64_t> uint64Data = {0x0, 0x1, 0xFFFFFFFFFFFFE, 0xFFFFFFFFFFFFF};
84 std::vector<double> doubleData{};
85
86 ConvertingFixture64()
87 {
88 for (auto i : uint64Data) {
89 doubleData.push_back(static_cast<double>(i));
90 }
91 };
92};
93
94BOOST_FIXTURE_TEST_SUITE(test_SIMDconvert64, ConvertingFixture64)
95
96BOOST_AUTO_TEST_CASE_TEMPLATE(simd_uint64ToDouble, epi64_T, epi64_types)
97{
98 for (size_t i = 0; i < uint64Data.size(); ++i) {
99 const epi64_T src{uint64Data[i]};
100 const auto dest = store(uint64ToDouble(load(src)));
101
102 for (auto elem : gsl::make_span(dest)) {
103 BOOST_CHECK_EQUAL(elem, doubleData[i]);
104 }
105 }
106}
107
108BOOST_AUTO_TEST_CASE_TEMPLATE(simd_doubleToUint64, pd_T, pd_types)
109{
110 for (size_t i = 0; i < doubleData.size(); ++i) {
111 const pd_T src{doubleData[i]};
112 const auto dest = store<uint64_t>(doubleToUint64(load(src)));
113
114 for (auto elem : gsl::make_span(dest)) {
115 BOOST_CHECK_EQUAL(elem, uint64Data[i]);
116 }
117 }
118}
119
120BOOST_AUTO_TEST_SUITE_END()
121
122struct ConvertingFixture32 {
123 epi32_t<SIMDWidth::SSE> uint32Data = {0x0, 0x1, 0x7FFFFFFE, 0x7FFFFFFF};
124 std::vector<double> doubleData;
125
126 ConvertingFixture32()
127 {
128 for (auto i : gsl::make_span(uint32Data)) {
129 doubleData.push_back(static_cast<double>(i));
130 }
131 };
132};
133
134BOOST_FIXTURE_TEST_SUITE(test_SIMDconvert32, ConvertingFixture32)
135
136BOOST_AUTO_TEST_CASE_TEMPLATE(simd_int32ToDouble, epi32_T, epi32_types)
137{
138 constexpr SIMDWidth simdWidth_V = simdWidth_v<epi32_T>;
139
140 for (size_t i = 0; i < uint32Data.size(); ++i) {
141 const epi32_t<SIMDWidth::SSE> src{uint32Data(i)};
142 auto dest = store(int32ToDouble<simdWidth_V>(load(src)));
143
144 for (auto elem : gsl::make_span(dest)) {
145 BOOST_CHECK_EQUAL(elem, doubleData[i]);
146 }
147 }
148}
149
150BOOST_AUTO_TEST_SUITE_END()
151
152struct ModDivFixture {
153 std::vector<uint32_t> numerator = {1, 6, 17};
154 std::vector<uint32_t> denominator = {1, 3, 4};
155 // test 1: mod = 0, div correctly rounded
156 // test 2: div = 0, mod correclty rounded
157 // test 3: mod, div nonzero and correctly rounded
158 std::array<uint32_t, 3> mod;
159 std::array<uint32_t, 3> div;
160
161 ModDivFixture()
162 {
163 for (size_t i = 0; i < numerator.size(); ++i) {
164 div[i] = numerator[i] / denominator[i];
165 mod[i] = numerator[i] % denominator[i];
166 }
167 };
168};
169
170BOOST_FIXTURE_TEST_SUITE(testModDiv, ModDivFixture)
171
172BOOST_AUTO_TEST_CASE_TEMPLATE(modDiv, pd_T, pd_types)
173{
174 for (size_t i = 0; i < numerator.size(); ++i) {
175 const pd_T numeratorPD{static_cast<double>(numerator[i])};
176 const pd_T denominatorPD{static_cast<double>(denominator[i])};
177
178 auto [divPDVec, modPDVec] = divMod(load(numeratorPD), load(denominatorPD));
179
180 pd_T divPD = store(divPDVec);
181 pd_T modPD = store(modPDVec);
182
183 pd_T modResult{static_cast<double>(mod[i])};
184 pd_T divResult{static_cast<double>(div[i])};
185
186 BOOST_CHECK_EQUAL_COLLECTIONS(gsl::make_span(divResult).begin(), gsl::make_span(divResult).end(), gsl::make_span(divPD).begin(), gsl::make_span(divPD).end());
187 BOOST_CHECK_EQUAL_COLLECTIONS(gsl::make_span(modResult).begin(), gsl::make_span(modResult).end(), gsl::make_span(modPD).begin(), gsl::make_span(modPD).end());
188 }
189}
190BOOST_AUTO_TEST_SUITE_END()
191
192#ifndef RANS_AVX2
193BOOST_AUTO_TEST_CASE(test_NoAVX2)
194{
195 BOOST_TEST_WARN("Tests were not Compiled for AVX2, cannot run all tests");
196}
197#endif /* RANS_AVX2 */
198
199#else /* !defined(RANS_SIMD) */
200
202{
203 BOOST_TEST_WARN("Tests were not Compiled for SIMD, cannot run all tests");
204}
205
206#endif /* RANS_SIMD */
int32_t i
static constexpr size_t size() noexcept
preprocessor defines to enable features based on CPU architecture
GLenum src
Definition glcorearb.h:1767
GLuint GLuint end
Definition glcorearb.h:469
auto make_span(const o2::rans::internal::simd::AlignedArray< T, width_V, size_V > &array)
Enum< T >::Iterator begin(Enum< T >)
Definition Defs.h:173
wrapper around basic SIMD operations
basic SIMD datatypes and traits
BOOST_AUTO_TEST_CASE_TEMPLATE(testInplaceEncoderEmpty, source_T, source_types)
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())
BOOST_AUTO_TEST_CASE(test_NoSIMD)