blob: ec59d426ea638b0f47dc2631d388c25de15183ee [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 Piepho65a0d3c2021-06-30 18:55:49 -070015#include <linux/limits.h>
Geert Uytterhoevenbcda5fd2021-09-07 19:58:33 -070016#include <linux/module.h>
Oskar Schirmer8759ef32009-06-11 14:51:15 +010017
18/*
19 * calculate best rational approximation for a given fraction
20 * taking into account restricted register size, e.g. to find
21 * appropriate values for a pll with 5 bit denominator and
22 * 8 bit numerator register fields, trying to set up with a
23 * frequency ratio of 3.1415, one would say:
24 *
25 * rational_best_approximation(31415, 10000,
26 * (1 << 8) - 1, (1 << 5) - 1, &n, &d);
27 *
28 * you may look at given_numerator as a fixed point number,
29 * with the fractional part size described in given_denominator.
30 *
31 * for theoretical background, see:
Alexander A. Klimovd89775f2020-08-11 18:34:50 -070032 * https://en.wikipedia.org/wiki/Continued_fraction
Oskar Schirmer8759ef32009-06-11 14:51:15 +010033 */
34
35void rational_best_approximation(
36 unsigned long given_numerator, unsigned long given_denominator,
37 unsigned long max_numerator, unsigned long max_denominator,
38 unsigned long *best_numerator, unsigned long *best_denominator)
39{
Trent Piepho323dd2c2019-12-04 16:51:57 -080040 /* n/d is the starting rational, which is continually
41 * decreased each iteration using the Euclidean algorithm.
42 *
43 * dp is the value of d from the prior iteration.
44 *
45 * n2/d2, n1/d1, and n0/d0 are our successively more accurate
46 * approximations of the rational. They are, respectively,
47 * the current, previous, and two prior iterations of it.
48 *
49 * a is current term of the continued fraction.
50 */
51 unsigned long n, d, n0, d0, n1, d1, n2, d2;
Oskar Schirmer8759ef32009-06-11 14:51:15 +010052 n = given_numerator;
53 d = given_denominator;
54 n0 = d1 = 0;
55 n1 = d0 = 1;
Trent Piepho323dd2c2019-12-04 16:51:57 -080056
Oskar Schirmer8759ef32009-06-11 14:51:15 +010057 for (;;) {
Trent Piepho323dd2c2019-12-04 16:51:57 -080058 unsigned long dp, a;
59
Oskar Schirmer8759ef32009-06-11 14:51:15 +010060 if (d == 0)
61 break;
Trent Piepho323dd2c2019-12-04 16:51:57 -080062 /* Find next term in continued fraction, 'a', via
63 * Euclidean algorithm.
64 */
65 dp = d;
Oskar Schirmer8759ef32009-06-11 14:51:15 +010066 a = n / d;
67 d = n % d;
Trent Piepho323dd2c2019-12-04 16:51:57 -080068 n = dp;
69
70 /* Calculate the current rational approximation (aka
71 * convergent), n2/d2, using the term just found and
72 * the two prior approximations.
73 */
74 n2 = n0 + a * n1;
75 d2 = d0 + a * d1;
76
77 /* If the current convergent exceeds the maxes, then
78 * return either the previous convergent or the
79 * largest semi-convergent, the final term of which is
80 * found below as 't'.
81 */
82 if ((n2 > max_numerator) || (d2 > max_denominator)) {
Trent Piepho65a0d3c2021-06-30 18:55:49 -070083 unsigned long t = ULONG_MAX;
Trent Piepho323dd2c2019-12-04 16:51:57 -080084
Trent Piepho65a0d3c2021-06-30 18:55:49 -070085 if (d1)
86 t = (max_denominator - d0) / d1;
87 if (n1)
88 t = min(t, (max_numerator - n0) / n1);
89
90 /* This tests if the semi-convergent is closer than the previous
91 * convergent. If d1 is zero there is no previous convergent as this
92 * is the 1st iteration, so always choose the semi-convergent.
Trent Piepho323dd2c2019-12-04 16:51:57 -080093 */
Trent Piepho65a0d3c2021-06-30 18:55:49 -070094 if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
Trent Piepho323dd2c2019-12-04 16:51:57 -080095 n1 = n0 + t * n1;
96 d1 = d0 + t * d1;
97 }
98 break;
99 }
Oskar Schirmer8759ef32009-06-11 14:51:15 +0100100 n0 = n1;
Trent Piepho323dd2c2019-12-04 16:51:57 -0800101 n1 = n2;
Oskar Schirmer8759ef32009-06-11 14:51:15 +0100102 d0 = d1;
Trent Piepho323dd2c2019-12-04 16:51:57 -0800103 d1 = d2;
Oskar Schirmer8759ef32009-06-11 14:51:15 +0100104 }
105 *best_numerator = n1;
106 *best_denominator = d1;
107}
108
109EXPORT_SYMBOL(rational_best_approximation);
Geert Uytterhoevenbcda5fd2021-09-07 19:58:33 -0700110
111MODULE_LICENSE("GPL v2");