|
| 1 | +// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- |
| 2 | +/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */ |
| 3 | +// |
| 4 | +// na.h: Rcpp R/C++ interface class library -- optimized na checking |
| 5 | +// |
| 6 | +// Copyright (C) 2012-2014 Dirk Eddelbuettel, Romain Francois and Kevin Ushey |
| 7 | +// |
| 8 | +// This file is part of Rcpp. |
| 9 | +// |
| 10 | +// Rcpp is free software: you can redistribute it and/or modify it |
| 11 | +// under the terms of the GNU General Public License as published by |
| 12 | +// the Free Software Foundation, either version 2 of the License, or |
| 13 | +// (at your option) any later version. |
| 14 | +// |
| 15 | +// Rcpp is distributed in the hope that it will be useful, but |
| 16 | +// WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +// GNU General Public License for more details. |
| 19 | +// |
| 20 | +// You should have received a copy of the GNU General Public License |
| 21 | +// along with Rcpp. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + |
| 23 | +namespace Rcpp { |
| 24 | + namespace internal { |
| 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( |
| 54 | + (void*) &x, |
| 55 | + (void*) &SmallNA, |
| 56 | + sizeof(double) |
| 57 | + ) == 0 or memcmp( |
| 58 | + (void*) &x, |
| 59 | + (void*) &LargeNA, |
| 60 | + sizeof(double) |
| 61 | + ) == 0; |
| 62 | + } |
| 63 | + |
| 64 | + template <> |
| 65 | + inline bool Rcpp_IsNA__impl<false>(double x) { |
| 66 | + printf("false\n"); |
| 67 | + return memcmp( |
| 68 | + (void*) &x, |
| 69 | + (void*) &LargeNA, |
| 70 | + 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 |
| 95 | + |
| 96 | + } |
| 97 | +} |
0 commit comments