1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
9 * \brief Integer math related to multiplication, division, and rounding.
12 #include "lib/intmath/muldiv.h"
13 #include "lib/err/torerr.h"
17 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
18 * <b>divisor</b> == 0. If no such x can be expressed as an unsigned, return
19 * UINT_MAX. Asserts if divisor is zero. */
21 round_to_next_multiple_of(unsigned number
, unsigned divisor
)
23 raw_assert(divisor
> 0);
24 if (UINT_MAX
- divisor
+ 1 < number
)
26 number
+= divisor
- 1;
27 number
-= number
% divisor
;
31 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
32 * <b>divisor</b> == 0. If no such x can be expressed as a uint32_t, return
33 * UINT32_MAX. Asserts if divisor is zero. */
35 round_uint32_to_next_multiple_of(uint32_t number
, uint32_t divisor
)
37 raw_assert(divisor
> 0);
38 if (UINT32_MAX
- divisor
+ 1 < number
)
41 number
+= divisor
- 1;
42 number
-= number
% divisor
;
46 /** Return the lowest x such that x is at least <b>number</b>, and x modulo
47 * <b>divisor</b> == 0. If no such x can be expressed as a uint64_t, return
48 * UINT64_MAX. Asserts if divisor is zero. */
50 round_uint64_to_next_multiple_of(uint64_t number
, uint64_t divisor
)
52 raw_assert(divisor
> 0);
53 if (UINT64_MAX
- divisor
+ 1 < number
)
55 number
+= divisor
- 1;
56 number
-= number
% divisor
;
60 /* Helper: return greatest common divisor of a,b */
62 gcd64(uint64_t a
, uint64_t b
)
72 /** Return the unsigned integer product of <b>a</b> and <b>b</b>. If overflow
73 * is detected, return UINT64_MAX instead. */
75 tor_mul_u64_nowrap(uint64_t a
, uint64_t b
)
77 if (a
== 0 || b
== 0) {
79 } else if (PREDICT_UNLIKELY(UINT64_MAX
/ a
< b
)) {
86 /* Given a fraction *<b>numer</b> / *<b>denom</b>, simplify it.
87 * Requires that the denominator is greater than 0. */
89 simplify_fraction64(uint64_t *numer
, uint64_t *denom
)
92 uint64_t gcd
= gcd64(*numer
, *denom
);