diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.c | 17 | ||||
-rw-r--r-- | src/common/util.h | 1 |
2 files changed, 17 insertions, 1 deletions
diff --git a/src/common/util.c b/src/common/util.c index c2dfb5432..e79e73a0f 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -333,7 +333,7 @@ tor_mathlog(double d) } /** Return the long integer closest to d. We define this wrapper here so - * that not all users of math.h need to use the right incancations to get + * that not all users of math.h need to use the right intancations to get * the c99 functions. */ long tor_lround(double d) @@ -347,6 +347,21 @@ tor_lround(double d) #endif } +/** Return the 64-bit integer closest to d. We define this wrapper here so + * that not all users of math.h need to use the right incantations to get the + * c99 functions. */ +int64_t +tor_llround(double d) +{ +#if defined(HAVE_LLROUND) + return (int64_t)llround(d); +#elif defined(HAVE_RINT) + return (int64_t)rint(d); +#else + return (int64_t)(d > 0 ? d + 0.5 : ceil(d - 0.5)); +#endif +} + /** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */ int tor_log2(uint64_t u64) diff --git a/src/common/util.h b/src/common/util.h index 3388759b3..04812df7c 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -160,6 +160,7 @@ void tor_log_mallinfo(int severity); /* Math functions */ double tor_mathlog(double d) ATTR_CONST; long tor_lround(double d) ATTR_CONST; +int64_t tor_llround(double d) ATTR_CONST; int tor_log2(uint64_t u64) ATTR_CONST; uint64_t round_to_power_of_2(uint64_t u64); unsigned round_to_next_multiple_of(unsigned number, unsigned divisor); |