Skip to content

Commit

Permalink
Assert that we're using IEEE floats and two's complement integers.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Dec 4, 2024
1 parent 9f91668 commit 684f741
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ The length of the array is guaranteed to be equal to `row_count`.
## Data encoding

Integer data (`"integer"`) are encoded as a DEFLATE-compressed array of 32-bit signed integers in the specified `byte_order`.
It is expected that integers are using a two's complement representation.
Missing values are represented as -2147483648.

Double-precision data (`"double"`) are encoded as a DEFLATE-compressed array of 64-bit IEEE double-precision floats in the specified `byte_order`.
Double-precision data (`"double"`) are encoded as a DEFLATE-compressed array of 64-bit IEEE754 double-precision floats in the specified `byte_order`.
This may contain IEEE special values like NaN and infinity.
Missing values are encoded as NaN with a payload of 1954, inherited from R
(see discussion [here](https://stackoverflow.com/questions/70471859/difference-between-na-real-and-nan/70472081#70472081)).
Expand Down
11 changes: 11 additions & 0 deletions src/get_byte_order.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#include "Rcpp.h"

#include <cstdint>
#include <limits>

//[[Rcpp::export(rng=false)]]
Rcpp::CharacterVector get_byte_order() {
uint32_t val = 1;
auto ptr = reinterpret_cast<unsigned char*>(&val);

// must use IEEE754 doubles for the binary representation to be correct, see:
// https://stackoverflow.com/questions/19351843/why-is-ieee-754-floating-point-not-exchangable-between-platforms
static_assert(std::numeric_limits<double>::is_iec559);

// check that we're using 2's complement, see:
// https://stackoverflow.com/questions/64842669/how-to-test-if-a-target-has-twos-complement-integers-with-the-c-preprocessor
// https://stackoverflow.com/questions/12276957/are-there-any-non-twos-complement-implementations-of-c
static_assert((static_cast<int32_t>(-1) & static_cast<int32_t>(3)) == static_cast<int32_t>(3));

if (ptr[0] == 1) {
return Rcpp::CharacterVector::create("little_endian");
} else {
Expand Down

0 comments on commit 684f741

Please sign in to comment.