gem5  v20.1.0.0
intmath.hh
Go to the documentation of this file.
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 
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 
61 template <class T>
62 inline typename std::enable_if<std::is_integral<T>::value, int>::type
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 
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 
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 
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 
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 
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__
roundDown
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:150
type
uint8_t type
Definition: inet.hh:421
floorLog2
std::enable_if< std::is_integral< T >::value, int >::type floorLog2(T x)
Definition: intmath.hh:63
sc_dt::align
void align(const scfx_rep &lhs, const scfx_rep &rhs, int &new_wp, int &len_mant, scfx_mant_ref &lhs_mant, scfx_mant_ref &rhs_mant)
Definition: scfx_rep.cc:2083
ArmISA::n
Bitfield< 31 > n
Definition: miscregs_types.hh:450
ArmISA::ts
Bitfield< 55, 52 > ts
Definition: miscregs_types.hh:89
ceilLog2
int ceilLog2(const T &n)
Definition: intmath.hh:88
divCeil
T divCeil(const T &a, const U &b)
Definition: intmath.hh:114
ArmISA::a
Bitfield< 8 > a
Definition: miscregs_types.hh:62
RiscvISA::x
Bitfield< 3 > x
Definition: pagetable.hh:69
X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:769
ArmISA::e
Bitfield< 9 > e
Definition: miscregs_types.hh:61
MipsISA::ux
Bitfield< 5 > ux
Definition: pra_constants.hh:133
warn_if
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition: logging.hh:263
ArmISA::b
Bitfield< 7 > b
Definition: miscregs_types.hh:376
types.hh
roundUp
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:131
logging.hh
power
uint64_t power(uint32_t n, uint32_t e)
Definition: intmath.hh:43
isPowerOf2
bool isPowerOf2(const T &n)
Definition: intmath.hh:102
ULL
#define ULL(N)
uint64_t constant
Definition: types.hh:50
ArmISA::mask
Bitfield< 28, 24 > mask
Definition: miscregs_types.hh:711

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17