[Rcpp-commits] r3269 - pkg/int64/inst/include/int64
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Nov 3 08:40:07 CET 2011
Author: romain
Date: 2011-11-03 08:40:06 +0100 (Thu, 03 Nov 2011)
New Revision: 3269
Added:
pkg/int64/inst/include/int64/math.h
Log:
abs/sign
Added: pkg/int64/inst/include/int64/math.h
===================================================================
--- pkg/int64/inst/include/int64/math.h (rev 0)
+++ pkg/int64/inst/include/int64/math.h 2011-11-03 07:40:06 UTC (rev 3269)
@@ -0,0 +1,72 @@
+// math.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google Inc. All rights reserved.
+//
+// This file is part of int64.
+//
+// int64 is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// int64 is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with int64. If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef int64__math__h
+#define int64__math__h
+
+namespace int64{
+ namespace internal{
+
+template <typename LONG>
+SEXP abs( SEXP x ){
+ int64::LongVector<LONG> data(x) ;
+ int n = data.size() ;
+ LONG tmp ;
+ int64::LongVector<LONG> res(n) ;
+ for( int i=0; i<n; i++){
+ tmp = data.get(i) ;
+ res.set( i, tmp > 0 ? tmp : -tmp ) ;
+ }
+ return res ;
+}
+template <>
+SEXP abs<uint64_t>( SEXP x ){ return x ; }
+
+template <typename LONG>
+SEXP sign( SEXP x){
+ int64::LongVector<LONG> data(x) ;
+ int n = data.size() ;
+ SEXP res = PROTECT(Rf_allocVector(REALSXP,n)) ;
+ double* p_res = REAL(res) ;
+ for( int i=0; i<n; i++){
+ p_res[i] = ( data.get(i) > 0 ) ? 0.0 : 1.0 ;
+ }
+ UNPROTECT(1) ;
+ return res ;
+}
+
+template <typename LONG>
+SEXP math( const char* op, SEXP x ){
+
+ if( !strcmp( op, "abs" ) ){
+ return abs<LONG>(x) ;
+ } else if( !strcmp(op, "sign") ) {
+ return sign<LONG>(x) ;
+ }
+
+ Rf_error( "operator not implemented" );
+ return R_NilValue ;
+}
+
+ } // namespace internal
+
+} // namespace int64
+
+#endif
More information about the Rcpp-commits
mailing list