From patchwork Fri Sep 18 12:09:30 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: 28340 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 07DCBC3240 for ; Fri, 18 Sep 2026 12:10:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B582B68737; Fri, 18 Sep 2026 14:09:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="IEZP6YbF"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B2B796870B for ; Fri, 18 Sep 2026 14:09:53 +0200 (CEST) Received: from pb-laptop.local (185.221.142.0.nat.pool.zt.hu [185.221.142.0]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D40FB4C39 for ; Fri, 18 Sep 2026 14:08:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1789733290; bh=m1gfdpyqB+yYfAhzdhuhdVOCRZjoduQNsQWDar55s30=; h=From:To:Subject:Date:In-Reply-To:References:From; b=IEZP6YbFPTnoWNVW105t0lFgjFYgPIduFx5TxmVEbdPis2lRUTzaTvxKNjtVbVA1L Wq7CRe2eS8c7gXuxsQrfZG1mnVZxqNQQGvqnY7ILWr225D7hVDQu3WUO0D5F1UeEVv lhuM2BIu00bzBFHj58MbpvxizVRyYEMcmAkgR3t4= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v3 02/21] ipa: libipa: quantized: Make floating point type customizable Date: Fri, 18 Sep 2026 14:09:30 +0200 Message-ID: <20260918120949.191668-3-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260918120949.191668-1-barnabas.pocze@ideasonboard.com> References: <20260918120949.191668-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" Retrieve the floating point type from `Traits` and use that instead of hard-coding `float`. Signed-off-by: Barnabás Pőcze --- 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);