46static_assert(std::endian::native == std::endian::little,
47 "Byte Stream Split encoding requires little-endian platform");
50namespace byte_stream_split {
67[[nodiscard]]
inline std::vector<uint8_t>
encode_float(
const float* values,
69 constexpr size_t WIDTH =
sizeof(float);
70 if (count > SIZE_MAX / WIDTH)
return {};
71 std::vector<uint8_t> out(count * WIDTH);
73 if (count == 0)
return out;
76 const auto* src =
reinterpret_cast<const uint8_t*
>(values);
80 for (
size_t b = 0; b < WIDTH; ++b) {
81 uint8_t* dst = out.data() + b * count;
82 for (
size_t i = 0; i < count; ++i) {
83 dst[i] = src[i * WIDTH + b];
101[[nodiscard]]
inline std::vector<uint8_t>
encode_double(
const double* values,
103 constexpr size_t WIDTH =
sizeof(double);
104 if (count > SIZE_MAX / WIDTH)
return {};
105 std::vector<uint8_t> out(count * WIDTH);
107 if (count == 0)
return out;
109 const auto* src =
reinterpret_cast<const uint8_t*
>(values);
111 for (
size_t b = 0; b < WIDTH; ++b) {
112 uint8_t* dst = out.data() + b * count;
113 for (
size_t i = 0; i < count; ++i) {
114 dst[i] = src[i * WIDTH + b];
136[[nodiscard]]
inline std::vector<float>
decode_float(
const uint8_t* data,
139 constexpr size_t WIDTH =
sizeof(float);
140 if (count > SIZE_MAX / WIDTH || count * WIDTH > size)
return {};
142 std::vector<float> out(count);
144 if (count == 0)
return out;
146 auto* dst =
reinterpret_cast<uint8_t*
>(out.data());
150 for (
size_t b = 0; b < WIDTH; ++b) {
151 const uint8_t* src = data + b * count;
152 for (
size_t i = 0; i < count; ++i) {
153 dst[i * WIDTH + b] = src[i];
174 constexpr size_t WIDTH =
sizeof(double);
175 if (count > SIZE_MAX / WIDTH || count * WIDTH > size)
return {};
177 std::vector<double> out(count);
179 if (count == 0)
return out;
181 auto* dst =
reinterpret_cast<uint8_t*
>(out.data());
183 for (
size_t b = 0; b < WIDTH; ++b) {
184 const uint8_t* src = data + b * count;
185 for (
size_t i = 0; i < count; ++i) {
186 dst[i * WIDTH + b] = src[i];
std::vector< uint8_t > encode_float(const float *values, size_t count)
Encode float values using the BYTE_STREAM_SPLIT algorithm.
std::vector< uint8_t > encode_double(const double *values, size_t count)
Encode double values using the BYTE_STREAM_SPLIT algorithm.
std::vector< float > decode_float(const uint8_t *data, size_t size, size_t count)
Decode float values from BYTE_STREAM_SPLIT encoding.
std::vector< double > decode_double(const uint8_t *data, size_t size, size_t count)
Decode double values from BYTE_STREAM_SPLIT encoding.