Skip to content

Commit 6cf7658

Browse files
committed
auto-generate commonly used functors
1 parent 7ad5f68 commit 6cf7658

File tree

5 files changed

+112
-15
lines changed

5 files changed

+112
-15
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
^src/.*\.o$
1212
^examples/
1313
^gallery/
14+
^gen/

gallery/simd-variance.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class VarianceTransformer : public SimdTransformer<double>
1111
packed_type initialize(const value_type* begin, const value_type* end)
1212
{
1313
n_ = end - begin;
14-
mean_ = boost::simd::accumulate(begin, end, 0.0, plus()) / n_;
14+
mean_ = boost::simd::accumulate(begin, end, 0.0, boost::simd::functors::plus()) / n_;
1515
return packed_type(0);
1616
}
1717

@@ -26,16 +26,6 @@ class VarianceTransformer : public SimdTransformer<double>
2626
}
2727

2828
private:
29-
30-
struct plus
31-
{
32-
template <typename T>
33-
T operator()(const T& lhs, const T& rhs)
34-
{
35-
return lhs + rhs;
36-
}
37-
};
38-
3929
double mean_;
4030
std::size_t n_;
4131
};

gen/simd-functors.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
path <- "inst/include/RcppParallel/simd/functors.h"
2+
dir.create(dirname(path), recursive = TRUE, showWarnings = FALSE)
3+
4+
template <- paste(c(
5+
"// Auto-generated functors for functions provided by Boost.SIMD.",
6+
"// See 'gen/simd-functors.R' for implementation.",
7+
"",
8+
"namespace boost {",
9+
"namespace simd {",
10+
"namespace functors {",
11+
"",
12+
"%s",
13+
"",
14+
"} // namespace functors",
15+
"} // namespace simd",
16+
"} // namespace boost"
17+
), collapse = "\n")
18+
19+
code <- NULL
20+
21+
# NOTE: Each function should accept a parameter called 'data'.
22+
unary <- list(
23+
sum = "boost::simd::sum(data)"
24+
)
25+
26+
unary_template <- paste(c(
27+
"struct %s {",
28+
"template <typename T>",
29+
"inline T operator()(const T& data) {",
30+
" return %s;",
31+
"}",
32+
"};",
33+
""
34+
), collapse = "\n")
35+
36+
for (i in seq_along(unary)) {
37+
code <- c(code, sprintf(unary_template, names(unary)[i], unary[[i]]))
38+
}
39+
40+
binary <- list(
41+
plus = "lhs + rhs",
42+
minus = "lhs - rhs",
43+
times = "lhs * rhs",
44+
divide = "lhs / rhs"
45+
)
46+
47+
binary_template <- paste(c(
48+
"struct %s {",
49+
"template <typename T>",
50+
"inline T operator()(const T& lhs, const T& rhs) {",
51+
" return %s;",
52+
"}",
53+
"};",
54+
""
55+
), collapse = "\n")
56+
57+
for (i in seq_along(binary)) {
58+
code <- c(code, sprintf(binary_template, names(binary)[[i]], binary[[i]]))
59+
}
60+
61+
compiled <- sprintf(template, paste(code, collapse = "\n"))
62+
cat(compiled, file = path, sep = "\n")

inst/include/RcppParallel/SIMD.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929

3030
#include <boost/simd/swar/swar.hpp>
3131

32-
33-
34-
35-
32+
// Auto-generated functors for use in 'transform()', 'accumulate()'
33+
#include <RcppParallel/simd/functors.h>
3634

3735
namespace RcppParallel {
3836

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Auto-generated functors for functions provided by Boost.SIMD.
2+
// See 'gen/simd-functors.R' for implementation.
3+
4+
namespace boost {
5+
namespace simd {
6+
namespace functors {
7+
8+
struct sum {
9+
template <typename T>
10+
inline T operator()(const T& data) {
11+
return boost::simd::sum(data);
12+
}
13+
};
14+
15+
struct plus {
16+
template <typename T>
17+
inline T operator()(const T& lhs, const T& rhs) {
18+
return lhs + rhs;
19+
}
20+
};
21+
22+
struct minus {
23+
template <typename T>
24+
inline T operator()(const T& lhs, const T& rhs) {
25+
return lhs - rhs;
26+
}
27+
};
28+
29+
struct times {
30+
template <typename T>
31+
inline T operator()(const T& lhs, const T& rhs) {
32+
return lhs * rhs;
33+
}
34+
};
35+
36+
struct divide {
37+
template <typename T>
38+
inline T operator()(const T& lhs, const T& rhs) {
39+
return lhs / rhs;
40+
}
41+
};
42+
43+
44+
} // namespace functors
45+
} // namespace simd
46+
} // namespace boost

0 commit comments

Comments
 (0)