Skip to content

Commit 352f344

Browse files
committed
helpers for constructing bitwise 1s and 0s; more BH includes
1 parent f328f5a commit 352f344

File tree

5 files changed

+56
-12
lines changed

5 files changed

+56
-12
lines changed

R/plugin.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ inlineCxxPlugin <- function() {
77
"-include", system.file("include/RcppNT2.h", package = "RcppNT2")
88
)
99
),
10+
LinkingTo = "BH",
1011
includes = "#include <RcppNT2.h>"
1112
)
1213
}

R/precompile.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ precompileRcppNT2 <- function() {
2525
CXX,
2626
"-x c++-header", header,
2727
paste("-I", include, sep = ""),
28+
paste("-I", system.file("include", package = "BH")),
2829
paste("-I", R.home("include"), sep = ""),
2930
"-std=c++11",
3031
cxxFlags

inst/examples/example-na-handling-variance.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,12 @@ using namespace RcppNT2;
99
#include <Rcpp.h>
1010
using namespace Rcpp;
1111

12-
namespace detail {
13-
14-
union DoublePunner {
15-
uint64_t i;
16-
double d;
17-
};
18-
19-
static const DoublePunner punner = {-1ULL};
20-
21-
} // namespace detail
22-
2312
struct IsNaN
2413
{
2514
template <typename T>
2615
T operator()(const T& data)
2716
{
28-
return nt2::if_else(data == data, detail::punner.d, (double) 0x0);
17+
return nt2::if_else(data == data, bitwise::ones(), bitwise::zeroes());
2918
}
3019
};
3120

inst/include/RcppNT2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include <RcppNT2/core/core.h>
4343
#include <RcppNT2/algorithm/algorithm.h>
44+
#include <RcppNT2/bitwise/bitwise.h>
4445
#include <RcppNT2/convert/convert.h>
4546
#include <RcppNT2/functor/functor.h>
4647
#include <RcppNT2/traits/traits.h>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef RCPP_NT2_BITWISE_BITWISE_H
2+
#define RCPP_NT2_BITWISE_BITWISE_H
3+
4+
#include <utility>
5+
#include <cstdint>
6+
7+
namespace RcppNT2 {
8+
namespace bitwise {
9+
10+
namespace detail {
11+
12+
// Used for constructing 'double's with all bits set to 1.
13+
union DoublePunner {
14+
uint64_t i;
15+
double d;
16+
};
17+
18+
} // namespace detail
19+
20+
// A class which can be (lazily) converted to an integral value
21+
// with all bits equal to 1. Primarily useful for 'double's.
22+
// Intended use case:
23+
//
24+
// double ones = bitwise::ones();
25+
// foo(static_cast<double>(bitwise::ones()));
26+
//
27+
class ones
28+
{
29+
public:
30+
31+
operator double() const {
32+
detail::DoublePunner punner { -1ULL };
33+
return punner.d;
34+
}
35+
36+
operator int() const {
37+
return ~0;
38+
}
39+
40+
};
41+
42+
class zeroes
43+
{
44+
public:
45+
template <typename T>
46+
operator T() const { return T(); }
47+
};
48+
49+
} // namespace bitwise
50+
} // namespace RcppNT2
51+
52+
#endif /* RCPP_NT2_BITWISE_BITWISE_H */

0 commit comments

Comments
 (0)