Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 58 additions & 52 deletions R/read_npy.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,60 +227,66 @@ convert_bytes_to_array <- function(bytes, what, shape, size, endian) {
for (j in seq_len(n_records)) {
res[[j]] <- lapply(res_fields, `[[`, j)
}
} else {
if (is.na(endian)) {
endian <- .Platform$endian
}
# FIXME: optimize this
if (what != "unicode" && endian != .Platform$endian) {
ind <- rep_len(rev(seq_len(size)), length(bytes)) +
(seq_along(bytes) - 1L) %/% size * size
bytes <- bytes[ind]
}
res <- switch(
what,
float = .Call(
C_type_convert_float,
bytes,
size,
PACKAGE = "grumpy"
),
int = .Call(
C_type_convert_int,
bytes,
size,
PACKAGE = "grumpy"
),
uint = .Call(
C_type_convert_uint,
bytes,
size,
PACKAGE = "grumpy"
),
bool = .Call(
C_type_convert_bool,
bytes,
size,
PACKAGE = "grumpy"
),
string = .Call(
C_type_convert_string,
bytes,
size,
PACKAGE = "grumpy"
),
unicode = .Call(
C_type_convert_unicode,
bytes,
size,
endian,
PACKAGE = "grumpy"
),
stop("Unsupported data type: ", what, call. = FALSE)
)
dim(res) <- shape
return(res)
}

dim(res) <- shape
if (is.na(endian)) {
endian <- .Platform$endian
}
# FIXME: optimize this
if (what != "unicode" && endian != .Platform$endian) {
ind <- rep_len(rev(seq_len(size)), length(bytes)) +
(seq_along(bytes) - 1L) %/% size * size
bytes <- bytes[ind]
}
res <- switch(
what,
float = .Call(
C_type_convert_float,
bytes,
size,
shape,
PACKAGE = "grumpy"
),
int = .Call(
C_type_convert_int,
bytes,
size,
shape,
PACKAGE = "grumpy"
),
uint = .Call(
C_type_convert_uint,
bytes,
size,
shape,
PACKAGE = "grumpy"
),
bool = .Call(
C_type_convert_bool,
bytes,
size,
shape,
PACKAGE = "grumpy"
),
string = .Call(
C_type_convert_string,
bytes,
size,
shape,
PACKAGE = "grumpy"
),
unicode = .Call(
C_type_convert_unicode,
bytes,
size,
shape,
endian,
PACKAGE = "grumpy"
),
stop("Unsupported data type: ", what, call. = FALSE)
)

return(res)
}
Expand Down
12 changes: 6 additions & 6 deletions src/grumpy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include "type_conversion.h"

static const R_CallMethodDef callMethods[] = {
{"type_convert_int", (DL_FUNC) &type_convert_int, 2},
{"type_convert_uint", (DL_FUNC) &type_convert_uint, 2},
{"type_convert_float", (DL_FUNC) &type_convert_float, 2},
{"type_convert_bool", (DL_FUNC) &type_convert_bool, 2},
{"type_convert_string", (DL_FUNC) &type_convert_string, 2},
{"type_convert_unicode", (DL_FUNC) &type_convert_unicode, 3},
{"type_convert_int", (DL_FUNC) &type_convert_int, 3},
{"type_convert_uint", (DL_FUNC) &type_convert_uint, 3},
{"type_convert_float", (DL_FUNC) &type_convert_float, 3},
{"type_convert_bool", (DL_FUNC) &type_convert_bool, 3},
{"type_convert_string", (DL_FUNC) &type_convert_string, 3},
{"type_convert_unicode", (DL_FUNC) &type_convert_unicode, 4},
{NULL, NULL, 0}
};

Expand Down
43 changes: 36 additions & 7 deletions src/type_conversion.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "type_conversion.h"
#include <R_ext/Riconv.h>

SEXP type_convert_int(SEXP input, SEXP _n_bytes) {
SEXP type_convert_int(SEXP input, SEXP _n_bytes, SEXP dims) {

const int n_bytes = INTEGER(_n_bytes)[0];
const R_xlen_t length = xlength(input);
Expand Down Expand Up @@ -35,11 +35,15 @@ SEXP type_convert_int(SEXP input, SEXP _n_bytes) {
}
}

/* Set dim attribute if dims is not NULL / NA */
if (!isNull(dims) && xlength(dims) > 0) {
setAttrib(data, R_DimSymbol, dims);
}
UNPROTECT(1);
return(data);
}

