| Message ID | 20260114173918.1744023-6-kieran.bingham@ideasonboard.com |
|---|---|
| State | Superseded |
| Headers | show |
| Series |
|
| Related | show |
2026. 01. 14. 18:39 keltezéssel, Kieran Bingham írta: > Provide tests to validate the conditions of FixedPoint types used > within libcamera explicitly. > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > --- > v3: > - Rename quantized_type to QuantizedType > > v5: > > - Use string_view for introduction of type > - move min/max check to static assert > - Squash down all tests into a single implementation and remove extra > type proliferation. > - Use Q/UQ types directly. > - use std::cout consistently > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > --- > test/ipa/libipa/fixedpoint.cpp | 126 +++++++++++++++++++++++++++++++-- > 1 file changed, 122 insertions(+), 4 deletions(-) > > diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp > index 4b017e86a74f..9fa7fa688c24 100644 > --- a/test/ipa/libipa/fixedpoint.cpp > +++ b/test/ipa/libipa/fixedpoint.cpp > @@ -5,12 +5,14 @@ > * Fixed / Floating point utility tests > */ > > +#include "../src/ipa/libipa/fixedpoint.h" > + > #include <cmath> > #include <iostream> > #include <map> > #include <stdint.h> > > -#include "../src/ipa/libipa/fixedpoint.h" > +#include <libcamera/base/utils.h> > > #include "test.h" > > @@ -95,14 +97,130 @@ protected: > return TestPass; > } > > - int run() > + template<typename Q> > + int quantizedCheck(float input, typename Q::QuantizedType expected, float value) > { > - /* fixed point conversion test */ > - if (testFixedPoint() != TestPass) > + Q q(input); > + using T = typename Q::QuantizedType; > + > + std::cout << " Checking " << input << " == " << q.toString() << std::endl; `toString()` can be omitted here and later. > + > + T quantized = q.quantized(); > + if (quantized != expected) { > + std::cout << " ** Q Expected " << input > + << " to quantize to " << utils::hex(expected) > + << ", got " << utils::hex(quantized) > + << " - (" << q.toString() << ")" > + << std::endl; > + return 1; > + } > + > + if ((std::abs(q.value() - value)) > 0.0001f) { > + std::cout << " ** V Expected " << input > + << " to quantize to " << value > + << ", got " << q.value() > + << " - (" << q.toString() << ")" > + << std::endl; > + return 1; > + } > + > + return 0; > + } > [...] > + int testFixedPointQuantizers() > + { > + unsigned int fails = 0; > + > + /* clang-format off */ > + > + [...] > + > + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ > + introduce<UQ<12, 4>>("UQ12.4"); > + fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); > + fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); > + > + /* Validate that exceeding limits clamps to type range */ > + std::cout << std::endl << "Range validation:" << std::endl; > + fails += quantizedCheck<Q<1, 7>>(-100.0f, 0b1'0000000, -1.0f); > + fails += quantizedCheck<Q<1, 7>>(+100.0f, 0b0'1111111, 0.992188f); > + fails += quantizedCheck<UQ<1, 7>>(-100.0f, 0b0'0000000, 0.0f); > + fails += quantizedCheck<UQ<1, 7>>(+100.0f, 0b1'1111111, 1.99219f); Would it be possible to add some tests that use 32 and 64 bit underlying types? > [...]
Quoting Barnabás Pőcze (2026-01-15 16:28:27) > 2026. 01. 14. 18:39 keltezéssel, Kieran Bingham írta: > > Provide tests to validate the conditions of FixedPoint types used > > within libcamera explicitly. > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > > --- > > v3: > > - Rename quantized_type to QuantizedType > > > > v5: > > > > - Use string_view for introduction of type > > - move min/max check to static assert > > - Squash down all tests into a single implementation and remove extra > > type proliferation. > > - Use Q/UQ types directly. > > - use std::cout consistently > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > --- > > test/ipa/libipa/fixedpoint.cpp | 126 +++++++++++++++++++++++++++++++-- > > 1 file changed, 122 insertions(+), 4 deletions(-) > > > > diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp > > index 4b017e86a74f..9fa7fa688c24 100644 > > --- a/test/ipa/libipa/fixedpoint.cpp > > +++ b/test/ipa/libipa/fixedpoint.cpp > > @@ -5,12 +5,14 @@ > > * Fixed / Floating point utility tests > > */ > > > > +#include "../src/ipa/libipa/fixedpoint.h" > > + > > #include <cmath> > > #include <iostream> > > #include <map> > > #include <stdint.h> > > > > -#include "../src/ipa/libipa/fixedpoint.h" > > +#include <libcamera/base/utils.h> > > > > #include "test.h" > > > > @@ -95,14 +97,130 @@ protected: > > return TestPass; > > } > > > > - int run() > > + template<typename Q> > > + int quantizedCheck(float input, typename Q::QuantizedType expected, float value) > > { > > - /* fixed point conversion test */ > > - if (testFixedPoint() != TestPass) > > + Q q(input); > > + using T = typename Q::QuantizedType; > > + > > + std::cout << " Checking " << input << " == " << q.toString() << std::endl; > > `toString()` can be omitted here and later. > > > > + > > + T quantized = q.quantized(); > > + if (quantized != expected) { > > + std::cout << " ** Q Expected " << input > > + << " to quantize to " << utils::hex(expected) > > + << ", got " << utils::hex(quantized) > > + << " - (" << q.toString() << ")" > > + << std::endl; > > + return 1; > > + } > > + > > + if ((std::abs(q.value() - value)) > 0.0001f) { > > + std::cout << " ** V Expected " << input > > + << " to quantize to " << value > > + << ", got " << q.value() > > + << " - (" << q.toString() << ")" > > + << std::endl; > > + return 1; > > + } > > + > > + return 0; > > + } > > [...] > > + int testFixedPointQuantizers() > > + { > > + unsigned int fails = 0; > > + > > + /* clang-format off */ > > + > > + [...] > > + > > + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ > > + introduce<UQ<12, 4>>("UQ12.4"); > > + fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); > > + fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); > > + > > + /* Validate that exceeding limits clamps to type range */ > > + std::cout << std::endl << "Range validation:" << std::endl; > > + fails += quantizedCheck<Q<1, 7>>(-100.0f, 0b1'0000000, -1.0f); > > + fails += quantizedCheck<Q<1, 7>>(+100.0f, 0b0'1111111, 0.992188f); > > + fails += quantizedCheck<UQ<1, 7>>(-100.0f, 0b0'0000000, 0.0f); > > + fails += quantizedCheck<UQ<1, 7>>(+100.0f, 0b1'1111111, 1.99219f); > > Would it be possible to add some tests that use 32 and 64 bit underlying types? Yes, and that's found some more bugs - also related to the undefined behviour with << shifts so thanks for the prompt! I'm adding: /* Storage selection tests */ static_assert(std::is_same_v<typename Q<1, 7>::TraitsType::QuantizedType, int8_t>); static_assert(std::is_same_v<typename UQ<1, 7>::TraitsType::QuantizedType, uint8_t>); static_assert(std::is_same_v<typename Q<8, 8>::TraitsType::QuantizedType, int16_t>); static_assert(std::is_same_v<typename UQ<8, 8>::TraitsType::QuantizedType, uint16_t>); static_assert(std::is_same_v<typename Q<16, 16>::TraitsType::QuantizedType, int32_t>); static_assert(std::is_same_v<typename UQ<16, 16>::TraitsType::QuantizedType, uint32_t>); And that's picked out that I have undefined behaviour hitting the qMin/qMax types. Fixing for next iteration. I'll also put in some {U,}Q<30, 2> {U,}Q<60, 4> value and range tests. > > > > [...] >
Quoting Kieran Bingham (2026-01-21 13:18:17) > Quoting Barnabás Pőcze (2026-01-15 16:28:27) > > 2026. 01. 14. 18:39 keltezéssel, Kieran Bingham írta: > > > Provide tests to validate the conditions of FixedPoint types used > > > within libcamera explicitly. > > > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > > > > --- > > > v3: > > > - Rename quantized_type to QuantizedType > > > > > > v5: > > > > > > - Use string_view for introduction of type > > > - move min/max check to static assert > > > - Squash down all tests into a single implementation and remove extra > > > type proliferation. > > > - Use Q/UQ types directly. > > > - use std::cout consistently > > > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > --- > > > test/ipa/libipa/fixedpoint.cpp | 126 +++++++++++++++++++++++++++++++-- > > > 1 file changed, 122 insertions(+), 4 deletions(-) > > > > > > diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp > > > index 4b017e86a74f..9fa7fa688c24 100644 > > > --- a/test/ipa/libipa/fixedpoint.cpp > > > +++ b/test/ipa/libipa/fixedpoint.cpp > > > @@ -5,12 +5,14 @@ > > > * Fixed / Floating point utility tests > > > */ > > > > > > +#include "../src/ipa/libipa/fixedpoint.h" > > > + > > > #include <cmath> > > > #include <iostream> > > > #include <map> > > > #include <stdint.h> > > > > > > -#include "../src/ipa/libipa/fixedpoint.h" > > > +#include <libcamera/base/utils.h> > > > > > > #include "test.h" > > > > > > @@ -95,14 +97,130 @@ protected: > > > return TestPass; > > > } > > > > > > - int run() > > > + template<typename Q> > > > + int quantizedCheck(float input, typename Q::QuantizedType expected, float value) > > > { > > > - /* fixed point conversion test */ > > > - if (testFixedPoint() != TestPass) > > > + Q q(input); > > > + using T = typename Q::QuantizedType; > > > + > > > + std::cout << " Checking " << input << " == " << q.toString() << std::endl; > > > > `toString()` can be omitted here and later. > > > > > > > + > > > + T quantized = q.quantized(); > > > + if (quantized != expected) { > > > + std::cout << " ** Q Expected " << input > > > + << " to quantize to " << utils::hex(expected) > > > + << ", got " << utils::hex(quantized) > > > + << " - (" << q.toString() << ")" > > > + << std::endl; > > > + return 1; > > > + } > > > + > > > + if ((std::abs(q.value() - value)) > 0.0001f) { > > > + std::cout << " ** V Expected " << input > > > + << " to quantize to " << value > > > + << ", got " << q.value() > > > + << " - (" << q.toString() << ")" > > > + << std::endl; > > > + return 1; > > > + } > > > + > > > + return 0; > > > + } > > > [...] > > > + int testFixedPointQuantizers() > > > + { > > > + unsigned int fails = 0; > > > + > > > + /* clang-format off */ > > > + > > > + [...] > > > + > > > + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ > > > + introduce<UQ<12, 4>>("UQ12.4"); > > > + fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); > > > + fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); > > > + > > > + /* Validate that exceeding limits clamps to type range */ > > > + std::cout << std::endl << "Range validation:" << std::endl; > > > + fails += quantizedCheck<Q<1, 7>>(-100.0f, 0b1'0000000, -1.0f); > > > + fails += quantizedCheck<Q<1, 7>>(+100.0f, 0b0'1111111, 0.992188f); > > > + fails += quantizedCheck<UQ<1, 7>>(-100.0f, 0b0'0000000, 0.0f); > > > + fails += quantizedCheck<UQ<1, 7>>(+100.0f, 0b1'1111111, 1.99219f); > > > > Would it be possible to add some tests that use 32 and 64 bit underlying types? Hrm - it looks like we have built in requirements/assumptions on a maximum type of 32 bits too for the toFloat implementation: static constexpr float toFloat(QuantizedType q) { if constexpr (std::is_unsigned_v<T>) return static_cast<float>(q) / static_cast<float>(UT{1} << F); /* * Recreate the upper bits in case of a negative number by * shifting the sign bit from the fixed point to the first bit * of the unsigned and then right shifting by the same amount * which keeps the sign bit in place. This can be optimized by * the compiler quite well. */ static_assert(sizeof(int) >= sizeof(T)); int remaining_bits = sizeof(int) * 8 - (I + F); int t = static_cast<int>(static_cast<unsigned>(q) << remaining_bits) >> remaining_bits; return static_cast<float>(t) / static_cast<float>(UT{1} << F); } I think I'll remove 64 bit support for now. Someone could add that in later if required but I don't think we need 64 bit fixed point types right now (or even in the foreseable?) -- Kieran
On Wed, Jan 21, 2026 at 01:29:42PM +0000, Kieran Bingham wrote: > Quoting Kieran Bingham (2026-01-21 13:18:17) > > Quoting Barnabás Pőcze (2026-01-15 16:28:27) > > > 2026. 01. 14. 18:39 keltezéssel, Kieran Bingham írta: > > > > Provide tests to validate the conditions of FixedPoint types used > > > > within libcamera explicitly. > > > > > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > > > > > > --- > > > > v3: > > > > - Rename quantized_type to QuantizedType > > > > > > > > v5: > > > > > > > > - Use string_view for introduction of type > > > > - move min/max check to static assert > > > > - Squash down all tests into a single implementation and remove extra > > > > type proliferation. > > > > - Use Q/UQ types directly. > > > > - use std::cout consistently > > > > > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > > --- > > > > test/ipa/libipa/fixedpoint.cpp | 126 +++++++++++++++++++++++++++++++-- > > > > 1 file changed, 122 insertions(+), 4 deletions(-) > > > > > > > > diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp > > > > index 4b017e86a74f..9fa7fa688c24 100644 > > > > --- a/test/ipa/libipa/fixedpoint.cpp > > > > +++ b/test/ipa/libipa/fixedpoint.cpp > > > > @@ -5,12 +5,14 @@ > > > > * Fixed / Floating point utility tests > > > > */ > > > > > > > > +#include "../src/ipa/libipa/fixedpoint.h" > > > > + > > > > #include <cmath> > > > > #include <iostream> > > > > #include <map> > > > > #include <stdint.h> > > > > > > > > -#include "../src/ipa/libipa/fixedpoint.h" > > > > +#include <libcamera/base/utils.h> > > > > > > > > #include "test.h" > > > > > > > > @@ -95,14 +97,130 @@ protected: > > > > return TestPass; > > > > } > > > > > > > > - int run() > > > > + template<typename Q> > > > > + int quantizedCheck(float input, typename Q::QuantizedType expected, float value) > > > > { > > > > - /* fixed point conversion test */ > > > > - if (testFixedPoint() != TestPass) > > > > + Q q(input); > > > > + using T = typename Q::QuantizedType; > > > > + > > > > + std::cout << " Checking " << input << " == " << q.toString() << std::endl; > > > > > > `toString()` can be omitted here and later. > > > > > > > + > > > > + T quantized = q.quantized(); > > > > + if (quantized != expected) { > > > > + std::cout << " ** Q Expected " << input > > > > + << " to quantize to " << utils::hex(expected) > > > > + << ", got " << utils::hex(quantized) > > > > + << " - (" << q.toString() << ")" > > > > + << std::endl; > > > > + return 1; > > > > + } > > > > + > > > > + if ((std::abs(q.value() - value)) > 0.0001f) { > > > > + std::cout << " ** V Expected " << input > > > > + << " to quantize to " << value > > > > + << ", got " << q.value() > > > > + << " - (" << q.toString() << ")" > > > > + << std::endl; > > > > + return 1; > > > > + } > > > > + > > > > + return 0; > > > > + } > > > > [...] > > > > + int testFixedPointQuantizers() > > > > + { > > > > + unsigned int fails = 0; > > > > + > > > > + /* clang-format off */ > > > > + > > > > + [...] > > > > + > > > > + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ > > > > + introduce<UQ<12, 4>>("UQ12.4"); > > > > + fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); > > > > + fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); > > > > + > > > > + /* Validate that exceeding limits clamps to type range */ > > > > + std::cout << std::endl << "Range validation:" << std::endl; > > > > + fails += quantizedCheck<Q<1, 7>>(-100.0f, 0b1'0000000, -1.0f); > > > > + fails += quantizedCheck<Q<1, 7>>(+100.0f, 0b0'1111111, 0.992188f); > > > > + fails += quantizedCheck<UQ<1, 7>>(-100.0f, 0b0'0000000, 0.0f); > > > > + fails += quantizedCheck<UQ<1, 7>>(+100.0f, 0b1'1111111, 1.99219f); > > > > > > Would it be possible to add some tests that use 32 and 64 bit underlying types? > > Hrm - it looks like we have built in requirements/assumptions on a > maximum type of 32 bits too for the toFloat implementation: > > static constexpr float toFloat(QuantizedType q) > { > if constexpr (std::is_unsigned_v<T>) > return static_cast<float>(q) / static_cast<float>(UT{1} << F); > > /* > * Recreate the upper bits in case of a negative number by > * shifting the sign bit from the fixed point to the first bit > * of the unsigned and then right shifting by the same amount > * which keeps the sign bit in place. This can be optimized by > * the compiler quite well. > */ > static_assert(sizeof(int) >= sizeof(T)); > > int remaining_bits = sizeof(int) * 8 - (I + F); > int t = static_cast<int>(static_cast<unsigned>(q) << remaining_bits) >> remaining_bits; > return static_cast<float>(t) / static_cast<float>(UT{1} << F); > } > > I think I'll remove 64 bit support for now. Someone could add that in > later if required but I don't think we need 64 bit fixed point types > right now (or even in the foreseable?) 32 bits is likely enough, I don't foresee the need to write 64-bit fixed points values to registers any time soon. Make sure there's a compilation-time error if anyone tries to set I + F > 32.
Quoting Laurent Pinchart (2026-01-21 15:18:31) > On Wed, Jan 21, 2026 at 01:29:42PM +0000, Kieran Bingham wrote: > > Quoting Kieran Bingham (2026-01-21 13:18:17) > > > Quoting Barnabás Pőcze (2026-01-15 16:28:27) > > > > 2026. 01. 14. 18:39 keltezéssel, Kieran Bingham írta: > > > > > Provide tests to validate the conditions of FixedPoint types used > > > > > within libcamera explicitly. > > > > > > > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > > > > > > > > --- > > > > > v3: > > > > > - Rename quantized_type to QuantizedType > > > > > > > > > > v5: > > > > > > > > > > - Use string_view for introduction of type > > > > > - move min/max check to static assert > > > > > - Squash down all tests into a single implementation and remove extra > > > > > type proliferation. > > > > > - Use Q/UQ types directly. > > > > > - use std::cout consistently > > > > > > > > > > Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com> > > > > > --- > > > > > test/ipa/libipa/fixedpoint.cpp | 126 +++++++++++++++++++++++++++++++-- > > > > > 1 file changed, 122 insertions(+), 4 deletions(-) > > > > > > > > > > diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp > > > > > index 4b017e86a74f..9fa7fa688c24 100644 > > > > > --- a/test/ipa/libipa/fixedpoint.cpp > > > > > +++ b/test/ipa/libipa/fixedpoint.cpp > > > > > @@ -5,12 +5,14 @@ > > > > > * Fixed / Floating point utility tests > > > > > */ > > > > > > > > > > +#include "../src/ipa/libipa/fixedpoint.h" > > > > > + > > > > > #include <cmath> > > > > > #include <iostream> > > > > > #include <map> > > > > > #include <stdint.h> > > > > > > > > > > -#include "../src/ipa/libipa/fixedpoint.h" > > > > > +#include <libcamera/base/utils.h> > > > > > > > > > > #include "test.h" > > > > > > > > > > @@ -95,14 +97,130 @@ protected: > > > > > return TestPass; > > > > > } > > > > > > > > > > - int run() > > > > > + template<typename Q> > > > > > + int quantizedCheck(float input, typename Q::QuantizedType expected, float value) > > > > > { > > > > > - /* fixed point conversion test */ > > > > > - if (testFixedPoint() != TestPass) > > > > > + Q q(input); > > > > > + using T = typename Q::QuantizedType; > > > > > + > > > > > + std::cout << " Checking " << input << " == " << q.toString() << std::endl; > > > > > > > > `toString()` can be omitted here and later. > > > > > > > > > + > > > > > + T quantized = q.quantized(); > > > > > + if (quantized != expected) { > > > > > + std::cout << " ** Q Expected " << input > > > > > + << " to quantize to " << utils::hex(expected) > > > > > + << ", got " << utils::hex(quantized) > > > > > + << " - (" << q.toString() << ")" > > > > > + << std::endl; > > > > > + return 1; > > > > > + } > > > > > + > > > > > + if ((std::abs(q.value() - value)) > 0.0001f) { > > > > > + std::cout << " ** V Expected " << input > > > > > + << " to quantize to " << value > > > > > + << ", got " << q.value() > > > > > + << " - (" << q.toString() << ")" > > > > > + << std::endl; > > > > > + return 1; > > > > > + } > > > > > + > > > > > + return 0; > > > > > + } > > > > > [...] > > > > > + int testFixedPointQuantizers() > > > > > + { > > > > > + unsigned int fails = 0; > > > > > + > > > > > + /* clang-format off */ > > > > > + > > > > > + [...] > > > > > + > > > > > + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ > > > > > + introduce<UQ<12, 4>>("UQ12.4"); > > > > > + fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); > > > > > + fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); > > > > > + > > > > > + /* Validate that exceeding limits clamps to type range */ > > > > > + std::cout << std::endl << "Range validation:" << std::endl; > > > > > + fails += quantizedCheck<Q<1, 7>>(-100.0f, 0b1'0000000, -1.0f); > > > > > + fails += quantizedCheck<Q<1, 7>>(+100.0f, 0b0'1111111, 0.992188f); > > > > > + fails += quantizedCheck<UQ<1, 7>>(-100.0f, 0b0'0000000, 0.0f); > > > > > + fails += quantizedCheck<UQ<1, 7>>(+100.0f, 0b1'1111111, 1.99219f); > > > > > > > > Would it be possible to add some tests that use 32 and 64 bit underlying types? > > > > Hrm - it looks like we have built in requirements/assumptions on a > > maximum type of 32 bits too for the toFloat implementation: > > > > static constexpr float toFloat(QuantizedType q) > > { > > if constexpr (std::is_unsigned_v<T>) > > return static_cast<float>(q) / static_cast<float>(UT{1} << F); > > > > /* > > * Recreate the upper bits in case of a negative number by > > * shifting the sign bit from the fixed point to the first bit > > * of the unsigned and then right shifting by the same amount > > * which keeps the sign bit in place. This can be optimized by > > * the compiler quite well. > > */ > > static_assert(sizeof(int) >= sizeof(T)); > > > > int remaining_bits = sizeof(int) * 8 - (I + F); > > int t = static_cast<int>(static_cast<unsigned>(q) << remaining_bits) >> remaining_bits; > > return static_cast<float>(t) / static_cast<float>(UT{1} << F); > > } > > > > I think I'll remove 64 bit support for now. Someone could add that in > > later if required but I don't think we need 64 bit fixed point types > > right now (or even in the foreseable?) > > 32 bits is likely enough, I don't foresee the need to write 64-bit fixed > points values to registers any time soon. Make sure there's a > compilation-time error if anyone tries to set I + F > 32. Actually - it turns out I'll probably restrict to 24 bits while we use 'float'. More than that and we'll want to swap up for double types. > > -- > Regards, > > Laurent Pinchart
diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp index 4b017e86a74f..9fa7fa688c24 100644 --- a/test/ipa/libipa/fixedpoint.cpp +++ b/test/ipa/libipa/fixedpoint.cpp @@ -5,12 +5,14 @@ * Fixed / Floating point utility tests */ +#include "../src/ipa/libipa/fixedpoint.h" + #include <cmath> #include <iostream> #include <map> #include <stdint.h> -#include "../src/ipa/libipa/fixedpoint.h" +#include <libcamera/base/utils.h> #include "test.h" @@ -95,14 +97,130 @@ protected: return TestPass; } - int run() + template<typename Q> + int quantizedCheck(float input, typename Q::QuantizedType expected, float value) { - /* fixed point conversion test */ - if (testFixedPoint() != TestPass) + Q q(input); + using T = typename Q::QuantizedType; + + std::cout << " Checking " << input << " == " << q.toString() << std::endl; + + T quantized = q.quantized(); + if (quantized != expected) { + std::cout << " ** Q Expected " << input + << " to quantize to " << utils::hex(expected) + << ", got " << utils::hex(quantized) + << " - (" << q.toString() << ")" + << std::endl; + return 1; + } + + if ((std::abs(q.value() - value)) > 0.0001f) { + std::cout << " ** V Expected " << input + << " to quantize to " << value + << ", got " << q.value() + << " - (" << q.toString() << ")" + << std::endl; + return 1; + } + + return 0; + } + + template<typename Q> + void introduce(std::string_view type) + { + using T = typename Q::QuantizedType; + + std::cout << std::endl; + + std::cout << type + << "(" << Q::TraitsType::min << " .. " << Q::TraitsType::max << ") " + << " Min: " << Q(Q::TraitsType::min).toString() + << " -- Max: " << Q(Q::TraitsType::max).toString() + << " Step:" << Q(T(1)).value() + << std::endl; + } + + int testFixedPointQuantizers() + { + unsigned int fails = 0; + + /* clang-format off */ + + /* Q1.7(-1 .. 0.992188) Min: [0x80:-1] -- Max: [0x7f:0.992188] Step:0.0078125*/ + introduce<Q<1, 7>>("Q1.7"); + fails += quantizedCheck<Q<1, 7>>(-1.000f, 0b1'0000000, -1.0f); /* Min */ + fails += quantizedCheck<Q<1, 7>>(-0.992f, 0b1'0000001, -0.992188f); /* Min + 1 step */ + fails += quantizedCheck<Q<1, 7>>(-0.006f, 0b1'1111111, -0.0078125f); /* -1 step */ + fails += quantizedCheck<Q<1, 7>>( 0.000f, 0b0'0000000, 0.0f); /* Zero */ + fails += quantizedCheck<Q<1, 7>>( 0.008f, 0b0'0000001, 0.0078125f); /* +1 step */ + fails += quantizedCheck<Q<1, 7>>( 0.992f, 0b0'1111111, 0.992188f); /* Max */ + + /* UQ1.7(0 .. 1.99219) Min: [0x00:0] -- Max: [0xff:1.99219] Step:0.0078125 */ + introduce<UQ<1, 7>>("UQ1.7"); + fails += quantizedCheck<UQ<1, 7>>(0.0f, 0b0'0000000, 0.0f); /* Min / Zero */ + fails += quantizedCheck<UQ<1, 7>>(1.0f, 0b1'0000000, 1.0f); /* Mid */ + fails += quantizedCheck<UQ<1, 7>>(1.992f, 0b1'1111111, 1.99219f); /* Max */ + + /* UQ4.8(0 .. 15.9961) Min: [0x0000:0] -- Max: [0x0fff:15.9961] Step:0.00390625 */ + introduce<UQ<4, 8>>("UQ4.8"); + fails += quantizedCheck<UQ<4, 8>>( 0.0f, 0b0000'00000000, 0.00f); + fails += quantizedCheck<UQ<4, 8>>(16.0f, 0b1111'11111111, 15.9961f); + + /* Q5.4(-16 .. 15.9375) Min: [0x0100:-16] -- Max: [0x00ff:15.9375] Step:0.0625 */ + introduce<Q<5, 4>>("Q5.4"); + fails += quantizedCheck<Q<5, 4>>(-16.00f, 0b10000'0000, -16.00f); + fails += quantizedCheck<Q<5, 4>>( 15.94f, 0b01111'1111, 15.9375f); + + /* UQ5.8(0 .. 31.9961) Min: [0x0000:0] -- Max: [0x1fff:31.9961] Step:0.00390625 */ + introduce<UQ<5, 8>>("UQ5.8"); + fails += quantizedCheck<UQ<5, 8>>( 0.00f, 0b00000'00000000, 0.00f); + fails += quantizedCheck<UQ<5, 8>>(32.00f, 0b11111'11111111, 31.9961f); + + /* Q12.4(-2048 .. 2047.94) Min: [0x8000:-2048] -- Max: [0x7fff:2047.94] Step:0.0625 */ + introduce<Q<12, 4>>("Q12.4"); + fails += quantizedCheck<Q<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); + fails += quantizedCheck<Q<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); + + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ + introduce<UQ<12, 4>>("UQ12.4"); + fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f); + fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f); + + /* Validate that exceeding limits clamps to type range */ + std::cout << std::endl << "Range validation:" << std::endl; + fails += quantizedCheck<Q<1, 7>>(-100.0f, 0b1'0000000, -1.0f); + fails += quantizedCheck<Q<1, 7>>(+100.0f, 0b0'1111111, 0.992188f); + fails += quantizedCheck<UQ<1, 7>>(-100.0f, 0b0'0000000, 0.0f); + fails += quantizedCheck<UQ<1, 7>>(+100.0f, 0b1'1111111, 1.99219f); + + /* clang-format on */ + + std::cout << std::endl; + + if (fails > 0) { + cout << "Fixed point quantizer tests failed: " + << std::dec << fails << " failures." << std::endl; return TestFail; + } return TestPass; } + + int run() + { + unsigned int fails = 0; + + /* fixed point conversion test */ + if (testFixedPoint() != TestPass) + fails++; + + if (testFixedPointQuantizers() != TestPass) + fails++; + + return fails ? TestFail : TestPass; + } }; TEST_REGISTER(FixedPointUtilsTest)