-
Notifications
You must be signed in to change notification settings - Fork 349
Add bit64 support #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kdkavanagh
wants to merge
5
commits into
rstudio:main
Choose a base branch
from
kdkavanagh:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add bit64 support #590
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
91e8747
Add bit64 support
kdkavanagh 4a697bb
Update tests and add dep on bit64
kdkavanagh 791bd66
Add options to configure bit64 usage
kdkavanagh 5dfec60
Merge branch 'master' of https://github.com/rstudio/reticulate
kdkavanagh 4a66b3a
Merge branch 'main' into master
kdkavanagh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ Imports: | |
| graphics, | ||
| jsonlite, | ||
| Rcpp (>= 0.12.7), | ||
| bit64, | ||
| Matrix, | ||
| methods | ||
| Suggests: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,17 +201,18 @@ int narrow_array_typenum(int typenum) { | |
| case NPY_SHORT: | ||
| case NPY_USHORT: | ||
| case NPY_INT: | ||
| case NPY_LONG: | ||
| typenum = NPY_LONG; | ||
| break; | ||
| // double | ||
| case NPY_UINT: | ||
| case NPY_ULONG: | ||
| case NPY_ULONGLONG: | ||
| case NPY_LONG: | ||
| case NPY_LONGLONG: | ||
| case NPY_HALF: | ||
| case NPY_FLOAT: | ||
| case NPY_DOUBLE: | ||
|
|
||
| typenum = NPY_DOUBLE; | ||
| break; | ||
|
|
||
|
|
@@ -618,9 +619,18 @@ SEXP py_to_r(PyObject* x, bool convert) { | |
| return LogicalVector::create(x == Py_True); | ||
|
|
||
| // integer | ||
| else if (scalarType == INTSXP) | ||
| return IntegerVector::create(PyInt_AsLong(x)); | ||
|
|
||
| else if (scalarType == INTSXP) { | ||
| long val = PyInt_AsLong(x); | ||
| if(val > std::numeric_limits<int>::max()) { | ||
| Rcpp::NumericVector vec(1); | ||
| std::memcpy(&(vec[0]), &(val), sizeof(double)); | ||
| vec.attr("class") = "integer64"; | ||
| return vec; | ||
| } | ||
| else{ | ||
| return IntegerVector::create(val); | ||
| } | ||
| } | ||
| // double | ||
| else if (scalarType == REALSXP) | ||
| return NumericVector::create(PyFloat_AsDouble(x)); | ||
|
|
@@ -653,9 +663,24 @@ SEXP py_to_r(PyObject* x, bool convert) { | |
| return vec; | ||
| } else if (scalarType == INTSXP) { | ||
| Rcpp::IntegerVector vec(len); | ||
| for (Py_ssize_t i = 0; i<len; i++) | ||
| vec[i] = PyInt_AsLong(PyList_GetItem(x, i)); | ||
| for (Py_ssize_t i = 0; i<len; i++) { | ||
| long num = PyInt_AsLong(PyList_GetItem(x, i)); | ||
| if(num > std::numeric_limits<int>::max()) { | ||
| //We need to start over an interpret as 64 bit int | ||
| Rcpp::NumericVector nVec(len); | ||
| long long* res_ptr = (long long*) dataptr(nVec); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that using |
||
| for (Py_ssize_t j = 0; j<len; j++) { | ||
| res_ptr[j] = PyInt_AsLong(PyList_GetItem(x, j));; | ||
| } | ||
| nVec.attr("class") = "integer64"; | ||
| return nVec; | ||
| break; | ||
| } else { | ||
| vec[i] = num; | ||
| } | ||
| } | ||
| return vec; | ||
|
|
||
| } else if (scalarType == CPLXSXP) { | ||
| Rcpp::ComplexVector vec(len); | ||
| for (Py_ssize_t i = 0; i<len; i++) { | ||
|
|
@@ -769,8 +794,17 @@ SEXP py_to_r(PyObject* x, bool convert) { | |
| case NPY_LONG: { | ||
| npy_long* pData = (npy_long*)PyArray_DATA(array); | ||
| rArray = Rf_allocArray(INTSXP, dimsVector); | ||
| for (int i=0; i<len; i++) | ||
| for (int i=0; i<len; i++) { | ||
| if(pData[i] > std::numeric_limits<int>::max()) { | ||
| Rcpp::NumericVector nVec(len); | ||
| //Inspired by https://gallery.rcpp.org/articles/creating-integer64-and-nanotime-vectors/ | ||
| std::memcpy(&(nVec[0]), &(pData[0]), len * sizeof(double)); | ||
| nVec.attr("class") = "integer64"; | ||
| nVec.attr("dim") = dimsVector; | ||
| return nVec; | ||
| } | ||
| INTEGER(rArray)[i] = pData[i]; | ||
| } | ||
| break; | ||
| } | ||
| case NPY_DOUBLE: { | ||
|
|
@@ -1083,7 +1117,7 @@ PyObject* r_to_py_cpp(RObject x, bool convert) { | |
| typenum = NPY_INT; | ||
| data = &(INTEGER(sexp)[0]); | ||
| } else if (type == REALSXP) { | ||
| typenum = NPY_DOUBLE; | ||
| typenum = x.inherits("integer64") ? NPY_LONG : NPY_DOUBLE; | ||
| data = &(REAL(sexp)[0]); | ||
| } else if (type == LGLSXP) { | ||
| typenum = NPY_BOOL; | ||
|
|
@@ -1178,15 +1212,16 @@ PyObject* r_to_py_cpp(RObject x, bool convert) { | |
|
|
||
| // numeric (pass length 1 vectors as scalars, otherwise pass list) | ||
| } else if (type == REALSXP) { | ||
| bool isInt64=x.inherits("integer64"); | ||
| if (LENGTH(sexp) == 1) { | ||
| double value = REAL(sexp)[0]; | ||
| return PyFloat_FromDouble(value); | ||
| return isInt64 ? PyInt_FromLong(reinterpret_cast<long&>(value)) : PyFloat_FromDouble(value); | ||
| } else { | ||
| PyObjectPtr list(PyList_New(LENGTH(sexp))); | ||
| for (R_xlen_t i = 0; i<LENGTH(sexp); i++) { | ||
| double value = REAL(sexp)[i]; | ||
| // NOTE: reference to added value is "stolen" by the list | ||
| int res = PyList_SetItem(list, i, PyFloat_FromDouble(value)); | ||
| int res = PyList_SetItem(list, i, isInt64 ? PyInt_FromLong(reinterpret_cast<long&>(value)) : PyFloat_FromDouble(value)); | ||
| if (res != 0) | ||
| stop(py_fetch_error()); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, could loop thru array once first to check max value, then create the right vector type the first time