rational.c 2.7 KB

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