|
21 | 21 | // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
22 | 22 |
|
23 | 23 | namespace Rcpp { |
24 | | - namespace internal { |
| 24 | +namespace internal { |
25 | 25 |
|
26 | | - // On 64bit processors, NAs can change |
27 | | - // we can still get a performance benefit by checking for specific |
28 | | - // bit patterns, though |
29 | | - |
30 | | - // we rely on the presence of unsigned long long types (could do it with |
31 | | - // a union, but that's messier; this is cleaner) |
32 | | - #ifdef RCPP_HAS_LONG_LONG_TYPES |
33 | | - |
34 | | - #ifdef HAS_STATIC_ASSERT |
35 | | - static_assert( |
36 | | - sizeof(rcpp_ulong_long_type) == sizeof(double), |
37 | | - "unsigned long long and double have same size" |
38 | | - ); |
39 | | - #endif |
40 | | - |
41 | | - static const rcpp_ulong_long_type SmallNA = 0x7FF00000000007A2; |
42 | | - static const rcpp_ulong_long_type LargeNA = 0x7FF80000000007A2; |
43 | | - |
44 | | - struct NACanChange { |
45 | | - enum { value = sizeof(void*) == 8 }; |
46 | | - }; |
47 | | - |
48 | | - template <bool NACanChange> |
49 | | - bool Rcpp_IsNA__impl(double); |
50 | | - |
51 | | - template <> |
52 | | - inline bool Rcpp_IsNA__impl<true>(double x) { |
53 | | - return memcmp( |
| 26 | +// On 64bit processors, NAs can change |
| 27 | +// we can still get a performance benefit by checking for specific |
| 28 | +// bit patterns, though |
| 29 | + |
| 30 | +// we rely on the presence of unsigned long long types (could do it with |
| 31 | +// a union, but that's messier; this is cleaner) |
| 32 | +#ifdef RCPP_HAS_LONG_LONG_TYPES |
| 33 | + |
| 34 | +#ifdef HAS_STATIC_ASSERT |
| 35 | +static_assert( |
| 36 | + sizeof(rcpp_ulong_long_type) == sizeof(double), |
| 37 | + "unsigned long long and double have same size" |
| 38 | +); |
| 39 | +#endif |
| 40 | + |
| 41 | +// motivation: on 32bit architectures, we only see 'LargeNA' |
| 42 | +// as defined ashead; on 64bit architectures, R defaults to |
| 43 | +// 'SmallNA' for R_NaReal, but this can get promoted to 'LargeNA' |
| 44 | +// if a certain operation can create a 'signalling' NA. |
| 45 | +static const rcpp_ulong_long_type SmallNA = 0x7FF00000000007A2; |
| 46 | +static const rcpp_ulong_long_type LargeNA = 0x7FF80000000007A2; |
| 47 | + |
| 48 | +struct NACanChange { |
| 49 | + enum { value = sizeof(void*) == 8 }; |
| 50 | +}; |
| 51 | + |
| 52 | +template <bool NACanChange> |
| 53 | +bool Rcpp_IsNA__impl(double); |
| 54 | + |
| 55 | +template <> |
| 56 | +inline bool Rcpp_IsNA__impl<true>(double x) { |
| 57 | + return memcmp( |
54 | 58 | (void*) &x, |
55 | 59 | (void*) &SmallNA, |
56 | 60 | sizeof(double) |
57 | | - ) == 0 or memcmp( |
| 61 | + ) == 0 or memcmp( |
58 | 62 | (void*) &x, |
59 | 63 | (void*) &LargeNA, |
60 | 64 | sizeof(double) |
61 | | - ) == 0; |
62 | | - } |
63 | | - |
64 | | - template <> |
65 | | - inline bool Rcpp_IsNA__impl<false>(double x) { |
66 | | - printf("false\n"); |
67 | | - return memcmp( |
| 65 | + ) == 0; |
| 66 | +} |
| 67 | + |
| 68 | +template <> |
| 69 | +inline bool Rcpp_IsNA__impl<false>(double x) { |
| 70 | + return memcmp( |
68 | 71 | (void*) &x, |
69 | 72 | (void*) &LargeNA, |
70 | 73 | sizeof(double) |
71 | | - ) == 0; |
72 | | - } |
73 | | - |
74 | | - inline bool Rcpp_IsNA(double x) { |
75 | | - return Rcpp_IsNA__impl< NACanChange::value >(x); |
76 | | - } |
77 | | - |
78 | | - inline bool Rcpp_IsNaN(double x) { |
79 | | - return R_IsNaN(x); |
80 | | - } |
81 | | - |
82 | | - #else |
83 | | - |
84 | | - // fallback when we don't have unsigned long long |
85 | | - |
86 | | - inline bool Rcpp_IsNA(double x) { |
87 | | - return R_IsNA(x); |
88 | | - } |
89 | | - |
90 | | - inline bool Rcpp_IsNaN(double x) { |
91 | | - return R_IsNaN(x); |
92 | | - } |
93 | | - |
94 | | - #endif |
| 74 | + ) == 0; |
| 75 | +} |
| 76 | + |
| 77 | +inline bool Rcpp_IsNA(double x) { |
| 78 | + return Rcpp_IsNA__impl< NACanChange::value >(x); |
| 79 | +} |
| 80 | + |
| 81 | +inline bool Rcpp_IsNaN(double x) { |
| 82 | + return R_IsNaN(x); |
| 83 | +} |
| 84 | + |
| 85 | +#else |
| 86 | + |
| 87 | +// fallback when we don't have unsigned long long |
| 88 | + |
| 89 | +inline bool Rcpp_IsNA(double x) { |
| 90 | + return R_IsNA(x); |
| 91 | +} |
| 92 | + |
| 93 | +inline bool Rcpp_IsNaN(double x) { |
| 94 | + return R_IsNaN(x); |
| 95 | +} |
| 96 | + |
| 97 | +#endif |
95 | 98 |
|
96 | | - } |
| 99 | +} |
97 | 100 | } |
0 commit comments