gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Nathan Binkert
29  */
30 
31 #ifndef __BASE_INTMATH_HH__
32 #define __BASE_INTMATH_HH__
33 
34 #include <cassert>
35 
36 #include "base/logging.hh"
37 #include "base/types.hh"
38 
39 inline uint64_t
40 power(uint32_t n, uint32_t e)
41 {
42  if (e > 20)
43  warn("Warning, power() function is quite slow for large exponents\n");
44 
45  if (e == 0)
46  return 1;
47 
48  uint64_t result = n;
49  uint64_t old_result = 0;
50  for (int x = 1; x < e; x++) {
51  old_result = result;
52  result *= n;
53  if (old_result > result)
54  warn("power() overflowed!\n");
55  }
56  return result;
57 }
58 
59 
60 inline int
61 floorLog2(unsigned x)
62 {
63  assert(x > 0);
64 
65  int y = 0;
66 
67  if (x & 0xffff0000) { y += 16; x >>= 16; }
68  if (x & 0x0000ff00) { y += 8; x >>= 8; }
69  if (x & 0x000000f0) { y += 4; x >>= 4; }
70  if (x & 0x0000000c) { y += 2; x >>= 2; }
71  if (x & 0x00000002) { y += 1; }
72 
73  return y;
74 }
75 
76 inline int
77 floorLog2(unsigned long x)
78 {
79  assert(x > 0);
80 
81  int y = 0;
82 
83 #if defined(__LP64__)
84  if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
85 #endif
86  if (x & 0xffff0000) { y += 16; x >>= 16; }
87  if (x & 0x0000ff00) { y += 8; x >>= 8; }
88  if (x & 0x000000f0) { y += 4; x >>= 4; }
89  if (x & 0x0000000c) { y += 2; x >>= 2; }
90  if (x & 0x00000002) { y += 1; }
91 
92  return y;
93 }
94 
95 inline int
96 floorLog2(unsigned long long x)
97 {
98  assert(x > 0);
99 
100  int y = 0;
101 
102  if (x & ULL(0xffffffff00000000)) { y += 32; x >>= 32; }
103  if (x & ULL(0x00000000ffff0000)) { y += 16; x >>= 16; }
104  if (x & ULL(0x000000000000ff00)) { y += 8; x >>= 8; }
105  if (x & ULL(0x00000000000000f0)) { y += 4; x >>= 4; }
106  if (x & ULL(0x000000000000000c)) { y += 2; x >>= 2; }
107  if (x & ULL(0x0000000000000002)) { y += 1; }
108 
109  return y;
110 }
111 
112 inline int
114 {
115  assert(x > 0);
116  return floorLog2((unsigned)x);
117 }
118 
119 inline int
121 {
122  assert(x > 0);
123  return floorLog2((unsigned long)x);
124 }
125 
126 inline int
127 floorLog2(long long x)
128 {
129  assert(x > 0);
130  return floorLog2((unsigned long long)x);
131 }
132 
133 template <class T>
134 inline int
135 ceilLog2(const T& n)
136 {
137  assert(n > 0);
138  if (n == 1)
139  return 0;
140 
141  return floorLog2(n - (T)1) + 1;
142 }
143 
144 template <class T>
145 inline bool
146 isPowerOf2(const T& n)
147 {
148  return n != 0 && floorLog2(n) == ceilLog2(n);
149 }
150 
151 template <class T, class U>
152 inline T
153 divCeil(const T& a, const U& b)
154 {
155  return (a + b - 1) / b;
156 }
157 
166 template <class T, class U>
167 inline T
168 roundUp(const T& val, const U& align)
169 {
170  assert(isPowerOf2(align));
171  T mask = (T)align - 1;
172  return (val + mask) & ~mask;
173 }
174 
183 template <class T, class U>
184 inline T
185 roundDown(const T& val, const U& align)
186 {
187  assert(isPowerOf2(align));
188  T mask = (T)align - 1;
189  return val & ~mask;
190 }
191 
192 #endif // __BASE_INTMATH_HH__
Bitfield< 8 > a
int ceilLog2(const T &n)
Definition: intmath.hh:135
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:168
Bitfield< 63 > val
Definition: misc.hh:771
Bitfield< 31 > n
Bitfield< 7 > b
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:2051
uint64_t power(uint32_t n, uint32_t e)
Definition: intmath.hh:40
bool isPowerOf2(const T &n)
Definition: intmath.hh:146
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:185
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
#define ULL(N)
uint64_t constant
Definition: types.hh:50
Bitfield< 9 > e
int floorLog2(unsigned x)
Definition: intmath.hh:61
T divCeil(const T &a, const U &b)
Definition: intmath.hh:153
Bitfield< 3, 0 > mask
Definition: types.hh:64
#define warn(...)
Definition: logging.hh:212
Bitfield< 1 > x
Definition: types.hh:105

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13