From patchwork Tue Dec 5 13:02:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13480220 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D6MUHoGg" Received: from mail-lj1-x230.google.com (mail-lj1-x230.google.com [IPv6:2a00:1450:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EF90D7; Tue, 5 Dec 2023 05:02:14 -0800 (PST) Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2c9f62447c2so28574411fa.0; Tue, 05 Dec 2023 05:02:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701781333; x=1702386133; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=wVkkONZNfkX/iqB+XjK9GHA8D0k5JMrE6DojR60eqaY=; b=D6MUHoGgJ/6SI9+AoORI8jqnKO0a7qeRmgw6djvw03qTewZZ4n6syf6F6yZc2Kkf0K AU9DiEhaz2/ZCzwpr3QPwiXL7+Jyv/mS393B4nwmrdliTUGODNq2RraufOTe7mVun/9H 172nG+607I1bXUvPskn027N/IeEeTNBaptb0XR7migdOajUe0A4oh2QNcXmDcz75P29w dNGNF3PQPP4lp3RMwOdE25XqK4fC7k6WLOAHs8tl12t8pBV3psbLLNbYYRCVPz74LqtV 1RS7BI4EY73IkhIuHdgoRedoMoR5bzLw7VCLkd750qZIAwXMdi/YoIcF90oeC8eZfQ20 5Vxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701781333; x=1702386133; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wVkkONZNfkX/iqB+XjK9GHA8D0k5JMrE6DojR60eqaY=; b=HVzO6HbICG81Cux2KVAf3XYLHSzkJ/fGJqoqG6QSYvllr4BKCL7/CJ3SacdQz/m+p+ AhDwDPxB0/uKlIJF5AwR0pfuXCcnLKnM8244T0fPZcsG9la/X7GmdpmmkWPD+KTYtIxQ IcTbdWiRUMpjptIcDMrW9bAHCb89sws3z9F8BcYhK6Cqr7PqMKRVU1GALg89GbV+nFkq zQ3yuzV9cBpibkPtAmkQ+zijtADHM+aUnmJgT7QZwalu1gQc9+GikFp0WmR0BziyYhyg Nggjyml9dF535wrmlRjxjRezhXh0oXpAmKlaLisMblrtoSzYFQamzYreK0Wz/hqErwF0 MZnA== X-Gm-Message-State: AOJu0Ywr/IaGbVdPE0nZsyVFn3bHuxvIC+7ESWwlKH9f+rt9i+nEb00j IKvLNfm9LwZN+ETF0GU2wao= X-Google-Smtp-Source: AGHT+IFIucVq/Ad+FCHssDPbCMwr4B9Zz+8RCfmeVFjlA7QUKRwCAc/TaISJtgr2H0CzQ9O+7oS1Ow== X-Received: by 2002:a2e:6a12:0:b0:2c9:e3ad:1f32 with SMTP id f18-20020a2e6a12000000b002c9e3ad1f32mr870027ljc.6.1701781332351; Tue, 05 Dec 2023 05:02:12 -0800 (PST) Received: from dc78bmyyyyyyyyyyyyyby-3.rev.dnainternet.fi (dc78bmyyyyyyyyyyyyyby-3.rev.dnainternet.fi. [2001:14ba:16f8:1500::2]) by smtp.gmail.com with ESMTPSA id u18-20020a2eb812000000b002ca0090a933sm801384ljo.10.2023.12.05.05.02.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 05:02:11 -0800 (PST) Date: Tue, 5 Dec 2023 15:02:02 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Matti Vaittinen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Subhajit Ghosh Subject: [PATCH v2] iio: gts-helpers: Round gains and scales Message-ID: <37d3aa193e69577353d314e94463a08d488ddd8d.1701780964.git.mazziesaccount@gmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline The GTS helpers do flooring of scale when calculating available scales. This results available-scales to be reported smaller than they should when the division in scale computation resulted remainder greater than half of the divider. (decimal part of result > 0.5) Furthermore, when gains are computed based on scale, the gain resulting from the scale computation is also floored. As a consequence the floored scales reported by available scales may not match the gains that can be set. Finally, the loop-based implementation of the 64-bit division which is used by GTS-helpers may be running for a _very long_ time. This patch does also replace this implementation by a well-known variant of a 64-bit division div64_u64(). The related discussion can be found from: https://lore.kernel.org/all/84d7c283-e8e5-4c98-835c-fe3f6ff94f4b@gmail.com/ Do rounding when computing scales and gains. Fixes: 38416c28e168 ("iio: light: Add gain-time-scale helpers") Signed-off-by: Matti Vaittinen --- Revision history: v1 => v2: - fix rounding in iio_gts_get_gain() The iio_gts_get_gain() is accidentally doing >> 2 when it attempts to divide by 2. Fix this. - use proper 64bit division div64_u64() instead of a loop and extra 32bit function - Fix iio_gts_total_gain_to_scale() documentation as it's not computing gain. - Add a comment on rounding logic in iio_gts_total_gain_to_scale(). It's fair to point out Jonathan questioned the rounding logic and suggested using the more of a "de-facto" method of adding divider / 2 to value being divided, and checking for overflow. The proper handling of the overflow however resulted even more confusing code (in my subjective opinion) than the (more) straightforward approach of checking the size of the reminder and adding +1 to result when needed. Subjahit, is there any chance you test this patch with your driver? Can you drop the: if (val2 % 10) val2 += 1; from scale setting and do you see written and read scales matching? I did run a few Kunit tests on this change - but I'm still a bit jumpy on it... Reviewing/testing is highly appreciated! --- drivers/iio/industrialio-gts-helper.c | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index 7653261d2dc2..19091193e430 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -28,28 +29,32 @@ * scale is 64 100 000 000. * @scale: Linearized scale to compute the gain for. * - * Return: (floored) gain corresponding to the scale. -EINVAL if scale + * Return: (rounded) gain corresponding to the scale. -EINVAL if scale * is invalid. */ static int iio_gts_get_gain(const u64 max, const u64 scale) { - u64 full = max; - int tmp = 1; + u64 full = max, half_div; + int tmp = 0; if (scale > full || !scale) return -EINVAL; - if (U64_MAX - full < scale) { - /* Risk of overflow */ - if (full - scale < scale) + half_div = scale >> 1; + + if (U64_MAX - full < half_div) { + /* + * Would overflow when adding half_div to full. Hence we need + * to subtract scale from full if full is big enough. + */ + if (full - scale <= half_div) return 1; full -= scale; tmp++; } - while (full > scale * (u64)tmp) - tmp++; + tmp += div64_u64(full + half_div, scale); return tmp; } @@ -140,10 +145,18 @@ int iio_gts_total_gain_to_scale(struct iio_gts *gts, int total_gain, int *scale_int, int *scale_nano) { u64 tmp; + int rem; tmp = gts->max_scale; - do_div(tmp, total_gain); + rem = do_div(tmp, total_gain); + + /* + * Round up if remainder is equal to or greater than the half of + * the divider. + */ + if (total_gain > 1 && rem >= total_gain / 2) + tmp += 1ULL; return iio_gts_delinearize(tmp, NANO, scale_int, scale_nano); }