5

I will need 64 bits integer in my package in a close future. I'm studying the feasibility based on the bit64 package. Basically I plan to have one or more columns in a data.table with an interger64 S3 class and I plan to pass this table to C++ functions using Rcpp.

The following nanotime example from Rcpp gallery explains clearly how a vector of 64 bits int is built upon a vector of double and explain how to create an integer64 object from C++ to R.

I'm now wondering how to deal with an interger64 from R to C++. I guess I can invert the principle.

void useInt64(NumericVector v) 
{
    double len = v.size();
    std::vector<int64_t> n(len);

    // transfers values 'keeping bits' but changing type
    // using reinterpret_cast would get us a warning
    std::memcpy(&(n[0]), &(v[0]), len * sizeof(double));

    // use n in further computations
}

Is that correct? Is there another way to do that? Can we use a wrapper as<std::vector<int64_t>>(v)? For this last question I guess the conversion is not based on a bit to bit copy.

JRR
  • 3,024
  • 2
  • 13
  • 37
  • Looks correct. We are doing just that in a few places for `nanotime` and `RcppCCTZ`, and `bit64` does it. You have to be careful with rounding though. In `RcppCCTZ` I added one converter from two doubles. As for the `as<>` converter: you would have to write one. – Dirk Eddelbuettel Feb 27 '18 at 22:39
  • Thanks. I guess you are talking about integer overflow right? No problem I figured out myself that we must be careful. After all `integer64` is a kind of hack. – JRR Feb 28 '18 at 09:53
  • `bit64` is pretty careful to take these 64 bits and interpret as an integer. So you can "transport" them via a `numeric` -- with care. But ... there is nothing Rcpp-specific here, and there is not Rcpp "support" as there is not _native_ integer64 in R we could lean on. – Dirk Eddelbuettel Feb 28 '18 at 13:17
  • @Dirk-Eddelbuettel your comment could be an answer here – jangorecki Nov 21 '20 at 15:12
  • @jangorecki Image a :man_shrugging_emoji: here... – Dirk Eddelbuettel Nov 21 '20 at 15:17

0 Answers0