[{"id":38233,"web_url":"https://patchwork.libcamera.org/comment/38233/","msgid":"<177149201420.607498.18166300659961304883@neptunite.rasen.tech>","date":"2026-02-19T09:06:54","subject":"Re: [PATCH v7 15/15] ipa: libipa: fixedpoint: Move float conversion\n\tinline","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"Quoting Kieran Bingham (2026-02-14 01:57:54)\n> With all users of the floatingToFixedPoint and fixedToFloatingPoint\n> calls converted to use the FixedPoint Quantized types, remove the calls\n> and documentation and move the implementation inline in the\n> FixedPointTraits implementation.\n> \n> Reviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\n> \n> ---\n> v5:\n>  - Reflow text when moved\n> \n> v6:\n> - Use UT{1} << F instead of 1 << F\n> ---\n>  src/ipa/libipa/fixedpoint.cpp | 22 -------------\n>  src/ipa/libipa/fixedpoint.h   | 75 +++++++++++++------------------------------\n>  2 files changed, 22 insertions(+), 75 deletions(-)\n> \n> diff --git a/src/ipa/libipa/fixedpoint.cpp b/src/ipa/libipa/fixedpoint.cpp\n> index caa9ce0fc1ec..7eee3614df6a 100644\n> --- a/src/ipa/libipa/fixedpoint.cpp\n> +++ b/src/ipa/libipa/fixedpoint.cpp\n> @@ -15,28 +15,6 @@ namespace libcamera {\n>  \n>  namespace ipa {\n>  \n> -/**\n> - * \\fn R floatingToFixedPoint(T number)\n> - * \\brief Convert a floating point number to a fixed-point representation\n> - * \\tparam I Bit width of the integer part of the fixed-point\n> - * \\tparam F Bit width of the fractional part of the fixed-point\n> - * \\tparam R Return type of the fixed-point representation\n> - * \\tparam T Input type of the floating point representation\n> - * \\param number The floating point number to convert to fixed point\n> - * \\return The converted value\n> - */\n> -\n> -/**\n> - * \\fn R fixedToFloatingPoint(T number)\n> - * \\brief Convert a fixed-point number to a floating point representation\n> - * \\tparam I Bit width of the integer part of the fixed-point\n> - * \\tparam F Bit width of the fractional part of the fixed-point\n> - * \\tparam R Return type of the floating point representation\n> - * \\tparam T Input type of the fixed-point representation\n> - * \\param number The fixed point number to convert to floating point\n> - * \\return The converted value\n> - */\n> -\n>  /**\n>   * \\struct libcamera::ipa::FixedPointQTraits\n>   * \\brief Traits type implementing fixed-point quantisation conversions\n> diff --git a/src/ipa/libipa/fixedpoint.h b/src/ipa/libipa/fixedpoint.h\n> index b6b611df7fc3..a0da31209271 100644\n> --- a/src/ipa/libipa/fixedpoint.h\n> +++ b/src/ipa/libipa/fixedpoint.h\n> @@ -16,55 +16,6 @@ namespace libcamera {\n>  \n>  namespace ipa {\n>  \n> -#ifndef __DOXYGEN__\n> -template<unsigned int I, unsigned int F, typename R, typename T,\n> -        std::enable_if_t<std::is_integral_v<R> &&\n> -                         std::is_floating_point_v<T>> * = nullptr>\n> -#else\n> -template<unsigned int I, unsigned int F, typename R, typename T>\n> -#endif\n> -constexpr R floatingToFixedPoint(T number)\n> -{\n> -       static_assert(sizeof(int) >= sizeof(R));\n> -       static_assert(I + F <= sizeof(R) * 8);\n> -\n> -       /*\n> -        * The intermediate cast to int is needed on arm platforms to properly\n> -        * cast negative values. See\n> -        * https://embeddeduse.com/2013/08/25/casting-a-negative-float-to-an-unsigned-int/\n> -        */\n> -       R mask = (1 << (F + I)) - 1;\n> -       R frac = static_cast<R>(static_cast<int>(std::round(number * (1 << F)))) & mask;\n> -\n> -       return frac;\n> -}\n> -\n> -#ifndef __DOXYGEN__\n> -template<unsigned int I, unsigned int F, typename R, typename T,\n> -        std::enable_if_t<std::is_floating_point_v<R> &&\n> -                         std::is_integral_v<T>> * = nullptr>\n> -#else\n> -template<unsigned int I, unsigned int F, typename R, typename T>\n> -#endif\n> -constexpr R fixedToFloatingPoint(T number)\n> -{\n> -       static_assert(sizeof(int) >= sizeof(T));\n> -       static_assert(I + F <= sizeof(T) * 8);\n> -\n> -       if constexpr (std::is_unsigned_v<T>)\n> -               return static_cast<R>(number) / static_cast<R>(T{ 1 } << F);\n> -\n> -       /*\n> -        * Recreate the upper bits in case of a negative number by shifting the sign\n> -        * bit from the fixed point to the first bit of the unsigned and then right shifting\n> -        * by the same amount which keeps the sign bit in place.\n> -        * This can be optimized by the compiler quite well.\n> -        */\n> -       int remaining_bits = sizeof(int) * 8 - (I + F);\n> -       int t = static_cast<int>(static_cast<unsigned>(number) << remaining_bits) >> remaining_bits;\n> -       return static_cast<R>(t) / static_cast<R>(1 << F);\n> -}\n> -\n>  template<unsigned int I, unsigned int F, typename T>\n>  struct FixedPointQTraits {\n>  private:\n> @@ -97,11 +48,23 @@ public:\n>  \n>         static constexpr float toFloat(QuantizedType q)\n>         {\n> -               return fixedToFloatingPoint<I, F, float, T>(q);\n> +               if constexpr (std::is_unsigned_v<T>)\n> +                       return static_cast<float>(q) / static_cast<float>(UT{ 1 } << F);\n> +\n> +               /*\n> +                * Recreate the upper bits in case of a negative number by\n> +                * shifting the sign bit from the fixed point to the first bit\n> +                * of the unsigned and then right shifting by the same amount\n> +                * which keeps the sign bit in place. This can be optimized by\n> +                * the compiler quite well.\n> +                */\n> +               unsigned int remaining_bits = sizeof(UT) * 8 - (I + F);\n> +               T t = static_cast<T>(static_cast<UT>(q) << remaining_bits) >> remaining_bits;\n> +               return static_cast<float>(t) / static_cast<float>(UT{ 1 } << F);\n>         }\n>  \n> -       static constexpr float min = fixedToFloatingPoint<I, F, float>(static_cast<T>(qMin));\n> -       static constexpr float max = fixedToFloatingPoint<I, F, float>(static_cast<T>(qMax));\n> +       static constexpr float min = toFloat(qMin);\n> +       static constexpr float max = toFloat(qMax);\n>  \n>         static_assert(min < max, \"FixedPointQTraits: Minimum must be less than maximum\");\n>  \n> @@ -109,7 +72,13 @@ public:\n>         static QuantizedType fromFloat(float v)\n>         {\n>                 v = std::clamp(v, min, max);\n> -               return floatingToFixedPoint<I, F, T, float>(v);\n> +\n> +               /*\n> +                * The intermediate cast to int is needed on arm platforms to\n> +                * properly cast negative values. See\n> +                * https://embeddeduse.com/2013/08/25/casting-a-negative-float-to-an-unsigned-int/\n> +                */\n> +               return static_cast<UT>(static_cast<T>(std::round(v * (1 << F)))) & bitMask;\n>         }\n>  };\n>  \n> \n> -- \n> 2.52.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id F1B1BC0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 19 Feb 2026 09:07:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AD52362244;\n\tThu, 19 Feb 2026 10:07:01 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 18F026222F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 19 Feb 2026 10:07:00 +0100 (CET)","from neptunite.rasen.tech (unknown\n\t[IPv6:2404:7a81:160:2100:3150:3f17:6415:4c60])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id C7D51673;\n\tThu, 19 Feb 2026 10:06:06 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"cnlLm4t+\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1771491967;\n\tbh=dZ+f2ESbKIOEU0WpCwq5cDWDmNR91oMIHeTTQZCjlyk=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=cnlLm4t+7fm+BcQK2DQ3xMTrFvrfxlhHbfCSoSnCW1wx5IhuTABH6FwOUv9vAcdhp\n\toX0yOo+Jtvc13/wyY4i2nGYINhRTlnIdDT5bgWVVtPV97OvbVTriDua5Yhf8sUIYrY\n\tnI+4d0G8APNErH7N7euNv1mtBP3DaBrWUoRhHyYg=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20260213-kbingham-quantizers-v7-15-1626b9aaabf1@ideasonboard.com>","References":"<20260213-kbingham-quantizers-v7-0-1626b9aaabf1@ideasonboard.com>\n\t<20260213-kbingham-quantizers-v7-15-1626b9aaabf1@ideasonboard.com>","Subject":"Re: [PATCH v7 15/15] ipa: libipa: fixedpoint: Move float conversion\n\tinline","From":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"Kieran Bingham <kieran.bingham@ideasonboard.com>,\n\tIsaac Scott <isaac.scott@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Thu, 19 Feb 2026 18:06:54 +0900","Message-ID":"<177149201420.607498.18166300659961304883@neptunite.rasen.tech>","User-Agent":"alot/0.0.0","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]