From patchwork Wed Mar 27 08:57:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 19814 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 0C3D0C0DA4 for ; Wed, 27 Mar 2024 08:57:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 452AA63036; Wed, 27 Mar 2024 09:57:18 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="BRSEUomg"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D13262CA2 for ; Wed, 27 Mar 2024 09:57:16 +0100 (CET) Received: from pyrite.hamster-moth.ts.net (h175-177-049-156.catv02.itscom.jp [175.177.49.156]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B032A675; Wed, 27 Mar 2024 09:56:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1711529803; bh=LAHY1CPfuhZAMeNiTektTwHLd9B5gscGvQyYyLTyw8I=; h=From:To:Cc:Subject:Date:From; b=BRSEUomgT1ljPx+PqKeTA3DgnWWJI7nocg68QWuR/8y/jZl6lofiNQaiPTegyRaz6 lnmdd6Tw28hyQHvUg26yEvlKVAIoNld/jC14TtSvDCx2X1YJmYvVbPpBfiwDdYfmbB /kRn7YDQdlBx5vbvBdkRfQ1V2sMVQ3km5eLA2pQU= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder Subject: [PATCH] libcamera: utils: Add a helper to convert floating-point to fixed-point Date: Wed, 27 Mar 2024 17:57:00 +0900 Message-Id: <20240327085700.273232-1-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 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" Add a helper function to convert floating-point numbers to fixed-point numbers. Signed-off-by: Paul Elder --- I originally needed this for rkisp1 crosstalk but then I realized it's more efficient to just have the tuning tool output fixed-point and have the IPA simply copy those values instead of calculating them on-the-fly. Still, I made this helper so if people think it's useful... --- include/libcamera/base/utils.h | 27 +++++++++++++++++++++++++++ src/libcamera/base/utils.cpp | 10 ++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 37d9af60..ab421cc7 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -369,6 +369,33 @@ decltype(auto) abs_diff(const T &a, const T &b) double strtod(const char *__restrict nptr, char **__restrict endptr); +#ifndef __DOXYGEN__ +template && + std::is_floating_point_v> * = nullptr> +#else +template +#endif +constexpr R floatingToFixedPoint(T number) +{ + static_assert(I + F <= sizeof(R) * 8); + + R integer = static_cast(number); + T fractional = number - integer; + + R mask = (1 << I) - 1; + R whole = (integer >= 0 ? integer : ~integer + 1) & mask; + + R frac = 0; + for (unsigned int i = 0; i < F; i++) { + fractional *= 2; + frac <<= 1; + frac |= (static_cast(fractional) & 0x1); + } + + return (whole << F) | frac; +} + } /* namespace utils */ #ifndef __DOXYGEN__ diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 3b73b442..38bee317 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -521,6 +521,16 @@ double strtod(const char *__restrict nptr, char **__restrict endptr) #endif } +/** + * \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 + * \return The converted value + */ + } /* namespace utils */ #ifndef __DOXYGEN__