![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0-only 0002 /* 0003 * Generic polynomial calculation using integer coefficients. 0004 * 0005 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC 0006 * 0007 * Authors: 0008 * Maxim Kaurkin <maxim.kaurkin@baikalelectronics.ru> 0009 * Serge Semin <Sergey.Semin@baikalelectronics.ru> 0010 * 0011 */ 0012 0013 #include <linux/kernel.h> 0014 #include <linux/module.h> 0015 #include <linux/polynomial.h> 0016 0017 /* 0018 * Originally this was part of drivers/hwmon/bt1-pvt.c. 0019 * There the following conversion is used and should serve as an example here: 0020 * 0021 * The original translation formulae of the temperature (in degrees of Celsius) 0022 * to PVT data and vice-versa are following: 0023 * 0024 * N = 1.8322e-8*(T^4) + 2.343e-5*(T^3) + 8.7018e-3*(T^2) + 3.9269*(T^1) + 0025 * 1.7204e2 0026 * T = -1.6743e-11*(N^4) + 8.1542e-8*(N^3) + -1.8201e-4*(N^2) + 0027 * 3.1020e-1*(N^1) - 4.838e1 0028 * 0029 * where T = [-48.380, 147.438]C and N = [0, 1023]. 0030 * 0031 * They must be accordingly altered to be suitable for the integer arithmetics. 0032 * The technique is called 'factor redistribution', which just makes sure the 0033 * multiplications and divisions are made so to have a result of the operations 0034 * within the integer numbers limit. In addition we need to translate the 0035 * formulae to accept millidegrees of Celsius. Here what they look like after 0036 * the alterations: 0037 * 0038 * N = (18322e-20*(T^4) + 2343e-13*(T^3) + 87018e-9*(T^2) + 39269e-3*T + 0039 * 17204e2) / 1e4 0040 * T = -16743e-12*(D^4) + 81542e-9*(D^3) - 182010e-6*(D^2) + 310200e-3*D - 0041 * 48380 0042 * where T = [-48380, 147438] mC and N = [0, 1023]. 0043 * 0044 * static const struct polynomial poly_temp_to_N = { 0045 * .total_divider = 10000, 0046 * .terms = { 0047 * {4, 18322, 10000, 10000}, 0048 * {3, 2343, 10000, 10}, 0049 * {2, 87018, 10000, 10}, 0050 * {1, 39269, 1000, 1}, 0051 * {0, 1720400, 1, 1} 0052 * } 0053 * }; 0054 * 0055 * static const struct polynomial poly_N_to_temp = { 0056 * .total_divider = 1, 0057 * .terms = { 0058 * {4, -16743, 1000, 1}, 0059 * {3, 81542, 1000, 1}, 0060 * {2, -182010, 1000, 1}, 0061 * {1, 310200, 1000, 1}, 0062 * {0, -48380, 1, 1} 0063 * } 0064 * }; 0065 */ 0066 0067 /** 0068 * polynomial_calc - calculate a polynomial using integer arithmetic 0069 * 0070 * @poly: pointer to the descriptor of the polynomial 0071 * @data: input value of the polynimal 0072 * 0073 * Calculate the result of a polynomial using only integer arithmetic. For 0074 * this to work without too much loss of precision the coefficients has to 0075 * be altered. This is called factor redistribution. 0076 * 0077 * Returns the result of the polynomial calculation. 0078 */ 0079 long polynomial_calc(const struct polynomial *poly, long data) 0080 { 0081 const struct polynomial_term *term = poly->terms; 0082 long total_divider = poly->total_divider ?: 1; 0083 long tmp, ret = 0; 0084 int deg; 0085 0086 /* 0087 * Here is the polynomial calculation function, which performs the 0088 * redistributed terms calculations. It's pretty straightforward. 0089 * We walk over each degree term up to the free one, and perform 0090 * the redistributed multiplication of the term coefficient, its 0091 * divider (as for the rationale fraction representation), data 0092 * power and the rational fraction divider leftover. Then all of 0093 * this is collected in a total sum variable, which value is 0094 * normalized by the total divider before being returned. 0095 */ 0096 do { 0097 tmp = term->coef; 0098 for (deg = 0; deg < term->deg; ++deg) 0099 tmp = mult_frac(tmp, data, term->divider); 0100 ret += tmp / term->divider_leftover; 0101 } while ((term++)->deg); 0102 0103 return ret / total_divider; 0104 } 0105 EXPORT_SYMBOL_GPL(polynomial_calc); 0106 0107 MODULE_DESCRIPTION("Generic polynomial calculations"); 0108 MODULE_LICENSE("GPL");
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |