{"id":20125,"url":"https://patchwork.libcamera.org/api/1.1/patches/20125/?format=json","web_url":"https://patchwork.libcamera.org/patch/20125/","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":"<20240529192144.801432-1-paul.elder@ideasonboard.com>","date":"2024-05-29T19:21:44","name":"[v3] ipa: rkisp1: Add a helper to convert floating-point to fixed-point","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d6f39652ff9f6a5185386be7183cb9baf40bb8e2","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/20125/mbox/","series":[{"id":4334,"url":"https://patchwork.libcamera.org/api/1.1/series/4334/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4334","date":"2024-05-29T19:21:44","name":"[v3] ipa: rkisp1: Add a helper to convert floating-point to fixed-point","version":3,"mbox":"https://patchwork.libcamera.org/series/4334/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/20125/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/20125/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 95BC4BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 May 2024 19:22:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 97365634B6;\n\tWed, 29 May 2024 21:22:04 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 006286347E\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 May 2024 21:22:02 +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 1D058149B;\n\tWed, 29 May 2024 21:21:57 +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=\"HMK9hiUO\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717010519;\n\tbh=Ef7jhPPC8a66gU7jnfaZFrjzCvuzkDE/jTWJ+OKeXYg=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=HMK9hiUObBdcU4fnBpVfTzdspLS9dlih4Gaz3LxaZj9jea5AoQ8MFB8h9Lxq+Hpv/\n\twkrsQpLX4lueX0V3LVFz+G+tzHCGtWpQv65yGijpuRkFiJXOadCMesIr39dFLo8u38\n\tf3gqqa+wKn27ixFKVxPAdwsqPU1ZtfMJOLJko4IE=","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Paul Elder <paul.elder@ideasonboard.com>","Subject":"[PATCH v3] ipa: rkisp1: Add a helper to convert floating-point to\n\tfixed-point","Date":"Thu, 30 May 2024 04:21:44 +0900","Message-Id":"<20240529192144.801432-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 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           | 67 +++++++++++++++++++++++++++\n test/ipa/meson.build             |  2 +\n test/ipa/rkisp1/meson.build      | 15 ++++++\n test/ipa/rkisp1/rkisp1-utils.cpp | 78 ++++++++++++++++++++++++++++++++\n 6 files changed, 203 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..7d647d6d7\n--- /dev/null\n+++ b/src/ipa/rkisp1/utils.h\n@@ -0,0 +1,67 @@\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 * Create a number with all upper bits set including the sign bit\n+\t * of the fixed point number. We need an extra cast as the compiler\n+\t * won't let us left-shift negative numbers.\n+\t */\n+\tint upper_ones = static_cast<int>(std::numeric_limits<unsigned int>::max() << (I + F - 1));\n+\tif (number & upper_ones)\n+\t\treturn static_cast<R>(upper_ones | number) / static_cast<R>(1 << F);\n+\n+\treturn static_cast<R>(number) / 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..c0c05dc68\n--- /dev/null\n+++ b/test/ipa/rkisp1/rkisp1-utils.cpp\n@@ -0,0 +1,78 @@\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.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/* 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":["v3"]}