Skip to content

Commit e4a0d4c

Browse files
committed
Merge pull request #90 from kevinushey/master
Merging PR#90 from Kevin: Use a union for type-punning; enforce 64bit integer
2 parents 7d64a5c + a06c05a commit e4a0d4c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
2013-12-20 Kevin Ushey <kevinushey@gmail.com>
22

3+
* inst/include/Rcpp/sugar/tools/mapcompare.h: Use a union for
4+
type punning, to avoid compiler warning on aliasing. Also
5+
switch to uint64_t to enforce 64bit comparison.
36
* inst/include/Rcpp/RObject.h: Add missing *this return for
47
RObject_impl::operator=
58

inst/include/Rcpp/sugar/tools/mapcompare.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@
2525
namespace Rcpp {
2626
namespace sugar {
2727

28-
static unsigned long const R_UNSIGNED_LONG_NA_REAL = *(unsigned long*)(&NA_REAL);
29-
static unsigned long const R_UNSIGNED_LONG_NAN_REAL = *(unsigned long*)(&R_NaN);
28+
union DoublePunner {
29+
double d;
30+
uint64_t p;
31+
};
32+
33+
static const DoublePunner NA_REAL_PUN = { NA_REAL };
34+
static const DoublePunner NAN_REAL_PUN = { R_NaN };
3035

3136
inline bool Rcpp_IsNA(double x) {
32-
return *reinterpret_cast<unsigned long*>(&x) == R_UNSIGNED_LONG_NA_REAL;
37+
DoublePunner xp = { x };
38+
return xp.p == NA_REAL_PUN.p;
3339
}
3440

3541
inline bool Rcpp_IsNaN(double x) {
36-
return *reinterpret_cast<unsigned long*>(&x) == R_UNSIGNED_LONG_NAN_REAL;
42+
DoublePunner xp = { x };
43+
return xp.p == NAN_REAL_PUN.p;
3744
}
3845

3946
inline int StrCmp(SEXP x, SEXP y) {

0 commit comments

Comments
 (0)