VectorCodec is a lossless compression algorithm for arrays of single-precision floating point values with a focus on speed. It is heavily based on FPC, another fast compression algorithm for arrays of doubles.
The current implementation is a ~400 LOC, STB-style header-only library and it (over)uses AVX2 intrinsics. Support for other architectures will be added in the future.
namespace VectorCodec
{
size_t UpperBound(size_t value_count);
size_t Encode(const float* values, size_t value_count, uint8_t* out);
void Decode(const uint8_t* compressed, size_t value_count, float* out);
size_t EncodeQuick(const float* values, size_t value_count, uint8_t* out);
void DecodeQuick(const uint8_t* compressed, size_t value_count, float* out);
}
#include <cstdlib>
#include <vector>
#define VECTOR_CODEC_IMPLEMENTATION
#include <VectorCodec.hpp>
// Generate float array:
auto GenerateExampleArray(size_t count)
{
std::vector<float> r(count);
for (auto& e : r)
e = (float)rand();
return r;
}
int main()
{
unsigned n;
scanf("%u", &n);
const auto values = GenerateExampleArray(n);
// Compression:
std::vector<uint8_t> compressed(VectorCodec::UpperBound(n));
size_t k = VectorCodec::Encode(values.data(), values.size(), compressed.data());
compressed.resize(k);
// Decompression:
std::vector<float> decompressed(values.size());
VectorCodec::Decode(compressed.data(), values.size(), decompressed.data());
return 0;
}