rational.c 2.9 KB

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