From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794665402; b=VjitGcxA+aTwi+LNjPNgfzO7sYkgITGcHqex+71sjVum7oojfotd89CDk6lrP80OFFsV QW3P3OkYAkSunrSYH6AgXz0SSGwwbrh91z/nO8VncpDPLhqls26okqGHRIdx9fKqirkSw zUBgtGeiJL2hwEHKY6yKlUheQWOiJuprZykgqPmykHpkeNg8VSZjSGDIeK+rqiYeDieLz vZM9Eot1oPI2BsGeYWi2uh/HxvTTrNxlpHbjl9YnVMo7pb6KK7BQUaIGboY1VESpIQVhE 7j+CdZq/y9wEdVV13Zdh19kKlOSAiqZoeRMjj9XgAlEa2mK2j2GatZ2PWHJCdE8pxQw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794665402; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=EPzCbxKELMHtph57rw7uF87sPahx/3UDJZ6gvRafZVk=; b=D7mwgFMBOVgUCyGRxxEamntcFm2T3v9KbifcPuGPoBYp0nUwDzGm3Wb/aKsQ4H2v9joO fJ7ib1l1p0D6FVP0V/+OO++1dH+pN4Pk9yekfeJvQ//FywjN58r6IaNUKxuigw73MJV4b Lak59IHUZPl8LtDnxhKbNObt5yx22eGF9lQdFAPj8VBqEbJhkKD5o9LTfw+GlkyD1lMmz izDgfwsXA8npnSWRLCen55/z0dmnmMB/ffAM0s+3kvgO3P5+So7S3lwiQIVUsiwAZa2IU +nWGh61K74UgVEmUFVNjCWwDpiYrgZZX7/3jnHiB+AE/yq7EWNkBLzzG+EumxsOZxSw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=O6PbuG4W; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B87DC606A88C; Sat, 14 Nov 2026 14:10:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=O6PbuG4W Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1794665402; b=i0GZoNBqhWkv8s6wnMO2VyYA/AKBFnDFKExN9NsSGLC8BYwGXVvN2/bVrsYB6zrK3zN5 BHg1Oul2kSUUPgbypJ4LN9G2L4G2U3NBRJLxpxZsQzChjnsBLVDoChjguqpMnSzk2SqAf GKFxwLEcAxk7OVOCy+KBuOaAmgMGVtc6y8OC7wwp6Eqpf2m+WsMVp4+OhAhwCljphvr19 zcFlQYHgCfb3UkyNjCa7fuYA2QMq2x0sWjUBDF9f49E6qOYojPAzEk3uzVRT3GvjZVejf 9JgAR9l9q64CEEkDTAr+VBSI889zsRKKYduB3W4w8QA2v2qzQv8q+xoswsXUwOI2KNw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1794665402; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=EPzCbxKELMHtph57rw7uF87sPahx/3UDJZ6gvRafZVk=; b=I1QdjYlfodravlG2b8Aj2zb0nnaFCvOelO/W02ehP6ySFpK7SXNUI+dWvDAbOlZxbBux gu4S9AbuuyyQ3gct4lcLh93UPMHIGieEH/JxefW47CwQL1zOkm4Zf5AvM47L2vA+3bto4 aBukkpWBk2PQOJut45WZEMtV/y+Nbj4zOk1JjCvoZBDuyTPBXd7m8KnGtQCt6OFWjBVyx w3zbwcFJFPiEol9eNizsa01UftkjWCWhX046TC+QlHWb52/UnJh6ohHxWMY4l8OtiwuNQ reepzY4fKvHZlMpM1RjhWxeCEHEOcQWRdAvF0DTnq5ir2XSeCLFOx09ijWbVI1QJ7Cg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 64DE61F000C4; Sat, 14 Nov 2026 14:10:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794665402; bh=EPzCbxKELMHtph57rw7uF87sPahx/3UDJZ6gvRafZVk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=O6PbuG4WzhjpkJjdfiFdZfOG5msxEUTSO3o/6Xodx4Nzol/ZhxDYuejnRVvqTxhS3 kdEOxaiiYYiY/YOy+sX6qtnooZP65oS5I/nBWQBnnEQuXswuI1VVIDqs8Hqgyt4cF9 EH2KZQzAvDA9ZKhYCF14kL/OWKtUMGnebMPDDf5aAFA5luZKOdyMKxtWgGpDkHxqmw MxMrzhKhOR/iY1odF0B/rhZ9fAl+DMaR+MQ6B7y9xYGk1qqTCaxz7uZQ4WxB4trcvV +g4g2ADMwdzbjulSGhtBYKVeMW7Oo0tA9lbirljYsVg8etqupAqH/jTL6Vu3KYolAE 3+LE2UMAAYVoA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id D35F249DF8; Sat, 14 Nov 2026 14:09:59 +0000 (UTC) From: Rodrigo Alencar via B4 Relay Date: Sat, 14 Nov 2026 14:09:57 +0000 Subject: [PATCH v6 5/7] iio: test: rescale: compare scales against the precision of the output X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261114-e-notation-v6-5-205ca9a1438a@analog.com> References: <20261114-e-notation-v6-0-205ca9a1438a@analog.com> In-Reply-To: <20261114-e-notation-v6-0-205ca9a1438a@analog.com> To: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org Cc: Andrew Morton , Jonathan Cameron , David Lechner , Andy Shevchenko , Rodrigo Alencar X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1794665398; l=5689; i=rodrigo.alencar@analog.com; s=default; h=from:subject:message-id; bh=MYurB/fu0EGmpzdutFErjw1ab4qsWqsloaJ03rKS+2A=; b=MJQvpM3blgIooooDJYURY226+G2pg+ZULwn8/WUZgkj+MYUQSoWQS22Ylou1yyaDPCXXLKon+ LjmRfWUI5cIBIcfchq7CZvduknBr9Nq4QgW8t7WeKoWc/DplGnWR7ed X-Developer-Key: i=rodrigo.alencar@analog.com; a=ed25519; pk=ULeHbgU/OYh/PG/4anHDfLgldFItQHAhOktYRVLMFRo= X-Endpoint-Received: by B4 Relay for rodrigo.alencar@analog.com/default with auth_id=561 X-Original-From: Rodrigo Alencar Reply-To: rodrigo.alencar@analog.com Content-Transfer-Encoding: 7bit From: Rodrigo Alencar The scale tests format the rescaled value with iio_format_value(), which prints at most nano precision, and require the relative error to the expected value to be below 1 ppm. Both strings are parsed back with iio_str_to_fixpoint() at nano precision, so for the small scale cases the check only passes because the expected value, like the formatted one, is truncated to nano. For example, "0.000000033594" is parsed as 33 nano, equal to the formatted "0.000000033", although the actual error is about 1.8%. If the expected value were rounded instead, it would be parsed as 34 nano and the test would fail even though the output is as accurate as the format allows. Parse both values with kstrtodec64() at a scale of 10, one digit more than the output precision, so the expected value keeps the digits lost when formatting. Scale 10 is the largest that still fits the overflow cases (about 2.1e8) in an s64. Compute the relative error with mul_u64_u64_div_u64(), as the product with 10^6 no longer fits in 64 bits for those cases. Then allow a relative error up to the relative size of one nano of the expected value, the precision of the formatted output. This is 0 ppm for values of 0.01 and above, so those cases are checked as before, and it accepts the formatting error for the small scale cases. Signed-off-by: Rodrigo Alencar --- drivers/iio/test/iio-test-rescale.c | 60 ++++++++++++++----------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/drivers/iio/test/iio-test-rescale.c b/drivers/iio/test/iio-test-rescale.c index de4a09f432c0..e37a20ea1ca6 100644 --- a/drivers/iio/test/iio-test-rescale.c +++ b/drivers/iio/test/iio-test-rescale.c @@ -6,6 +6,8 @@ */ #include +#include +#include #include #include @@ -581,55 +583,37 @@ static void case_to_desc(const struct rescale_tc_data *t, char *desc) KUNIT_ARRAY_PARAM(iio_rescale_scale, scale_cases, case_to_desc); KUNIT_ARRAY_PARAM(iio_rescale_offset, offset_cases, case_to_desc); -/** - * iio_str_to_nano() - Parse a fixed-point string to get an - * IIO_VAL_INT_PLUS_NANO value - * @str: The string to parse - * @nano: The number as an integer - * - * Returns 0 on success, or a negative error code if the string could not be - * parsed. +/* + * iio_format_value() prints scales with up to nano precision. Parse values + * with one more digit, so the expected values keep the precision lost there. + * Scale 10 is the largest that fits the overflow test cases in an s64. */ -static int iio_str_to_nano(const char *str, s64 *nano) -{ - int tmp, tmp2; - int ret = 0; - - /* - * iio_str_to_fixpoint() uses 10^8 here instead of 10^9 as fract_mult is - * the multiplier for the first decimal place. - */ - ret = iio_str_to_fixpoint(str, 100000000, &tmp, &tmp2); - if (ret < 0) - return ret; - - if (tmp < 0) - tmp2 *= -1; - - *nano = (s64)tmp * 1000000000UL + tmp2; - - return ret; -} +#define IIO_TEST_EXTRA_DEC_SCALE 1 +#define IIO_TEST_DEC_SCALE (9 + IIO_TEST_EXTRA_DEC_SCALE) +#define IIO_TEST_NANO_PER_DEC_SCALE int_pow(10, IIO_TEST_EXTRA_DEC_SCALE) /** * iio_test_relative_error_ppm() - Compute relative error (in parts-per-million) * between two fixed-point strings * @real_str: The real value as a string * @exp_str: The expected value as a string + * @max_ppm: The relative size (in parts-per-million) of one nano of the + * expected value, the precision of iio_format_value() * * Returns a negative error code if the strings could not be parsed, or the * relative error in parts-per-million. */ -static int iio_test_relative_error_ppm(const char *real_str, const char *exp_str) +static int iio_test_relative_error_ppm(const char *real_str, const char *exp_str, + int *max_ppm) { - s64 real, exp, err; + s64 real, exp; int ret; - ret = iio_str_to_nano(real_str, &real); + ret = kstrtodec64(real_str, IIO_TEST_DEC_SCALE, &real); if (ret < 0) return ret; - ret = iio_str_to_nano(exp_str, &exp); + ret = kstrtodec64(exp_str, IIO_TEST_DEC_SCALE, &exp); if (ret < 0) return ret; @@ -638,9 +622,9 @@ static int iio_test_relative_error_ppm(const char *real_str, const char *exp_str return -EINVAL; } - err = 1000000UL * abs(exp - real); + *max_ppm = (int)div64_u64(IIO_TEST_NANO_PER_DEC_SCALE * 1000000ULL, abs(exp)); - return (int)div64_u64(err, abs(exp)); + return (int)mul_u64_u64_div_u64(abs(exp - real), 1000000, abs(exp)); } static void iio_rescale_test_scale(struct kunit *test) @@ -650,6 +634,7 @@ static void iio_rescale_test_scale(struct kunit *test) struct rescale rescale; int values[2]; int rel_ppm; + int max_ppm; int ret; KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buff); @@ -666,10 +651,11 @@ static void iio_rescale_test_scale(struct kunit *test) ret = iio_format_value(buff, ret, 2, values); KUNIT_EXPECT_EQ(test, (int)strlen(buff), ret); - rel_ppm = iio_test_relative_error_ppm(buff, t->expected); - KUNIT_EXPECT_GE_MSG(test, rel_ppm, 0, "failed to compute ppm\n"); + rel_ppm = iio_test_relative_error_ppm(buff, t->expected, &max_ppm); + KUNIT_ASSERT_GE_MSG(test, rel_ppm, 0, "failed to compute ppm\n"); - KUNIT_EXPECT_EQ_MSG(test, rel_ppm, 0, + /* allow the error of the nano precision used to format the value */ + KUNIT_EXPECT_LE_MSG(test, rel_ppm, max_ppm, "\t real=%s" "\texpected=%s\n", buff, t->expected); -- 2.43.0