diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-07-26 11:23:34 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-07-26 11:23:34 -0400 |
commit | 09c25697d74cdfdefe5fa365e4691cc9401e2129 (patch) | |
tree | a7a45a6d71af53d971fa94581ad5d75ceefedc79 /src/common/util.c | |
parent | 90ca4460489bbc92a8de8f32b8aa4bb82416dcbb (diff) | |
download | tor-09c25697d74cdfdefe5fa365e4691cc9401e2129.tar.gz tor-09c25697d74cdfdefe5fa365e4691cc9401e2129.zip |
Add a function to simplify a fraction.
Apparently remembering euclid's algorithm does pay off sooner or later.
Diffstat (limited to 'src/common/util.c')
-rw-r--r-- | src/common/util.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c index d9361563ef..5a73098b91 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -580,6 +580,29 @@ add_laplace_noise(int64_t signal, double random, double delta_f, return signal + noise; } +/* Helper: return greatest common divisor of a,b */ +static uint64_t +gcd64(uint64_t a, uint64_t b) +{ + while (b) { + uint64_t t = b; + b = a % b; + a = t; + } + return a; +} + +/* Given a fraction *<b>numer</b> / *<b>denom</b>, simplify it. + * Requires that the denominator is greater than 0. */ +void +simplify_fraction64(uint64_t *numer, uint64_t *denom) +{ + tor_assert(denom); + uint64_t gcd = gcd64(*numer, *denom); + *numer /= gcd; + *denom /= gcd; +} + /** Return the number of bits set in <b>v</b>. */ int n_bits_set_u8(uint8_t v) |