BitStream
quantization_traits.h
Go to the documentation of this file.
1 #pragma once
2 #include "../quantization/bounded_range.h"
3 #include "../quantization/half_precision.h"
4 #include "../quantization/smallest_three.h"
5 #include "../utility/assert.h"
6 #include "../utility/meta.h"
7 #include "../utility/parameter.h"
8 
9 #include "../stream/serialize_traits.h"
10 
11 #include <cstdint>
12 
13 namespace bitstream
14 {
18  template<>
20  {
21  template<typename Stream>
23  static serialize(Stream& stream, in<float> value) noexcept
24  {
25  uint32_t int_value = half_precision::quantize(value);
26 
27  BS_ASSERT(stream.serialize_bits(int_value, 16));
28 
29  return true;
30  }
31 
32  template<typename Stream>
34  static serialize(Stream& stream, out<float> value) noexcept
35  {
36  uint32_t int_value;
37 
38  BS_ASSERT(stream.serialize_bits(int_value, 16));
39 
40  value = half_precision::dequantize(int_value);
41 
42  return true;
43  }
44  };
45 
49  template<>
51  {
52  template<typename Stream>
54  static serialize(Stream& stream, in<bounded_range> range, in<float> value) noexcept
55  {
56  uint32_t int_value = range.quantize(value);
57 
58  BS_ASSERT(stream.serialize_bits(int_value, range.get_bits_required()));
59 
60  return true;
61  }
62 
63  template<typename Stream>
65  static serialize(Stream& stream, in<bounded_range> range, out<float> value) noexcept
66  {
67  uint32_t int_value;
68 
69  BS_ASSERT(stream.serialize_bits(int_value, range.get_bits_required()));
70 
71  value = range.dequantize(int_value);
72 
73  return true;
74  }
75  };
76 
80  template<typename Q, size_t BitsPerElement>
81  struct serialize_traits<smallest_three<Q, BitsPerElement>>
82  {
83  template<typename Stream>
85  static serialize(Stream& stream, in<Q> value) noexcept
86  {
88 
89  BS_ASSERT(stream.serialize_bits(quantized_quat.m, 2));
90  BS_ASSERT(stream.serialize_bits(quantized_quat.a, BitsPerElement));
91  BS_ASSERT(stream.serialize_bits(quantized_quat.b, BitsPerElement));
92  BS_ASSERT(stream.serialize_bits(quantized_quat.c, BitsPerElement));
93 
94  return true;
95  }
96 
97  template<typename Stream>
99  static serialize(Stream& stream, out<Q> value) noexcept
100  {
101  quantized_quaternion quantized_quat;
102 
103  BS_ASSERT(stream.serialize_bits(quantized_quat.m, 2));
104  BS_ASSERT(stream.serialize_bits(quantized_quat.a, BitsPerElement));
105  BS_ASSERT(stream.serialize_bits(quantized_quat.b, BitsPerElement));
106  BS_ASSERT(stream.serialize_bits(quantized_quat.c, BitsPerElement));
107 
108  value = smallest_three<Q, BitsPerElement>::dequantize(quantized_quat);
109 
110  return true;
111  }
112  };
113 }
#define BS_ASSERT(x)
Definition: assert.h:15
Class for quantizing single-precision floats into a range and precision.
Definition: bounded_range.h:33
Class for quantizing single-precision floats into half-precision.
Definition: half_precision.h:34
static float dequantize(uint16_t value) noexcept
Definition: half_precision.h:82
static uint16_t quantize(float value) noexcept
Definition: half_precision.h:36
Definition: parameter.h:64
Class for quantizing a user-specified quaternion into fewer bits using the smallest-three algorithm.
Definition: smallest_three.h:56
static T dequantize(const quantized_quaternion &data) noexcept
Definition: smallest_three.h:132
static quantized_quaternion quantize(const T &quaternion) noexcept
Definition: smallest_three.h:62
std::enable_if_t< T::writing, R > is_writing_t
Definition: meta.h:25
std::enable_if_t< T::reading, R > is_reading_t
Definition: meta.h:28
Definition: bounded_range.h:28
std::conditional_t<(sizeof(T)<=16 &&std::is_trivially_copy_constructible_v< T >), std::add_const_t< T >, std::add_lvalue_reference_t< std::add_const_t< T > >> in
Passes by const or const reference depending on size.
Definition: parameter.h:94
A quantized representation of a quaternion.
Definition: smallest_three.h:34
uint32_t c
Definition: smallest_three.h:38
uint32_t m
Definition: smallest_three.h:35
uint32_t b
Definition: smallest_three.h:37
uint32_t a
Definition: smallest_three.h:36
static utility::is_reading_t< Stream > serialize(Stream &stream, in< bounded_range > range, out< float > value) noexcept
Definition: quantization_traits.h:65
static utility::is_writing_t< Stream > serialize(Stream &stream, in< bounded_range > range, in< float > value) noexcept
Definition: quantization_traits.h:54
static utility::is_reading_t< Stream > serialize(Stream &stream, out< float > value) noexcept
Definition: quantization_traits.h:34
static utility::is_writing_t< Stream > serialize(Stream &stream, in< float > value) noexcept
Definition: quantization_traits.h:23
static utility::is_reading_t< Stream > serialize(Stream &stream, out< Q > value) noexcept
Definition: quantization_traits.h:99
static utility::is_writing_t< Stream > serialize(Stream &stream, in< Q > value) noexcept
Definition: quantization_traits.h:85
A class for specializing trait serialization functions.
Definition: serialize_traits.h:11