[{"id":37907,"web_url":"https://patchwork.libcamera.org/comment/37907/","msgid":"<176917468659.302817.2648058744437994151@localhost>","date":"2026-01-23T13:24:46","subject":"Re: [PATCH v6 05/16] test: libipa: Provide FixedPoint Quantized\n\ttests","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Kieran,\n\nThank you for the patch.\n\nQuoting Kieran Bingham (2026-01-21 18:37:24)\n> Provide tests to validate the conditions of FixedPoint types used\n> within libcamera explicitly.\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> v3:\n> - Rename quantized_type to QuantizedType\n> \n> v5:\n> \n> - Use string_view for introduction of type\n> - move min/max check to static assert\n> - Squash down all tests into a single implementation and remove extra\n>   type proliferation.\n> - Use Q/UQ types directly.\n> - use std::cout consistently\n> \n> v6:\n> - Add notes on test introduction\n> - Expand Q12.4 to include clamp tests to validate int16_t range usage\n> - Add storage selection assertion checks\n> - Move clamp checks to their types to expand\n> - Add {U,}Q<4,20> to check up to 24 bit precision\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  test/ipa/libipa/fixedpoint.cpp | 162 ++++++++++++++++++++++++++++++++-\n>  1 file changed, 158 insertions(+), 4 deletions(-)\n> \n> diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp\n> index 4b017e86a74f..500cd308be98 100644\n> --- a/test/ipa/libipa/fixedpoint.cpp\n> +++ b/test/ipa/libipa/fixedpoint.cpp\n> @@ -5,12 +5,14 @@\n>   * Fixed / Floating point utility tests\n>   */\n>  \n> +#include \"../src/ipa/libipa/fixedpoint.h\"\n> +\n>  #include <cmath>\n>  #include <iostream>\n>  #include <map>\n>  #include <stdint.h>\n>  \n> -#include \"../src/ipa/libipa/fixedpoint.h\"\n> +#include <libcamera/base/utils.h>\n>  \n>  #include \"test.h\"\n>  \n> @@ -95,14 +97,166 @@ protected:\n>                 return TestPass;\n>         }\n>  \n> -       int run()\n> +       template<typename Q>\n> +       int quantizedCheck(float input, typename Q::QuantizedType expected, float value)\n>         {\n> -               /* fixed point conversion test */\n> -               if (testFixedPoint() != TestPass)\n> +               Q q(input);\n> +               using T = typename Q::QuantizedType;\n> +\n> +               std::cout << \"  Checking \" << input << \" == \" << q << std::endl;\n> +\n> +               T quantized = q.quantized();\n> +               if (quantized != expected) {\n> +                       std::cout << \"    ** Q Expected \" << input\n> +                                 << \" to quantize to \" << utils::hex(expected)\n> +                                 << \", got \" << utils::hex(quantized)\n> +                                 << \" - (\" << q << \")\"\n> +                                 << std::endl;\n> +                       return 1;\n> +               }\n> +\n> +               if ((std::abs(q.value() - value)) > 0.0001f) {\n> +                       std::cout << \"    ** V Expected \" << input\n> +                                 << \" to quantize to \" << value\n> +                                 << \", got \" << q.value()\n> +                                 << \" - (\" << q << \")\"\n> +                                 << std::endl;\n> +                       return 1;\n> +               }\n> +\n> +               return 0;\n> +       }\n> +\n> +       template<typename Q>\n> +       void introduce(std::string_view type)\n> +       {\n> +               using T = typename Q::QuantizedType;\n> +\n> +               std::cout << std::endl;\n> +\n> +               std::cout << type\n> +                         << \"(\" << Q::TraitsType::min << \" .. \" << Q::TraitsType::max << \") \"\n> +                         << \" Min: \" << Q(Q::TraitsType::min)\n> +                         << \" -- Max: \" << Q(Q::TraitsType::max)\n> +                         << \" Step:\" << Q(T(1)).value()\n> +                         << std::endl;\n> +       }\n> +\n> +       int testFixedPointQuantizers()\n> +       {\n> +               unsigned int fails = 0;\n> +\n> +               /*\n> +                * These aim to specifically test all the corner cases of the\n> +                * quantization and de-quantization process. Including clamping\n> +                * to min/max, zero points and making sure that steps are\n> +                * correct.\n> +                *\n> +                * In particular test signed and unsigned types and a mix of the\n> +                * highest bit width of a storage type and smaller widths that\n> +                * require bit masking and sign extension.\n> +                *\n> +                * Note we must hard code these. Any calculation of expected\n> +                * values risks replicating bugs in the implementation.\n> +                *\n> +                * As the underlying types are integer and float the limit of\n> +                * precision is around 24 bits so we do not test wider types.\n> +                */\n> +\n> +               /* clang-format off */\n> +\n> +               /* Q1.7(-1 .. 0.992188)  Min: [0x80:-1] -- Max: [0x7f:0.992188] Step:0.0078125*/\n> +               introduce<Q<1, 7>>(\"Q1.7\");\n> +               fails += quantizedCheck<Q<1, 7>>(-2.000f, 0b1'0000000, -1.0f);          /* Clamped to Min */\n> +               fails += quantizedCheck<Q<1, 7>>(-1.000f, 0b1'0000000, -1.0f);          /* Min */\n> +               fails += quantizedCheck<Q<1, 7>>(-0.992f, 0b1'0000001, -0.992188f);     /* Min + 1 step */\n> +               fails += quantizedCheck<Q<1, 7>>(-0.006f, 0b1'1111111, -0.0078125f);    /* -1 step */\n> +               fails += quantizedCheck<Q<1, 7>>( 0.000f, 0b0'0000000,  0.0f);          /* Zero */\n> +               fails += quantizedCheck<Q<1, 7>>( 0.008f, 0b0'0000001,  0.0078125f);    /* +1 step */\n> +               fails += quantizedCheck<Q<1, 7>>( 0.992f, 0b0'1111111,  0.992188f);     /* Max */\n> +               fails += quantizedCheck<Q<1, 7>>( 2.000f, 0b0'1111111,  0.992188f);     /* Clamped to Max */\n\nOh I didn't know you could use ' as separator.\n\n> +\n> +               /* UQ1.7(0 .. 1.99219)  Min: [0x00:0] -- Max: [0xff:1.99219] Step:0.0078125 */\n> +               introduce<UQ<1, 7>>(\"UQ1.7\");\n> +               fails += quantizedCheck<UQ<1, 7>>(-1.0f,   0b0'0000000, 0.0f);          /* Clamped to Min */\n> +               fails += quantizedCheck<UQ<1, 7>>( 0.0f,   0b0'0000000, 0.0f);          /* Min / Zero */\n> +               fails += quantizedCheck<UQ<1, 7>>( 1.0f,   0b1'0000000, 1.0f);          /* Mid */\n> +               fails += quantizedCheck<UQ<1, 7>>( 1.992f, 0b1'1111111, 1.99219f);      /* Max */\n> +               fails += quantizedCheck<UQ<1, 7>>( 2.000f, 0b1'1111111, 1.99219f);      /* Clamped to Max */\n> +\n> +               /* UQ4.8(0 .. 15.9961)  Min: [0x0000:0] -- Max: [0x0fff:15.9961] Step:0.00390625 */\n> +               introduce<UQ<4, 8>>(\"UQ4.8\");\n> +               fails += quantizedCheck<UQ<4, 8>>( 0.0f, 0b0000'00000000,  0.00f);\n> +               fails += quantizedCheck<UQ<4, 8>>(16.0f, 0b1111'11111111, 15.9961f);\n> +\n> +               /* Q5.4(-16 .. 15.9375)  Min: [0x0100:-16] -- Max: [0x00ff:15.9375] Step:0.0625 */\n> +               introduce<Q<5, 4>>(\"Q5.4\");\n> +               fails += quantizedCheck<Q<5, 4>>(-16.00f, 0b10000'0000, -16.00f);\n> +               fails += quantizedCheck<Q<5, 4>>( 15.94f, 0b01111'1111,  15.9375f);\n> +\n> +               /* UQ5.8(0 .. 31.9961)  Min: [0x0000:0] -- Max: [0x1fff:31.9961] Step:0.00390625 */\n> +               introduce<UQ<5, 8>>(\"UQ5.8\");\n> +               fails += quantizedCheck<UQ<5, 8>>( 0.00f, 0b00000'00000000,  0.00f);\n> +               fails += quantizedCheck<UQ<5, 8>>(32.00f, 0b11111'11111111, 31.9961f);\n> +\n> +               /* Q12.4(-2048 .. 2047.94)  Min: [0x8000:-2048] -- Max: [0x7fff:2047.94] Step:0.0625 */\n> +               introduce<Q<12, 4>>(\"Q12.4\");\n> +               fails += quantizedCheck<Q<12, 4>>(    0.0f, 0b000000000000'0000,     0.0f);\n> +               fails += quantizedCheck<Q<12, 4>>(    7.5f, 0b000000000111'1000,     7.5f);\n> +\n> +               /* UQ12.4(0 .. 4095.94)  Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */\n> +               introduce<UQ<12, 4>>(\"UQ12.4\");\n> +               fails += quantizedCheck<UQ<12, 4>>(0.0f, 0b000000000000'0000, 0.0f);\n> +               fails += quantizedCheck<UQ<12, 4>>(7.5f, 0b000000000111'1000, 7.5f);\n> +\n> +               /* Q4.20 */\n> +               introduce<Q<4, 20>>(\"Q4.20\");\n> +               fails += quantizedCheck<Q<4, 20>>( -9.0f, 0b1000'00000000000000000000, -8.0f);\n> +               fails += quantizedCheck<Q<4, 20>>( -8.0f, 0b1000'00000000000000000000, -8.0f);\n> +               fails += quantizedCheck<Q<4, 20>>(  8.0f, 0b0111'11111111111111111111,  8.0f);\n> +               fails += quantizedCheck<Q<4, 20>>(  9.0f, 0b0111'11111111111111111111,  8.0f);\n> +\n> +               /* UQ4.20 */\n> +               introduce<UQ<4, 20>>(\"UQ4.20\");\n> +               fails += quantizedCheck<UQ<4, 20>>(-1.0f, 0b0000'00000000000000000000,  0.0f);\n> +               fails += quantizedCheck<UQ<4, 20>>( 0.0f, 0b0000'00000000000000000000,  0.0f);\n> +               fails += quantizedCheck<UQ<4, 20>>(16.0f, 0b1111'11111111111111111111, 16.0f);\n> +               fails += quantizedCheck<UQ<4, 20>>(20.0f, 0b1111'11111111111111111111, 16.0f);\n> +\n> +               /* Storage selection tests */\n> +               static_assert(std::is_same_v<typename Q<4, 4>::TraitsType::QuantizedType, int8_t>);\n> +               static_assert(std::is_same_v<typename UQ<4, 4>::TraitsType::QuantizedType, uint8_t>);\n> +               static_assert(std::is_same_v<typename Q<8, 8>::TraitsType::QuantizedType, int16_t>);\n> +               static_assert(std::is_same_v<typename UQ<8, 8>::TraitsType::QuantizedType, uint16_t>);\n> +               static_assert(std::is_same_v<typename Q<20, 4>::TraitsType::QuantizedType, int32_t>);\n> +               static_assert(std::is_same_v<typename UQ<20, 4>::TraitsType::QuantizedType, uint32_t>);\n> +\n> +               /* clang-format on */\n> +\n> +               std::cout << std::endl;\n> +\n> +               if (fails > 0) {\n\nI like that you get all potential failures at once.\n\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\nCheers,\nStefan\n\n> +                       cout << \"Fixed point quantizer tests failed: \"\n> +                            << std::dec << fails << \" failures.\" << std::endl;\n>                         return TestFail;\n> +               }\n>  \n>                 return TestPass;\n>         }\n> +\n> +       int run()\n> +       {\n> +               unsigned int fails = 0;\n> +\n> +               /* fixed point conversion test */\n> +               if (testFixedPoint() != TestPass)\n> +                       fails++;\n> +\n> +               if (testFixedPointQuantizers() != TestPass)\n> +                       fails++;\n> +\n> +               return fails ? TestFail : TestPass;\n> +       }\n>  };\n>  \n>  TEST_REGISTER(FixedPointUtilsTest)\n> -- \n> 2.52.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id EDC7DBDCBF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 23 Jan 2026 13:24:52 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DD27461FC9;\n\tFri, 23 Jan 2026 14:24:51 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id CB595615B2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 23 Jan 2026 14:24:49 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:1d92:9f27:5dd1:dc89])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id 625859CE;\n\tFri, 23 Jan 2026 14:24:16 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"VeRiOYeZ\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1769174656;\n\tbh=Jg85heF5s66yxl9mxrYEOQ2WcwRr2eIiNzHQLvyJubo=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=VeRiOYeZpxxCmP8EjlQCDQPHwHN3bLX7nUfjSJyWRr/9qkdnIVFajc88bPToxVaEc\n\tbxK23EqoaIdCSvT+4ZA9evZOgsH4gQMCLKgAIYItMIZYqglsaTylVHEsis8+Dh9IvF\n\tAIBnZv82WcJKby6LKea/m+5tY19iFteObfFNy3sA=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20260121173737.376113-6-kieran.bingham@ideasonboard.com>","References":"<20260121173737.376113-1-kieran.bingham@ideasonboard.com>\n\t<20260121173737.376113-6-kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v6 05/16] test: libipa: Provide FixedPoint Quantized\n\ttests","From":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>,\n\tlibcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Fri, 23 Jan 2026 14:24:46 +0100","Message-ID":"<176917468659.302817.2648058744437994151@localhost>","User-Agent":"alot/0.12.dev8+g2c003385c862.d20250602","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]