-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oskar Schirmer
authored and
Linus Torvalds
committed
Jun 11, 2009
1 parent
849aa65
commit f054f4f
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
--- | ||
refs/heads/master: 9f322ad064f9210e7d472dfe77e702274d5c9dba | ||
refs/heads/master: 8759ef32d992fc6c0bcbe40fca7aa302190918a5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* rational fractions | ||
* | ||
* Copyright (C) 2009 emlix GmbH, Oskar Schirmer <os@emlix.com> | ||
* | ||
* helper functions when coping with rational numbers, | ||
* e.g. when calculating optimum numerator/denominator pairs for | ||
* pll configuration taking into account restricted register size | ||
*/ | ||
|
||
#ifndef _LINUX_RATIONAL_H | ||
#define _LINUX_RATIONAL_H | ||
|
||
void rational_best_approximation( | ||
unsigned long given_numerator, unsigned long given_denominator, | ||
unsigned long max_numerator, unsigned long max_denominator, | ||
unsigned long *best_numerator, unsigned long *best_denominator); | ||
|
||
#endif /* _LINUX_RATIONAL_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* rational fractions | ||
* | ||
* Copyright (C) 2009 emlix GmbH, Oskar Schirmer <os@emlix.com> | ||
* | ||
* helper functions when coping with rational numbers | ||
*/ | ||
|
||
#include <linux/rational.h> | ||
|
||
/* | ||
* calculate best rational approximation for a given fraction | ||
* taking into account restricted register size, e.g. to find | ||
* appropriate values for a pll with 5 bit denominator and | ||
* 8 bit numerator register fields, trying to set up with a | ||
* frequency ratio of 3.1415, one would say: | ||
* | ||
* rational_best_approximation(31415, 10000, | ||
* (1 << 8) - 1, (1 << 5) - 1, &n, &d); | ||
* | ||
* you may look at given_numerator as a fixed point number, | ||
* with the fractional part size described in given_denominator. | ||
* | ||
* for theoretical background, see: | ||
* http://en.wikipedia.org/wiki/Continued_fraction | ||
*/ | ||
|
||
void rational_best_approximation( | ||
unsigned long given_numerator, unsigned long given_denominator, | ||
unsigned long max_numerator, unsigned long max_denominator, | ||
unsigned long *best_numerator, unsigned long *best_denominator) | ||
{ | ||
unsigned long n, d, n0, d0, n1, d1; | ||
n = given_numerator; | ||
d = given_denominator; | ||
n0 = d1 = 0; | ||
n1 = d0 = 1; | ||
for (;;) { | ||
unsigned long t, a; | ||
if ((n1 > max_numerator) || (d1 > max_denominator)) { | ||
n1 = n0; | ||
d1 = d0; | ||
break; | ||
} | ||
if (d == 0) | ||
break; | ||
t = d; | ||
a = n / d; | ||
d = n % d; | ||
n = t; | ||
t = n0 + a * n1; | ||
n0 = n1; | ||
n1 = t; | ||
t = d0 + a * d1; | ||
d0 = d1; | ||
d1 = t; | ||
} | ||
*best_numerator = n1; | ||
*best_denominator = d1; | ||
} | ||
|
||
EXPORT_SYMBOL(rational_best_approximation); |