1- path <- " inst/include/RcppParallel/simd/functors .h"
1+ path <- " inst/include/RcppParallel/simd/ops .h"
22dir.create(dirname(path ), recursive = TRUE , showWarnings = FALSE )
33
4+ forEach <- function (data , f ) {
5+ nm <- names(data )
6+ for (i in seq_along(data )) {
7+ f(nm [[i ]], data [[i ]])
8+ }
9+ }
10+
11+ indent <- function (code , indent = " " ) {
12+ code <- paste(indent , code , sep = " " )
13+ gsub(" \n " , " \n " , code , fixed = TRUE )
14+ }
15+
416template <- paste(c(
517 " // Auto-generated functors for functions provided by Boost.SIMD." ,
6- " // See 'gen/simd-functors .R' for implementation." ,
18+ " // See 'gen/simd-ops .R' for implementation." ,
719 " " ,
820 " namespace boost {" ,
921 " namespace simd {" ,
10- " namespace functors {" ,
22+ " namespace ops {" ,
1123 " " ,
1224 " %s" ,
1325 " " ,
14- " } // namespace functors " ,
26+ " } // namespace ops " ,
1527 " } // namespace simd" ,
1628 " } // namespace boost"
1729), collapse = " \n " )
@@ -20,43 +32,43 @@ code <- NULL
2032
2133# NOTE: Each function should accept a parameter called 'data'.
2234unary <- list (
23- sum = " boost::simd:: sum(data)"
35+ sum = " return sum(data); "
2436)
2537
2638unary_template <- paste(c(
2739 " struct %s {" ,
2840 " template <typename T>" ,
2941 " inline T operator()(const T& data) {" ,
30- " return %s; " ,
42+ " %s " ,
3143 " }" ,
3244 " };" ,
3345 " "
3446), collapse = " \n " )
3547
36- for ( i in seq_along( unary ) ) {
37- code <- c(code , sprintf(unary_template , names( unary )[ i ], unary [[ i ]] ))
38- }
48+ forEach( unary , function ( name , value ) {
49+ code << - c(code , sprintf(unary_template , name , indent( value ) ))
50+ })
3951
4052binary <- list (
41- plus = " lhs + rhs" ,
42- minus = " lhs - rhs" ,
43- times = " lhs * rhs" ,
44- divide = " lhs / rhs"
53+ plus = " return lhs + rhs; " ,
54+ minus = " return lhs - rhs; " ,
55+ times = " return lhs * rhs; " ,
56+ divide = " return lhs / rhs; "
4557)
4658
4759binary_template <- paste(c(
4860 " struct %s {" ,
4961 " template <typename T>" ,
5062 " inline T operator()(const T& lhs, const T& rhs) {" ,
51- " return %s; " ,
63+ " %s " ,
5264 " }" ,
5365 " };" ,
5466 " "
5567), collapse = " \n " )
5668
57- for ( i in seq_along( binary ) ) {
58- code <- c(code , sprintf(binary_template , names( binary )[[ i ]], binary [[ i ]] ))
59- }
69+ forEach( binary , function ( name , value ) {
70+ code << - c(code , sprintf(binary_template , name , indent( value ) ))
71+ })
6072
6173compiled <- sprintf(template , paste(code , collapse = " \n " ))
6274cat(compiled , file = path , sep = " \n " )
0 commit comments