{"id":19814,"url":"https://patchwork.libcamera.org/api/1.1/patches/19814/?format=json","web_url":"https://patchwork.libcamera.org/patch/19814/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240327085700.273232-1-paul.elder@ideasonboard.com>","date":"2024-03-27T08:57:00","name":"libcamera: utils: Add a helper to convert floating-point to fixed-point","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"c24ef4581369cc450094a732db84bad1f6f12a03","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/1.1/people/17/?format=json","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19814/mbox/","series":[{"id":4242,"url":"https://patchwork.libcamera.org/api/1.1/series/4242/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4242","date":"2024-03-27T08:57:00","name":"libcamera: utils: Add a helper to convert floating-point to fixed-point","version":1,"mbox":"https://patchwork.libcamera.org/series/4242/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19814/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19814/checks/","tags":{},"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 0C3D0C0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 27 Mar 2024 08:57:19 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 452AA63036;\n\tWed, 27 Mar 2024 09:57:18 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D13262CA2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 27 Mar 2024 09:57:16 +0100 (CET)","from pyrite.hamster-moth.ts.net (h175-177-049-156.catv02.itscom.jp\n\t[175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B032A675;\n\tWed, 27 Mar 2024 09:56:42 +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=\"BRSEUomg\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1711529803;\n\tbh=LAHY1CPfuhZAMeNiTektTwHLd9B5gscGvQyYyLTyw8I=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=BRSEUomgT1ljPx+PqKeTA3DgnWWJI7nocg68QWuR/8y/jZl6lofiNQaiPTegyRaz6\n\tlnmdd6Tw28hyQHvUg26yEvlKVAIoNld/jC14TtSvDCx2X1YJmYvVbPpBfiwDdYfmbB\n\t/kRn7YDQdlBx5vbvBdkRfQ1V2sMVQ3km5eLA2pQU=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>","Subject":"[PATCH] libcamera: utils: Add a helper to convert floating-point to\n\tfixed-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","Content-Transfer-Encoding":"8bit","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>"},"content":"Add a helper function to convert floating-point numbers to fixed-point\nnumbers.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\nI originally needed this for rkisp1 crosstalk but then I realized it's\nmore efficient to just have the tuning tool output fixed-point and have\nthe IPA simply copy those values instead of calculating them on-the-fly.\nStill, I made this helper so if people think it's useful...\n---\n include/libcamera/base/utils.h | 27 +++++++++++++++++++++++++++\n src/libcamera/base/utils.cpp   | 10 ++++++++++\n 2 files changed, 37 insertions(+)","diff":"diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex 37d9af60..ab421cc7 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -369,6 +369,33 @@ decltype(auto) abs_diff(const T &a, const T &b)\n \n double strtod(const char *__restrict nptr, char **__restrict endptr);\n \n+#ifndef __DOXYGEN__\n+template<unsigned int I, unsigned int F, typename R, typename T,\n+\t std::enable_if_t<std::is_integral_v<R> &&\n+\t\t\t  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+\tstatic_assert(I + F <= sizeof(R) * 8);\n+\n+\tR integer = static_cast<R>(number);\n+\tT fractional = number - integer;\n+\n+\tR mask = (1 << I) - 1;\n+\tR whole = (integer >= 0 ? integer : ~integer + 1) & mask;\n+\n+\tR frac = 0;\n+\tfor (unsigned int i = 0; i < F; i++) {\n+\t\tfractional *= 2;\n+\t\tfrac <<= 1;\n+\t\tfrac |= (static_cast<R>(fractional) & 0x1);\n+\t}\n+\n+\treturn (whole << F) | frac;\n+}\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\nindex 3b73b442..38bee317 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -521,6 +521,16 @@ double strtod(const char *__restrict nptr, char **__restrict endptr)\n #endif\n }\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+ * \\return The converted value\n+ */\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\n","prefixes":[]}