From 7eece6917c541af24a6161b10a150b5744695f80 Mon Sep 17 00:00:00 2001 From: Andy Shevchenko Date: Mon, 2 Mar 2026 10:28:07 +0100 Subject: lib: polynomial: move to math/ subfolder Patch series "lib: polynomial: Move to math/ and clean up", v2. While removing Baikal SoC and platform code pieces I found that this code belongs to lib/math/ rather than generic lib/. Hence the move and followed up cleanups. This patch (of 3): The algorithm behind polynomial belongs to our collection of math equations and expressions handling. Move it to math/ subfolder where others of the kind are located. Link: https://lkml.kernel.org/r/20260302092831.2267785-2-andriy.shevchenko@linux.intel.com Signed-off-by: Andy Shevchenko Reviewed-by: Kuan-Wei Chiu Cc: Randy Dunlap Signed-off-by: Andrew Morton --- lib/Kconfig | 3 -- lib/Makefile | 2 - lib/math/Kconfig | 3 ++ lib/math/Makefile | 1 + lib/math/polynomial.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/polynomial.c | 108 -------------------------------------------------- 6 files changed, 112 insertions(+), 113 deletions(-) create mode 100644 lib/math/polynomial.c delete mode 100644 lib/polynomial.c (limited to 'lib') diff --git a/lib/Kconfig b/lib/Kconfig index 0f2fb9610647..2b0c56a53a2a 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -625,9 +625,6 @@ config PLDMFW config ASN1_ENCODER tristate -config POLYNOMIAL - tristate - config FIRMWARE_TABLE bool diff --git a/lib/Makefile b/lib/Makefile index 1b9ee167517f..60c9c9e79375 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -243,8 +243,6 @@ obj-$(CONFIG_MEMREGION) += memregion.o obj-$(CONFIG_STMP_DEVICE) += stmp_device.o obj-$(CONFIG_IRQ_POLL) += irq_poll.o -obj-$(CONFIG_POLYNOMIAL) += polynomial.o - # stackdepot.c should not be instrumented or call instrumented functions. # Prevent the compiler from calling builtins like memcmp() or bcmp() from this # file. diff --git a/lib/math/Kconfig b/lib/math/Kconfig index 0634b428d0cb..0e6d9cffc5d6 100644 --- a/lib/math/Kconfig +++ b/lib/math/Kconfig @@ -5,6 +5,9 @@ config CORDIC This option provides an implementation of the CORDIC algorithm; calculations are in fixed point. Module will be called cordic. +config POLYNOMIAL + tristate + config PRIME_NUMBERS tristate "Simple prime number generator for testing" help diff --git a/lib/math/Makefile b/lib/math/Makefile index d1caba23baa0..9a3850d55b79 100644 --- a/lib/math/Makefile +++ b/lib/math/Makefile @@ -2,6 +2,7 @@ obj-y += div64.o gcd.o lcm.o int_log.o int_pow.o int_sqrt.o reciprocal_div.o obj-$(CONFIG_CORDIC) += cordic.o +obj-$(CONFIG_POLYNOMIAL) += polynomial.o obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o obj-$(CONFIG_RATIONAL) += rational.o diff --git a/lib/math/polynomial.c b/lib/math/polynomial.c new file mode 100644 index 000000000000..66d383445fec --- /dev/null +++ b/lib/math/polynomial.c @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Generic polynomial calculation using integer coefficients. + * + * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC + * + * Authors: + * Maxim Kaurkin + * Serge Semin + * + */ + +#include +#include +#include + +/* + * Originally this was part of drivers/hwmon/bt1-pvt.c. + * There the following conversion is used and should serve as an example here: + * + * The original translation formulae of the temperature (in degrees of Celsius) + * to PVT data and vice-versa are following: + * + * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) + + * 1.7204e2 + * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) + + * 3.1020e-1*(N^1) - 4.838e1 + * + * where T = [-48.380, 147.438]C and N = [0, 1023]. + * + * They must be accordingly altered to be suitable for the integer arithmetics. + * The technique is called 'factor redistribution', which just makes sure the + * multiplications and divisions are made so to have a result of the operations + * within the integer numbers limit. In addition we need to translate the + * formulae to accept millidegrees of Celsius. Here what they look like after + * the alterations: + * + * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T + + * 17204e2) / 1e4 + * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D - + * 48380 + * where T = [-48380, 147438] mC and N = [0, 1023]. + * + * static const struct polynomial poly_temp_to_N = { + * .total_divider = 10000, + * .terms = { + * {4, 18322, 10000, 10000}, + * {3, 2343, 10000, 10}, + * {2, 87018, 10000, 10}, + * {1, 39269, 1000, 1}, + * {0, 1720400, 1, 1} + * } + * }; + * + * static const struct polynomial poly_N_to_temp = { + * .total_divider = 1, + * .terms = { + * {4, -16743, 1000, 1}, + * {3, 81542, 1000, 1}, + * {2, -182010, 1000, 1}, + * {1, 310200, 1000, 1}, + * {0, -48380, 1, 1} + * } + * }; + */ + +/** + * polynomial_calc - calculate a polynomial using integer arithmetic + * + * @poly: pointer to the descriptor of the polynomial + * @data: input value of the polynimal + * + * Calculate the result of a polynomial using only integer arithmetic. For + * this to work without too much loss of precision the coefficients has to + * be altered. This is called factor redistribution. + * + * Returns the result of the polynomial calculation. + */ +long polynomial_calc(const struct polynomial *poly, long data) +{ + const struct polynomial_term *term = poly->terms; + long total_divider = poly->total_divider ?: 1; + long tmp, ret = 0; + int deg; + + /* + * Here is the polynomial calculation function, which performs the + * redistributed terms calculations. It's pretty straightforward. + * We walk over each degree term up to the free one, and perform + * the redistributed multiplication of the term coefficient, its + * divider (as for the rationale fraction representation), data + * power and the rational fraction divider leftover. Then all of + * this is collected in a total sum variable, which value is + * normalized by the total divider before being returned. + */ + do { + tmp = term->coef; + for (deg = 0; deg < term->deg; ++deg) + tmp = mult_frac(tmp, data, term->divider); + ret += tmp / term->divider_leftover; + } while ((term++)->deg); + + return ret / total_divider; +} +EXPORT_SYMBOL_GPL(polynomial_calc); + +MODULE_DESCRIPTION("Generic polynomial calculations"); +MODULE_LICENSE("GPL"); diff --git a/lib/polynomial.c b/lib/polynomial.c deleted file mode 100644 index 66d383445fec..000000000000 --- a/lib/polynomial.c +++ /dev/null @@ -1,108 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - * Generic polynomial calculation using integer coefficients. - * - * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC - * - * Authors: - * Maxim Kaurkin - * Serge Semin - * - */ - -#include -#include -#include - -/* - * Originally this was part of drivers/hwmon/bt1-pvt.c. - * There the following conversion is used and should serve as an example here: - * - * The original translation formulae of the temperature (in degrees of Celsius) - * to PVT data and vice-versa are following: - * - * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) + - * 1.7204e2 - * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) + - * 3.1020e-1*(N^1) - 4.838e1 - * - * where T = [-48.380, 147.438]C and N = [0, 1023]. - * - * They must be accordingly altered to be suitable for the integer arithmetics. - * The technique is called 'factor redistribution', which just makes sure the - * multiplications and divisions are made so to have a result of the operations - * within the integer numbers limit. In addition we need to translate the - * formulae to accept millidegrees of Celsius. Here what they look like after - * the alterations: - * - * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T + - * 17204e2) / 1e4 - * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D - - * 48380 - * where T = [-48380, 147438] mC and N = [0, 1023]. - * - * static const struct polynomial poly_temp_to_N = { - * .total_divider = 10000, - * .terms = { - * {4, 18322, 10000, 10000}, - * {3, 2343, 10000, 10}, - * {2, 87018, 10000, 10}, - * {1, 39269, 1000, 1}, - * {0, 1720400, 1, 1} - * } - * }; - * - * static const struct polynomial poly_N_to_temp = { - * .total_divider = 1, - * .terms = { - * {4, -16743, 1000, 1}, - * {3, 81542, 1000, 1}, - * {2, -182010, 1000, 1}, - * {1, 310200, 1000, 1}, - * {0, -48380, 1, 1} - * } - * }; - */ - -/** - * polynomial_calc - calculate a polynomial using integer arithmetic - * - * @poly: pointer to the descriptor of the polynomial - * @data: input value of the polynimal - * - * Calculate the result of a polynomial using only integer arithmetic. For - * this to work without too much loss of precision the coefficients has to - * be altered. This is called factor redistribution. - * - * Returns the result of the polynomial calculation. - */ -long polynomial_calc(const struct polynomial *poly, long data) -{ - const struct polynomial_term *term = poly->terms; - long total_divider = poly->total_divider ?: 1; - long tmp, ret = 0; - int deg; - - /* - * Here is the polynomial calculation function, which performs the - * redistributed terms calculations. It's pretty straightforward. - * We walk over each degree term up to the free one, and perform - * the redistributed multiplication of the term coefficient, its - * divider (as for the rationale fraction representation), data - * power and the rational fraction divider leftover. Then all of - * this is collected in a total sum variable, which value is - * normalized by the total divider before being returned. - */ - do { - tmp = term->coef; - for (deg = 0; deg < term->deg; ++deg) - tmp = mult_frac(tmp, data, term->divider); - ret += tmp / term->divider_leftover; - } while ((term++)->deg); - - return ret / total_divider; -} -EXPORT_SYMBOL_GPL(polynomial_calc); - -MODULE_DESCRIPTION("Generic polynomial calculations"); -MODULE_LICENSE("GPL"); -- cgit v1.2.3