tests: log_call is not returning any value
[gem5.git] / src / base / bitfield.hh
index 23c8b4b12bbfceac92b30f4dcefcaeceeea99f93..c2ed72be20e8d736d19b6ba0c71512e6247e5349 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited
+ * Copyright (c) 2017, 2019 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Steve Reinhardt
- *          Nathan Binkert
- *          Giacomo Travaglini
  */
 
 #ifndef __BASE_BITFIELD_HH__
 extern const uint8_t reverseLookUpTable[];
 
 /**
- * Generate a 64-bit mask of 'nbits' 1s, right justified.
+ * Generate a 64-bit mask of 'nbits' 1s, right justified. If a number of bits
+ * greater than 64 is given, it is truncated to 64.
+ *
+ * @param nbits The number of bits set in the mask.
  */
 inline uint64_t
 mask(int nbits)
 {
-    return (nbits == 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
+    return (nbits >= 64) ? (uint64_t)-1LL : (1ULL << nbits) - 1;
 }
 
 /**
@@ -117,7 +116,14 @@ sext(uint64_t val)
 }
 
 /**
- * Return val with bits first to last set to bit_val
+ * Returns val with bits first to last set to the LSBs of bit_val
+ *
+ * E.g.:
+ * first: 7
+ * last:  4
+ * val:      0xFFFF
+ * bit_val:  0x0000
+ * returned: 0xFF0F
  */
 template <class T, class B>
 inline
@@ -182,7 +188,7 @@ reverseBits(T val, std::size_t size = sizeof(T))
     assert(size <= sizeof(T));
 
     T output = 0;
-    for (auto byte = 0; byte < size; byte++, val >>= 8) {
+    for (auto byte = 0; byte < size; byte++, val = static_cast<T>(val >> 8)) {
         output = (output << 8) | reverseLookUpTable[val & 0xFF];
     }
 
@@ -282,4 +288,26 @@ inline uint64_t alignToPowerOfTwo(uint64_t val)
     return val;
 };
 
+/**
+ * Count trailing zeros in a 32-bit value.
+ *
+ * @param An input value
+ * @return The number of trailing zeros or 32 if the value is zero.
+ */
+inline int ctz32(uint32_t value)
+{
+    return value ? __builtin_ctzl(value) : 32;
+}
+
+/**
+ * Count trailing zeros in a 64-bit value.
+ *
+ * @param An input value
+ * @return The number of trailing zeros or 64 if the value is zero.
+ */
+inline int ctz64(uint64_t value)
+{
+    return value ? __builtin_ctzll(value) : 64;
+}
+
 #endif // __BASE_BITFIELD_HH__