162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * rational fractions 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2009 emlix GmbH, Oskar Schirmer <oskar@scara.com> 662306a36Sopenharmony_ci * Copyright (C) 2019 Trent Piepho <tpiepho@gmail.com> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * helper functions when coping with rational numbers 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/rational.h> 1262306a36Sopenharmony_ci#include <linux/compiler.h> 1362306a36Sopenharmony_ci#include <linux/export.h> 1462306a36Sopenharmony_ci#include <linux/minmax.h> 1562306a36Sopenharmony_ci#include <linux/limits.h> 1662306a36Sopenharmony_ci#include <linux/module.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci/* 1962306a36Sopenharmony_ci * calculate best rational approximation for a given fraction 2062306a36Sopenharmony_ci * taking into account restricted register size, e.g. to find 2162306a36Sopenharmony_ci * appropriate values for a pll with 5 bit denominator and 2262306a36Sopenharmony_ci * 8 bit numerator register fields, trying to set up with a 2362306a36Sopenharmony_ci * frequency ratio of 3.1415, one would say: 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci * rational_best_approximation(31415, 10000, 2662306a36Sopenharmony_ci * (1 << 8) - 1, (1 << 5) - 1, &n, &d); 2762306a36Sopenharmony_ci * 2862306a36Sopenharmony_ci * you may look at given_numerator as a fixed point number, 2962306a36Sopenharmony_ci * with the fractional part size described in given_denominator. 3062306a36Sopenharmony_ci * 3162306a36Sopenharmony_ci * for theoretical background, see: 3262306a36Sopenharmony_ci * https://en.wikipedia.org/wiki/Continued_fraction 3362306a36Sopenharmony_ci */ 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_civoid rational_best_approximation( 3662306a36Sopenharmony_ci unsigned long given_numerator, unsigned long given_denominator, 3762306a36Sopenharmony_ci unsigned long max_numerator, unsigned long max_denominator, 3862306a36Sopenharmony_ci unsigned long *best_numerator, unsigned long *best_denominator) 3962306a36Sopenharmony_ci{ 4062306a36Sopenharmony_ci /* n/d is the starting rational, which is continually 4162306a36Sopenharmony_ci * decreased each iteration using the Euclidean algorithm. 4262306a36Sopenharmony_ci * 4362306a36Sopenharmony_ci * dp is the value of d from the prior iteration. 4462306a36Sopenharmony_ci * 4562306a36Sopenharmony_ci * n2/d2, n1/d1, and n0/d0 are our successively more accurate 4662306a36Sopenharmony_ci * approximations of the rational. They are, respectively, 4762306a36Sopenharmony_ci * the current, previous, and two prior iterations of it. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * a is current term of the continued fraction. 5062306a36Sopenharmony_ci */ 5162306a36Sopenharmony_ci unsigned long n, d, n0, d0, n1, d1, n2, d2; 5262306a36Sopenharmony_ci n = given_numerator; 5362306a36Sopenharmony_ci d = given_denominator; 5462306a36Sopenharmony_ci n0 = d1 = 0; 5562306a36Sopenharmony_ci n1 = d0 = 1; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci for (;;) { 5862306a36Sopenharmony_ci unsigned long dp, a; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci if (d == 0) 6162306a36Sopenharmony_ci break; 6262306a36Sopenharmony_ci /* Find next term in continued fraction, 'a', via 6362306a36Sopenharmony_ci * Euclidean algorithm. 6462306a36Sopenharmony_ci */ 6562306a36Sopenharmony_ci dp = d; 6662306a36Sopenharmony_ci a = n / d; 6762306a36Sopenharmony_ci d = n % d; 6862306a36Sopenharmony_ci n = dp; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci /* Calculate the current rational approximation (aka 7162306a36Sopenharmony_ci * convergent), n2/d2, using the term just found and 7262306a36Sopenharmony_ci * the two prior approximations. 7362306a36Sopenharmony_ci */ 7462306a36Sopenharmony_ci n2 = n0 + a * n1; 7562306a36Sopenharmony_ci d2 = d0 + a * d1; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci /* If the current convergent exceeds the maxes, then 7862306a36Sopenharmony_ci * return either the previous convergent or the 7962306a36Sopenharmony_ci * largest semi-convergent, the final term of which is 8062306a36Sopenharmony_ci * found below as 't'. 8162306a36Sopenharmony_ci */ 8262306a36Sopenharmony_ci if ((n2 > max_numerator) || (d2 > max_denominator)) { 8362306a36Sopenharmony_ci unsigned long t = ULONG_MAX; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci if (d1) 8662306a36Sopenharmony_ci t = (max_denominator - d0) / d1; 8762306a36Sopenharmony_ci if (n1) 8862306a36Sopenharmony_ci t = min(t, (max_numerator - n0) / n1); 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci /* This tests if the semi-convergent is closer than the previous 9162306a36Sopenharmony_ci * convergent. If d1 is zero there is no previous convergent as this 9262306a36Sopenharmony_ci * is the 1st iteration, so always choose the semi-convergent. 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_ci if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) { 9562306a36Sopenharmony_ci n1 = n0 + t * n1; 9662306a36Sopenharmony_ci d1 = d0 + t * d1; 9762306a36Sopenharmony_ci } 9862306a36Sopenharmony_ci break; 9962306a36Sopenharmony_ci } 10062306a36Sopenharmony_ci n0 = n1; 10162306a36Sopenharmony_ci n1 = n2; 10262306a36Sopenharmony_ci d0 = d1; 10362306a36Sopenharmony_ci d1 = d2; 10462306a36Sopenharmony_ci } 10562306a36Sopenharmony_ci *best_numerator = n1; 10662306a36Sopenharmony_ci *best_denominator = d1; 10762306a36Sopenharmony_ci} 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ciEXPORT_SYMBOL(rational_best_approximation); 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 112