2 * Copyright (c) 2001, 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __BASE_INTMATH_HH__
30 #define __BASE_INTMATH_HH__
34 #include <type_traits>
36 #include "base/logging.hh"
37 #include "base/types.hh"
40 * @ingroup api_base_utils
43 power(uint32_t n, uint32_t e)
46 uint64_t component = n;
48 uint64_t last = result;
51 warn_if(result < last, "power() overflowed!");
53 component *= component;
59 * @ingroup api_base_utils
62 inline typename std::enable_if_t<std::is_integral<T>::value, int>
67 // A guaranteed unsigned version of x.
68 uint64_t ux = (typename std::make_unsigned<T>::type)x;
71 constexpr auto ts = sizeof(T);
73 if (ts >= 8 && (ux & ULL(0xffffffff00000000))) { y += 32; ux >>= 32; }
74 if (ts >= 4 && (ux & ULL(0x00000000ffff0000))) { y += 16; ux >>= 16; }
75 if (ts >= 2 && (ux & ULL(0x000000000000ff00))) { y += 8; ux >>= 8; }
76 if (ux & ULL(0x00000000000000f0)) { y += 4; ux >>= 4; }
77 if (ux & ULL(0x000000000000000c)) { y += 2; ux >>= 2; }
78 if (ux & ULL(0x0000000000000002)) { y += 1; }
84 * @ingroup api_base_utils
94 return floorLog2(n - (T)1) + 1;
98 * @ingroup api_base_utils
102 isPowerOf2(const T& n)
104 // If n is non-zero, and subtracting one borrows all the way to the MSB
105 // and flips all bits, then this is a power of 2.
106 return n && !(n & (n - 1));
110 * @ingroup api_base_utils
112 template <class T, class U>
114 divCeil(const T& a, const U& b)
116 return (a + b - 1) / b;
120 * This function is used to align addresses in memory.
122 * @param val is the address to be aligned.
123 * @param align is the alignment. Can only be a power of 2.
124 * @return The aligned address. The smallest number divisible
125 * by @param align which is greater than or equal to @param val.
127 * @ingroup api_base_utils
129 template <class T, class U>
131 roundUp(const T& val, const U& align)
133 assert(isPowerOf2(align));
134 T mask = (T)align - 1;
135 return (val + mask) & ~mask;
139 * This function is used to align addresses in memory.
141 * @param val is the address to be aligned.
142 * @param align is the alignment. Can only be a power of 2.
143 * @return The aligned address. The biggest number divisible
144 * by @param align which is less than or equal to @param val.
146 * @ingroup api_base_utils
148 template <class T, class U>
150 roundDown(const T& val, const U& align)
152 assert(isPowerOf2(align));
153 T mask = (T)align - 1;
157 #endif // __BASE_INTMATH_HH__