The Sparta Modeling Framework
Loading...
Searching...
No Matches
Bits.hpp
Go to the documentation of this file.
1// <Bits.hpp> -*- C++ -*-
2#pragma once
3
4#include <cinttypes>
5#include <cassert>
6#include <typeinfo>
7
9
15namespace sparta {
16 namespace utils {
17
18 template <class T>
19 inline uint32_t count_1_bits(const T&)
20 {
21 throw SpartaException("Unsupported type for count_1_bits: ") << typeid(T).name();
22 }
23
24 // Courtesy: Warren, Henry S. (2012-09-25). Hacker's Delight
25 // (2nd Edition)
26 template<>
27 inline uint32_t count_1_bits<uint32_t>(const uint32_t& n)
28 {
29 uint32_t x = n - (( n >> 1) & 0x55555555);
30 x = (x & 0x33333333) + (( x >> 2) & 0x33333333);
31 x = (x + (x >> 4)) & 0x0F0F0F0F;
32 x = x + (x >> 8);
33 x = x + (x >> 16);
34 return x & 0x3F;
35 }
36
37 template<>
38 inline uint32_t count_1_bits<uint64_t>(const uint64_t& n)
39 {
40 uint64_t x = n - (( n >> 1) & 0x5555555555555555ull);
41 x = (x & 0x3333333333333333ull) + (( x >> 2) & 0x3333333333333333ull);
42 x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0Full;
43 x = x + (x >> 8);
44 x = x + (x >> 16);
45 x = x + (x >> 32);
46 return x & 0x7F;
47 }
48
49 } // namespace sparta::utils
50} // namespace sparta
Exception class for all of Sparta.
Macros for handling exponential backoff.