diff options
author | Lennart Poettering <lennart@poettering.net> | 2009-01-10 02:53:57 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2009-01-10 02:53:57 +0100 |
commit | c850aa0c5b5e856618f11021a3e2a338c625de67 (patch) | |
tree | 179bf64f144edbfecdc5e62857ec15a3e8ee9420 /src | |
parent | 98049fbf81fae18707281642f6ae5b23632bd068 (diff) |
Add new pa_reduce() and pa_gcd() functions
Diffstat (limited to 'src')
-rw-r--r-- | src/pulsecore/core-util.c | 24 | ||||
-rw-r--r-- | src/pulsecore/core-util.h | 3 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index dde34d7b..6f315666 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -2487,3 +2487,27 @@ pa_bool_t pa_in_valgrind(void) { return b > 1; } #endif + +unsigned pa_gcd(unsigned a, unsigned b) { + + while (b > 0) { + unsigned t = b; + b = a % b; + a = t; + } + + return a; +} + +void pa_reduce(unsigned *num, unsigned *den) { + + unsigned gcd = pa_gcd(*num, *den); + + if (gcd <= 0) + return; + + *num /= gcd; + *den /= gcd; + + pa_assert(pa_gcd(*num, *den) == 1); +} diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h index fd6ee896..d9fad11e 100644 --- a/src/pulsecore/core-util.h +++ b/src/pulsecore/core-util.h @@ -206,4 +206,7 @@ static inline pa_bool_t pa_in_valgrind(void) { } #endif +unsigned pa_gcd(unsigned a, unsigned b); +void pa_reduce(unsigned *num, unsigned *den); + #endif |