rational.c 3.0 KB

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