summaryrefslogtreecommitdiff
path: root/src/lib/intmath
diff options
context:
space:
mode:
authorJosé M. Guisado <guigom@riseup.net>2019-09-18 12:30:28 +0200
committerJosé M. Guisado <guigom@riseup.net>2019-10-27 10:27:54 +0100
commitf237529fff657bbbf8d2f2632337d9848446d7da (patch)
treef850a8ec3c5445b9489108a9d358cc71a3326508 /src/lib/intmath
parent7225cfc58671aaaeb2e17e7e46bc1f33b87ab22a (diff)
downloadtor-f237529fff657bbbf8d2f2632337d9848446d7da.tar.gz
tor-f237529fff657bbbf8d2f2632337d9848446d7da.zip
Add nowrap u64 multiplication function
Follows #30920 Signed-off-by: José M. Guisado <guigom@riseup.net>
Diffstat (limited to 'src/lib/intmath')
-rw-r--r--src/lib/intmath/muldiv.c14
-rw-r--r--src/lib/intmath/muldiv.h2
2 files changed, 16 insertions, 0 deletions
diff --git a/src/lib/intmath/muldiv.c b/src/lib/intmath/muldiv.c
index 6a292db7ba..3330a4c569 100644
--- a/src/lib/intmath/muldiv.c
+++ b/src/lib/intmath/muldiv.c
@@ -69,6 +69,20 @@ gcd64(uint64_t a, uint64_t b)
return a;
}
+/** Return the unsigned integer product of <b>a</b> and <b>b</b>, if overflow
+ * is detected return UINT64_MAX instead. */
+uint64_t
+tor_mul_u64_nowrap(uint64_t a, uint64_t b)
+{
+ if (a == 0 || b == 0) {
+ return 0;
+ } else if (PREDICT_UNLIKELY(UINT64_MAX / a < b)) {
+ return UINT64_MAX;
+ } else {
+ return a*b;
+ }
+}
+
/* Given a fraction *<b>numer</b> / *<b>denom</b>, simplify it.
* Requires that the denominator is greater than 0. */
void
diff --git a/src/lib/intmath/muldiv.h b/src/lib/intmath/muldiv.h
index 64500b6dce..7aa0f9b235 100644
--- a/src/lib/intmath/muldiv.h
+++ b/src/lib/intmath/muldiv.h
@@ -18,6 +18,8 @@ unsigned round_to_next_multiple_of(unsigned number, unsigned divisor);
uint32_t round_uint32_to_next_multiple_of(uint32_t number, uint32_t divisor);
uint64_t round_uint64_to_next_multiple_of(uint64_t number, uint64_t divisor);
+uint64_t tor_mul_u64_nowrap(uint64_t a, uint64_t b);
+
void simplify_fraction64(uint64_t *numer, uint64_t *denom);
/* Compute the CEIL of <b>a</b> divided by <b>b</b>, for nonnegative <b>a</b>