1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * rational fractions 3 * rational fractions 4 * 4 * 5 * Copyright (C) 2009 emlix GmbH, Oskar Schirm 5 * Copyright (C) 2009 emlix GmbH, Oskar Schirmer <oskar@scara.com> 6 * Copyright (C) 2019 Trent Piepho <tpiepho@gm 6 * Copyright (C) 2019 Trent Piepho <tpiepho@gmail.com> 7 * 7 * 8 * helper functions when coping with rational 8 * helper functions when coping with rational numbers 9 */ 9 */ 10 10 11 #include <linux/rational.h> 11 #include <linux/rational.h> 12 #include <linux/compiler.h> 12 #include <linux/compiler.h> 13 #include <linux/export.h> 13 #include <linux/export.h> 14 #include <linux/minmax.h> !! 14 #include <linux/kernel.h> 15 #include <linux/limits.h> << 16 #include <linux/module.h> << 17 15 18 /* 16 /* 19 * calculate best rational approximation for a 17 * calculate best rational approximation for a given fraction 20 * taking into account restricted register siz 18 * taking into account restricted register size, e.g. to find 21 * appropriate values for a pll with 5 bit den 19 * appropriate values for a pll with 5 bit denominator and 22 * 8 bit numerator register fields, trying to 20 * 8 bit numerator register fields, trying to set up with a 23 * frequency ratio of 3.1415, one would say: 21 * frequency ratio of 3.1415, one would say: 24 * 22 * 25 * rational_best_approximation(31415, 10000, 23 * rational_best_approximation(31415, 10000, 26 * (1 << 8) - 1, (1 << 5) - 1, &n 24 * (1 << 8) - 1, (1 << 5) - 1, &n, &d); 27 * 25 * 28 * you may look at given_numerator as a fixed 26 * you may look at given_numerator as a fixed point number, 29 * with the fractional part size described in 27 * with the fractional part size described in given_denominator. 30 * 28 * 31 * for theoretical background, see: 29 * for theoretical background, see: 32 * https://en.wikipedia.org/wiki/Continued_fra !! 30 * http://en.wikipedia.org/wiki/Continued_fraction 33 */ 31 */ 34 32 35 void rational_best_approximation( 33 void rational_best_approximation( 36 unsigned long given_numerator, unsigne 34 unsigned long given_numerator, unsigned long given_denominator, 37 unsigned long max_numerator, unsigned 35 unsigned long max_numerator, unsigned long max_denominator, 38 unsigned long *best_numerator, unsigne 36 unsigned long *best_numerator, unsigned long *best_denominator) 39 { 37 { 40 /* n/d is the starting rational, which 38 /* n/d is the starting rational, which is continually 41 * decreased each iteration using the 39 * decreased each iteration using the Euclidean algorithm. 42 * 40 * 43 * dp is the value of d from the prior 41 * dp is the value of d from the prior iteration. 44 * 42 * 45 * n2/d2, n1/d1, and n0/d0 are our suc 43 * n2/d2, n1/d1, and n0/d0 are our successively more accurate 46 * approximations of the rational. Th 44 * approximations of the rational. They are, respectively, 47 * the current, previous, and two prio 45 * the current, previous, and two prior iterations of it. 48 * 46 * 49 * a is current term of the continued 47 * a is current term of the continued fraction. 50 */ 48 */ 51 unsigned long n, d, n0, d0, n1, d1, n2 49 unsigned long n, d, n0, d0, n1, d1, n2, d2; 52 n = given_numerator; 50 n = given_numerator; 53 d = given_denominator; 51 d = given_denominator; 54 n0 = d1 = 0; 52 n0 = d1 = 0; 55 n1 = d0 = 1; 53 n1 = d0 = 1; 56 54 57 for (;;) { 55 for (;;) { 58 unsigned long dp, a; 56 unsigned long dp, a; 59 57 60 if (d == 0) 58 if (d == 0) 61 break; 59 break; 62 /* Find next term in continued 60 /* Find next term in continued fraction, 'a', via 63 * Euclidean algorithm. 61 * Euclidean algorithm. 64 */ 62 */ 65 dp = d; 63 dp = d; 66 a = n / d; 64 a = n / d; 67 d = n % d; 65 d = n % d; 68 n = dp; 66 n = dp; 69 67 70 /* Calculate the current ratio 68 /* Calculate the current rational approximation (aka 71 * convergent), n2/d2, using t 69 * convergent), n2/d2, using the term just found and 72 * the two prior approximation 70 * the two prior approximations. 73 */ 71 */ 74 n2 = n0 + a * n1; 72 n2 = n0 + a * n1; 75 d2 = d0 + a * d1; 73 d2 = d0 + a * d1; 76 74 77 /* If the current convergent e 75 /* If the current convergent exceeds the maxes, then 78 * return either the previous 76 * return either the previous convergent or the 79 * largest semi-convergent, th 77 * largest semi-convergent, the final term of which is 80 * found below as 't'. 78 * found below as 't'. 81 */ 79 */ 82 if ((n2 > max_numerator) || (d 80 if ((n2 > max_numerator) || (d2 > max_denominator)) { 83 unsigned long t = ULON !! 81 unsigned long t = min((max_numerator - n0) / n1, >> 82 (max_denominator - d0) / d1); 84 83 85 if (d1) !! 84 /* This tests if the semi-convergent is closer 86 t = (max_denom !! 85 * than the previous convergent. 87 if (n1) << 88 t = min(t, (ma << 89 << 90 /* This tests if the s << 91 * convergent. If d1 << 92 * is the 1st iteratio << 93 */ 86 */ 94 if (!d1 || 2u * t > a !! 87 if (2u * t > a || (2u * t == a && d0 * dp > d1 * d)) { 95 n1 = n0 + t * 88 n1 = n0 + t * n1; 96 d1 = d0 + t * 89 d1 = d0 + t * d1; 97 } 90 } 98 break; 91 break; 99 } 92 } 100 n0 = n1; 93 n0 = n1; 101 n1 = n2; 94 n1 = n2; 102 d0 = d1; 95 d0 = d1; 103 d1 = d2; 96 d1 = d2; 104 } 97 } 105 *best_numerator = n1; 98 *best_numerator = n1; 106 *best_denominator = d1; 99 *best_denominator = d1; 107 } 100 } 108 101 109 EXPORT_SYMBOL(rational_best_approximation); 102 EXPORT_SYMBOL(rational_best_approximation); 110 << 111 MODULE_DESCRIPTION("Rational fraction support << 112 MODULE_LICENSE("GPL v2"); << 113 103
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.