From patchwork Wed Jan 14 17:39:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 25811 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 76B83C32EA for ; Wed, 14 Jan 2026 17:39:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 18B5A61FD7; Wed, 14 Jan 2026 18:39:42 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="CiwYs72H"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EFDD461FD9 for ; Wed, 14 Jan 2026 18:39:26 +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 104C82835; Wed, 14 Jan 2026 18:39:00 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1768412340; bh=p59VEdYtGBIOVU/88itgeMbzeNFIHgsYdld9mxiccq4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CiwYs72HJN3GEj0dtukideL2hykVVTUUH6mtTomyR6mvaQW68RLi11c+N3K3ysSWb tRz84sDgJlS7dVSLlEcbdc+8vpWFfgxEQuA0c4TCkQ16mxkX/3X5sHJIS59XHS4rdQ X0r0IIZDXrX//11T1VF0MUj5yG76cTqtdZ3DQbFU= From: Kieran Bingham To: libcamera devel Cc: Kieran Bingham , Isaac Scott , Laurent Pinchart Subject: [PATCH v5 15/16] ipa: libipa: fixedpoint: Move float conversion inline Date: Wed, 14 Jan 2026 17:39:15 +0000 Message-ID: <20260114173918.1744023-16-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" With all users of the floatingToFixedPoint and fixedToFloatingPoint calls converted to use the FixedPoint Quantized types, remove the calls and documentation and move the implementation inline in the FixedPointTraits implementation. Reviewed-by: Isaac Scott Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham --- v5: - Reflow text when moved Signed-off-by: Kieran Bingham --- src/ipa/libipa/fixedpoint.cpp | 22 ---------- src/ipa/libipa/fixedpoint.h | 77 +++++++++++------------------------ 2 files changed, 24 insertions(+), 75 deletions(-) diff --git a/src/ipa/libipa/fixedpoint.cpp b/src/ipa/libipa/fixedpoint.cpp index 43d76f745d8a..6a15f9b068c7 100644 --- a/src/ipa/libipa/fixedpoint.cpp +++ b/src/ipa/libipa/fixedpoint.cpp @@ -15,28 +15,6 @@ namespace libcamera { namespace ipa { -/** - * \fn R floatingToFixedPoint(T number) - * \brief Convert a floating point number to a fixed-point representation - * \tparam I Bit width of the integer part of the fixed-point - * \tparam F Bit width of the fractional part of the fixed-point - * \tparam R Return type of the fixed-point representation - * \tparam T Input type of the floating point representation - * \param number The floating point number to convert to fixed point - * \return The converted value - */ - -/** - * \fn R fixedToFloatingPoint(T number) - * \brief Convert a fixed-point number to a floating point representation - * \tparam I Bit width of the integer part of the fixed-point - * \tparam F Bit width of the fractional part of the fixed-point - * \tparam R Return type of the floating point representation - * \tparam T Input type of the fixed-point representation - * \param number The fixed point number to convert to floating point - * \return The converted value - */ - /** * \struct libcamera::ipa::FixedPointQTraits * \brief Traits type implementing fixed-point quantisation conversions diff --git a/src/ipa/libipa/fixedpoint.h b/src/ipa/libipa/fixedpoint.h index 4f6ee081604b..1d53b0b9fdde 100644 --- a/src/ipa/libipa/fixedpoint.h +++ b/src/ipa/libipa/fixedpoint.h @@ -16,55 +16,6 @@ namespace libcamera { namespace ipa { -#ifndef __DOXYGEN__ -template && - std::is_floating_point_v> * = nullptr> -#else -template -#endif -constexpr R floatingToFixedPoint(T number) -{ - static_assert(sizeof(int) >= sizeof(R)); - static_assert(I + F <= sizeof(R) * 8); - - /* - * The intermediate cast to int is needed on arm platforms to properly - * cast negative values. See - * https://embeddeduse.com/2013/08/25/casting-a-negative-float-to-an-unsigned-int/ - */ - R mask = (1 << (F + I)) - 1; - R frac = static_cast(static_cast(std::round(number * (1 << F)))) & mask; - - return frac; -} - -#ifndef __DOXYGEN__ -template && - std::is_integral_v> * = nullptr> -#else -template -#endif -constexpr R fixedToFloatingPoint(T number) -{ - static_assert(sizeof(int) >= sizeof(T)); - static_assert(I + F <= sizeof(T) * 8); - - if constexpr (std::is_unsigned_v) - return static_cast(number) / static_cast(1 << F); - - /* - * Recreate the upper bits in case of a negative number by shifting the sign - * bit from the fixed point to the first bit of the unsigned and then right shifting - * by the same amount which keeps the sign bit in place. - * This can be optimized by the compiler quite well. - */ - int remaining_bits = sizeof(int) * 8 - (I + F); - int t = static_cast(static_cast(number) << remaining_bits) >> remaining_bits; - return static_cast(t) / static_cast(1 << F); -} - template struct FixedPointQTraits { private: @@ -91,11 +42,25 @@ public: static constexpr float toFloat(QuantizedType q) { - return fixedToFloatingPoint(q); + if constexpr (std::is_unsigned_v) + return static_cast(q) / static_cast(1 << F); + + /* + * Recreate the upper bits in case of a negative number by + * shifting the sign bit from the fixed point to the first bit + * of the unsigned and then right shifting by the same amount + * which keeps the sign bit in place. This can be optimized by + * the compiler quite well. + */ + static_assert(sizeof(int) >= sizeof(T)); + + int remaining_bits = sizeof(int) * 8 - (I + F); + int t = static_cast(static_cast(q) << remaining_bits) >> remaining_bits; + return static_cast(t) / static_cast(1 << F); } - static constexpr float min = fixedToFloatingPoint(qMin); - static constexpr float max = fixedToFloatingPoint(qMax); + static constexpr float min = toFloat(qMin); + static constexpr float max = toFloat(qMax); static_assert(min < max, "FixedPointQTraits: Minimum must be less than maximum"); @@ -103,7 +68,13 @@ public: static QuantizedType fromFloat(float v) { v = std::clamp(v, min, max); - return floatingToFixedPoint(v); + + /* + * The intermediate cast to int is needed on arm platforms to + * properly cast negative values. See + * https://embeddeduse.com/2013/08/25/casting-a-negative-float-to-an-unsigned-int/ + */ + return static_cast(static_cast(std::round(v * (1 << F)))) & bitMask; } };