misc: Replace enable_if<>::type with enable_if_t<>.
[gem5.git] / src / base / intmath.hh
1 /*
2 * Copyright (c) 2001, 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 #ifndef __BASE_INTMATH_HH__
30 #define __BASE_INTMATH_HH__
31
32 #include <cassert>
33 #include <cstdint>
34 #include <type_traits>
35
36 #include "base/logging.hh"
37 #include "base/types.hh"
38
39 /**
40 * @ingroup api_base_utils
41 */
42 inline uint64_t
43 power(uint32_t n, uint32_t e)
44 {
45 uint64_t result = 1;
46 uint64_t component = n;
47 while (e) {
48 uint64_t last = result;
49 if (e & 0x1)
50 result *= component;
51 warn_if(result < last, "power() overflowed!");
52 e >>= 1;
53 component *= component;
54 }
55 return result;
56 }
57
58 /**
59 * @ingroup api_base_utils
60 */
61 template <class T>
62 inline typename std::enable_if_t<std::is_integral<T>::value, int>
63 floorLog2(T x)
64 {
65 assert(x > 0);
66
67 // A guaranteed unsigned version of x.
68 uint64_t ux = (typename std::make_unsigned<T>::type)x;
69
70 int y = 0;
71 constexpr auto ts = sizeof(T);
72
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; }
79
80 return y;
81 }
82
83 /**
84 * @ingroup api_base_utils
85 */
86 template <class T>
87 inline int
88 ceilLog2(const T& n)
89 {
90 assert(n > 0);
91 if (n == 1)
92 return 0;
93
94 return floorLog2(n - (T)1) + 1;
95 }
96
97 /**
98 * @ingroup api_base_utils
99 */
100 template <class T>
101 inline bool
102 isPowerOf2(const T& n)
103 {
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));
107 }
108
109 /**
110 * @ingroup api_base_utils
111 */
112 template <class T, class U>
113 inline T
114 divCeil(const T& a, const U& b)
115 {
116 return (a + b - 1) / b;
117 }
118
119 /**
120 * This function is used to align addresses in memory.
121 *
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.
126 *
127 * @ingroup api_base_utils
128 */
129 template <class T, class U>
130 inline T
131 roundUp(const T& val, const U& align)
132 {
133 assert(isPowerOf2(align));
134 T mask = (T)align - 1;
135 return (val + mask) & ~mask;
136 }
137
138 /**
139 * This function is used to align addresses in memory.
140 *
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.
145 *
146 * @ingroup api_base_utils
147 */
148 template <class T, class U>
149 inline T
150 roundDown(const T& val, const U& align)
151 {
152 assert(isPowerOf2(align));
153 T mask = (T)align - 1;
154 return val & ~mask;
155 }
156
157 #endif // __BASE_INTMATH_HH__