Skip to content

Commit 89533c7

Browse files
committed
example using fast_sqrt
1 parent 9203423 commit 89533c7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

examples/rcpp-simd-transform.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,31 @@ double simd_sum(NumericVector data)
4343
return simd::accumulate(data, 0.0, plus());
4444
}
4545

46+
#include <boost/simd/arithmetic/include/functions/fast_sqrt.hpp>
47+
48+
// NOTE: To use the bundled SIMD functions, it looks like we need the
49+
// following header. We should expose all of these functions and auto-include
50+
// them in a utility namespace so users don't have to keep track of this sort
51+
// of thing.
52+
#include <boost/dispatch/functor/meta/call.hpp>
53+
54+
// It also stinks that we have to wrap it in our own functor. Surely
55+
// there's a better way?
56+
struct fast_sqrt {
57+
template <typename T>
58+
T operator()(const T& t) { return boost::simd::fast_sqrt(t); }
59+
};
60+
61+
// [[Rcpp::export]]
62+
NumericVector simd_sqrt(NumericVector data)
63+
{
64+
return simd::transform(data, fast_sqrt());
65+
}
66+
4667
/*** R
4768
data <- as.numeric(1:16)
4869
simd_add_two(data)
4970
simd_add(data, data)
5071
simd_sum(data)
72+
simd_sqrt(data)
5173
*/

0 commit comments

Comments
 (0)