SEXP type_convert_uint(SEXP input, SEXP _n_bytes) {
SEXP type_convert_uint(SEXP input, SEXP _n_bytes, SEXP dims) {

const int n_bytes = INTEGER(_n_bytes)[0];
const R_xlen_t length = xlength(input);
Expand Down Expand Up @@ -73,11 +77,16 @@ SEXP type_convert_uint(SEXP input, SEXP _n_bytes) {
}
}

/* Set dim attribute if dims is not NULL / NA */
if (!isNull(dims) && xlength(dims) > 0) {
setAttrib(data, R_DimSymbol, dims);
}

UNPROTECT(1);
return(data);
}

SEXP type_convert_float(SEXP input, SEXP _n_bytes){
SEXP type_convert_float(SEXP input, SEXP _n_bytes, SEXP dims){

const int n_bytes = INTEGER(_n_bytes)[0];
const R_xlen_t length = xlength(input);
Expand Down Expand Up @@ -111,11 +120,16 @@ SEXP type_convert_float(SEXP input, SEXP _n_bytes){
error("%d byte floating point values are not currently supported\n", n_bytes);
}

/* Set dim attribute if dims is not NULL / NA */
if (!isNull(dims) && xlength(dims) > 0) {
setAttrib(data, R_DimSymbol, dims);
}

UNPROTECT(1);
return(data);
}

SEXP type_convert_bool(SEXP input, SEXP _n_bytes) {
SEXP type_convert_bool(SEXP input, SEXP _n_bytes, SEXP dims) {

const R_xlen_t length = xlength(input);
const void* raw_buffer = RAW(input);
Expand All @@ -133,11 +147,16 @@ SEXP type_convert_bool(SEXP input, SEXP _n_bytes) {
p_data[i] = ((const int8_t *)raw_buffer)[i];
}

/* Set dim attribute if dims is not NULL / NA */
if (!isNull(dims) && xlength(dims) > 0) {
setAttrib(data, R_DimSymbol, dims);
}

UNPROTECT(1);
return(data);
}

SEXP type_convert_string(SEXP input, SEXP _n_bytes) {
SEXP type_convert_string(SEXP input, SEXP _n_bytes, SEXP dims) {

const int n_bytes = INTEGER(_n_bytes)[0];
const R_xlen_t length = xlength(input);
Expand Down Expand Up @@ -171,11 +190,16 @@ SEXP type_convert_string(SEXP input, SEXP _n_bytes) {
SET_STRING_ELT(data, i, mkCharCE(field, CE_BYTES));
}

/* Set dim attribute if dims is not NULL / NA */
if (!isNull(dims) && xlength(dims) > 0) {
setAttrib(data, R_DimSymbol, dims);
}

UNPROTECT(1);
return(data);
}

SEXP type_convert_unicode(SEXP input, SEXP _n_bytes, SEXP _endian) {
SEXP type_convert_unicode(SEXP input, SEXP _n_bytes, SEXP dims, SEXP _endian) {

// n_bytes is the total bytes per string element (num_codepoints * 4).
// Bytes are passed as-is from the file; we select UTF-32LE or UTF-32BE
Expand Down Expand Up @@ -232,8 +256,13 @@ SEXP type_convert_unicode(SEXP input, SEXP _n_bytes, SEXP _endian) {

SET_STRING_ELT(data, i, mkCharCE(utf8_buf, CE_UTF8));
}

Riconv_close(cd);

/* Set dim attribute if dims is not NULL / NA */
if (!isNull(dims) && xlength(dims) > 0) {
setAttrib(data, R_DimSymbol, dims);
}

UNPROTECT(1);
return(data);
}
12 changes: 6 additions & 6 deletions src/type_conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "bit64_conversion.h"
#include "float16_conversion.h"

SEXP type_convert_int(SEXP input, SEXP _n_bytes);
SEXP type_convert_uint(SEXP input, SEXP _n_bytes);
SEXP type_convert_float(SEXP input, SEXP _n_bytes);
SEXP type_convert_bool(SEXP input, SEXP _n_bytes);
SEXP type_convert_string(SEXP input, SEXP _n_bytes);
SEXP type_convert_unicode(SEXP input, SEXP _n_bytes, SEXP _endian);
SEXP type_convert_int(SEXP input, SEXP _n_bytes, SEXP dims);
SEXP type_convert_uint(SEXP input, SEXP _n_bytes, SEXP dims);
SEXP type_convert_float(SEXP input, SEXP _n_bytes, SEXP dims);
SEXP type_convert_bool(SEXP input, SEXP _n_bytes, SEXP dims);
SEXP type_convert_string(SEXP input, SEXP _n_bytes, SEXP dims);
SEXP type_convert_unicode(SEXP input, SEXP _n_bytes, SEXP dims, SEXP _endian);
Loading