-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconversion.h
86 lines (68 loc) · 2.95 KB
/
conversion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef FARR_CONVERSION_H
#define FARR_CONVERSION_H
#include "common.h"
#include "serialize.h"
#include "defs.h"
double na_cplx_dbl();
SEXP realToInt64(Rcpp::NumericVector x, const double min_ = NA_REAL, const double max_ = NA_REAL, const int strict = 1);
SEXP realToInt64_inplace(SEXP x, const double min_ = NA_REAL, const double max_ = NA_REAL, const int strict = 1);
SEXP convert_as(SEXP x, SEXPTYPE type);
SEXP convert_as2(SEXP x, SEXP y, SEXPTYPE type);
// void realToCplx(double* x, Rcomplex* y, size_t nelem);
void realToCplx(const double* x, Rcomplex* y, const size_t& nelem, const bool swap_endian = false);
void cplxToReal(Rcomplex* x, double* y, size_t nelem);
void realToFloat(double* x, float* y, size_t nelem);
void floatToReal(float* x, double* y, size_t nelem);
/**********************************************************
* Transform functions
***********************************************************/
template <typename T>
inline void transform_asis(const T* x, T* y, const bool& swap_endian = false) {
if( swap_endian ){
const size_t size = sizeof(T);
T tmp;
unsigned char *buffer_src = (unsigned char*) x;
unsigned char *buffer_dst = (unsigned char*) (&tmp);
for (size_t ix = 0; ix < size; ix++) {
*(buffer_dst + (size - ix - 1)) = *(buffer_src + ix);
}
*y = tmp;
} else {
memcpy(y, x, sizeof(T));
// *y = *x;
}
}
void transform_float(const float* x, double* y, const bool& swap_endian = false);
void transform_logical(const Rbyte* x, int* y, const bool& swap_endian = false);
void transform_complex(const double* x, Rcomplex* y, const bool& swap_endian = false);
template <typename T>
inline void transforms_asis(const T* x, T* y, const int& nelem, const bool& swap_endian = false){
memcpy(y, x, nelem * sizeof(T));
if( swap_endian ){
swap_endianess(y, sizeof(T), nelem);
}
}
void transforms_float(const float* x, double* y, const int& nelem, const bool& swap_endian = false);
void transforms_logical(const Rbyte* x, int* y, const int& nelem, const bool& swap_endian = false);
void transforms_complex(const double* x, Rcomplex* y, const int& nelem, const bool& swap_endian = false);
template <typename T, typename B>
inline void transforms(const T* x, B* y, const int& nelem, const bool& swap_endian = false){
if(std::is_same<T, B>::value){
transforms_asis<T>(x, (T*) y, nelem, swap_endian);
return;
}
if(std::is_same<T, float>::value){
transforms_float((const float*) x, (double*) y, nelem, swap_endian);
return;
}
if(std::is_same<T, Rbyte>::value){
transforms_logical((const Rbyte*) x, (int*) y, nelem, swap_endian);
return;
}
if(std::is_same<B, Rcomplex>::value){
transforms_complex((const double*) x, (Rcomplex*) y, nelem, swap_endian);
return;
}
Rcpp::stop("Unknown type in `transforms`.");
}
#endif // FARR_CONVERSION_H