blob: c0ab51d8fbb98d17514ae63bf2b700d40f3cb752 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Oskar Schirmer8759ef32009-06-11 14:51:15 +01002/*
3 * rational fractions
4 *
Oskar Schirmer6684b572012-05-16 09:41:19 +00005 * Copyright (C) 2009 emlix GmbH, Oskar Schirmer <oskar@scara.com>
Trent Piepho323dd2c2019-12-04 16:51:57 -08006 * Copyright (C) 2019 Trent Piepho <tpiepho@gmail.com>
Oskar Schirmer8759ef32009-06-11 14:51:15 +01007 *
8 * helper functions when coping with rational numbers
9 */
10
11#include <linux/rational.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050012#include <linux/compiler.h>
13#include <linux/export.h>
Andy Shevchenkob296a6d2020-10-15 20:10:21 -070014#include <linux/minmax.h>
Trent Piepho45655402021-06-30 18:55:49 -070015#include <linux/limits.h>
Oskar Schirmer8759ef32009-06-11 14:51:15 +010016
17/*
18 * calculate best rational approximation for a given fraction
19 * taking into account restricted register size, e.g. to find
20 * appropriate values for a pll with 5 bit denominator and
21 * 8 bit numerator register fields, trying to set up with a
22 * frequency ratio of 3.1415, one would say:
23 *
24 * rational_best_approximation(31415, 10000,
25 * (1 << 8) - 1, (1 << 5) - 1, &n, &d);
26 *
27 * you may look at given_numerator as a fixed point number,
28 * with the fractional part size described in given_denominator.
29 *
30 * for theoretical background, see:
Alexander A. Klimovd89775f2020-08-11 18:34:50 -070031 * https://en.wikipedia.org/wiki/Continued_fraction
Oskar Schirmer8759ef32009-06-11 14:51:15 +010032 */
33
34void rational_best_approximation(
35 unsigned long given_numerator, unsigned long given_denominator,
36 unsigned long max_numerator, unsigned long max_denominator,
37 unsigned long *best_numerator, unsigned long *best_denominator)
38{
Trent Piepho323dd2c2019-12-04 16:51:57 -080039 /* n/d is the starting rational, which is continually
40 * decreased each iteration using the Euclidean algorithm.
41 *
42 * dp is the value of d from the prior iteration.
43 *
44 * n2/d2, n1/d1, and n0/d0 are our successively more accurate
45 * approximations of the rational. They are, respectively,
46 * the current, previous, and two prior iterations of it.
47 *
48 * a is current term of the continued fraction.
49 */
50 unsigned long n, d, n0, d0, n1, d1, n2, d2;
Oskar Schirmer8759ef32009-06-11 14:51:15 +010051 n = given_numerator;
52 d = given_denominator;
53 n0 = d1 = 0;
54 n1 = d0 = 1;
Trent Piepho323dd2c2019-12-04 16:51:57 -080055
Oskar Schirmer8759ef32009-06-11 14:51:15 +010056 for (;;) {
Trent Piepho323dd2c2019-12-04 16:51:57 -080057 unsigned long dp, a;
58
Oskar Schirmer8759ef32009-06-11 14:51:15 +010059 if (d == 0)
60 break;
Trent Piepho323dd2c2019-12-04 16:51:57 -080061 /* Find next term in continued fraction, 'a', via
62 * Euclidean algorithm.
63 */
64 dp = d;
Oskar Schirmer8759ef32009-06-11 14:51:15 +010065 a = n / d;
66 d = n % d;
Trent Piepho323dd2c2019-12-04 16:51:57 -080067 n = dp;
68
69 /* Calculate the current rational approximation (aka
70 * convergent), n2/d2, using the term just found and
71 * the two prior approximations.
72 */
73 n2 = n0 + a * n1;
74 d2 = d0 + a * d1;
75
76 /* If the current convergent exceeds the maxes, then
77 * return either the previous convergent or the
78 * largest semi-convergent, the final term of which is
79 * found below as 't'.
80 */
81 if ((n2 > max_numerator) || (d2 > max_denominator)) {
Trent Piepho45655402021-06-30 18:55:49 -070082 unsigned long t = ULONG_MAX;
Trent Piepho323dd2c2019-12-04 16:51:57 -080083
Trent Piepho45655402021-06-30 18:55:49 -070084 if (d1)
85 t = (max_denominator - d0) / d1;
86 if (n1)
87 t = min(t, (max_numerator - n0) / n1);
88
89 /* This tests if the semi-convergent is closer than the previous
90 * convergent. If d1 is zero there is no previous convergent as this
91 * is the 1st iteration, so always choose the semi-convergent.
Trent Piepho323dd2c2019-12-04 16:51:57 -080092 */
Trent Piepho45655402021-06-30 18:55:49 -070093 if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
Trent Piepho323dd2c2019-12-04 16:51:57 -080094 n1 = n0 + t * n1;
95 d1 = d0 + t * d1;
96 }
97 break;
98 }
Oskar Schirmer8759ef32009-06-11 14:51:15 +010099 n0 = n1;
Trent Piepho323dd2c2019-12-04 16:51:57 -0800100 n1 = n2;
Oskar Schirmer8759ef32009-06-11 14:51:15 +0100101 d0 = d1;
Trent Piepho323dd2c2019-12-04 16:51:57 -0800102 d1 = d2;
Oskar Schirmer8759ef32009-06-11 14:51:15 +0100103 }
104 *best_numerator = n1;
105 *best_denominator = d1;
106}
107
108EXPORT_SYMBOL(rational_best_approximation);