From patchwork Fri Nov 14 00:54:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 25040 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 A7318C3331 for ; Fri, 14 Nov 2025 00:55:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 45C9A60B33; Fri, 14 Nov 2025 01:55:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="tRemIF5s"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 61AA860AAF for ; Fri, 14 Nov 2025 01:54:51 +0100 (CET) Received: from charm.hippo-penny.ts.net (cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D22031D1C; Fri, 14 Nov 2025 01:52:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1763081571; bh=DUlrlB6Trv3GWQH/Kyuo8tA9uj00B6YqWBYvIM2LaE4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tRemIF5sfHlhePWHVJr41J0kZYUZJCU3z+D5+udrkvfWet325JjmE/ZxwS4kDkDw5 wo6OjrFCd6oWqbsl8Hk6ojWcYWLsaNHv3AbnGmVVIKQuji5cDWKKor6BmWJ4Izt4p+ clBQmS+g5YANd4ProMUZGMlnf9fdC+jsacoV1NrY= From: Kieran Bingham To: libcamera devel Cc: Kieran Bingham Subject: [PATCH v4 10/21] ipa: libipa: fixedpoint: Provide a ScaledFixedPoint type Date: Fri, 14 Nov 2025 00:54:14 +0000 Message-ID: <20251114005428.90024-11-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251114005428.90024-1-kieran.bingham@ideasonboard.com> References: <20251114005428.90024-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 FixedPoint capabilities with a ScaledFixedPointQTraits which takes an existing Quantized type and scales the conversion by the given Scale template parameter. This can be useful to represent fixed point values which represent a linear range with a different scale to the underlying float or integral type. Signed-off-by: Kieran Bingham --- v3: - Rename quantized_type to QuantizedType v4: - Fix copydoc for ScaledFixedPointQTraits::toFloat src/ipa/libipa/fixedpoint.cpp | 59 +++++++++++++++++++++++++++++++++++ src/ipa/libipa/fixedpoint.h | 24 ++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/ipa/libipa/fixedpoint.cpp b/src/ipa/libipa/fixedpoint.cpp index 03053668ed79..97cdd9b3f7aa 100644 --- a/src/ipa/libipa/fixedpoint.cpp +++ b/src/ipa/libipa/fixedpoint.cpp @@ -185,6 +185,65 @@ namespace ipa { * Represents values in the range [0.0, 4095.9375] with a resolution of 1/16. */ +/** + * \struct ScaledFixedPointQTraits + * \brief Wrap a FixedPointQTraits with a linear scaling factor + * + * This trait extends an existing fixed-point quantisation policy + * by applying an additional multiplicative scale between the + * floating-point and quantised domains. + * + * \tparam Q The base fixed-point traits type + * \tparam Scale The scale factor applied to the floating-point domain + */ + +/** + * \typedef ScaledFixedPointQTraits::QuantizedType + * \copydoc FixedPointQTraits::QuantizedType + */ + +/** + * \var ScaledFixedPointQTraits::scale + * \brief The constant scaling factor applied to the floating-point domain + * + * Floating-point inputs are divided by this factor before quantisation, + * and multiplied by it after dequantisation. + */ + +/** + * \var ScaledFixedPointQTraits::qmin + * \copydoc FixedPointQTraits::qmin + */ + +/** + * \var ScaledFixedPointQTraits::qmax + * \copydoc FixedPointQTraits::qmax + */ + +/** + * \var ScaledFixedPointQTraits::min + * \copydoc FixedPointQTraits::min + */ + +/** + * \var ScaledFixedPointQTraits::max + * \copydoc FixedPointQTraits::max + */ + +/** + * \fn ScaledFixedPointQTraits::fromFloat(float v) + * \copydoc FixedPointQTraits::fromFloat(float v) + * + * The input value \a v is divided by the scaling factor before conversion. + */ + +/** + * \fn ScaledFixedPointQTraits::toFloat(QuantizedType q) + * \copydoc FixedPointQTraits::toFloat() + * + * The output value is multiplied by the scaling factor after conversion. + */ + } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/fixedpoint.h b/src/ipa/libipa/fixedpoint.h index 05dd97e64f40..1e984350111c 100644 --- a/src/ipa/libipa/fixedpoint.h +++ b/src/ipa/libipa/fixedpoint.h @@ -113,6 +113,30 @@ using UQ5_8 = Quantized>; using Q12_4 = Quantized>; using UQ12_4 = Quantized>; +template +struct ScaledFixedPointQTraits { + using QuantizedType = typename Q::QuantizedType; + + static constexpr float scale = static_cast(Scale); + + /* Re-expose base limits, adjusted by the scaling factor */ + static constexpr QuantizedType qmin = Q::qmin; + static constexpr QuantizedType qmax = Q::qmax; + static constexpr float min = Q::min * scale; + static constexpr float max = Q::max * scale; + + static QuantizedType fromFloat(float v) + { + v = std::clamp(v, min, max); + return Q::fromFloat(v / scale); + } + + static float toFloat(QuantizedType q) + { + return Q::toFloat(q) * scale; + } +}; + } /* namespace ipa */ } /* namespace libcamera */