[{"id":33809,"web_url":"https://patchwork.libcamera.org/comment/33809/","msgid":"<174344444965.3394313.7542759682581787694@ping.linuxembedded.co.uk>","date":"2025-03-31T18:07:29","subject":"Re: [PATCH 2/5] libipa: histogram: Fix quantile() calculation for\n\tfractional results","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Stefan Klug (2025-03-24 17:07:37)\n> The calculation of the frac variable is based solely on integers and\n> therefore results in the fractional part being either 0 or 1.\n> \n> In the original code from RaspberryPi this is mitigated by casting the\n> nominator to a double. This works for most cases, but fails when q is\n> very small because of the quantization introduced by item being an\n> integer.\n> \n> Fix both issues by doing the full calculation in double.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\nas far as I can tell this seems reasonable...\n\nProof is in the pudding-test.... which I see was added later...\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n>  src/ipa/libipa/histogram.cpp | 3 ++-\n>  1 file changed, 2 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp\n> index 10e44b54a0cf..c19a4cbbf3cd 100644\n> --- a/src/ipa/libipa/histogram.cpp\n> +++ b/src/ipa/libipa/histogram.cpp\n> @@ -130,7 +130,8 @@ double Histogram::quantile(double q, uint32_t first, uint32_t last) const\n>         if (cumulative_[first + 1] == cumulative_[first])\n>                 frac = 0;\n>         else\n> -               frac = (item - cumulative_[first]) / (cumulative_[first + 1] - cumulative_[first]);\n> +               frac = (q * total() - cumulative_[first]) /\n> +                      (cumulative_[first + 1] - cumulative_[first]);\n>         return first + frac;\n>  }\n>  \n> -- \n> 2.43.0\n>","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 3889EC323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 31 Mar 2025 18:07:35 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 80AE76897A;\n\tMon, 31 Mar 2025 20:07:34 +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 2FDB16897A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 31 Mar 2025 20:07:33 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 89BDF725;\n\tMon, 31 Mar 2025 20:05: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=\"A60PWATG\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743444341;\n\tbh=hAEjtg8nB8HAhKa1Su1k2Qrl6O/iKWJPlb/b/JyE2Fw=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=A60PWATGUgEDWP2U7hcRlKnyjKGjw5CVnn0BMbugX0lCJsJ4Q3q8df3YAn3lP61oT\n\tO+Fpp50DH46QahLS5qHv1dX7OMTOi0X1dGQyIknJOVMWYU25BUvq5egeJ2IDH04Ni/\n\tpIZ0HxKmKvmIpCXuSJRqJJu9V/h46ls3R4aJQ+vM=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250324170803.103296-3-stefan.klug@ideasonboard.com>","References":"<20250324170803.103296-1-stefan.klug@ideasonboard.com>\n\t<20250324170803.103296-3-stefan.klug@ideasonboard.com>","Subject":"Re: [PATCH 2/5] libipa: histogram: Fix quantile() calculation for\n\tfractional results","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Mon, 31 Mar 2025 19:07:29 +0100","Message-ID":"<174344444965.3394313.7542759682581787694@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}},{"id":33819,"web_url":"https://patchwork.libcamera.org/comment/33819/","msgid":"<20250331220711.GL14432@pendragon.ideasonboard.com>","date":"2025-03-31T22:07:11","subject":"Re: [PATCH 2/5] libipa: histogram: Fix quantile() calculation for\n\tfractional results","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Stefan,\n\nThank you for the patch.\n\nOn Mon, Mar 24, 2025 at 06:07:37PM +0100, Stefan Klug wrote:\n> The calculation of the frac variable is based solely on integers and\n> therefore results in the fractional part being either 0 or 1.\n\nOops.\n\n> In the original code from RaspberryPi this is mitigated by casting the\n> nominator to a double. This works for most cases, but fails when q is\n> very small because of the quantization introduced by item being an\n> integer.\n> \n> Fix both issues by doing the full calculation in double.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> ---\n>  src/ipa/libipa/histogram.cpp | 3 ++-\n>  1 file changed, 2 insertions(+), 1 deletion(-)\n> \n> diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp\n> index 10e44b54a0cf..c19a4cbbf3cd 100644\n> --- a/src/ipa/libipa/histogram.cpp\n> +++ b/src/ipa/libipa/histogram.cpp\n> @@ -130,7 +130,8 @@ double Histogram::quantile(double q, uint32_t first, uint32_t last) const\n>  \tif (cumulative_[first + 1] == cumulative_[first])\n>  \t\tfrac = 0;\n>  \telse\n> -\t\tfrac = (item - cumulative_[first]) / (cumulative_[first + 1] - cumulative_[first]);\n> +\t\tfrac = (q * total() - cumulative_[first]) /\n> +\t\t       (cumulative_[first + 1] - cumulative_[first]);\n\nMaybe\n\n\t\tfrac = (q * total() - cumulative_[first])\n\t\t     / (cumulative_[first + 1] - cumulative_[first]);\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \treturn first + frac;\n>  }\n>","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 5A8FDC323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 31 Mar 2025 22:07:40 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5920F68981;\n\tTue,  1 Apr 2025 00:07:39 +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 05F7E68979\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  1 Apr 2025 00:07:36 +0200 (CEST)","from pendragon.ideasonboard.com (85-76-147-224-nat.elisa-mobile.fi\n\t[85.76.147.224])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D9F50105D;\n\tTue,  1 Apr 2025 00:05:44 +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=\"Pm8UeUbt\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743458745;\n\tbh=0EoMiBGd5LEly5kOYPT37InG0bmAD0k2//Eak04Jqv8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Pm8UeUbtM1lPGV19c7YBhGLPuyyJyPdAfTGV11vkdYTUaxg7xkl6fCoz9br32FQ/P\n\t0wzGpJtlHDMvf4guUQQ4uxSLq1zvanEHAovzlIYPtK/7gSFA3ppOdK7lK8LCfz6OpN\n\t2bGQFsElfCgXMRxMR1zuwPeuREshdySZq3NsnA8E=","Date":"Tue, 1 Apr 2025 01:07:11 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH 2/5] libipa: histogram: Fix quantile() calculation for\n\tfractional results","Message-ID":"<20250331220711.GL14432@pendragon.ideasonboard.com>","References":"<20250324170803.103296-1-stefan.klug@ideasonboard.com>\n\t<20250324170803.103296-3-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20250324170803.103296-3-stefan.klug@ideasonboard.com>","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>"}}]