From patchwork Wed Jan 14 17:39:04 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 25800 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 C7739C3284 for ; Wed, 14 Jan 2026 17:39:34 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7CD6361FC5; Wed, 14 Jan 2026 18:39:28 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="t7YQaSDZ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8C59D61FBF for ; Wed, 14 Jan 2026 18:39:23 +0100 (CET) Received: from Monstersaurus.infra.iob (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A879D1F16; Wed, 14 Jan 2026 18:38:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1768412336; bh=6AWHoe1C/ACRuMNYY21QXcRal1P9jzdXPX6+MHKvNec=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t7YQaSDZpq8H3XJu5PoBtP8qwtT7TR8XeaJlueMo8wsPFVYcfpHeSiDIC6lPHO0V9 NXwllSlF/8W8bbUYsKOJF8p7U2tR0hGPXz1O7HGv0tISX0zrTjOC37CLiPNSp+pwBH Zh/Kfjk7LbdYnkgte8CQNU/4n6dkOSFULZymmoaw= From: Kieran Bingham To: libcamera devel Cc: Kieran Bingham , Isaac Scott Subject: [PATCH v5 04/16] ipa: libipa: Provide fixed point quantized traits Date: Wed, 14 Jan 2026 17:39:04 +0000 Message-ID: <20260114173918.1744023-5-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260114173918.1744023-1-kieran.bingham@ideasonboard.com> References: <20260114173918.1744023-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 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" Extend the new Quantized type infrastructure by providing a FixedPointQTraits template. This allows construction of fixed point types with a Quantized storage that allows easy reading of both the underlying quantized type value and a floating point representation of that same value. Reviewed-by: Isaac Scott Signed-off-by: Kieran Bingham --- v4: - Assert that the given type has enough bits for the usage - Use unsigned types for calculating qmin/qmax - Reorder toFloat/fromFloat and min/max for future inlining - Make toFloat and fromFloat constexpr v5: - Make UT, Bits and Bitmask private (and remove doxygen) - Remove constexpr from fromFloat which uses std::round (only constexpr in C++23) - static_assert that minqMin qmax->qMax Bits->bits BitMask->bitMask - Remove typedefs for Q1_7 etc Signed-off-by: Kieran Bingham --- src/ipa/libipa/fixedpoint.cpp | 89 +++++++++++++++++++++++++++++++++++ src/ipa/libipa/fixedpoint.h | 69 +++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/src/ipa/libipa/fixedpoint.cpp b/src/ipa/libipa/fixedpoint.cpp index 6b698fc5d680..43d76f745d8a 100644 --- a/src/ipa/libipa/fixedpoint.cpp +++ b/src/ipa/libipa/fixedpoint.cpp @@ -37,6 +37,95 @@ namespace ipa { * \return The converted value */ +/** + * \struct libcamera::ipa::FixedPointQTraits + * \brief Traits type implementing fixed-point quantisation conversions + * + * The FixedPointQTraits structure defines a policy for mapping floating-point + * values to and from fixed-point integer representations. It is parameterised + * by the number of integer bits \a I, fractional bits \a F, and the integral + * storage type \a T. The traits are used with Quantized to create a + * quantised type that stores both the fixed-point representation and the + * corresponding floating-point value. + * + * The trait exposes compile-time constants describing the bit layout, limits, + * and scaling factors used in the fixed-point representation. + * + * \tparam I Number of integer bits + * \tparam F Number of fractional bits + * \tparam T Integral type used to store the quantised value + */ + +/** + * \typedef FixedPointQTraits::QuantizedType + * \brief The integral storage type used for the fixed-point representation + */ + +/** + * \var FixedPointQTraits::qMin + * \brief Minimum representable quantised integer value + * + * This corresponds to the most negative value for signed formats or zero for + * unsigned formats. + */ + +/** + * \var FixedPointQTraits::qMax + * \brief Maximum representable quantised integer value + */ + +/** + * \var FixedPointQTraits::min + * \brief Minimum representable floating-point value corresponding to qMin + */ + +/** + * \var FixedPointQTraits::max + * \brief Maximum representable floating-point value corresponding to qMax + */ + +/** + * \fn FixedPointQTraits::fromFloat(float v) + * \brief Convert a floating-point value to a fixed-point integer + * \param[in] v The floating-point value to be converted + * \return The quantised fixed-point integer representation + * + * The conversion rounds the floating-point input \a v to the nearest integer + * according to the scaling factor defined by the number of fractional bits F. + */ + +/** + * \fn FixedPointQTraits::toFloat(QuantizedType q) + * \brief Convert a fixed-point integer to a floating-point value + * \param[in] q The fixed-point integer value to be converted + * \return The corresponding floating-point value + * + * The conversion sign-extends the integer value if required and divides by the + * scaling factor defined by the number of fractional bits F. + */ + +/** + * \typedef Q + * \brief Define a signed fixed-point quantised type with automatic storage width + * \tparam I The number of integer bits + * \tparam F The number of fractional bits + * + * This alias defines a signed fixed-point quantised type using the + * \ref FixedPointQTraits trait and a suitable signed integer storage type + * automatically selected based on the total number of bits \a (I + F). + */ + +/** + * \typedef UQ + * \brief Define an unsigned fixed-point quantised type with automatic storage width + * \tparam I The number of integer bits + * \tparam F The number of fractional bits + * + * This alias defines an unsigned fixed-point quantised type using the + * \ref FixedPointQTraits trait and a suitable unsigned integer storage type + * automatically selected based on the total number of bits \a (I + F). + */ + } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/fixedpoint.h b/src/ipa/libipa/fixedpoint.h index b4a7fa5e0ecd..4f6ee081604b 100644 --- a/src/ipa/libipa/fixedpoint.h +++ b/src/ipa/libipa/fixedpoint.h @@ -10,6 +10,8 @@ #include #include +#include "quantized.h" + namespace libcamera { namespace ipa { @@ -63,6 +65,73 @@ constexpr R fixedToFloatingPoint(T number) return static_cast(t) / static_cast(1 << F); } +template +struct FixedPointQTraits { +private: + static_assert(std::is_integral_v, "FixedPointQTraits: T must be integral"); + using UT = std::make_unsigned_t; + + static constexpr unsigned int bits = I + F; + static_assert(bits <= sizeof(T) * 8, "FixedPointQTraits: too many bits for type T"); + + static constexpr T bitMask = (bits < sizeof(T) * 8) + ? static_cast((UT{1} << bits) - 1) + : static_cast(~UT{0}); + +public: + using QuantizedType = T; + + static constexpr T qMin = std::is_signed_v + ? static_cast(-(UT{1} << (bits - 1))) + : static_cast(0); + + static constexpr T qMax = std::is_signed_v + ? static_cast((UT{1} << (bits - 1)) - 1) + : static_cast((UT{1} << bits) - 1); + + static constexpr float toFloat(QuantizedType q) + { + return fixedToFloatingPoint(q); + } + + static constexpr float min = fixedToFloatingPoint(qMin); + static constexpr float max = fixedToFloatingPoint(qMax); + + static_assert(min < max, "FixedPointQTraits: Minimum must be less than maximum"); + + /* Conversion functions required by Quantized */ + static QuantizedType fromFloat(float v) + { + v = std::clamp(v, min, max); + return floatingToFixedPoint(v); + } +}; + +namespace details { + +template +constexpr auto qtype() +{ + static_assert(Bits <= 64); + + if constexpr (Bits <= 8) + return int8_t(); + else if constexpr (Bits <= 16) + return int16_t(); + else if constexpr (Bits <= 32) + return int32_t(); + else if constexpr (Bits <= 64) + return int64_t(); +} + +} /* namespace details */ + +template +using Q = Quantized())>>; + +template +using UQ = Quantized())>>>; + } /* namespace ipa */ } /* namespace libcamera */