Patch Detail
Show a patch.
GET /api/patches/19935/?format=api
{ "id": 19935, "url": "https://patchwork.libcamera.org/api/patches/19935/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19935/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<20240424074409.1275425-1-paul.elder@ideasonboard.com>", "date": "2024-04-24T07:44:09", "name": "[v2] libcamera: utils: Add a helper to convert floating-point to fixed-point", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "faa2cbfe6418274f71de00e3d6d1521f1fc40430", "submitter": { "id": 17, "url": "https://patchwork.libcamera.org/api/people/17/?format=api", "name": "Paul Elder", "email": "paul.elder@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19935/mbox/", "series": [ { "id": 4269, "url": "https://patchwork.libcamera.org/api/series/4269/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4269", "date": "2024-04-24T07:44:09", "name": "[v2] libcamera: utils: Add a helper to convert floating-point to fixed-point", "version": 2, "mbox": "https://patchwork.libcamera.org/series/4269/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19935/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19935/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 91916C3200\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 24 Apr 2024 07:44:36 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 6EA5C633ED;\n\tWed, 24 Apr 2024 09:44:35 +0200 (CEST)", "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E855E61B14\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 24 Apr 2024 09:44:33 +0200 (CEST)", "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 EC7F9B1;\n\tWed, 24 Apr 2024 09:43:40 +0200 (CEST)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"G9FTynnY\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1713944622;\n\tbh=r8eS9lDyLCVKJ7GtzSqXzFLJ+nXVadwK6EuA6mBSp6I=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=G9FTynnYjGew/3xymZHQWdHXWHIIKT+xsjhl2KFTMECsZNCyx6dy2JIKtyAOIcyrm\n\twRzLmzHhdNIM2oeDpNVEfjbxwqLzLMzlMWdVNwHS4KqMI/QRTnWGuN6wTLt1ax3oqQ\n\tXEzzSZkfjepmmxI1CAtdm93LJUs0III0zakaHHZc=", "From": "Paul Elder <paul.elder@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Paul Elder <paul.elder@ideasonboard.com>", "Subject": "[PATCH v2] libcamera: utils: Add a helper to convert floating-point\n\tto fixed-point", "Date": "Wed, 24 Apr 2024 16:44:09 +0900", "Message-Id": "<20240424074409.1275425-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 helper functions for converting between floating point and fixed\npoint numbers. Also add tests for them.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\nChanges in v2:\n- added the reverse conversion function (fixed -> floating)\n- added tests\n- make the conversion code cleaner\n---\n include/libcamera/base/utils.h | 37 +++++++++++++++++++++++++\n src/libcamera/base/utils.cpp | 20 ++++++++++++++\n test/utils.cpp | 49 ++++++++++++++++++++++++++++++++++\n 3 files changed, 106 insertions(+)", "diff": "diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex 37d9af60..da0767e3 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -9,6 +9,7 @@\n \n #include <algorithm>\n #include <chrono>\n+#include <cmath>\n #include <iterator>\n #include <memory>\n #include <ostream>\n@@ -369,6 +370,42 @@ 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 maskI = (1 << I) - 1;\n+\tR whole = (static_cast<R>(number) & maskI) << F;\n+\n+\tR maskF = (1 << F) - 1;\n+\tR frac = static_cast<R>(std::round(number * (1 << F))) & maskF;\n+\n+\treturn whole | frac;\n+}\n+\n+#ifndef __DOXYGEN__\n+template<unsigned int I, unsigned int F, typename R, typename T,\n+\t std::enable_if_t<std::is_floating_point_v<R> &&\n+\t\t\t 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+\tstatic_assert(I + F <= sizeof(T) * 8);\n+\n+\tint coeff = number >> (I + F - 1) ? -1 : 1;\n+\n+\treturn coeff * static_cast<R>(number) / static_cast<R>(1 << F);\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..ba36026d 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -521,6 +521,26 @@ 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+/**\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+ * \\return The converted value\n+ */\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/test/utils.cpp b/test/utils.cpp\nindex fc56e14e..f1805207 100644\n--- a/test/utils.cpp\n+++ b/test/utils.cpp\n@@ -170,6 +170,51 @@ protected:\n \t\treturn TestPass;\n \t}\n \n+\ttemplate<unsigned int intPrec, unsigned fracPrec, typename T>\n+\tint testSingleFixedPoint(double input, T expected)\n+\t{\n+\t\tT ret = utils::floatingToFixedPoint<intPrec, fracPrec, T>(input);\n+\t\tif (ret != expected) {\n+\t\t\tcerr << \"Expected \" << input << \" to convert to \"\n+\t\t\t << expected << \", got \" << ret << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\t/*\n+\t\t * The precision check is fairly arbitrary but is based on what\n+\t\t * the rkisp1 is capable of in the crosstalk module.\n+\t\t */\n+\t\tdouble f = utils::fixedToFloatingPoint<intPrec, fracPrec, double>(ret);\n+\t\tif (std::abs(f - input) > 0.001) {\n+\t\t\tcerr << \"Reverse conversion expected \" << ret\n+\t\t\t << \" to convert to \" << input\n+\t\t\t << \", got \" << f << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n+\tint testFixedPoint()\n+\t{\n+\t\t/* These are the only cases that we know for certain */\n+\t\tstd::map<double, uint16_t> testCases = {\n+\t\t\t{ 7.992, 0x3FF },\n+\t\t\t{ -8, 0x400 },\n+\t\t\t{ 0, 0 },\n+\t\t};\n+\n+\t\tint ret;\n+\t\tfor (const auto &testCase : testCases) {\n+\t\t\tret = testSingleFixedPoint<4, 7, uint16_t>(testCase.first,\n+\t\t\t\t\t\t\t\t testCase.second);\n+\t\t\tif (ret != TestPass)\n+\t\t\t\treturn ret;\n+\t\t}\n+\n+\t\treturn TestPass;\n+\t}\n+\n \tint run()\n \t{\n \t\t/* utils::hex() test. */\n@@ -290,6 +335,10 @@ protected:\n \t\tif (testDuration() != TestPass)\n \t\t\treturn TestFail;\n \n+\t\t/* fixed point conversion test */\n+\t\tif (testFixedPoint() != TestPass)\n+\t\t\treturn TestFail;\n+\n \t\treturn TestPass;\n \t}\n };\n", "prefixes": [ "v2" ] }