mirror of
https://github.com/adulau/aha.git
synced 2024-12-27 03:06:10 +00:00
lib: isolate rational fractions helper function
Provide a helper function to determine optimum numerator denominator value pairs taking into account restricted register size. Useful especially with PLL and other clock configurations. Signed-off-by: Oskar Schirmer <os@emlix.com> Signed-off-by: Alan Cox <alan@linux.intel.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
9f322ad064
commit
8759ef32d9
4 changed files with 85 additions and 0 deletions
19
include/linux/rational.h
Normal file
19
include/linux/rational.h
Normal file
|
@ -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 */
|
|
@ -10,6 +10,9 @@ menu "Library routines"
|
|||
config BITREVERSE
|
||||
tristate
|
||||
|
||||
config RATIONAL
|
||||
boolean
|
||||
|
||||
config GENERIC_FIND_FIRST_BIT
|
||||
bool
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ ifneq ($(CONFIG_HAVE_DEC_LOCK),y)
|
|||
endif
|
||||
|
||||
obj-$(CONFIG_BITREVERSE) += bitrev.o
|
||||
obj-$(CONFIG_RATIONAL) += rational.o
|
||||
obj-$(CONFIG_CRC_CCITT) += crc-ccitt.o
|
||||
obj-$(CONFIG_CRC16) += crc16.o
|
||||
obj-$(CONFIG_CRC_T10DIF)+= crc-t10dif.o
|
||||
|
|
62
lib/rational.c
Normal file
62
lib/rational.c
Normal file
|
@ -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);
|
Loading…
Reference in a new issue