23#ifndef LSST_SPHGEOM_CODEC_H_
24#define LSST_SPHGEOM_CODEC_H_
27#if defined(__x86_64__) or \
28 (defined(__aarch64__) and \
29 (defined(__LITTLE_ENDIAN__) or \
30 (defined(__BYTE_ORDER__) and __BYTE_ORDER__ == 1234)))
31#define OPTIMIZED_LITTLE_ENDIAN
34#ifdef NO_OPTIMIZED_PATHS
35#undef OPTIMIZED_LITTLE_ENDIAN
49inline void encodeDouble(
double item, std::vector<uint8_t> & buffer) {
50#ifdef OPTIMIZED_LITTLE_ENDIAN
51 auto ptr =
reinterpret_cast<uint8_t
const *
>(&item);
52 buffer.insert(buffer.end(), ptr, ptr + 8);
54 union { uint64_t u;
double d; };
56 buffer.push_back(
static_cast<uint8_t
>(u));
57 buffer.push_back(
static_cast<uint8_t
>(u >> 8));
58 buffer.push_back(
static_cast<uint8_t
>(u >> 16));
59 buffer.push_back(
static_cast<uint8_t
>(u >> 24));
60 buffer.push_back(
static_cast<uint8_t
>(u >> 32));
61 buffer.push_back(
static_cast<uint8_t
>(u >> 40));
62 buffer.push_back(
static_cast<uint8_t
>(u >> 48));
63 buffer.push_back(
static_cast<uint8_t
>(u >> 56));
70#ifdef OPTIMIZED_LITTLE_ENDIAN
71 return *
reinterpret_cast<double const *
>(buffer);
73 union { uint64_t u;
double d; };
74 u =
static_cast<uint64_t
>(buffer[0]) +
75 (
static_cast<uint64_t
>(buffer[1]) << 8) +
76 (
static_cast<uint64_t
>(buffer[2]) << 16) +
77 (
static_cast<uint64_t
>(buffer[3]) << 24) +
78 (
static_cast<uint64_t
>(buffer[4]) << 32) +
79 (
static_cast<uint64_t
>(buffer[5]) << 40) +
80 (
static_cast<uint64_t
>(buffer[6]) << 48) +
81 (
static_cast<uint64_t
>(buffer[7]) << 56);
88inline void encodeU64(std::uint64_t item, std::vector<uint8_t> & buffer) {
89#ifdef OPTIMIZED_LITTLE_ENDIAN
90 auto ptr =
reinterpret_cast<uint8_t
const *
>(&item);
91 buffer.insert(buffer.end(), ptr, ptr + 8);
93 union { uint64_t u;
double d; };
95 buffer.push_back(
static_cast<uint8_t
>(u));
96 buffer.push_back(
static_cast<uint8_t
>(u >> 8));
97 buffer.push_back(
static_cast<uint8_t
>(u >> 16));
98 buffer.push_back(
static_cast<uint8_t
>(u >> 24));
99 buffer.push_back(
static_cast<uint8_t
>(u >> 32));
100 buffer.push_back(
static_cast<uint8_t
>(u >> 40));
101 buffer.push_back(
static_cast<uint8_t
>(u >> 48));
102 buffer.push_back(
static_cast<uint8_t
>(u >> 56));
109#ifdef OPTIMIZED_LITTLE_ENDIAN
110 return *
reinterpret_cast<uint64_t
const *
>(buffer);
112 std::uint64_t u =
static_cast<uint64_t
>(buffer[0]) +
113 (
static_cast<uint64_t
>(buffer[1]) << 8) +
114 (
static_cast<uint64_t
>(buffer[2]) << 16) +
115 (
static_cast<uint64_t
>(buffer[3]) << 24) +
116 (
static_cast<uint64_t
>(buffer[4]) << 32) +
117 (
static_cast<uint64_t
>(buffer[5]) << 40) +
118 (
static_cast<uint64_t
>(buffer[6]) << 48) +
119 (
static_cast<uint64_t
>(buffer[7]) << 56);
void encodeDouble(double item, std::vector< uint8_t > &buffer)
Definition: codec.h:49
void encodeU64(std::uint64_t item, std::vector< uint8_t > &buffer)
Definition: codec.h:88
double decodeDouble(uint8_t const *buffer)
Definition: codec.h:69
std::uint64_t decodeU64(uint8_t const *buffer)
Definition: codec.h:108