30#ifndef LSST_SPHGEOM_CODEC_H_
31#define LSST_SPHGEOM_CODEC_H_
34#if defined(__x86_64__) or \
35 (defined(__aarch64__) and \
36 (defined(__LITTLE_ENDIAN__) or \
37 (defined(__BYTE_ORDER__) and __BYTE_ORDER__ == 1234)))
38#define OPTIMIZED_LITTLE_ENDIAN
41#ifdef NO_OPTIMIZED_PATHS
42#undef OPTIMIZED_LITTLE_ENDIAN
56inline void encodeDouble(
double item, std::vector<uint8_t> & buffer) {
57#ifdef OPTIMIZED_LITTLE_ENDIAN
58 auto ptr =
reinterpret_cast<uint8_t
const *
>(&item);
59 buffer.insert(buffer.end(), ptr, ptr + 8);
61 union { uint64_t u;
double d; };
63 buffer.push_back(
static_cast<uint8_t
>(u));
64 buffer.push_back(
static_cast<uint8_t
>(u >> 8));
65 buffer.push_back(
static_cast<uint8_t
>(u >> 16));
66 buffer.push_back(
static_cast<uint8_t
>(u >> 24));
67 buffer.push_back(
static_cast<uint8_t
>(u >> 32));
68 buffer.push_back(
static_cast<uint8_t
>(u >> 40));
69 buffer.push_back(
static_cast<uint8_t
>(u >> 48));
70 buffer.push_back(
static_cast<uint8_t
>(u >> 56));
77#ifdef OPTIMIZED_LITTLE_ENDIAN
78 return *
reinterpret_cast<double const *
>(buffer);
80 union { uint64_t u;
double d; };
81 u =
static_cast<uint64_t
>(buffer[0]) +
82 (
static_cast<uint64_t
>(buffer[1]) << 8) +
83 (
static_cast<uint64_t
>(buffer[2]) << 16) +
84 (
static_cast<uint64_t
>(buffer[3]) << 24) +
85 (
static_cast<uint64_t
>(buffer[4]) << 32) +
86 (
static_cast<uint64_t
>(buffer[5]) << 40) +
87 (
static_cast<uint64_t
>(buffer[6]) << 48) +
88 (
static_cast<uint64_t
>(buffer[7]) << 56);
95inline void encodeU64(std::uint64_t item, std::vector<uint8_t> & buffer) {
96#ifdef OPTIMIZED_LITTLE_ENDIAN
97 auto ptr =
reinterpret_cast<uint8_t
const *
>(&item);
98 buffer.insert(buffer.end(), ptr, ptr + 8);
100 union { uint64_t u;
double d; };
102 buffer.push_back(
static_cast<uint8_t
>(u));
103 buffer.push_back(
static_cast<uint8_t
>(u >> 8));
104 buffer.push_back(
static_cast<uint8_t
>(u >> 16));
105 buffer.push_back(
static_cast<uint8_t
>(u >> 24));
106 buffer.push_back(
static_cast<uint8_t
>(u >> 32));
107 buffer.push_back(
static_cast<uint8_t
>(u >> 40));
108 buffer.push_back(
static_cast<uint8_t
>(u >> 48));
109 buffer.push_back(
static_cast<uint8_t
>(u >> 56));
116#ifdef OPTIMIZED_LITTLE_ENDIAN
117 return *
reinterpret_cast<uint64_t
const *
>(buffer);
119 std::uint64_t u =
static_cast<uint64_t
>(buffer[0]) +
120 (
static_cast<uint64_t
>(buffer[1]) << 8) +
121 (
static_cast<uint64_t
>(buffer[2]) << 16) +
122 (
static_cast<uint64_t
>(buffer[3]) << 24) +
123 (
static_cast<uint64_t
>(buffer[4]) << 32) +
124 (
static_cast<uint64_t
>(buffer[5]) << 40) +
125 (
static_cast<uint64_t
>(buffer[6]) << 48) +
126 (
static_cast<uint64_t
>(buffer[7]) << 56);
void encodeDouble(double item, std::vector< uint8_t > &buffer)
Definition codec.h:56
void encodeU64(std::uint64_t item, std::vector< uint8_t > &buffer)
Definition codec.h:95
double decodeDouble(uint8_t const *buffer)
Definition codec.h:76
std::uint64_t decodeU64(uint8_t const *buffer)
Definition codec.h:115