19template<
unsigned int I,
unsigned int F,
typename T>
22 static_assert(std::is_integral_v<T>,
"FixedPointQTraits: T must be integral");
23 using UT = std::make_unsigned_t<T>;
25 static constexpr unsigned int bits = I + F;
26 static_assert(bits <=
sizeof(UT) * 8,
"FixedPointQTraits: too many bits for type UT");
32 static_assert(bits <= 24,
"Floating point precision may be insufficient for more than 24 bits");
34 static constexpr UT bitMask = bits <
sizeof(UT) * 8
35 ? (UT{ 1 } << bits) - 1
41 static constexpr UT
qMin = std::is_signed_v<T>
42 ? -(UT{ 1 } << (bits - 1))
45 static constexpr UT
qMax = std::is_signed_v<T>
46 ? (UT{ 1 } << (bits - 1)) - 1
51 if constexpr (std::is_unsigned_v<T>)
52 return static_cast<float>(q) /
static_cast<float>(UT{ 1 } << F);
61 unsigned int remaining_bits =
sizeof(UT) * 8 - (I + F);
62 T t =
static_cast<T
>(
static_cast<UT
>(q) << remaining_bits) >> remaining_bits;
63 return static_cast<float>(t) /
static_cast<float>(UT{ 1 } << F);
69 static_assert(
min <
max,
"FixedPointQTraits: Minimum must be less than maximum");
74 v = std::clamp(v,
min,
max);
81 return static_cast<UT
>(
static_cast<T
>(std::round(v * (1 << F)))) & bitMask;
87template<
unsigned int Bits>
90 static_assert(Bits <= 32,
91 "Unsupported number of bits for quantized type");
93 if constexpr (Bits <= 8)
95 else if constexpr (Bits <= 16)
97 else if constexpr (Bits <= 32)
103template<
unsigned int I,
unsigned int F>
106template<
unsigned int I,
unsigned int F>
Top-level libcamera namespace.
Definition backtrace.h:17
Quantized storage and Quantizer representations.
Traits type implementing fixed-point quantization conversions.
Definition fixedpoint.h:20
static constexpr float max
Maximum representable floating-point value corresponding to qMax.
Definition fixedpoint.h:67
static constexpr float min
Minimum representable floating-point value corresponding to qMin.
Definition fixedpoint.h:66
static constexpr UT qMin
Minimum representable quantized integer value.
Definition fixedpoint.h:41
static QuantizedType fromFloat(float v)
Convert a floating-point value to a fixed-point integer.
Definition fixedpoint.h:72
static constexpr float toFloat(QuantizedType q)
Convert a fixed-point integer to a floating-point value.
Definition fixedpoint.h:49
static constexpr UT qMax
Maximum representable quantized integer value.
Definition fixedpoint.h:45
UT QuantizedType
The integral storage type used for the fixed-point representation.
Definition fixedpoint.h:39
Wrapper that stores a value in both quantized and floating-point form.
Definition quantized.h:21