1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic polynomial calculation using integer coefficients. 4 * 5 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC 6 * 7 * Authors: 8 * Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru> 9 * Serge Semin <Sergey.Semin@baikalelectronics.ru> 10 * 11 */ 12 13 #include <linux/export.h> 14 #include <linux/math.h> 15 #include <linux/module.h> 16 #include <linux/polynomial.h> 17 18 /* 19 * Originally this was part of drivers/hwmon/bt1-pvt.c. 20 * There the following conversion is used and should serve as an example here: 21 * 22 * The original translation formulae of the temperature (in degrees of Celsius) 23 * to PVT data and vice-versa are following: 24 * 25 * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) + 26 * 1.7204e2 27 * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) + 28 * 3.1020e-1*(N^1) - 4.838e1 29 * 30 * where T = [-48.380, 147.438]C and N = [0, 1023]. 31 * 32 * They must be accordingly altered to be suitable for the integer arithmetics. 33 * The technique is called 'factor redistribution', which just makes sure the 34 * multiplications and divisions are made so to have a result of the operations 35 * within the integer numbers limit. In addition we need to translate the 36 * formulae to accept millidegrees of Celsius. Here what they look like after 37 * the alterations: 38 * 39 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T + 40 * 17204e2) / 1e4 41 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D - 42 * 48380 43 * where T = [-48380, 147438] mC and N = [0, 1023]. 44 * 45 * static const struct polynomial poly_temp_to_N = { 46 * .total_divider = 10000, 47 * .terms = { 48 * {4, 18322, 10000, 10000}, 49 * {3, 2343, 10000, 10}, 50 * {2, 87018, 10000, 10}, 51 * {1, 39269, 1000, 1}, 52 * {0, 1720400, 1, 1} 53 * } 54 * }; 55 * 56 * static const struct polynomial poly_N_to_temp = { 57 * .total_divider = 1, 58 * .terms = { 59 * {4, -16743, 1000, 1}, 60 * {3, 81542, 1000, 1}, 61 * {2, -182010, 1000, 1}, 62 * {1, 310200, 1000, 1}, 63 * {0, -48380, 1, 1} 64 * } 65 * }; 66 */ 67 68 /** 69 * polynomial_calc - calculate a polynomial using integer arithmetic 70 * 71 * @poly: pointer to the descriptor of the polynomial 72 * @data: input value of the polynimal 73 * 74 * Calculate the result of a polynomial using only integer arithmetic. For 75 * this to work without too much loss of precision the coefficients has to 76 * be altered. This is called factor redistribution. 77 * 78 * Returns the result of the polynomial calculation. 79 */ 80 long polynomial_calc(const struct polynomial *poly, long data) 81 { 82 const struct polynomial_term *term = poly->terms; 83 long total_divider = poly->total_divider ?: 1; 84 long tmp, ret = 0; 85 int deg; 86 87 /* 88 * Here is the polynomial calculation function, which performs the 89 * redistributed terms calculations. It's pretty straightforward. 90 * We walk over each degree term up to the free one, and perform 91 * the redistributed multiplication of the term coefficient, its 92 * divider (as for the rationale fraction representation), data 93 * power and the rational fraction divider leftover. Then all of 94 * this is collected in a total sum variable, which value is 95 * normalized by the total divider before being returned. 96 */ 97 do { 98 tmp = term->coef; 99 for (deg = 0; deg < term->deg; ++deg) 100 tmp = mult_frac(tmp, data, term->divider); 101 ret += tmp / term->divider_leftover; 102 } while ((term++)->deg); 103 104 return ret / total_divider; 105 } 106 EXPORT_SYMBOL_GPL(polynomial_calc); 107 108 MODULE_DESCRIPTION("Generic polynomial calculations"); 109 MODULE_LICENSE("GPL"); 110