diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Kconfig | 3 | ||||
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/cpumask.c | 11 | ||||
-rw-r--r-- | lib/rational.c | 62 |
4 files changed, 68 insertions, 9 deletions
diff --git a/lib/Kconfig b/lib/Kconfig index 8ade0a7a91e0..9960be04cbbe 100644 --- a/lib/Kconfig +++ b/lib/Kconfig | |||
@@ -10,6 +10,9 @@ menu "Library routines" | |||
10 | config BITREVERSE | 10 | config BITREVERSE |
11 | tristate | 11 | tristate |
12 | 12 | ||
13 | config RATIONAL | ||
14 | boolean | ||
15 | |||
13 | config GENERIC_FIND_FIRST_BIT | 16 | config GENERIC_FIND_FIRST_BIT |
14 | bool | 17 | bool |
15 | 18 | ||
diff --git a/lib/Makefile b/lib/Makefile index 33a40e40e3ee..1f6edefebffe 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -50,6 +50,7 @@ ifneq ($(CONFIG_HAVE_DEC_LOCK),y) | |||
50 | endif | 50 | endif |
51 | 51 | ||
52 | obj-$(CONFIG_BITREVERSE) += bitrev.o | 52 | obj-$(CONFIG_BITREVERSE) += bitrev.o |
53 | obj-$(CONFIG_RATIONAL) += rational.o | ||
53 | obj-$(CONFIG_CRC_CCITT) += crc-ccitt.o | 54 | obj-$(CONFIG_CRC_CCITT) += crc-ccitt.o |
54 | obj-$(CONFIG_CRC16) += crc16.o | 55 | obj-$(CONFIG_CRC16) += crc16.o |
55 | obj-$(CONFIG_CRC_T10DIF)+= crc-t10dif.o | 56 | obj-$(CONFIG_CRC_T10DIF)+= crc-t10dif.o |
diff --git a/lib/cpumask.c b/lib/cpumask.c index eb23aaa0c7b8..7bb4142a502f 100644 --- a/lib/cpumask.c +++ b/lib/cpumask.c | |||
@@ -92,15 +92,8 @@ int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) | |||
92 | */ | 92 | */ |
93 | bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) | 93 | bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) |
94 | { | 94 | { |
95 | if (likely(slab_is_available())) | 95 | *mask = kmalloc_node(cpumask_size(), flags, node); |
96 | *mask = kmalloc_node(cpumask_size(), flags, node); | 96 | |
97 | else { | ||
98 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS | ||
99 | printk(KERN_ERR | ||
100 | "=> alloc_cpumask_var: kmalloc not available!\n"); | ||
101 | #endif | ||
102 | *mask = NULL; | ||
103 | } | ||
104 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS | 97 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS |
105 | if (!*mask) { | 98 | if (!*mask) { |
106 | printk(KERN_ERR "=> alloc_cpumask_var: failed!\n"); | 99 | printk(KERN_ERR "=> alloc_cpumask_var: failed!\n"); |
diff --git a/lib/rational.c b/lib/rational.c new file mode 100644 index 000000000000..b3c099b5478e --- /dev/null +++ b/lib/rational.c | |||
@@ -0,0 +1,62 @@ | |||
1 | /* | ||
2 | * rational fractions | ||
3 | * | ||
4 | * Copyright (C) 2009 emlix GmbH, Oskar Schirmer <os@emlix.com> | ||
5 | * | ||
6 | * helper functions when coping with rational numbers | ||
7 | */ | ||
8 | |||
9 | #include <linux/rational.h> | ||
10 | |||
11 | /* | ||
12 | * calculate best rational approximation for a given fraction | ||
13 | * taking into account restricted register size, e.g. to find | ||
14 | * appropriate values for a pll with 5 bit denominator and | ||
15 | * 8 bit numerator register fields, trying to set up with a | ||
16 | * frequency ratio of 3.1415, one would say: | ||
17 | * | ||
18 | * rational_best_approximation(31415, 10000, | ||
19 | * (1 << 8) - 1, (1 << 5) - 1, &n, &d); | ||
20 | * | ||
21 | * you may look at given_numerator as a fixed point number, | ||
22 | * with the fractional part size described in given_denominator. | ||
23 | * | ||
24 | * for theoretical background, see: | ||
25 | * http://en.wikipedia.org/wiki/Continued_fraction | ||
26 | */ | ||
27 | |||
28 | void rational_best_approximation( | ||
29 | unsigned long given_numerator, unsigned long given_denominator, | ||
30 | unsigned long max_numerator, unsigned long max_denominator, | ||
31 | unsigned long *best_numerator, unsigned long *best_denominator) | ||
32 | { | ||
33 | unsigned long n, d, n0, d0, n1, d1; | ||
34 | n = given_numerator; | ||
35 | d = given_denominator; | ||
36 | n0 = d1 = 0; | ||
37 | n1 = d0 = 1; | ||
38 | for (;;) { | ||
39 | unsigned long t, a; | ||
40 | if ((n1 > max_numerator) || (d1 > max_denominator)) { | ||
41 | n1 = n0; | ||
42 | d1 = d0; | ||
43 | break; | ||
44 | } | ||
45 | if (d == 0) | ||
46 | break; | ||
47 | t = d; | ||
48 | a = n / d; | ||
49 | d = n % d; | ||
50 | n = t; | ||
51 | t = n0 + a * n1; | ||
52 | n0 = n1; | ||
53 | n1 = t; | ||
54 | t = d0 + a * d1; | ||
55 | d0 = d1; | ||
56 | d1 = t; | ||
57 | } | ||
58 | *best_numerator = n1; | ||
59 | *best_denominator = d1; | ||
60 | } | ||
61 | |||
62 | EXPORT_SYMBOL(rational_best_approximation); | ||