Skip to content

Commit e1b4783

Browse files
committed
Separation of files by algorithm type
1 parent be5f035 commit e1b4783

File tree

9 files changed

+504
-419
lines changed

9 files changed

+504
-419
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* Copyright (c) QuantStack *
5+
* Copyright (c) Serge Guelton *
6+
* *
7+
* Distributed under the terms of the BSD 3-Clause License. *
8+
* *
9+
* The full license is in the file LICENSE, distributed with this software. *
10+
****************************************************************************/
11+
12+
#ifndef XSIMD_ALGORITHMS_HPP
13+
#define XSIMD_ALGORITHMS_HPP
14+
15+
#include "xsimd_algorithm/stl/reduce.hpp"
16+
#include "xsimd_algorithm/stl/transform.hpp"
17+
18+
#endif
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* Copyright (c) QuantStack *
5+
* Copyright (c) Serge Guelton *
6+
* *
7+
* Distributed under the terms of the BSD 3-Clause License. *
8+
* *
9+
* The full license is in the file LICENSE, distributed with this software. *
10+
****************************************************************************/
11+
12+
#ifndef XSIMD_ALGORITHMS_REDUCE_HPP
13+
#define XSIMD_ALGORITHMS_REDUCE_HPP
14+
15+
#include <array>
16+
#include <cstddef>
17+
#include <iterator>
18+
#include <type_traits>
19+
20+
#include "xsimd/xsimd.hpp"
21+
22+
namespace xsimd
23+
{
24+
// TODO: Remove this once we drop C++11 support
25+
namespace detail
26+
{
27+
struct plus
28+
{
29+
template <class X, class Y>
30+
auto operator()(X&& x, Y&& y) noexcept -> decltype(x + y) { return x + y; }
31+
};
32+
}
33+
34+
template <class Arch = default_arch, class Iterator1, class Iterator2, class Init, class BinaryFunction = detail::plus>
35+
Init reduce(Iterator1 first, Iterator2 last, Init init, BinaryFunction&& binfun = detail::plus {}) noexcept
36+
{
37+
using value_type = typename std::decay<decltype(*first)>::type;
38+
using batch_type = batch<value_type, Arch>;
39+
40+
std::size_t size = static_cast<std::size_t>(std::distance(first, last));
41+
constexpr std::size_t simd_size = batch_type::size;
42+
43+
if (size < simd_size)
44+
{
45+
while (first != last)
46+
{
47+
init = binfun(init, *first++);
48+
}
49+
return init;
50+
}
51+
52+
const auto* const ptr_begin = &(*first);
53+
54+
std::size_t align_begin = xsimd::get_alignment_offset(ptr_begin, size, simd_size);
55+
std::size_t align_end = align_begin + ((size - align_begin) & ~(simd_size - 1));
56+
57+
// reduce initial unaligned part
58+
for (std::size_t i = 0; i < align_begin; ++i)
59+
{
60+
init = binfun(init, first[i]);
61+
}
62+
63+
// reduce aligned part
64+
auto ptr = ptr_begin + align_begin;
65+
batch_type batch_init = batch_type::load_aligned(ptr);
66+
ptr += simd_size;
67+
for (auto const end = ptr_begin + align_end; ptr < end; ptr += simd_size)
68+
{
69+
batch_type batch = batch_type::load_aligned(ptr);
70+
batch_init = binfun(batch_init, batch);
71+
}
72+
73+
// reduce across batch
74+
alignas(batch_type) std::array<value_type, simd_size> arr;
75+
xsimd::store_aligned(arr.data(), batch_init);
76+
for (auto x : arr)
77+
init = binfun(init, x);
78+
79+
// reduce final unaligned part
80+
for (std::size_t i = align_end; i < size; ++i)
81+
{
82+
init = binfun(init, first[i]);
83+
}
84+
85+
return init;
86+
}
87+
88+
}
89+
90+
#endif

include/xsimd_algo/algorithms.hpp renamed to include/xsimd_algorithm/stl/transform.hpp

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
* The full license is in the file LICENSE, distributed with this software. *
1010
****************************************************************************/
1111

12-
#ifndef XSIMD_ALGORITHMS_HPP
13-
#define XSIMD_ALGORITHMS_HPP
12+
#ifndef XSIMD_ALGORITHMS_TRANSFORM_HPP
13+
#define XSIMD_ALGORITHMS_TRANSFORM_HPP
1414

15-
#include <array>
1615
#include <cstddef>
1716
#include <iterator>
1817
#include <type_traits>
@@ -131,71 +130,6 @@ namespace xsimd
131130

132131
#undef XSIMD_LOOP_MACRO
133132
}
134-
135-
// TODO: Remove this once we drop C++11 support
136-
namespace detail
137-
{
138-
struct plus
139-
{
140-
template <class X, class Y>
141-
auto operator()(X&& x, Y&& y) noexcept -> decltype(x + y) { return x + y; }
142-
};
143-
}
144-
145-
template <class Arch = default_arch, class Iterator1, class Iterator2, class Init, class BinaryFunction = detail::plus>
146-
Init reduce(Iterator1 first, Iterator2 last, Init init, BinaryFunction&& binfun = detail::plus {}) noexcept
147-
{
148-
using value_type = typename std::decay<decltype(*first)>::type;
149-
using batch_type = batch<value_type, Arch>;
150-
151-
std::size_t size = static_cast<std::size_t>(std::distance(first, last));
152-
constexpr std::size_t simd_size = batch_type::size;
153-
154-
if (size < simd_size)
155-
{
156-
while (first != last)
157-
{
158-
init = binfun(init, *first++);
159-
}
160-
return init;
161-
}
162-
163-
const auto* const ptr_begin = &(*first);
164-
165-
std::size_t align_begin = xsimd::get_alignment_offset(ptr_begin, size, simd_size);
166-
std::size_t align_end = align_begin + ((size - align_begin) & ~(simd_size - 1));
167-
168-
// reduce initial unaligned part
169-
for (std::size_t i = 0; i < align_begin; ++i)
170-
{
171-
init = binfun(init, first[i]);
172-
}
173-
174-
// reduce aligned part
175-
auto ptr = ptr_begin + align_begin;
176-
batch_type batch_init = batch_type::load_aligned(ptr);
177-
ptr += simd_size;
178-
for (auto const end = ptr_begin + align_end; ptr < end; ptr += simd_size)
179-
{
180-
batch_type batch = batch_type::load_aligned(ptr);
181-
batch_init = binfun(batch_init, batch);
182-
}
183-
184-
// reduce across batch
185-
alignas(batch_type) std::array<value_type, simd_size> arr;
186-
xsimd::store_aligned(arr.data(), batch_init);
187-
for (auto x : arr)
188-
init = binfun(init, x);
189-
190-
// reduce final unaligned part
191-
for (std::size_t i = align_end; i < size; ++i)
192-
{
193-
init = binfun(init, first[i]);
194-
}
195-
196-
return init;
197-
}
198-
199133
}
200134

201135
#endif

test/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ endif()
4747

4848
set(XSIMD_ALGORITHM_TESTS
4949
main.cpp
50-
test_algorithms.cpp
50+
test_iterator.cpp
51+
test_reduce.cpp
52+
test_transform.cpp
5153
)
5254

5355
add_executable(test_xsimd_algorithm ${XSIMD_ALGORITHM_TESTS})# ${XSIMD_ALGORITHM_HEADERS})

test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ EMSCRIPTEN_BINDINGS(my_module)
2828
emscripten::function("run_tests", &run_tests);
2929
}
3030

31-
#endif
31+
#endif

0 commit comments

Comments
 (0)