Patch Detail
Show a patch.
GET /api/patches/20145/?format=api
{ "id": 20145, "url": "https://patchwork.libcamera.org/api/patches/20145/?format=api", "web_url": "https://patchwork.libcamera.org/patch/20145/", "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": "<20240530123934.2325922-1-paul.elder@ideasonboard.com>", "date": "2024-05-30T12:39:34", "name": "[v4] ipa: rkisp1: Add a helper to convert floating-point to fixed-point", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "ffdcda1fed404f1b9fc343ef859cc22a3a62c02b", "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/20145/mbox/", "series": [ { "id": 4340, "url": "https://patchwork.libcamera.org/api/series/4340/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4340", "date": "2024-05-30T12:39:34", "name": "[v4] ipa: rkisp1: Add a helper to convert floating-point to fixed-point", "version": 4, "mbox": "https://patchwork.libcamera.org/series/4340/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/20145/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/20145/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 66864BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 30 May 2024 12:39:50 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 25E71634B6;\n\tThu, 30 May 2024 14:39:49 +0200 (CEST)", "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 CFCD361A43\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 30 May 2024 14:39:46 +0200 (CEST)", "from neptunite.hamster-moth.ts.net\n\t(h175-177-049-156.catv02.itscom.jp [175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5D39829F;\n\tThu, 30 May 2024 14:39:41 +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=\"As2j/XyZ\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717072782;\n\tbh=ZK3TkKM+opmWoUzR50pczRjiMSUtFlizf4azNitmVK0=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=As2j/XyZM7tbyi5j9avDBXDyI9ScH4oQgQxk0T4zy16cLAy/c+E7O6Tjac4A1dIc9\n\tYfe21LigLNeznRQV3gToVSOe6wr0NJLZjYsw7IqbIPVmBOkkQngTt426ltzyAKBTVW\n\tQIy6z6pFz75hJWcJJwqY/DzqFMRNLebUad5Dckcw=", "From": "Paul Elder <paul.elder@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Paul Elder <paul.elder@ideasonboard.com>", "Subject": "[PATCH v4] ipa: rkisp1: Add a helper to convert floating-point to\n\tfixed-point", "Date": "Thu, 30 May 2024 21:39:34 +0900", "Message-Id": "<20240530123934.2325922-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 v4:\n- relax the test case floating point precision\n- optimize the fixed -> floating point converter\n\nChanges in v3:\n- (used to be \"libcamera: utils: Add a helper to convert floating-point to fixed-point\")\n- move the everything from libcamera global utils to the rkisp1 ipa\n\nChanges in v2:\n- added the reverse conversion function (fixed -> floating)\n- added tests\n- make the conversion code cleaner\n---\n src/ipa/rkisp1/meson.build | 1 +\n src/ipa/rkisp1/utils.cpp | 40 +++++++++++++++\n src/ipa/rkisp1/utils.h | 66 +++++++++++++++++++++++++\n test/ipa/meson.build | 2 +\n test/ipa/rkisp1/meson.build | 15 ++++++\n test/ipa/rkisp1/rkisp1-utils.cpp | 84 ++++++++++++++++++++++++++++++++\n 6 files changed, 208 insertions(+)\n create mode 100644 src/ipa/rkisp1/utils.cpp\n create mode 100644 src/ipa/rkisp1/utils.h\n create mode 100644 test/ipa/rkisp1/meson.build\n create mode 100644 test/ipa/rkisp1/rkisp1-utils.cpp", "diff": "diff --git a/src/ipa/rkisp1/meson.build b/src/ipa/rkisp1/meson.build\nindex e813da53a..cf05cdb27 100644\n--- a/src/ipa/rkisp1/meson.build\n+++ b/src/ipa/rkisp1/meson.build\n@@ -8,6 +8,7 @@ ipa_name = 'ipa_rkisp1'\n rkisp1_ipa_sources = files([\n 'ipa_context.cpp',\n 'rkisp1.cpp',\n+ 'utils.cpp',\n ])\n \n rkisp1_ipa_sources += rkisp1_ipa_algorithms\ndiff --git a/src/ipa/rkisp1/utils.cpp b/src/ipa/rkisp1/utils.cpp\nnew file mode 100644\nindex 000000000..53186fdd6\n--- /dev/null\n+++ b/src/ipa/rkisp1/utils.cpp\n@@ -0,0 +1,40 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Miscellaneous utility functions specific to rkisp1\n+ */\n+\n+#include \"utils.h\"\n+\n+/**\n+ * \\file utils.h\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa::rkisp1::utils {\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 ipa::rkisp1::utils */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/rkisp1/utils.h b/src/ipa/rkisp1/utils.h\nnew file mode 100644\nindex 000000000..450f22442\n--- /dev/null\n+++ b/src/ipa/rkisp1/utils.h\n@@ -0,0 +1,66 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Miscellaneous utility functions specific to rkisp1\n+ */\n+\n+#pragma once\n+\n+#include <cmath>\n+#include <limits>\n+#include <type_traits>\n+\n+namespace libcamera {\n+\n+namespace ipa::rkisp1::utils {\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(sizeof(int) >= sizeof(R));\n+\tstatic_assert(I + F <= sizeof(R) * 8);\n+\n+\t/*\n+\t * The intermediate cast to int is needed on arm platforms to properly\n+\t * cast negative values. See\n+\t * https://embeddeduse.com/2013/08/25/casting-a-negative-float-to-an-unsigned-int/\n+\t */\n+\tR mask = (1 << (F + I)) - 1;\n+\tR frac = static_cast<R>(static_cast<int>(std::round(number * (1 << F)))) & mask;\n+\n+\treturn 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(sizeof(int) >= sizeof(T));\n+\tstatic_assert(I + F <= sizeof(T) * 8);\n+\n+\t/*\n+\t * Recreate the upper bits in case of a negative number by shifting the sign\n+\t * bit from the fixed point to the first bit of the unsigned and then right shifting\n+\t * by the same amount which keeps the sign bit in place.\n+\t * This can be optimized by the compiler quite well.\n+\t */\n+\tint remaining_bits = sizeof(int) * 8 - (I + F);\n+\tint t = static_cast<int>(static_cast<unsigned>(number) << remaining_bits) >> remaining_bits;\n+\treturn static_cast<R>(t) / static_cast<R>(1 << F);\n+}\n+\n+} /* namespace ipa::rkisp1::utils */\n+\n+} /* namespace libcamera */\ndiff --git a/test/ipa/meson.build b/test/ipa/meson.build\nindex 180b0da0a..dc956284c 100644\n--- a/test/ipa/meson.build\n+++ b/test/ipa/meson.build\n@@ -1,5 +1,7 @@\n # SPDX-License-Identifier: CC0-1.0\n \n+subdir('rkisp1')\n+\n ipa_test = [\n {'name': 'ipa_module_test', 'sources': ['ipa_module_test.cpp']},\n {'name': 'ipa_interface_test', 'sources': ['ipa_interface_test.cpp']},\ndiff --git a/test/ipa/rkisp1/meson.build b/test/ipa/rkisp1/meson.build\nnew file mode 100644\nindex 000000000..5ffc5dd60\n--- /dev/null\n+++ b/test/ipa/rkisp1/meson.build\n@@ -0,0 +1,15 @@\n+# SPDX-License-Identifier: CC0-1.0\n+\n+rkisp1_ipa_test = [\n+ {'name': 'rkisp1-utils', 'sources': ['rkisp1-utils.cpp']},\n+]\n+\n+foreach test : rkisp1_ipa_test\n+ exe = executable(test['name'], test['sources'], libcamera_generated_ipa_headers,\n+ dependencies : libcamera_private,\n+ link_with : [libipa, test_libraries],\n+ include_directories : [libipa_includes, test_includes_internal,\n+ '../../../src/ipa/rkisp1/'])\n+\n+ test(test['name'], exe, suite : 'ipa')\n+endforeach\ndiff --git a/test/ipa/rkisp1/rkisp1-utils.cpp b/test/ipa/rkisp1/rkisp1-utils.cpp\nnew file mode 100644\nindex 000000000..d23d87e0f\n--- /dev/null\n+++ b/test/ipa/rkisp1/rkisp1-utils.cpp\n@@ -0,0 +1,84 @@\n+/* SPDX-License-Identifier: GPL-2.0-or-later */\n+/*\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Miscellaneous utility tests\n+ */\n+\n+#include <cmath>\n+#include <iostream>\n+#include <map>\n+\n+#include \"../src/ipa/rkisp1/utils.h\"\n+\n+#include \"test.h\"\n+\n+using namespace std;\n+using namespace libcamera;\n+using namespace ipa::rkisp1;\n+\n+class RkISP1UtilsTest : public Test\n+{\n+protected:\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.005) {\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{ 7.992, 0xBFF },\n+\t\t\t{ 0.2, 0x01A },\n+\t\t\t{ -0.2, 0x7E6 },\n+\t\t\t{ -0.8, 0x79A },\n+\t\t\t{ -0.4, 0x7CD },\n+\t\t\t{ -1.4, 0x74D },\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/* fixed point conversion test */\n+\t\tif (testFixedPoint() != TestPass)\n+\t\t\treturn TestFail;\n+\n+\t\treturn TestPass;\n+\t}\n+};\n+\n+TEST_REGISTER(RkISP1UtilsTest)\n", "prefixes": [ "v4" ] }