Skip to content

Instantly share code, notes, and snippets.

@froody
Created April 18, 2021 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save froody/3fc7131af40592abdd97357fd3dc688e to your computer and use it in GitHub Desktop.
Save froody/3fc7131af40592abdd97357fd3dc688e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <xtensor/xfixed.hpp>
#include <xtensor/xview.hpp>
static struct UnpackBitsLookupTable {
xt::xtensor_fixed<uint8_t, xt::xshape<256, 8>> table;
UnpackBitsLookupTable() {
for (uint32_t i = 0; i < 256; i++) {
for (uint32_t j = 0; j < 8; j++) {
if (i & (1U << (7 - j))) {
table(i, j) = 1;
std::cout << i << "," << j << "=1\n";
} else {
table(i, j) = 0;
std::cout << i << "," << j << "=0\n";
}
}
}
}
} BitsLookup;
int main(void) {
xt::xarray<uint8_t> voxels = {20, 15, 33, 218, 5, 9};
size_t num_bits = voxels.size() * 8 - 1;
auto iv = xt::view(BitsLookup.table, xt::keep(voxels), xt::all());
for (int i = 0; i < iv.shape(0); i++) {
for (int j = 0; j < iv.shape(1); j++) {
std::cout << "iv (" << i << "," << j << ") = " << (int)iv(i, j)
<< "\n" << std::flush;
}
}
auto rv = xt::reshape_view(iv, {iv.size()});
auto fv = xt::view(rv, xt::range(0, num_bits));
xt::xarray<uint8_t> unpacked = xt::xarray<uint8_t>::from_shape({num_bits});
unpacked = fv;
for (int i = 0; i < unpacked.size(); i++) {
std::cout << "up (" << i << ") = " << (int)unpacked(i) << "\n" << std::flush;
if (unpacked(i) != fv(i)) {
std::cout << "mismatch at " << i << ": " << (int)unpacked(i)
<< " != " << (int)fv(i) << "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment