Attempting to use an external library to handle bitstreams. Isn't going the grreatest.

This commit is contained in:
2024-10-11 15:41:46 -04:00
parent b9502ebe86
commit 170fdddcf0
30 changed files with 3239 additions and 334 deletions
@@ -0,0 +1,104 @@
#pragma once
/*
* Copyright (c) 2018 Stanislav Denisov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstdint>
namespace bitstream
{
/**
* @brief Class for quantizing single-precision floats into a range and precision
*/
class bounded_range
{
public:
constexpr bounded_range() noexcept :
m_Min(0),
m_Max(0),
m_Precision(0),
m_BitsRequired(0),
m_Mask(0) {}
constexpr bounded_range(float min, float max, float precision) noexcept :
m_Min(min),
m_Max(max),
m_Precision(precision),
m_BitsRequired(log2(static_cast<uint32_t>((m_Max - m_Min) * (1.0f / precision) + 0.5f)) + 1),
m_Mask((1U << m_BitsRequired) - 1U) {}
constexpr inline float get_min() const noexcept { return m_Min; }
constexpr inline float get_max() const noexcept { return m_Max; }
constexpr inline float get_precision() const noexcept { return m_Precision; }
constexpr inline uint32_t get_bits_required() const noexcept { return m_BitsRequired; }
constexpr inline uint32_t quantize(float value) const noexcept
{
if (value < m_Min)
value = m_Min;
else if (value > m_Max)
value = m_Max;
return static_cast<uint32_t>(static_cast<float>((value - m_Min) * (1.0f / m_Precision)) + 0.5f) & m_Mask;
}
constexpr inline float dequantize(uint32_t data) const noexcept
{
float adjusted = (static_cast<float>(data) * m_Precision) + m_Min;
if (adjusted < m_Min)
adjusted = m_Min;
else if (adjusted > m_Max)
adjusted = m_Max;
return adjusted;
}
private:
constexpr inline static uint32_t log2(uint32_t value) noexcept
{
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
return DE_BRUIJN[(value * 0x07C4ACDDU) >> 27];
}
private:
float m_Min;
float m_Max;
float m_Precision;
uint32_t m_BitsRequired;
uint32_t m_Mask;
constexpr inline static uint32_t DE_BRUIJN[32]
{
0, 9, 1, 10, 13, 21, 2, 29,
11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7,
19, 27, 23, 6, 26, 5, 4, 31
};
};
}
@@ -0,0 +1,114 @@
#pragma once
/*
* Copyright (c) 2018 Stanislav Denisov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstdint>
#include <cstring>
namespace bitstream
{
/**
* @brief Class for quantizing single-precision floats into half-precision
*/
class half_precision
{
public:
inline static uint16_t quantize(float value) noexcept
{
int32_t tmp;
std::memcpy(&tmp, &value, sizeof(float));
int32_t s = (tmp >> 16) & 0x00008000;
int32_t e = ((tmp >> 23) & 0X000000FF) - (127 - 15);
int32_t m = tmp & 0X007FFFFF;
if (e <= 0) {
if (e < -10)
return static_cast<uint16_t>(s);
m |= 0x00800000;
int32_t t = 14 - e;
int32_t a = (1 << (t - 1)) - 1;
int32_t b = (m >> t) & 1;
m = (m + a + b) >> t;
return static_cast<uint16_t>(s | m);
}
if (e == 0XFF - (127 - 15)) {
if (m == 0)
return static_cast<uint16_t>(s | 0X7C00);
m >>= 13;
return static_cast<uint16_t>(s | 0X7C00 | m | ((m == 0) ? 1 : 0));
}
m = m + 0X00000FFF + ((m >> 13) & 1);
if ((m & 0x00800000) != 0) {
m = 0;
e++;
}
if (e > 30)
return static_cast<uint16_t>(s | 0X7C00);
return static_cast<uint16_t>(s | (e << 10) | (m >> 13));
}
inline static float dequantize(uint16_t value) noexcept
{
uint32_t tmp;
uint32_t mantissa = static_cast<uint32_t>(value & 1023);
uint32_t exponent = 0XFFFFFFF2;
if ((value & -33792) == 0) {
if (mantissa != 0) {
while ((mantissa & 1024) == 0) {
exponent--;
mantissa <<= 1;
}
mantissa &= 0XFFFFFBFF;
tmp = ((static_cast<uint32_t>(value) & 0x8000) << 16) | ((exponent + 127) << 23) | (mantissa << 13);
}
else
{
tmp = static_cast<uint32_t>((value & 0x8000) << 16);
}
}
else
{
tmp = ((static_cast<uint32_t>(value) & 0x8000) << 16) | (((((static_cast<uint32_t>(value) >> 10) & 0X1F) - 15) + 127) << 23) | (mantissa << 13);
}
float result;
std::memcpy(&result, &tmp, sizeof(float));
return result;
}
};
}
@@ -0,0 +1,156 @@
#pragma once
/*
* Copyright (c) 2020 Stanislav Denisov, Maxim Munning, Davin Carten
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <cstdint>
#include <cmath>
namespace bitstream
{
/**
* @brief A quantized representation of a quaternion
*/
struct quantized_quaternion
{
uint32_t m;
uint32_t a;
uint32_t b;
uint32_t c;
constexpr quantized_quaternion() noexcept :
m(0),
a(0),
b(0),
c(0) {}
constexpr quantized_quaternion(uint32_t w, uint32_t x, uint32_t y, uint32_t z) noexcept :
m(w), a(x), b(y), c(z) {}
};
/**
* @brief Class for quantizing a user-specified quaternion into fewer bits using the smallest-three algorithm
* @tparam T The quaternion-type to quantize
*/
template<typename T, size_t BitsPerElement = 12>
class smallest_three
{
private:
static constexpr float SMALLEST_THREE_UNPACK = 0.70710678118654752440084436210485f + 0.0000001f;
static constexpr float SMALLEST_THREE_PACK = 1.0f / SMALLEST_THREE_UNPACK;
public:
inline static quantized_quaternion quantize(const T& quaternion) noexcept
{
constexpr float half_range = static_cast<float>(1 << (BitsPerElement - 1));
constexpr float packer = SMALLEST_THREE_PACK * half_range;
float max_value = -1.0f;
bool sign_minus = false;
uint32_t m = 0;
uint32_t a = 0;
uint32_t b = 0;
uint32_t c = 0;
for (uint32_t i = 0; i < 4; i++)
{
float element = quaternion[i];
float abs = element > 0.0f ? element : -element;
if (abs > max_value)
{
sign_minus = element < 0.0f;
m = i;
max_value = abs;
}
}
float af = 0.0f;
float bf = 0.0f;
float cf = 0.0f;
switch (m)
{
case 0:
af = quaternion[1];
bf = quaternion[2];
cf = quaternion[3];
break;
case 1:
af = quaternion[0];
bf = quaternion[2];
cf = quaternion[3];
break;
case 2:
af = quaternion[0];
bf = quaternion[1];
cf = quaternion[3];
break;
default: // case 3
af = quaternion[0];
bf = quaternion[1];
cf = quaternion[2];
break;
}
if (sign_minus)
{
a = static_cast<uint32_t>((-af * packer) + half_range);
b = static_cast<uint32_t>((-bf * packer) + half_range);
c = static_cast<uint32_t>((-cf * packer) + half_range);
}
else
{
a = static_cast<uint32_t>((af * packer) + half_range);
b = static_cast<uint32_t>((bf * packer) + half_range);
c = static_cast<uint32_t>((cf * packer) + half_range);
}
return { m, a, b, c };
}
inline static T dequantize(const quantized_quaternion& data) noexcept
{
constexpr uint32_t half_range = (1 << (BitsPerElement - 1));
constexpr float unpacker = SMALLEST_THREE_UNPACK * (1.0f / half_range);
float a = static_cast<float>(data.a * unpacker - half_range * unpacker);
float b = static_cast<float>(data.b * unpacker - half_range * unpacker);
float c = static_cast<float>(data.c * unpacker - half_range * unpacker);
float d = std::sqrt(1.0f - ((a * a) + (b * b) + (c * c)));
switch (data.m)
{
case 0:
return T{ d, a, b, c };
case 1:
return T{ a, d, b, c };
case 2:
return T{ a, b, d, c };
default: // case 3
return T{ a, b, c, d };
}
}
};
}