From patchwork Fri Nov 13 16:40:35 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 28889 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 5B537C3304 for ; Fri, 13 Nov 2026 16:41:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B60F66A827; Fri, 13 Nov 2026 17:41:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Nq/YZYSH"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8477A69107 for ; Fri, 13 Nov 2026 17:40:58 +0100 (CET) Received: from pb-laptop.local (185.221.140.49.nat.pool.zt.hu [185.221.140.49]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 7EDEC219; Fri, 13 Nov 2026 17:38:33 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1794587913; bh=7ipo5lgLqCxVElN5BZF9f2Q10EbmQFiYutelpf7H/tg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Nq/YZYSHNKPjh6EOOt3pH3FEYtH/YXv6BddilfHp8J+GjBaCtzB3rLKgfil94PYey lgg4Tb6yqhqWzY3YewHXHb/6KxuJN0CCx0LT3OucXI1YFL4RU7umpWjsZ765zbNgqu 2xvbjCGaVGUjJJvZsNxRyEtacRPHJWkIG+iwtVP0= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Kieran Bingham , Jacopo Mondi Subject: [PATCH v4 02/21] ipa: libipa: quantized: Make floating point type customizable Date: Fri, 13 Nov 2026 17:40:35 +0100 Message-ID: <20261113164054.267164-3-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261113164054.267164-1-barnabas.pocze@ideasonboard.com> References: <20261113164054.267164-1-barnabas.pocze@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" Adjust the `Quantized` type to retrieve the floating point from the `FloatingType` type alias in the `Traits` type, instead of hard-coding `float`. Additionally, adjust the various traits type, to provide the now required type alias. They are currently hard-coded to use `float`. Signed-off-by: Barnabás Pőcze Reviewed-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/ipa/libipa/fixedpoint.cpp | 7 ++++++- src/ipa/libipa/fixedpoint.h | 11 ++++++----- src/ipa/libipa/quantized.cpp | 10 +++++++++- src/ipa/libipa/quantized.h | 14 +++++++++----- test/ipa/libipa/quantized.cpp | 8 ++++++-- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/ipa/libipa/fixedpoint.cpp b/src/ipa/libipa/fixedpoint.cpp index b06ad8d267..8c0e9746c7 100644 --- a/src/ipa/libipa/fixedpoint.cpp +++ b/src/ipa/libipa/fixedpoint.cpp @@ -47,6 +47,11 @@ namespace ipa { * \brief The integral storage type used for the fixed-point representation */ +/** + * \typedef FixedPointQTraits::FloatingType + * \brief The type used for the floating-point representation + */ + /** * \var FixedPointQTraits::qMin * \brief Minimum representable quantized integer value @@ -71,7 +76,7 @@ namespace ipa { */ /** - * \fn FixedPointQTraits::fromFloat(float v) + * \fn FixedPointQTraits::fromFloat(FloatingType v) * \brief Convert a floating-point value to a fixed-point integer * \param[in] v The floating-point value to be converted * \return The quantized fixed-point integer representation diff --git a/src/ipa/libipa/fixedpoint.h b/src/ipa/libipa/fixedpoint.h index 537bc774a4..66863075c6 100644 --- a/src/ipa/libipa/fixedpoint.h +++ b/src/ipa/libipa/fixedpoint.h @@ -37,6 +37,7 @@ private: public: using QuantizedType = UT; + using FloatingType = float; static constexpr UT qMin = std::is_signed_v ? -(UT{ 1 } << (bits - 1)) @@ -49,7 +50,7 @@ public: static constexpr float toFloat(QuantizedType q) { if constexpr (std::is_unsigned_v) - return static_cast(q) / static_cast(UT{ 1 } << F); + return static_cast(q) / static_cast(UT{ 1 } << F); /* * Recreate the upper bits in case of a negative number by @@ -60,16 +61,16 @@ public: */ unsigned int remaining_bits = sizeof(UT) * 8 - (I + F); T t = static_cast(static_cast(q) << remaining_bits) >> remaining_bits; - return static_cast(t) / static_cast(UT{ 1 } << F); + return static_cast(t) / static_cast(UT{ 1 } << F); } - static constexpr float min = toFloat(qMin); - static constexpr float max = toFloat(qMax); + static constexpr FloatingType min = toFloat(qMin); + static constexpr FloatingType max = toFloat(qMax); static_assert(min < max, "FixedPointQTraits: Minimum must be less than maximum"); /* Conversion functions required by Quantized */ - static QuantizedType fromFloat(float v) + static QuantizedType fromFloat(FloatingType v) { v = std::clamp(v, min, max); diff --git a/src/ipa/libipa/quantized.cpp b/src/ipa/libipa/quantized.cpp index 06143a97ab..34cf6b1b97 100644 --- a/src/ipa/libipa/quantized.cpp +++ b/src/ipa/libipa/quantized.cpp @@ -58,7 +58,15 @@ namespace ipa { */ /** - * \fn Quantized::Quantized(float x) + * \typedef Quantized::FloatingType + * \brief The floating type used for the value representation + * + * This alias corresponds to \c TraitsType::FloatingType, as defined by + * the traits class. + */ + +/** + * \fn Quantized::Quantized(FloatingType x) * \brief Construct a Quantized value from a floating-point number * \param[in] x The floating-point value to be quantized * diff --git a/src/ipa/libipa/quantized.h b/src/ipa/libipa/quantized.h index 8d7b06a2df..5c41d0b5dd 100644 --- a/src/ipa/libipa/quantized.h +++ b/src/ipa/libipa/quantized.h @@ -21,13 +21,17 @@ template struct Quantized { using TraitsType = Traits; using QuantizedType = typename Traits::QuantizedType; - static_assert(std::is_arithmetic_v, - "Quantized: QuantizedType must be arithmetic"); + using FloatingType = typename Traits::FloatingType; + + static_assert(std::is_integral_v, + "Quantized: QuantizedType must be integral"); + static_assert(std::is_floating_point_v, + "Quantized: FloatingType must be floating"); constexpr Quantized() - : Quantized(0.0f) {} + : Quantized(FloatingType{}) {} - constexpr Quantized(float x) + constexpr Quantized(FloatingType x) : Quantized(Traits::fromFloat(x)) { } @@ -73,7 +77,7 @@ struct Quantized { private: QuantizedType quantized_; - float value_; + FloatingType value_; }; } /* namespace ipa */ diff --git a/test/ipa/libipa/quantized.cpp b/test/ipa/libipa/quantized.cpp index 18655dac52..4b1d869bf2 100644 --- a/test/ipa/libipa/quantized.cpp +++ b/test/ipa/libipa/quantized.cpp @@ -20,7 +20,9 @@ using namespace ipa; struct BrightnessHueTraits { using QuantizedType = uint8_t; - static QuantizedType fromFloat(float v) + using FloatingType = float; + + static QuantizedType fromFloat(FloatingType v) { int quantized = std::lround(v * 128.0f); return std::clamp(quantized, -128, 127); @@ -35,7 +37,9 @@ using BrightnessHueQuantizer = Quantized; struct ContrastSaturationTraits { using QuantizedType = uint8_t; - static QuantizedType fromFloat(float v) + using FloatingType = float; + + static QuantizedType fromFloat(FloatingType v) { int quantized = std::lround(v * 128.0f); return std::clamp(quantized, 0, 255);