From patchwork Fri Feb 13 16:57:44 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 26147 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 463FEC32F0 for ; Fri, 13 Feb 2026 16:58:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 0088A621FD; Fri, 13 Feb 2026 17:58:12 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="SZaovovD"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 07D99621DA for ; Fri, 13 Feb 2026 17:58:05 +0100 (CET) Received: from ping.linuxembedded.co.uk (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4055F2691; Fri, 13 Feb 2026 17:57:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1771001836; bh=CcqHKbSFJTlNPu2pqPDbYDz1BxBjbUkyGH4h9eONKUY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SZaovovDAkaQY5qGImls6ulStwQqFLTVBf5mf3RSh0FFIGUuWnsE67YUHUbLgJbLG 2ta4mvjv6OFP37XU69fW9LBEtVmRdr0jD+5sDcsvkWOGYP49VdwC5kcDPq8lEV/cfu iaO1ZE5kJ0TvmKLNUCwXiZ2MaI+057cUnOGVNvXE= From: Kieran Bingham Date: Fri, 13 Feb 2026 16:57:44 +0000 Subject: [PATCH v7 05/15] test: libipa: Provide FixedPoint Quantized tests MIME-Version: 1.0 Message-Id: <20260213-kbingham-quantizers-v7-5-1626b9aaabf1@ideasonboard.com> References: <20260213-kbingham-quantizers-v7-0-1626b9aaabf1@ideasonboard.com> In-Reply-To: <20260213-kbingham-quantizers-v7-0-1626b9aaabf1@ideasonboard.com> To: libcamera-devel@lists.libcamera.org Cc: Kieran Bingham X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1771001883; l=9816; i=kieran.bingham@ideasonboard.com; s=20260207; h=from:subject:message-id; bh=CcqHKbSFJTlNPu2pqPDbYDz1BxBjbUkyGH4h9eONKUY=; b=ffc4LwZDOoylavLT+pRkBxysrcTYwnUDCi5rDC59sKxVmb3civxp/fC6/UvAhYDXZSIS7jvrr d0NB03xYYtiAPdy2132fq9GXJ5BgcI0Rk/jrDVqFVnTIQX5XjSOdScG X-Developer-Key: i=kieran.bingham@ideasonboard.com; a=ed25519; pk=FVXKN7YuwHc6UtbRUeTMAmranfsQomA+vnilfglWdaY= X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Provide tests to validate the conditions of FixedPoint types used within libcamera explicitly. Signed-off-by: Kieran Bingham --- 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 v6: - Add notes on test introduction - Expand Q12.4 to include clamp tests to validate int16_t range usage - Add storage selection assertion checks - Move clamp checks to their types to expand - Add {U,}Q<4,20> to check up to 24 bit precision v7: - Provide signed register extension tests --- test/ipa/libipa/fixedpoint.cpp | 209 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 205 insertions(+), 4 deletions(-) diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp index 4b017e86a74f..2907426b7a69 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 #include #include #include -#include "../src/ipa/libipa/fixedpoint.h" +#include #include "test.h" @@ -95,14 +97,213 @@ protected: return TestPass; } - int run() + template + 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 << 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 << ")" + << 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 << ")" + << std::endl; + return 1; + } + + return 0; + } + + template + int signExtendCheck() + { + Q minValue(Q::TraitsType::min); + using T = typename Q::QuantizedType; + T minQuantized = minValue.quantized(); + + /* Take the value and expand it into a larger register */ + RegisterType reg = static_cast(minValue.quantized()); + + std::cout << " Checking " << minValue << " == " << utils::hex(reg) << std::endl; + + /* + * Ensure that the minimum negative value is not sign + * extended when casting up to a larger register storage + * type. + */ + if (reg != minQuantized) { + std::cout << " ** Sign extension check failed for min value. Expected " + << utils::hex(minQuantized) << ", got " + << utils::hex(reg) << std::endl; + return 1; + } + + /* And we should be consistent when we convert back to Q */ + Q q2(static_cast(reg)); + if (!(q2 == minValue)) { + std::cout << " ** Sign extension check failed for min value. Expected " + << minValue << ", got " << q2 << std::endl; + return 1; + } + + return 0; + } + + template + 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) + << " -- Max: " << Q(Q::TraitsType::max) + << " Step:" << Q(T(1)).value() + << std::endl; + } + + int testFixedPointQuantizers() + { + unsigned int fails = 0; + + /* + * These aim to specifically test all the corner cases of the + * quantization and de-quantization process. Including clamping + * to min/max, zero points and making sure that steps are + * correct. + * + * In particular test signed and unsigned types and a mix of the + * highest bit width of a storage type and smaller widths that + * require bit masking and sign extension. + * + * Note we must hard code these. Any calculation of expected + * values risks replicating bugs in the implementation. + * + * As the underlying types are integer and float the limit of + * precision is around 24 bits so we do not test wider types. + */ + + /* clang-format off */ + + /* Q1.7(-1 .. 0.992188) Min: [0x80:-1] -- Max: [0x7f:0.992188] Step:0.0078125*/ + introduce>("Q1.7"); + fails += quantizedCheck>(-2.000f, 0b1'0000000, -1.0f); /* Clamped to Min */ + fails += quantizedCheck>(-1.000f, 0b1'0000000, -1.0f); /* Min */ + fails += quantizedCheck>(-0.992f, 0b1'0000001, -0.992188f); /* Min + 1 step */ + fails += quantizedCheck>(-0.006f, 0b1'1111111, -0.0078125f); /* -1 step */ + fails += quantizedCheck>( 0.000f, 0b0'0000000, 0.0f); /* Zero */ + fails += quantizedCheck>( 0.008f, 0b0'0000001, 0.0078125f); /* +1 step */ + fails += quantizedCheck>( 0.992f, 0b0'1111111, 0.992188f); /* Max */ + fails += quantizedCheck>( 2.000f, 0b0'1111111, 0.992188f); /* Clamped to Max */ + + /* UQ1.7(0 .. 1.99219) Min: [0x00:0] -- Max: [0xff:1.99219] Step:0.0078125 */ + introduce>("UQ1.7"); + fails += quantizedCheck>(-1.0f, 0b0'0000000, 0.0f); /* Clamped to Min */ + fails += quantizedCheck>( 0.0f, 0b0'0000000, 0.0f); /* Min / Zero */ + fails += quantizedCheck>( 1.0f, 0b1'0000000, 1.0f); /* Mid */ + fails += quantizedCheck>( 1.992f, 0b1'1111111, 1.99219f); /* Max */ + fails += quantizedCheck>( 2.000f, 0b1'1111111, 1.99219f); /* Clamped to Max */ + + /* UQ4.8(0 .. 15.9961) Min: [0x0000:0] -- Max: [0x0fff:15.9961] Step:0.00390625 */ + introduce>("UQ4.8"); + fails += quantizedCheck>( 0.0f, 0b0000'00000000, 0.00f); + fails += quantizedCheck>(16.0f, 0b1111'11111111, 15.9961f); + + /* Q5.4(-16 .. 15.9375) Min: [0x0100:-16] -- Max: [0x00ff:15.9375] Step:0.0625 */ + introduce>("Q5.4"); + fails += quantizedCheck>(-16.00f, 0b10000'0000, -16.00f); + fails += quantizedCheck>( 15.94f, 0b01111'1111, 15.9375f); + + /* UQ5.8(0 .. 31.9961) Min: [0x0000:0] -- Max: [0x1fff:31.9961] Step:0.00390625 */ + introduce>("UQ5.8"); + fails += quantizedCheck>( 0.00f, 0b00000'00000000, 0.00f); + fails += quantizedCheck>(32.00f, 0b11111'11111111, 31.9961f); + + /* Q12.4(-2048 .. 2047.94) Min: [0x8000:-2048] -- Max: [0x7fff:2047.94] Step:0.0625 */ + introduce>("Q12.4"); + fails += quantizedCheck>( 0.0f, 0b000000000000'0000, 0.0f); + fails += quantizedCheck>( 7.5f, 0b000000000111'1000, 7.5f); + + /* UQ12.4(0 .. 4095.94) Min: [0x0000:0] -- Max: [0xffff:4095.94] Step:0.0625 */ + introduce>("UQ12.4"); + fails += quantizedCheck>(0.0f, 0b000000000000'0000, 0.0f); + fails += quantizedCheck>(7.5f, 0b000000000111'1000, 7.5f); + + /* Q4.20 */ + introduce>("Q4.20"); + fails += quantizedCheck>( -9.0f, 0b1000'00000000000000000000, -8.0f); + fails += quantizedCheck>( -8.0f, 0b1000'00000000000000000000, -8.0f); + fails += quantizedCheck>( 8.0f, 0b0111'11111111111111111111, 8.0f); + fails += quantizedCheck>( 9.0f, 0b0111'11111111111111111111, 8.0f); + + /* UQ4.20 */ + introduce>("UQ4.20"); + fails += quantizedCheck>(-1.0f, 0b0000'00000000000000000000, 0.0f); + fails += quantizedCheck>( 0.0f, 0b0000'00000000000000000000, 0.0f); + fails += quantizedCheck>(16.0f, 0b1111'11111111111111111111, 16.0f); + fails += quantizedCheck>(20.0f, 0b1111'11111111111111111111, 16.0f); + + /* Storage selection tests */ + static_assert(std::is_same_v::TraitsType::QuantizedType, uint8_t>); + static_assert(std::is_same_v::TraitsType::QuantizedType, uint8_t>); + static_assert(std::is_same_v::TraitsType::QuantizedType, uint16_t>); + static_assert(std::is_same_v::TraitsType::QuantizedType, uint16_t>); + static_assert(std::is_same_v::TraitsType::QuantizedType, uint32_t>); + static_assert(std::is_same_v::TraitsType::QuantizedType, uint32_t>); + + /* + * Test and validate that sign extension can not modify a + * quantized value when stored or cast to a larger register. + */ + std::cout << std::endl; + std::cout << "Testing sign extension of quantized values when cast to larger registers" << std::endl; + fails += signExtendCheck, uint8_t>(); + fails += signExtendCheck, uint8_t>(); + fails += signExtendCheck, uint16_t>(); + fails += signExtendCheck, uint16_t>(); + fails += signExtendCheck, uint32_t>(); + + /* 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)